blob: 156ba3c269068b8079da1ab221250338973ad00e [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 Viro4b7068c2015-06-04 14:27:23 -0400122static u64 ufs_frag_map(struct inode *inode, unsigned offsets[4], int depth)
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 Viro724bb092015-06-17 12:02:56 -0400129 Indirect chain[4], *q = chain;
Al Viro4b7068c2015-06-04 14:27:23 -0400130 unsigned *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unsigned flags = UFS_SB(sb)->s_flags;
Al Viro724bb092015-06-17 12:02:56 -0400132 u64 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Andrew Morton7256d812006-06-29 02:24:29 -0700134 UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
135 uspi->s_fpbshift, uspi->s_apbmask,
136 (unsigned long long)mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 if (depth == 0)
Al Viro724bb092015-06-17 12:02:56 -0400139 goto no_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Al Viro724bb092015-06-17 12:02:56 -0400141again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 p = offsets;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
145 goto ufs2;
146
Al Viro724bb092015-06-17 12:02:56 -0400147 if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q))
148 goto changed;
149 if (!q->key32)
150 goto no_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 while (--depth) {
Al Viro724bb092015-06-17 12:02:56 -0400152 __fs32 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct buffer_head *bh;
Al Viro4e3911f2015-06-04 14:13:14 -0400154 unsigned n = *p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Al Viro724bb092015-06-17 12:02:56 -0400156 bh = sb_bread(sb, uspi->s_sbbase +
157 fs32_to_cpu(sb, q->key32) + (n>>shift));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (!bh)
Al Viro724bb092015-06-17 12:02:56 -0400159 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Al Viro724bb092015-06-17 12:02:56 -0400166 res = fs32_to_cpu(sb, q->key32);
167 goto found;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169ufs2:
Al Viro724bb092015-06-17 12:02:56 -0400170 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 while (--depth) {
Al Viro724bb092015-06-17 12:02:56 -0400176 __fs64 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct buffer_head *bh;
Al Viro4e3911f2015-06-04 14:13:14 -0400178 unsigned n = *p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Al Viro724bb092015-06-17 12:02:56 -0400180 bh = sb_bread(sb, uspi->s_sbbase +
181 fs64_to_cpu(sb, q->key64) + (n>>shift));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (!bh)
Al Viro724bb092015-06-17 12:02:56 -0400183 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Al Viro724bb092015-06-17 12:02:56 -0400190 res = fs64_to_cpu(sb, q->key64);
191found:
Al Viro4b7068c2015-06-04 14:27:23 -0400192 res += uspi->s_sbbase;
Al Viro724bb092015-06-17 12:02:56 -0400193no_block:
194 while (q > chain) {
195 brelse(q->bh);
196 q--;
197 }
198 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Al Viro724bb092015-06-17 12:02:56 -0400200changed:
201 while (q > chain) {
202 brelse(q->bh);
203 q--;
204 }
205 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700208/**
209 * ufs_inode_getfrag() - allocate new fragment(s)
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700210 * @inode: pointer to inode
211 * @fragment: number of `fragment' which hold pointer
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700212 * to new allocated fragment(s)
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700213 * @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,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700217 * NULL if we allocate not data(indirect blocks for example).
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700218 * @new: we set it if we allocate new block
219 * @locked_page: for ufs_new_fragments()
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700220 */
221static struct buffer_head *
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800222ufs_inode_getfrag(struct inode *inode, u64 fragment,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700223 sector_t new_fragment, unsigned int required, int *err,
224 long *phys, int *new, struct page *locked_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700227 struct super_block *sb = inode->i_sb;
228 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800229 unsigned blockoff, lastblockoff;
230 u64 tmp, goal, lastfrag, block, lastblock;
231 void *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800233 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, required %u, "
234 "metadata %d\n", inode->i_ino, (unsigned long long)fragment,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700235 (unsigned long long)new_fragment, required, !phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* 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);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800244 p = ufs_get_direct_data_ptr(uspi, ufsi, block);
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 goal = 0;
247
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800248 tmp = ufs_data_ptr_to_cpu(sb, p);
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 lastfrag = ufsi->i_lastfrag;
Al Virobbb3eb92015-06-19 00:10:00 -0400251 if (tmp && fragment < lastfrag)
252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
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) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800264 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);
Al Viro5a39c252015-06-18 22:39:46 -0400269 if (!tmp)
270 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 lastfrag = ufsi->i_lastfrag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800273 tmp = ufs_data_ptr_to_cpu(sb,
274 ufs_get_direct_data_ptr(uspi, ufsi,
275 lastblock));
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700276 if (tmp)
277 goal = tmp + uspi->s_fpb;
Al Viro010d3312015-06-17 12:44:14 -0400278 tmp = ufs_new_fragments (inode, p, fragment - blockoff,
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700279 goal, required + blockoff,
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800280 err,
281 phys != NULL ? locked_page : NULL);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800282 } else if (lastblock == block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /*
284 * We will extend last allocated block
285 */
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800286 tmp = ufs_new_fragments(inode, p, fragment -
287 (blockoff - lastblockoff),
288 ufs_data_ptr_to_cpu(sb, p),
289 required + (blockoff - lastblockoff),
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800290 err, phys != NULL ? locked_page : NULL);
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700291 } else /* (lastblock > block) */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /*
293 * We will allocate new block before last allocated block
294 */
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700295 if (block) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800296 tmp = ufs_data_ptr_to_cpu(sb,
297 ufs_get_direct_data_ptr(uspi, ufsi, block - 1));
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700298 if (tmp)
299 goal = tmp + uspi->s_fpb;
300 }
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700301 tmp = ufs_new_fragments(inode, p, fragment - blockoff,
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800302 goal, uspi->s_fpb, err,
303 phys != NULL ? locked_page : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 if (!tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *err = -ENOSPC;
307 return NULL;
308 }
309
Al Virobbb3eb92015-06-19 00:10:00 -0400310 if (phys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 *err = 0;
312 *new = 1;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 inode->i_ctime = CURRENT_TIME_SEC;
315 if (IS_SYNC(inode))
316 ufs_sync_inode (inode);
317 mark_inode_dirty(inode);
Al Virobbb3eb92015-06-19 00:10:00 -0400318out:
319 tmp += uspi->s_sbbase + blockoff;
320 if (!phys) {
321 return sb_getblk(sb, tmp);
322 } else {
323 *phys = tmp;
324 return NULL;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* This part : To be implemented ....
328 Required only for writing, not required for READ-ONLY.
329ufs2:
330
331 u2_block = ufs_fragstoblks(fragment);
332 u2_blockoff = ufs_fragnum(fragment);
333 p = ufsi->i_u1.u2_i_data + block;
334 goal = 0;
335
336repeat2:
337 tmp = fs32_to_cpu(sb, *p);
338 lastfrag = ufsi->i_lastfrag;
339
340 */
341}
342
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700343/**
344 * ufs_inode_getblock() - allocate new block
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700345 * @inode: pointer to inode
346 * @bh: pointer to block which hold "pointer" to new allocated block
347 * @fragment: number of `fragment' which hold pointer
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700348 * to new allocated block
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700349 * @new_fragment: number of new allocated fragment
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700350 * (block will hold this fragment and also uspi->s_fpb-1)
Fabian Frederickedc023ca2014-08-08 14:21:08 -0700351 * @err: see ufs_inode_getfrag()
352 * @phys: see ufs_inode_getfrag()
353 * @new: see ufs_inode_getfrag()
354 * @locked_page: see ufs_inode_getfrag()
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700355 */
356static struct buffer_head *
357ufs_inode_getblock(struct inode *inode, struct buffer_head *bh,
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800358 u64 fragment, sector_t new_fragment, int *err,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700359 long *phys, int *new, struct page *locked_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700361 struct super_block *sb = inode->i_sb;
362 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct buffer_head * result;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800364 unsigned blockoff;
Al Virobbb3eb92015-06-19 00:10:00 -0400365 u64 tmp = 0, goal, block;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800366 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 block = ufs_fragstoblks (fragment);
369 blockoff = ufs_fragnum (fragment);
370
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800371 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, metadata %d\n",
372 inode->i_ino, (unsigned long long)fragment,
373 (unsigned long long)new_fragment, !phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 result = NULL;
376 if (!bh)
377 goto out;
378 if (!buffer_uptodate(bh)) {
379 ll_rw_block (READ, 1, &bh);
380 wait_on_buffer (bh);
381 if (!buffer_uptodate(bh))
382 goto out;
383 }
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800384 if (uspi->fs_magic == UFS2_MAGIC)
385 p = (__fs64 *)bh->b_data + block;
386 else
387 p = (__fs32 *)bh->b_data + block;
Al Viro5a39c252015-06-18 22:39:46 -0400388
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800389 tmp = ufs_data_ptr_to_cpu(sb, p);
Al Virobbb3eb92015-06-19 00:10:00 -0400390 if (tmp)
Al Viro5a39c252015-06-18 22:39:46 -0400391 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800393 if (block && (uspi->fs_magic == UFS2_MAGIC ?
394 (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[block-1])) :
395 (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[block-1]))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 goal = tmp + uspi->s_fpb;
397 else
398 goal = bh->b_blocknr + uspi->s_fpb;
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700399 tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal,
400 uspi->s_fpb, err, locked_page);
Al Viro5a39c252015-06-18 22:39:46 -0400401 if (!tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto out;
Evgeniy Dushistovc9a27b52006-06-25 05:47:19 -0700403
Al Virobbb3eb92015-06-19 00:10:00 -0400404 if (new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 *new = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 mark_buffer_dirty(bh);
408 if (IS_SYNC(inode))
409 sync_dirty_buffer(bh);
410 inode->i_ctime = CURRENT_TIME_SEC;
411 mark_inode_dirty(inode);
412out:
413 brelse (bh);
Al Virobbb3eb92015-06-19 00:10:00 -0400414 if (tmp) {
415 tmp += uspi->s_sbbase + blockoff;
416 if (phys) {
417 *phys = tmp;
418 } else {
419 result = sb_getblk(sb, tmp);
420 }
421 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700422 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return result;
424}
425
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700426/**
Alessio Igor Bogani7422caa2011-04-08 19:33:07 +0200427 * ufs_getfrag_block() - `get_block_t' function, interface between UFS and
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700428 * readpage, writepage and so on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
430
Al Viro010d3312015-06-17 12:44:14 -0400431static int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct super_block * sb = inode->i_sb;
Arnd Bergmann788257d2011-01-24 10:14:12 +0100434 struct ufs_sb_info * sbi = UFS_SB(sb);
435 struct ufs_sb_private_info * uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct buffer_head * bh;
437 int ret, err, new;
Al Viro4b7068c2015-06-04 14:27:23 -0400438 unsigned offsets[4];
439 int depth = ufs_block_to_path(inode, fragment >> uspi->s_fpbshift, offsets);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 unsigned long ptr,phys;
441 u64 phys64 = 0;
Al Viro010d3312015-06-17 12:44:14 -0400442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (!create) {
Al Viro4b7068c2015-06-04 14:27:23 -0400444 phys64 = ufs_frag_map(inode, offsets, depth);
445 if (phys64) {
446 phys64 += fragment & uspi->s_fpbmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 map_bh(bh_result, sb, phys64);
Al Viro4b7068c2015-06-04 14:27:23 -0400448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
450 }
451
452 /* This code entered only while writing ....? */
453
454 err = -EIO;
455 new = 0;
456 ret = 0;
457 bh = NULL;
458
Al Viro724bb092015-06-17 12:02:56 -0400459 mutex_lock(&UFS_I(inode)->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700461 UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
Al Viro71dd4282015-06-04 14:34:43 -0400462 if (!depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto abort_too_big;
464
465 err = 0;
466 ptr = fragment;
Al Viro010d3312015-06-17 12:44:14 -0400467
Al Viro71dd4282015-06-04 14:34:43 -0400468 if (depth == 1) {
Al Viro8d9dcf12015-06-19 00:32:42 -0400469 bh = ufs_inode_getfrag(inode, ptr, fragment, 1, &err, &phys,
470 &new, bh_result->b_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 goto out;
472 }
473 ptr -= UFS_NDIR_FRAGMENT;
Al Viro71dd4282015-06-04 14:34:43 -0400474 if (depth == 2) {
Al Viro8d9dcf12015-06-19 00:32:42 -0400475 bh = ufs_inode_getfrag(inode,
476 UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift),
477 fragment, uspi->s_fpb, &err, NULL, NULL,
478 bh_result->b_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto get_indirect;
480 }
481 ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift);
Al Viro71dd4282015-06-04 14:34:43 -0400482 if (depth == 3) {
Al Viro8d9dcf12015-06-19 00:32:42 -0400483 bh = ufs_inode_getfrag(inode,
484 UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift),
485 fragment, uspi->s_fpb, &err, NULL, NULL,
486 bh_result->b_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto get_double;
488 }
489 ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift);
Al Viro8d9dcf12015-06-19 00:32:42 -0400490 bh = ufs_inode_getfrag(inode,
491 UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift),
492 fragment, uspi->s_fpb, &err, NULL, NULL,
493 bh_result->b_page);
494 bh = ufs_inode_getblock(inode, bh,
495 (ptr >> uspi->s_2apbshift) & uspi->s_apbmask,
496 fragment, &err, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497get_double:
Al Viro8d9dcf12015-06-19 00:32:42 -0400498 bh = ufs_inode_getblock(inode, bh,
499 (ptr >> uspi->s_apbshift) & uspi->s_apbmask,
500 fragment, &err, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501get_indirect:
Al Viro8d9dcf12015-06-19 00:32:42 -0400502 bh = ufs_inode_getblock(inode, bh, ptr & uspi->s_apbmask, fragment,
503 &err, &phys, &new, bh_result->b_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505out:
506 if (err)
507 goto abort;
508 if (new)
509 set_buffer_new(bh_result);
510 map_bh(bh_result, sb, phys);
511abort:
Al Viro724bb092015-06-17 12:02:56 -0400512 mutex_unlock(&UFS_I(inode)->truncate_mutex);
Arnd Bergmann788257d2011-01-24 10:14:12 +0100513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return err;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516abort_too_big:
517 ufs_warning(sb, "ufs_get_block", "block > big");
518 goto abort;
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static int ufs_writepage(struct page *page, struct writeback_control *wbc)
522{
523 return block_write_full_page(page,ufs_getfrag_block,wbc);
524}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526static int ufs_readpage(struct file *file, struct page *page)
527{
528 return block_read_full_page(page,ufs_getfrag_block);
529}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700530
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200531int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200533 return __block_write_begin(page, pos, len, ufs_getfrag_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700535
Al Viro010d3312015-06-17 12:44:14 -0400536static void ufs_truncate_blocks(struct inode *);
537
Marco Stornelli83f6e372012-12-15 11:45:14 +0100538static void ufs_write_failed(struct address_space *mapping, loff_t to)
539{
540 struct inode *inode = mapping->host;
541
Al Viro3b7a3a02015-06-16 18:06:40 -0400542 if (to > inode->i_size) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700543 truncate_pagecache(inode, inode->i_size);
Al Viro3b7a3a02015-06-16 18:06:40 -0400544 ufs_truncate_blocks(inode);
545 }
Marco Stornelli83f6e372012-12-15 11:45:14 +0100546}
547
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700548static int ufs_write_begin(struct file *file, struct address_space *mapping,
549 loff_t pos, unsigned len, unsigned flags,
550 struct page **pagep, void **fsdata)
551{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200552 int ret;
553
554 ret = block_write_begin(mapping, pos, len, flags, pagep,
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200555 ufs_getfrag_block);
Marco Stornelli83f6e372012-12-15 11:45:14 +0100556 if (unlikely(ret))
557 ufs_write_failed(mapping, pos + len);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200558
559 return ret;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700560}
561
Al Viro3b7a3a02015-06-16 18:06:40 -0400562static int ufs_write_end(struct file *file, struct address_space *mapping,
563 loff_t pos, unsigned len, unsigned copied,
564 struct page *page, void *fsdata)
565{
566 int ret;
567
568 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
569 if (ret < len)
570 ufs_write_failed(mapping, pos + len);
571 return ret;
572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
575{
576 return generic_block_bmap(mapping,block,ufs_getfrag_block);
577}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700578
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700579const struct address_space_operations ufs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 .readpage = ufs_readpage,
581 .writepage = ufs_writepage,
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700582 .write_begin = ufs_write_begin,
Al Viro3b7a3a02015-06-16 18:06:40 -0400583 .write_end = ufs_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 .bmap = ufs_bmap
585};
586
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700587static void ufs_set_inode_ops(struct inode *inode)
588{
589 if (S_ISREG(inode->i_mode)) {
590 inode->i_op = &ufs_file_inode_operations;
591 inode->i_fop = &ufs_file_operations;
592 inode->i_mapping->a_ops = &ufs_aops;
593 } else if (S_ISDIR(inode->i_mode)) {
594 inode->i_op = &ufs_dir_inode_operations;
595 inode->i_fop = &ufs_dir_operations;
596 inode->i_mapping->a_ops = &ufs_aops;
597 } else if (S_ISLNK(inode->i_mode)) {
Al Viro4b8061a2015-05-02 10:28:56 -0400598 if (!inode->i_blocks) {
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700599 inode->i_op = &ufs_fast_symlink_inode_operations;
Al Viro4b8061a2015-05-02 10:28:56 -0400600 inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink;
601 } else {
Dmitry Monakhov311b95492010-04-15 00:56:58 +0200602 inode->i_op = &ufs_symlink_inode_operations;
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700603 inode->i_mapping->a_ops = &ufs_aops;
604 }
605 } else
606 init_special_inode(inode, inode->i_mode,
607 ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
608}
609
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700610static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700613 struct super_block *sb = inode->i_sb;
Al Viro6a9a06d2011-07-26 02:49:13 -0400614 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /*
617 * Copy data to the in-core inode.
618 */
619 inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200620 set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink));
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700621 if (inode->i_nlink == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700623 return -1;
624 }
Al Viro010d3312015-06-17 12:44:14 -0400625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /*
627 * Linux now has 32-bit uid and gid, so we can support EFT.
628 */
Eric W. Biederman722354652012-02-10 12:21:22 -0800629 i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode));
630 i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
633 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
634 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
635 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
636 inode->i_mtime.tv_nsec = 0;
637 inode->i_atime.tv_nsec = 0;
638 inode->i_ctime.tv_nsec = 0;
639 inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800640 inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
643 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700644
Al Viro010d3312015-06-17 12:44:14 -0400645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000647 memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr,
648 sizeof(ufs_inode->ui_u2.ui_addr));
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700649 } else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000650 memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink,
Duane Griffinb12903f2009-01-08 22:43:50 +0000651 sizeof(ufs_inode->ui_u2.ui_symlink) - 1);
652 ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700654 return 0;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700655}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700657static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700658{
659 struct ufs_inode_info *ufsi = UFS_I(inode);
660 struct super_block *sb = inode->i_sb;
Al Viro6a9a06d2011-07-26 02:49:13 -0400661 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700663 UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * Copy data to the in-core inode.
666 */
667 inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200668 set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink));
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700669 if (inode->i_nlink == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700671 return -1;
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /*
675 * Linux now has 32-bit uid and gid, so we can support EFT.
676 */
Eric W. Biederman722354652012-02-10 12:21:22 -0800677 i_uid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_uid));
678 i_gid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
Evgeniy Dushistov21898502007-03-16 13:38:07 -0800681 inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime);
682 inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime);
683 inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime);
684 inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec);
685 inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec);
686 inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800688 inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
692 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000696 memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr,
697 sizeof(ufs2_inode->ui_u2.ui_addr));
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700698 } else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000699 memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink,
Duane Griffinb12903f2009-01-08 22:43:50 +0000700 sizeof(ufs2_inode->ui_u2.ui_symlink) - 1);
701 ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700703 return 0;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700704}
705
David Howellsb55c4602008-02-07 00:15:48 -0800706struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700707{
David Howellsb55c4602008-02-07 00:15:48 -0800708 struct ufs_inode_info *ufsi;
709 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700710 struct buffer_head * bh;
David Howellsb55c4602008-02-07 00:15:48 -0800711 struct inode *inode;
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700712 int err;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700713
David Howellsb55c4602008-02-07 00:15:48 -0800714 UFSD("ENTER, ino %lu\n", ino);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700715
David Howellsb55c4602008-02-07 00:15:48 -0800716 if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) {
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700717 ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n",
David Howellsb55c4602008-02-07 00:15:48 -0800718 ino);
719 return ERR_PTR(-EIO);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700720 }
721
David Howellsb55c4602008-02-07 00:15:48 -0800722 inode = iget_locked(sb, ino);
723 if (!inode)
724 return ERR_PTR(-ENOMEM);
725 if (!(inode->i_state & I_NEW))
726 return inode;
727
728 ufsi = UFS_I(inode);
729
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700730 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
731 if (!bh) {
732 ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n",
733 inode->i_ino);
734 goto bad_inode;
735 }
736 if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
737 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
738
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700739 err = ufs2_read_inode(inode,
740 ufs2_inode + ufs_inotofsbo(inode->i_ino));
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700741 } else {
742 struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data;
743
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700744 err = ufs1_read_inode(inode,
745 ufs_inode + ufs_inotofsbo(inode->i_ino));
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700746 }
747
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700748 if (err)
749 goto bad_inode;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700750 inode->i_version++;
751 ufsi->i_lastfrag =
752 (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
753 ufsi->i_dir_start_lookup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 ufsi->i_osync = 0;
755
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700756 ufs_set_inode_ops(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 brelse(bh);
759
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700760 UFSD("EXIT\n");
David Howellsb55c4602008-02-07 00:15:48 -0800761 unlock_new_inode(inode);
762 return inode;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700763
764bad_inode:
David Howellsb55c4602008-02-07 00:15:48 -0800765 iget_failed(inode);
766 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800769static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800771 struct super_block *sb = inode->i_sb;
772 struct ufs_inode_info *ufsi = UFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
775 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
776
Eric W. Biederman722354652012-02-10 12:21:22 -0800777 ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode));
778 ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode));
Al Viro010d3312015-06-17 12:44:14 -0400779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
781 ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
782 ufs_inode->ui_atime.tv_usec = 0;
783 ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
784 ufs_inode->ui_ctime.tv_usec = 0;
785 ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
786 ufs_inode->ui_mtime.tv_usec = 0;
787 ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
788 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800789 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800791 if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
793 ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
794 }
795
796 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
797 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
798 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
799 } else if (inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000800 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data,
801 sizeof(ufs_inode->ui_u2.ui_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000804 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
805 sizeof(ufs_inode->ui_u2.ui_symlink));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808 if (!inode->i_nlink)
809 memset (ufs_inode, 0, sizeof(struct ufs_inode));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800810}
811
812static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
813{
814 struct super_block *sb = inode->i_sb;
815 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800816
817 UFSD("ENTER\n");
818 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
819 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
820
Eric W. Biederman722354652012-02-10 12:21:22 -0800821 ufs_inode->ui_uid = cpu_to_fs32(sb, i_uid_read(inode));
822 ufs_inode->ui_gid = cpu_to_fs32(sb, i_gid_read(inode));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800823
824 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
Evgeniy Dushistov21898502007-03-16 13:38:07 -0800825 ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec);
826 ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec);
827 ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec);
828 ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec);
829 ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec);
830 ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800831
832 ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks);
833 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
834 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
835
836 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
837 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
838 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0];
839 } else if (inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000840 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data,
841 sizeof(ufs_inode->ui_u2.ui_addr));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800842 } else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000843 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
844 sizeof(ufs_inode->ui_u2.ui_symlink));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800845 }
846
847 if (!inode->i_nlink)
848 memset (ufs_inode, 0, sizeof(struct ufs2_inode));
849 UFSD("EXIT\n");
850}
851
852static int ufs_update_inode(struct inode * inode, int do_sync)
853{
854 struct super_block *sb = inode->i_sb;
855 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
856 struct buffer_head * bh;
857
858 UFSD("ENTER, ino %lu\n", inode->i_ino);
859
860 if (inode->i_ino < UFS_ROOTINO ||
861 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
862 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
863 return -1;
864 }
865
866 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
867 if (!bh) {
868 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
869 return -1;
870 }
871 if (uspi->fs_magic == UFS2_MAGIC) {
872 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
873
874 ufs2_update_inode(inode,
875 ufs2_inode + ufs_inotofsbo(inode->i_ino));
876 } else {
877 struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data;
878
879 ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
880 }
Al Viro010d3312015-06-17 12:44:14 -0400881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 mark_buffer_dirty(bh);
883 if (do_sync)
884 sync_dirty_buffer(bh);
885 brelse (bh);
Al Viro010d3312015-06-17 12:44:14 -0400886
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700887 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 0;
889}
890
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100891int ufs_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Al Virof3e0f3d2015-06-16 02:35:14 -0400893 return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896int ufs_sync_inode (struct inode *inode)
897{
898 return ufs_update_inode (inode, 1);
899}
900
Al Viro58e82682010-06-05 19:40:56 -0400901void ufs_evict_inode(struct inode * inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Al Viro58e82682010-06-05 19:40:56 -0400903 int want_delete = 0;
904
905 if (!inode->i_nlink && !is_bad_inode(inode))
906 want_delete = 1;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700907
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700908 truncate_inode_pages_final(&inode->i_data);
Al Viro58e82682010-06-05 19:40:56 -0400909 if (want_delete) {
Al Viro58e82682010-06-05 19:40:56 -0400910 inode->i_size = 0;
Al Virod622f1672015-06-16 18:04:16 -0400911 if (inode->i_blocks)
912 ufs_truncate_blocks(inode);
Al Viro58e82682010-06-05 19:40:56 -0400913 }
914
915 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200916 clear_inode(inode);
Al Viro58e82682010-06-05 19:40:56 -0400917
Al Virof3e0f3d2015-06-16 02:35:14 -0400918 if (want_delete)
Alexey Khoroshilov9ef7db72014-09-02 11:40:17 +0400919 ufs_free_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
Al Viro010d3312015-06-17 12:44:14 -0400921
Al Viroa138b4b2015-06-18 02:18:54 -0400922struct to_free {
923 struct inode *inode;
924 u64 to;
925 unsigned count;
926};
927
928static inline void free_data(struct to_free *ctx, u64 from, unsigned count)
929{
930 if (ctx->count && ctx->to != from) {
931 ufs_free_blocks(ctx->inode, ctx->to - ctx->count, ctx->count);
932 ctx->count = 0;
933 }
934 ctx->count += count;
935 ctx->to = from + count;
936}
937
Al Viro010d3312015-06-17 12:44:14 -0400938#define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
939#define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
940
941static void ufs_trunc_direct(struct inode *inode)
942{
943 struct ufs_inode_info *ufsi = UFS_I(inode);
944 struct super_block * sb;
945 struct ufs_sb_private_info * uspi;
946 void *p;
947 u64 frag1, frag2, frag3, frag4, block1, block2;
Al Viroa138b4b2015-06-18 02:18:54 -0400948 struct to_free ctx = {.inode = inode};
Al Viro010d3312015-06-17 12:44:14 -0400949 unsigned i, tmp;
950
951 UFSD("ENTER: ino %lu\n", inode->i_ino);
952
953 sb = inode->i_sb;
954 uspi = UFS_SB(sb)->s_uspi;
955
Al Viro010d3312015-06-17 12:44:14 -0400956 frag1 = DIRECT_FRAGMENT;
957 frag4 = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
958 frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
959 frag3 = frag4 & ~uspi->s_fpbmask;
960 block1 = block2 = 0;
961 if (frag2 > frag3) {
962 frag2 = frag4;
963 frag3 = frag4 = 0;
964 } else if (frag2 < frag3) {
965 block1 = ufs_fragstoblks (frag2);
966 block2 = ufs_fragstoblks (frag3);
967 }
968
969 UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
970 " frag3 %llu, frag4 %llu\n", inode->i_ino,
971 (unsigned long long)frag1, (unsigned long long)frag2,
972 (unsigned long long)block1, (unsigned long long)block2,
973 (unsigned long long)frag3, (unsigned long long)frag4);
974
975 if (frag1 >= frag2)
976 goto next1;
977
978 /*
979 * Free first free fragments
980 */
981 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
982 tmp = ufs_data_ptr_to_cpu(sb, p);
983 if (!tmp )
984 ufs_panic (sb, "ufs_trunc_direct", "internal error");
985 frag2 -= frag1;
986 frag1 = ufs_fragnum (frag1);
987
988 ufs_free_fragments(inode, tmp + frag1, frag2);
Al Viro010d3312015-06-17 12:44:14 -0400989
990next1:
991 /*
992 * Free whole blocks
993 */
994 for (i = block1 ; i < block2; i++) {
995 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
996 tmp = ufs_data_ptr_to_cpu(sb, p);
997 if (!tmp)
998 continue;
999 write_seqlock(&ufsi->meta_lock);
1000 ufs_data_ptr_clear(uspi, p);
1001 write_sequnlock(&ufsi->meta_lock);
1002
Al Viroa138b4b2015-06-18 02:18:54 -04001003 free_data(&ctx, tmp, uspi->s_fpb);
Al Viro010d3312015-06-17 12:44:14 -04001004 }
1005
Al Viroa138b4b2015-06-18 02:18:54 -04001006 free_data(&ctx, 0, 0);
Al Viro010d3312015-06-17 12:44:14 -04001007
1008 if (frag3 >= frag4)
1009 goto next3;
1010
1011 /*
1012 * Free last free fragments
1013 */
1014 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
1015 tmp = ufs_data_ptr_to_cpu(sb, p);
1016 if (!tmp )
1017 ufs_panic(sb, "ufs_truncate_direct", "internal error");
1018 frag4 = ufs_fragnum (frag4);
1019 write_seqlock(&ufsi->meta_lock);
1020 ufs_data_ptr_clear(uspi, p);
1021 write_sequnlock(&ufsi->meta_lock);
1022
1023 ufs_free_fragments (inode, tmp, frag4);
Al Viro010d3312015-06-17 12:44:14 -04001024 next3:
1025
1026 UFSD("EXIT: ino %lu\n", inode->i_ino);
1027}
1028
Al Viro163073d2015-06-18 20:07:08 -04001029static void free_full_branch(struct inode *inode, u64 ind_block, int depth)
Al Viro010d3312015-06-17 12:44:14 -04001030{
1031 struct super_block *sb = inode->i_sb;
1032 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Al Viro163073d2015-06-18 20:07:08 -04001033 struct ufs_buffer_head *ubh = ubh_bread(sb, ind_block, uspi->s_bsize);
Al Viro18ca51d2015-06-18 13:45:07 -04001034 unsigned i;
Al Viro010d3312015-06-17 12:44:14 -04001035
Al Viro163073d2015-06-18 20:07:08 -04001036 if (!ubh)
Al Viro010d3312015-06-17 12:44:14 -04001037 return;
Al Viro010d3312015-06-17 12:44:14 -04001038
Al Viro9e0fbbd2015-06-18 15:33:47 -04001039 if (--depth) {
Al Viro163073d2015-06-18 20:07:08 -04001040 for (i = 0; i < uspi->s_apb; i++) {
1041 void *p = ubh_get_data_ptr(uspi, ubh, i);
1042 u64 block = ufs_data_ptr_to_cpu(sb, p);
Al Virocc7231e2015-06-18 20:14:02 -04001043 if (block)
Al Viro163073d2015-06-18 20:07:08 -04001044 free_full_branch(inode, block, depth);
Al Viro6d1ebbc2015-06-18 17:11:49 -04001045 }
1046 } else {
1047 struct to_free ctx = {.inode = inode};
1048
1049 for (i = 0; i < uspi->s_apb; i++) {
Al Viro163073d2015-06-18 20:07:08 -04001050 void *p = ubh_get_data_ptr(uspi, ubh, i);
1051 u64 block = ufs_data_ptr_to_cpu(sb, p);
Al Virocc7231e2015-06-18 20:14:02 -04001052 if (block)
Al Viro163073d2015-06-18 20:07:08 -04001053 free_data(&ctx, block, uspi->s_fpb);
Al Viro6d1ebbc2015-06-18 17:11:49 -04001054 }
1055 free_data(&ctx, 0, 0);
1056 }
Al Viro6d1ebbc2015-06-18 17:11:49 -04001057
1058 ubh_bforget(ubh);
Al Viro163073d2015-06-18 20:07:08 -04001059 ufs_free_blocks(inode, ind_block, uspi->s_fpb);
Al Viro6d1ebbc2015-06-18 17:11:49 -04001060}
1061
Al Viro7b4e4f72015-06-18 19:13:02 -04001062static void free_branch_tail(struct inode *inode, unsigned from, struct ufs_buffer_head *ubh, int depth)
Al Viro6d1ebbc2015-06-18 17:11:49 -04001063{
1064 struct super_block *sb = inode->i_sb;
1065 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Al Viro6d1ebbc2015-06-18 17:11:49 -04001066 unsigned i;
1067
Al Viro6d1ebbc2015-06-18 17:11:49 -04001068 if (--depth) {
Al Viro7b4e4f72015-06-18 19:13:02 -04001069 for (i = from; i < uspi->s_apb ; i++) {
Al Viro163073d2015-06-18 20:07:08 -04001070 void *p = ubh_get_data_ptr(uspi, ubh, i);
1071 u64 block = ufs_data_ptr_to_cpu(sb, p);
1072 if (block) {
1073 write_seqlock(&UFS_I(inode)->meta_lock);
1074 ufs_data_ptr_clear(uspi, p);
1075 write_sequnlock(&UFS_I(inode)->meta_lock);
1076 ubh_mark_buffer_dirty(ubh);
1077 free_full_branch(inode, block, depth);
1078 }
Al Viroa9657422015-06-18 16:13:56 -04001079 }
Al Viro9e0fbbd2015-06-18 15:33:47 -04001080 } else {
Al Viroa138b4b2015-06-18 02:18:54 -04001081 struct to_free ctx = {.inode = inode};
Al Viro9e0fbbd2015-06-18 15:33:47 -04001082
1083 for (i = from; i < uspi->s_apb; i++) {
Al Viro163073d2015-06-18 20:07:08 -04001084 void *p = ubh_get_data_ptr(uspi, ubh, i);
1085 u64 block = ufs_data_ptr_to_cpu(sb, p);
1086 if (block) {
1087 write_seqlock(&UFS_I(inode)->meta_lock);
1088 ufs_data_ptr_clear(uspi, p);
1089 write_sequnlock(&UFS_I(inode)->meta_lock);
1090 ubh_mark_buffer_dirty(ubh);
1091 free_data(&ctx, block, uspi->s_fpb);
Al Viro163073d2015-06-18 20:07:08 -04001092 }
Al Viro9e0fbbd2015-06-18 15:33:47 -04001093 }
Al Viroa138b4b2015-06-18 02:18:54 -04001094 free_data(&ctx, 0, 0);
Al Viro010d3312015-06-17 12:44:14 -04001095 }
Al Viro9e0fbbd2015-06-18 15:33:47 -04001096 if (IS_SYNC(inode) && ubh_buffer_dirty(ubh))
1097 ubh_sync_block(ubh);
1098 ubh_brelse(ubh);
Al Viro010d3312015-06-17 12:44:14 -04001099}
1100
1101static int ufs_alloc_lastblock(struct inode *inode, loff_t size)
1102{
1103 int err = 0;
1104 struct super_block *sb = inode->i_sb;
1105 struct address_space *mapping = inode->i_mapping;
1106 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1107 unsigned i, end;
1108 sector_t lastfrag;
1109 struct page *lastpage;
1110 struct buffer_head *bh;
1111 u64 phys64;
1112
1113 lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
1114
1115 if (!lastfrag)
1116 goto out;
1117
1118 lastfrag--;
1119
1120 lastpage = ufs_get_locked_page(mapping, lastfrag >>
1121 (PAGE_CACHE_SHIFT - inode->i_blkbits));
1122 if (IS_ERR(lastpage)) {
1123 err = -EIO;
1124 goto out;
1125 }
1126
1127 end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
1128 bh = page_buffers(lastpage);
1129 for (i = 0; i < end; ++i)
1130 bh = bh->b_this_page;
1131
1132
1133 err = ufs_getfrag_block(inode, lastfrag, bh, 1);
1134
1135 if (unlikely(err))
1136 goto out_unlock;
1137
1138 if (buffer_new(bh)) {
1139 clear_buffer_new(bh);
1140 unmap_underlying_metadata(bh->b_bdev,
1141 bh->b_blocknr);
1142 /*
1143 * we do not zeroize fragment, because of
1144 * if it maped to hole, it already contains zeroes
1145 */
1146 set_buffer_uptodate(bh);
1147 mark_buffer_dirty(bh);
1148 set_page_dirty(lastpage);
1149 }
1150
1151 if (lastfrag >= UFS_IND_FRAGMENT) {
1152 end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
1153 phys64 = bh->b_blocknr + 1;
1154 for (i = 0; i < end; ++i) {
1155 bh = sb_getblk(sb, i + phys64);
1156 lock_buffer(bh);
1157 memset(bh->b_data, 0, sb->s_blocksize);
1158 set_buffer_uptodate(bh);
1159 mark_buffer_dirty(bh);
1160 unlock_buffer(bh);
1161 sync_dirty_buffer(bh);
1162 brelse(bh);
1163 }
1164 }
1165out_unlock:
1166 ufs_put_locked_page(lastpage);
1167out:
1168 return err;
1169}
1170
1171static void __ufs_truncate_blocks(struct inode *inode)
1172{
1173 struct ufs_inode_info *ufsi = UFS_I(inode);
1174 struct super_block *sb = inode->i_sb;
1175 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Al Viro7bad5932015-06-18 14:21:09 -04001176 unsigned offsets[4];
Al Viro31cd0432015-06-17 01:10:03 -04001177 int depth = ufs_block_to_path(inode, DIRECT_BLOCK, offsets);
Al Viro6775e242015-06-18 14:55:50 -04001178 int depth2;
Al Viro42432732015-06-18 15:47:17 -04001179 unsigned i;
Al Viro7b4e4f72015-06-18 19:13:02 -04001180 struct ufs_buffer_head *ubh[3];
1181 void *p;
1182 u64 block;
Al Viro6775e242015-06-18 14:55:50 -04001183
1184 if (!depth)
1185 return;
1186
1187 /* find the last non-zero in offsets[] */
1188 for (depth2 = depth - 1; depth2; depth2--)
1189 if (offsets[depth2])
1190 break;
Al Viro010d3312015-06-17 12:44:14 -04001191
1192 mutex_lock(&ufsi->truncate_mutex);
Al Viro42432732015-06-18 15:47:17 -04001193 if (depth == 1) {
Al Viro31cd0432015-06-17 01:10:03 -04001194 ufs_trunc_direct(inode);
Al Viro42432732015-06-18 15:47:17 -04001195 offsets[0] = UFS_IND_BLOCK;
1196 } else {
Al Viro7b4e4f72015-06-18 19:13:02 -04001197 /* get the blocks that should be partially emptied */
1198 p = ufs_get_direct_data_ptr(uspi, ufsi, offsets[0]);
1199 for (i = 0; i < depth2; i++) {
1200 offsets[i]++; /* next branch is fully freed */
1201 block = ufs_data_ptr_to_cpu(sb, p);
1202 if (!block)
1203 break;
1204 ubh[i] = ubh_bread(sb, block, uspi->s_bsize);
1205 if (!ubh[i]) {
1206 write_seqlock(&ufsi->meta_lock);
1207 ufs_data_ptr_clear(uspi, p);
1208 write_sequnlock(&ufsi->meta_lock);
1209 break;
1210 }
1211 p = ubh_get_data_ptr(uspi, ubh[i], offsets[i + 1]);
1212 }
Al Virof53bd142015-06-18 20:17:32 -04001213 while (i--)
Al Viro7b4e4f72015-06-18 19:13:02 -04001214 free_branch_tail(inode, offsets[i + 1], ubh[i], depth - i - 1);
Al Viro31cd0432015-06-17 01:10:03 -04001215 }
Al Viro42432732015-06-18 15:47:17 -04001216 for (i = offsets[0]; i <= UFS_TIND_BLOCK; i++) {
Al Viro163073d2015-06-18 20:07:08 -04001217 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
1218 block = ufs_data_ptr_to_cpu(sb, p);
1219 if (block) {
1220 write_seqlock(&ufsi->meta_lock);
1221 ufs_data_ptr_clear(uspi, p);
1222 write_sequnlock(&ufsi->meta_lock);
1223 free_full_branch(inode, block, i - UFS_IND_BLOCK + 1);
1224 }
Al Viro42432732015-06-18 15:47:17 -04001225 }
Al Viro010d3312015-06-17 12:44:14 -04001226 ufsi->i_lastfrag = DIRECT_FRAGMENT;
Al Virob6eede02015-06-18 20:09:39 -04001227 mark_inode_dirty(inode);
Al Viro010d3312015-06-17 12:44:14 -04001228 mutex_unlock(&ufsi->truncate_mutex);
1229}
1230
1231static int ufs_truncate(struct inode *inode, loff_t size)
1232{
1233 int err = 0;
1234
1235 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
1236 inode->i_ino, (unsigned long long)size,
1237 (unsigned long long)i_size_read(inode));
1238
1239 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1240 S_ISLNK(inode->i_mode)))
1241 return -EINVAL;
1242 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1243 return -EPERM;
1244
1245 err = ufs_alloc_lastblock(inode, size);
1246
1247 if (err)
1248 goto out;
1249
1250 block_truncate_page(inode->i_mapping, size, ufs_getfrag_block);
1251
1252 truncate_setsize(inode, size);
1253
1254 __ufs_truncate_blocks(inode);
1255 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1256 mark_inode_dirty(inode);
1257out:
1258 UFSD("EXIT: err %d\n", err);
1259 return err;
1260}
1261
1262void ufs_truncate_blocks(struct inode *inode)
1263{
1264 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1265 S_ISLNK(inode->i_mode)))
1266 return;
1267 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1268 return;
1269 __ufs_truncate_blocks(inode);
1270}
1271
1272int ufs_setattr(struct dentry *dentry, struct iattr *attr)
1273{
1274 struct inode *inode = d_inode(dentry);
1275 unsigned int ia_valid = attr->ia_valid;
1276 int error;
1277
1278 error = inode_change_ok(inode, attr);
1279 if (error)
1280 return error;
1281
1282 if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
1283 error = ufs_truncate(inode, attr->ia_size);
1284 if (error)
1285 return error;
1286 }
1287
1288 setattr_copy(inode, attr);
1289 mark_inode_dirty(inode);
1290 return 0;
1291}
1292
1293const struct inode_operations ufs_file_inode_operations = {
1294 .setattr = ufs_setattr,
1295};