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 * Adoptation to use page cache and UFS2 write support by
34 * Evgeniy Dushistov <dushistov@mail.ru>, 2006-2007
37 #include <linux/errno.h>
39 #include <linux/fcntl.h>
40 #include <linux/time.h>
41 #include <linux/stat.h>
42 #include <linux/string.h>
43 #include <linux/smp_lock.h>
44 #include <linux/buffer_head.h>
45 #include <linux/blkdev.h>
46 #include <linux/sched.h>
47 #include <linux/quotaops.h>
55 * Secure deletion currently doesn't work. It interacts very badly
56 * with buffers shared with memory mappings, and for that reason
57 * can't be done in the truncate() routines. It should instead be
58 * done separately in "release()" before calling the truncate routines
59 * that will release the actual file blocks.
64 #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
65 #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
68 static int ufs_trunc_direct(struct inode
*inode
)
70 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
71 struct super_block
* sb
;
72 struct ufs_sb_private_info
* uspi
;
74 u64 frag1
, frag2
, frag3
, frag4
, block1
, block2
;
75 unsigned frag_to_free
, free_count
;
79 UFSD("ENTER: ino %lu\n", inode
->i_ino
);
82 uspi
= UFS_SB(sb
)->s_uspi
;
88 frag1
= DIRECT_FRAGMENT
;
89 frag4
= min_t(u32
, UFS_NDIR_FRAGMENT
, ufsi
->i_lastfrag
);
90 frag2
= ((frag1
& uspi
->s_fpbmask
) ? ((frag1
| uspi
->s_fpbmask
) + 1) : frag1
);
91 frag3
= frag4
& ~uspi
->s_fpbmask
;
96 } else if (frag2
< frag3
) {
97 block1
= ufs_fragstoblks (frag2
);
98 block2
= ufs_fragstoblks (frag3
);
101 UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
102 " frag3 %llu, frag4 %llu\n", inode
->i_ino
,
103 (unsigned long long)frag1
, (unsigned long long)frag2
,
104 (unsigned long long)block1
, (unsigned long long)block2
,
105 (unsigned long long)frag3
, (unsigned long long)frag4
);
111 * Free first free fragments
113 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, ufs_fragstoblks(frag1
));
114 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
116 ufs_panic (sb
, "ufs_trunc_direct", "internal error");
118 frag1
= ufs_fragnum (frag1
);
120 ufs_free_fragments(inode
, tmp
+ frag1
, frag2
);
121 mark_inode_dirty(inode
);
122 frag_to_free
= tmp
+ frag1
;
128 for (i
= block1
; i
< block2
; i
++) {
129 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, i
);
130 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
133 ufs_data_ptr_clear(uspi
, p
);
135 if (free_count
== 0) {
137 free_count
= uspi
->s_fpb
;
138 } else if (free_count
> 0 && frag_to_free
== tmp
- free_count
)
139 free_count
+= uspi
->s_fpb
;
141 ufs_free_blocks (inode
, frag_to_free
, free_count
);
143 free_count
= uspi
->s_fpb
;
145 mark_inode_dirty(inode
);
149 ufs_free_blocks (inode
, frag_to_free
, free_count
);
155 * Free last free fragments
157 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, ufs_fragstoblks(frag3
));
158 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
160 ufs_panic(sb
, "ufs_truncate_direct", "internal error");
161 frag4
= ufs_fragnum (frag4
);
162 ufs_data_ptr_clear(uspi
, p
);
164 ufs_free_fragments (inode
, tmp
, frag4
);
165 mark_inode_dirty(inode
);
168 UFSD("EXIT: ino %lu\n", inode
->i_ino
);
173 static int ufs_trunc_indirect(struct inode
*inode
, u64 offset
, void *p
)
175 struct super_block
* sb
;
176 struct ufs_sb_private_info
* uspi
;
177 struct ufs_buffer_head
* ind_ubh
;
179 u64 tmp
, indirect_block
, i
, frag_to_free
;
183 UFSD("ENTER: ino %lu, offset %llu, p: %p\n",
184 inode
->i_ino
, (unsigned long long)offset
, p
);
189 uspi
= UFS_SB(sb
)->s_uspi
;
195 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
198 ind_ubh
= ubh_bread(sb
, tmp
, uspi
->s_bsize
);
199 if (tmp
!= ufs_data_ptr_to_cpu(sb
, p
)) {
200 ubh_brelse (ind_ubh
);
204 ufs_data_ptr_clear(uspi
, p
);
208 indirect_block
= (DIRECT_BLOCK
> offset
) ? (DIRECT_BLOCK
- offset
) : 0;
209 for (i
= indirect_block
; i
< uspi
->s_apb
; i
++) {
210 ind
= ubh_get_data_ptr(uspi
, ind_ubh
, i
);
211 tmp
= ufs_data_ptr_to_cpu(sb
, ind
);
215 ufs_data_ptr_clear(uspi
, ind
);
216 ubh_mark_buffer_dirty(ind_ubh
);
217 if (free_count
== 0) {
219 free_count
= uspi
->s_fpb
;
220 } else if (free_count
> 0 && frag_to_free
== tmp
- free_count
)
221 free_count
+= uspi
->s_fpb
;
223 ufs_free_blocks (inode
, frag_to_free
, free_count
);
225 free_count
= uspi
->s_fpb
;
228 mark_inode_dirty(inode
);
231 if (free_count
> 0) {
232 ufs_free_blocks (inode
, frag_to_free
, free_count
);
234 for (i
= 0; i
< uspi
->s_apb
; i
++)
235 if (!ufs_is_data_ptr_zero(uspi
,
236 ubh_get_data_ptr(uspi
, ind_ubh
, i
)))
238 if (i
>= uspi
->s_apb
) {
239 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
240 ufs_data_ptr_clear(uspi
, p
);
242 ufs_free_blocks (inode
, tmp
, uspi
->s_fpb
);
243 mark_inode_dirty(inode
);
244 ubh_bforget(ind_ubh
);
247 if (IS_SYNC(inode
) && ind_ubh
&& ubh_buffer_dirty(ind_ubh
)) {
248 ubh_ll_rw_block(SWRITE
, ind_ubh
);
249 ubh_wait_on_buffer (ind_ubh
);
251 ubh_brelse (ind_ubh
);
253 UFSD("EXIT: ino %lu\n", inode
->i_ino
);
258 static int ufs_trunc_dindirect(struct inode
*inode
, u64 offset
, void *p
)
260 struct super_block
* sb
;
261 struct ufs_sb_private_info
* uspi
;
262 struct ufs_buffer_head
*dind_bh
;
263 u64 i
, tmp
, dindirect_block
;
267 UFSD("ENTER: ino %lu\n", inode
->i_ino
);
270 uspi
= UFS_SB(sb
)->s_uspi
;
272 dindirect_block
= (DIRECT_BLOCK
> offset
)
273 ? ((DIRECT_BLOCK
- offset
) >> uspi
->s_apbshift
) : 0;
276 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
279 dind_bh
= ubh_bread(sb
, tmp
, uspi
->s_bsize
);
280 if (tmp
!= ufs_data_ptr_to_cpu(sb
, p
)) {
281 ubh_brelse (dind_bh
);
285 ufs_data_ptr_clear(uspi
, p
);
289 for (i
= dindirect_block
; i
< uspi
->s_apb
; i
++) {
290 dind
= ubh_get_data_ptr(uspi
, dind_bh
, i
);
291 tmp
= ufs_data_ptr_to_cpu(sb
, dind
);
294 retry
|= ufs_trunc_indirect (inode
, offset
+ (i
<< uspi
->s_apbshift
), dind
);
295 ubh_mark_buffer_dirty(dind_bh
);
298 for (i
= 0; i
< uspi
->s_apb
; i
++)
299 if (!ufs_is_data_ptr_zero(uspi
,
300 ubh_get_data_ptr(uspi
, dind_bh
, i
)))
302 if (i
>= uspi
->s_apb
) {
303 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
304 ufs_data_ptr_clear(uspi
, p
);
306 ufs_free_blocks(inode
, tmp
, uspi
->s_fpb
);
307 mark_inode_dirty(inode
);
308 ubh_bforget(dind_bh
);
311 if (IS_SYNC(inode
) && dind_bh
&& ubh_buffer_dirty(dind_bh
)) {
312 ubh_ll_rw_block(SWRITE
, dind_bh
);
313 ubh_wait_on_buffer (dind_bh
);
315 ubh_brelse (dind_bh
);
317 UFSD("EXIT: ino %lu\n", inode
->i_ino
);
322 static int ufs_trunc_tindirect(struct inode
*inode
)
324 struct super_block
*sb
= inode
->i_sb
;
325 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
326 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
327 struct ufs_buffer_head
* tind_bh
;
328 u64 tindirect_block
, tmp
, i
;
332 UFSD("ENTER: ino %lu\n", inode
->i_ino
);
336 tindirect_block
= (DIRECT_BLOCK
> (UFS_NDADDR
+ uspi
->s_apb
+ uspi
->s_2apb
))
337 ? ((DIRECT_BLOCK
- UFS_NDADDR
- uspi
->s_apb
- uspi
->s_2apb
) >> uspi
->s_2apbshift
) : 0;
339 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, UFS_TIND_BLOCK
);
340 if (!(tmp
= ufs_data_ptr_to_cpu(sb
, p
)))
342 tind_bh
= ubh_bread (sb
, tmp
, uspi
->s_bsize
);
343 if (tmp
!= ufs_data_ptr_to_cpu(sb
, p
)) {
344 ubh_brelse (tind_bh
);
348 ufs_data_ptr_clear(uspi
, p
);
352 for (i
= tindirect_block
; i
< uspi
->s_apb
; i
++) {
353 tind
= ubh_get_data_ptr(uspi
, tind_bh
, i
);
354 retry
|= ufs_trunc_dindirect(inode
, UFS_NDADDR
+
355 uspi
->s_apb
+ ((i
+ 1) << uspi
->s_2apbshift
), tind
);
356 ubh_mark_buffer_dirty(tind_bh
);
358 for (i
= 0; i
< uspi
->s_apb
; i
++)
359 if (!ufs_is_data_ptr_zero(uspi
,
360 ubh_get_data_ptr(uspi
, tind_bh
, i
)))
362 if (i
>= uspi
->s_apb
) {
363 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
364 ufs_data_ptr_clear(uspi
, p
);
366 ufs_free_blocks(inode
, tmp
, uspi
->s_fpb
);
367 mark_inode_dirty(inode
);
368 ubh_bforget(tind_bh
);
371 if (IS_SYNC(inode
) && tind_bh
&& ubh_buffer_dirty(tind_bh
)) {
372 ubh_ll_rw_block(SWRITE
, tind_bh
);
373 ubh_wait_on_buffer (tind_bh
);
375 ubh_brelse (tind_bh
);
377 UFSD("EXIT: ino %lu\n", inode
->i_ino
);
381 static int ufs_alloc_lastblock(struct inode
*inode
)
384 struct super_block
*sb
= inode
->i_sb
;
385 struct address_space
*mapping
= inode
->i_mapping
;
386 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
389 struct page
*lastpage
;
390 struct buffer_head
*bh
;
393 lastfrag
= (i_size_read(inode
) + uspi
->s_fsize
- 1) >> uspi
->s_fshift
;
400 lastpage
= ufs_get_locked_page(mapping
, lastfrag
>>
401 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
));
402 if (IS_ERR(lastpage
)) {
407 end
= lastfrag
& ((1 << (PAGE_CACHE_SHIFT
- inode
->i_blkbits
)) - 1);
408 bh
= page_buffers(lastpage
);
409 for (i
= 0; i
< end
; ++i
)
410 bh
= bh
->b_this_page
;
413 err
= ufs_getfrag_block(inode
, lastfrag
, bh
, 1);
418 if (buffer_new(bh
)) {
419 clear_buffer_new(bh
);
420 unmap_underlying_metadata(bh
->b_bdev
,
423 * we do not zeroize fragment, because of
424 * if it maped to hole, it already contains zeroes
426 set_buffer_uptodate(bh
);
427 mark_buffer_dirty(bh
);
428 set_page_dirty(lastpage
);
431 if (lastfrag
>= UFS_IND_FRAGMENT
) {
432 end
= uspi
->s_fpb
- ufs_fragnum(lastfrag
) - 1;
433 phys64
= bh
->b_blocknr
+ 1;
434 for (i
= 0; i
< end
; ++i
) {
435 bh
= sb_getblk(sb
, i
+ phys64
);
437 memset(bh
->b_data
, 0, sb
->s_blocksize
);
438 set_buffer_uptodate(bh
);
439 mark_buffer_dirty(bh
);
441 sync_dirty_buffer(bh
);
446 ufs_put_locked_page(lastpage
);
451 int ufs_truncate(struct inode
*inode
, loff_t old_i_size
)
453 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
454 struct super_block
*sb
= inode
->i_sb
;
455 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
458 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
459 inode
->i_ino
, (unsigned long long)i_size_read(inode
),
460 (unsigned long long)old_i_size
);
462 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
463 S_ISLNK(inode
->i_mode
)))
465 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
468 err
= ufs_alloc_lastblock(inode
);
471 i_size_write(inode
, old_i_size
);
475 block_truncate_page(inode
->i_mapping
, inode
->i_size
, ufs_getfrag_block
);
479 retry
= ufs_trunc_direct(inode
);
480 retry
|= ufs_trunc_indirect(inode
, UFS_IND_BLOCK
,
481 ufs_get_direct_data_ptr(uspi
, ufsi
,
483 retry
|= ufs_trunc_dindirect(inode
, UFS_IND_BLOCK
+ uspi
->s_apb
,
484 ufs_get_direct_data_ptr(uspi
, ufsi
,
486 retry
|= ufs_trunc_tindirect (inode
);
489 if (IS_SYNC(inode
) && (inode
->i_state
& I_DIRTY
))
490 ufs_sync_inode (inode
);
491 blk_run_address_space(inode
->i_mapping
);
495 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
496 ufsi
->i_lastfrag
= DIRECT_FRAGMENT
;
498 mark_inode_dirty(inode
);
500 UFSD("EXIT: err %d\n", err
);
506 * We don't define our `inode->i_op->truncate', and call it here,
508 * - there is no way to know old size
509 * - there is no way inform user about error, if it happens in `truncate'
511 static int ufs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
513 struct inode
*inode
= dentry
->d_inode
;
514 unsigned int ia_valid
= attr
->ia_valid
;
517 error
= inode_change_ok(inode
, attr
);
521 if ((ia_valid
& ATTR_UID
&& attr
->ia_uid
!= inode
->i_uid
) ||
522 (ia_valid
& ATTR_GID
&& attr
->ia_gid
!= inode
->i_gid
)) {
523 error
= dquot_transfer(inode
, attr
);
527 if (ia_valid
& ATTR_SIZE
&&
528 attr
->ia_size
!= i_size_read(inode
)) {
529 loff_t old_i_size
= inode
->i_size
;
531 dquot_initialize(inode
);
533 error
= vmtruncate(inode
, attr
->ia_size
);
536 error
= ufs_truncate(inode
, old_i_size
);
540 return inode_setattr(inode
, attr
);
543 const struct inode_operations ufs_file_inode_operations
= {
544 .setattr
= ufs_setattr
,