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>
45 #include <linux/sched.h>
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)
66 static int ufs_trunc_direct(
struct inode *
inode)
72 u64 frag1, frag2, frag3, frag4, block1, block2;
73 unsigned frag_to_free, free_count;
80 uspi = UFS_SB(sb)->s_uspi;
94 }
else if (frag2 < frag3) {
99 UFSD(
"ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
100 " frag3 %llu, frag4 %llu\n", inode->
i_ino,
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);
112 tmp = ufs_data_ptr_to_cpu(sb, p);
114 ufs_panic (sb,
"ufs_trunc_direct",
"internal error");
119 mark_inode_dirty(inode);
120 frag_to_free = tmp + frag1;
126 for (i = block1 ; i < block2; i++) {
127 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
128 tmp = ufs_data_ptr_to_cpu(sb, p);
131 ufs_data_ptr_clear(uspi, p);
133 if (free_count == 0) {
135 free_count = uspi->
s_fpb;
136 }
else if (free_count > 0 && frag_to_free == tmp - free_count)
137 free_count += uspi->
s_fpb;
141 free_count = uspi->
s_fpb;
143 mark_inode_dirty(inode);
156 tmp = ufs_data_ptr_to_cpu(sb, p);
158 ufs_panic(sb,
"ufs_truncate_direct",
"internal error");
160 ufs_data_ptr_clear(uspi, p);
163 mark_inode_dirty(inode);
177 u64 tmp, indirect_block,
i, frag_to_free;
181 UFSD(
"ENTER: ino %lu, offset %llu, p: %p\n",
182 inode->
i_ino, (
unsigned long long)offset, p);
187 uspi = UFS_SB(sb)->s_uspi;
193 tmp = ufs_data_ptr_to_cpu(sb, p);
197 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
202 ufs_data_ptr_clear(uspi, p);
207 for (i = indirect_block; i < uspi->
s_apb; i++) {
208 ind = ubh_get_data_ptr(uspi, ind_ubh, i);
209 tmp = ufs_data_ptr_to_cpu(sb, ind);
213 ufs_data_ptr_clear(uspi, ind);
215 if (free_count == 0) {
217 free_count = uspi->
s_fpb;
218 }
else if (free_count > 0 && frag_to_free == tmp - free_count)
219 free_count += uspi->
s_fpb;
223 free_count = uspi->
s_fpb;
226 mark_inode_dirty(inode);
229 if (free_count > 0) {
232 for (i = 0; i < uspi->
s_apb; i++)
233 if (!ufs_is_data_ptr_zero(uspi,
234 ubh_get_data_ptr(uspi, ind_ubh, i)))
236 if (i >= uspi->
s_apb) {
237 tmp = ufs_data_ptr_to_cpu(sb, p);
238 ufs_data_ptr_clear(uspi, p);
241 mark_inode_dirty(inode);
254 static int ufs_trunc_dindirect(
struct inode *inode,
u64 offset,
void *p)
266 uspi = UFS_SB(sb)->s_uspi;
272 tmp = ufs_data_ptr_to_cpu(sb, p);
276 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
281 ufs_data_ptr_clear(uspi, p);
285 for (i = dindirect_block ; i < uspi->
s_apb ; i++) {
286 dind = ubh_get_data_ptr(uspi, dind_bh, i);
287 tmp = ufs_data_ptr_to_cpu(sb, dind);
290 retry |= ufs_trunc_indirect (inode, offset + (i << uspi->
s_apbshift), dind);
294 for (i = 0; i < uspi->
s_apb; i++)
295 if (!ufs_is_data_ptr_zero(uspi,
296 ubh_get_data_ptr(uspi, dind_bh, i)))
298 if (i >= uspi->
s_apb) {
299 tmp = ufs_data_ptr_to_cpu(sb, p);
300 ufs_data_ptr_clear(uspi, p);
303 mark_inode_dirty(inode);
316 static int ufs_trunc_tindirect(
struct inode *inode)
334 if (!(tmp = ufs_data_ptr_to_cpu(sb, p)))
337 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
342 ufs_data_ptr_clear(uspi, p);
346 for (i = tindirect_block ; i < uspi->
s_apb ; i++) {
347 tind = ubh_get_data_ptr(uspi, tind_bh, i);
348 retry |= ufs_trunc_dindirect(inode,
UFS_NDADDR +
352 for (i = 0; i < uspi->
s_apb; i++)
353 if (!ufs_is_data_ptr_zero(uspi,
354 ubh_get_data_ptr(uspi, tind_bh, i)))
356 if (i >= uspi->
s_apb) {
357 tmp = ufs_data_ptr_to_cpu(sb, p);
358 ufs_data_ptr_clear(uspi, p);
361 mark_inode_dirty(inode);
373 static int ufs_alloc_lastblock(
struct inode *inode)
381 struct page *lastpage;
382 struct buffer_head *bh;
394 if (IS_ERR(lastpage)) {
400 bh = page_buffers(lastpage);
401 for (i = 0; i <
end; ++
i)
402 bh = bh->b_this_page;
410 if (buffer_new(bh)) {
411 clear_buffer_new(bh);
418 set_buffer_uptodate(bh);
425 phys64 = bh->b_blocknr + 1;
426 for (i = 0; i <
end; ++
i) {
427 bh = sb_getblk(sb, i + phys64);
430 set_buffer_uptodate(bh);
438 ufs_put_locked_page(lastpage);
450 UFSD(
"ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
451 inode->
i_ino, (
unsigned long long)i_size_read(inode),
452 (
unsigned long long)old_i_size);
460 err = ufs_alloc_lastblock(inode);
463 i_size_write(inode, old_i_size);
470 retry = ufs_trunc_direct(inode);
472 ufs_get_direct_data_ptr(uspi, ufsi,
475 ufs_get_direct_data_ptr(uspi, ufsi,
477 retry |= ufs_trunc_tindirect (inode);
487 mark_inode_dirty(inode);
489 UFSD(
"EXIT: err %d\n", err);
495 struct inode *inode = dentry->
d_inode;
496 unsigned int ia_valid = attr->
ia_valid;
504 loff_t old_i_size = inode->
i_size;
517 mark_inode_dirty(inode);