xref: /linux/fs/erofs/inode.c (revision 019b383d1132e4051de0d2e43254454b86538cf4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017-2018 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Copyright (C) 2021, Alibaba Cloud
6  */
7 #include "xattr.h"
8 
9 #include <trace/events/erofs.h>
10 
11 static void *erofs_read_inode(struct erofs_buf *buf,
12 			      struct inode *inode, unsigned int *ofs)
13 {
14 	struct super_block *sb = inode->i_sb;
15 	struct erofs_sb_info *sbi = EROFS_SB(sb);
16 	struct erofs_inode *vi = EROFS_I(inode);
17 	const erofs_off_t inode_loc = erofs_iloc(inode);
18 
19 	erofs_blk_t blkaddr, nblks = 0;
20 	void *kaddr;
21 	struct erofs_inode_compact *dic;
22 	struct erofs_inode_extended *die, *copied = NULL;
23 	unsigned int ifmt;
24 	int err;
25 
26 	blkaddr = erofs_blknr(sb, inode_loc);
27 	*ofs = erofs_blkoff(sb, inode_loc);
28 
29 	kaddr = erofs_read_metabuf(buf, sb, blkaddr, EROFS_KMAP);
30 	if (IS_ERR(kaddr)) {
31 		erofs_err(sb, "failed to get inode (nid: %llu) page, err %ld",
32 			  vi->nid, PTR_ERR(kaddr));
33 		return kaddr;
34 	}
35 
36 	dic = kaddr + *ofs;
37 	ifmt = le16_to_cpu(dic->i_format);
38 
39 	if (ifmt & ~EROFS_I_ALL) {
40 		erofs_err(inode->i_sb, "unsupported i_format %u of nid %llu",
41 			  ifmt, vi->nid);
42 		err = -EOPNOTSUPP;
43 		goto err_out;
44 	}
45 
46 	vi->datalayout = erofs_inode_datalayout(ifmt);
47 	if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
48 		erofs_err(inode->i_sb, "unsupported datalayout %u of nid %llu",
49 			  vi->datalayout, vi->nid);
50 		err = -EOPNOTSUPP;
51 		goto err_out;
52 	}
53 
54 	switch (erofs_inode_version(ifmt)) {
55 	case EROFS_INODE_LAYOUT_EXTENDED:
56 		vi->inode_isize = sizeof(struct erofs_inode_extended);
57 		/* check if the extended inode acrosses block boundary */
58 		if (*ofs + vi->inode_isize <= sb->s_blocksize) {
59 			*ofs += vi->inode_isize;
60 			die = (struct erofs_inode_extended *)dic;
61 		} else {
62 			const unsigned int gotten = sb->s_blocksize - *ofs;
63 
64 			copied = kmalloc(vi->inode_isize, GFP_NOFS);
65 			if (!copied) {
66 				err = -ENOMEM;
67 				goto err_out;
68 			}
69 			memcpy(copied, dic, gotten);
70 			kaddr = erofs_read_metabuf(buf, sb, blkaddr + 1,
71 						   EROFS_KMAP);
72 			if (IS_ERR(kaddr)) {
73 				erofs_err(sb, "failed to get inode payload block (nid: %llu), err %ld",
74 					  vi->nid, PTR_ERR(kaddr));
75 				kfree(copied);
76 				return kaddr;
77 			}
78 			*ofs = vi->inode_isize - gotten;
79 			memcpy((u8 *)copied + gotten, kaddr, *ofs);
80 			die = copied;
81 		}
82 		vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
83 
84 		inode->i_mode = le16_to_cpu(die->i_mode);
85 		switch (inode->i_mode & S_IFMT) {
86 		case S_IFREG:
87 		case S_IFDIR:
88 		case S_IFLNK:
89 			vi->raw_blkaddr = le32_to_cpu(die->i_u.raw_blkaddr);
90 			break;
91 		case S_IFCHR:
92 		case S_IFBLK:
93 			inode->i_rdev =
94 				new_decode_dev(le32_to_cpu(die->i_u.rdev));
95 			break;
96 		case S_IFIFO:
97 		case S_IFSOCK:
98 			inode->i_rdev = 0;
99 			break;
100 		default:
101 			goto bogusimode;
102 		}
103 		i_uid_write(inode, le32_to_cpu(die->i_uid));
104 		i_gid_write(inode, le32_to_cpu(die->i_gid));
105 		set_nlink(inode, le32_to_cpu(die->i_nlink));
106 
107 		/* extended inode has its own timestamp */
108 		inode_set_ctime(inode, le64_to_cpu(die->i_mtime),
109 				le32_to_cpu(die->i_mtime_nsec));
110 
111 		inode->i_size = le64_to_cpu(die->i_size);
112 
113 		/* total blocks for compressed files */
114 		if (erofs_inode_is_data_compressed(vi->datalayout))
115 			nblks = le32_to_cpu(die->i_u.compressed_blocks);
116 		else if (vi->datalayout == EROFS_INODE_CHUNK_BASED)
117 			/* fill chunked inode summary info */
118 			vi->chunkformat = le16_to_cpu(die->i_u.c.format);
119 		kfree(copied);
120 		copied = NULL;
121 		break;
122 	case EROFS_INODE_LAYOUT_COMPACT:
123 		vi->inode_isize = sizeof(struct erofs_inode_compact);
124 		*ofs += vi->inode_isize;
125 		vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
126 
127 		inode->i_mode = le16_to_cpu(dic->i_mode);
128 		switch (inode->i_mode & S_IFMT) {
129 		case S_IFREG:
130 		case S_IFDIR:
131 		case S_IFLNK:
132 			vi->raw_blkaddr = le32_to_cpu(dic->i_u.raw_blkaddr);
133 			break;
134 		case S_IFCHR:
135 		case S_IFBLK:
136 			inode->i_rdev =
137 				new_decode_dev(le32_to_cpu(dic->i_u.rdev));
138 			break;
139 		case S_IFIFO:
140 		case S_IFSOCK:
141 			inode->i_rdev = 0;
142 			break;
143 		default:
144 			goto bogusimode;
145 		}
146 		i_uid_write(inode, le16_to_cpu(dic->i_uid));
147 		i_gid_write(inode, le16_to_cpu(dic->i_gid));
148 		set_nlink(inode, le16_to_cpu(dic->i_nlink));
149 
150 		/* use build time for compact inodes */
151 		inode_set_ctime(inode, sbi->build_time, sbi->build_time_nsec);
152 
153 		inode->i_size = le32_to_cpu(dic->i_size);
154 		if (erofs_inode_is_data_compressed(vi->datalayout))
155 			nblks = le32_to_cpu(dic->i_u.compressed_blocks);
156 		else if (vi->datalayout == EROFS_INODE_CHUNK_BASED)
157 			vi->chunkformat = le16_to_cpu(dic->i_u.c.format);
158 		break;
159 	default:
160 		erofs_err(inode->i_sb,
161 			  "unsupported on-disk inode version %u of nid %llu",
162 			  erofs_inode_version(ifmt), vi->nid);
163 		err = -EOPNOTSUPP;
164 		goto err_out;
165 	}
166 
167 	if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
168 		if (vi->chunkformat & ~EROFS_CHUNK_FORMAT_ALL) {
169 			erofs_err(inode->i_sb,
170 				  "unsupported chunk format %x of nid %llu",
171 				  vi->chunkformat, vi->nid);
172 			err = -EOPNOTSUPP;
173 			goto err_out;
174 		}
175 		vi->chunkbits = sb->s_blocksize_bits +
176 			(vi->chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
177 	}
178 	inode->i_mtime = inode->i_atime = inode_get_ctime(inode);
179 
180 	inode->i_flags &= ~S_DAX;
181 	if (test_opt(&sbi->opt, DAX_ALWAYS) && S_ISREG(inode->i_mode) &&
182 	    (vi->datalayout == EROFS_INODE_FLAT_PLAIN ||
183 	     vi->datalayout == EROFS_INODE_CHUNK_BASED))
184 		inode->i_flags |= S_DAX;
185 
186 	if (!nblks)
187 		/* measure inode.i_blocks as generic filesystems */
188 		inode->i_blocks = round_up(inode->i_size, sb->s_blocksize) >> 9;
189 	else
190 		inode->i_blocks = nblks << (sb->s_blocksize_bits - 9);
191 	return kaddr;
192 
193 bogusimode:
194 	erofs_err(inode->i_sb, "bogus i_mode (%o) @ nid %llu",
195 		  inode->i_mode, vi->nid);
196 	err = -EFSCORRUPTED;
197 err_out:
198 	DBG_BUGON(1);
199 	kfree(copied);
200 	erofs_put_metabuf(buf);
201 	return ERR_PTR(err);
202 }
203 
204 static int erofs_fill_symlink(struct inode *inode, void *kaddr,
205 			      unsigned int m_pofs)
206 {
207 	struct erofs_inode *vi = EROFS_I(inode);
208 	unsigned int bsz = i_blocksize(inode);
209 	char *lnk;
210 
211 	/* if it cannot be handled with fast symlink scheme */
212 	if (vi->datalayout != EROFS_INODE_FLAT_INLINE ||
213 	    inode->i_size >= bsz || inode->i_size < 0) {
214 		inode->i_op = &erofs_symlink_iops;
215 		return 0;
216 	}
217 
218 	lnk = kmalloc(inode->i_size + 1, GFP_KERNEL);
219 	if (!lnk)
220 		return -ENOMEM;
221 
222 	m_pofs += vi->xattr_isize;
223 	/* inline symlink data shouldn't cross block boundary */
224 	if (m_pofs + inode->i_size > bsz) {
225 		kfree(lnk);
226 		erofs_err(inode->i_sb,
227 			  "inline data cross block boundary @ nid %llu",
228 			  vi->nid);
229 		DBG_BUGON(1);
230 		return -EFSCORRUPTED;
231 	}
232 	memcpy(lnk, kaddr + m_pofs, inode->i_size);
233 	lnk[inode->i_size] = '\0';
234 
235 	inode->i_link = lnk;
236 	inode->i_op = &erofs_fast_symlink_iops;
237 	return 0;
238 }
239 
240 static int erofs_fill_inode(struct inode *inode)
241 {
242 	struct erofs_inode *vi = EROFS_I(inode);
243 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
244 	void *kaddr;
245 	unsigned int ofs;
246 	int err = 0;
247 
248 	trace_erofs_fill_inode(inode);
249 
250 	/* read inode base data from disk */
251 	kaddr = erofs_read_inode(&buf, inode, &ofs);
252 	if (IS_ERR(kaddr))
253 		return PTR_ERR(kaddr);
254 
255 	/* setup the new inode */
256 	switch (inode->i_mode & S_IFMT) {
257 	case S_IFREG:
258 		inode->i_op = &erofs_generic_iops;
259 		if (erofs_inode_is_data_compressed(vi->datalayout))
260 			inode->i_fop = &generic_ro_fops;
261 		else
262 			inode->i_fop = &erofs_file_fops;
263 		break;
264 	case S_IFDIR:
265 		inode->i_op = &erofs_dir_iops;
266 		inode->i_fop = &erofs_dir_fops;
267 		inode_nohighmem(inode);
268 		break;
269 	case S_IFLNK:
270 		err = erofs_fill_symlink(inode, kaddr, ofs);
271 		if (err)
272 			goto out_unlock;
273 		inode_nohighmem(inode);
274 		break;
275 	case S_IFCHR:
276 	case S_IFBLK:
277 	case S_IFIFO:
278 	case S_IFSOCK:
279 		inode->i_op = &erofs_generic_iops;
280 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
281 		goto out_unlock;
282 	default:
283 		err = -EFSCORRUPTED;
284 		goto out_unlock;
285 	}
286 
287 	if (erofs_inode_is_data_compressed(vi->datalayout)) {
288 #ifdef CONFIG_EROFS_FS_ZIP
289 		if (!erofs_is_fscache_mode(inode->i_sb) &&
290 		    inode->i_sb->s_blocksize_bits == PAGE_SHIFT) {
291 			inode->i_mapping->a_ops = &z_erofs_aops;
292 			err = 0;
293 			goto out_unlock;
294 		}
295 #endif
296 		err = -EOPNOTSUPP;
297 		goto out_unlock;
298 	}
299 	inode->i_mapping->a_ops = &erofs_raw_access_aops;
300 	mapping_set_large_folios(inode->i_mapping);
301 #ifdef CONFIG_EROFS_FS_ONDEMAND
302 	if (erofs_is_fscache_mode(inode->i_sb))
303 		inode->i_mapping->a_ops = &erofs_fscache_access_aops;
304 #endif
305 
306 out_unlock:
307 	erofs_put_metabuf(&buf);
308 	return err;
309 }
310 
311 /*
312  * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
313  * so that it will fit.
314  */
315 static ino_t erofs_squash_ino(erofs_nid_t nid)
316 {
317 	ino_t ino = (ino_t)nid;
318 
319 	if (sizeof(ino_t) < sizeof(erofs_nid_t))
320 		ino ^= nid >> (sizeof(erofs_nid_t) - sizeof(ino_t)) * 8;
321 	return ino;
322 }
323 
324 static int erofs_iget5_eq(struct inode *inode, void *opaque)
325 {
326 	return EROFS_I(inode)->nid == *(erofs_nid_t *)opaque;
327 }
328 
329 static int erofs_iget5_set(struct inode *inode, void *opaque)
330 {
331 	const erofs_nid_t nid = *(erofs_nid_t *)opaque;
332 
333 	inode->i_ino = erofs_squash_ino(nid);
334 	EROFS_I(inode)->nid = nid;
335 	return 0;
336 }
337 
338 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid)
339 {
340 	struct inode *inode;
341 
342 	inode = iget5_locked(sb, erofs_squash_ino(nid), erofs_iget5_eq,
343 			     erofs_iget5_set, &nid);
344 	if (!inode)
345 		return ERR_PTR(-ENOMEM);
346 
347 	if (inode->i_state & I_NEW) {
348 		int err = erofs_fill_inode(inode);
349 
350 		if (err) {
351 			iget_failed(inode);
352 			return ERR_PTR(err);
353 		}
354 		unlock_new_inode(inode);
355 	}
356 	return inode;
357 }
358 
359 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
360 		  struct kstat *stat, u32 request_mask,
361 		  unsigned int query_flags)
362 {
363 	struct inode *const inode = d_inode(path->dentry);
364 
365 	if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout))
366 		stat->attributes |= STATX_ATTR_COMPRESSED;
367 
368 	stat->attributes |= STATX_ATTR_IMMUTABLE;
369 	stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
370 				  STATX_ATTR_IMMUTABLE);
371 
372 	generic_fillattr(idmap, request_mask, inode, stat);
373 	return 0;
374 }
375 
376 const struct inode_operations erofs_generic_iops = {
377 	.getattr = erofs_getattr,
378 	.listxattr = erofs_listxattr,
379 	.get_inode_acl = erofs_get_acl,
380 	.fiemap = erofs_fiemap,
381 };
382 
383 const struct inode_operations erofs_symlink_iops = {
384 	.get_link = page_get_link,
385 	.getattr = erofs_getattr,
386 	.listxattr = erofs_listxattr,
387 	.get_inode_acl = erofs_get_acl,
388 };
389 
390 const struct inode_operations erofs_fast_symlink_iops = {
391 	.get_link = simple_get_link,
392 	.getattr = erofs_getattr,
393 	.listxattr = erofs_listxattr,
394 	.get_inode_acl = erofs_get_acl,
395 };
396