]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/ufs/inode.c
ufs: move calculation of offsets into ufs_getfrag_block()
[mirror_ubuntu-zesty-kernel.git] / fs / ufs / inode.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/ufs/inode.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/inode.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/inode.c
20 *
21 * Copyright (C) 1991, 1992 Linus Torvalds
22 *
23 * Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
24 * Big-endian to little-endian byte-swapping/bitmaps by
25 * David S. Miller (davem@caip.rutgers.edu), 1995
26 */
27
28#include <asm/uaccess.h>
1da177e4
LT
29
30#include <linux/errno.h>
31#include <linux/fs.h>
1da177e4
LT
32#include <linux/time.h>
33#include <linux/stat.h>
34#include <linux/string.h>
35#include <linux/mm.h>
1da177e4 36#include <linux/buffer_head.h>
a9185b41 37#include <linux/writeback.h>
1da177e4 38
e5420598 39#include "ufs_fs.h"
bcd6d4ec 40#include "ufs.h"
1da177e4
LT
41#include "swab.h"
42#include "util.h"
43
4e3911f3 44static int ufs_block_to_path(struct inode *inode, sector_t i_block, unsigned offsets[4])
1da177e4
LT
45{
46 struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi;
47 int ptrs = uspi->s_apb;
48 int ptrs_bits = uspi->s_apbshift;
49 const long direct_blocks = UFS_NDADDR,
50 indirect_blocks = ptrs,
51 double_blocks = (1 << (ptrs_bits * 2));
52 int n = 0;
53
54
abf5d15f 55 UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks);
37044c86 56 if (i_block < direct_blocks) {
1da177e4
LT
57 offsets[n++] = i_block;
58 } else if ((i_block -= direct_blocks) < indirect_blocks) {
59 offsets[n++] = UFS_IND_BLOCK;
60 offsets[n++] = i_block;
61 } else if ((i_block -= indirect_blocks) < double_blocks) {
62 offsets[n++] = UFS_DIND_BLOCK;
63 offsets[n++] = i_block >> ptrs_bits;
64 offsets[n++] = i_block & (ptrs - 1);
65 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
66 offsets[n++] = UFS_TIND_BLOCK;
67 offsets[n++] = i_block >> (ptrs_bits * 2);
68 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
69 offsets[n++] = i_block & (ptrs - 1);
70 } else {
71 ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
72 }
73 return n;
74}
75
724bb09f
AV
76typedef struct {
77 void *p;
78 union {
79 __fs32 key32;
80 __fs64 key64;
81 };
82 struct buffer_head *bh;
83} Indirect;
84
85static inline int grow_chain32(struct ufs_inode_info *ufsi,
86 struct buffer_head *bh, __fs32 *v,
87 Indirect *from, Indirect *to)
88{
89 Indirect *p;
90 unsigned seq;
91 to->bh = bh;
92 do {
93 seq = read_seqbegin(&ufsi->meta_lock);
94 to->key32 = *(__fs32 *)(to->p = v);
95 for (p = from; p <= to && p->key32 == *(__fs32 *)p->p; p++)
96 ;
97 } while (read_seqretry(&ufsi->meta_lock, seq));
98 return (p > to);
99}
100
101static inline int grow_chain64(struct ufs_inode_info *ufsi,
102 struct buffer_head *bh, __fs64 *v,
103 Indirect *from, Indirect *to)
104{
105 Indirect *p;
106 unsigned seq;
107 to->bh = bh;
108 do {
109 seq = read_seqbegin(&ufsi->meta_lock);
110 to->key64 = *(__fs64 *)(to->p = v);
111 for (p = from; p <= to && p->key64 == *(__fs64 *)p->p; p++)
112 ;
113 } while (read_seqretry(&ufsi->meta_lock, seq));
114 return (p > to);
115}
116
1da177e4
LT
117/*
118 * Returns the location of the fragment from
25985edc 119 * the beginning of the filesystem.
1da177e4
LT
120 */
121
4b7068c8 122static u64 ufs_frag_map(struct inode *inode, unsigned offsets[4], int depth)
1da177e4
LT
123{
124 struct ufs_inode_info *ufsi = UFS_I(inode);
125 struct super_block *sb = inode->i_sb;
126 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
127 u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift;
128 int shift = uspi->s_apbshift-uspi->s_fpbshift;
724bb09f 129 Indirect chain[4], *q = chain;
4b7068c8 130 unsigned *p;
1da177e4 131 unsigned flags = UFS_SB(sb)->s_flags;
724bb09f 132 u64 res = 0;
1da177e4 133
7256d819
AM
134 UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
135 uspi->s_fpbshift, uspi->s_apbmask,
136 (unsigned long long)mask);
1da177e4
LT
137
138 if (depth == 0)
724bb09f 139 goto no_block;
1da177e4 140
724bb09f 141again:
1da177e4
LT
142 p = offsets;
143
1da177e4
LT
144 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
145 goto ufs2;
146
724bb09f
AV
147 if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q))
148 goto changed;
149 if (!q->key32)
150 goto no_block;
1da177e4 151 while (--depth) {
724bb09f 152 __fs32 *ptr;
1da177e4 153 struct buffer_head *bh;
4e3911f3 154 unsigned n = *p++;
1da177e4 155
724bb09f
AV
156 bh = sb_bread(sb, uspi->s_sbbase +
157 fs32_to_cpu(sb, q->key32) + (n>>shift));
1da177e4 158 if (!bh)
724bb09f
AV
159 goto no_block;
160 ptr = (__fs32 *)bh->b_data + (n & mask);
161 if (!grow_chain32(ufsi, bh, ptr, chain, ++q))
162 goto changed;
163 if (!q->key32)
164 goto no_block;
1da177e4 165 }
724bb09f
AV
166 res = fs32_to_cpu(sb, q->key32);
167 goto found;
1da177e4 168
724bb09f
AV
169ufs2:
170 if (!grow_chain64(ufsi, NULL, &ufsi->i_u1.u2_i_data[*p++], chain, q))
171 goto changed;
172 if (!q->key64)
173 goto no_block;
1da177e4
LT
174
175 while (--depth) {
724bb09f 176 __fs64 *ptr;
1da177e4 177 struct buffer_head *bh;
4e3911f3 178 unsigned n = *p++;
1da177e4 179
724bb09f
AV
180 bh = sb_bread(sb, uspi->s_sbbase +
181 fs64_to_cpu(sb, q->key64) + (n>>shift));
1da177e4 182 if (!bh)
724bb09f
AV
183 goto no_block;
184 ptr = (__fs64 *)bh->b_data + (n & mask);
185 if (!grow_chain64(ufsi, bh, ptr, chain, ++q))
186 goto changed;
187 if (!q->key64)
188 goto no_block;
189 }
190 res = fs64_to_cpu(sb, q->key64);
191found:
4b7068c8 192 res += uspi->s_sbbase;
724bb09f
AV
193no_block:
194 while (q > chain) {
195 brelse(q->bh);
196 q--;
1da177e4 197 }
724bb09f 198 return res;
1da177e4 199
724bb09f
AV
200changed:
201 while (q > chain) {
202 brelse(q->bh);
203 q--;
204 }
205 goto again;
1da177e4
LT
206}
207
022a6dc5
ED
208/**
209 * ufs_inode_getfrag() - allocate new fragment(s)
edc023ca
FF
210 * @inode: pointer to inode
211 * @fragment: number of `fragment' which hold pointer
022a6dc5 212 * to new allocated fragment(s)
edc023ca
FF
213 * @new_fragment: number of new allocated fragment(s)
214 * @required: how many fragment(s) we require
215 * @err: we set it if something wrong
216 * @phys: pointer to where we save physical number of new allocated fragments,
022a6dc5 217 * NULL if we allocate not data(indirect blocks for example).
edc023ca
FF
218 * @new: we set it if we allocate new block
219 * @locked_page: for ufs_new_fragments()
022a6dc5
ED
220 */
221static struct buffer_head *
54fb996a 222ufs_inode_getfrag(struct inode *inode, u64 fragment,
022a6dc5
ED
223 sector_t new_fragment, unsigned int required, int *err,
224 long *phys, int *new, struct page *locked_page)
1da177e4
LT
225{
226 struct ufs_inode_info *ufsi = UFS_I(inode);
022a6dc5
ED
227 struct super_block *sb = inode->i_sb;
228 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1da177e4 229 struct buffer_head * result;
54fb996a
ED
230 unsigned blockoff, lastblockoff;
231 u64 tmp, goal, lastfrag, block, lastblock;
232 void *p, *p2;
1da177e4 233
54fb996a
ED
234 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, required %u, "
235 "metadata %d\n", inode->i_ino, (unsigned long long)fragment,
022a6dc5 236 (unsigned long long)new_fragment, required, !phys);
1da177e4 237
1da177e4
LT
238 /* TODO : to be done for write support
239 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
240 goto ufs2;
241 */
242
243 block = ufs_fragstoblks (fragment);
244 blockoff = ufs_fragnum (fragment);
54fb996a
ED
245 p = ufs_get_direct_data_ptr(uspi, ufsi, block);
246
1da177e4
LT
247 goal = 0;
248
54fb996a
ED
249 tmp = ufs_data_ptr_to_cpu(sb, p);
250
1da177e4
LT
251 lastfrag = ufsi->i_lastfrag;
252 if (tmp && fragment < lastfrag) {
022a6dc5 253 if (!phys) {
5a39c255 254 return sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
1da177e4 255 } else {
4b25a37e 256 *phys = uspi->s_sbbase + tmp + blockoff;
1da177e4
LT
257 return NULL;
258 }
259 }
260
261 lastblock = ufs_fragstoblks (lastfrag);
262 lastblockoff = ufs_fragnum (lastfrag);
263 /*
264 * We will extend file into new block beyond last allocated block
265 */
266 if (lastblock < block) {
267 /*
268 * We must reallocate last allocated block
269 */
270 if (lastblockoff) {
54fb996a
ED
271 p2 = ufs_get_direct_data_ptr(uspi, ufsi, lastblock);
272 tmp = ufs_new_fragments(inode, p2, lastfrag,
273 ufs_data_ptr_to_cpu(sb, p2),
274 uspi->s_fpb - lastblockoff,
275 err, locked_page);
5a39c255
AV
276 if (!tmp)
277 return NULL;
1da177e4 278 lastfrag = ufsi->i_lastfrag;
1da177e4 279 }
54fb996a
ED
280 tmp = ufs_data_ptr_to_cpu(sb,
281 ufs_get_direct_data_ptr(uspi, ufsi,
282 lastblock));
c37336b0
ED
283 if (tmp)
284 goal = tmp + uspi->s_fpb;
010d331f 285 tmp = ufs_new_fragments (inode, p, fragment - blockoff,
6ef4d6bf 286 goal, required + blockoff,
a685e26f
ED
287 err,
288 phys != NULL ? locked_page : NULL);
54fb996a 289 } else if (lastblock == block) {
1da177e4
LT
290 /*
291 * We will extend last allocated block
292 */
54fb996a
ED
293 tmp = ufs_new_fragments(inode, p, fragment -
294 (blockoff - lastblockoff),
295 ufs_data_ptr_to_cpu(sb, p),
296 required + (blockoff - lastblockoff),
a685e26f 297 err, phys != NULL ? locked_page : NULL);
c37336b0 298 } else /* (lastblock > block) */ {
1da177e4
LT
299 /*
300 * We will allocate new block before last allocated block
301 */
c37336b0 302 if (block) {
54fb996a
ED
303 tmp = ufs_data_ptr_to_cpu(sb,
304 ufs_get_direct_data_ptr(uspi, ufsi, block - 1));
c37336b0
ED
305 if (tmp)
306 goal = tmp + uspi->s_fpb;
307 }
6ef4d6bf 308 tmp = ufs_new_fragments(inode, p, fragment - blockoff,
a685e26f
ED
309 goal, uspi->s_fpb, err,
310 phys != NULL ? locked_page : NULL);
1da177e4
LT
311 }
312 if (!tmp) {
1da177e4
LT
313 *err = -ENOSPC;
314 return NULL;
315 }
316
022a6dc5 317 if (!phys) {
4b25a37e 318 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
1da177e4 319 } else {
4b25a37e 320 *phys = uspi->s_sbbase + tmp + blockoff;
1da177e4
LT
321 result = NULL;
322 *err = 0;
323 *new = 1;
324 }
325
326 inode->i_ctime = CURRENT_TIME_SEC;
327 if (IS_SYNC(inode))
328 ufs_sync_inode (inode);
329 mark_inode_dirty(inode);
1da177e4
LT
330 return result;
331
332 /* This part : To be implemented ....
333 Required only for writing, not required for READ-ONLY.
334ufs2:
335
336 u2_block = ufs_fragstoblks(fragment);
337 u2_blockoff = ufs_fragnum(fragment);
338 p = ufsi->i_u1.u2_i_data + block;
339 goal = 0;
340
341repeat2:
342 tmp = fs32_to_cpu(sb, *p);
343 lastfrag = ufsi->i_lastfrag;
344
345 */
346}
347
022a6dc5
ED
348/**
349 * ufs_inode_getblock() - allocate new block
edc023ca
FF
350 * @inode: pointer to inode
351 * @bh: pointer to block which hold "pointer" to new allocated block
352 * @fragment: number of `fragment' which hold pointer
022a6dc5 353 * to new allocated block
edc023ca 354 * @new_fragment: number of new allocated fragment
022a6dc5 355 * (block will hold this fragment and also uspi->s_fpb-1)
edc023ca
FF
356 * @err: see ufs_inode_getfrag()
357 * @phys: see ufs_inode_getfrag()
358 * @new: see ufs_inode_getfrag()
359 * @locked_page: see ufs_inode_getfrag()
022a6dc5
ED
360 */
361static struct buffer_head *
362ufs_inode_getblock(struct inode *inode, struct buffer_head *bh,
54fb996a 363 u64 fragment, sector_t new_fragment, int *err,
022a6dc5 364 long *phys, int *new, struct page *locked_page)
1da177e4 365{
022a6dc5
ED
366 struct super_block *sb = inode->i_sb;
367 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1da177e4 368 struct buffer_head * result;
54fb996a
ED
369 unsigned blockoff;
370 u64 tmp, goal, block;
371 void *p;
1da177e4 372
1da177e4
LT
373 block = ufs_fragstoblks (fragment);
374 blockoff = ufs_fragnum (fragment);
375
54fb996a
ED
376 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, metadata %d\n",
377 inode->i_ino, (unsigned long long)fragment,
378 (unsigned long long)new_fragment, !phys);
1da177e4
LT
379
380 result = NULL;
381 if (!bh)
382 goto out;
383 if (!buffer_uptodate(bh)) {
384 ll_rw_block (READ, 1, &bh);
385 wait_on_buffer (bh);
386 if (!buffer_uptodate(bh))
387 goto out;
388 }
54fb996a
ED
389 if (uspi->fs_magic == UFS2_MAGIC)
390 p = (__fs64 *)bh->b_data + block;
391 else
392 p = (__fs32 *)bh->b_data + block;
5a39c255 393
54fb996a 394 tmp = ufs_data_ptr_to_cpu(sb, p);
1da177e4 395 if (tmp) {
5a39c255 396 if (!phys)
1da177e4 397 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
5a39c255 398 else
4b25a37e 399 *phys = uspi->s_sbbase + tmp + blockoff;
5a39c255 400 goto out;
1da177e4
LT
401 }
402
54fb996a
ED
403 if (block && (uspi->fs_magic == UFS2_MAGIC ?
404 (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[block-1])) :
405 (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[block-1]))))
1da177e4
LT
406 goal = tmp + uspi->s_fpb;
407 else
408 goal = bh->b_blocknr + uspi->s_fpb;
6ef4d6bf
ED
409 tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal,
410 uspi->s_fpb, err, locked_page);
5a39c255 411 if (!tmp)
1da177e4 412 goto out;
c9a27b5d 413
022a6dc5 414 if (!phys) {
4b25a37e 415 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
1da177e4 416 } else {
4b25a37e 417 *phys = uspi->s_sbbase + tmp + blockoff;
1da177e4
LT
418 *new = 1;
419 }
420
421 mark_buffer_dirty(bh);
422 if (IS_SYNC(inode))
423 sync_dirty_buffer(bh);
424 inode->i_ctime = CURRENT_TIME_SEC;
425 mark_inode_dirty(inode);
426out:
427 brelse (bh);
abf5d15f 428 UFSD("EXIT\n");
1da177e4
LT
429 return result;
430}
431
022a6dc5 432/**
7422caa5 433 * ufs_getfrag_block() - `get_block_t' function, interface between UFS and
022a6dc5 434 * readpage, writepage and so on
1da177e4
LT
435 */
436
010d331f 437static int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
1da177e4
LT
438{
439 struct super_block * sb = inode->i_sb;
788257d6
AB
440 struct ufs_sb_info * sbi = UFS_SB(sb);
441 struct ufs_sb_private_info * uspi = sbi->s_uspi;
1da177e4
LT
442 struct buffer_head * bh;
443 int ret, err, new;
4b7068c8
AV
444 unsigned offsets[4];
445 int depth = ufs_block_to_path(inode, fragment >> uspi->s_fpbshift, offsets);
1da177e4
LT
446 unsigned long ptr,phys;
447 u64 phys64 = 0;
010d331f 448
1da177e4 449 if (!create) {
4b7068c8
AV
450 phys64 = ufs_frag_map(inode, offsets, depth);
451 if (phys64) {
452 phys64 += fragment & uspi->s_fpbmask;
1da177e4 453 map_bh(bh_result, sb, phys64);
4b7068c8 454 }
1da177e4
LT
455 return 0;
456 }
457
458 /* This code entered only while writing ....? */
459
460 err = -EIO;
461 new = 0;
462 ret = 0;
463 bh = NULL;
464
724bb09f 465 mutex_lock(&UFS_I(inode)->truncate_mutex);
1da177e4 466
abf5d15f 467 UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
1da177e4
LT
468 if (fragment >
469 ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb)
470 << uspi->s_fpbshift))
471 goto abort_too_big;
472
473 err = 0;
474 ptr = fragment;
010d331f 475
1da177e4
LT
476 /*
477 * ok, these macros clean the logic up a bit and make
478 * it much more readable:
479 */
480#define GET_INODE_DATABLOCK(x) \
a685e26f
ED
481 ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new,\
482 bh_result->b_page)
1da177e4 483#define GET_INODE_PTR(x) \
a685e26f
ED
484 ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL,\
485 bh_result->b_page)
1da177e4 486#define GET_INDIRECT_DATABLOCK(x) \
022a6dc5 487 ufs_inode_getblock(inode, bh, x, fragment, \
d63b7090 488 &err, &phys, &new, bh_result->b_page)
1da177e4 489#define GET_INDIRECT_PTR(x) \
022a6dc5 490 ufs_inode_getblock(inode, bh, x, fragment, \
d63b7090 491 &err, NULL, NULL, NULL)
1da177e4
LT
492
493 if (ptr < UFS_NDIR_FRAGMENT) {
494 bh = GET_INODE_DATABLOCK(ptr);
495 goto out;
496 }
497 ptr -= UFS_NDIR_FRAGMENT;
498 if (ptr < (1 << (uspi->s_apbshift + uspi->s_fpbshift))) {
499 bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift));
500 goto get_indirect;
501 }
502 ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift);
503 if (ptr < (1 << (uspi->s_2apbshift + uspi->s_fpbshift))) {
504 bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift));
505 goto get_double;
506 }
507 ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift);
508 bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift));
509 bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask);
510get_double:
511 bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask);
512get_indirect:
513 bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask);
514
515#undef GET_INODE_DATABLOCK
516#undef GET_INODE_PTR
517#undef GET_INDIRECT_DATABLOCK
518#undef GET_INDIRECT_PTR
519
520out:
521 if (err)
522 goto abort;
523 if (new)
524 set_buffer_new(bh_result);
525 map_bh(bh_result, sb, phys);
526abort:
724bb09f 527 mutex_unlock(&UFS_I(inode)->truncate_mutex);
788257d6 528
1da177e4
LT
529 return err;
530
1da177e4
LT
531abort_too_big:
532 ufs_warning(sb, "ufs_get_block", "block > big");
533 goto abort;
534}
535
1da177e4
LT
536static int ufs_writepage(struct page *page, struct writeback_control *wbc)
537{
538 return block_write_full_page(page,ufs_getfrag_block,wbc);
539}
82b9d1d0 540
1da177e4
LT
541static int ufs_readpage(struct file *file, struct page *page)
542{
543 return block_read_full_page(page,ufs_getfrag_block);
544}
82b9d1d0 545
f4e420dc 546int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len)
1da177e4 547{
6e1db88d 548 return __block_write_begin(page, pos, len, ufs_getfrag_block);
1da177e4 549}
82b9d1d0 550
010d331f
AV
551static void ufs_truncate_blocks(struct inode *);
552
83f6e371
MS
553static void ufs_write_failed(struct address_space *mapping, loff_t to)
554{
555 struct inode *inode = mapping->host;
556
3b7a3a05 557 if (to > inode->i_size) {
7caef267 558 truncate_pagecache(inode, inode->i_size);
3b7a3a05
AV
559 ufs_truncate_blocks(inode);
560 }
83f6e371
MS
561}
562
82b9d1d0
NP
563static int ufs_write_begin(struct file *file, struct address_space *mapping,
564 loff_t pos, unsigned len, unsigned flags,
565 struct page **pagep, void **fsdata)
566{
155130a4
CH
567 int ret;
568
569 ret = block_write_begin(mapping, pos, len, flags, pagep,
f4e420dc 570 ufs_getfrag_block);
83f6e371
MS
571 if (unlikely(ret))
572 ufs_write_failed(mapping, pos + len);
155130a4
CH
573
574 return ret;
82b9d1d0
NP
575}
576
3b7a3a05
AV
577static int ufs_write_end(struct file *file, struct address_space *mapping,
578 loff_t pos, unsigned len, unsigned copied,
579 struct page *page, void *fsdata)
580{
581 int ret;
582
583 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
584 if (ret < len)
585 ufs_write_failed(mapping, pos + len);
586 return ret;
587}
588
1da177e4
LT
589static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
590{
591 return generic_block_bmap(mapping,block,ufs_getfrag_block);
592}
82b9d1d0 593
f5e54d6e 594const struct address_space_operations ufs_aops = {
1da177e4
LT
595 .readpage = ufs_readpage,
596 .writepage = ufs_writepage,
82b9d1d0 597 .write_begin = ufs_write_begin,
3b7a3a05 598 .write_end = ufs_write_end,
1da177e4
LT
599 .bmap = ufs_bmap
600};
601
826843a3
ED
602static void ufs_set_inode_ops(struct inode *inode)
603{
604 if (S_ISREG(inode->i_mode)) {
605 inode->i_op = &ufs_file_inode_operations;
606 inode->i_fop = &ufs_file_operations;
607 inode->i_mapping->a_ops = &ufs_aops;
608 } else if (S_ISDIR(inode->i_mode)) {
609 inode->i_op = &ufs_dir_inode_operations;
610 inode->i_fop = &ufs_dir_operations;
611 inode->i_mapping->a_ops = &ufs_aops;
612 } else if (S_ISLNK(inode->i_mode)) {
4b8061a6 613 if (!inode->i_blocks) {
826843a3 614 inode->i_op = &ufs_fast_symlink_inode_operations;
4b8061a6
AV
615 inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink;
616 } else {
311b9549 617 inode->i_op = &ufs_symlink_inode_operations;
826843a3
ED
618 inode->i_mapping->a_ops = &ufs_aops;
619 }
620 } else
621 init_special_inode(inode, inode->i_mode,
622 ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
623}
624
07a0cfec 625static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
1da177e4
LT
626{
627 struct ufs_inode_info *ufsi = UFS_I(inode);
05f225dc 628 struct super_block *sb = inode->i_sb;
6a9a06d9 629 umode_t mode;
1da177e4
LT
630
631 /*
632 * Copy data to the in-core inode.
633 */
634 inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
bfe86848 635 set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink));
07a0cfec 636 if (inode->i_nlink == 0) {
1da177e4 637 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
07a0cfec
ED
638 return -1;
639 }
010d331f 640
1da177e4
LT
641 /*
642 * Linux now has 32-bit uid and gid, so we can support EFT.
643 */
72235465
EB
644 i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode));
645 i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode));
1da177e4
LT
646
647 inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
648 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
649 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
650 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
651 inode->i_mtime.tv_nsec = 0;
652 inode->i_atime.tv_nsec = 0;
653 inode->i_ctime.tv_nsec = 0;
654 inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
3313e292 655 inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen);
1da177e4 656 ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
1da177e4
LT
657 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
658 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
05f225dc 659
010d331f 660
1da177e4 661 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
f33219b7
DG
662 memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr,
663 sizeof(ufs_inode->ui_u2.ui_addr));
dd187a26 664 } else {
f33219b7 665 memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink,
b12903f1
DG
666 sizeof(ufs_inode->ui_u2.ui_symlink) - 1);
667 ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0;
1da177e4 668 }
07a0cfec 669 return 0;
05f225dc 670}
1da177e4 671
07a0cfec 672static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
05f225dc
ED
673{
674 struct ufs_inode_info *ufsi = UFS_I(inode);
675 struct super_block *sb = inode->i_sb;
6a9a06d9 676 umode_t mode;
1da177e4 677
abf5d15f 678 UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
1da177e4
LT
679 /*
680 * Copy data to the in-core inode.
681 */
682 inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
bfe86848 683 set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink));
07a0cfec 684 if (inode->i_nlink == 0) {
1da177e4 685 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
07a0cfec
ED
686 return -1;
687 }
1da177e4
LT
688
689 /*
690 * Linux now has 32-bit uid and gid, so we can support EFT.
691 */
72235465
EB
692 i_uid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_uid));
693 i_gid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_gid));
1da177e4
LT
694
695 inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
2189850f
ED
696 inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime);
697 inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime);
698 inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime);
699 inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec);
700 inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec);
701 inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec);
1da177e4 702 inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
3313e292 703 inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen);
1da177e4 704 ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
1da177e4
LT
705 /*
706 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
707 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
708 */
1da177e4
LT
709
710 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
f33219b7
DG
711 memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr,
712 sizeof(ufs2_inode->ui_u2.ui_addr));
05f225dc 713 } else {
f33219b7 714 memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink,
b12903f1
DG
715 sizeof(ufs2_inode->ui_u2.ui_symlink) - 1);
716 ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0;
1da177e4 717 }
07a0cfec 718 return 0;
05f225dc
ED
719}
720
b55c460d 721struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
05f225dc 722{
b55c460d
DH
723 struct ufs_inode_info *ufsi;
724 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
05f225dc 725 struct buffer_head * bh;
b55c460d 726 struct inode *inode;
07a0cfec 727 int err;
05f225dc 728
b55c460d 729 UFSD("ENTER, ino %lu\n", ino);
05f225dc 730
b55c460d 731 if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) {
05f225dc 732 ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n",
b55c460d
DH
733 ino);
734 return ERR_PTR(-EIO);
05f225dc
ED
735 }
736
b55c460d
DH
737 inode = iget_locked(sb, ino);
738 if (!inode)
739 return ERR_PTR(-ENOMEM);
740 if (!(inode->i_state & I_NEW))
741 return inode;
742
743 ufsi = UFS_I(inode);
744
05f225dc
ED
745 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
746 if (!bh) {
747 ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n",
748 inode->i_ino);
749 goto bad_inode;
750 }
751 if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
752 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
753
07a0cfec
ED
754 err = ufs2_read_inode(inode,
755 ufs2_inode + ufs_inotofsbo(inode->i_ino));
05f225dc
ED
756 } else {
757 struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data;
758
07a0cfec
ED
759 err = ufs1_read_inode(inode,
760 ufs_inode + ufs_inotofsbo(inode->i_ino));
05f225dc
ED
761 }
762
07a0cfec
ED
763 if (err)
764 goto bad_inode;
05f225dc
ED
765 inode->i_version++;
766 ufsi->i_lastfrag =
767 (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
768 ufsi->i_dir_start_lookup = 0;
1da177e4
LT
769 ufsi->i_osync = 0;
770
826843a3 771 ufs_set_inode_ops(inode);
1da177e4
LT
772
773 brelse(bh);
774
abf5d15f 775 UFSD("EXIT\n");
b55c460d
DH
776 unlock_new_inode(inode);
777 return inode;
05f225dc
ED
778
779bad_inode:
b55c460d
DH
780 iget_failed(inode);
781 return ERR_PTR(-EIO);
1da177e4
LT
782}
783
3313e292 784static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
1da177e4 785{
3313e292
ED
786 struct super_block *sb = inode->i_sb;
787 struct ufs_inode_info *ufsi = UFS_I(inode);
1da177e4
LT
788
789 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
790 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
791
72235465
EB
792 ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode));
793 ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode));
010d331f 794
1da177e4
LT
795 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
796 ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
797 ufs_inode->ui_atime.tv_usec = 0;
798 ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
799 ufs_inode->ui_ctime.tv_usec = 0;
800 ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
801 ufs_inode->ui_mtime.tv_usec = 0;
802 ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
803 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
3313e292 804 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
1da177e4 805
3313e292 806 if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) {
1da177e4
LT
807 ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
808 ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
809 }
810
811 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
812 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
813 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
814 } else if (inode->i_blocks) {
f33219b7
DG
815 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data,
816 sizeof(ufs_inode->ui_u2.ui_addr));
1da177e4
LT
817 }
818 else {
f33219b7
DG
819 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
820 sizeof(ufs_inode->ui_u2.ui_symlink));
1da177e4
LT
821 }
822
823 if (!inode->i_nlink)
824 memset (ufs_inode, 0, sizeof(struct ufs_inode));
3313e292
ED
825}
826
827static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
828{
829 struct super_block *sb = inode->i_sb;
830 struct ufs_inode_info *ufsi = UFS_I(inode);
3313e292
ED
831
832 UFSD("ENTER\n");
833 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
834 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
835
72235465
EB
836 ufs_inode->ui_uid = cpu_to_fs32(sb, i_uid_read(inode));
837 ufs_inode->ui_gid = cpu_to_fs32(sb, i_gid_read(inode));
3313e292
ED
838
839 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
2189850f
ED
840 ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec);
841 ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec);
842 ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec);
843 ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec);
844 ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec);
845 ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec);
3313e292
ED
846
847 ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks);
848 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
849 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
850
851 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
852 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
853 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0];
854 } else if (inode->i_blocks) {
f33219b7
DG
855 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data,
856 sizeof(ufs_inode->ui_u2.ui_addr));
3313e292 857 } else {
f33219b7
DG
858 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
859 sizeof(ufs_inode->ui_u2.ui_symlink));
3313e292
ED
860 }
861
862 if (!inode->i_nlink)
863 memset (ufs_inode, 0, sizeof(struct ufs2_inode));
864 UFSD("EXIT\n");
865}
866
867static int ufs_update_inode(struct inode * inode, int do_sync)
868{
869 struct super_block *sb = inode->i_sb;
870 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
871 struct buffer_head * bh;
872
873 UFSD("ENTER, ino %lu\n", inode->i_ino);
874
875 if (inode->i_ino < UFS_ROOTINO ||
876 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
877 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
878 return -1;
879 }
880
881 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
882 if (!bh) {
883 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
884 return -1;
885 }
886 if (uspi->fs_magic == UFS2_MAGIC) {
887 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
888
889 ufs2_update_inode(inode,
890 ufs2_inode + ufs_inotofsbo(inode->i_ino));
891 } else {
892 struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data;
893
894 ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
895 }
010d331f 896
1da177e4
LT
897 mark_buffer_dirty(bh);
898 if (do_sync)
899 sync_dirty_buffer(bh);
900 brelse (bh);
010d331f 901
abf5d15f 902 UFSD("EXIT\n");
1da177e4
LT
903 return 0;
904}
905
a9185b41 906int ufs_write_inode(struct inode *inode, struct writeback_control *wbc)
1da177e4 907{
f3e0f3da 908 return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1da177e4
LT
909}
910
911int ufs_sync_inode (struct inode *inode)
912{
913 return ufs_update_inode (inode, 1);
914}
915
58e8268c 916void ufs_evict_inode(struct inode * inode)
1da177e4 917{
58e8268c
AV
918 int want_delete = 0;
919
920 if (!inode->i_nlink && !is_bad_inode(inode))
921 want_delete = 1;
10e5dce0 922
91b0abe3 923 truncate_inode_pages_final(&inode->i_data);
58e8268c 924 if (want_delete) {
58e8268c 925 inode->i_size = 0;
d622f167
AV
926 if (inode->i_blocks)
927 ufs_truncate_blocks(inode);
58e8268c
AV
928 }
929
930 invalidate_inode_buffers(inode);
dbd5768f 931 clear_inode(inode);
58e8268c 932
f3e0f3da 933 if (want_delete)
9ef7db7f 934 ufs_free_inode(inode);
1da177e4 935}
010d331f 936
a138b4b6
AV
937struct to_free {
938 struct inode *inode;
939 u64 to;
940 unsigned count;
941};
942
943static inline void free_data(struct to_free *ctx, u64 from, unsigned count)
944{
945 if (ctx->count && ctx->to != from) {
946 ufs_free_blocks(ctx->inode, ctx->to - ctx->count, ctx->count);
947 ctx->count = 0;
948 }
949 ctx->count += count;
950 ctx->to = from + count;
951}
952
010d331f
AV
953#define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
954#define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
955
956static void ufs_trunc_direct(struct inode *inode)
957{
958 struct ufs_inode_info *ufsi = UFS_I(inode);
959 struct super_block * sb;
960 struct ufs_sb_private_info * uspi;
961 void *p;
962 u64 frag1, frag2, frag3, frag4, block1, block2;
a138b4b6 963 struct to_free ctx = {.inode = inode};
010d331f
AV
964 unsigned i, tmp;
965
966 UFSD("ENTER: ino %lu\n", inode->i_ino);
967
968 sb = inode->i_sb;
969 uspi = UFS_SB(sb)->s_uspi;
970
010d331f
AV
971 frag1 = DIRECT_FRAGMENT;
972 frag4 = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
973 frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
974 frag3 = frag4 & ~uspi->s_fpbmask;
975 block1 = block2 = 0;
976 if (frag2 > frag3) {
977 frag2 = frag4;
978 frag3 = frag4 = 0;
979 } else if (frag2 < frag3) {
980 block1 = ufs_fragstoblks (frag2);
981 block2 = ufs_fragstoblks (frag3);
982 }
983
984 UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
985 " frag3 %llu, frag4 %llu\n", inode->i_ino,
986 (unsigned long long)frag1, (unsigned long long)frag2,
987 (unsigned long long)block1, (unsigned long long)block2,
988 (unsigned long long)frag3, (unsigned long long)frag4);
989
990 if (frag1 >= frag2)
991 goto next1;
992
993 /*
994 * Free first free fragments
995 */
996 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
997 tmp = ufs_data_ptr_to_cpu(sb, p);
998 if (!tmp )
999 ufs_panic (sb, "ufs_trunc_direct", "internal error");
1000 frag2 -= frag1;
1001 frag1 = ufs_fragnum (frag1);
1002
1003 ufs_free_fragments(inode, tmp + frag1, frag2);
010d331f
AV
1004
1005next1:
1006 /*
1007 * Free whole blocks
1008 */
1009 for (i = block1 ; i < block2; i++) {
1010 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
1011 tmp = ufs_data_ptr_to_cpu(sb, p);
1012 if (!tmp)
1013 continue;
1014 write_seqlock(&ufsi->meta_lock);
1015 ufs_data_ptr_clear(uspi, p);
1016 write_sequnlock(&ufsi->meta_lock);
1017
a138b4b6 1018 free_data(&ctx, tmp, uspi->s_fpb);
010d331f
AV
1019 }
1020
a138b4b6 1021 free_data(&ctx, 0, 0);
010d331f
AV
1022
1023 if (frag3 >= frag4)
1024 goto next3;
1025
1026 /*
1027 * Free last free fragments
1028 */
1029 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
1030 tmp = ufs_data_ptr_to_cpu(sb, p);
1031 if (!tmp )
1032 ufs_panic(sb, "ufs_truncate_direct", "internal error");
1033 frag4 = ufs_fragnum (frag4);
1034 write_seqlock(&ufsi->meta_lock);
1035 ufs_data_ptr_clear(uspi, p);
1036 write_sequnlock(&ufsi->meta_lock);
1037
1038 ufs_free_fragments (inode, tmp, frag4);
010d331f
AV
1039 next3:
1040
1041 UFSD("EXIT: ino %lu\n", inode->i_ino);
1042}
1043
163073db 1044static void free_full_branch(struct inode *inode, u64 ind_block, int depth)
6d1ebbca
AV
1045{
1046 struct super_block *sb = inode->i_sb;
1047 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
163073db 1048 struct ufs_buffer_head *ubh = ubh_bread(sb, ind_block, uspi->s_bsize);
6d1ebbca
AV
1049 unsigned i;
1050
163073db 1051 if (!ubh)
6d1ebbca 1052 return;
6d1ebbca
AV
1053
1054 if (--depth) {
163073db
AV
1055 for (i = 0; i < uspi->s_apb; i++) {
1056 void *p = ubh_get_data_ptr(uspi, ubh, i);
1057 u64 block = ufs_data_ptr_to_cpu(sb, p);
cc7231e3 1058 if (block)
163073db 1059 free_full_branch(inode, block, depth);
6d1ebbca
AV
1060 }
1061 } else {
1062 struct to_free ctx = {.inode = inode};
1063
1064 for (i = 0; i < uspi->s_apb; i++) {
163073db
AV
1065 void *p = ubh_get_data_ptr(uspi, ubh, i);
1066 u64 block = ufs_data_ptr_to_cpu(sb, p);
cc7231e3 1067 if (block)
163073db 1068 free_data(&ctx, block, uspi->s_fpb);
6d1ebbca
AV
1069 }
1070 free_data(&ctx, 0, 0);
1071 }
6d1ebbca
AV
1072
1073 ubh_bforget(ubh);
163073db 1074 ufs_free_blocks(inode, ind_block, uspi->s_fpb);
6d1ebbca
AV
1075}
1076
7b4e4f7f 1077static void free_branch_tail(struct inode *inode, unsigned from, struct ufs_buffer_head *ubh, int depth)
010d331f 1078{
7bad5939
AV
1079 struct super_block *sb = inode->i_sb;
1080 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
7bad5939 1081 unsigned i;
010d331f 1082
9e0fbbde 1083 if (--depth) {
7b4e4f7f 1084 for (i = from; i < uspi->s_apb ; i++) {
163073db
AV
1085 void *p = ubh_get_data_ptr(uspi, ubh, i);
1086 u64 block = ufs_data_ptr_to_cpu(sb, p);
1087 if (block) {
1088 write_seqlock(&UFS_I(inode)->meta_lock);
1089 ufs_data_ptr_clear(uspi, p);
1090 write_sequnlock(&UFS_I(inode)->meta_lock);
1091 ubh_mark_buffer_dirty(ubh);
1092 free_full_branch(inode, block, depth);
1093 }
a9657423 1094 }
9e0fbbde 1095 } else {
a138b4b6 1096 struct to_free ctx = {.inode = inode};
9e0fbbde
AV
1097
1098 for (i = from; i < uspi->s_apb; i++) {
163073db
AV
1099 void *p = ubh_get_data_ptr(uspi, ubh, i);
1100 u64 block = ufs_data_ptr_to_cpu(sb, p);
1101 if (block) {
1102 write_seqlock(&UFS_I(inode)->meta_lock);
1103 ufs_data_ptr_clear(uspi, p);
1104 write_sequnlock(&UFS_I(inode)->meta_lock);
1105 ubh_mark_buffer_dirty(ubh);
1106 free_data(&ctx, block, uspi->s_fpb);
163073db 1107 }
9e0fbbde 1108 }
a138b4b6 1109 free_data(&ctx, 0, 0);
010d331f 1110 }
9e0fbbde
AV
1111 if (IS_SYNC(inode) && ubh_buffer_dirty(ubh))
1112 ubh_sync_block(ubh);
1113 ubh_brelse(ubh);
010d331f
AV
1114}
1115
1116static int ufs_alloc_lastblock(struct inode *inode, loff_t size)
1117{
1118 int err = 0;
1119 struct super_block *sb = inode->i_sb;
1120 struct address_space *mapping = inode->i_mapping;
1121 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1122 unsigned i, end;
1123 sector_t lastfrag;
1124 struct page *lastpage;
1125 struct buffer_head *bh;
1126 u64 phys64;
1127
1128 lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
1129
1130 if (!lastfrag)
1131 goto out;
1132
1133 lastfrag--;
1134
1135 lastpage = ufs_get_locked_page(mapping, lastfrag >>
1136 (PAGE_CACHE_SHIFT - inode->i_blkbits));
1137 if (IS_ERR(lastpage)) {
1138 err = -EIO;
1139 goto out;
1140 }
1141
1142 end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
1143 bh = page_buffers(lastpage);
1144 for (i = 0; i < end; ++i)
1145 bh = bh->b_this_page;
1146
1147
1148 err = ufs_getfrag_block(inode, lastfrag, bh, 1);
1149
1150 if (unlikely(err))
1151 goto out_unlock;
1152
1153 if (buffer_new(bh)) {
1154 clear_buffer_new(bh);
1155 unmap_underlying_metadata(bh->b_bdev,
1156 bh->b_blocknr);
1157 /*
1158 * we do not zeroize fragment, because of
1159 * if it maped to hole, it already contains zeroes
1160 */
1161 set_buffer_uptodate(bh);
1162 mark_buffer_dirty(bh);
1163 set_page_dirty(lastpage);
1164 }
1165
1166 if (lastfrag >= UFS_IND_FRAGMENT) {
1167 end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
1168 phys64 = bh->b_blocknr + 1;
1169 for (i = 0; i < end; ++i) {
1170 bh = sb_getblk(sb, i + phys64);
1171 lock_buffer(bh);
1172 memset(bh->b_data, 0, sb->s_blocksize);
1173 set_buffer_uptodate(bh);
1174 mark_buffer_dirty(bh);
1175 unlock_buffer(bh);
1176 sync_dirty_buffer(bh);
1177 brelse(bh);
1178 }
1179 }
1180out_unlock:
1181 ufs_put_locked_page(lastpage);
1182out:
1183 return err;
1184}
1185
1186static void __ufs_truncate_blocks(struct inode *inode)
1187{
1188 struct ufs_inode_info *ufsi = UFS_I(inode);
1189 struct super_block *sb = inode->i_sb;
1190 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
7bad5939 1191 unsigned offsets[4];
31cd043e 1192 int depth = ufs_block_to_path(inode, DIRECT_BLOCK, offsets);
6775e24d 1193 int depth2;
42432739 1194 unsigned i;
7b4e4f7f
AV
1195 struct ufs_buffer_head *ubh[3];
1196 void *p;
1197 u64 block;
6775e24d
AV
1198
1199 if (!depth)
1200 return;
1201
1202 /* find the last non-zero in offsets[] */
1203 for (depth2 = depth - 1; depth2; depth2--)
1204 if (offsets[depth2])
1205 break;
010d331f
AV
1206
1207 mutex_lock(&ufsi->truncate_mutex);
42432739 1208 if (depth == 1) {
31cd043e 1209 ufs_trunc_direct(inode);
42432739
AV
1210 offsets[0] = UFS_IND_BLOCK;
1211 } else {
7b4e4f7f
AV
1212 /* get the blocks that should be partially emptied */
1213 p = ufs_get_direct_data_ptr(uspi, ufsi, offsets[0]);
1214 for (i = 0; i < depth2; i++) {
1215 offsets[i]++; /* next branch is fully freed */
1216 block = ufs_data_ptr_to_cpu(sb, p);
1217 if (!block)
1218 break;
1219 ubh[i] = ubh_bread(sb, block, uspi->s_bsize);
1220 if (!ubh[i]) {
1221 write_seqlock(&ufsi->meta_lock);
1222 ufs_data_ptr_clear(uspi, p);
1223 write_sequnlock(&ufsi->meta_lock);
1224 break;
1225 }
1226 p = ubh_get_data_ptr(uspi, ubh[i], offsets[i + 1]);
1227 }
f53bd142 1228 while (i--)
7b4e4f7f 1229 free_branch_tail(inode, offsets[i + 1], ubh[i], depth - i - 1);
42432739
AV
1230 }
1231 for (i = offsets[0]; i <= UFS_TIND_BLOCK; i++) {
163073db
AV
1232 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
1233 block = ufs_data_ptr_to_cpu(sb, p);
1234 if (block) {
1235 write_seqlock(&ufsi->meta_lock);
1236 ufs_data_ptr_clear(uspi, p);
1237 write_sequnlock(&ufsi->meta_lock);
1238 free_full_branch(inode, block, i - UFS_IND_BLOCK + 1);
1239 }
31cd043e 1240 }
010d331f 1241 ufsi->i_lastfrag = DIRECT_FRAGMENT;
b6eede0e 1242 mark_inode_dirty(inode);
010d331f
AV
1243 mutex_unlock(&ufsi->truncate_mutex);
1244}
1245
1246static int ufs_truncate(struct inode *inode, loff_t size)
1247{
1248 int err = 0;
1249
1250 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
1251 inode->i_ino, (unsigned long long)size,
1252 (unsigned long long)i_size_read(inode));
1253
1254 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1255 S_ISLNK(inode->i_mode)))
1256 return -EINVAL;
1257 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1258 return -EPERM;
1259
1260 err = ufs_alloc_lastblock(inode, size);
1261
1262 if (err)
1263 goto out;
1264
1265 block_truncate_page(inode->i_mapping, size, ufs_getfrag_block);
1266
1267 truncate_setsize(inode, size);
1268
1269 __ufs_truncate_blocks(inode);
1270 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1271 mark_inode_dirty(inode);
1272out:
1273 UFSD("EXIT: err %d\n", err);
1274 return err;
1275}
1276
1277void ufs_truncate_blocks(struct inode *inode)
1278{
1279 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1280 S_ISLNK(inode->i_mode)))
1281 return;
1282 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1283 return;
1284 __ufs_truncate_blocks(inode);
1285}
1286
1287int ufs_setattr(struct dentry *dentry, struct iattr *attr)
1288{
1289 struct inode *inode = d_inode(dentry);
1290 unsigned int ia_valid = attr->ia_valid;
1291 int error;
1292
1293 error = inode_change_ok(inode, attr);
1294 if (error)
1295 return error;
1296
1297 if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
1298 error = ufs_truncate(inode, attr->ia_size);
1299 if (error)
1300 return error;
1301 }
1302
1303 setattr_copy(inode, attr);
1304 mark_inode_dirty(inode);
1305 return 0;
1306}
1307
1308const struct inode_operations ufs_file_inode_operations = {
1309 .setattr = ufs_setattr,
1310};