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/buffer_head.h>
11 #include <linux/module.h>
14 #include <linux/efs_fs_sb.h>
16 static int efs_readpage(struct file
*file
, struct page
*page
)
18 return block_read_full_page(page
,efs_get_block
);
20 static sector_t
_efs_bmap(struct address_space
*mapping
, sector_t block
)
22 return generic_block_bmap(mapping
,block
,efs_get_block
);
24 static const struct address_space_operations efs_aops
= {
25 .readpage
= efs_readpage
,
29 static inline void extent_copy(efs_extent
*src
, efs_extent
*dst
) {
31 * this is slightly evil. it doesn't just copy
32 * efs_extent from src to dst, it also mangles
33 * the bits so that dst ends up in cpu byte-order.
36 dst
->cooked
.ex_magic
= (unsigned int) src
->raw
[0];
37 dst
->cooked
.ex_bn
= ((unsigned int) src
->raw
[1] << 16) |
38 ((unsigned int) src
->raw
[2] << 8) |
39 ((unsigned int) src
->raw
[3] << 0);
40 dst
->cooked
.ex_length
= (unsigned int) src
->raw
[4];
41 dst
->cooked
.ex_offset
= ((unsigned int) src
->raw
[5] << 16) |
42 ((unsigned int) src
->raw
[6] << 8) |
43 ((unsigned int) src
->raw
[7] << 0);
47 struct inode
*efs_iget(struct super_block
*super
, unsigned long ino
)
52 struct buffer_head
*bh
;
53 struct efs_sb_info
*sb
= SUPER_INFO(super
);
54 struct efs_inode_info
*in
;
55 efs_block_t block
, offset
;
56 struct efs_dinode
*efs_inode
;
59 inode
= iget_locked(super
, ino
);
61 return ERR_PTR(-ENOMEM
);
62 if (!(inode
->i_state
& I_NEW
))
65 in
= INODE_INFO(inode
);
70 ** | cylinder group | cylinder group | cylinder group ..etc
71 ** |inodes|data |inodes|data |inodes|data ..etc
73 ** work out the inode block index, (considering initially that the
74 ** inodes are stored as consecutive blocks). then work out the block
75 ** number of that inode given the above layout, and finally the
76 ** offset of the inode within that block.
79 inode_index
= inode
->i_ino
/
80 (EFS_BLOCKSIZE
/ sizeof(struct efs_dinode
));
82 block
= sb
->fs_start
+ sb
->first_block
+
83 (sb
->group_size
* (inode_index
/ sb
->inode_blocks
)) +
84 (inode_index
% sb
->inode_blocks
);
86 offset
= (inode
->i_ino
%
87 (EFS_BLOCKSIZE
/ sizeof(struct efs_dinode
))) *
88 sizeof(struct efs_dinode
);
90 bh
= sb_bread(inode
->i_sb
, block
);
92 printk(KERN_WARNING
"EFS: bread() failed at block %d\n", block
);
93 goto read_inode_error
;
96 efs_inode
= (struct efs_dinode
*) (bh
->b_data
+ offset
);
98 inode
->i_mode
= be16_to_cpu(efs_inode
->di_mode
);
99 set_nlink(inode
, be16_to_cpu(efs_inode
->di_nlink
));
100 inode
->i_uid
= (uid_t
)be16_to_cpu(efs_inode
->di_uid
);
101 inode
->i_gid
= (gid_t
)be16_to_cpu(efs_inode
->di_gid
);
102 inode
->i_size
= be32_to_cpu(efs_inode
->di_size
);
103 inode
->i_atime
.tv_sec
= be32_to_cpu(efs_inode
->di_atime
);
104 inode
->i_mtime
.tv_sec
= be32_to_cpu(efs_inode
->di_mtime
);
105 inode
->i_ctime
.tv_sec
= be32_to_cpu(efs_inode
->di_ctime
);
106 inode
->i_atime
.tv_nsec
= inode
->i_mtime
.tv_nsec
= inode
->i_ctime
.tv_nsec
= 0;
108 /* this is the number of blocks in the file */
109 if (inode
->i_size
== 0) {
112 inode
->i_blocks
= ((inode
->i_size
- 1) >> EFS_BLOCKSIZE_BITS
) + 1;
115 rdev
= be16_to_cpu(efs_inode
->di_u
.di_dev
.odev
);
116 if (rdev
== 0xffff) {
117 rdev
= be32_to_cpu(efs_inode
->di_u
.di_dev
.ndev
);
118 if (sysv_major(rdev
) > 0xfff)
121 device
= MKDEV(sysv_major(rdev
), sysv_minor(rdev
));
123 device
= old_decode_dev(rdev
);
125 /* get the number of extents for this object */
126 in
->numextents
= be16_to_cpu(efs_inode
->di_numextents
);
129 /* copy the extents contained within the inode to memory */
130 for(i
= 0; i
< EFS_DIRECTEXTENTS
; i
++) {
131 extent_copy(&(efs_inode
->di_u
.di_extents
[i
]), &(in
->extents
[i
]));
132 if (i
< in
->numextents
&& in
->extents
[i
].cooked
.ex_magic
!= 0) {
133 printk(KERN_WARNING
"EFS: extent %d has bad magic number in inode %lu\n", i
, inode
->i_ino
);
135 goto read_inode_error
;
142 printk(KERN_DEBUG
"EFS: efs_iget(): inode %lu, extents %d, mode %o\n",
143 inode
->i_ino
, in
->numextents
, inode
->i_mode
);
146 switch (inode
->i_mode
& S_IFMT
) {
148 inode
->i_op
= &efs_dir_inode_operations
;
149 inode
->i_fop
= &efs_dir_operations
;
152 inode
->i_fop
= &generic_ro_fops
;
153 inode
->i_data
.a_ops
= &efs_aops
;
156 inode
->i_op
= &page_symlink_inode_operations
;
157 inode
->i_data
.a_ops
= &efs_symlink_aops
;
162 init_special_inode(inode
, inode
->i_mode
, device
);
165 printk(KERN_WARNING
"EFS: unsupported inode mode %o\n", inode
->i_mode
);
166 goto read_inode_error
;
170 unlock_new_inode(inode
);
174 printk(KERN_WARNING
"EFS: failed to read inode %lu\n", inode
->i_ino
);
176 return ERR_PTR(-EIO
);
179 static inline efs_block_t
180 efs_extent_check(efs_extent
*ptr
, efs_block_t block
, struct efs_sb_info
*sb
) {
186 * given an extent and a logical block within a file,
187 * can this block be found within this extent ?
189 start
= ptr
->cooked
.ex_bn
;
190 length
= ptr
->cooked
.ex_length
;
191 offset
= ptr
->cooked
.ex_offset
;
193 if ((block
>= offset
) && (block
< offset
+length
)) {
194 return(sb
->fs_start
+ start
+ block
- offset
);
200 efs_block_t
efs_map_block(struct inode
*inode
, efs_block_t block
) {
201 struct efs_sb_info
*sb
= SUPER_INFO(inode
->i_sb
);
202 struct efs_inode_info
*in
= INODE_INFO(inode
);
203 struct buffer_head
*bh
= NULL
;
205 int cur
, last
, first
= 1;
206 int ibase
, ioffset
, dirext
, direxts
, indext
, indexts
;
207 efs_block_t iblock
, result
= 0, lastblock
= 0;
208 efs_extent ext
, *exts
;
210 last
= in
->lastextent
;
212 if (in
->numextents
<= EFS_DIRECTEXTENTS
) {
213 /* first check the last extent we returned */
214 if ((result
= efs_extent_check(&in
->extents
[last
], block
, sb
)))
217 /* if we only have one extent then nothing can be found */
218 if (in
->numextents
== 1) {
219 printk(KERN_ERR
"EFS: map_block() failed to map (1 extent)\n");
223 direxts
= in
->numextents
;
226 * check the stored extents in the inode
227 * start with next extent and check forwards
229 for(dirext
= 1; dirext
< direxts
; dirext
++) {
230 cur
= (last
+ dirext
) % in
->numextents
;
231 if ((result
= efs_extent_check(&in
->extents
[cur
], block
, sb
))) {
232 in
->lastextent
= cur
;
237 printk(KERN_ERR
"EFS: map_block() failed to map block %u (dir)\n", block
);
242 printk(KERN_DEBUG
"EFS: map_block(): indirect search for logical block %u\n", block
);
244 direxts
= in
->extents
[0].cooked
.ex_offset
;
245 indexts
= in
->numextents
;
247 for(indext
= 0; indext
< indexts
; indext
++) {
248 cur
= (last
+ indext
) % indexts
;
251 * work out which direct extent contains `cur'.
253 * also compute ibase: i.e. the number of the first
254 * indirect extent contained within direct extent `cur'.
258 for(dirext
= 0; cur
< ibase
&& dirext
< direxts
; dirext
++) {
259 ibase
+= in
->extents
[dirext
].cooked
.ex_length
*
260 (EFS_BLOCKSIZE
/ sizeof(efs_extent
));
263 if (dirext
== direxts
) {
264 /* should never happen */
265 printk(KERN_ERR
"EFS: couldn't find direct extent for indirect extent %d (block %u)\n", cur
, block
);
270 /* work out block number and offset of this indirect extent */
271 iblock
= sb
->fs_start
+ in
->extents
[dirext
].cooked
.ex_bn
+
273 (EFS_BLOCKSIZE
/ sizeof(efs_extent
));
274 ioffset
= (cur
- ibase
) %
275 (EFS_BLOCKSIZE
/ sizeof(efs_extent
));
277 if (first
|| lastblock
!= iblock
) {
280 bh
= sb_bread(inode
->i_sb
, iblock
);
282 printk(KERN_ERR
"EFS: bread() failed at block %d\n", iblock
);
286 printk(KERN_DEBUG
"EFS: map_block(): read indirect extent block %d\n", iblock
);
292 exts
= (efs_extent
*) bh
->b_data
;
294 extent_copy(&(exts
[ioffset
]), &ext
);
296 if (ext
.cooked
.ex_magic
!= 0) {
297 printk(KERN_ERR
"EFS: extent %d has bad magic number in block %d\n", cur
, iblock
);
302 if ((result
= efs_extent_check(&ext
, block
, sb
))) {
304 in
->lastextent
= cur
;
309 printk(KERN_ERR
"EFS: map_block() failed to map block %u (indir)\n", block
);
313 MODULE_LICENSE("GPL");