blob: 3c3f62ce2ad9a926e71b5e52bb76f6b763009425 [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
44#undef UFS_INODE_DEBUG
45#undef UFS_INODE_DEBUG_MORE
46
47#ifdef UFS_INODE_DEBUG
48#define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
49#else
50#define UFSD(x)
51#endif
52
53static int ufs_block_to_path(struct inode *inode, sector_t i_block, sector_t offsets[4])
54{
55 struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi;
56 int ptrs = uspi->s_apb;
57 int ptrs_bits = uspi->s_apbshift;
58 const long direct_blocks = UFS_NDADDR,
59 indirect_blocks = ptrs,
60 double_blocks = (1 << (ptrs_bits * 2));
61 int n = 0;
62
63
Evgeniy7b4ee732006-01-14 11:42:06 +030064 UFSD(("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (i_block < 0) {
66 ufs_warning(inode->i_sb, "ufs_block_to_path", "block < 0");
67 } else if (i_block < direct_blocks) {
68 offsets[n++] = i_block;
69 } else if ((i_block -= direct_blocks) < indirect_blocks) {
70 offsets[n++] = UFS_IND_BLOCK;
71 offsets[n++] = i_block;
72 } else if ((i_block -= indirect_blocks) < double_blocks) {
73 offsets[n++] = UFS_DIND_BLOCK;
74 offsets[n++] = i_block >> ptrs_bits;
75 offsets[n++] = i_block & (ptrs - 1);
76 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
77 offsets[n++] = UFS_TIND_BLOCK;
78 offsets[n++] = i_block >> (ptrs_bits * 2);
79 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
80 offsets[n++] = i_block & (ptrs - 1);
81 } else {
82 ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
83 }
84 return n;
85}
86
87/*
88 * Returns the location of the fragment from
89 * the begining of the filesystem.
90 */
91
92u64 ufs_frag_map(struct inode *inode, sector_t frag)
93{
94 struct ufs_inode_info *ufsi = UFS_I(inode);
95 struct super_block *sb = inode->i_sb;
96 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
97 u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift;
98 int shift = uspi->s_apbshift-uspi->s_fpbshift;
99 sector_t offsets[4], *p;
100 int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets);
101 u64 ret = 0L;
102 __fs32 block;
103 __fs64 u2_block = 0L;
104 unsigned flags = UFS_SB(sb)->s_flags;
105 u64 temp = 0L;
106
Evgeniy7b4ee732006-01-14 11:42:06 +0300107 UFSD((": frag = %llu depth = %d\n", (unsigned long long)frag, depth));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 UFSD((": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",uspi->s_fpbshift,uspi->s_apbmask,mask));
109
110 if (depth == 0)
111 return 0;
112
113 p = offsets;
114
115 lock_kernel();
116 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
117 goto ufs2;
118
119 block = ufsi->i_u1.i_data[*p++];
120 if (!block)
121 goto out;
122 while (--depth) {
123 struct buffer_head *bh;
124 sector_t n = *p++;
125
126 bh = sb_bread(sb, uspi->s_sbbase + fs32_to_cpu(sb, block)+(n>>shift));
127 if (!bh)
128 goto out;
129 block = ((__fs32 *) bh->b_data)[n & mask];
130 brelse (bh);
131 if (!block)
132 goto out;
133 }
134 ret = (u64) (uspi->s_sbbase + fs32_to_cpu(sb, block) + (frag & uspi->s_fpbmask));
135 goto out;
136ufs2:
137 u2_block = ufsi->i_u1.u2_i_data[*p++];
138 if (!u2_block)
139 goto out;
140
141
142 while (--depth) {
143 struct buffer_head *bh;
144 sector_t n = *p++;
145
146
147 temp = (u64)(uspi->s_sbbase) + fs64_to_cpu(sb, u2_block);
148 bh = sb_bread(sb, temp +(u64) (n>>shift));
149 if (!bh)
150 goto out;
151 u2_block = ((__fs64 *)bh->b_data)[n & mask];
152 brelse(bh);
153 if (!u2_block)
154 goto out;
155 }
156 temp = (u64)uspi->s_sbbase + fs64_to_cpu(sb, u2_block);
157 ret = temp + (u64) (frag & uspi->s_fpbmask);
158
159out:
160 unlock_kernel();
161 return ret;
162}
163
164static struct buffer_head * ufs_inode_getfrag (struct inode *inode,
165 unsigned int fragment, unsigned int new_fragment,
166 unsigned int required, int *err, int metadata, long *phys, int *new)
167{
168 struct ufs_inode_info *ufsi = UFS_I(inode);
169 struct super_block * sb;
170 struct ufs_sb_private_info * uspi;
171 struct buffer_head * result;
172 unsigned block, blockoff, lastfrag, lastblock, lastblockoff;
173 unsigned tmp, goal;
174 __fs32 * p, * p2;
175 unsigned flags = 0;
176
177 UFSD(("ENTER, ino %lu, fragment %u, new_fragment %u, required %u\n",
178 inode->i_ino, fragment, new_fragment, required))
179
180 sb = inode->i_sb;
181 uspi = UFS_SB(sb)->s_uspi;
182
183 flags = UFS_SB(sb)->s_flags;
184 /* TODO : to be done for write support
185 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
186 goto ufs2;
187 */
188
189 block = ufs_fragstoblks (fragment);
190 blockoff = ufs_fragnum (fragment);
191 p = ufsi->i_u1.i_data + block;
192 goal = 0;
193
194repeat:
195 tmp = fs32_to_cpu(sb, *p);
196 lastfrag = ufsi->i_lastfrag;
197 if (tmp && fragment < lastfrag) {
198 if (metadata) {
199 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
200 if (tmp == fs32_to_cpu(sb, *p)) {
201 UFSD(("EXIT, result %u\n", tmp + blockoff))
202 return result;
203 }
204 brelse (result);
205 goto repeat;
206 } else {
207 *phys = tmp;
208 return NULL;
209 }
210 }
211
212 lastblock = ufs_fragstoblks (lastfrag);
213 lastblockoff = ufs_fragnum (lastfrag);
214 /*
215 * We will extend file into new block beyond last allocated block
216 */
217 if (lastblock < block) {
218 /*
219 * We must reallocate last allocated block
220 */
221 if (lastblockoff) {
222 p2 = ufsi->i_u1.i_data + lastblock;
223 tmp = ufs_new_fragments (inode, p2, lastfrag,
224 fs32_to_cpu(sb, *p2), uspi->s_fpb - lastblockoff, err);
225 if (!tmp) {
226 if (lastfrag != ufsi->i_lastfrag)
227 goto repeat;
228 else
229 return NULL;
230 }
231 lastfrag = ufsi->i_lastfrag;
232
233 }
234 goal = fs32_to_cpu(sb, ufsi->i_u1.i_data[lastblock]) + uspi->s_fpb;
235 tmp = ufs_new_fragments (inode, p, fragment - blockoff,
236 goal, required + blockoff, err);
237 }
238 /*
239 * We will extend last allocated block
240 */
241 else if (lastblock == block) {
242 tmp = ufs_new_fragments (inode, p, fragment - (blockoff - lastblockoff),
243 fs32_to_cpu(sb, *p), required + (blockoff - lastblockoff), err);
244 }
245 /*
246 * We will allocate new block before last allocated block
247 */
248 else /* (lastblock > block) */ {
249 if (lastblock && (tmp = fs32_to_cpu(sb, ufsi->i_u1.i_data[lastblock-1])))
250 goal = tmp + uspi->s_fpb;
251 tmp = ufs_new_fragments (inode, p, fragment - blockoff,
252 goal, uspi->s_fpb, err);
253 }
254 if (!tmp) {
255 if ((!blockoff && *p) ||
256 (blockoff && lastfrag != ufsi->i_lastfrag))
257 goto repeat;
258 *err = -ENOSPC;
259 return NULL;
260 }
261
262 /* The nullification of framgents done in ufs/balloc.c is
263 * something I don't have the stomache to move into here right
264 * now. -DaveM
265 */
266 if (metadata) {
267 result = sb_getblk(inode->i_sb, tmp + blockoff);
268 } else {
269 *phys = tmp;
270 result = NULL;
271 *err = 0;
272 *new = 1;
273 }
274
275 inode->i_ctime = CURRENT_TIME_SEC;
276 if (IS_SYNC(inode))
277 ufs_sync_inode (inode);
278 mark_inode_dirty(inode);
279 UFSD(("EXIT, result %u\n", tmp + blockoff))
280 return result;
281
282 /* This part : To be implemented ....
283 Required only for writing, not required for READ-ONLY.
284ufs2:
285
286 u2_block = ufs_fragstoblks(fragment);
287 u2_blockoff = ufs_fragnum(fragment);
288 p = ufsi->i_u1.u2_i_data + block;
289 goal = 0;
290
291repeat2:
292 tmp = fs32_to_cpu(sb, *p);
293 lastfrag = ufsi->i_lastfrag;
294
295 */
296}
297
298static struct buffer_head * ufs_block_getfrag (struct inode *inode,
299 struct buffer_head *bh, unsigned int fragment, unsigned int new_fragment,
300 unsigned int blocksize, int * err, int metadata, long *phys, int *new)
301{
302 struct super_block * sb;
303 struct ufs_sb_private_info * uspi;
304 struct buffer_head * result;
305 unsigned tmp, goal, block, blockoff;
306 __fs32 * p;
307
308 sb = inode->i_sb;
309 uspi = UFS_SB(sb)->s_uspi;
310 block = ufs_fragstoblks (fragment);
311 blockoff = ufs_fragnum (fragment);
312
313 UFSD(("ENTER, ino %lu, fragment %u, new_fragment %u\n", inode->i_ino, fragment, new_fragment))
314
315 result = NULL;
316 if (!bh)
317 goto out;
318 if (!buffer_uptodate(bh)) {
319 ll_rw_block (READ, 1, &bh);
320 wait_on_buffer (bh);
321 if (!buffer_uptodate(bh))
322 goto out;
323 }
324
325 p = (__fs32 *) bh->b_data + block;
326repeat:
327 tmp = fs32_to_cpu(sb, *p);
328 if (tmp) {
329 if (metadata) {
330 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
331 if (tmp == fs32_to_cpu(sb, *p))
332 goto out;
333 brelse (result);
334 goto repeat;
335 } else {
336 *phys = tmp;
337 goto out;
338 }
339 }
340
341 if (block && (tmp = fs32_to_cpu(sb, ((__fs32*)bh->b_data)[block-1]) + uspi->s_fpb))
342 goal = tmp + uspi->s_fpb;
343 else
344 goal = bh->b_blocknr + uspi->s_fpb;
345 tmp = ufs_new_fragments (inode, p, ufs_blknum(new_fragment), goal, uspi->s_fpb, err);
346 if (!tmp) {
347 if (fs32_to_cpu(sb, *p))
348 goto repeat;
349 goto out;
350 }
351
352 /* The nullification of framgents done in ufs/balloc.c is
353 * something I don't have the stomache to move into here right
354 * now. -DaveM
355 */
356 if (metadata) {
357 result = sb_getblk(sb, tmp + blockoff);
358 } else {
359 *phys = tmp;
360 *new = 1;
361 }
362
363 mark_buffer_dirty(bh);
364 if (IS_SYNC(inode))
365 sync_dirty_buffer(bh);
366 inode->i_ctime = CURRENT_TIME_SEC;
367 mark_inode_dirty(inode);
Evgeniy7b4ee732006-01-14 11:42:06 +0300368 UFSD(("result %u\n", tmp + blockoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369out:
370 brelse (bh);
Evgeniy7b4ee732006-01-14 11:42:06 +0300371 UFSD(("EXIT\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return result;
373}
374
375/*
376 * This function gets the block which contains the fragment.
377 */
378
Evgeniy Dushistov09114eb2006-02-03 03:04:06 -0800379int ufs_getfrag_block (struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct super_block * sb = inode->i_sb;
382 struct ufs_sb_private_info * uspi = UFS_SB(sb)->s_uspi;
383 struct buffer_head * bh;
384 int ret, err, new;
385 unsigned long ptr,phys;
386 u64 phys64 = 0;
387
388 if (!create) {
389 phys64 = ufs_frag_map(inode, fragment);
Evgeniy7b4ee732006-01-14 11:42:06 +0300390 UFSD(("phys64 = %llu \n",phys64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (phys64)
392 map_bh(bh_result, sb, phys64);
393 return 0;
394 }
395
396 /* This code entered only while writing ....? */
397
398 err = -EIO;
399 new = 0;
400 ret = 0;
401 bh = NULL;
402
403 lock_kernel();
404
Evgeniy7b4ee732006-01-14 11:42:06 +0300405 UFSD(("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (fragment < 0)
407 goto abort_negative;
408 if (fragment >
409 ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb)
410 << uspi->s_fpbshift))
411 goto abort_too_big;
412
413 err = 0;
414 ptr = fragment;
415
416 /*
417 * ok, these macros clean the logic up a bit and make
418 * it much more readable:
419 */
420#define GET_INODE_DATABLOCK(x) \
421 ufs_inode_getfrag(inode, x, fragment, 1, &err, 0, &phys, &new)
422#define GET_INODE_PTR(x) \
423 ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, 1, NULL, NULL)
424#define GET_INDIRECT_DATABLOCK(x) \
425 ufs_block_getfrag(inode, bh, x, fragment, sb->s_blocksize, \
426 &err, 0, &phys, &new);
427#define GET_INDIRECT_PTR(x) \
428 ufs_block_getfrag(inode, bh, x, fragment, sb->s_blocksize, \
429 &err, 1, NULL, NULL);
430
431 if (ptr < UFS_NDIR_FRAGMENT) {
432 bh = GET_INODE_DATABLOCK(ptr);
433 goto out;
434 }
435 ptr -= UFS_NDIR_FRAGMENT;
436 if (ptr < (1 << (uspi->s_apbshift + uspi->s_fpbshift))) {
437 bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift));
438 goto get_indirect;
439 }
440 ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift);
441 if (ptr < (1 << (uspi->s_2apbshift + uspi->s_fpbshift))) {
442 bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift));
443 goto get_double;
444 }
445 ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift);
446 bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift));
447 bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask);
448get_double:
449 bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask);
450get_indirect:
451 bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask);
452
453#undef GET_INODE_DATABLOCK
454#undef GET_INODE_PTR
455#undef GET_INDIRECT_DATABLOCK
456#undef GET_INDIRECT_PTR
457
458out:
459 if (err)
460 goto abort;
461 if (new)
462 set_buffer_new(bh_result);
463 map_bh(bh_result, sb, phys);
464abort:
465 unlock_kernel();
466 return err;
467
468abort_negative:
469 ufs_warning(sb, "ufs_get_block", "block < 0");
470 goto abort;
471
472abort_too_big:
473 ufs_warning(sb, "ufs_get_block", "block > big");
474 goto abort;
475}
476
477struct buffer_head *ufs_getfrag(struct inode *inode, unsigned int fragment,
478 int create, int *err)
479{
480 struct buffer_head dummy;
481 int error;
482
483 dummy.b_state = 0;
484 dummy.b_blocknr = -1000;
485 error = ufs_getfrag_block(inode, fragment, &dummy, create);
486 *err = error;
487 if (!error && buffer_mapped(&dummy)) {
488 struct buffer_head *bh;
489 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
490 if (buffer_new(&dummy)) {
491 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
492 set_buffer_uptodate(bh);
493 mark_buffer_dirty(bh);
494 }
495 return bh;
496 }
497 return NULL;
498}
499
500struct buffer_head * ufs_bread (struct inode * inode, unsigned fragment,
501 int create, int * err)
502{
503 struct buffer_head * bh;
504
505 UFSD(("ENTER, ino %lu, fragment %u\n", inode->i_ino, fragment))
506 bh = ufs_getfrag (inode, fragment, create, err);
507 if (!bh || buffer_uptodate(bh))
508 return bh;
509 ll_rw_block (READ, 1, &bh);
510 wait_on_buffer (bh);
511 if (buffer_uptodate(bh))
512 return bh;
513 brelse (bh);
514 *err = -EIO;
515 return NULL;
516}
517
518static int ufs_writepage(struct page *page, struct writeback_control *wbc)
519{
520 return block_write_full_page(page,ufs_getfrag_block,wbc);
521}
522static int ufs_readpage(struct file *file, struct page *page)
523{
524 return block_read_full_page(page,ufs_getfrag_block);
525}
526static int ufs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
527{
528 return block_prepare_write(page,from,to,ufs_getfrag_block);
529}
530static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
531{
532 return generic_block_bmap(mapping,block,ufs_getfrag_block);
533}
534struct address_space_operations ufs_aops = {
535 .readpage = ufs_readpage,
536 .writepage = ufs_writepage,
537 .sync_page = block_sync_page,
538 .prepare_write = ufs_prepare_write,
539 .commit_write = generic_commit_write,
540 .bmap = ufs_bmap
541};
542
543void ufs_read_inode (struct inode * inode)
544{
545 struct ufs_inode_info *ufsi = UFS_I(inode);
546 struct super_block * sb;
547 struct ufs_sb_private_info * uspi;
548 struct ufs_inode * ufs_inode;
549 struct ufs2_inode *ufs2_inode;
550 struct buffer_head * bh;
551 mode_t mode;
552 unsigned i;
553 unsigned flags;
554
555 UFSD(("ENTER, ino %lu\n", inode->i_ino))
556
557 sb = inode->i_sb;
558 uspi = UFS_SB(sb)->s_uspi;
559 flags = UFS_SB(sb)->s_flags;
560
561 if (inode->i_ino < UFS_ROOTINO ||
562 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
563 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
564 goto bad_inode;
565 }
566
567 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
568 if (!bh) {
569 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
570 goto bad_inode;
571 }
572 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
573 goto ufs2_inode;
574
575 ufs_inode = (struct ufs_inode *) (bh->b_data + sizeof(struct ufs_inode) * ufs_inotofsbo(inode->i_ino));
576
577 /*
578 * Copy data to the in-core inode.
579 */
580 inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
581 inode->i_nlink = fs16_to_cpu(sb, ufs_inode->ui_nlink);
582 if (inode->i_nlink == 0)
583 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
584
585 /*
586 * Linux now has 32-bit uid and gid, so we can support EFT.
587 */
588 inode->i_uid = ufs_get_inode_uid(sb, ufs_inode);
589 inode->i_gid = ufs_get_inode_gid(sb, ufs_inode);
590
591 inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
592 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
593 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
594 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
595 inode->i_mtime.tv_nsec = 0;
596 inode->i_atime.tv_nsec = 0;
597 inode->i_ctime.tv_nsec = 0;
598 inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
599 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat) */
600 inode->i_version++;
601 ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
602 ufsi->i_gen = fs32_to_cpu(sb, ufs_inode->ui_gen);
603 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
604 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
605 ufsi->i_lastfrag = (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
606
607 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
608 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
609 ufsi->i_u1.i_data[i] = ufs_inode->ui_u2.ui_addr.ui_db[i];
610 }
611 else {
612 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
613 ufsi->i_u1.i_symlink[i] = ufs_inode->ui_u2.ui_symlink[i];
614 }
615 ufsi->i_osync = 0;
616
617 if (S_ISREG(inode->i_mode)) {
618 inode->i_op = &ufs_file_inode_operations;
619 inode->i_fop = &ufs_file_operations;
620 inode->i_mapping->a_ops = &ufs_aops;
621 } else if (S_ISDIR(inode->i_mode)) {
622 inode->i_op = &ufs_dir_inode_operations;
623 inode->i_fop = &ufs_dir_operations;
624 } else if (S_ISLNK(inode->i_mode)) {
625 if (!inode->i_blocks)
626 inode->i_op = &ufs_fast_symlink_inode_operations;
627 else {
628 inode->i_op = &page_symlink_inode_operations;
629 inode->i_mapping->a_ops = &ufs_aops;
630 }
631 } else
632 init_special_inode(inode, inode->i_mode,
633 ufs_get_inode_dev(sb, ufsi));
634
635 brelse (bh);
636
637 UFSD(("EXIT\n"))
638 return;
639
640bad_inode:
641 make_bad_inode(inode);
642 return;
643
644ufs2_inode :
645 UFSD(("Reading ufs2 inode, ino %lu\n", inode->i_ino))
646
647 ufs2_inode = (struct ufs2_inode *)(bh->b_data + sizeof(struct ufs2_inode) * ufs_inotofsbo(inode->i_ino));
648
649 /*
650 * Copy data to the in-core inode.
651 */
652 inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
653 inode->i_nlink = fs16_to_cpu(sb, ufs2_inode->ui_nlink);
654 if (inode->i_nlink == 0)
655 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
656
657 /*
658 * Linux now has 32-bit uid and gid, so we can support EFT.
659 */
660 inode->i_uid = fs32_to_cpu(sb, ufs2_inode->ui_uid);
661 inode->i_gid = fs32_to_cpu(sb, ufs2_inode->ui_gid);
662
663 inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
664 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_atime.tv_sec);
665 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_ctime.tv_sec);
666 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_mtime.tv_sec);
667 inode->i_mtime.tv_nsec = 0;
668 inode->i_atime.tv_nsec = 0;
669 inode->i_ctime.tv_nsec = 0;
670 inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
671 inode->i_blksize = PAGE_SIZE; /*This is the optimal IO size(for stat)*/
672
673 inode->i_version++;
674 ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
675 ufsi->i_gen = fs32_to_cpu(sb, ufs2_inode->ui_gen);
676 /*
677 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
678 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
679 */
680 ufsi->i_lastfrag= (inode->i_size + uspi->s_fsize- 1) >> uspi->s_fshift;
681
682 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
683 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
684 ufsi->i_u1.u2_i_data[i] =
685 ufs2_inode->ui_u2.ui_addr.ui_db[i];
686 }
687 else {
688 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
689 ufsi->i_u1.i_symlink[i] = ufs2_inode->ui_u2.ui_symlink[i];
690 }
691 ufsi->i_osync = 0;
692
693 if (S_ISREG(inode->i_mode)) {
694 inode->i_op = &ufs_file_inode_operations;
695 inode->i_fop = &ufs_file_operations;
696 inode->i_mapping->a_ops = &ufs_aops;
697 } else if (S_ISDIR(inode->i_mode)) {
698 inode->i_op = &ufs_dir_inode_operations;
699 inode->i_fop = &ufs_dir_operations;
700 } else if (S_ISLNK(inode->i_mode)) {
701 if (!inode->i_blocks)
702 inode->i_op = &ufs_fast_symlink_inode_operations;
703 else {
704 inode->i_op = &page_symlink_inode_operations;
705 inode->i_mapping->a_ops = &ufs_aops;
706 }
707 } else /* TODO : here ...*/
708 init_special_inode(inode, inode->i_mode,
709 ufs_get_inode_dev(sb, ufsi));
710
711 brelse(bh);
712
713 UFSD(("EXIT\n"))
714 return;
715}
716
717static int ufs_update_inode(struct inode * inode, int do_sync)
718{
719 struct ufs_inode_info *ufsi = UFS_I(inode);
720 struct super_block * sb;
721 struct ufs_sb_private_info * uspi;
722 struct buffer_head * bh;
723 struct ufs_inode * ufs_inode;
724 unsigned i;
725 unsigned flags;
726
727 UFSD(("ENTER, ino %lu\n", inode->i_ino))
728
729 sb = inode->i_sb;
730 uspi = UFS_SB(sb)->s_uspi;
731 flags = UFS_SB(sb)->s_flags;
732
733 if (inode->i_ino < UFS_ROOTINO ||
734 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
735 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
736 return -1;
737 }
738
739 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
740 if (!bh) {
741 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
742 return -1;
743 }
744 ufs_inode = (struct ufs_inode *) (bh->b_data + ufs_inotofsbo(inode->i_ino) * sizeof(struct ufs_inode));
745
746 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
747 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
748
749 ufs_set_inode_uid(sb, ufs_inode, inode->i_uid);
750 ufs_set_inode_gid(sb, ufs_inode, inode->i_gid);
751
752 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
753 ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
754 ufs_inode->ui_atime.tv_usec = 0;
755 ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
756 ufs_inode->ui_ctime.tv_usec = 0;
757 ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
758 ufs_inode->ui_mtime.tv_usec = 0;
759 ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
760 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
761 ufs_inode->ui_gen = cpu_to_fs32(sb, ufsi->i_gen);
762
763 if ((flags & UFS_UID_MASK) == UFS_UID_EFT) {
764 ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
765 ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
766 }
767
768 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
769 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
770 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
771 } else if (inode->i_blocks) {
772 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
773 ufs_inode->ui_u2.ui_addr.ui_db[i] = ufsi->i_u1.i_data[i];
774 }
775 else {
776 for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
777 ufs_inode->ui_u2.ui_symlink[i] = ufsi->i_u1.i_symlink[i];
778 }
779
780 if (!inode->i_nlink)
781 memset (ufs_inode, 0, sizeof(struct ufs_inode));
782
783 mark_buffer_dirty(bh);
784 if (do_sync)
785 sync_dirty_buffer(bh);
786 brelse (bh);
787
788 UFSD(("EXIT\n"))
789 return 0;
790}
791
792int ufs_write_inode (struct inode * inode, int wait)
793{
794 int ret;
795 lock_kernel();
796 ret = ufs_update_inode (inode, wait);
797 unlock_kernel();
798 return ret;
799}
800
801int ufs_sync_inode (struct inode *inode)
802{
803 return ufs_update_inode (inode, 1);
804}
805
806void ufs_delete_inode (struct inode * inode)
807{
Mark Fashehfef26652005-09-09 13:01:31 -0700808 truncate_inode_pages(&inode->i_data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /*UFS_I(inode)->i_dtime = CURRENT_TIME;*/
810 lock_kernel();
811 mark_inode_dirty(inode);
812 ufs_update_inode(inode, IS_SYNC(inode));
813 inode->i_size = 0;
814 if (inode->i_blocks)
815 ufs_truncate (inode);
816 ufs_free_inode (inode);
817 unlock_kernel();
818}