blob: a8e7797b947795f2ef70a05c82a204fd493ba472 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c
3 *
4 * Copyright (c) 1999 Al Smith
5 *
6 * Portions derived from work (c) 1995,1996 Christian Vogelgsang,
7 * and from work (c) 1998 Mike Shaver.
8 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/buffer_head.h>
11#include <linux/module.h>
12#include <linux/fs.h>
Christoph Hellwig45254b42008-02-23 15:23:51 -080013#include "efs.h"
14#include <linux/efs_fs_sb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16static int efs_readpage(struct file *file, struct page *page)
17{
18 return block_read_full_page(page,efs_get_block);
19}
20static sector_t _efs_bmap(struct address_space *mapping, sector_t block)
21{
22 return generic_block_bmap(mapping,block,efs_get_block);
23}
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070024static const struct address_space_operations efs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 .readpage = efs_readpage,
26 .sync_page = block_sync_page,
27 .bmap = _efs_bmap
28};
29
30static inline void extent_copy(efs_extent *src, efs_extent *dst) {
31 /*
32 * this is slightly evil. it doesn't just copy
33 * efs_extent from src to dst, it also mangles
34 * the bits so that dst ends up in cpu byte-order.
35 */
36
37 dst->cooked.ex_magic = (unsigned int) src->raw[0];
38 dst->cooked.ex_bn = ((unsigned int) src->raw[1] << 16) |
39 ((unsigned int) src->raw[2] << 8) |
40 ((unsigned int) src->raw[3] << 0);
41 dst->cooked.ex_length = (unsigned int) src->raw[4];
42 dst->cooked.ex_offset = ((unsigned int) src->raw[5] << 16) |
43 ((unsigned int) src->raw[6] << 8) |
44 ((unsigned int) src->raw[7] << 0);
45 return;
46}
47
David Howells298384cd2008-02-07 00:15:34 -080048struct inode *efs_iget(struct super_block *super, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 int i, inode_index;
51 dev_t device;
52 u32 rdev;
53 struct buffer_head *bh;
David Howells298384cd2008-02-07 00:15:34 -080054 struct efs_sb_info *sb = SUPER_INFO(super);
55 struct efs_inode_info *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 efs_block_t block, offset;
57 struct efs_dinode *efs_inode;
David Howells298384cd2008-02-07 00:15:34 -080058 struct inode *inode;
59
60 inode = iget_locked(super, ino);
61 if (IS_ERR(inode))
62 return ERR_PTR(-ENOMEM);
63 if (!(inode->i_state & I_NEW))
64 return inode;
65
66 in = INODE_INFO(inode);
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /*
69 ** EFS layout:
70 **
71 ** | cylinder group | cylinder group | cylinder group ..etc
72 ** |inodes|data |inodes|data |inodes|data ..etc
73 **
74 ** work out the inode block index, (considering initially that the
75 ** inodes are stored as consecutive blocks). then work out the block
76 ** number of that inode given the above layout, and finally the
77 ** offset of the inode within that block.
78 */
79
80 inode_index = inode->i_ino /
81 (EFS_BLOCKSIZE / sizeof(struct efs_dinode));
82
83 block = sb->fs_start + sb->first_block +
84 (sb->group_size * (inode_index / sb->inode_blocks)) +
85 (inode_index % sb->inode_blocks);
86
87 offset = (inode->i_ino %
88 (EFS_BLOCKSIZE / sizeof(struct efs_dinode))) *
89 sizeof(struct efs_dinode);
90
91 bh = sb_bread(inode->i_sb, block);
92 if (!bh) {
93 printk(KERN_WARNING "EFS: bread() failed at block %d\n", block);
94 goto read_inode_error;
95 }
96
97 efs_inode = (struct efs_dinode *) (bh->b_data + offset);
98
99 inode->i_mode = be16_to_cpu(efs_inode->di_mode);
100 inode->i_nlink = be16_to_cpu(efs_inode->di_nlink);
101 inode->i_uid = (uid_t)be16_to_cpu(efs_inode->di_uid);
102 inode->i_gid = (gid_t)be16_to_cpu(efs_inode->di_gid);
103 inode->i_size = be32_to_cpu(efs_inode->di_size);
104 inode->i_atime.tv_sec = be32_to_cpu(efs_inode->di_atime);
105 inode->i_mtime.tv_sec = be32_to_cpu(efs_inode->di_mtime);
106 inode->i_ctime.tv_sec = be32_to_cpu(efs_inode->di_ctime);
107 inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
108
109 /* this is the number of blocks in the file */
110 if (inode->i_size == 0) {
111 inode->i_blocks = 0;
112 } else {
113 inode->i_blocks = ((inode->i_size - 1) >> EFS_BLOCKSIZE_BITS) + 1;
114 }
115
116 rdev = be16_to_cpu(efs_inode->di_u.di_dev.odev);
117 if (rdev == 0xffff) {
118 rdev = be32_to_cpu(efs_inode->di_u.di_dev.ndev);
119 if (sysv_major(rdev) > 0xfff)
120 device = 0;
121 else
122 device = MKDEV(sysv_major(rdev), sysv_minor(rdev));
123 } else
124 device = old_decode_dev(rdev);
125
126 /* get the number of extents for this object */
127 in->numextents = be16_to_cpu(efs_inode->di_numextents);
128 in->lastextent = 0;
129
130 /* copy the extents contained within the inode to memory */
131 for(i = 0; i < EFS_DIRECTEXTENTS; i++) {
132 extent_copy(&(efs_inode->di_u.di_extents[i]), &(in->extents[i]));
133 if (i < in->numextents && in->extents[i].cooked.ex_magic != 0) {
134 printk(KERN_WARNING "EFS: extent %d has bad magic number in inode %lu\n", i, inode->i_ino);
135 brelse(bh);
136 goto read_inode_error;
137 }
138 }
139
140 brelse(bh);
141
142#ifdef DEBUG
Robert P. J. Day865965a2008-04-02 13:04:49 -0700143 printk(KERN_DEBUG "EFS: efs_iget(): inode %lu, extents %d, mode %o\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 inode->i_ino, in->numextents, inode->i_mode);
145#endif
146
147 switch (inode->i_mode & S_IFMT) {
148 case S_IFDIR:
149 inode->i_op = &efs_dir_inode_operations;
150 inode->i_fop = &efs_dir_operations;
151 break;
152 case S_IFREG:
153 inode->i_fop = &generic_ro_fops;
154 inode->i_data.a_ops = &efs_aops;
155 break;
156 case S_IFLNK:
157 inode->i_op = &page_symlink_inode_operations;
158 inode->i_data.a_ops = &efs_symlink_aops;
159 break;
160 case S_IFCHR:
161 case S_IFBLK:
162 case S_IFIFO:
163 init_special_inode(inode, inode->i_mode, device);
164 break;
165 default:
166 printk(KERN_WARNING "EFS: unsupported inode mode %o\n", inode->i_mode);
167 goto read_inode_error;
168 break;
169 }
170
David Howells298384cd2008-02-07 00:15:34 -0800171 unlock_new_inode(inode);
172 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174read_inode_error:
175 printk(KERN_WARNING "EFS: failed to read inode %lu\n", inode->i_ino);
David Howells298384cd2008-02-07 00:15:34 -0800176 iget_failed(inode);
177 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static inline efs_block_t
181efs_extent_check(efs_extent *ptr, efs_block_t block, struct efs_sb_info *sb) {
182 efs_block_t start;
183 efs_block_t length;
184 efs_block_t offset;
185
186 /*
187 * given an extent and a logical block within a file,
188 * can this block be found within this extent ?
189 */
190 start = ptr->cooked.ex_bn;
191 length = ptr->cooked.ex_length;
192 offset = ptr->cooked.ex_offset;
193
194 if ((block >= offset) && (block < offset+length)) {
195 return(sb->fs_start + start + block - offset);
196 } else {
197 return 0;
198 }
199}
200
201efs_block_t efs_map_block(struct inode *inode, efs_block_t block) {
202 struct efs_sb_info *sb = SUPER_INFO(inode->i_sb);
203 struct efs_inode_info *in = INODE_INFO(inode);
204 struct buffer_head *bh = NULL;
205
206 int cur, last, first = 1;
207 int ibase, ioffset, dirext, direxts, indext, indexts;
208 efs_block_t iblock, result = 0, lastblock = 0;
209 efs_extent ext, *exts;
210
211 last = in->lastextent;
212
213 if (in->numextents <= EFS_DIRECTEXTENTS) {
214 /* first check the last extent we returned */
215 if ((result = efs_extent_check(&in->extents[last], block, sb)))
216 return result;
217
218 /* if we only have one extent then nothing can be found */
219 if (in->numextents == 1) {
220 printk(KERN_ERR "EFS: map_block() failed to map (1 extent)\n");
221 return 0;
222 }
223
224 direxts = in->numextents;
225
226 /*
227 * check the stored extents in the inode
228 * start with next extent and check forwards
229 */
230 for(dirext = 1; dirext < direxts; dirext++) {
231 cur = (last + dirext) % in->numextents;
232 if ((result = efs_extent_check(&in->extents[cur], block, sb))) {
233 in->lastextent = cur;
234 return result;
235 }
236 }
237
238 printk(KERN_ERR "EFS: map_block() failed to map block %u (dir)\n", block);
239 return 0;
240 }
241
242#ifdef DEBUG
243 printk(KERN_DEBUG "EFS: map_block(): indirect search for logical block %u\n", block);
244#endif
245 direxts = in->extents[0].cooked.ex_offset;
246 indexts = in->numextents;
247
248 for(indext = 0; indext < indexts; indext++) {
249 cur = (last + indext) % indexts;
250
251 /*
252 * work out which direct extent contains `cur'.
253 *
254 * also compute ibase: i.e. the number of the first
255 * indirect extent contained within direct extent `cur'.
256 *
257 */
258 ibase = 0;
259 for(dirext = 0; cur < ibase && dirext < direxts; dirext++) {
260 ibase += in->extents[dirext].cooked.ex_length *
261 (EFS_BLOCKSIZE / sizeof(efs_extent));
262 }
263
264 if (dirext == direxts) {
265 /* should never happen */
266 printk(KERN_ERR "EFS: couldn't find direct extent for indirect extent %d (block %u)\n", cur, block);
267 if (bh) brelse(bh);
268 return 0;
269 }
270
271 /* work out block number and offset of this indirect extent */
272 iblock = sb->fs_start + in->extents[dirext].cooked.ex_bn +
273 (cur - ibase) /
274 (EFS_BLOCKSIZE / sizeof(efs_extent));
275 ioffset = (cur - ibase) %
276 (EFS_BLOCKSIZE / sizeof(efs_extent));
277
278 if (first || lastblock != iblock) {
279 if (bh) brelse(bh);
280
281 bh = sb_bread(inode->i_sb, iblock);
282 if (!bh) {
283 printk(KERN_ERR "EFS: bread() failed at block %d\n", iblock);
284 return 0;
285 }
286#ifdef DEBUG
287 printk(KERN_DEBUG "EFS: map_block(): read indirect extent block %d\n", iblock);
288#endif
289 first = 0;
290 lastblock = iblock;
291 }
292
293 exts = (efs_extent *) bh->b_data;
294
295 extent_copy(&(exts[ioffset]), &ext);
296
297 if (ext.cooked.ex_magic != 0) {
298 printk(KERN_ERR "EFS: extent %d has bad magic number in block %d\n", cur, iblock);
299 if (bh) brelse(bh);
300 return 0;
301 }
302
303 if ((result = efs_extent_check(&ext, block, sb))) {
304 if (bh) brelse(bh);
305 in->lastextent = cur;
306 return result;
307 }
308 }
309 if (bh) brelse(bh);
310 printk(KERN_ERR "EFS: map_block() failed to map block %u (indir)\n", block);
311 return 0;
312}
313
314MODULE_LICENSE("GPL");