blob: cdf0872382afa3e4755d5276fa1588be03d272a1 [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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 .bmap = _efs_bmap
27};
28
29static inline void extent_copy(efs_extent *src, efs_extent *dst) {
30 /*
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.
34 */
35
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);
44 return;
45}
46
David Howells298384cd2008-02-07 00:15:34 -080047struct inode *efs_iget(struct super_block *super, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 int i, inode_index;
50 dev_t device;
51 u32 rdev;
52 struct buffer_head *bh;
David Howells298384cd2008-02-07 00:15:34 -080053 struct efs_sb_info *sb = SUPER_INFO(super);
54 struct efs_inode_info *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 efs_block_t block, offset;
56 struct efs_dinode *efs_inode;
David Howells298384cd2008-02-07 00:15:34 -080057 struct inode *inode;
58
59 inode = iget_locked(super, ino);
Dan Carpenter136eefa2013-08-14 12:49:40 +030060 if (!inode)
David Howells298384cd2008-02-07 00:15:34 -080061 return ERR_PTR(-ENOMEM);
62 if (!(inode->i_state & I_NEW))
63 return inode;
64
65 in = INODE_INFO(inode);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /*
68 ** EFS layout:
69 **
70 ** | cylinder group | cylinder group | cylinder group ..etc
71 ** |inodes|data |inodes|data |inodes|data ..etc
72 **
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.
77 */
78
79 inode_index = inode->i_ino /
80 (EFS_BLOCKSIZE / sizeof(struct efs_dinode));
81
82 block = sb->fs_start + sb->first_block +
83 (sb->group_size * (inode_index / sb->inode_blocks)) +
84 (inode_index % sb->inode_blocks);
85
86 offset = (inode->i_ino %
87 (EFS_BLOCKSIZE / sizeof(struct efs_dinode))) *
88 sizeof(struct efs_dinode);
89
90 bh = sb_bread(inode->i_sb, block);
91 if (!bh) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -070092 pr_warn("%s() failed at block %d\n", __func__, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 goto read_inode_error;
94 }
95
96 efs_inode = (struct efs_dinode *) (bh->b_data + offset);
97
98 inode->i_mode = be16_to_cpu(efs_inode->di_mode);
Miklos Szeredibfe86842011-10-28 14:13:29 +020099 set_nlink(inode, be16_to_cpu(efs_inode->di_nlink));
Eric W. Biederman5d4ea4d2012-02-10 11:10:33 -0800100 i_uid_write(inode, (uid_t)be16_to_cpu(efs_inode->di_uid));
101 i_gid_write(inode, (gid_t)be16_to_cpu(efs_inode->di_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 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;
107
108 /* this is the number of blocks in the file */
109 if (inode->i_size == 0) {
110 inode->i_blocks = 0;
111 } else {
112 inode->i_blocks = ((inode->i_size - 1) >> EFS_BLOCKSIZE_BITS) + 1;
113 }
114
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)
119 device = 0;
120 else
121 device = MKDEV(sysv_major(rdev), sysv_minor(rdev));
122 } else
123 device = old_decode_dev(rdev);
124
125 /* get the number of extents for this object */
126 in->numextents = be16_to_cpu(efs_inode->di_numextents);
127 in->lastextent = 0;
128
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) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -0700133 pr_warn("extent %d has bad magic number in inode %lu\n",
134 i, inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 brelse(bh);
136 goto read_inode_error;
137 }
138 }
139
140 brelse(bh);
Fabian Frederickd1826f22014-06-04 16:12:13 -0700141 pr_debug("efs_iget(): inode %lu, extents %d, mode %o\n",
142 inode->i_ino, in->numextents, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 switch (inode->i_mode & S_IFMT) {
144 case S_IFDIR:
145 inode->i_op = &efs_dir_inode_operations;
146 inode->i_fop = &efs_dir_operations;
147 break;
148 case S_IFREG:
149 inode->i_fop = &generic_ro_fops;
150 inode->i_data.a_ops = &efs_aops;
151 break;
152 case S_IFLNK:
153 inode->i_op = &page_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500154 inode_nohighmem(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 inode->i_data.a_ops = &efs_symlink_aops;
156 break;
157 case S_IFCHR:
158 case S_IFBLK:
159 case S_IFIFO:
160 init_special_inode(inode, inode->i_mode, device);
161 break;
162 default:
Fabian Frederickf403d1d2014-06-04 16:12:12 -0700163 pr_warn("unsupported inode mode %o\n", inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto read_inode_error;
165 break;
166 }
167
David Howells298384cd2008-02-07 00:15:34 -0800168 unlock_new_inode(inode);
169 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171read_inode_error:
Fabian Frederickf403d1d2014-06-04 16:12:12 -0700172 pr_warn("failed to read inode %lu\n", inode->i_ino);
David Howells298384cd2008-02-07 00:15:34 -0800173 iget_failed(inode);
174 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177static inline efs_block_t
178efs_extent_check(efs_extent *ptr, efs_block_t block, struct efs_sb_info *sb) {
179 efs_block_t start;
180 efs_block_t length;
181 efs_block_t offset;
182
183 /*
184 * given an extent and a logical block within a file,
185 * can this block be found within this extent ?
186 */
187 start = ptr->cooked.ex_bn;
188 length = ptr->cooked.ex_length;
189 offset = ptr->cooked.ex_offset;
190
191 if ((block >= offset) && (block < offset+length)) {
192 return(sb->fs_start + start + block - offset);
193 } else {
194 return 0;
195 }
196}
197
198efs_block_t efs_map_block(struct inode *inode, efs_block_t block) {
199 struct efs_sb_info *sb = SUPER_INFO(inode->i_sb);
200 struct efs_inode_info *in = INODE_INFO(inode);
201 struct buffer_head *bh = NULL;
202
203 int cur, last, first = 1;
204 int ibase, ioffset, dirext, direxts, indext, indexts;
205 efs_block_t iblock, result = 0, lastblock = 0;
206 efs_extent ext, *exts;
207
208 last = in->lastextent;
209
210 if (in->numextents <= EFS_DIRECTEXTENTS) {
211 /* first check the last extent we returned */
212 if ((result = efs_extent_check(&in->extents[last], block, sb)))
213 return result;
214
215 /* if we only have one extent then nothing can be found */
216 if (in->numextents == 1) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -0700217 pr_err("%s() failed to map (1 extent)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219 }
220
221 direxts = in->numextents;
222
223 /*
224 * check the stored extents in the inode
225 * start with next extent and check forwards
226 */
227 for(dirext = 1; dirext < direxts; dirext++) {
228 cur = (last + dirext) % in->numextents;
229 if ((result = efs_extent_check(&in->extents[cur], block, sb))) {
230 in->lastextent = cur;
231 return result;
232 }
233 }
234
Fabian Frederickf403d1d2014-06-04 16:12:12 -0700235 pr_err("%s() failed to map block %u (dir)\n", __func__, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return 0;
237 }
238
Fabian Frederickd1826f22014-06-04 16:12:13 -0700239 pr_debug("%s(): indirect search for logical block %u\n",
240 __func__, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 direxts = in->extents[0].cooked.ex_offset;
242 indexts = in->numextents;
243
244 for(indext = 0; indext < indexts; indext++) {
245 cur = (last + indext) % indexts;
246
247 /*
248 * work out which direct extent contains `cur'.
249 *
250 * also compute ibase: i.e. the number of the first
251 * indirect extent contained within direct extent `cur'.
252 *
253 */
254 ibase = 0;
255 for(dirext = 0; cur < ibase && dirext < direxts; dirext++) {
256 ibase += in->extents[dirext].cooked.ex_length *
257 (EFS_BLOCKSIZE / sizeof(efs_extent));
258 }
259
260 if (dirext == direxts) {
261 /* should never happen */
Fabian Frederickf403d1d2014-06-04 16:12:12 -0700262 pr_err("couldn't find direct extent for indirect extent %d (block %u)\n",
263 cur, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (bh) brelse(bh);
265 return 0;
266 }
267
268 /* work out block number and offset of this indirect extent */
269 iblock = sb->fs_start + in->extents[dirext].cooked.ex_bn +
270 (cur - ibase) /
271 (EFS_BLOCKSIZE / sizeof(efs_extent));
272 ioffset = (cur - ibase) %
273 (EFS_BLOCKSIZE / sizeof(efs_extent));
274
275 if (first || lastblock != iblock) {
276 if (bh) brelse(bh);
277
278 bh = sb_bread(inode->i_sb, iblock);
279 if (!bh) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -0700280 pr_err("%s() failed at block %d\n",
281 __func__, iblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
283 }
Fabian Frederickd1826f22014-06-04 16:12:13 -0700284 pr_debug("%s(): read indirect extent block %d\n",
285 __func__, iblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 first = 0;
287 lastblock = iblock;
288 }
289
290 exts = (efs_extent *) bh->b_data;
291
292 extent_copy(&(exts[ioffset]), &ext);
293
294 if (ext.cooked.ex_magic != 0) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -0700295 pr_err("extent %d has bad magic number in block %d\n",
296 cur, iblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (bh) brelse(bh);
298 return 0;
299 }
300
301 if ((result = efs_extent_check(&ext, block, sb))) {
302 if (bh) brelse(bh);
303 in->lastextent = cur;
304 return result;
305 }
306 }
307 if (bh) brelse(bh);
Fabian Frederickf403d1d2014-06-04 16:12:12 -0700308 pr_err("%s() failed to map block %u (indir)\n", __func__, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return 0;
310}
311
312MODULE_LICENSE("GPL");