Import 2.3.26pre2
[davej-history.git] / fs / efs / inode.c
blobb6a2ad3fa70ab5c7bef2d6112e953751876f93bd
1 /*
2 * inode.c
4 * Copyright (c) 1999 Al Smith
6 * Portions derived from work (c) 1995,1996 Christian Vogelgsang,
7 * and from work (c) 1998 Mike Shaver.
8 */
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);
28 return;
31 void efs_read_inode(struct inode *inode) {
32 int i, inode_index;
33 dev_t device;
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;
41 ** EFS layout:
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);
64 if (!bh) {
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) {
82 inode->i_blocks = 0;
83 } else {
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
91 * linux 2.3.
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);
104 in->lastextent = 0;
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);
111 brelse(bh);
112 goto read_inode_error;
116 brelse(bh);
118 #ifdef DEBUG
119 printk(KERN_DEBUG "EFS: read_inode(): inode %lu, extents %d, mode %o\n",
120 inode->i_ino, in->numextents, inode->i_mode);
121 #endif
123 switch (inode->i_mode & S_IFMT) {
124 case S_IFDIR:
125 inode->i_op = &efs_dir_inode_operations;
126 break;
127 case S_IFREG:
128 inode->i_op = &efs_file_inode_operations;
129 break;
130 case S_IFLNK:
131 inode->i_op = &efs_symlink_inode_operations;
132 break;
133 case S_IFCHR:
134 inode->i_rdev = device;
135 inode->i_op = &chrdev_inode_operations;
136 break;
137 case S_IFBLK:
138 inode->i_rdev = device;
139 inode->i_op = &blkdev_inode_operations;
140 break;
141 case S_IFIFO:
142 init_fifo(inode);
143 break;
144 default:
145 printk(KERN_WARNING "EFS: unsupported inode mode %o\n", inode->i_mode);
146 goto read_inode_error;
147 break;
150 return;
152 read_inode_error:
153 printk(KERN_WARNING "EFS: failed to read inode %lu\n", inode->i_ino);
154 inode->i_mode = S_IFREG;
155 inode->i_atime = 0;
156 inode->i_ctime = 0;
157 inode->i_mtime = 0;
158 inode->i_nlink = 1;
159 inode->i_size = 0;
160 inode->i_blocks = 0;
161 inode->i_uid = 0;
162 inode->i_gid = 0;
163 inode->i_op = NULL;
165 return;
168 static inline efs_block_t
169 efs_extent_check(efs_extent *ptr, efs_block_t block, struct efs_sb_info *sb) {
170 efs_block_t start;
171 efs_block_t length;
172 efs_block_t offset;
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);
184 } else {
185 return 0;
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)))
204 return result;
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");
209 return 0;
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;
222 return result;
226 printk(KERN_ERR "EFS: map_block() failed to map block %u (dir)\n", block);
227 return 0;
230 #ifdef DEBUG
231 printk(KERN_DEBUG "EFS: map_block(): indirect search for logical block %u\n", block);
232 #endif
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'.
246 ibase = 0;
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);
255 if (bh) brelse(bh);
256 return 0;
259 /* work out block number and offset of this indirect extent */
260 iblock = sb->fs_start + in->extents[dirext].cooked.ex_bn +
261 (cur - ibase) /
262 (EFS_BLOCKSIZE / sizeof(efs_extent));
263 ioffset = (cur - ibase) %
264 (EFS_BLOCKSIZE / sizeof(efs_extent));
266 if (first || lastblock != iblock) {
267 if (bh) brelse(bh);
269 bh = bread(inode->i_dev, iblock, EFS_BLOCKSIZE);
270 if (!bh) {
271 printk(KERN_ERR "EFS: bread() failed at block %d\n", iblock);
272 return 0;
274 #ifdef DEBUG
275 printk(KERN_DEBUG "EFS: map_block(): read indirect extent block %d\n", iblock);
276 #endif
277 first = 0;
278 lastblock = 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);
287 if (bh) brelse(bh);
288 return 0;
291 if ((result = efs_extent_check(&ext, block, sb))) {
292 if (bh) brelse(bh);
293 in->lastextent = cur;
294 return result;
297 if (bh) brelse(bh);
298 printk(KERN_ERR "EFS: map_block() failed to map block %u (indir)\n", block);
299 return 0;