1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * inode.c
4 *
5 * Copyright (c) 1999 Al Smith
6 *
7 * Portions derived from work (c) 1995,1996 Christian Vogelgsang,
8 * and from work (c) 1998 Mike Shaver.
9 */
10
11 #include <linux/buffer_head.h>
12 #include <linux/module.h>
13 #include <linux/fs.h>
14 #include "efs.h"
15 #include <linux/efs_fs_sb.h>
16
efs_readpage(struct file * file,struct page * page)17 static int efs_readpage(struct file *file, struct page *page)
18 {
19 return block_read_full_page(page,efs_get_block);
20 }
_efs_bmap(struct address_space * mapping,sector_t block)21 static sector_t _efs_bmap(struct address_space *mapping, sector_t block)
22 {
23 return generic_block_bmap(mapping,block,efs_get_block);
24 }
25 static const struct address_space_operations efs_aops = {
26 .readpage = efs_readpage,
27 .bmap = _efs_bmap
28 };
29
extent_copy(efs_extent * src,efs_extent * dst)30 static 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
efs_iget(struct super_block * super,unsigned long ino)48 struct inode *efs_iget(struct super_block *super, unsigned long ino)
49 {
50 int i, inode_index;
51 dev_t device;
52 u32 rdev;
53 struct buffer_head *bh;
54 struct efs_sb_info *sb = SUPER_INFO(super);
55 struct efs_inode_info *in;
56 efs_block_t block, offset;
57 struct efs_dinode *efs_inode;
58 struct inode *inode;
59
60 inode = iget_locked(super, ino);
61 if (!inode)
62 return ERR_PTR(-ENOMEM);
63 if (!(inode->i_state & I_NEW))
64 return inode;
65
66 in = INODE_INFO(inode);
67
68 /*
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 pr_warn("%s() failed at block %d\n", __func__, 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 set_nlink(inode, be16_to_cpu(efs_inode->di_nlink));
101 i_uid_write(inode, (uid_t)be16_to_cpu(efs_inode->di_uid));
102 i_gid_write(inode, (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 pr_warn("extent %d has bad magic number in inode %lu\n",
135 i, inode->i_ino);
136 brelse(bh);
137 goto read_inode_error;
138 }
139 }
140
141 brelse(bh);
142 pr_debug("efs_iget(): inode %lu, extents %d, mode %o\n",
143 inode->i_ino, in->numextents, inode->i_mode);
144 switch (inode->i_mode & S_IFMT) {
145 case S_IFDIR:
146 inode->i_op = &efs_dir_inode_operations;
147 inode->i_fop = &efs_dir_operations;
148 break;
149 case S_IFREG:
150 inode->i_fop = &generic_ro_fops;
151 inode->i_data.a_ops = &efs_aops;
152 break;
153 case S_IFLNK:
154 inode->i_op = &page_symlink_inode_operations;
155 inode_nohighmem(inode);
156 inode->i_data.a_ops = &efs_symlink_aops;
157 break;
158 case S_IFCHR:
159 case S_IFBLK:
160 case S_IFIFO:
161 init_special_inode(inode, inode->i_mode, device);
162 break;
163 default:
164 pr_warn("unsupported inode mode %o\n", inode->i_mode);
165 goto read_inode_error;
166 break;
167 }
168
169 unlock_new_inode(inode);
170 return inode;
171
172 read_inode_error:
173 pr_warn("failed to read inode %lu\n", inode->i_ino);
174 iget_failed(inode);
175 return ERR_PTR(-EIO);
176 }
177
178 static inline efs_block_t
efs_extent_check(efs_extent * ptr,efs_block_t block,struct efs_sb_info * sb)179 efs_extent_check(efs_extent *ptr, efs_block_t block, struct efs_sb_info *sb) {
180 efs_block_t start;
181 efs_block_t length;
182 efs_block_t offset;
183
184 /*
185 * given an extent and a logical block within a file,
186 * can this block be found within this extent ?
187 */
188 start = ptr->cooked.ex_bn;
189 length = ptr->cooked.ex_length;
190 offset = ptr->cooked.ex_offset;
191
192 if ((block >= offset) && (block < offset+length)) {
193 return(sb->fs_start + start + block - offset);
194 } else {
195 return 0;
196 }
197 }
198
efs_map_block(struct inode * inode,efs_block_t block)199 efs_block_t efs_map_block(struct inode *inode, efs_block_t block) {
200 struct efs_sb_info *sb = SUPER_INFO(inode->i_sb);
201 struct efs_inode_info *in = INODE_INFO(inode);
202 struct buffer_head *bh = NULL;
203
204 int cur, last, first = 1;
205 int ibase, ioffset, dirext, direxts, indext, indexts;
206 efs_block_t iblock, result = 0, lastblock = 0;
207 efs_extent ext, *exts;
208
209 last = in->lastextent;
210
211 if (in->numextents <= EFS_DIRECTEXTENTS) {
212 /* first check the last extent we returned */
213 if ((result = efs_extent_check(&in->extents[last], block, sb)))
214 return result;
215
216 /* if we only have one extent then nothing can be found */
217 if (in->numextents == 1) {
218 pr_err("%s() failed to map (1 extent)\n", __func__);
219 return 0;
220 }
221
222 direxts = in->numextents;
223
224 /*
225 * check the stored extents in the inode
226 * start with next extent and check forwards
227 */
228 for(dirext = 1; dirext < direxts; dirext++) {
229 cur = (last + dirext) % in->numextents;
230 if ((result = efs_extent_check(&in->extents[cur], block, sb))) {
231 in->lastextent = cur;
232 return result;
233 }
234 }
235
236 pr_err("%s() failed to map block %u (dir)\n", __func__, block);
237 return 0;
238 }
239
240 pr_debug("%s(): indirect search for logical block %u\n",
241 __func__, block);
242 direxts = in->extents[0].cooked.ex_offset;
243 indexts = in->numextents;
244
245 for(indext = 0; indext < indexts; indext++) {
246 cur = (last + indext) % indexts;
247
248 /*
249 * work out which direct extent contains `cur'.
250 *
251 * also compute ibase: i.e. the number of the first
252 * indirect extent contained within direct extent `cur'.
253 *
254 */
255 ibase = 0;
256 for(dirext = 0; cur < ibase && dirext < direxts; dirext++) {
257 ibase += in->extents[dirext].cooked.ex_length *
258 (EFS_BLOCKSIZE / sizeof(efs_extent));
259 }
260
261 if (dirext == direxts) {
262 /* should never happen */
263 pr_err("couldn't find direct extent for indirect extent %d (block %u)\n",
264 cur, block);
265 if (bh) brelse(bh);
266 return 0;
267 }
268
269 /* work out block number and offset of this indirect extent */
270 iblock = sb->fs_start + in->extents[dirext].cooked.ex_bn +
271 (cur - ibase) /
272 (EFS_BLOCKSIZE / sizeof(efs_extent));
273 ioffset = (cur - ibase) %
274 (EFS_BLOCKSIZE / sizeof(efs_extent));
275
276 if (first || lastblock != iblock) {
277 if (bh) brelse(bh);
278
279 bh = sb_bread(inode->i_sb, iblock);
280 if (!bh) {
281 pr_err("%s() failed at block %d\n",
282 __func__, iblock);
283 return 0;
284 }
285 pr_debug("%s(): read indirect extent block %d\n",
286 __func__, iblock);
287 first = 0;
288 lastblock = iblock;
289 }
290
291 exts = (efs_extent *) bh->b_data;
292
293 extent_copy(&(exts[ioffset]), &ext);
294
295 if (ext.cooked.ex_magic != 0) {
296 pr_err("extent %d has bad magic number in block %d\n",
297 cur, iblock);
298 if (bh) brelse(bh);
299 return 0;
300 }
301
302 if ((result = efs_extent_check(&ext, block, sb))) {
303 if (bh) brelse(bh);
304 in->lastextent = cur;
305 return result;
306 }
307 }
308 if (bh) brelse(bh);
309 pr_err("%s() failed to map block %u (indir)\n", __func__, block);
310 return 0;
311 }
312
313 MODULE_LICENSE("GPL");
314