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