blob: 43672183fee33aed9cec936156546c3a30cbd243 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <linux/errno.h>
31#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/time.h>
33#include <linux/stat.h>
34#include <linux/string.h>
35#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/buffer_head.h>
Christoph Hellwiga9185b42010-03-05 09:21:37 +010037#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mike Frysingere5420592008-02-08 04:21:31 -080039#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070040#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "swab.h"
42#include "util.h"
43
Al Viro4e3911f2015-06-04 14:13:14 -040044static int ufs_block_to_path(struct inode *inode, sector_t i_block, unsigned offsets[4])
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
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
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070055 UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks);
Roel Kluin37044c82009-06-17 16:26:28 -070056 if (i_block < direct_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 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
Al Viro724bb092015-06-17 12:02:56 -040076typedef 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/*
118 * Returns the location of the fragment from
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300119 * the beginning of the filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
121
Al Viro724bb092015-06-17 12:02:56 -0400122static u64 ufs_frag_map(struct inode *inode, sector_t frag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
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;
Al Viro010d3312015-06-17 12:44:14 -0400129 unsigned offsets[4], *p;
Al Viro724bb092015-06-17 12:02:56 -0400130 Indirect chain[4], *q = chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned flags = UFS_SB(sb)->s_flags;
Al Viro724bb092015-06-17 12:02:56 -0400133 u64 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700135 UFSD(": frag = %llu depth = %d\n", (unsigned long long)frag, depth);
Andrew Morton7256d812006-06-29 02:24:29 -0700136 UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
137 uspi->s_fpbshift, uspi->s_apbmask,
138 (unsigned long long)mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 if (depth == 0)
Al Viro724bb092015-06-17 12:02:56 -0400141 goto no_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Al Viro724bb092015-06-17 12:02:56 -0400143again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 p = offsets;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
147 goto ufs2;
148
Al Viro724bb092015-06-17 12:02:56 -0400149 if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q))
150 goto changed;
151 if (!q->key32)
152 goto no_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 while (--depth) {
Al Viro724bb092015-06-17 12:02:56 -0400154 __fs32 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct buffer_head *bh;
Al Viro4e3911f2015-06-04 14:13:14 -0400156 unsigned n = *p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Al Viro724bb092015-06-17 12:02:56 -0400158 bh = sb_bread(sb, uspi->s_sbbase +
159 fs32_to_cpu(sb, q->key32) + (n>>shift));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!bh)
Al Viro724bb092015-06-17 12:02:56 -0400161 goto no_block;
162 ptr = (__fs32 *)bh->b_data + (n & mask);
163 if (!grow_chain32(ufsi, bh, ptr, chain, ++q))
164 goto changed;
165 if (!q->key32)
166 goto no_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Al Viro724bb092015-06-17 12:02:56 -0400168 res = fs32_to_cpu(sb, q->key32);
169 goto found;
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171ufs2:
Al Viro724bb092015-06-17 12:02:56 -0400172 if (!grow_chain64(ufsi, NULL, &ufsi->i_u1.u2_i_data[*p++], chain, q))
173 goto changed;
174 if (!q->key64)
175 goto no_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 while (--depth) {
Al Viro724bb092015-06-17 12:02:56 -0400178 __fs64 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct buffer_head *bh;
Al Viro4e3911f2015-06-04 14:13:14 -0400180 unsigned n = *p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Al Viro724bb092015-06-17 12:02:56 -0400182 bh = sb_bread(sb, uspi->s_sbbase +
183 fs64_to_cpu(sb, q->key64) + (n>>shift));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!bh)
Al Viro724bb092015-06-17 12:02:56 -0400185 goto no_block;
186 ptr = (__fs64 *)bh->b_data + (n & mask);
187 if (!grow_chain64(ufsi, bh, ptr, chain, ++q))
188 goto changed;
189 if (!q->key64)
190 goto no_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Al Viro724bb092015-06-17 12:02:56 -0400192 res = fs64_to_cpu(sb, q->key64);
193found:
194 res += uspi->s_sbbase + (frag & uspi->s_fpbmask);
195no_block:
196 while (q > chain) {
197 brelse(q->bh);
198 q--;
199 }
200 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Al Viro724bb092015-06-17 12:02:56 -0400202changed:
203 while (q > chain) {
204 brelse(q->bh);
205 q--;
206 }
207 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700210/**
211 * ufs_inode_getfrag() - allocate new fragment(s)
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700212 * @inode: pointer to inode
213 * @fragment: number of `fragment' which hold pointer
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700214 * to new allocated fragment(s)
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700215 * @new_fragment: number of new allocated fragment(s)
216 * @required: how many fragment(s) we require
217 * @err: we set it if something wrong
218 * @phys: pointer to where we save physical number of new allocated fragments,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700219 * NULL if we allocate not data(indirect blocks for example).
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700220 * @new: we set it if we allocate new block
221 * @locked_page: for ufs_new_fragments()
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700222 */
223static struct buffer_head *
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800224ufs_inode_getfrag(struct inode *inode, u64 fragment,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700225 sector_t new_fragment, unsigned int required, int *err,
226 long *phys, int *new, struct page *locked_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700229 struct super_block *sb = inode->i_sb;
230 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct buffer_head * result;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800232 unsigned blockoff, lastblockoff;
233 u64 tmp, goal, lastfrag, block, lastblock;
234 void *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800236 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, required %u, "
237 "metadata %d\n", inode->i_ino, (unsigned long long)fragment,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700238 (unsigned long long)new_fragment, required, !phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* TODO : to be done for write support
241 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
242 goto ufs2;
243 */
244
245 block = ufs_fragstoblks (fragment);
246 blockoff = ufs_fragnum (fragment);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800247 p = ufs_get_direct_data_ptr(uspi, ufsi, block);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 goal = 0;
250
251repeat:
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800252 tmp = ufs_data_ptr_to_cpu(sb, p);
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 lastfrag = ufsi->i_lastfrag;
255 if (tmp && fragment < lastfrag) {
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700256 if (!phys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800258 if (tmp == ufs_data_ptr_to_cpu(sb, p)) {
259 UFSD("EXIT, result %llu\n",
260 (unsigned long long)tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return result;
262 }
263 brelse (result);
264 goto repeat;
265 } else {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800266 *phys = uspi->s_sbbase + tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return NULL;
268 }
269 }
270
271 lastblock = ufs_fragstoblks (lastfrag);
272 lastblockoff = ufs_fragnum (lastfrag);
273 /*
274 * We will extend file into new block beyond last allocated block
275 */
276 if (lastblock < block) {
277 /*
278 * We must reallocate last allocated block
279 */
280 if (lastblockoff) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800281 p2 = ufs_get_direct_data_ptr(uspi, ufsi, lastblock);
282 tmp = ufs_new_fragments(inode, p2, lastfrag,
283 ufs_data_ptr_to_cpu(sb, p2),
284 uspi->s_fpb - lastblockoff,
285 err, locked_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (!tmp) {
287 if (lastfrag != ufsi->i_lastfrag)
288 goto repeat;
289 else
290 return NULL;
291 }
292 lastfrag = ufsi->i_lastfrag;
Al Viro010d3312015-06-17 12:44:14 -0400293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800295 tmp = ufs_data_ptr_to_cpu(sb,
296 ufs_get_direct_data_ptr(uspi, ufsi,
297 lastblock));
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700298 if (tmp)
299 goal = tmp + uspi->s_fpb;
Al Viro010d3312015-06-17 12:44:14 -0400300 tmp = ufs_new_fragments (inode, p, fragment - blockoff,
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700301 goal, required + blockoff,
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800302 err,
303 phys != NULL ? locked_page : NULL);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800304 } else if (lastblock == block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /*
306 * We will extend last allocated block
307 */
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800308 tmp = ufs_new_fragments(inode, p, fragment -
309 (blockoff - lastblockoff),
310 ufs_data_ptr_to_cpu(sb, p),
311 required + (blockoff - lastblockoff),
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800312 err, phys != NULL ? locked_page : NULL);
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700313 } else /* (lastblock > block) */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /*
315 * We will allocate new block before last allocated block
316 */
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700317 if (block) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800318 tmp = ufs_data_ptr_to_cpu(sb,
319 ufs_get_direct_data_ptr(uspi, ufsi, block - 1));
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700320 if (tmp)
321 goal = tmp + uspi->s_fpb;
322 }
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700323 tmp = ufs_new_fragments(inode, p, fragment - blockoff,
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800324 goal, uspi->s_fpb, err,
325 phys != NULL ? locked_page : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 if (!tmp) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800328 if ((!blockoff && ufs_data_ptr_to_cpu(sb, p)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 (blockoff && lastfrag != ufsi->i_lastfrag))
330 goto repeat;
331 *err = -ENOSPC;
332 return NULL;
333 }
334
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700335 if (!phys) {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800336 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 } else {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800338 *phys = uspi->s_sbbase + tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 result = NULL;
340 *err = 0;
341 *new = 1;
342 }
343
344 inode->i_ctime = CURRENT_TIME_SEC;
345 if (IS_SYNC(inode))
346 ufs_sync_inode (inode);
347 mark_inode_dirty(inode);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800348 UFSD("EXIT, result %llu\n", (unsigned long long)tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return result;
350
351 /* This part : To be implemented ....
352 Required only for writing, not required for READ-ONLY.
353ufs2:
354
355 u2_block = ufs_fragstoblks(fragment);
356 u2_blockoff = ufs_fragnum(fragment);
357 p = ufsi->i_u1.u2_i_data + block;
358 goal = 0;
359
360repeat2:
361 tmp = fs32_to_cpu(sb, *p);
362 lastfrag = ufsi->i_lastfrag;
363
364 */
365}
366
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700367/**
368 * ufs_inode_getblock() - allocate new block
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700369 * @inode: pointer to inode
370 * @bh: pointer to block which hold "pointer" to new allocated block
371 * @fragment: number of `fragment' which hold pointer
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700372 * to new allocated block
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700373 * @new_fragment: number of new allocated fragment
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700374 * (block will hold this fragment and also uspi->s_fpb-1)
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700375 * @err: see ufs_inode_getfrag()
376 * @phys: see ufs_inode_getfrag()
377 * @new: see ufs_inode_getfrag()
378 * @locked_page: see ufs_inode_getfrag()
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700379 */
380static struct buffer_head *
381ufs_inode_getblock(struct inode *inode, struct buffer_head *bh,
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800382 u64 fragment, sector_t new_fragment, int *err,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700383 long *phys, int *new, struct page *locked_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700385 struct super_block *sb = inode->i_sb;
386 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct buffer_head * result;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800388 unsigned blockoff;
389 u64 tmp, goal, block;
390 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 block = ufs_fragstoblks (fragment);
393 blockoff = ufs_fragnum (fragment);
394
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800395 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, metadata %d\n",
396 inode->i_ino, (unsigned long long)fragment,
397 (unsigned long long)new_fragment, !phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 result = NULL;
400 if (!bh)
401 goto out;
402 if (!buffer_uptodate(bh)) {
403 ll_rw_block (READ, 1, &bh);
404 wait_on_buffer (bh);
405 if (!buffer_uptodate(bh))
406 goto out;
407 }
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800408 if (uspi->fs_magic == UFS2_MAGIC)
409 p = (__fs64 *)bh->b_data + block;
410 else
411 p = (__fs32 *)bh->b_data + block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412repeat:
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800413 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (tmp) {
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700415 if (!phys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800417 if (tmp == ufs_data_ptr_to_cpu(sb, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 goto out;
419 brelse (result);
420 goto repeat;
421 } else {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800422 *phys = uspi->s_sbbase + tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 goto out;
424 }
425 }
426
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800427 if (block && (uspi->fs_magic == UFS2_MAGIC ?
428 (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[block-1])) :
429 (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[block-1]))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goal = tmp + uspi->s_fpb;
431 else
432 goal = bh->b_blocknr + uspi->s_fpb;
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700433 tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal,
434 uspi->s_fpb, err, locked_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!tmp) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800436 if (ufs_data_ptr_to_cpu(sb, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 goto repeat;
438 goto out;
Al Viro010d3312015-06-17 12:44:14 -0400439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Evgeniy Dushistovc9a27b52006-06-25 05:47:19 -0700441
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700442 if (!phys) {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800443 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } else {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800445 *phys = uspi->s_sbbase + tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *new = 1;
447 }
448
449 mark_buffer_dirty(bh);
450 if (IS_SYNC(inode))
451 sync_dirty_buffer(bh);
452 inode->i_ctime = CURRENT_TIME_SEC;
453 mark_inode_dirty(inode);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800454 UFSD("result %llu\n", (unsigned long long)tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455out:
456 brelse (bh);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700457 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return result;
459}
460
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700461/**
Alessio Igor Bogani7422caa2011-04-08 19:33:07 +0200462 * ufs_getfrag_block() - `get_block_t' function, interface between UFS and
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700463 * readpage, writepage and so on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
465
Al Viro010d3312015-06-17 12:44:14 -0400466static int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct super_block * sb = inode->i_sb;
Arnd Bergmann788257d2011-01-24 10:14:12 +0100469 struct ufs_sb_info * sbi = UFS_SB(sb);
470 struct ufs_sb_private_info * uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct buffer_head * bh;
472 int ret, err, new;
473 unsigned long ptr,phys;
474 u64 phys64 = 0;
Al Viro010d3312015-06-17 12:44:14 -0400475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (!create) {
Al Viro724bb092015-06-17 12:02:56 -0400477 phys64 = ufs_frag_map(inode, fragment);
Andrew Morton7256d812006-06-29 02:24:29 -0700478 UFSD("phys64 = %llu\n", (unsigned long long)phys64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (phys64)
480 map_bh(bh_result, sb, phys64);
481 return 0;
482 }
483
484 /* This code entered only while writing ....? */
485
486 err = -EIO;
487 new = 0;
488 ret = 0;
489 bh = NULL;
490
Al Viro724bb092015-06-17 12:02:56 -0400491 mutex_lock(&UFS_I(inode)->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700493 UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (fragment >
495 ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb)
496 << uspi->s_fpbshift))
497 goto abort_too_big;
498
499 err = 0;
500 ptr = fragment;
Al Viro010d3312015-06-17 12:44:14 -0400501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /*
503 * ok, these macros clean the logic up a bit and make
504 * it much more readable:
505 */
506#define GET_INODE_DATABLOCK(x) \
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800507 ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new,\
508 bh_result->b_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509#define GET_INODE_PTR(x) \
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800510 ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL,\
511 bh_result->b_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512#define GET_INDIRECT_DATABLOCK(x) \
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700513 ufs_inode_getblock(inode, bh, x, fragment, \
Evgeniy Dushistovd63b7092007-01-05 16:37:04 -0800514 &err, &phys, &new, bh_result->b_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515#define GET_INDIRECT_PTR(x) \
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700516 ufs_inode_getblock(inode, bh, x, fragment, \
Evgeniy Dushistovd63b7092007-01-05 16:37:04 -0800517 &err, NULL, NULL, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (ptr < UFS_NDIR_FRAGMENT) {
520 bh = GET_INODE_DATABLOCK(ptr);
521 goto out;
522 }
523 ptr -= UFS_NDIR_FRAGMENT;
524 if (ptr < (1 << (uspi->s_apbshift + uspi->s_fpbshift))) {
525 bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift));
526 goto get_indirect;
527 }
528 ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift);
529 if (ptr < (1 << (uspi->s_2apbshift + uspi->s_fpbshift))) {
530 bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift));
531 goto get_double;
532 }
533 ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift);
534 bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift));
535 bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask);
536get_double:
537 bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask);
538get_indirect:
539 bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask);
540
541#undef GET_INODE_DATABLOCK
542#undef GET_INODE_PTR
543#undef GET_INDIRECT_DATABLOCK
544#undef GET_INDIRECT_PTR
545
546out:
547 if (err)
548 goto abort;
549 if (new)
550 set_buffer_new(bh_result);
551 map_bh(bh_result, sb, phys);
552abort:
Al Viro724bb092015-06-17 12:02:56 -0400553 mutex_unlock(&UFS_I(inode)->truncate_mutex);
Arnd Bergmann788257d2011-01-24 10:14:12 +0100554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return err;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557abort_too_big:
558 ufs_warning(sb, "ufs_get_block", "block > big");
559 goto abort;
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static int ufs_writepage(struct page *page, struct writeback_control *wbc)
563{
564 return block_write_full_page(page,ufs_getfrag_block,wbc);
565}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static int ufs_readpage(struct file *file, struct page *page)
568{
569 return block_read_full_page(page,ufs_getfrag_block);
570}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700571
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200572int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200574 return __block_write_begin(page, pos, len, ufs_getfrag_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700576
Al Viro010d3312015-06-17 12:44:14 -0400577static void ufs_truncate_blocks(struct inode *);
578
Marco Stornelli83f6e372012-12-15 11:45:14 +0100579static void ufs_write_failed(struct address_space *mapping, loff_t to)
580{
581 struct inode *inode = mapping->host;
582
Al Viro3b7a3a02015-06-16 18:06:40 -0400583 if (to > inode->i_size) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700584 truncate_pagecache(inode, inode->i_size);
Al Viro3b7a3a02015-06-16 18:06:40 -0400585 ufs_truncate_blocks(inode);
586 }
Marco Stornelli83f6e372012-12-15 11:45:14 +0100587}
588
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700589static int ufs_write_begin(struct file *file, struct address_space *mapping,
590 loff_t pos, unsigned len, unsigned flags,
591 struct page **pagep, void **fsdata)
592{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200593 int ret;
594
595 ret = block_write_begin(mapping, pos, len, flags, pagep,
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200596 ufs_getfrag_block);
Marco Stornelli83f6e372012-12-15 11:45:14 +0100597 if (unlikely(ret))
598 ufs_write_failed(mapping, pos + len);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200599
600 return ret;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700601}
602
Al Viro3b7a3a02015-06-16 18:06:40 -0400603static int ufs_write_end(struct file *file, struct address_space *mapping,
604 loff_t pos, unsigned len, unsigned copied,
605 struct page *page, void *fsdata)
606{
607 int ret;
608
609 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
610 if (ret < len)
611 ufs_write_failed(mapping, pos + len);
612 return ret;
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
616{
617 return generic_block_bmap(mapping,block,ufs_getfrag_block);
618}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700619
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700620const struct address_space_operations ufs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 .readpage = ufs_readpage,
622 .writepage = ufs_writepage,
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700623 .write_begin = ufs_write_begin,
Al Viro3b7a3a02015-06-16 18:06:40 -0400624 .write_end = ufs_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 .bmap = ufs_bmap
626};
627
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700628static void ufs_set_inode_ops(struct inode *inode)
629{
630 if (S_ISREG(inode->i_mode)) {
631 inode->i_op = &ufs_file_inode_operations;
632 inode->i_fop = &ufs_file_operations;
633 inode->i_mapping->a_ops = &ufs_aops;
634 } else if (S_ISDIR(inode->i_mode)) {
635 inode->i_op = &ufs_dir_inode_operations;
636 inode->i_fop = &ufs_dir_operations;
637 inode->i_mapping->a_ops = &ufs_aops;
638 } else if (S_ISLNK(inode->i_mode)) {
Al Viro4b8061a2015-05-02 10:28:56 -0400639 if (!inode->i_blocks) {
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700640 inode->i_op = &ufs_fast_symlink_inode_operations;
Al Viro4b8061a2015-05-02 10:28:56 -0400641 inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink;
642 } else {
Dmitry Monakhov311b95492010-04-15 00:56:58 +0200643 inode->i_op = &ufs_symlink_inode_operations;
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700644 inode->i_mapping->a_ops = &ufs_aops;
645 }
646 } else
647 init_special_inode(inode, inode->i_mode,
648 ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
649}
650
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700651static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700654 struct super_block *sb = inode->i_sb;
Al Viro6a9a06d2011-07-26 02:49:13 -0400655 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 /*
658 * Copy data to the in-core inode.
659 */
660 inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200661 set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink));
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700662 if (inode->i_nlink == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700664 return -1;
665 }
Al Viro010d3312015-06-17 12:44:14 -0400666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
668 * Linux now has 32-bit uid and gid, so we can support EFT.
669 */
Eric W. Biederman722354652012-02-10 12:21:22 -0800670 i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode));
671 i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
674 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
675 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
676 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
677 inode->i_mtime.tv_nsec = 0;
678 inode->i_atime.tv_nsec = 0;
679 inode->i_ctime.tv_nsec = 0;
680 inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800681 inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
684 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700685
Al Viro010d3312015-06-17 12:44:14 -0400686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000688 memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr,
689 sizeof(ufs_inode->ui_u2.ui_addr));
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700690 } else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000691 memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink,
Duane Griffinb12903f2009-01-08 22:43:50 +0000692 sizeof(ufs_inode->ui_u2.ui_symlink) - 1);
693 ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700695 return 0;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700696}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700698static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700699{
700 struct ufs_inode_info *ufsi = UFS_I(inode);
701 struct super_block *sb = inode->i_sb;
Al Viro6a9a06d2011-07-26 02:49:13 -0400702 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700704 UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /*
706 * Copy data to the in-core inode.
707 */
708 inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200709 set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink));
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700710 if (inode->i_nlink == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700712 return -1;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /*
716 * Linux now has 32-bit uid and gid, so we can support EFT.
717 */
Eric W. Biederman722354652012-02-10 12:21:22 -0800718 i_uid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_uid));
719 i_gid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
Evgeniy Dushistov21898502007-03-16 13:38:07 -0800722 inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime);
723 inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime);
724 inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime);
725 inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec);
726 inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec);
727 inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800729 inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /*
732 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
733 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
734 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000737 memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr,
738 sizeof(ufs2_inode->ui_u2.ui_addr));
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700739 } else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000740 memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink,
Duane Griffinb12903f2009-01-08 22:43:50 +0000741 sizeof(ufs2_inode->ui_u2.ui_symlink) - 1);
742 ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700744 return 0;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700745}
746
David Howellsb55c4602008-02-07 00:15:48 -0800747struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700748{
David Howellsb55c4602008-02-07 00:15:48 -0800749 struct ufs_inode_info *ufsi;
750 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700751 struct buffer_head * bh;
David Howellsb55c4602008-02-07 00:15:48 -0800752 struct inode *inode;
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700753 int err;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700754
David Howellsb55c4602008-02-07 00:15:48 -0800755 UFSD("ENTER, ino %lu\n", ino);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700756
David Howellsb55c4602008-02-07 00:15:48 -0800757 if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) {
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700758 ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n",
David Howellsb55c4602008-02-07 00:15:48 -0800759 ino);
760 return ERR_PTR(-EIO);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700761 }
762
David Howellsb55c4602008-02-07 00:15:48 -0800763 inode = iget_locked(sb, ino);
764 if (!inode)
765 return ERR_PTR(-ENOMEM);
766 if (!(inode->i_state & I_NEW))
767 return inode;
768
769 ufsi = UFS_I(inode);
770
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700771 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
772 if (!bh) {
773 ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n",
774 inode->i_ino);
775 goto bad_inode;
776 }
777 if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
778 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
779
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700780 err = ufs2_read_inode(inode,
781 ufs2_inode + ufs_inotofsbo(inode->i_ino));
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700782 } else {
783 struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data;
784
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700785 err = ufs1_read_inode(inode,
786 ufs_inode + ufs_inotofsbo(inode->i_ino));
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700787 }
788
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700789 if (err)
790 goto bad_inode;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700791 inode->i_version++;
792 ufsi->i_lastfrag =
793 (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
794 ufsi->i_dir_start_lookup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 ufsi->i_osync = 0;
796
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700797 ufs_set_inode_ops(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 brelse(bh);
800
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700801 UFSD("EXIT\n");
David Howellsb55c4602008-02-07 00:15:48 -0800802 unlock_new_inode(inode);
803 return inode;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700804
805bad_inode:
David Howellsb55c4602008-02-07 00:15:48 -0800806 iget_failed(inode);
807 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800810static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800812 struct super_block *sb = inode->i_sb;
813 struct ufs_inode_info *ufsi = UFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
816 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
817
Eric W. Biederman722354652012-02-10 12:21:22 -0800818 ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode));
819 ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode));
Al Viro010d3312015-06-17 12:44:14 -0400820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
822 ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
823 ufs_inode->ui_atime.tv_usec = 0;
824 ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
825 ufs_inode->ui_ctime.tv_usec = 0;
826 ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
827 ufs_inode->ui_mtime.tv_usec = 0;
828 ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
829 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800830 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800832 if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
834 ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
835 }
836
837 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
838 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
839 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
840 } else if (inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000841 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data,
842 sizeof(ufs_inode->ui_u2.ui_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000845 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
846 sizeof(ufs_inode->ui_u2.ui_symlink));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
849 if (!inode->i_nlink)
850 memset (ufs_inode, 0, sizeof(struct ufs_inode));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800851}
852
853static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
854{
855 struct super_block *sb = inode->i_sb;
856 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800857
858 UFSD("ENTER\n");
859 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
860 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
861
Eric W. Biederman722354652012-02-10 12:21:22 -0800862 ufs_inode->ui_uid = cpu_to_fs32(sb, i_uid_read(inode));
863 ufs_inode->ui_gid = cpu_to_fs32(sb, i_gid_read(inode));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800864
865 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
Evgeniy Dushistov21898502007-03-16 13:38:07 -0800866 ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec);
867 ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec);
868 ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec);
869 ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec);
870 ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec);
871 ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800872
873 ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks);
874 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
875 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
876
877 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
878 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
879 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0];
880 } else if (inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000881 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data,
882 sizeof(ufs_inode->ui_u2.ui_addr));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800883 } else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000884 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
885 sizeof(ufs_inode->ui_u2.ui_symlink));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800886 }
887
888 if (!inode->i_nlink)
889 memset (ufs_inode, 0, sizeof(struct ufs2_inode));
890 UFSD("EXIT\n");
891}
892
893static int ufs_update_inode(struct inode * inode, int do_sync)
894{
895 struct super_block *sb = inode->i_sb;
896 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
897 struct buffer_head * bh;
898
899 UFSD("ENTER, ino %lu\n", inode->i_ino);
900
901 if (inode->i_ino < UFS_ROOTINO ||
902 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
903 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
904 return -1;
905 }
906
907 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
908 if (!bh) {
909 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
910 return -1;
911 }
912 if (uspi->fs_magic == UFS2_MAGIC) {
913 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
914
915 ufs2_update_inode(inode,
916 ufs2_inode + ufs_inotofsbo(inode->i_ino));
917 } else {
918 struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data;
919
920 ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
921 }
Al Viro010d3312015-06-17 12:44:14 -0400922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 mark_buffer_dirty(bh);
924 if (do_sync)
925 sync_dirty_buffer(bh);
926 brelse (bh);
Al Viro010d3312015-06-17 12:44:14 -0400927
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700928 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
930}
931
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100932int ufs_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Al Virof3e0f3d2015-06-16 02:35:14 -0400934 return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937int ufs_sync_inode (struct inode *inode)
938{
939 return ufs_update_inode (inode, 1);
940}
941
Al Viro58e82682010-06-05 19:40:56 -0400942void ufs_evict_inode(struct inode * inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Al Viro58e82682010-06-05 19:40:56 -0400944 int want_delete = 0;
945
946 if (!inode->i_nlink && !is_bad_inode(inode))
947 want_delete = 1;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700948
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700949 truncate_inode_pages_final(&inode->i_data);
Al Viro58e82682010-06-05 19:40:56 -0400950 if (want_delete) {
Al Viro58e82682010-06-05 19:40:56 -0400951 inode->i_size = 0;
Al Virod622f1672015-06-16 18:04:16 -0400952 if (inode->i_blocks)
953 ufs_truncate_blocks(inode);
Al Viro58e82682010-06-05 19:40:56 -0400954 }
955
956 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200957 clear_inode(inode);
Al Viro58e82682010-06-05 19:40:56 -0400958
Al Virof3e0f3d2015-06-16 02:35:14 -0400959 if (want_delete)
Alexey Khoroshilov9ef7db72014-09-02 11:40:17 +0400960 ufs_free_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
Al Viro010d3312015-06-17 12:44:14 -0400962
963#define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
964#define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
965
966static void ufs_trunc_direct(struct inode *inode)
967{
968 struct ufs_inode_info *ufsi = UFS_I(inode);
969 struct super_block * sb;
970 struct ufs_sb_private_info * uspi;
971 void *p;
972 u64 frag1, frag2, frag3, frag4, block1, block2;
973 unsigned frag_to_free, free_count;
974 unsigned i, tmp;
975
976 UFSD("ENTER: ino %lu\n", inode->i_ino);
977
978 sb = inode->i_sb;
979 uspi = UFS_SB(sb)->s_uspi;
980
981 frag_to_free = 0;
982 free_count = 0;
983
984 frag1 = DIRECT_FRAGMENT;
985 frag4 = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
986 frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
987 frag3 = frag4 & ~uspi->s_fpbmask;
988 block1 = block2 = 0;
989 if (frag2 > frag3) {
990 frag2 = frag4;
991 frag3 = frag4 = 0;
992 } else if (frag2 < frag3) {
993 block1 = ufs_fragstoblks (frag2);
994 block2 = ufs_fragstoblks (frag3);
995 }
996
997 UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
998 " frag3 %llu, frag4 %llu\n", inode->i_ino,
999 (unsigned long long)frag1, (unsigned long long)frag2,
1000 (unsigned long long)block1, (unsigned long long)block2,
1001 (unsigned long long)frag3, (unsigned long long)frag4);
1002
1003 if (frag1 >= frag2)
1004 goto next1;
1005
1006 /*
1007 * Free first free fragments
1008 */
1009 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
1010 tmp = ufs_data_ptr_to_cpu(sb, p);
1011 if (!tmp )
1012 ufs_panic (sb, "ufs_trunc_direct", "internal error");
1013 frag2 -= frag1;
1014 frag1 = ufs_fragnum (frag1);
1015
1016 ufs_free_fragments(inode, tmp + frag1, frag2);
1017 mark_inode_dirty(inode);
1018 frag_to_free = tmp + frag1;
1019
1020next1:
1021 /*
1022 * Free whole blocks
1023 */
1024 for (i = block1 ; i < block2; i++) {
1025 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
1026 tmp = ufs_data_ptr_to_cpu(sb, p);
1027 if (!tmp)
1028 continue;
1029 write_seqlock(&ufsi->meta_lock);
1030 ufs_data_ptr_clear(uspi, p);
1031 write_sequnlock(&ufsi->meta_lock);
1032
1033 if (free_count == 0) {
1034 frag_to_free = tmp;
1035 free_count = uspi->s_fpb;
1036 } else if (free_count > 0 && frag_to_free == tmp - free_count)
1037 free_count += uspi->s_fpb;
1038 else {
1039 ufs_free_blocks (inode, frag_to_free, free_count);
1040 frag_to_free = tmp;
1041 free_count = uspi->s_fpb;
1042 }
1043 mark_inode_dirty(inode);
1044 }
1045
1046 if (free_count > 0)
1047 ufs_free_blocks (inode, frag_to_free, free_count);
1048
1049 if (frag3 >= frag4)
1050 goto next3;
1051
1052 /*
1053 * Free last free fragments
1054 */
1055 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
1056 tmp = ufs_data_ptr_to_cpu(sb, p);
1057 if (!tmp )
1058 ufs_panic(sb, "ufs_truncate_direct", "internal error");
1059 frag4 = ufs_fragnum (frag4);
1060 write_seqlock(&ufsi->meta_lock);
1061 ufs_data_ptr_clear(uspi, p);
1062 write_sequnlock(&ufsi->meta_lock);
1063
1064 ufs_free_fragments (inode, tmp, frag4);
1065 mark_inode_dirty(inode);
1066 next3:
1067
1068 UFSD("EXIT: ino %lu\n", inode->i_ino);
1069}
1070
1071
1072static void ufs_trunc_indirect(struct inode *inode, u64 offset, void *p)
1073{
1074 struct super_block * sb;
1075 struct ufs_sb_private_info * uspi;
1076 struct ufs_buffer_head * ind_ubh;
1077 void *ind;
1078 u64 tmp, indirect_block, i, frag_to_free;
1079 unsigned free_count;
1080
1081 UFSD("ENTER: ino %lu, offset %llu, p: %p\n",
1082 inode->i_ino, (unsigned long long)offset, p);
1083
1084 BUG_ON(!p);
1085
1086 sb = inode->i_sb;
1087 uspi = UFS_SB(sb)->s_uspi;
1088
1089 frag_to_free = 0;
1090 free_count = 0;
1091
1092 tmp = ufs_data_ptr_to_cpu(sb, p);
1093 if (!tmp)
1094 return;
1095 ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
1096 if (!ind_ubh) {
1097 write_seqlock(&UFS_I(inode)->meta_lock);
1098 ufs_data_ptr_clear(uspi, p);
1099 write_sequnlock(&UFS_I(inode)->meta_lock);
1100 return;
1101 }
1102
1103 indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
1104 for (i = indirect_block; i < uspi->s_apb; i++) {
1105 ind = ubh_get_data_ptr(uspi, ind_ubh, i);
1106 tmp = ufs_data_ptr_to_cpu(sb, ind);
1107 if (!tmp)
1108 continue;
1109
1110 write_seqlock(&UFS_I(inode)->meta_lock);
1111 ufs_data_ptr_clear(uspi, ind);
1112 write_sequnlock(&UFS_I(inode)->meta_lock);
1113 ubh_mark_buffer_dirty(ind_ubh);
1114 if (free_count == 0) {
1115 frag_to_free = tmp;
1116 free_count = uspi->s_fpb;
1117 } else if (free_count > 0 && frag_to_free == tmp - free_count)
1118 free_count += uspi->s_fpb;
1119 else {
1120 ufs_free_blocks (inode, frag_to_free, free_count);
1121 frag_to_free = tmp;
1122 free_count = uspi->s_fpb;
1123 }
1124
1125 mark_inode_dirty(inode);
1126 }
1127
1128 if (free_count > 0) {
1129 ufs_free_blocks (inode, frag_to_free, free_count);
1130 }
1131 for (i = 0; i < uspi->s_apb; i++)
1132 if (!ufs_is_data_ptr_zero(uspi,
1133 ubh_get_data_ptr(uspi, ind_ubh, i)))
1134 break;
1135 if (i >= uspi->s_apb) {
1136 tmp = ufs_data_ptr_to_cpu(sb, p);
1137 write_seqlock(&UFS_I(inode)->meta_lock);
1138 ufs_data_ptr_clear(uspi, p);
1139 write_sequnlock(&UFS_I(inode)->meta_lock);
1140
1141 ubh_bforget(ind_ubh);
1142 ufs_free_blocks (inode, tmp, uspi->s_fpb);
1143 mark_inode_dirty(inode);
1144 ind_ubh = NULL;
1145 }
1146 if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh))
1147 ubh_sync_block(ind_ubh);
1148 ubh_brelse (ind_ubh);
1149
1150 UFSD("EXIT: ino %lu\n", inode->i_ino);
1151}
1152
1153static void ufs_trunc_dindirect(struct inode *inode, u64 offset, void *p)
1154{
1155 struct super_block * sb;
1156 struct ufs_sb_private_info * uspi;
1157 struct ufs_buffer_head *dind_bh;
1158 u64 i, tmp, dindirect_block;
1159 void *dind;
1160
1161 UFSD("ENTER: ino %lu\n", inode->i_ino);
1162
1163 sb = inode->i_sb;
1164 uspi = UFS_SB(sb)->s_uspi;
1165
1166 dindirect_block = (DIRECT_BLOCK > offset)
1167 ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
1168
1169 tmp = ufs_data_ptr_to_cpu(sb, p);
1170 if (!tmp)
1171 return;
1172 dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
1173 if (!dind_bh) {
1174 write_seqlock(&UFS_I(inode)->meta_lock);
1175 ufs_data_ptr_clear(uspi, p);
1176 write_sequnlock(&UFS_I(inode)->meta_lock);
1177 return;
1178 }
1179
1180 for (i = dindirect_block ; i < uspi->s_apb ; i++) {
1181 dind = ubh_get_data_ptr(uspi, dind_bh, i);
1182 tmp = ufs_data_ptr_to_cpu(sb, dind);
1183 if (!tmp)
1184 continue;
1185 ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
1186 ubh_mark_buffer_dirty(dind_bh);
1187 }
1188
1189 for (i = 0; i < uspi->s_apb; i++)
1190 if (!ufs_is_data_ptr_zero(uspi,
1191 ubh_get_data_ptr(uspi, dind_bh, i)))
1192 break;
1193 if (i >= uspi->s_apb) {
1194 tmp = ufs_data_ptr_to_cpu(sb, p);
1195 write_seqlock(&UFS_I(inode)->meta_lock);
1196 ufs_data_ptr_clear(uspi, p);
1197 write_sequnlock(&UFS_I(inode)->meta_lock);
1198
1199 ubh_bforget(dind_bh);
1200 ufs_free_blocks(inode, tmp, uspi->s_fpb);
1201 mark_inode_dirty(inode);
1202 dind_bh = NULL;
1203 }
1204 if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh))
1205 ubh_sync_block(dind_bh);
1206 ubh_brelse (dind_bh);
1207
1208 UFSD("EXIT: ino %lu\n", inode->i_ino);
1209}
1210
1211static void ufs_trunc_tindirect(struct inode *inode)
1212{
1213 struct super_block *sb = inode->i_sb;
1214 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1215 struct ufs_inode_info *ufsi = UFS_I(inode);
1216 struct ufs_buffer_head * tind_bh;
1217 u64 tindirect_block, tmp, i;
1218 void *tind, *p;
1219
1220 UFSD("ENTER: ino %lu\n", inode->i_ino);
1221
1222 tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
1223 ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
1224
1225 p = ufs_get_direct_data_ptr(uspi, ufsi, UFS_TIND_BLOCK);
1226 if (!(tmp = ufs_data_ptr_to_cpu(sb, p)))
1227 return;
1228 tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
1229 if (!tind_bh) {
1230 write_seqlock(&ufsi->meta_lock);
1231 ufs_data_ptr_clear(uspi, p);
1232 write_sequnlock(&ufsi->meta_lock);
1233 return;
1234 }
1235
1236 for (i = tindirect_block ; i < uspi->s_apb ; i++) {
1237 tind = ubh_get_data_ptr(uspi, tind_bh, i);
1238 ufs_trunc_dindirect(inode, UFS_NDADDR +
1239 uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
1240 ubh_mark_buffer_dirty(tind_bh);
1241 }
1242 for (i = 0; i < uspi->s_apb; i++)
1243 if (!ufs_is_data_ptr_zero(uspi,
1244 ubh_get_data_ptr(uspi, tind_bh, i)))
1245 break;
1246 if (i >= uspi->s_apb) {
1247 tmp = ufs_data_ptr_to_cpu(sb, p);
1248 write_seqlock(&ufsi->meta_lock);
1249 ufs_data_ptr_clear(uspi, p);
1250 write_sequnlock(&ufsi->meta_lock);
1251
1252 ubh_bforget(tind_bh);
1253 ufs_free_blocks(inode, tmp, uspi->s_fpb);
1254 mark_inode_dirty(inode);
1255 tind_bh = NULL;
1256 }
1257 if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh))
1258 ubh_sync_block(tind_bh);
1259 ubh_brelse (tind_bh);
1260
1261 UFSD("EXIT: ino %lu\n", inode->i_ino);
1262}
1263
1264static int ufs_alloc_lastblock(struct inode *inode, loff_t size)
1265{
1266 int err = 0;
1267 struct super_block *sb = inode->i_sb;
1268 struct address_space *mapping = inode->i_mapping;
1269 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1270 unsigned i, end;
1271 sector_t lastfrag;
1272 struct page *lastpage;
1273 struct buffer_head *bh;
1274 u64 phys64;
1275
1276 lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
1277
1278 if (!lastfrag)
1279 goto out;
1280
1281 lastfrag--;
1282
1283 lastpage = ufs_get_locked_page(mapping, lastfrag >>
1284 (PAGE_CACHE_SHIFT - inode->i_blkbits));
1285 if (IS_ERR(lastpage)) {
1286 err = -EIO;
1287 goto out;
1288 }
1289
1290 end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
1291 bh = page_buffers(lastpage);
1292 for (i = 0; i < end; ++i)
1293 bh = bh->b_this_page;
1294
1295
1296 err = ufs_getfrag_block(inode, lastfrag, bh, 1);
1297
1298 if (unlikely(err))
1299 goto out_unlock;
1300
1301 if (buffer_new(bh)) {
1302 clear_buffer_new(bh);
1303 unmap_underlying_metadata(bh->b_bdev,
1304 bh->b_blocknr);
1305 /*
1306 * we do not zeroize fragment, because of
1307 * if it maped to hole, it already contains zeroes
1308 */
1309 set_buffer_uptodate(bh);
1310 mark_buffer_dirty(bh);
1311 set_page_dirty(lastpage);
1312 }
1313
1314 if (lastfrag >= UFS_IND_FRAGMENT) {
1315 end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
1316 phys64 = bh->b_blocknr + 1;
1317 for (i = 0; i < end; ++i) {
1318 bh = sb_getblk(sb, i + phys64);
1319 lock_buffer(bh);
1320 memset(bh->b_data, 0, sb->s_blocksize);
1321 set_buffer_uptodate(bh);
1322 mark_buffer_dirty(bh);
1323 unlock_buffer(bh);
1324 sync_dirty_buffer(bh);
1325 brelse(bh);
1326 }
1327 }
1328out_unlock:
1329 ufs_put_locked_page(lastpage);
1330out:
1331 return err;
1332}
1333
1334static void __ufs_truncate_blocks(struct inode *inode)
1335{
1336 struct ufs_inode_info *ufsi = UFS_I(inode);
1337 struct super_block *sb = inode->i_sb;
1338 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1339
1340 mutex_lock(&ufsi->truncate_mutex);
1341 ufs_trunc_direct(inode);
1342 ufs_trunc_indirect(inode, UFS_IND_BLOCK,
1343 ufs_get_direct_data_ptr(uspi, ufsi, UFS_IND_BLOCK));
1344 ufs_trunc_dindirect(inode, UFS_IND_BLOCK + uspi->s_apb,
1345 ufs_get_direct_data_ptr(uspi, ufsi, UFS_DIND_BLOCK));
1346 ufs_trunc_tindirect(inode);
1347 ufsi->i_lastfrag = DIRECT_FRAGMENT;
1348 mutex_unlock(&ufsi->truncate_mutex);
1349}
1350
1351static int ufs_truncate(struct inode *inode, loff_t size)
1352{
1353 int err = 0;
1354
1355 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
1356 inode->i_ino, (unsigned long long)size,
1357 (unsigned long long)i_size_read(inode));
1358
1359 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1360 S_ISLNK(inode->i_mode)))
1361 return -EINVAL;
1362 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1363 return -EPERM;
1364
1365 err = ufs_alloc_lastblock(inode, size);
1366
1367 if (err)
1368 goto out;
1369
1370 block_truncate_page(inode->i_mapping, size, ufs_getfrag_block);
1371
1372 truncate_setsize(inode, size);
1373
1374 __ufs_truncate_blocks(inode);
1375 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1376 mark_inode_dirty(inode);
1377out:
1378 UFSD("EXIT: err %d\n", err);
1379 return err;
1380}
1381
1382void ufs_truncate_blocks(struct inode *inode)
1383{
1384 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1385 S_ISLNK(inode->i_mode)))
1386 return;
1387 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1388 return;
1389 __ufs_truncate_blocks(inode);
1390}
1391
1392int ufs_setattr(struct dentry *dentry, struct iattr *attr)
1393{
1394 struct inode *inode = d_inode(dentry);
1395 unsigned int ia_valid = attr->ia_valid;
1396 int error;
1397
1398 error = inode_change_ok(inode, attr);
1399 if (error)
1400 return error;
1401
1402 if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
1403 error = ufs_truncate(inode, attr->ia_size);
1404 if (error)
1405 return error;
1406 }
1407
1408 setattr_copy(inode, attr);
1409 mark_inode_dirty(inode);
1410 return 0;
1411}
1412
1413const struct inode_operations ufs_file_inode_operations = {
1414 .setattr = ufs_setattr,
1415};