4 * Copyright (c) 1999 Al Smith
6 * Portions derived from work (c) 1995,1996 Christian Vogelgsang,
7 * and from work (c) 1998 Mike Shaver.
10 #include <linux/efs_fs.h>
11 #include <linux/efs_fs_sb.h>
13 static inline void extent_copy(efs_extent
*src
, efs_extent
*dst
) {
15 * this is slightly evil. it doesn't just copy
16 * efs_extent from src to dst, it also mangles
17 * the bits so that dst ends up in cpu byte-order.
20 dst
->cooked
.ex_magic
= (unsigned int) src
->raw
[0];
21 dst
->cooked
.ex_bn
= ((unsigned int) src
->raw
[1] << 16) |
22 ((unsigned int) src
->raw
[2] << 8) |
23 ((unsigned int) src
->raw
[3] << 0);
24 dst
->cooked
.ex_length
= (unsigned int) src
->raw
[4];
25 dst
->cooked
.ex_offset
= ((unsigned int) src
->raw
[5] << 16) |
26 ((unsigned int) src
->raw
[6] << 8) |
27 ((unsigned int) src
->raw
[7] << 0);
31 void efs_read_inode(struct inode
*inode
) {
34 struct buffer_head
*bh
;
35 struct efs_sb_info
*sb
= SUPER_INFO(inode
->i_sb
);
36 struct efs_inode_info
*in
= INODE_INFO(inode
);
37 efs_block_t block
, offset
;
38 struct efs_dinode
*efs_inode
;
43 ** | cylinder group | cylinder group | cylinder group ..etc
44 ** |inodes|data |inodes|data |inodes|data ..etc
46 ** work out the inode block index, (considering initially that the
47 ** inodes are stored as consecutive blocks). then work out the block
48 ** number of that inode given the above layout, and finally the
49 ** offset of the inode within that block.
52 inode_index
= inode
->i_ino
/
53 (EFS_BLOCKSIZE
/ sizeof(struct efs_dinode
));
55 block
= sb
->fs_start
+ sb
->first_block
+
56 (sb
->group_size
* (inode_index
/ sb
->inode_blocks
)) +
57 (inode_index
% sb
->inode_blocks
);
59 offset
= (inode
->i_ino
%
60 (EFS_BLOCKSIZE
/ sizeof(struct efs_dinode
))) *
61 sizeof(struct efs_dinode
);
63 bh
= bread(inode
->i_dev
, block
, EFS_BLOCKSIZE
);
65 printk(KERN_WARNING
"EFS: bread() failed at block %d\n", block
);
66 goto read_inode_error
;
69 efs_inode
= (struct efs_dinode
*) (bh
->b_data
+ offset
);
71 inode
->i_mode
= be16_to_cpu(efs_inode
->di_mode
);
72 inode
->i_nlink
= be16_to_cpu(efs_inode
->di_nlink
);
73 inode
->i_uid
= be16_to_cpu(efs_inode
->di_uid
);
74 inode
->i_gid
= be16_to_cpu(efs_inode
->di_gid
);
75 inode
->i_size
= be32_to_cpu(efs_inode
->di_size
);
76 inode
->i_atime
= be32_to_cpu(efs_inode
->di_atime
);
77 inode
->i_mtime
= be32_to_cpu(efs_inode
->di_mtime
);
78 inode
->i_ctime
= be32_to_cpu(efs_inode
->di_ctime
);
80 /* this is the number of blocks in the file */
81 if (inode
->i_size
== 0) {
84 inode
->i_blocks
= ((inode
->i_size
- 1) >> EFS_BLOCKSIZE_BITS
) + 1;
88 * BUG: irix dev_t is 32-bits. linux dev_t is only 16-bits.
90 * apparently linux will change to 32-bit dev_t sometime during
93 * as is, this code maps devices that can't be represented in
94 * 16-bits (ie major > 255 or minor > 255) to major = minor = 255.
96 * during 2.3 when 32-bit dev_t become available, we should test
97 * to see whether odev contains 65535. if this is the case then we
98 * should then do device = be32_to_cpu(efs_inode->di_u.di_dev.ndev).
100 device
= be16_to_cpu(efs_inode
->di_u
.di_dev
.odev
);
102 /* get the number of extents for this object */
103 in
->numextents
= be16_to_cpu(efs_inode
->di_numextents
);
106 /* copy the extents contained within the inode to memory */
107 for(i
= 0; i
< EFS_DIRECTEXTENTS
; i
++) {
108 extent_copy(&(efs_inode
->di_u
.di_extents
[i
]), &(in
->extents
[i
]));
109 if (i
< in
->numextents
&& in
->extents
[i
].cooked
.ex_magic
!= 0) {
110 printk(KERN_WARNING
"EFS: extent %d has bad magic number in inode %lu\n", i
, inode
->i_ino
);
112 goto read_inode_error
;
119 printk(KERN_DEBUG
"EFS: read_inode(): inode %lu, extents %d, mode %o\n",
120 inode
->i_ino
, in
->numextents
, inode
->i_mode
);
123 switch (inode
->i_mode
& S_IFMT
) {
125 inode
->i_op
= &efs_dir_inode_operations
;
128 inode
->i_op
= &efs_file_inode_operations
;
131 inode
->i_op
= &efs_symlink_inode_operations
;
134 inode
->i_rdev
= device
;
135 inode
->i_op
= &chrdev_inode_operations
;
138 inode
->i_rdev
= device
;
139 inode
->i_op
= &blkdev_inode_operations
;
145 printk(KERN_WARNING
"EFS: unsupported inode mode %o\n", inode
->i_mode
);
146 goto read_inode_error
;
153 printk(KERN_WARNING
"EFS: failed to read inode %lu\n", inode
->i_ino
);
154 inode
->i_mode
= S_IFREG
;
168 static inline efs_block_t
169 efs_extent_check(efs_extent
*ptr
, efs_block_t block
, struct efs_sb_info
*sb
) {
175 * given an extent and a logical block within a file,
176 * can this block be found within this extent ?
178 start
= ptr
->cooked
.ex_bn
;
179 length
= ptr
->cooked
.ex_length
;
180 offset
= ptr
->cooked
.ex_offset
;
182 if ((block
>= offset
) && (block
< offset
+length
)) {
183 return(sb
->fs_start
+ start
+ block
- offset
);
189 efs_block_t
efs_map_block(struct inode
*inode
, efs_block_t block
) {
190 struct efs_sb_info
*sb
= SUPER_INFO(inode
->i_sb
);
191 struct efs_inode_info
*in
= INODE_INFO(inode
);
192 struct buffer_head
*bh
= NULL
;
194 int cur
, last
, first
= 1;
195 int ibase
, ioffset
, dirext
, direxts
, indext
, indexts
;
196 efs_block_t iblock
, result
= 0, lastblock
= 0;
197 efs_extent ext
, *exts
;
199 last
= in
->lastextent
;
201 if (in
->numextents
<= EFS_DIRECTEXTENTS
) {
202 /* first check the last extent we returned */
203 if ((result
= efs_extent_check(&in
->extents
[last
], block
, sb
)))
206 /* if we only have one extent then nothing can be found */
207 if (in
->numextents
== 1) {
208 printk(KERN_ERR
"EFS: map_block() failed to map (1 extent)\n");
212 direxts
= in
->numextents
;
215 * check the stored extents in the inode
216 * start with next extent and check forwards
218 for(dirext
= 1; dirext
< direxts
; dirext
++) {
219 cur
= (last
+ dirext
) % in
->numextents
;
220 if ((result
= efs_extent_check(&in
->extents
[cur
], block
, sb
))) {
221 in
->lastextent
= cur
;
226 printk(KERN_ERR
"EFS: map_block() failed to map block %u (dir)\n", block
);
231 printk(KERN_DEBUG
"EFS: map_block(): indirect search for logical block %u\n", block
);
233 direxts
= in
->extents
[0].cooked
.ex_offset
;
234 indexts
= in
->numextents
;
236 for(indext
= 0; indext
< indexts
; indext
++) {
237 cur
= (last
+ indext
) % indexts
;
240 * work out which direct extent contains `cur'.
242 * also compute ibase: i.e. the number of the first
243 * indirect extent contained within direct extent `cur'.
247 for(dirext
= 0; cur
< ibase
&& dirext
< direxts
; dirext
++) {
248 ibase
+= in
->extents
[dirext
].cooked
.ex_length
*
249 (EFS_BLOCKSIZE
/ sizeof(efs_extent
));
252 if (dirext
== direxts
) {
253 /* should never happen */
254 printk(KERN_ERR
"EFS: couldn't find direct extent for indirect extent %d (block %u)\n", cur
, block
);
259 /* work out block number and offset of this indirect extent */
260 iblock
= sb
->fs_start
+ in
->extents
[dirext
].cooked
.ex_bn
+
262 (EFS_BLOCKSIZE
/ sizeof(efs_extent
));
263 ioffset
= (cur
- ibase
) %
264 (EFS_BLOCKSIZE
/ sizeof(efs_extent
));
266 if (first
|| lastblock
!= iblock
) {
269 bh
= bread(inode
->i_dev
, iblock
, EFS_BLOCKSIZE
);
271 printk(KERN_ERR
"EFS: bread() failed at block %d\n", iblock
);
275 printk(KERN_DEBUG
"EFS: map_block(): read indirect extent block %d\n", iblock
);
281 exts
= (efs_extent
*) bh
->b_data
;
283 extent_copy(&(exts
[ioffset
]), &ext
);
285 if (ext
.cooked
.ex_magic
!= 0) {
286 printk(KERN_ERR
"EFS: extent %d has bad magic number in block %d\n", cur
, iblock
);
291 if ((result
= efs_extent_check(&ext
, block
, sb
))) {
293 in
->lastextent
= cur
;
298 printk(KERN_ERR
"EFS: map_block() failed to map block %u (indir)\n", block
);