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 <<<<<<< HEAD
:fs
/efs
/inode
.c
11 #include <linux/efs_fs.h>
12 #include <linux/efs_fs_sb.h>
14 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/efs
/inode
.c
15 #include <linux/buffer_head.h>
16 #include <linux/module.h>
18 <<<<<<< HEAD
:fs
/efs
/inode
.c
21 #include <linux/efs_fs_sb.h>
22 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/efs
/inode
.c
24 static int efs_readpage(struct file
*file
, struct page
*page
)
26 return block_read_full_page(page
,efs_get_block
);
28 static sector_t
_efs_bmap(struct address_space
*mapping
, sector_t block
)
30 return generic_block_bmap(mapping
,block
,efs_get_block
);
32 static const struct address_space_operations efs_aops
= {
33 .readpage
= efs_readpage
,
34 .sync_page
= block_sync_page
,
38 static inline void extent_copy(efs_extent
*src
, efs_extent
*dst
) {
40 * this is slightly evil. it doesn't just copy
41 * efs_extent from src to dst, it also mangles
42 * the bits so that dst ends up in cpu byte-order.
45 dst
->cooked
.ex_magic
= (unsigned int) src
->raw
[0];
46 dst
->cooked
.ex_bn
= ((unsigned int) src
->raw
[1] << 16) |
47 ((unsigned int) src
->raw
[2] << 8) |
48 ((unsigned int) src
->raw
[3] << 0);
49 dst
->cooked
.ex_length
= (unsigned int) src
->raw
[4];
50 dst
->cooked
.ex_offset
= ((unsigned int) src
->raw
[5] << 16) |
51 ((unsigned int) src
->raw
[6] << 8) |
52 ((unsigned int) src
->raw
[7] << 0);
56 struct inode
*efs_iget(struct super_block
*super
, unsigned long ino
)
61 struct buffer_head
*bh
;
62 struct efs_sb_info
*sb
= SUPER_INFO(super
);
63 struct efs_inode_info
*in
;
64 efs_block_t block
, offset
;
65 struct efs_dinode
*efs_inode
;
68 inode
= iget_locked(super
, ino
);
70 return ERR_PTR(-ENOMEM
);
71 if (!(inode
->i_state
& I_NEW
))
74 in
= INODE_INFO(inode
);
79 ** | cylinder group | cylinder group | cylinder group ..etc
80 ** |inodes|data |inodes|data |inodes|data ..etc
82 ** work out the inode block index, (considering initially that the
83 ** inodes are stored as consecutive blocks). then work out the block
84 ** number of that inode given the above layout, and finally the
85 ** offset of the inode within that block.
88 inode_index
= inode
->i_ino
/
89 (EFS_BLOCKSIZE
/ sizeof(struct efs_dinode
));
91 block
= sb
->fs_start
+ sb
->first_block
+
92 (sb
->group_size
* (inode_index
/ sb
->inode_blocks
)) +
93 (inode_index
% sb
->inode_blocks
);
95 offset
= (inode
->i_ino
%
96 (EFS_BLOCKSIZE
/ sizeof(struct efs_dinode
))) *
97 sizeof(struct efs_dinode
);
99 bh
= sb_bread(inode
->i_sb
, block
);
101 printk(KERN_WARNING
"EFS: bread() failed at block %d\n", block
);
102 goto read_inode_error
;
105 efs_inode
= (struct efs_dinode
*) (bh
->b_data
+ offset
);
107 inode
->i_mode
= be16_to_cpu(efs_inode
->di_mode
);
108 inode
->i_nlink
= be16_to_cpu(efs_inode
->di_nlink
);
109 inode
->i_uid
= (uid_t
)be16_to_cpu(efs_inode
->di_uid
);
110 inode
->i_gid
= (gid_t
)be16_to_cpu(efs_inode
->di_gid
);
111 inode
->i_size
= be32_to_cpu(efs_inode
->di_size
);
112 inode
->i_atime
.tv_sec
= be32_to_cpu(efs_inode
->di_atime
);
113 inode
->i_mtime
.tv_sec
= be32_to_cpu(efs_inode
->di_mtime
);
114 inode
->i_ctime
.tv_sec
= be32_to_cpu(efs_inode
->di_ctime
);
115 inode
->i_atime
.tv_nsec
= inode
->i_mtime
.tv_nsec
= inode
->i_ctime
.tv_nsec
= 0;
117 /* this is the number of blocks in the file */
118 if (inode
->i_size
== 0) {
121 inode
->i_blocks
= ((inode
->i_size
- 1) >> EFS_BLOCKSIZE_BITS
) + 1;
124 rdev
= be16_to_cpu(efs_inode
->di_u
.di_dev
.odev
);
125 if (rdev
== 0xffff) {
126 rdev
= be32_to_cpu(efs_inode
->di_u
.di_dev
.ndev
);
127 if (sysv_major(rdev
) > 0xfff)
130 device
= MKDEV(sysv_major(rdev
), sysv_minor(rdev
));
132 device
= old_decode_dev(rdev
);
134 /* get the number of extents for this object */
135 in
->numextents
= be16_to_cpu(efs_inode
->di_numextents
);
138 /* copy the extents contained within the inode to memory */
139 for(i
= 0; i
< EFS_DIRECTEXTENTS
; i
++) {
140 extent_copy(&(efs_inode
->di_u
.di_extents
[i
]), &(in
->extents
[i
]));
141 if (i
< in
->numextents
&& in
->extents
[i
].cooked
.ex_magic
!= 0) {
142 printk(KERN_WARNING
"EFS: extent %d has bad magic number in inode %lu\n", i
, inode
->i_ino
);
144 goto read_inode_error
;
151 printk(KERN_DEBUG
"EFS: read_inode(): inode %lu, extents %d, mode %o\n",
152 inode
->i_ino
, in
->numextents
, inode
->i_mode
);
155 switch (inode
->i_mode
& S_IFMT
) {
157 inode
->i_op
= &efs_dir_inode_operations
;
158 inode
->i_fop
= &efs_dir_operations
;
161 inode
->i_fop
= &generic_ro_fops
;
162 inode
->i_data
.a_ops
= &efs_aops
;
165 inode
->i_op
= &page_symlink_inode_operations
;
166 inode
->i_data
.a_ops
= &efs_symlink_aops
;
171 init_special_inode(inode
, inode
->i_mode
, device
);
174 printk(KERN_WARNING
"EFS: unsupported inode mode %o\n", inode
->i_mode
);
175 goto read_inode_error
;
179 unlock_new_inode(inode
);
183 printk(KERN_WARNING
"EFS: failed to read inode %lu\n", inode
->i_ino
);
185 return ERR_PTR(-EIO
);
188 static inline efs_block_t
189 efs_extent_check(efs_extent
*ptr
, efs_block_t block
, struct efs_sb_info
*sb
) {
195 * given an extent and a logical block within a file,
196 * can this block be found within this extent ?
198 start
= ptr
->cooked
.ex_bn
;
199 length
= ptr
->cooked
.ex_length
;
200 offset
= ptr
->cooked
.ex_offset
;
202 if ((block
>= offset
) && (block
< offset
+length
)) {
203 return(sb
->fs_start
+ start
+ block
- offset
);
209 efs_block_t
efs_map_block(struct inode
*inode
, efs_block_t block
) {
210 struct efs_sb_info
*sb
= SUPER_INFO(inode
->i_sb
);
211 struct efs_inode_info
*in
= INODE_INFO(inode
);
212 struct buffer_head
*bh
= NULL
;
214 int cur
, last
, first
= 1;
215 int ibase
, ioffset
, dirext
, direxts
, indext
, indexts
;
216 efs_block_t iblock
, result
= 0, lastblock
= 0;
217 efs_extent ext
, *exts
;
219 last
= in
->lastextent
;
221 if (in
->numextents
<= EFS_DIRECTEXTENTS
) {
222 /* first check the last extent we returned */
223 if ((result
= efs_extent_check(&in
->extents
[last
], block
, sb
)))
226 /* if we only have one extent then nothing can be found */
227 if (in
->numextents
== 1) {
228 printk(KERN_ERR
"EFS: map_block() failed to map (1 extent)\n");
232 direxts
= in
->numextents
;
235 * check the stored extents in the inode
236 * start with next extent and check forwards
238 for(dirext
= 1; dirext
< direxts
; dirext
++) {
239 cur
= (last
+ dirext
) % in
->numextents
;
240 if ((result
= efs_extent_check(&in
->extents
[cur
], block
, sb
))) {
241 in
->lastextent
= cur
;
246 printk(KERN_ERR
"EFS: map_block() failed to map block %u (dir)\n", block
);
251 printk(KERN_DEBUG
"EFS: map_block(): indirect search for logical block %u\n", block
);
253 direxts
= in
->extents
[0].cooked
.ex_offset
;
254 indexts
= in
->numextents
;
256 for(indext
= 0; indext
< indexts
; indext
++) {
257 cur
= (last
+ indext
) % indexts
;
260 * work out which direct extent contains `cur'.
262 * also compute ibase: i.e. the number of the first
263 * indirect extent contained within direct extent `cur'.
267 for(dirext
= 0; cur
< ibase
&& dirext
< direxts
; dirext
++) {
268 ibase
+= in
->extents
[dirext
].cooked
.ex_length
*
269 (EFS_BLOCKSIZE
/ sizeof(efs_extent
));
272 if (dirext
== direxts
) {
273 /* should never happen */
274 printk(KERN_ERR
"EFS: couldn't find direct extent for indirect extent %d (block %u)\n", cur
, block
);
279 /* work out block number and offset of this indirect extent */
280 iblock
= sb
->fs_start
+ in
->extents
[dirext
].cooked
.ex_bn
+
282 (EFS_BLOCKSIZE
/ sizeof(efs_extent
));
283 ioffset
= (cur
- ibase
) %
284 (EFS_BLOCKSIZE
/ sizeof(efs_extent
));
286 if (first
|| lastblock
!= iblock
) {
289 bh
= sb_bread(inode
->i_sb
, iblock
);
291 printk(KERN_ERR
"EFS: bread() failed at block %d\n", iblock
);
295 printk(KERN_DEBUG
"EFS: map_block(): read indirect extent block %d\n", iblock
);
301 exts
= (efs_extent
*) bh
->b_data
;
303 extent_copy(&(exts
[ioffset
]), &ext
);
305 if (ext
.cooked
.ex_magic
!= 0) {
306 printk(KERN_ERR
"EFS: extent %d has bad magic number in block %d\n", cur
, iblock
);
311 if ((result
= efs_extent_check(&ext
, block
, sb
))) {
313 in
->lastextent
= cur
;
318 printk(KERN_ERR
"EFS: map_block() failed to map block %u (indir)\n", block
);
322 MODULE_LICENSE("GPL");