blob: 79c54c85fb58e8cd937da6fa1e8b46505e26908b [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
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
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -080066static int ufs_trunc_direct(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 struct ufs_inode_info *ufsi = UFS_I(inode);
69 struct super_block * sb;
70 struct ufs_sb_private_info * uspi;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -080071 void *p;
72 u64 frag1, frag2, frag3, frag4, block1, block2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 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 Dushistov4b25a372007-03-16 13:38:09 -080077 UFSD("ENTER: ino %lu\n", inode->i_ino);
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;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -080094 } else if (frag2 < frag3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 block1 = ufs_fragstoblks (frag2);
96 block2 = ufs_fragstoblks (frag3);
97 }
98
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -080099 UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
100 " frag3 %llu, frag4 %llu\n", inode->i_ino,
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800101 (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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 if (frag1 >= frag2)
106 goto next1;
107
108 /*
109 * Free first free fragments
110 */
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800111 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
112 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (!tmp )
114 ufs_panic (sb, "ufs_trunc_direct", "internal error");
Evgeniy Dushistov86821642007-01-29 13:19:55 -0800115 frag2 -= frag1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 frag1 = ufs_fragnum (frag1);
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800117
Evgeniy Dushistov86821642007-01-29 13:19:55 -0800118 ufs_free_fragments(inode, tmp + frag1, frag2);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700119 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 frag_to_free = tmp + frag1;
121
122next1:
123 /*
124 * Free whole blocks
125 */
126 for (i = block1 ; i < block2; i++) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800127 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
128 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (!tmp)
130 continue;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800131 ufs_data_ptr_clear(uspi, p);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 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 }
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700143 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
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 */
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800155 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
156 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!tmp )
158 ufs_panic(sb, "ufs_truncate_direct", "internal error");
159 frag4 = ufs_fragnum (frag4);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800160 ufs_data_ptr_clear(uspi, p);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 ufs_free_fragments (inode, tmp, frag4);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700163 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 next3:
165
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800166 UFSD("EXIT: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return retry;
168}
169
170
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800171static int ufs_trunc_indirect(struct inode *inode, u64 offset, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct super_block * sb;
174 struct ufs_sb_private_info * uspi;
175 struct ufs_buffer_head * ind_ubh;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800176 void *ind;
177 u64 tmp, indirect_block, i, frag_to_free;
178 unsigned free_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int retry;
180
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800181 UFSD("ENTER: ino %lu, offset %llu, p: %p\n",
182 inode->i_ino, (unsigned long long)offset, p);
183
184 BUG_ON(!p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
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
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800193 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (!tmp)
195 return 0;
196 ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800197 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ubh_brelse (ind_ubh);
199 return 1;
200 }
201 if (!ind_ubh) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800202 ufs_data_ptr_clear(uspi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return 0;
204 }
205
206 indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
207 for (i = indirect_block; i < uspi->s_apb; i++) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800208 ind = ubh_get_data_ptr(uspi, ind_ubh, i);
209 tmp = ufs_data_ptr_to_cpu(sb, ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (!tmp)
211 continue;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800212
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800213 ufs_data_ptr_clear(uspi, ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 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 }
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
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++)
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800233 if (!ufs_is_data_ptr_zero(uspi,
234 ubh_get_data_ptr(uspi, ind_ubh, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 break;
236 if (i >= uspi->s_apb) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800237 tmp = ufs_data_ptr_to_cpu(sb, p);
238 ufs_data_ptr_clear(uspi, p);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700239
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700240 ufs_free_blocks (inode, tmp, uspi->s_fpb);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700241 mark_inode_dirty(inode);
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700242 ubh_bforget(ind_ubh);
243 ind_ubh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700246 ubh_ll_rw_block(SWRITE, ind_ubh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 ubh_wait_on_buffer (ind_ubh);
248 }
249 ubh_brelse (ind_ubh);
250
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800251 UFSD("EXIT: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 return retry;
254}
255
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800256static int ufs_trunc_dindirect(struct inode *inode, u64 offset, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct super_block * sb;
259 struct ufs_sb_private_info * uspi;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800260 struct ufs_buffer_head *dind_bh;
261 u64 i, tmp, dindirect_block;
262 void *dind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 int retry = 0;
264
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800265 UFSD("ENTER: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
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
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800274 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!tmp)
276 return 0;
277 dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800278 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ubh_brelse (dind_bh);
280 return 1;
281 }
282 if (!dind_bh) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800283 ufs_data_ptr_clear(uspi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285 }
286
287 for (i = dindirect_block ; i < uspi->s_apb ; i++) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800288 dind = ubh_get_data_ptr(uspi, dind_bh, i);
289 tmp = ufs_data_ptr_to_cpu(sb, dind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 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++)
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800297 if (!ufs_is_data_ptr_zero(uspi,
298 ubh_get_data_ptr(uspi, dind_bh, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 break;
300 if (i >= uspi->s_apb) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800301 tmp = ufs_data_ptr_to_cpu(sb, p);
302 ufs_data_ptr_clear(uspi, p);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700303
304 ufs_free_blocks(inode, tmp, uspi->s_fpb);
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700305 mark_inode_dirty(inode);
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700306 ubh_bforget(dind_bh);
307 dind_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309 if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700310 ubh_ll_rw_block(SWRITE, dind_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 ubh_wait_on_buffer (dind_bh);
312 }
313 ubh_brelse (dind_bh);
314
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800315 UFSD("EXIT: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 return retry;
318}
319
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800320static int ufs_trunc_tindirect(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800322 struct super_block *sb = inode->i_sb;
323 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct ufs_inode_info *ufsi = UFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct ufs_buffer_head * tind_bh;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800326 u64 tindirect_block, tmp, i;
327 void *tind, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 int retry;
329
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800330 UFSD("ENTER: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 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;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800336
337 p = ufs_get_direct_data_ptr(uspi, ufsi, UFS_TIND_BLOCK);
338 if (!(tmp = ufs_data_ptr_to_cpu(sb, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
340 tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800341 if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 ubh_brelse (tind_bh);
343 return 1;
344 }
345 if (!tind_bh) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800346 ufs_data_ptr_clear(uspi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348 }
349
350 for (i = tindirect_block ; i < uspi->s_apb ; i++) {
Evgeniy Dushistov0465fc02007-03-16 13:38:09 -0800351 tind = ubh_get_data_ptr(uspi, tind_bh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 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++)
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800357 if (!ufs_is_data_ptr_zero(uspi,
358 ubh_get_data_ptr(uspi, tind_bh, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
360 if (i >= uspi->s_apb) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800361 tmp = ufs_data_ptr_to_cpu(sb, p);
362 ufs_data_ptr_clear(uspi, p);
Evgeniy Dushistov50aa4eb2006-06-25 05:47:26 -0700363
364 ufs_free_blocks(inode, tmp, uspi->s_fpb);
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700365 mark_inode_dirty(inode);
Evgeniy Dushistov2061df02006-06-25 05:47:18 -0700366 ubh_bforget(tind_bh);
367 tind_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700370 ubh_ll_rw_block(SWRITE, tind_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ubh_wait_on_buffer (tind_bh);
372 }
373 ubh_brelse (tind_bh);
374
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800375 UFSD("EXIT: ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return retry;
377}
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700378
379static int ufs_alloc_lastblock(struct inode *inode)
380{
381 int err = 0;
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800382 struct super_block *sb = inode->i_sb;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700383 struct address_space *mapping = inode->i_mapping;
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800384 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800385 unsigned i, end;
386 sector_t lastfrag;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700387 struct page *lastpage;
388 struct buffer_head *bh;
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800389 u64 phys64;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700390
391 lastfrag = (i_size_read(inode) + uspi->s_fsize - 1) >> uspi->s_fshift;
392
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700393 if (!lastfrag)
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700394 goto out;
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700395
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700396 lastfrag--;
397
398 lastpage = ufs_get_locked_page(mapping, lastfrag >>
399 (PAGE_CACHE_SHIFT - inode->i_blkbits));
400 if (IS_ERR(lastpage)) {
401 err = -EIO;
402 goto out;
403 }
404
405 end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
406 bh = page_buffers(lastpage);
407 for (i = 0; i < end; ++i)
408 bh = bh->b_this_page;
409
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700410
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700411 err = ufs_getfrag_block(inode, lastfrag, bh, 1);
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700412
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700413 if (unlikely(err))
414 goto out_unlock;
415
416 if (buffer_new(bh)) {
417 clear_buffer_new(bh);
418 unmap_underlying_metadata(bh->b_bdev,
419 bh->b_blocknr);
420 /*
421 * we do not zeroize fragment, because of
422 * if it maped to hole, it already contains zeroes
423 */
424 set_buffer_uptodate(bh);
425 mark_buffer_dirty(bh);
426 set_page_dirty(lastpage);
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700427 }
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700428
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800429 if (lastfrag >= UFS_IND_FRAGMENT) {
430 end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
431 phys64 = bh->b_blocknr + 1;
432 for (i = 0; i < end; ++i) {
433 bh = sb_getblk(sb, i + phys64);
434 lock_buffer(bh);
435 memset(bh->b_data, 0, sb->s_blocksize);
436 set_buffer_uptodate(bh);
437 mark_buffer_dirty(bh);
438 unlock_buffer(bh);
439 sync_dirty_buffer(bh);
440 brelse(bh);
441 }
442 }
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700443out_unlock:
444 ufs_put_locked_page(lastpage);
445out:
446 return err;
447}
448
449int ufs_truncate(struct inode *inode, loff_t old_i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700452 struct super_block *sb = inode->i_sb;
453 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
454 int retry, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800456 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
457 inode->i_ino, (unsigned long long)i_size_read(inode),
458 (unsigned long long)old_i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700460 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
461 S_ISLNK(inode->i_mode)))
462 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700464 return -EPERM;
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800465
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700466 err = ufs_alloc_lastblock(inode);
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700467
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700468 if (err) {
469 i_size_write(inode, old_i_size);
470 goto out;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700471 }
472
473 block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 lock_kernel();
476 while (1) {
477 retry = ufs_trunc_direct(inode);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800478 retry |= ufs_trunc_indirect(inode, UFS_IND_BLOCK,
479 ufs_get_direct_data_ptr(uspi, ufsi,
480 UFS_IND_BLOCK));
481 retry |= ufs_trunc_dindirect(inode, UFS_IND_BLOCK + uspi->s_apb,
482 ufs_get_direct_data_ptr(uspi, ufsi,
483 UFS_DIND_BLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 retry |= ufs_trunc_tindirect (inode);
485 if (!retry)
486 break;
487 if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
488 ufs_sync_inode (inode);
489 blk_run_address_space(inode->i_mapping);
490 yield();
491 }
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
Evgeniy Dushistovecdc6392006-08-27 01:23:46 -0700494 ufsi->i_lastfrag = DIRECT_FRAGMENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 unlock_kernel();
496 mark_inode_dirty(inode);
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700497out:
498 UFSD("EXIT: err %d\n", err);
499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700501
502
503/*
504 * We don't define our `inode->i_op->truncate', and call it here,
505 * because of:
506 * - there is no way to know old size
507 * - there is no way inform user about error, if it happens in `truncate'
508 */
509static int ufs_setattr(struct dentry *dentry, struct iattr *attr)
510{
511 struct inode *inode = dentry->d_inode;
512 unsigned int ia_valid = attr->ia_valid;
513 int error;
514
515 error = inode_change_ok(inode, attr);
516 if (error)
517 return error;
518
519 if (ia_valid & ATTR_SIZE &&
520 attr->ia_size != i_size_read(inode)) {
521 loff_t old_i_size = inode->i_size;
522 error = vmtruncate(inode, attr->ia_size);
523 if (error)
524 return error;
525 error = ufs_truncate(inode, old_i_size);
526 if (error)
527 return error;
528 }
529 return inode_setattr(inode, attr);
530}
531
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800532const struct inode_operations ufs_file_inode_operations = {
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700533 .setattr = ufs_setattr,
534};