]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ufs/truncate.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6
[mirror_ubuntu-bionic-kernel.git] / fs / ufs / truncate.c
CommitLineData
1da177e4
LT
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
09114eb8 32/*
54fb996a
ED
33 * Adoptation to use page cache and UFS2 write support by
34 * Evgeniy Dushistov <dushistov@mail.ru>, 2006-2007
09114eb8
ED
35 */
36
1da177e4
LT
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
1da177e4
LT
52/*
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
1da177e4 65
54fb996a 66static int ufs_trunc_direct(struct inode *inode)
1da177e4
LT
67{
68 struct ufs_inode_info *ufsi = UFS_I(inode);
69 struct super_block * sb;
70 struct ufs_sb_private_info * uspi;
54fb996a
ED
71 void *p;
72 u64 frag1, frag2, frag3, frag4, block1, block2;
1da177e4 73 unsigned frag_to_free, free_count;
09114eb8 74 unsigned i, tmp;
1da177e4
LT
75 int retry;
76
abf5d15f 77 UFSD("ENTER\n");
1da177e4
LT
78
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;
54fb996a 94 } else if (frag2 < frag3) {
1da177e4
LT
95 block1 = ufs_fragstoblks (frag2);
96 block2 = ufs_fragstoblks (frag3);
97 }
98
54fb996a
ED
99 UFSD("frag1 %llu, frag2 %llu, block1 %llu, block2 %llu, frag3 %llu,"
100 " frag4 %llu\n",
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);
1da177e4
LT
104
105 if (frag1 >= frag2)
106 goto next1;
107
108 /*
109 * Free first free fragments
110 */
54fb996a
ED
111 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
112 tmp = ufs_data_ptr_to_cpu(sb, p);
1da177e4
LT
113 if (!tmp )
114 ufs_panic (sb, "ufs_trunc_direct", "internal error");
8682164a 115 frag2 -= frag1;
1da177e4 116 frag1 = ufs_fragnum (frag1);
09114eb8 117
8682164a 118 ufs_free_fragments(inode, tmp + frag1, frag2);
50aa4eb0 119 mark_inode_dirty(inode);
1da177e4
LT
120 frag_to_free = tmp + frag1;
121
122next1:
123 /*
124 * Free whole blocks
125 */
126 for (i = block1 ; i < block2; i++) {
54fb996a
ED
127 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
128 tmp = ufs_data_ptr_to_cpu(sb, p);
1da177e4
LT
129 if (!tmp)
130 continue;
54fb996a 131 ufs_data_ptr_clear(uspi, p);
50aa4eb0 132
1da177e4
LT
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 }
50aa4eb0 143 mark_inode_dirty(inode);
1da177e4
LT
144 }
145
146 if (free_count > 0)
147 ufs_free_blocks (inode, frag_to_free, free_count);
148
149 if (frag3 >= frag4)
150 goto next3;
151
152 /*
153 * Free last free fragments
154 */
54fb996a
ED
155 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
156 tmp = ufs_data_ptr_to_cpu(sb, p);
1da177e4
LT
157 if (!tmp )
158 ufs_panic(sb, "ufs_truncate_direct", "internal error");
159 frag4 = ufs_fragnum (frag4);
54fb996a 160 ufs_data_ptr_clear(uspi, p);
50aa4eb0 161
1da177e4 162 ufs_free_fragments (inode, tmp, frag4);
50aa4eb0 163 mark_inode_dirty(inode);
1da177e4
LT
164 next3:
165
abf5d15f 166 UFSD("EXIT\n");
1da177e4
LT
167 return retry;
168}
169
170
54fb996a 171static int ufs_trunc_indirect(struct inode *inode, u64 offset, void *p)
1da177e4
LT
172{
173 struct super_block * sb;
174 struct ufs_sb_private_info * uspi;
175 struct ufs_buffer_head * ind_ubh;
54fb996a
ED
176 void *ind;
177 u64 tmp, indirect_block, i, frag_to_free;
178 unsigned free_count;
1da177e4
LT
179 int retry;
180
54fb996a
ED
181 UFSD("ENTER: ino %lu, offset %llu, p: %p\n",
182 inode->i_ino, (unsigned long long)offset, p);
183
184 BUG_ON(!p);
1da177e4
LT
185
186 sb = inode->i_sb;
187 uspi = UFS_SB(sb)->s_uspi;
188
189 frag_to_free = 0;
190 free_count = 0;
191 retry = 0;
192
54fb996a 193 tmp = ufs_data_ptr_to_cpu(sb, p);
1da177e4
LT
194 if (!tmp)
195 return 0;
196 ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
54fb996a 197 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
1da177e4
LT
198 ubh_brelse (ind_ubh);
199 return 1;
200 }
201 if (!ind_ubh) {
54fb996a 202 ufs_data_ptr_clear(uspi, p);
1da177e4
LT
203 return 0;
204 }
205
206 indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
207 for (i = indirect_block; i < uspi->s_apb; i++) {
54fb996a
ED
208 ind = ubh_get_data_ptr(uspi, ind_ubh, i);
209 tmp = ufs_data_ptr_to_cpu(sb, ind);
1da177e4
LT
210 if (!tmp)
211 continue;
09114eb8 212
54fb996a 213 ufs_data_ptr_clear(uspi, ind);
1da177e4
LT
214 ubh_mark_buffer_dirty(ind_ubh);
215 if (free_count == 0) {
216 frag_to_free = tmp;
217 free_count = uspi->s_fpb;
218 } else if (free_count > 0 && frag_to_free == tmp - free_count)
219 free_count += uspi->s_fpb;
220 else {
221 ufs_free_blocks (inode, frag_to_free, free_count);
222 frag_to_free = tmp;
223 free_count = uspi->s_fpb;
224 }
50aa4eb0 225
1da177e4 226 mark_inode_dirty(inode);
1da177e4
LT
227 }
228
229 if (free_count > 0) {
230 ufs_free_blocks (inode, frag_to_free, free_count);
231 }
232 for (i = 0; i < uspi->s_apb; i++)
54fb996a
ED
233 if (!ufs_is_data_ptr_zero(uspi,
234 ubh_get_data_ptr(uspi, ind_ubh, i)))
1da177e4
LT
235 break;
236 if (i >= uspi->s_apb) {
54fb996a
ED
237 tmp = ufs_data_ptr_to_cpu(sb, p);
238 ufs_data_ptr_clear(uspi, p);
50aa4eb0 239
2061df0f 240 ufs_free_blocks (inode, tmp, uspi->s_fpb);
50aa4eb0 241 mark_inode_dirty(inode);
2061df0f
ED
242 ubh_bforget(ind_ubh);
243 ind_ubh = NULL;
1da177e4
LT
244 }
245 if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) {
098d5af7 246 ubh_ll_rw_block(SWRITE, ind_ubh);
1da177e4
LT
247 ubh_wait_on_buffer (ind_ubh);
248 }
249 ubh_brelse (ind_ubh);
250
abf5d15f 251 UFSD("EXIT\n");
1da177e4
LT
252
253 return retry;
254}
255
54fb996a 256static int ufs_trunc_dindirect(struct inode *inode, u64 offset, void *p)
1da177e4
LT
257{
258 struct super_block * sb;
259 struct ufs_sb_private_info * uspi;
54fb996a
ED
260 struct ufs_buffer_head *dind_bh;
261 u64 i, tmp, dindirect_block;
262 void *dind;
1da177e4
LT
263 int retry = 0;
264
abf5d15f 265 UFSD("ENTER\n");
1da177e4
LT
266
267 sb = inode->i_sb;
268 uspi = UFS_SB(sb)->s_uspi;
269
270 dindirect_block = (DIRECT_BLOCK > offset)
271 ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
272 retry = 0;
273
54fb996a 274 tmp = ufs_data_ptr_to_cpu(sb, p);
1da177e4
LT
275 if (!tmp)
276 return 0;
277 dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
54fb996a 278 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
1da177e4
LT
279 ubh_brelse (dind_bh);
280 return 1;
281 }
282 if (!dind_bh) {
54fb996a 283 ufs_data_ptr_clear(uspi, p);
1da177e4
LT
284 return 0;
285 }
286
287 for (i = dindirect_block ; i < uspi->s_apb ; i++) {
54fb996a
ED
288 dind = ubh_get_data_ptr(uspi, dind_bh, i);
289 tmp = ufs_data_ptr_to_cpu(sb, dind);
1da177e4
LT
290 if (!tmp)
291 continue;
292 retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
293 ubh_mark_buffer_dirty(dind_bh);
294 }
295
296 for (i = 0; i < uspi->s_apb; i++)
54fb996a
ED
297 if (!ufs_is_data_ptr_zero(uspi,
298 ubh_get_data_ptr(uspi, dind_bh, i)))
1da177e4
LT
299 break;
300 if (i >= uspi->s_apb) {
54fb996a
ED
301 tmp = ufs_data_ptr_to_cpu(sb, p);
302 ufs_data_ptr_clear(uspi, p);
50aa4eb0
ED
303
304 ufs_free_blocks(inode, tmp, uspi->s_fpb);
2061df0f 305 mark_inode_dirty(inode);
2061df0f
ED
306 ubh_bforget(dind_bh);
307 dind_bh = NULL;
1da177e4
LT
308 }
309 if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) {
098d5af7 310 ubh_ll_rw_block(SWRITE, dind_bh);
1da177e4
LT
311 ubh_wait_on_buffer (dind_bh);
312 }
313 ubh_brelse (dind_bh);
314
abf5d15f 315 UFSD("EXIT\n");
1da177e4
LT
316
317 return retry;
318}
319
54fb996a 320static int ufs_trunc_tindirect(struct inode *inode)
1da177e4 321{
54fb996a
ED
322 struct super_block *sb = inode->i_sb;
323 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1da177e4 324 struct ufs_inode_info *ufsi = UFS_I(inode);
1da177e4 325 struct ufs_buffer_head * tind_bh;
54fb996a
ED
326 u64 tindirect_block, tmp, i;
327 void *tind, *p;
1da177e4
LT
328 int retry;
329
abf5d15f 330 UFSD("ENTER\n");
1da177e4 331
1da177e4
LT
332 retry = 0;
333
334 tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
335 ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
54fb996a
ED
336
337 p = ufs_get_direct_data_ptr(uspi, ufsi, UFS_TIND_BLOCK);
338 if (!(tmp = ufs_data_ptr_to_cpu(sb, p)))
1da177e4
LT
339 return 0;
340 tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
54fb996a 341 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
1da177e4
LT
342 ubh_brelse (tind_bh);
343 return 1;
344 }
345 if (!tind_bh) {
54fb996a 346 ufs_data_ptr_clear(uspi, p);
1da177e4
LT
347 return 0;
348 }
349
350 for (i = tindirect_block ; i < uspi->s_apb ; i++) {
351 tind = ubh_get_addr32 (tind_bh, i);
352 retry |= ufs_trunc_dindirect(inode, UFS_NDADDR +
353 uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
354 ubh_mark_buffer_dirty(tind_bh);
355 }
356 for (i = 0; i < uspi->s_apb; i++)
54fb996a
ED
357 if (!ufs_is_data_ptr_zero(uspi,
358 ubh_get_data_ptr(uspi, tind_bh, i)))
1da177e4
LT
359 break;
360 if (i >= uspi->s_apb) {
54fb996a
ED
361 tmp = ufs_data_ptr_to_cpu(sb, p);
362 ufs_data_ptr_clear(uspi, p);
50aa4eb0
ED
363
364 ufs_free_blocks(inode, tmp, uspi->s_fpb);
2061df0f 365 mark_inode_dirty(inode);
2061df0f
ED
366 ubh_bforget(tind_bh);
367 tind_bh = NULL;
1da177e4
LT
368 }
369 if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) {
098d5af7 370 ubh_ll_rw_block(SWRITE, tind_bh);
1da177e4
LT
371 ubh_wait_on_buffer (tind_bh);
372 }
373 ubh_brelse (tind_bh);
374
abf5d15f 375 UFSD("EXIT\n");
1da177e4
LT
376 return retry;
377}
10e5dce0
ED
378
379static int ufs_alloc_lastblock(struct inode *inode)
1da177e4 380{
10e5dce0
ED
381 int err = 0;
382 struct address_space *mapping = inode->i_mapping;
383 struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi;
54fb996a
ED
384 unsigned i, end;
385 sector_t lastfrag;
10e5dce0
ED
386 struct page *lastpage;
387 struct buffer_head *bh;
388
389 lastfrag = (i_size_read(inode) + uspi->s_fsize - 1) >> uspi->s_fshift;
390
ecdc6394 391 if (!lastfrag)
10e5dce0 392 goto out;
ecdc6394 393
10e5dce0
ED
394 lastfrag--;
395
396 lastpage = ufs_get_locked_page(mapping, lastfrag >>
397 (PAGE_CACHE_SHIFT - inode->i_blkbits));
398 if (IS_ERR(lastpage)) {
399 err = -EIO;
400 goto out;
401 }
402
403 end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
404 bh = page_buffers(lastpage);
405 for (i = 0; i < end; ++i)
406 bh = bh->b_this_page;
407
ecdc6394
ED
408
409 err = ufs_getfrag_block(inode, lastfrag, bh, 1);
410
411 if (unlikely(err))
412 goto out_unlock;
413
414 if (buffer_new(bh)) {
415 clear_buffer_new(bh);
416 unmap_underlying_metadata(bh->b_bdev,
417 bh->b_blocknr);
418 /*
419 * we do not zeroize fragment, because of
420 * if it maped to hole, it already contains zeroes
421 */
422 set_buffer_uptodate(bh);
423 mark_buffer_dirty(bh);
424 set_page_dirty(lastpage);
10e5dce0 425 }
ecdc6394 426
10e5dce0
ED
427out_unlock:
428 ufs_put_locked_page(lastpage);
429out:
430 return err;
431}
432
433int ufs_truncate(struct inode *inode, loff_t old_i_size)
434{
435 struct ufs_inode_info *ufsi = UFS_I(inode);
436 struct super_block *sb = inode->i_sb;
437 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
438 int retry, err = 0;
1da177e4 439
54fb996a
ED
440 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
441 inode->i_ino, (unsigned long long)i_size_read(inode),
442 (unsigned long long)old_i_size);
1da177e4 443
10e5dce0
ED
444 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
445 S_ISLNK(inode->i_mode)))
446 return -EINVAL;
1da177e4 447 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
10e5dce0
ED
448 return -EPERM;
449
ecdc6394 450 err = ufs_alloc_lastblock(inode);
10e5dce0 451
ecdc6394
ED
452 if (err) {
453 i_size_write(inode, old_i_size);
454 goto out;
10e5dce0 455 }
09114eb8 456
10e5dce0 457 block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
09114eb8 458
1da177e4
LT
459 lock_kernel();
460 while (1) {
461 retry = ufs_trunc_direct(inode);
54fb996a
ED
462 retry |= ufs_trunc_indirect(inode, UFS_IND_BLOCK,
463 ufs_get_direct_data_ptr(uspi, ufsi,
464 UFS_IND_BLOCK));
465 retry |= ufs_trunc_dindirect(inode, UFS_IND_BLOCK + uspi->s_apb,
466 ufs_get_direct_data_ptr(uspi, ufsi,
467 UFS_DIND_BLOCK));
1da177e4
LT
468 retry |= ufs_trunc_tindirect (inode);
469 if (!retry)
470 break;
471 if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
472 ufs_sync_inode (inode);
473 blk_run_address_space(inode->i_mapping);
474 yield();
475 }
09114eb8 476
1da177e4 477 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
ecdc6394 478 ufsi->i_lastfrag = DIRECT_FRAGMENT;
1da177e4
LT
479 unlock_kernel();
480 mark_inode_dirty(inode);
10e5dce0
ED
481out:
482 UFSD("EXIT: err %d\n", err);
483 return err;
1da177e4 484}
10e5dce0
ED
485
486
487/*
488 * We don't define our `inode->i_op->truncate', and call it here,
489 * because of:
490 * - there is no way to know old size
491 * - there is no way inform user about error, if it happens in `truncate'
492 */
493static int ufs_setattr(struct dentry *dentry, struct iattr *attr)
494{
495 struct inode *inode = dentry->d_inode;
496 unsigned int ia_valid = attr->ia_valid;
497 int error;
498
499 error = inode_change_ok(inode, attr);
500 if (error)
501 return error;
502
503 if (ia_valid & ATTR_SIZE &&
504 attr->ia_size != i_size_read(inode)) {
505 loff_t old_i_size = inode->i_size;
506 error = vmtruncate(inode, attr->ia_size);
507 if (error)
508 return error;
509 error = ufs_truncate(inode, old_i_size);
510 if (error)
511 return error;
512 }
513 return inode_setattr(inode, attr);
514}
515
c5ef1c42 516const struct inode_operations ufs_file_inode_operations = {
10e5dce0
ED
517 .setattr = ufs_setattr,
518};