eCryptfs: Remove mmap from directory operations
[linux/fpc-iii.git] / fs / ufs / inode.c
blob7cf33379fd467825d91957befaab253bcc78f4a1
1 /*
2 * linux/fs/ufs/inode.c
4 * Copyright (C) 1998
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
8 * from
10 * linux/fs/ext2/inode.c
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)
17 * from
19 * linux/fs/minix/inode.c
21 * Copyright (C) 1991, 1992 Linus Torvalds
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
28 #include <asm/uaccess.h>
29 #include <asm/system.h>
31 #include <linux/errno.h>
32 #include <linux/fs.h>
33 #include <linux/time.h>
34 #include <linux/stat.h>
35 #include <linux/string.h>
36 #include <linux/mm.h>
37 #include <linux/smp_lock.h>
38 #include <linux/buffer_head.h>
40 #include "ufs_fs.h"
41 #include "ufs.h"
42 #include "swab.h"
43 #include "util.h"
45 static u64 ufs_frag_map(struct inode *inode, sector_t frag);
47 static int ufs_block_to_path(struct inode *inode, sector_t i_block, sector_t offsets[4])
49 struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi;
50 int ptrs = uspi->s_apb;
51 int ptrs_bits = uspi->s_apbshift;
52 const long direct_blocks = UFS_NDADDR,
53 indirect_blocks = ptrs,
54 double_blocks = (1 << (ptrs_bits * 2));
55 int n = 0;
58 UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks);
59 if (i_block < direct_blocks) {
60 offsets[n++] = i_block;
61 } else if ((i_block -= direct_blocks) < indirect_blocks) {
62 offsets[n++] = UFS_IND_BLOCK;
63 offsets[n++] = i_block;
64 } else if ((i_block -= indirect_blocks) < double_blocks) {
65 offsets[n++] = UFS_DIND_BLOCK;
66 offsets[n++] = i_block >> ptrs_bits;
67 offsets[n++] = i_block & (ptrs - 1);
68 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
69 offsets[n++] = UFS_TIND_BLOCK;
70 offsets[n++] = i_block >> (ptrs_bits * 2);
71 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
72 offsets[n++] = i_block & (ptrs - 1);
73 } else {
74 ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
76 return n;
80 * Returns the location of the fragment from
81 * the begining of the filesystem.
84 static u64 ufs_frag_map(struct inode *inode, sector_t frag)
86 struct ufs_inode_info *ufsi = UFS_I(inode);
87 struct super_block *sb = inode->i_sb;
88 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
89 u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift;
90 int shift = uspi->s_apbshift-uspi->s_fpbshift;
91 sector_t offsets[4], *p;
92 int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets);
93 u64 ret = 0L;
94 __fs32 block;
95 __fs64 u2_block = 0L;
96 unsigned flags = UFS_SB(sb)->s_flags;
97 u64 temp = 0L;
99 UFSD(": frag = %llu depth = %d\n", (unsigned long long)frag, depth);
100 UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
101 uspi->s_fpbshift, uspi->s_apbmask,
102 (unsigned long long)mask);
104 if (depth == 0)
105 return 0;
107 p = offsets;
109 lock_kernel();
110 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
111 goto ufs2;
113 block = ufsi->i_u1.i_data[*p++];
114 if (!block)
115 goto out;
116 while (--depth) {
117 struct buffer_head *bh;
118 sector_t n = *p++;
120 bh = sb_bread(sb, uspi->s_sbbase + fs32_to_cpu(sb, block)+(n>>shift));
121 if (!bh)
122 goto out;
123 block = ((__fs32 *) bh->b_data)[n & mask];
124 brelse (bh);
125 if (!block)
126 goto out;
128 ret = (u64) (uspi->s_sbbase + fs32_to_cpu(sb, block) + (frag & uspi->s_fpbmask));
129 goto out;
130 ufs2:
131 u2_block = ufsi->i_u1.u2_i_data[*p++];
132 if (!u2_block)
133 goto out;
136 while (--depth) {
137 struct buffer_head *bh;
138 sector_t n = *p++;
141 temp = (u64)(uspi->s_sbbase) + fs64_to_cpu(sb, u2_block);
142 bh = sb_bread(sb, temp +(u64) (n>>shift));
143 if (!bh)
144 goto out;
145 u2_block = ((__fs64 *)bh->b_data)[n & mask];
146 brelse(bh);
147 if (!u2_block)
148 goto out;
150 temp = (u64)uspi->s_sbbase + fs64_to_cpu(sb, u2_block);
151 ret = temp + (u64) (frag & uspi->s_fpbmask);
153 out:
154 unlock_kernel();
155 return ret;
159 * ufs_inode_getfrag() - allocate new fragment(s)
160 * @inode - pointer to inode
161 * @fragment - number of `fragment' which hold pointer
162 * to new allocated fragment(s)
163 * @new_fragment - number of new allocated fragment(s)
164 * @required - how many fragment(s) we require
165 * @err - we set it if something wrong
166 * @phys - pointer to where we save physical number of new allocated fragments,
167 * NULL if we allocate not data(indirect blocks for example).
168 * @new - we set it if we allocate new block
169 * @locked_page - for ufs_new_fragments()
171 static struct buffer_head *
172 ufs_inode_getfrag(struct inode *inode, u64 fragment,
173 sector_t new_fragment, unsigned int required, int *err,
174 long *phys, int *new, struct page *locked_page)
176 struct ufs_inode_info *ufsi = UFS_I(inode);
177 struct super_block *sb = inode->i_sb;
178 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
179 struct buffer_head * result;
180 unsigned blockoff, lastblockoff;
181 u64 tmp, goal, lastfrag, block, lastblock;
182 void *p, *p2;
184 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, required %u, "
185 "metadata %d\n", inode->i_ino, (unsigned long long)fragment,
186 (unsigned long long)new_fragment, required, !phys);
188 /* TODO : to be done for write support
189 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
190 goto ufs2;
193 block = ufs_fragstoblks (fragment);
194 blockoff = ufs_fragnum (fragment);
195 p = ufs_get_direct_data_ptr(uspi, ufsi, block);
197 goal = 0;
199 repeat:
200 tmp = ufs_data_ptr_to_cpu(sb, p);
202 lastfrag = ufsi->i_lastfrag;
203 if (tmp && fragment < lastfrag) {
204 if (!phys) {
205 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
206 if (tmp == ufs_data_ptr_to_cpu(sb, p)) {
207 UFSD("EXIT, result %llu\n",
208 (unsigned long long)tmp + blockoff);
209 return result;
211 brelse (result);
212 goto repeat;
213 } else {
214 *phys = uspi->s_sbbase + tmp + blockoff;
215 return NULL;
219 lastblock = ufs_fragstoblks (lastfrag);
220 lastblockoff = ufs_fragnum (lastfrag);
222 * We will extend file into new block beyond last allocated block
224 if (lastblock < block) {
226 * We must reallocate last allocated block
228 if (lastblockoff) {
229 p2 = ufs_get_direct_data_ptr(uspi, ufsi, lastblock);
230 tmp = ufs_new_fragments(inode, p2, lastfrag,
231 ufs_data_ptr_to_cpu(sb, p2),
232 uspi->s_fpb - lastblockoff,
233 err, locked_page);
234 if (!tmp) {
235 if (lastfrag != ufsi->i_lastfrag)
236 goto repeat;
237 else
238 return NULL;
240 lastfrag = ufsi->i_lastfrag;
243 tmp = ufs_data_ptr_to_cpu(sb,
244 ufs_get_direct_data_ptr(uspi, ufsi,
245 lastblock));
246 if (tmp)
247 goal = tmp + uspi->s_fpb;
248 tmp = ufs_new_fragments (inode, p, fragment - blockoff,
249 goal, required + blockoff,
250 err,
251 phys != NULL ? locked_page : NULL);
252 } else if (lastblock == block) {
254 * We will extend last allocated block
256 tmp = ufs_new_fragments(inode, p, fragment -
257 (blockoff - lastblockoff),
258 ufs_data_ptr_to_cpu(sb, p),
259 required + (blockoff - lastblockoff),
260 err, phys != NULL ? locked_page : NULL);
261 } else /* (lastblock > block) */ {
263 * We will allocate new block before last allocated block
265 if (block) {
266 tmp = ufs_data_ptr_to_cpu(sb,
267 ufs_get_direct_data_ptr(uspi, ufsi, block - 1));
268 if (tmp)
269 goal = tmp + uspi->s_fpb;
271 tmp = ufs_new_fragments(inode, p, fragment - blockoff,
272 goal, uspi->s_fpb, err,
273 phys != NULL ? locked_page : NULL);
275 if (!tmp) {
276 if ((!blockoff && ufs_data_ptr_to_cpu(sb, p)) ||
277 (blockoff && lastfrag != ufsi->i_lastfrag))
278 goto repeat;
279 *err = -ENOSPC;
280 return NULL;
283 if (!phys) {
284 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
285 } else {
286 *phys = uspi->s_sbbase + tmp + blockoff;
287 result = NULL;
288 *err = 0;
289 *new = 1;
292 inode->i_ctime = CURRENT_TIME_SEC;
293 if (IS_SYNC(inode))
294 ufs_sync_inode (inode);
295 mark_inode_dirty(inode);
296 UFSD("EXIT, result %llu\n", (unsigned long long)tmp + blockoff);
297 return result;
299 /* This part : To be implemented ....
300 Required only for writing, not required for READ-ONLY.
301 ufs2:
303 u2_block = ufs_fragstoblks(fragment);
304 u2_blockoff = ufs_fragnum(fragment);
305 p = ufsi->i_u1.u2_i_data + block;
306 goal = 0;
308 repeat2:
309 tmp = fs32_to_cpu(sb, *p);
310 lastfrag = ufsi->i_lastfrag;
316 * ufs_inode_getblock() - allocate new block
317 * @inode - pointer to inode
318 * @bh - pointer to block which hold "pointer" to new allocated block
319 * @fragment - number of `fragment' which hold pointer
320 * to new allocated block
321 * @new_fragment - number of new allocated fragment
322 * (block will hold this fragment and also uspi->s_fpb-1)
323 * @err - see ufs_inode_getfrag()
324 * @phys - see ufs_inode_getfrag()
325 * @new - see ufs_inode_getfrag()
326 * @locked_page - see ufs_inode_getfrag()
328 static struct buffer_head *
329 ufs_inode_getblock(struct inode *inode, struct buffer_head *bh,
330 u64 fragment, sector_t new_fragment, int *err,
331 long *phys, int *new, struct page *locked_page)
333 struct super_block *sb = inode->i_sb;
334 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
335 struct buffer_head * result;
336 unsigned blockoff;
337 u64 tmp, goal, block;
338 void *p;
340 block = ufs_fragstoblks (fragment);
341 blockoff = ufs_fragnum (fragment);
343 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, metadata %d\n",
344 inode->i_ino, (unsigned long long)fragment,
345 (unsigned long long)new_fragment, !phys);
347 result = NULL;
348 if (!bh)
349 goto out;
350 if (!buffer_uptodate(bh)) {
351 ll_rw_block (READ, 1, &bh);
352 wait_on_buffer (bh);
353 if (!buffer_uptodate(bh))
354 goto out;
356 if (uspi->fs_magic == UFS2_MAGIC)
357 p = (__fs64 *)bh->b_data + block;
358 else
359 p = (__fs32 *)bh->b_data + block;
360 repeat:
361 tmp = ufs_data_ptr_to_cpu(sb, p);
362 if (tmp) {
363 if (!phys) {
364 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
365 if (tmp == ufs_data_ptr_to_cpu(sb, p))
366 goto out;
367 brelse (result);
368 goto repeat;
369 } else {
370 *phys = uspi->s_sbbase + tmp + blockoff;
371 goto out;
375 if (block && (uspi->fs_magic == UFS2_MAGIC ?
376 (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[block-1])) :
377 (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[block-1]))))
378 goal = tmp + uspi->s_fpb;
379 else
380 goal = bh->b_blocknr + uspi->s_fpb;
381 tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal,
382 uspi->s_fpb, err, locked_page);
383 if (!tmp) {
384 if (ufs_data_ptr_to_cpu(sb, p))
385 goto repeat;
386 goto out;
390 if (!phys) {
391 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
392 } else {
393 *phys = uspi->s_sbbase + tmp + blockoff;
394 *new = 1;
397 mark_buffer_dirty(bh);
398 if (IS_SYNC(inode))
399 sync_dirty_buffer(bh);
400 inode->i_ctime = CURRENT_TIME_SEC;
401 mark_inode_dirty(inode);
402 UFSD("result %llu\n", (unsigned long long)tmp + blockoff);
403 out:
404 brelse (bh);
405 UFSD("EXIT\n");
406 return result;
410 * ufs_getfrag_bloc() - `get_block_t' function, interface between UFS and
411 * readpage, writepage and so on
414 int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
416 struct super_block * sb = inode->i_sb;
417 struct ufs_sb_private_info * uspi = UFS_SB(sb)->s_uspi;
418 struct buffer_head * bh;
419 int ret, err, new;
420 unsigned long ptr,phys;
421 u64 phys64 = 0;
423 if (!create) {
424 phys64 = ufs_frag_map(inode, fragment);
425 UFSD("phys64 = %llu\n", (unsigned long long)phys64);
426 if (phys64)
427 map_bh(bh_result, sb, phys64);
428 return 0;
431 /* This code entered only while writing ....? */
433 err = -EIO;
434 new = 0;
435 ret = 0;
436 bh = NULL;
438 lock_kernel();
440 UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
441 if (fragment >
442 ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb)
443 << uspi->s_fpbshift))
444 goto abort_too_big;
446 err = 0;
447 ptr = fragment;
450 * ok, these macros clean the logic up a bit and make
451 * it much more readable:
453 #define GET_INODE_DATABLOCK(x) \
454 ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new,\
455 bh_result->b_page)
456 #define GET_INODE_PTR(x) \
457 ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL,\
458 bh_result->b_page)
459 #define GET_INDIRECT_DATABLOCK(x) \
460 ufs_inode_getblock(inode, bh, x, fragment, \
461 &err, &phys, &new, bh_result->b_page)
462 #define GET_INDIRECT_PTR(x) \
463 ufs_inode_getblock(inode, bh, x, fragment, \
464 &err, NULL, NULL, NULL)
466 if (ptr < UFS_NDIR_FRAGMENT) {
467 bh = GET_INODE_DATABLOCK(ptr);
468 goto out;
470 ptr -= UFS_NDIR_FRAGMENT;
471 if (ptr < (1 << (uspi->s_apbshift + uspi->s_fpbshift))) {
472 bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift));
473 goto get_indirect;
475 ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift);
476 if (ptr < (1 << (uspi->s_2apbshift + uspi->s_fpbshift))) {
477 bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift));
478 goto get_double;
480 ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift);
481 bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift));
482 bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask);
483 get_double:
484 bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask);
485 get_indirect:
486 bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask);
488 #undef GET_INODE_DATABLOCK
489 #undef GET_INODE_PTR
490 #undef GET_INDIRECT_DATABLOCK
491 #undef GET_INDIRECT_PTR
493 out:
494 if (err)
495 goto abort;
496 if (new)
497 set_buffer_new(bh_result);
498 map_bh(bh_result, sb, phys);
499 abort:
500 unlock_kernel();
501 return err;
503 abort_too_big:
504 ufs_warning(sb, "ufs_get_block", "block > big");
505 goto abort;
508 static struct buffer_head *ufs_getfrag(struct inode *inode,
509 unsigned int fragment,
510 int create, int *err)
512 struct buffer_head dummy;
513 int error;
515 dummy.b_state = 0;
516 dummy.b_blocknr = -1000;
517 error = ufs_getfrag_block(inode, fragment, &dummy, create);
518 *err = error;
519 if (!error && buffer_mapped(&dummy)) {
520 struct buffer_head *bh;
521 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
522 if (buffer_new(&dummy)) {
523 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
524 set_buffer_uptodate(bh);
525 mark_buffer_dirty(bh);
527 return bh;
529 return NULL;
532 struct buffer_head * ufs_bread (struct inode * inode, unsigned fragment,
533 int create, int * err)
535 struct buffer_head * bh;
537 UFSD("ENTER, ino %lu, fragment %u\n", inode->i_ino, fragment);
538 bh = ufs_getfrag (inode, fragment, create, err);
539 if (!bh || buffer_uptodate(bh))
540 return bh;
541 ll_rw_block (READ, 1, &bh);
542 wait_on_buffer (bh);
543 if (buffer_uptodate(bh))
544 return bh;
545 brelse (bh);
546 *err = -EIO;
547 return NULL;
550 static int ufs_writepage(struct page *page, struct writeback_control *wbc)
552 return block_write_full_page(page,ufs_getfrag_block,wbc);
555 static int ufs_readpage(struct file *file, struct page *page)
557 return block_read_full_page(page,ufs_getfrag_block);
560 int __ufs_write_begin(struct file *file, struct address_space *mapping,
561 loff_t pos, unsigned len, unsigned flags,
562 struct page **pagep, void **fsdata)
564 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
565 ufs_getfrag_block);
568 static int ufs_write_begin(struct file *file, struct address_space *mapping,
569 loff_t pos, unsigned len, unsigned flags,
570 struct page **pagep, void **fsdata)
572 *pagep = NULL;
573 return __ufs_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
576 static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
578 return generic_block_bmap(mapping,block,ufs_getfrag_block);
581 const struct address_space_operations ufs_aops = {
582 .readpage = ufs_readpage,
583 .writepage = ufs_writepage,
584 .sync_page = block_sync_page,
585 .write_begin = ufs_write_begin,
586 .write_end = generic_write_end,
587 .bmap = ufs_bmap
590 static void ufs_set_inode_ops(struct inode *inode)
592 if (S_ISREG(inode->i_mode)) {
593 inode->i_op = &ufs_file_inode_operations;
594 inode->i_fop = &ufs_file_operations;
595 inode->i_mapping->a_ops = &ufs_aops;
596 } else if (S_ISDIR(inode->i_mode)) {
597 inode->i_op = &ufs_dir_inode_operations;
598 inode->i_fop = &ufs_dir_operations;
599 inode->i_mapping->a_ops = &ufs_aops;
600 } else if (S_ISLNK(inode->i_mode)) {
601 if (!inode->i_blocks)
602 inode->i_op = &ufs_fast_symlink_inode_operations;
603 else {
604 inode->i_op = &page_symlink_inode_operations;
605 inode->i_mapping->a_ops = &ufs_aops;
607 } else
608 init_special_inode(inode, inode->i_mode,
609 ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
612 static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
614 struct ufs_inode_info *ufsi = UFS_I(inode);
615 struct super_block *sb = inode->i_sb;
616 mode_t mode;
619 * Copy data to the in-core inode.
621 inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
622 inode->i_nlink = fs16_to_cpu(sb, ufs_inode->ui_nlink);
623 if (inode->i_nlink == 0) {
624 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
625 return -1;
629 * Linux now has 32-bit uid and gid, so we can support EFT.
631 inode->i_uid = ufs_get_inode_uid(sb, ufs_inode);
632 inode->i_gid = ufs_get_inode_gid(sb, ufs_inode);
634 inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
635 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
636 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
637 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
638 inode->i_mtime.tv_nsec = 0;
639 inode->i_atime.tv_nsec = 0;
640 inode->i_ctime.tv_nsec = 0;
641 inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
642 inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen);
643 ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
644 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
645 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
648 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
649 memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr,
650 sizeof(ufs_inode->ui_u2.ui_addr));
651 } else {
652 memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink,
653 sizeof(ufs_inode->ui_u2.ui_symlink) - 1);
654 ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0;
656 return 0;
659 static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
661 struct ufs_inode_info *ufsi = UFS_I(inode);
662 struct super_block *sb = inode->i_sb;
663 mode_t mode;
665 UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
667 * Copy data to the in-core inode.
669 inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
670 inode->i_nlink = fs16_to_cpu(sb, ufs2_inode->ui_nlink);
671 if (inode->i_nlink == 0) {
672 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
673 return -1;
677 * Linux now has 32-bit uid and gid, so we can support EFT.
679 inode->i_uid = fs32_to_cpu(sb, ufs2_inode->ui_uid);
680 inode->i_gid = fs32_to_cpu(sb, ufs2_inode->ui_gid);
682 inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
683 inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime);
684 inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime);
685 inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime);
686 inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec);
687 inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec);
688 inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec);
689 inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
690 inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen);
691 ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
693 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
694 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
697 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
698 memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr,
699 sizeof(ufs2_inode->ui_u2.ui_addr));
700 } else {
701 memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink,
702 sizeof(ufs2_inode->ui_u2.ui_symlink) - 1);
703 ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0;
705 return 0;
708 struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
710 struct ufs_inode_info *ufsi;
711 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
712 struct buffer_head * bh;
713 struct inode *inode;
714 int err;
716 UFSD("ENTER, ino %lu\n", ino);
718 if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) {
719 ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n",
720 ino);
721 return ERR_PTR(-EIO);
724 inode = iget_locked(sb, ino);
725 if (!inode)
726 return ERR_PTR(-ENOMEM);
727 if (!(inode->i_state & I_NEW))
728 return inode;
730 ufsi = UFS_I(inode);
732 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
733 if (!bh) {
734 ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n",
735 inode->i_ino);
736 goto bad_inode;
738 if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
739 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
741 err = ufs2_read_inode(inode,
742 ufs2_inode + ufs_inotofsbo(inode->i_ino));
743 } else {
744 struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data;
746 err = ufs1_read_inode(inode,
747 ufs_inode + ufs_inotofsbo(inode->i_ino));
750 if (err)
751 goto bad_inode;
752 inode->i_version++;
753 ufsi->i_lastfrag =
754 (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
755 ufsi->i_dir_start_lookup = 0;
756 ufsi->i_osync = 0;
758 ufs_set_inode_ops(inode);
760 brelse(bh);
762 UFSD("EXIT\n");
763 unlock_new_inode(inode);
764 return inode;
766 bad_inode:
767 iget_failed(inode);
768 return ERR_PTR(-EIO);
771 static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
773 struct super_block *sb = inode->i_sb;
774 struct ufs_inode_info *ufsi = UFS_I(inode);
776 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
777 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
779 ufs_set_inode_uid(sb, ufs_inode, inode->i_uid);
780 ufs_set_inode_gid(sb, ufs_inode, inode->i_gid);
782 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
783 ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
784 ufs_inode->ui_atime.tv_usec = 0;
785 ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
786 ufs_inode->ui_ctime.tv_usec = 0;
787 ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
788 ufs_inode->ui_mtime.tv_usec = 0;
789 ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
790 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
791 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
793 if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) {
794 ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
795 ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
798 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
799 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
800 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
801 } else if (inode->i_blocks) {
802 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data,
803 sizeof(ufs_inode->ui_u2.ui_addr));
805 else {
806 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
807 sizeof(ufs_inode->ui_u2.ui_symlink));
810 if (!inode->i_nlink)
811 memset (ufs_inode, 0, sizeof(struct ufs_inode));
814 static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
816 struct super_block *sb = inode->i_sb;
817 struct ufs_inode_info *ufsi = UFS_I(inode);
819 UFSD("ENTER\n");
820 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
821 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
823 ufs_inode->ui_uid = cpu_to_fs32(sb, inode->i_uid);
824 ufs_inode->ui_gid = cpu_to_fs32(sb, inode->i_gid);
826 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
827 ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec);
828 ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec);
829 ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec);
830 ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec);
831 ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec);
832 ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec);
834 ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks);
835 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
836 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
838 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
839 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
840 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0];
841 } else if (inode->i_blocks) {
842 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data,
843 sizeof(ufs_inode->ui_u2.ui_addr));
844 } else {
845 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
846 sizeof(ufs_inode->ui_u2.ui_symlink));
849 if (!inode->i_nlink)
850 memset (ufs_inode, 0, sizeof(struct ufs2_inode));
851 UFSD("EXIT\n");
854 static int ufs_update_inode(struct inode * inode, int do_sync)
856 struct super_block *sb = inode->i_sb;
857 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
858 struct buffer_head * bh;
860 UFSD("ENTER, ino %lu\n", inode->i_ino);
862 if (inode->i_ino < UFS_ROOTINO ||
863 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
864 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
865 return -1;
868 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
869 if (!bh) {
870 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
871 return -1;
873 if (uspi->fs_magic == UFS2_MAGIC) {
874 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
876 ufs2_update_inode(inode,
877 ufs2_inode + ufs_inotofsbo(inode->i_ino));
878 } else {
879 struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data;
881 ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
884 mark_buffer_dirty(bh);
885 if (do_sync)
886 sync_dirty_buffer(bh);
887 brelse (bh);
889 UFSD("EXIT\n");
890 return 0;
893 int ufs_write_inode (struct inode * inode, int wait)
895 int ret;
896 lock_kernel();
897 ret = ufs_update_inode (inode, wait);
898 unlock_kernel();
899 return ret;
902 int ufs_sync_inode (struct inode *inode)
904 return ufs_update_inode (inode, 1);
907 void ufs_delete_inode (struct inode * inode)
909 loff_t old_i_size;
911 truncate_inode_pages(&inode->i_data, 0);
912 if (is_bad_inode(inode))
913 goto no_delete;
914 /*UFS_I(inode)->i_dtime = CURRENT_TIME;*/
915 lock_kernel();
916 mark_inode_dirty(inode);
917 ufs_update_inode(inode, IS_SYNC(inode));
918 old_i_size = inode->i_size;
919 inode->i_size = 0;
920 if (inode->i_blocks && ufs_truncate(inode, old_i_size))
921 ufs_warning(inode->i_sb, __func__, "ufs_truncate failed\n");
922 ufs_free_inode (inode);
923 unlock_kernel();
924 return;
925 no_delete:
926 clear_inode(inode); /* We must guarantee clearing of inode... */