Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / efs / inode.c
blob307ac018c7608b970bed52e1c8dd09e0a5c567e6
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 <<<<<<< HEAD:fs/efs/inode.c
11 #include <linux/efs_fs.h>
12 #include <linux/efs_fs_sb.h>
13 =======
14 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/efs/inode.c
15 #include <linux/buffer_head.h>
16 #include <linux/module.h>
17 #include <linux/fs.h>
18 <<<<<<< HEAD:fs/efs/inode.c
19 =======
20 #include "efs.h"
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,
35 .bmap = _efs_bmap
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);
53 return;
56 struct inode *efs_iget(struct super_block *super, unsigned long ino)
58 int i, inode_index;
59 dev_t device;
60 u32 rdev;
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;
66 struct inode *inode;
68 inode = iget_locked(super, ino);
69 if (IS_ERR(inode))
70 return ERR_PTR(-ENOMEM);
71 if (!(inode->i_state & I_NEW))
72 return inode;
74 in = INODE_INFO(inode);
77 ** EFS layout:
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);
100 if (!bh) {
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) {
119 inode->i_blocks = 0;
120 } else {
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)
128 device = 0;
129 else
130 device = MKDEV(sysv_major(rdev), sysv_minor(rdev));
131 } else
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);
136 in->lastextent = 0;
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);
143 brelse(bh);
144 goto read_inode_error;
148 brelse(bh);
150 #ifdef DEBUG
151 printk(KERN_DEBUG "EFS: read_inode(): inode %lu, extents %d, mode %o\n",
152 inode->i_ino, in->numextents, inode->i_mode);
153 #endif
155 switch (inode->i_mode & S_IFMT) {
156 case S_IFDIR:
157 inode->i_op = &efs_dir_inode_operations;
158 inode->i_fop = &efs_dir_operations;
159 break;
160 case S_IFREG:
161 inode->i_fop = &generic_ro_fops;
162 inode->i_data.a_ops = &efs_aops;
163 break;
164 case S_IFLNK:
165 inode->i_op = &page_symlink_inode_operations;
166 inode->i_data.a_ops = &efs_symlink_aops;
167 break;
168 case S_IFCHR:
169 case S_IFBLK:
170 case S_IFIFO:
171 init_special_inode(inode, inode->i_mode, device);
172 break;
173 default:
174 printk(KERN_WARNING "EFS: unsupported inode mode %o\n", inode->i_mode);
175 goto read_inode_error;
176 break;
179 unlock_new_inode(inode);
180 return inode;
182 read_inode_error:
183 printk(KERN_WARNING "EFS: failed to read inode %lu\n", inode->i_ino);
184 iget_failed(inode);
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) {
190 efs_block_t start;
191 efs_block_t length;
192 efs_block_t offset;
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);
204 } else {
205 return 0;
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)))
224 return result;
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");
229 return 0;
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;
242 return result;
246 printk(KERN_ERR "EFS: map_block() failed to map block %u (dir)\n", block);
247 return 0;
250 #ifdef DEBUG
251 printk(KERN_DEBUG "EFS: map_block(): indirect search for logical block %u\n", block);
252 #endif
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'.
266 ibase = 0;
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);
275 if (bh) brelse(bh);
276 return 0;
279 /* work out block number and offset of this indirect extent */
280 iblock = sb->fs_start + in->extents[dirext].cooked.ex_bn +
281 (cur - ibase) /
282 (EFS_BLOCKSIZE / sizeof(efs_extent));
283 ioffset = (cur - ibase) %
284 (EFS_BLOCKSIZE / sizeof(efs_extent));
286 if (first || lastblock != iblock) {
287 if (bh) brelse(bh);
289 bh = sb_bread(inode->i_sb, iblock);
290 if (!bh) {
291 printk(KERN_ERR "EFS: bread() failed at block %d\n", iblock);
292 return 0;
294 #ifdef DEBUG
295 printk(KERN_DEBUG "EFS: map_block(): read indirect extent block %d\n", iblock);
296 #endif
297 first = 0;
298 lastblock = 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);
307 if (bh) brelse(bh);
308 return 0;
311 if ((result = efs_extent_check(&ext, block, sb))) {
312 if (bh) brelse(bh);
313 in->lastextent = cur;
314 return result;
317 if (bh) brelse(bh);
318 printk(KERN_ERR "EFS: map_block() failed to map block %u (indir)\n", block);
319 return 0;
322 MODULE_LICENSE("GPL");