blob: 72282043a8f465edf1b38b52b1d620496c7e1b8b [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>
29#include <asm/system.h>
30
31#include <linux/errno.h>
32#include <linux/fs.h>
33#include <linux/ufs_fs.h>
34#include <linux/time.h>
35#include <linux/stat.h>
36#include <linux/string.h>
37#include <linux/mm.h>
38#include <linux/smp_lock.h>
39#include <linux/buffer_head.h>
40
41#include "swab.h"
42#include "util.h"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static int ufs_block_to_path(struct inode *inode, sector_t i_block, sector_t offsets[4])
45{
46 struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi;
47 int ptrs = uspi->s_apb;
48 int ptrs_bits = uspi->s_apbshift;
49 const long direct_blocks = UFS_NDADDR,
50 indirect_blocks = ptrs,
51 double_blocks = (1 << (ptrs_bits * 2));
52 int n = 0;
53
54
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070055 UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (i_block < 0) {
57 ufs_warning(inode->i_sb, "ufs_block_to_path", "block < 0");
58 } else if (i_block < direct_blocks) {
59 offsets[n++] = i_block;
60 } else if ((i_block -= direct_blocks) < indirect_blocks) {
61 offsets[n++] = UFS_IND_BLOCK;
62 offsets[n++] = i_block;
63 } else if ((i_block -= indirect_blocks) < double_blocks) {
64 offsets[n++] = UFS_DIND_BLOCK;
65 offsets[n++] = i_block >> ptrs_bits;
66 offsets[n++] = i_block & (ptrs - 1);
67 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
68 offsets[n++] = UFS_TIND_BLOCK;
69 offsets[n++] = i_block >> (ptrs_bits * 2);
70 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
71 offsets[n++] = i_block & (ptrs - 1);
72 } else {
73 ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
74 }
75 return n;
76}
77
78/*
79 * Returns the location of the fragment from
80 * the begining of the filesystem.
81 */
82
83u64 ufs_frag_map(struct inode *inode, sector_t frag)
84{
85 struct ufs_inode_info *ufsi = UFS_I(inode);
86 struct super_block *sb = inode->i_sb;
87 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
88 u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift;
89 int shift = uspi->s_apbshift-uspi->s_fpbshift;
90 sector_t offsets[4], *p;
91 int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets);
92 u64 ret = 0L;
93 __fs32 block;
94 __fs64 u2_block = 0L;
95 unsigned flags = UFS_SB(sb)->s_flags;
96 u64 temp = 0L;
97
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070098 UFSD(": frag = %llu depth = %d\n", (unsigned long long)frag, depth);
99 UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",uspi->s_fpbshift,uspi->s_apbmask,mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 if (depth == 0)
102 return 0;
103
104 p = offsets;
105
106 lock_kernel();
107 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
108 goto ufs2;
109
110 block = ufsi->i_u1.i_data[*p++];
111 if (!block)
112 goto out;
113 while (--depth) {
114 struct buffer_head *bh;
115 sector_t n = *p++;
116
117 bh = sb_bread(sb, uspi->s_sbbase + fs32_to_cpu(sb, block)+(n>>shift));
118 if (!bh)
119 goto out;
120 block = ((__fs32 *) bh->b_data)[n & mask];
121 brelse (bh);
122 if (!block)
123 goto out;
124 }
125 ret = (u64) (uspi->s_sbbase + fs32_to_cpu(sb, block) + (frag & uspi->s_fpbmask));
126 goto out;
127ufs2:
128 u2_block = ufsi->i_u1.u2_i_data[*p++];
129 if (!u2_block)
130 goto out;
131
132
133 while (--depth) {
134 struct buffer_head *bh;
135 sector_t n = *p++;
136
137
138 temp = (u64)(uspi->s_sbbase) + fs64_to_cpu(sb, u2_block);
139 bh = sb_bread(sb, temp +(u64) (n>>shift));
140 if (!bh)
141 goto out;
142 u2_block = ((__fs64 *)bh->b_data)[n & mask];
143 brelse(bh);
144 if (!u2_block)
145 goto out;
146 }
147 temp = (u64)uspi->s_sbbase + fs64_to_cpu(sb, u2_block);
148 ret = temp + (u64) (frag & uspi->s_fpbmask);
149
150out:
151 unlock_kernel();
152 return ret;
153}
154
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700155static void ufs_clear_frag(struct inode *inode, struct buffer_head *bh)
Evgeniy Dushistovc9a27b52006-06-25 05:47:19 -0700156{
157 lock_buffer(bh);
158 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
159 set_buffer_uptodate(bh);
160 mark_buffer_dirty(bh);
161 unlock_buffer(bh);
162 if (IS_SYNC(inode))
163 sync_dirty_buffer(bh);
164}
165
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700166static struct buffer_head *
167ufs_clear_frags(struct inode *inode, sector_t beg,
168 unsigned int n)
169{
170 struct buffer_head *res, *bh;
171 sector_t end = beg + n;
172
173 res = sb_getblk(inode->i_sb, beg);
174 ufs_clear_frag(inode, res);
175 for (++beg; beg < end; ++beg) {
176 bh = sb_getblk(inode->i_sb, beg);
177 ufs_clear_frag(inode, bh);
178 }
179 return res;
180}
181
182/**
183 * ufs_inode_getfrag() - allocate new fragment(s)
184 * @inode - pointer to inode
185 * @fragment - number of `fragment' which hold pointer
186 * to new allocated fragment(s)
187 * @new_fragment - number of new allocated fragment(s)
188 * @required - how many fragment(s) we require
189 * @err - we set it if something wrong
190 * @phys - pointer to where we save physical number of new allocated fragments,
191 * NULL if we allocate not data(indirect blocks for example).
192 * @new - we set it if we allocate new block
193 * @locked_page - for ufs_new_fragments()
194 */
195static struct buffer_head *
196ufs_inode_getfrag(struct inode *inode, unsigned int fragment,
197 sector_t new_fragment, unsigned int required, int *err,
198 long *phys, int *new, struct page *locked_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700201 struct super_block *sb = inode->i_sb;
202 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct buffer_head * result;
204 unsigned block, blockoff, lastfrag, lastblock, lastblockoff;
205 unsigned tmp, goal;
206 __fs32 * p, * p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700208 UFSD("ENTER, ino %lu, fragment %u, new_fragment %llu, required %u, "
209 "metadata %d\n", inode->i_ino, fragment,
210 (unsigned long long)new_fragment, required, !phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* TODO : to be done for write support
213 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
214 goto ufs2;
215 */
216
217 block = ufs_fragstoblks (fragment);
218 blockoff = ufs_fragnum (fragment);
219 p = ufsi->i_u1.i_data + block;
220 goal = 0;
221
222repeat:
223 tmp = fs32_to_cpu(sb, *p);
224 lastfrag = ufsi->i_lastfrag;
225 if (tmp && fragment < lastfrag) {
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700226 if (!phys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
228 if (tmp == fs32_to_cpu(sb, *p)) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700229 UFSD("EXIT, result %u\n", tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return result;
231 }
232 brelse (result);
233 goto repeat;
234 } else {
Evgeniy Dushistovc9a27b52006-06-25 05:47:19 -0700235 *phys = tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return NULL;
237 }
238 }
239
240 lastblock = ufs_fragstoblks (lastfrag);
241 lastblockoff = ufs_fragnum (lastfrag);
242 /*
243 * We will extend file into new block beyond last allocated block
244 */
245 if (lastblock < block) {
246 /*
247 * We must reallocate last allocated block
248 */
249 if (lastblockoff) {
250 p2 = ufsi->i_u1.i_data + lastblock;
251 tmp = ufs_new_fragments (inode, p2, lastfrag,
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700252 fs32_to_cpu(sb, *p2), uspi->s_fpb - lastblockoff,
253 err, locked_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!tmp) {
255 if (lastfrag != ufsi->i_lastfrag)
256 goto repeat;
257 else
258 return NULL;
259 }
260 lastfrag = ufsi->i_lastfrag;
261
262 }
263 goal = fs32_to_cpu(sb, ufsi->i_u1.i_data[lastblock]) + uspi->s_fpb;
264 tmp = ufs_new_fragments (inode, p, fragment - blockoff,
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700265 goal, required + blockoff,
266 err, locked_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 /*
269 * We will extend last allocated block
270 */
271 else if (lastblock == block) {
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700272 tmp = ufs_new_fragments(inode, p, fragment - (blockoff - lastblockoff),
273 fs32_to_cpu(sb, *p), required + (blockoff - lastblockoff),
274 err, locked_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 /*
277 * We will allocate new block before last allocated block
278 */
279 else /* (lastblock > block) */ {
280 if (lastblock && (tmp = fs32_to_cpu(sb, ufsi->i_u1.i_data[lastblock-1])))
281 goal = tmp + uspi->s_fpb;
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700282 tmp = ufs_new_fragments(inode, p, fragment - blockoff,
283 goal, uspi->s_fpb, err, locked_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285 if (!tmp) {
286 if ((!blockoff && *p) ||
287 (blockoff && lastfrag != ufsi->i_lastfrag))
288 goto repeat;
289 *err = -ENOSPC;
290 return NULL;
291 }
292
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700293 if (!phys) {
294 result = ufs_clear_frags(inode, tmp + blockoff, required);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 } else {
Evgeniy Dushistovc9a27b52006-06-25 05:47:19 -0700296 *phys = tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 result = NULL;
298 *err = 0;
299 *new = 1;
300 }
301
302 inode->i_ctime = CURRENT_TIME_SEC;
303 if (IS_SYNC(inode))
304 ufs_sync_inode (inode);
305 mark_inode_dirty(inode);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700306 UFSD("EXIT, result %u\n", tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return result;
308
309 /* This part : To be implemented ....
310 Required only for writing, not required for READ-ONLY.
311ufs2:
312
313 u2_block = ufs_fragstoblks(fragment);
314 u2_blockoff = ufs_fragnum(fragment);
315 p = ufsi->i_u1.u2_i_data + block;
316 goal = 0;
317
318repeat2:
319 tmp = fs32_to_cpu(sb, *p);
320 lastfrag = ufsi->i_lastfrag;
321
322 */
323}
324
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700325/**
326 * ufs_inode_getblock() - allocate new block
327 * @inode - pointer to inode
328 * @bh - pointer to block which hold "pointer" to new allocated block
329 * @fragment - number of `fragment' which hold pointer
330 * to new allocated block
331 * @new_fragment - number of new allocated fragment
332 * (block will hold this fragment and also uspi->s_fpb-1)
333 * @err - see ufs_inode_getfrag()
334 * @phys - see ufs_inode_getfrag()
335 * @new - see ufs_inode_getfrag()
336 * @locked_page - see ufs_inode_getfrag()
337 */
338static struct buffer_head *
339ufs_inode_getblock(struct inode *inode, struct buffer_head *bh,
340 unsigned int fragment, sector_t new_fragment, int *err,
341 long *phys, int *new, struct page *locked_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700343 struct super_block *sb = inode->i_sb;
344 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct buffer_head * result;
346 unsigned tmp, goal, block, blockoff;
347 __fs32 * p;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 block = ufs_fragstoblks (fragment);
350 blockoff = ufs_fragnum (fragment);
351
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700352 UFSD("ENTER, ino %lu, fragment %u, new_fragment %llu, metadata %d\n",
353 inode->i_ino, fragment, (unsigned long long)new_fragment, !phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 result = NULL;
356 if (!bh)
357 goto out;
358 if (!buffer_uptodate(bh)) {
359 ll_rw_block (READ, 1, &bh);
360 wait_on_buffer (bh);
361 if (!buffer_uptodate(bh))
362 goto out;
363 }
364
365 p = (__fs32 *) bh->b_data + block;
366repeat:
367 tmp = fs32_to_cpu(sb, *p);
368 if (tmp) {
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700369 if (!phys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
371 if (tmp == fs32_to_cpu(sb, *p))
372 goto out;
373 brelse (result);
374 goto repeat;
375 } else {
Evgeniy Dushistovc9a27b52006-06-25 05:47:19 -0700376 *phys = tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto out;
378 }
379 }
380
381 if (block && (tmp = fs32_to_cpu(sb, ((__fs32*)bh->b_data)[block-1]) + uspi->s_fpb))
382 goal = tmp + uspi->s_fpb;
383 else
384 goal = bh->b_blocknr + uspi->s_fpb;
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700385 tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal,
386 uspi->s_fpb, err, locked_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!tmp) {
388 if (fs32_to_cpu(sb, *p))
389 goto repeat;
390 goto out;
391 }
392
Evgeniy Dushistovc9a27b52006-06-25 05:47:19 -0700393
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700394 if (!phys) {
395 result = ufs_clear_frags(inode, tmp + blockoff, uspi->s_fpb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else {
Evgeniy Dushistovc9a27b52006-06-25 05:47:19 -0700397 *phys = tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 *new = 1;
399 }
400
401 mark_buffer_dirty(bh);
402 if (IS_SYNC(inode))
403 sync_dirty_buffer(bh);
404 inode->i_ctime = CURRENT_TIME_SEC;
405 mark_inode_dirty(inode);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700406 UFSD("result %u\n", tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407out:
408 brelse (bh);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700409 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return result;
411}
412
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700413/**
414 * ufs_getfrag_bloc() - `get_block_t' function, interface between UFS and
415 * readpage, writepage and so on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
417
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700418int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct super_block * sb = inode->i_sb;
421 struct ufs_sb_private_info * uspi = UFS_SB(sb)->s_uspi;
422 struct buffer_head * bh;
423 int ret, err, new;
424 unsigned long ptr,phys;
425 u64 phys64 = 0;
426
427 if (!create) {
428 phys64 = ufs_frag_map(inode, fragment);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700429 UFSD("phys64 = %llu \n",phys64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (phys64)
431 map_bh(bh_result, sb, phys64);
432 return 0;
433 }
434
435 /* This code entered only while writing ....? */
436
437 err = -EIO;
438 new = 0;
439 ret = 0;
440 bh = NULL;
441
442 lock_kernel();
443
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700444 UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (fragment < 0)
446 goto abort_negative;
447 if (fragment >
448 ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb)
449 << uspi->s_fpbshift))
450 goto abort_too_big;
451
452 err = 0;
453 ptr = fragment;
454
455 /*
456 * ok, these macros clean the logic up a bit and make
457 * it much more readable:
458 */
459#define GET_INODE_DATABLOCK(x) \
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700460 ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new, bh_result->b_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#define GET_INODE_PTR(x) \
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700462 ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL, bh_result->b_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#define GET_INDIRECT_DATABLOCK(x) \
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700464 ufs_inode_getblock(inode, bh, x, fragment, \
465 &err, &phys, &new, bh_result->b_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#define GET_INDIRECT_PTR(x) \
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700467 ufs_inode_getblock(inode, bh, x, fragment, \
468 &err, NULL, NULL, bh_result->b_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if (ptr < UFS_NDIR_FRAGMENT) {
471 bh = GET_INODE_DATABLOCK(ptr);
472 goto out;
473 }
474 ptr -= UFS_NDIR_FRAGMENT;
475 if (ptr < (1 << (uspi->s_apbshift + uspi->s_fpbshift))) {
476 bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift));
477 goto get_indirect;
478 }
479 ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift);
480 if (ptr < (1 << (uspi->s_2apbshift + uspi->s_fpbshift))) {
481 bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift));
482 goto get_double;
483 }
484 ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift);
485 bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift));
486 bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask);
487get_double:
488 bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask);
489get_indirect:
490 bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask);
491
492#undef GET_INODE_DATABLOCK
493#undef GET_INODE_PTR
494#undef GET_INDIRECT_DATABLOCK
495#undef GET_INDIRECT_PTR
496
497out:
498 if (err)
499 goto abort;
500 if (new)
501 set_buffer_new(bh_result);
502 map_bh(bh_result, sb, phys);
503abort:
504 unlock_kernel();
505 return err;
506
507abort_negative:
508 ufs_warning(sb, "ufs_get_block", "block < 0");
509 goto abort;
510
511abort_too_big:
512 ufs_warning(sb, "ufs_get_block", "block > big");
513 goto abort;
514}
515
516struct buffer_head *ufs_getfrag(struct inode *inode, unsigned int fragment,
517 int create, int *err)
518{
519 struct buffer_head dummy;
520 int error;
521
522 dummy.b_state = 0;
523 dummy.b_blocknr = -1000;
524 error = ufs_getfrag_block(inode, fragment, &dummy, create);
525 *err = error;
526 if (!error && buffer_mapped(&dummy)) {
527 struct buffer_head *bh;
528 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
529 if (buffer_new(&dummy)) {
530 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
531 set_buffer_uptodate(bh);
532 mark_buffer_dirty(bh);
533 }
534 return bh;
535 }
536 return NULL;
537}
538
539struct buffer_head * ufs_bread (struct inode * inode, unsigned fragment,
540 int create, int * err)
541{
542 struct buffer_head * bh;
543
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700544 UFSD("ENTER, ino %lu, fragment %u\n", inode->i_ino, fragment);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 bh = ufs_getfrag (inode, fragment, create, err);
546 if (!bh || buffer_uptodate(bh))
547 return bh;
548 ll_rw_block (READ, 1, &bh);
549 wait_on_buffer (bh);
550 if (buffer_uptodate(bh))
551 return bh;
552 brelse (bh);
553 *err = -EIO;
554 return NULL;
555}
556
557static int ufs_writepage(struct page *page, struct writeback_control *wbc)
558{
559 return block_write_full_page(page,ufs_getfrag_block,wbc);
560}
561static int ufs_readpage(struct file *file, struct page *page)
562{
563 return block_read_full_page(page,ufs_getfrag_block);
564}
565static int ufs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
566{
567 return block_prepare_write(page,from,to,ufs_getfrag_block);
568}
569static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
570{
571 return generic_block_bmap(mapping,block,ufs_getfrag_block);
572}
573struct address_space_operations ufs_aops = {
574 .readpage = ufs_readpage,
575 .writepage = ufs_writepage,
576 .sync_page = block_sync_page,
577 .prepare_write = ufs_prepare_write,
578 .commit_write = generic_commit_write,
579 .bmap = ufs_bmap
580};
581
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700582static void ufs_set_inode_ops(struct inode *inode)
583{
584 if (S_ISREG(inode->i_mode)) {
585 inode->i_op = &ufs_file_inode_operations;
586 inode->i_fop = &ufs_file_operations;
587 inode->i_mapping->a_ops = &ufs_aops;
588 } else if (S_ISDIR(inode->i_mode)) {
589 inode->i_op = &ufs_dir_inode_operations;
590 inode->i_fop = &ufs_dir_operations;
591 inode->i_mapping->a_ops = &ufs_aops;
592 } else if (S_ISLNK(inode->i_mode)) {
593 if (!inode->i_blocks)
594 inode->i_op = &ufs_fast_symlink_inode_operations;
595 else {
596 inode->i_op = &page_symlink_inode_operations;
597 inode->i_mapping->a_ops = &ufs_aops;
598 }
599 } else
600 init_special_inode(inode, inode->i_mode,
601 ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604void ufs_read_inode (struct inode * inode)
605{
606 struct ufs_inode_info *ufsi = UFS_I(inode);
607 struct super_block * sb;
608 struct ufs_sb_private_info * uspi;
609 struct ufs_inode * ufs_inode;
610 struct ufs2_inode *ufs2_inode;
611 struct buffer_head * bh;
612 mode_t mode;
613 unsigned i;
614 unsigned flags;
615
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700616 UFSD("ENTER, ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 sb = inode->i_sb;
619 uspi = UFS_SB(sb)->s_uspi;
620 flags = UFS_SB(sb)->s_flags;
621
622 if (inode->i_ino < UFS_ROOTINO ||
623 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
624 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
625 goto bad_inode;
626 }
627
628 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
629 if (!bh) {
630 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
631 goto bad_inode;
632 }
633 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
634 goto ufs2_inode;
635
636 ufs_inode = (struct ufs_inode *) (bh->b_data + sizeof(struct ufs_inode) * ufs_inotofsbo(inode->i_ino));
637
638 /*
639 * Copy data to the in-core inode.
640 */
641 inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
642 inode->i_nlink = fs16_to_cpu(sb, ufs_inode->ui_nlink);
643 if (inode->i_nlink == 0)
644 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
645
646 /*
647 * Linux now has 32-bit uid and gid, so we can support EFT.
648 */
649 inode->i_uid = ufs_get_inode_uid(sb, ufs_inode);
650 inode->i_gid = ufs_get_inode_gid(sb, ufs_inode);
651
652 inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
653 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
654 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
655 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
656 inode->i_mtime.tv_nsec = 0;
657 inode->i_atime.tv_nsec = 0;
658 inode->i_ctime.tv_nsec = 0;
659 inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
660 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat) */
661 inode->i_version++;
662 ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
663 ufsi->i_gen = fs32_to_cpu(sb, ufs_inode->ui_gen);
664 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
665 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
666 ufsi->i_lastfrag = (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700667 ufsi->i_dir_start_lookup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
670 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
671 ufsi->i_u1.i_data[i] = ufs_inode->ui_u2.ui_addr.ui_db[i];
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700672 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
674 ufsi->i_u1.i_symlink[i] = ufs_inode->ui_u2.ui_symlink[i];
675 }
676 ufsi->i_osync = 0;
677
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700678 ufs_set_inode_ops(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 brelse (bh);
681
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700682 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return;
684
685bad_inode:
686 make_bad_inode(inode);
687 return;
688
689ufs2_inode :
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700690 UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 ufs2_inode = (struct ufs2_inode *)(bh->b_data + sizeof(struct ufs2_inode) * ufs_inotofsbo(inode->i_ino));
693
694 /*
695 * Copy data to the in-core inode.
696 */
697 inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
698 inode->i_nlink = fs16_to_cpu(sb, ufs2_inode->ui_nlink);
699 if (inode->i_nlink == 0)
700 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
701
702 /*
703 * Linux now has 32-bit uid and gid, so we can support EFT.
704 */
705 inode->i_uid = fs32_to_cpu(sb, ufs2_inode->ui_uid);
706 inode->i_gid = fs32_to_cpu(sb, ufs2_inode->ui_gid);
707
708 inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
709 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_atime.tv_sec);
710 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_ctime.tv_sec);
711 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_mtime.tv_sec);
712 inode->i_mtime.tv_nsec = 0;
713 inode->i_atime.tv_nsec = 0;
714 inode->i_ctime.tv_nsec = 0;
715 inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
716 inode->i_blksize = PAGE_SIZE; /*This is the optimal IO size(for stat)*/
717
718 inode->i_version++;
719 ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
720 ufsi->i_gen = fs32_to_cpu(sb, ufs2_inode->ui_gen);
721 /*
722 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
723 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
724 */
725 ufsi->i_lastfrag= (inode->i_size + uspi->s_fsize- 1) >> uspi->s_fshift;
726
727 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
728 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
729 ufsi->i_u1.u2_i_data[i] =
730 ufs2_inode->ui_u2.ui_addr.ui_db[i];
731 }
732 else {
733 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
734 ufsi->i_u1.i_symlink[i] = ufs2_inode->ui_u2.ui_symlink[i];
735 }
736 ufsi->i_osync = 0;
737
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700738 ufs_set_inode_ops(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 brelse(bh);
741
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700742 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return;
744}
745
746static int ufs_update_inode(struct inode * inode, int do_sync)
747{
748 struct ufs_inode_info *ufsi = UFS_I(inode);
749 struct super_block * sb;
750 struct ufs_sb_private_info * uspi;
751 struct buffer_head * bh;
752 struct ufs_inode * ufs_inode;
753 unsigned i;
754 unsigned flags;
755
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700756 UFSD("ENTER, ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 sb = inode->i_sb;
759 uspi = UFS_SB(sb)->s_uspi;
760 flags = UFS_SB(sb)->s_flags;
761
762 if (inode->i_ino < UFS_ROOTINO ||
763 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
764 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
765 return -1;
766 }
767
768 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
769 if (!bh) {
770 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
771 return -1;
772 }
773 ufs_inode = (struct ufs_inode *) (bh->b_data + ufs_inotofsbo(inode->i_ino) * sizeof(struct ufs_inode));
774
775 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
776 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
777
778 ufs_set_inode_uid(sb, ufs_inode, inode->i_uid);
779 ufs_set_inode_gid(sb, ufs_inode, inode->i_gid);
780
781 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
782 ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
783 ufs_inode->ui_atime.tv_usec = 0;
784 ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
785 ufs_inode->ui_ctime.tv_usec = 0;
786 ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
787 ufs_inode->ui_mtime.tv_usec = 0;
788 ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
789 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
790 ufs_inode->ui_gen = cpu_to_fs32(sb, ufsi->i_gen);
791
792 if ((flags & UFS_UID_MASK) == UFS_UID_EFT) {
793 ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
794 ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
795 }
796
797 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
798 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
799 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
800 } else if (inode->i_blocks) {
801 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
802 ufs_inode->ui_u2.ui_addr.ui_db[i] = ufsi->i_u1.i_data[i];
803 }
804 else {
805 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
806 ufs_inode->ui_u2.ui_symlink[i] = ufsi->i_u1.i_symlink[i];
807 }
808
809 if (!inode->i_nlink)
810 memset (ufs_inode, 0, sizeof(struct ufs_inode));
811
812 mark_buffer_dirty(bh);
813 if (do_sync)
814 sync_dirty_buffer(bh);
815 brelse (bh);
816
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700817 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return 0;
819}
820
821int ufs_write_inode (struct inode * inode, int wait)
822{
823 int ret;
824 lock_kernel();
825 ret = ufs_update_inode (inode, wait);
826 unlock_kernel();
827 return ret;
828}
829
830int ufs_sync_inode (struct inode *inode)
831{
832 return ufs_update_inode (inode, 1);
833}
834
835void ufs_delete_inode (struct inode * inode)
836{
Mark Fashehfef26652005-09-09 13:01:31 -0700837 truncate_inode_pages(&inode->i_data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*UFS_I(inode)->i_dtime = CURRENT_TIME;*/
839 lock_kernel();
840 mark_inode_dirty(inode);
841 ufs_update_inode(inode, IS_SYNC(inode));
842 inode->i_size = 0;
843 if (inode->i_blocks)
844 ufs_truncate (inode);
845 ufs_free_inode (inode);
846 unlock_kernel();
847}