5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
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)
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>
33 #include <linux/time.h>
34 #include <linux/stat.h>
35 #include <linux/string.h>
37 #include <linux/smp_lock.h>
38 #include <linux/buffer_head.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));
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);
74 ufs_warning(inode
->i_sb
, "ufs_block_to_path", "block > big");
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
);
96 unsigned flags
= UFS_SB(sb
)->s_flags
;
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
);
110 if ((flags
& UFS_TYPE_MASK
) == UFS_TYPE_UFS2
)
113 block
= ufsi
->i_u1
.i_data
[*p
++];
117 struct buffer_head
*bh
;
120 bh
= sb_bread(sb
, uspi
->s_sbbase
+ fs32_to_cpu(sb
, block
)+(n
>>shift
));
123 block
= ((__fs32
*) bh
->b_data
)[n
& mask
];
128 ret
= (u64
) (uspi
->s_sbbase
+ fs32_to_cpu(sb
, block
) + (frag
& uspi
->s_fpbmask
));
131 u2_block
= ufsi
->i_u1
.u2_i_data
[*p
++];
137 struct buffer_head
*bh
;
141 temp
= (u64
)(uspi
->s_sbbase
) + fs64_to_cpu(sb
, u2_block
);
142 bh
= sb_bread(sb
, temp
+(u64
) (n
>>shift
));
145 u2_block
= ((__fs64
*)bh
->b_data
)[n
& mask
];
150 temp
= (u64
)uspi
->s_sbbase
+ fs64_to_cpu(sb
, u2_block
);
151 ret
= temp
+ (u64
) (frag
& uspi
->s_fpbmask
);
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
;
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)
193 block
= ufs_fragstoblks (fragment
);
194 blockoff
= ufs_fragnum (fragment
);
195 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, block
);
200 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
202 lastfrag
= ufsi
->i_lastfrag
;
203 if (tmp
&& fragment
< lastfrag
) {
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
);
214 *phys
= uspi
->s_sbbase
+ tmp
+ blockoff
;
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
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
,
235 if (lastfrag
!= ufsi
->i_lastfrag
)
240 lastfrag
= ufsi
->i_lastfrag
;
243 tmp
= ufs_data_ptr_to_cpu(sb
,
244 ufs_get_direct_data_ptr(uspi
, ufsi
,
247 goal
= tmp
+ uspi
->s_fpb
;
248 tmp
= ufs_new_fragments (inode
, p
, fragment
- blockoff
,
249 goal
, required
+ blockoff
,
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
266 tmp
= ufs_data_ptr_to_cpu(sb
,
267 ufs_get_direct_data_ptr(uspi
, ufsi
, block
- 1));
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
);
276 if ((!blockoff
&& ufs_data_ptr_to_cpu(sb
, p
)) ||
277 (blockoff
&& lastfrag
!= ufsi
->i_lastfrag
))
284 result
= sb_getblk(sb
, uspi
->s_sbbase
+ tmp
+ blockoff
);
286 *phys
= uspi
->s_sbbase
+ tmp
+ blockoff
;
292 inode
->i_ctime
= CURRENT_TIME_SEC
;
294 ufs_sync_inode (inode
);
295 mark_inode_dirty(inode
);
296 UFSD("EXIT, result %llu\n", (unsigned long long)tmp
+ blockoff
);
299 /* This part : To be implemented ....
300 Required only for writing, not required for READ-ONLY.
303 u2_block = ufs_fragstoblks(fragment);
304 u2_blockoff = ufs_fragnum(fragment);
305 p = ufsi->i_u1.u2_i_data + block;
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
;
337 u64 tmp
, goal
, block
;
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
);
350 if (!buffer_uptodate(bh
)) {
351 ll_rw_block (READ
, 1, &bh
);
353 if (!buffer_uptodate(bh
))
356 if (uspi
->fs_magic
== UFS2_MAGIC
)
357 p
= (__fs64
*)bh
->b_data
+ block
;
359 p
= (__fs32
*)bh
->b_data
+ block
;
361 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
364 result
= sb_getblk(sb
, uspi
->s_sbbase
+ tmp
+ blockoff
);
365 if (tmp
== ufs_data_ptr_to_cpu(sb
, p
))
370 *phys
= uspi
->s_sbbase
+ tmp
+ blockoff
;
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
;
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
);
384 if (ufs_data_ptr_to_cpu(sb
, p
))
391 result
= sb_getblk(sb
, uspi
->s_sbbase
+ tmp
+ blockoff
);
393 *phys
= uspi
->s_sbbase
+ tmp
+ blockoff
;
397 mark_buffer_dirty(bh
);
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
);
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
;
420 unsigned long ptr
,phys
;
424 phys64
= ufs_frag_map(inode
, fragment
);
425 UFSD("phys64 = %llu\n", (unsigned long long)phys64
);
427 map_bh(bh_result
, sb
, phys64
);
431 /* This code entered only while writing ....? */
440 UFSD("ENTER, ino %lu, fragment %llu\n", inode
->i_ino
, (unsigned long long)fragment
);
442 ((UFS_NDADDR
+ uspi
->s_apb
+ uspi
->s_2apb
+ uspi
->s_3apb
)
443 << uspi
->s_fpbshift
))
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,\
456 #define GET_INODE_PTR(x) \
457 ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL,\
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
);
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
));
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
));
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
);
484 bh
= GET_INDIRECT_PTR((ptr
>> uspi
->s_apbshift
) & uspi
->s_apbmask
);
486 bh
= GET_INDIRECT_DATABLOCK(ptr
& uspi
->s_apbmask
);
488 #undef GET_INODE_DATABLOCK
490 #undef GET_INDIRECT_DATABLOCK
491 #undef GET_INDIRECT_PTR
497 set_buffer_new(bh_result
);
498 map_bh(bh_result
, sb
, phys
);
504 ufs_warning(sb
, "ufs_get_block", "block > big");
508 static struct buffer_head
*ufs_getfrag(struct inode
*inode
,
509 unsigned int fragment
,
510 int create
, int *err
)
512 struct buffer_head dummy
;
516 dummy
.b_blocknr
= -1000;
517 error
= ufs_getfrag_block(inode
, fragment
, &dummy
, create
);
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
);
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
))
541 ll_rw_block (READ
, 1, &bh
);
543 if (buffer_uptodate(bh
))
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
,
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
)
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
,
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
;
604 inode
->i_op
= &page_symlink_inode_operations
;
605 inode
->i_mapping
->a_ops
= &ufs_aops
;
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
;
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
);
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
));
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;
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
;
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
);
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
));
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;
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
;
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",
721 return ERR_PTR(-EIO
);
724 inode
= iget_locked(sb
, ino
);
726 return ERR_PTR(-ENOMEM
);
727 if (!(inode
->i_state
& I_NEW
))
732 bh
= sb_bread(sb
, uspi
->s_sbbase
+ ufs_inotofsba(inode
->i_ino
));
734 ufs_warning(sb
, "ufs_read_inode", "unable to read inode %lu\n",
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
));
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
));
754 (inode
->i_size
+ uspi
->s_fsize
- 1) >> uspi
->s_fshift
;
755 ufsi
->i_dir_start_lookup
= 0;
758 ufs_set_inode_ops(inode
);
763 unlock_new_inode(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
));
806 memcpy(&ufs_inode
->ui_u2
.ui_symlink
, ufsi
->i_u1
.i_symlink
,
807 sizeof(ufs_inode
->ui_u2
.ui_symlink
));
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
);
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
));
845 memcpy(&ufs_inode
->ui_u2
.ui_symlink
, ufsi
->i_u1
.i_symlink
,
846 sizeof(ufs_inode
->ui_u2
.ui_symlink
));
850 memset (ufs_inode
, 0, sizeof(struct ufs2_inode
));
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
);
868 bh
= sb_bread(sb
, ufs_inotofsba(inode
->i_ino
));
870 ufs_warning (sb
, "ufs_read_inode", "unable to read inode %lu\n", inode
->i_ino
);
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
));
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
);
886 sync_dirty_buffer(bh
);
893 int ufs_write_inode (struct inode
* inode
, int wait
)
897 ret
= ufs_update_inode (inode
, wait
);
902 int ufs_sync_inode (struct inode
*inode
)
904 return ufs_update_inode (inode
, 1);
907 void ufs_delete_inode (struct inode
* inode
)
911 truncate_inode_pages(&inode
->i_data
, 0);
912 if (is_bad_inode(inode
))
914 /*UFS_I(inode)->i_dtime = CURRENT_TIME;*/
916 mark_inode_dirty(inode
);
917 ufs_update_inode(inode
, IS_SYNC(inode
));
918 old_i_size
= inode
->i_size
;
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
);
926 clear_inode(inode
); /* We must guarantee clearing of inode... */