blob: 311ded34c2b2b1027dc17e3f8b7d4f9682ba7412 [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/*
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -080033 * Adoptation to use page cache and UFS2 write support by
34 * Evgeniy Dushistov <dushistov@mail.ru>, 2006-2007
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -080035 */
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
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070049#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "swab.h"
51#include "util.h"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * Secure deletion currently doesn't work. It interacts very badly
55 * with buffers shared with memory mappings, and for that reason
56 * can't be done in the truncate() routines. It should instead be
57 * done separately in "release()" before calling the truncate routines
58 * that will release the actual file blocks.
59 *
60 * Linus
61 */
62
63#define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
64#define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -080067static int ufs_trunc_direct(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct ufs_inode_info *ufsi = UFS_I(inode);
70 struct super_block * sb;
71 struct ufs_sb_private_info * uspi;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -080072 void *p;
73 u64 frag1, frag2, frag3, frag4, block1, block2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 unsigned frag_to_free, free_count;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -080075 unsigned i, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int retry;
77
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -080078 UFSD("ENTER: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 sb = inode->i_sb;
81 uspi = UFS_SB(sb)->s_uspi;
82
83 frag_to_free = 0;
84 free_count = 0;
85 retry = 0;
86
87 frag1 = DIRECT_FRAGMENT;
88 frag4 = min_t(u32, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
89 frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
90 frag3 = frag4 & ~uspi->s_fpbmask;
91 block1 = block2 = 0;
92 if (frag2 > frag3) {
93 frag2 = frag4;
94 frag3 = frag4 = 0;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -080095 } else if (frag2 < frag3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 block1 = ufs_fragstoblks (frag2);
97 block2 = ufs_fragstoblks (frag3);
98 }
99
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800100 UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
101 " frag3 %llu, frag4 %llu\n", inode->i_ino,
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800102 (unsigned long long)frag1, (unsigned long long)frag2,
103 (unsigned long long)block1, (unsigned long long)block2,
104 (unsigned long long)frag3, (unsigned long long)frag4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (frag1 >= frag2)
107 goto next1;
108
109 /*
110 * Free first free fragments
111 */
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800112 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
113 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (!tmp )
115 ufs_panic (sb, "ufs_trunc_direct", "internal error");
Evgeniy Dushistov86821642007-01-29 13:19:55 -0800116 frag2 -= frag1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 frag1 = ufs_fragnum (frag1);
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800118
Evgeniy Dushistov86821642007-01-29 13:19:55 -0800119 ufs_free_fragments(inode, tmp + frag1, frag2);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700120 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 frag_to_free = tmp + frag1;
122
123next1:
124 /*
125 * Free whole blocks
126 */
127 for (i = block1 ; i < block2; i++) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800128 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
129 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (!tmp)
131 continue;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800132 ufs_data_ptr_clear(uspi, p);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (free_count == 0) {
135 frag_to_free = tmp;
136 free_count = uspi->s_fpb;
137 } else if (free_count > 0 && frag_to_free == tmp - free_count)
138 free_count += uspi->s_fpb;
139 else {
140 ufs_free_blocks (inode, frag_to_free, free_count);
141 frag_to_free = tmp;
142 free_count = uspi->s_fpb;
143 }
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700144 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
147 if (free_count > 0)
148 ufs_free_blocks (inode, frag_to_free, free_count);
149
150 if (frag3 >= frag4)
151 goto next3;
152
153 /*
154 * Free last free fragments
155 */
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800156 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
157 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (!tmp )
159 ufs_panic(sb, "ufs_truncate_direct", "internal error");
160 frag4 = ufs_fragnum (frag4);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800161 ufs_data_ptr_clear(uspi, p);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 ufs_free_fragments (inode, tmp, frag4);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700164 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 next3:
166
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800167 UFSD("EXIT: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return retry;
169}
170
171
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800172static int ufs_trunc_indirect(struct inode *inode, u64 offset, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct super_block * sb;
175 struct ufs_sb_private_info * uspi;
176 struct ufs_buffer_head * ind_ubh;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800177 void *ind;
178 u64 tmp, indirect_block, i, frag_to_free;
179 unsigned free_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int retry;
181
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800182 UFSD("ENTER: ino %lu, offset %llu, p: %p\n",
183 inode->i_ino, (unsigned long long)offset, p);
184
185 BUG_ON(!p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 sb = inode->i_sb;
188 uspi = UFS_SB(sb)->s_uspi;
189
190 frag_to_free = 0;
191 free_count = 0;
192 retry = 0;
193
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800194 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!tmp)
196 return 0;
197 ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800198 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 ubh_brelse (ind_ubh);
200 return 1;
201 }
202 if (!ind_ubh) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800203 ufs_data_ptr_clear(uspi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return 0;
205 }
206
207 indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
208 for (i = indirect_block; i < uspi->s_apb; i++) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800209 ind = ubh_get_data_ptr(uspi, ind_ubh, i);
210 tmp = ufs_data_ptr_to_cpu(sb, ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!tmp)
212 continue;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800213
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800214 ufs_data_ptr_clear(uspi, ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ubh_mark_buffer_dirty(ind_ubh);
216 if (free_count == 0) {
217 frag_to_free = tmp;
218 free_count = uspi->s_fpb;
219 } else if (free_count > 0 && frag_to_free == tmp - free_count)
220 free_count += uspi->s_fpb;
221 else {
222 ufs_free_blocks (inode, frag_to_free, free_count);
223 frag_to_free = tmp;
224 free_count = uspi->s_fpb;
225 }
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
230 if (free_count > 0) {
231 ufs_free_blocks (inode, frag_to_free, free_count);
232 }
233 for (i = 0; i < uspi->s_apb; i++)
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800234 if (!ufs_is_data_ptr_zero(uspi,
235 ubh_get_data_ptr(uspi, ind_ubh, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 break;
237 if (i >= uspi->s_apb) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800238 tmp = ufs_data_ptr_to_cpu(sb, p);
239 ufs_data_ptr_clear(uspi, p);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700240
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700241 ufs_free_blocks (inode, tmp, uspi->s_fpb);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700242 mark_inode_dirty(inode);
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700243 ubh_bforget(ind_ubh);
244 ind_ubh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700247 ubh_ll_rw_block(SWRITE, ind_ubh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 ubh_wait_on_buffer (ind_ubh);
249 }
250 ubh_brelse (ind_ubh);
251
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800252 UFSD("EXIT: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 return retry;
255}
256
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800257static int ufs_trunc_dindirect(struct inode *inode, u64 offset, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct super_block * sb;
260 struct ufs_sb_private_info * uspi;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800261 struct ufs_buffer_head *dind_bh;
262 u64 i, tmp, dindirect_block;
263 void *dind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 int retry = 0;
265
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800266 UFSD("ENTER: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 sb = inode->i_sb;
269 uspi = UFS_SB(sb)->s_uspi;
270
271 dindirect_block = (DIRECT_BLOCK > offset)
272 ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
273 retry = 0;
274
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800275 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (!tmp)
277 return 0;
278 dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800279 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ubh_brelse (dind_bh);
281 return 1;
282 }
283 if (!dind_bh) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800284 ufs_data_ptr_clear(uspi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286 }
287
288 for (i = dindirect_block ; i < uspi->s_apb ; i++) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800289 dind = ubh_get_data_ptr(uspi, dind_bh, i);
290 tmp = ufs_data_ptr_to_cpu(sb, dind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!tmp)
292 continue;
293 retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
294 ubh_mark_buffer_dirty(dind_bh);
295 }
296
297 for (i = 0; i < uspi->s_apb; i++)
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800298 if (!ufs_is_data_ptr_zero(uspi,
299 ubh_get_data_ptr(uspi, dind_bh, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
301 if (i >= uspi->s_apb) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800302 tmp = ufs_data_ptr_to_cpu(sb, p);
303 ufs_data_ptr_clear(uspi, p);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700304
305 ufs_free_blocks(inode, tmp, uspi->s_fpb);
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700306 mark_inode_dirty(inode);
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700307 ubh_bforget(dind_bh);
308 dind_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700311 ubh_ll_rw_block(SWRITE, dind_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ubh_wait_on_buffer (dind_bh);
313 }
314 ubh_brelse (dind_bh);
315
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800316 UFSD("EXIT: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 return retry;
319}
320
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800321static int ufs_trunc_tindirect(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800323 struct super_block *sb = inode->i_sb;
324 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct ufs_inode_info *ufsi = UFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct ufs_buffer_head * tind_bh;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800327 u64 tindirect_block, tmp, i;
328 void *tind, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int retry;
330
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800331 UFSD("ENTER: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 retry = 0;
334
335 tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
336 ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800337
338 p = ufs_get_direct_data_ptr(uspi, ufsi, UFS_TIND_BLOCK);
339 if (!(tmp = ufs_data_ptr_to_cpu(sb, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341 tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800342 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 ubh_brelse (tind_bh);
344 return 1;
345 }
346 if (!tind_bh) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800347 ufs_data_ptr_clear(uspi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
349 }
350
351 for (i = tindirect_block ; i < uspi->s_apb ; i++) {
Evgeniy Dushistov0465fc02007-03-16 13:38:09 -0800352 tind = ubh_get_data_ptr(uspi, tind_bh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 retry |= ufs_trunc_dindirect(inode, UFS_NDADDR +
354 uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
355 ubh_mark_buffer_dirty(tind_bh);
356 }
357 for (i = 0; i < uspi->s_apb; i++)
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800358 if (!ufs_is_data_ptr_zero(uspi,
359 ubh_get_data_ptr(uspi, tind_bh, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 if (i >= uspi->s_apb) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800362 tmp = ufs_data_ptr_to_cpu(sb, p);
363 ufs_data_ptr_clear(uspi, p);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700364
365 ufs_free_blocks(inode, tmp, uspi->s_fpb);
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700366 mark_inode_dirty(inode);
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700367 ubh_bforget(tind_bh);
368 tind_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370 if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700371 ubh_ll_rw_block(SWRITE, tind_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ubh_wait_on_buffer (tind_bh);
373 }
374 ubh_brelse (tind_bh);
375
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800376 UFSD("EXIT: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return retry;
378}
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700379
380static int ufs_alloc_lastblock(struct inode *inode)
381{
382 int err = 0;
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800383 struct super_block *sb = inode->i_sb;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700384 struct address_space *mapping = inode->i_mapping;
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800385 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800386 unsigned i, end;
387 sector_t lastfrag;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700388 struct page *lastpage;
389 struct buffer_head *bh;
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800390 u64 phys64;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700391
392 lastfrag = (i_size_read(inode) + uspi->s_fsize - 1) >> uspi->s_fshift;
393
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700394 if (!lastfrag)
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700395 goto out;
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700396
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700397 lastfrag--;
398
399 lastpage = ufs_get_locked_page(mapping, lastfrag >>
400 (PAGE_CACHE_SHIFT - inode->i_blkbits));
401 if (IS_ERR(lastpage)) {
402 err = -EIO;
403 goto out;
404 }
405
406 end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
407 bh = page_buffers(lastpage);
408 for (i = 0; i < end; ++i)
409 bh = bh->b_this_page;
410
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700411
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700412 err = ufs_getfrag_block(inode, lastfrag, bh, 1);
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700413
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700414 if (unlikely(err))
415 goto out_unlock;
416
417 if (buffer_new(bh)) {
418 clear_buffer_new(bh);
419 unmap_underlying_metadata(bh->b_bdev,
420 bh->b_blocknr);
421 /*
422 * we do not zeroize fragment, because of
423 * if it maped to hole, it already contains zeroes
424 */
425 set_buffer_uptodate(bh);
426 mark_buffer_dirty(bh);
427 set_page_dirty(lastpage);
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700428 }
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700429
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800430 if (lastfrag >= UFS_IND_FRAGMENT) {
431 end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
432 phys64 = bh->b_blocknr + 1;
433 for (i = 0; i < end; ++i) {
434 bh = sb_getblk(sb, i + phys64);
435 lock_buffer(bh);
436 memset(bh->b_data, 0, sb->s_blocksize);
437 set_buffer_uptodate(bh);
438 mark_buffer_dirty(bh);
439 unlock_buffer(bh);
440 sync_dirty_buffer(bh);
441 brelse(bh);
442 }
443 }
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700444out_unlock:
445 ufs_put_locked_page(lastpage);
446out:
447 return err;
448}
449
450int ufs_truncate(struct inode *inode, loff_t old_i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700453 struct super_block *sb = inode->i_sb;
454 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
455 int retry, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800457 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
458 inode->i_ino, (unsigned long long)i_size_read(inode),
459 (unsigned long long)old_i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700461 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
462 S_ISLNK(inode->i_mode)))
463 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700465 return -EPERM;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800466
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700467 err = ufs_alloc_lastblock(inode);
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700468
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700469 if (err) {
470 i_size_write(inode, old_i_size);
471 goto out;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700472 }
473
474 block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 lock_kernel();
477 while (1) {
478 retry = ufs_trunc_direct(inode);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800479 retry |= ufs_trunc_indirect(inode, UFS_IND_BLOCK,
480 ufs_get_direct_data_ptr(uspi, ufsi,
481 UFS_IND_BLOCK));
482 retry |= ufs_trunc_dindirect(inode, UFS_IND_BLOCK + uspi->s_apb,
483 ufs_get_direct_data_ptr(uspi, ufsi,
484 UFS_DIND_BLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 retry |= ufs_trunc_tindirect (inode);
486 if (!retry)
487 break;
488 if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
489 ufs_sync_inode (inode);
490 blk_run_address_space(inode->i_mapping);
491 yield();
492 }
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700495 ufsi->i_lastfrag = DIRECT_FRAGMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 unlock_kernel();
497 mark_inode_dirty(inode);
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700498out:
499 UFSD("EXIT: err %d\n", err);
500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700502
503
504/*
505 * We don't define our `inode->i_op->truncate', and call it here,
506 * because of:
507 * - there is no way to know old size
508 * - there is no way inform user about error, if it happens in `truncate'
509 */
510static int ufs_setattr(struct dentry *dentry, struct iattr *attr)
511{
512 struct inode *inode = dentry->d_inode;
513 unsigned int ia_valid = attr->ia_valid;
514 int error;
515
516 error = inode_change_ok(inode, attr);
517 if (error)
518 return error;
519
520 if (ia_valid & ATTR_SIZE &&
521 attr->ia_size != i_size_read(inode)) {
522 loff_t old_i_size = inode->i_size;
523 error = vmtruncate(inode, attr->ia_size);
524 if (error)
525 return error;
526 error = ufs_truncate(inode, old_i_size);
527 if (error)
528 return error;
529 }
530 return inode_setattr(inode, attr);
531}
532
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800533const struct inode_operations ufs_file_inode_operations = {
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700534 .setattr = ufs_setattr,
535};