xref: /openbmc/linux/fs/efs/inode.c (revision a68d1f51)
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_read_folio(struct file * file,struct folio * folio)17 static int efs_read_folio(struct file *file, struct folio *folio)
18 {
19 	return block_read_full_folio(folio, efs_get_block);
20 }
21 
_efs_bmap(struct address_space * mapping,sector_t block)22 static sector_t _efs_bmap(struct address_space *mapping, sector_t block)
23 {
24 	return generic_block_bmap(mapping,block,efs_get_block);
25 }
26 
27 static const struct address_space_operations efs_aops = {
28 	.read_folio = efs_read_folio,
29 	.bmap = _efs_bmap
30 };
31 
extent_copy(efs_extent * src,efs_extent * dst)32 static inline void extent_copy(efs_extent *src, efs_extent *dst) {
33 	/*
34 	 * this is slightly evil. it doesn't just copy
35 	 * efs_extent from src to dst, it also mangles
36 	 * the bits so that dst ends up in cpu byte-order.
37 	 */
38 
39 	dst->cooked.ex_magic  =  (unsigned int) src->raw[0];
40 	dst->cooked.ex_bn     = ((unsigned int) src->raw[1] << 16) |
41 				((unsigned int) src->raw[2] <<  8) |
42 				((unsigned int) src->raw[3] <<  0);
43 	dst->cooked.ex_length =  (unsigned int) src->raw[4];
44 	dst->cooked.ex_offset = ((unsigned int) src->raw[5] << 16) |
45 				((unsigned int) src->raw[6] <<  8) |
46 				((unsigned int) src->raw[7] <<  0);
47 	return;
48 }
49 
efs_iget(struct super_block * super,unsigned long ino)50 struct inode *efs_iget(struct super_block *super, unsigned long ino)
51 {
52 	int i, inode_index;
53 	dev_t device;
54 	u32 rdev;
55 	struct buffer_head *bh;
56 	struct efs_sb_info    *sb = SUPER_INFO(super);
57 	struct efs_inode_info *in;
58 	efs_block_t block, offset;
59 	struct efs_dinode *efs_inode;
60 	struct inode *inode;
61 
62 	inode = iget_locked(super, ino);
63 	if (!inode)
64 		return ERR_PTR(-ENOMEM);
65 	if (!(inode->i_state & I_NEW))
66 		return inode;
67 
68 	in = INODE_INFO(inode);
69 
70 	/*
71 	** EFS layout:
72 	**
73 	** |   cylinder group    |   cylinder group    |   cylinder group ..etc
74 	** |inodes|data          |inodes|data          |inodes|data       ..etc
75 	**
76 	** work out the inode block index, (considering initially that the
77 	** inodes are stored as consecutive blocks). then work out the block
78 	** number of that inode given the above layout, and finally the
79 	** offset of the inode within that block.
80 	*/
81 
82 	inode_index = inode->i_ino /
83 		(EFS_BLOCKSIZE / sizeof(struct efs_dinode));
84 
85 	block = sb->fs_start + sb->first_block +
86 		(sb->group_size * (inode_index / sb->inode_blocks)) +
87 		(inode_index % sb->inode_blocks);
88 
89 	offset = (inode->i_ino %
90 			(EFS_BLOCKSIZE / sizeof(struct efs_dinode))) *
91 		sizeof(struct efs_dinode);
92 
93 	bh = sb_bread(inode->i_sb, block);
94 	if (!bh) {
95 		pr_warn("%s() failed at block %d\n", __func__, block);
96 		goto read_inode_error;
97 	}
98 
99 	efs_inode = (struct efs_dinode *) (bh->b_data + offset);
100 
101 	inode->i_mode  = be16_to_cpu(efs_inode->di_mode);
102 	set_nlink(inode, be16_to_cpu(efs_inode->di_nlink));
103 	i_uid_write(inode, (uid_t)be16_to_cpu(efs_inode->di_uid));
104 	i_gid_write(inode, (gid_t)be16_to_cpu(efs_inode->di_gid));
105 	inode->i_size  = be32_to_cpu(efs_inode->di_size);
106 	inode->i_atime.tv_sec = be32_to_cpu(efs_inode->di_atime);
107 	inode->i_mtime.tv_sec = be32_to_cpu(efs_inode->di_mtime);
108 	inode_set_ctime(inode, be32_to_cpu(efs_inode->di_ctime), 0);
109 	inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
110 
111 	/* this is the number of blocks in the file */
112 	if (inode->i_size == 0) {
113 		inode->i_blocks = 0;
114 	} else {
115 		inode->i_blocks = ((inode->i_size - 1) >> EFS_BLOCKSIZE_BITS) + 1;
116 	}
117 
118 	rdev = be16_to_cpu(efs_inode->di_u.di_dev.odev);
119 	if (rdev == 0xffff) {
120 		rdev = be32_to_cpu(efs_inode->di_u.di_dev.ndev);
121 		if (sysv_major(rdev) > 0xfff)
122 			device = 0;
123 		else
124 			device = MKDEV(sysv_major(rdev), sysv_minor(rdev));
125 	} else
126 		device = old_decode_dev(rdev);
127 
128 	/* get the number of extents for this object */
129 	in->numextents = be16_to_cpu(efs_inode->di_numextents);
130 	in->lastextent = 0;
131 
132 	/* copy the extents contained within the inode to memory */
133 	for(i = 0; i < EFS_DIRECTEXTENTS; i++) {
134 		extent_copy(&(efs_inode->di_u.di_extents[i]), &(in->extents[i]));
135 		if (i < in->numextents && in->extents[i].cooked.ex_magic != 0) {
136 			pr_warn("extent %d has bad magic number in inode %lu\n",
137 				i, inode->i_ino);
138 			brelse(bh);
139 			goto read_inode_error;
140 		}
141 	}
142 
143 	brelse(bh);
144 	pr_debug("efs_iget(): inode %lu, extents %d, mode %o\n",
145 		 inode->i_ino, in->numextents, inode->i_mode);
146 	switch (inode->i_mode & S_IFMT) {
147 		case S_IFDIR:
148 			inode->i_op = &efs_dir_inode_operations;
149 			inode->i_fop = &efs_dir_operations;
150 			break;
151 		case S_IFREG:
152 			inode->i_fop = &generic_ro_fops;
153 			inode->i_data.a_ops = &efs_aops;
154 			break;
155 		case S_IFLNK:
156 			inode->i_op = &page_symlink_inode_operations;
157 			inode_nohighmem(inode);
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 			pr_warn("unsupported inode mode %o\n", inode->i_mode);
167 			goto read_inode_error;
168 			break;
169 	}
170 
171 	unlock_new_inode(inode);
172 	return inode;
173 
174 read_inode_error:
175 	pr_warn("failed to read inode %lu\n", inode->i_ino);
176 	iget_failed(inode);
177 	return ERR_PTR(-EIO);
178 }
179 
180 static inline efs_block_t
efs_extent_check(efs_extent * ptr,efs_block_t block,struct efs_sb_info * sb)181 efs_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 
efs_map_block(struct inode * inode,efs_block_t block)201 efs_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 			pr_err("%s() failed to map (1 extent)\n", __func__);
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 		pr_err("%s() failed to map block %u (dir)\n", __func__, block);
239 		return 0;
240 	}
241 
242 	pr_debug("%s(): indirect search for logical block %u\n",
243 		 __func__, block);
244 	direxts = in->extents[0].cooked.ex_offset;
245 	indexts = in->numextents;
246 
247 	for(indext = 0; indext < indexts; indext++) {
248 		cur = (last + indext) % indexts;
249 
250 		/*
251 		 * work out which direct extent contains `cur'.
252 		 *
253 		 * also compute ibase: i.e. the number of the first
254 		 * indirect extent contained within direct extent `cur'.
255 		 *
256 		 */
257 		ibase = 0;
258 		for(dirext = 0; cur < ibase && dirext < direxts; dirext++) {
259 			ibase += in->extents[dirext].cooked.ex_length *
260 				(EFS_BLOCKSIZE / sizeof(efs_extent));
261 		}
262 
263 		if (dirext == direxts) {
264 			/* should never happen */
265 			pr_err("couldn't find direct extent for indirect extent %d (block %u)\n",
266 			       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 				pr_err("%s() failed at block %d\n",
284 				       __func__, iblock);
285 				return 0;
286 			}
287 			pr_debug("%s(): read indirect extent block %d\n",
288 				 __func__, iblock);
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 			pr_err("extent %d has bad magic number in block %d\n",
299 			       cur, iblock);
300 			if (bh) brelse(bh);
301 			return 0;
302 		}
303 
304 		if ((result = efs_extent_check(&ext, block, sb))) {
305 			if (bh) brelse(bh);
306 			in->lastextent = cur;
307 			return result;
308 		}
309 	}
310 	if (bh) brelse(bh);
311 	pr_err("%s() failed to map block %u (indir)\n", __func__, block);
312 	return 0;
313 }
314 
315 MODULE_LICENSE("GPL");
316