blob: d13e0709599c7d35a85d22464fe3fb06be0d2ca2 [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiang431339b2018-07-26 20:21:48 +08002/*
Gao Xiang431339b2018-07-26 20:21:48 +08003 * Copyright (C) 2017-2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang431339b2018-07-26 20:21:48 +08005 */
Gao Xiangb17500a2018-07-26 20:21:52 +08006#include "xattr.h"
Gao Xiang431339b2018-07-26 20:21:48 +08007
Chao Yu13f06f42018-07-26 20:21:55 +08008#include <trace/events/erofs.h>
9
Gao Xiang0dcd3c92020-07-30 01:58:01 +080010/*
11 * if inode is successfully read, return its inode page (or sometimes
12 * the inode payload page if it's an extended inode) in order to fill
13 * inline data if possible.
14 */
15static struct page *erofs_read_inode(struct inode *inode,
16 unsigned int *ofs)
Gao Xiang431339b2018-07-26 20:21:48 +080017{
Gao Xiang0dcd3c92020-07-30 01:58:01 +080018 struct super_block *sb = inode->i_sb;
19 struct erofs_sb_info *sbi = EROFS_SB(sb);
Gao Xianga5876e22019-09-04 10:08:56 +080020 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang0dcd3c92020-07-30 01:58:01 +080021 const erofs_off_t inode_loc = iloc(sbi, vi->nid);
Gao Xiang8a765682019-09-04 10:08:54 +080022
Gao Xiang0dcd3c92020-07-30 01:58:01 +080023 erofs_blk_t blkaddr, nblks = 0;
24 struct page *page;
25 struct erofs_inode_compact *dic;
26 struct erofs_inode_extended *die, *copied = NULL;
27 unsigned int ifmt;
28 int err;
29
30 blkaddr = erofs_blknr(inode_loc);
31 *ofs = erofs_blkoff(inode_loc);
32
33 erofs_dbg("%s, reading inode nid %llu at %u of blkaddr %u",
34 __func__, vi->nid, *ofs, blkaddr);
35
36 page = erofs_get_meta_page(sb, blkaddr);
37 if (IS_ERR(page)) {
38 erofs_err(sb, "failed to get inode (nid: %llu) page, err %ld",
39 vi->nid, PTR_ERR(page));
40 return page;
41 }
42
43 dic = page_address(page) + *ofs;
44 ifmt = le16_to_cpu(dic->i_format);
Gao Xiang431339b2018-07-26 20:21:48 +080045
Gao Xiang24a806d2021-03-29 08:36:14 +080046 if (ifmt & ~EROFS_I_ALL) {
47 erofs_err(inode->i_sb, "unsupported i_format %u of nid %llu",
48 ifmt, vi->nid);
49 err = -EOPNOTSUPP;
50 goto err_out;
51 }
52
Gao Xiang8a765682019-09-04 10:08:54 +080053 vi->datalayout = erofs_inode_datalayout(ifmt);
Gao Xiang8a765682019-09-04 10:08:54 +080054 if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080055 erofs_err(inode->i_sb, "unsupported datalayout %u of nid %llu",
56 vi->datalayout, vi->nid);
Gao Xiang0dcd3c92020-07-30 01:58:01 +080057 err = -EOPNOTSUPP;
58 goto err_out;
Gao Xiang431339b2018-07-26 20:21:48 +080059 }
60
Gao Xiang8a765682019-09-04 10:08:54 +080061 switch (erofs_inode_version(ifmt)) {
62 case EROFS_INODE_LAYOUT_EXTENDED:
Gao Xiang8a765682019-09-04 10:08:54 +080063 vi->inode_isize = sizeof(struct erofs_inode_extended);
Gao Xiang0dcd3c92020-07-30 01:58:01 +080064 /* check if the inode acrosses page boundary */
65 if (*ofs + vi->inode_isize <= PAGE_SIZE) {
66 *ofs += vi->inode_isize;
67 die = (struct erofs_inode_extended *)dic;
68 } else {
69 const unsigned int gotten = PAGE_SIZE - *ofs;
70
71 copied = kmalloc(vi->inode_isize, GFP_NOFS);
72 if (!copied) {
73 err = -ENOMEM;
74 goto err_out;
75 }
76 memcpy(copied, dic, gotten);
77 unlock_page(page);
78 put_page(page);
79
80 page = erofs_get_meta_page(sb, blkaddr + 1);
81 if (IS_ERR(page)) {
82 erofs_err(sb, "failed to get inode payload page (nid: %llu), err %ld",
83 vi->nid, PTR_ERR(page));
84 kfree(copied);
85 return page;
86 }
87 *ofs = vi->inode_isize - gotten;
88 memcpy((u8 *)copied + gotten, page_address(page), *ofs);
89 die = copied;
90 }
Gao Xiang8a765682019-09-04 10:08:54 +080091 vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
Gao Xiang431339b2018-07-26 20:21:48 +080092
Gao Xiang8a765682019-09-04 10:08:54 +080093 inode->i_mode = le16_to_cpu(die->i_mode);
94 switch (inode->i_mode & S_IFMT) {
95 case S_IFREG:
96 case S_IFDIR:
97 case S_IFLNK:
98 vi->raw_blkaddr = le32_to_cpu(die->i_u.raw_blkaddr);
99 break;
100 case S_IFCHR:
101 case S_IFBLK:
Chao Yud5beb31b2018-07-26 20:21:53 +0800102 inode->i_rdev =
Gao Xiang8a765682019-09-04 10:08:54 +0800103 new_decode_dev(le32_to_cpu(die->i_u.rdev));
104 break;
105 case S_IFIFO:
106 case S_IFSOCK:
Chao Yud5beb31b2018-07-26 20:21:53 +0800107 inode->i_rdev = 0;
Gao Xiang8a765682019-09-04 10:08:54 +0800108 break;
109 default:
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800110 goto bogusimode;
Gao Xiang8a765682019-09-04 10:08:54 +0800111 }
112 i_uid_write(inode, le32_to_cpu(die->i_uid));
113 i_gid_write(inode, le32_to_cpu(die->i_gid));
114 set_nlink(inode, le32_to_cpu(die->i_nlink));
Gao Xiang431339b2018-07-26 20:21:48 +0800115
Gao Xiangd3938ee2020-11-01 03:51:02 +0800116 /* extended inode has its own timestamp */
117 inode->i_ctime.tv_sec = le64_to_cpu(die->i_ctime);
118 inode->i_ctime.tv_nsec = le32_to_cpu(die->i_ctime_nsec);
Gao Xiang431339b2018-07-26 20:21:48 +0800119
Gao Xiang8a765682019-09-04 10:08:54 +0800120 inode->i_size = le64_to_cpu(die->i_size);
Gao Xiangfe6d9872019-05-28 11:19:43 +0800121
122 /* total blocks for compressed files */
Gao Xiang8a765682019-09-04 10:08:54 +0800123 if (erofs_inode_is_data_compressed(vi->datalayout))
124 nblks = le32_to_cpu(die->i_u.compressed_blocks);
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800125
126 kfree(copied);
Gao Xiang8a765682019-09-04 10:08:54 +0800127 break;
128 case EROFS_INODE_LAYOUT_COMPACT:
129 vi->inode_isize = sizeof(struct erofs_inode_compact);
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800130 *ofs += vi->inode_isize;
Gao Xiang8a765682019-09-04 10:08:54 +0800131 vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
Gao Xiang431339b2018-07-26 20:21:48 +0800132
Gao Xiang8a765682019-09-04 10:08:54 +0800133 inode->i_mode = le16_to_cpu(dic->i_mode);
134 switch (inode->i_mode & S_IFMT) {
135 case S_IFREG:
136 case S_IFDIR:
137 case S_IFLNK:
138 vi->raw_blkaddr = le32_to_cpu(dic->i_u.raw_blkaddr);
139 break;
140 case S_IFCHR:
141 case S_IFBLK:
Chao Yud5beb31b2018-07-26 20:21:53 +0800142 inode->i_rdev =
Gao Xiang8a765682019-09-04 10:08:54 +0800143 new_decode_dev(le32_to_cpu(dic->i_u.rdev));
144 break;
145 case S_IFIFO:
146 case S_IFSOCK:
Chao Yud5beb31b2018-07-26 20:21:53 +0800147 inode->i_rdev = 0;
Gao Xiang8a765682019-09-04 10:08:54 +0800148 break;
149 default:
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800150 goto bogusimode;
Gao Xiang8a765682019-09-04 10:08:54 +0800151 }
152 i_uid_write(inode, le16_to_cpu(dic->i_uid));
153 i_gid_write(inode, le16_to_cpu(dic->i_gid));
154 set_nlink(inode, le16_to_cpu(dic->i_nlink));
Gao Xiang431339b2018-07-26 20:21:48 +0800155
Gao Xiangd3938ee2020-11-01 03:51:02 +0800156 /* use build time for compact inodes */
157 inode->i_ctime.tv_sec = sbi->build_time;
158 inode->i_ctime.tv_nsec = sbi->build_time_nsec;
Gao Xiang431339b2018-07-26 20:21:48 +0800159
Gao Xiang8a765682019-09-04 10:08:54 +0800160 inode->i_size = le32_to_cpu(dic->i_size);
161 if (erofs_inode_is_data_compressed(vi->datalayout))
162 nblks = le32_to_cpu(dic->i_u.compressed_blocks);
163 break;
164 default:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800165 erofs_err(inode->i_sb,
166 "unsupported on-disk inode version %u of nid %llu",
167 erofs_inode_version(ifmt), vi->nid);
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800168 err = -EOPNOTSUPP;
169 goto err_out;
Gao Xiang431339b2018-07-26 20:21:48 +0800170 }
171
Gao Xiangd3938ee2020-11-01 03:51:02 +0800172 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec;
173 inode->i_atime.tv_sec = inode->i_ctime.tv_sec;
174 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec;
175 inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec;
176
Gao Xiang06252e92021-08-05 08:36:00 +0800177 inode->i_flags &= ~S_DAX;
178 if (test_opt(&sbi->ctx, DAX_ALWAYS) && S_ISREG(inode->i_mode) &&
179 vi->datalayout == EROFS_INODE_FLAT_PLAIN)
180 inode->i_flags |= S_DAX;
Gao Xiangfe6d9872019-05-28 11:19:43 +0800181 if (!nblks)
182 /* measure inode.i_blocks as generic filesystems */
183 inode->i_blocks = roundup(inode->i_size, EROFS_BLKSIZ) >> 9;
184 else
185 inode->i_blocks = nblks << LOG_SECTORS_PER_BLOCK;
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800186 return page;
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800187
188bogusimode:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800189 erofs_err(inode->i_sb, "bogus i_mode (%o) @ nid %llu",
190 inode->i_mode, vi->nid);
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800191 err = -EFSCORRUPTED;
192err_out:
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800193 DBG_BUGON(1);
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800194 kfree(copied);
195 unlock_page(page);
196 put_page(page);
197 return ERR_PTR(err);
Gao Xiang431339b2018-07-26 20:21:48 +0800198}
199
Gao Xianga2c75c82019-09-04 10:08:59 +0800200static int erofs_fill_symlink(struct inode *inode, void *data,
201 unsigned int m_pofs)
Gao Xiang431339b2018-07-26 20:21:48 +0800202{
Gao Xianga5876e22019-09-04 10:08:56 +0800203 struct erofs_inode *vi = EROFS_I(inode);
Gao Xianga2c75c82019-09-04 10:08:59 +0800204 char *lnk;
Gao Xiang431339b2018-07-26 20:21:48 +0800205
Gao Xianga2c75c82019-09-04 10:08:59 +0800206 /* if it cannot be handled with fast symlink scheme */
207 if (vi->datalayout != EROFS_INODE_FLAT_INLINE ||
208 inode->i_size >= PAGE_SIZE) {
209 inode->i_op = &erofs_symlink_iops;
Gao Xiang431339b2018-07-26 20:21:48 +0800210 return 0;
Gao Xiang431339b2018-07-26 20:21:48 +0800211 }
Gao Xianga2c75c82019-09-04 10:08:59 +0800212
Gao Xiange2c71e72019-09-04 10:09:06 +0800213 lnk = kmalloc(inode->i_size + 1, GFP_KERNEL);
Gao Xianga2c75c82019-09-04 10:08:59 +0800214 if (!lnk)
215 return -ENOMEM;
216
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800217 m_pofs += vi->xattr_isize;
Gao Xianga2c75c82019-09-04 10:08:59 +0800218 /* inline symlink data shouldn't cross page boundary as well */
219 if (m_pofs + inode->i_size > PAGE_SIZE) {
220 kfree(lnk);
Gao Xiang4f761fa2019-09-04 10:09:09 +0800221 erofs_err(inode->i_sb,
222 "inline data cross block boundary @ nid %llu",
223 vi->nid);
Gao Xianga2c75c82019-09-04 10:08:59 +0800224 DBG_BUGON(1);
225 return -EFSCORRUPTED;
226 }
227
228 memcpy(lnk, data + m_pofs, inode->i_size);
229 lnk[inode->i_size] = '\0';
230
231 inode->i_link = lnk;
232 inode->i_op = &erofs_fast_symlink_iops;
Yue Hu55457452019-06-27 17:46:15 +0800233 return 0;
Gao Xiang431339b2018-07-26 20:21:48 +0800234}
235
Gao Xiang99634bf2019-09-04 10:09:05 +0800236static int erofs_fill_inode(struct inode *inode, int isdir)
Gao Xiang431339b2018-07-26 20:21:48 +0800237{
Gao Xianga5876e22019-09-04 10:08:56 +0800238 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang431339b2018-07-26 20:21:48 +0800239 struct page *page;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200240 unsigned int ofs;
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800241 int err = 0;
Gao Xiang431339b2018-07-26 20:21:48 +0800242
Chao Yu13f06f42018-07-26 20:21:55 +0800243 trace_erofs_fill_inode(inode, isdir);
Gao Xiang431339b2018-07-26 20:21:48 +0800244
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800245 /* read inode base data from disk */
246 page = erofs_read_inode(inode, &ofs);
247 if (IS_ERR(page))
Gao Xiang431339b2018-07-26 20:21:48 +0800248 return PTR_ERR(page);
Gao Xiang431339b2018-07-26 20:21:48 +0800249
Gao Xiang84947eb2019-09-04 10:09:08 +0800250 /* setup the new inode */
251 switch (inode->i_mode & S_IFMT) {
252 case S_IFREG:
253 inode->i_op = &erofs_generic_iops;
Huang Jianana08e67a2021-08-05 08:35:59 +0800254 if (erofs_inode_is_data_compressed(vi->datalayout))
255 inode->i_fop = &generic_ro_fops;
256 else
257 inode->i_fop = &erofs_file_fops;
Gao Xiang84947eb2019-09-04 10:09:08 +0800258 break;
259 case S_IFDIR:
260 inode->i_op = &erofs_dir_iops;
261 inode->i_fop = &erofs_dir_fops;
262 break;
263 case S_IFLNK:
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800264 err = erofs_fill_symlink(inode, page_address(page), ofs);
Gao Xiang84947eb2019-09-04 10:09:08 +0800265 if (err)
Gao Xiang431339b2018-07-26 20:21:48 +0800266 goto out_unlock;
Gao Xiang84947eb2019-09-04 10:09:08 +0800267 inode_nohighmem(inode);
268 break;
269 case S_IFCHR:
270 case S_IFBLK:
271 case S_IFIFO:
272 case S_IFSOCK:
273 inode->i_op = &erofs_generic_iops;
274 init_special_inode(inode, inode->i_mode, inode->i_rdev);
275 goto out_unlock;
276 default:
277 err = -EFSCORRUPTED;
278 goto out_unlock;
Gao Xiang431339b2018-07-26 20:21:48 +0800279 }
280
Gao Xiang84947eb2019-09-04 10:09:08 +0800281 if (erofs_inode_is_data_compressed(vi->datalayout)) {
282 err = z_erofs_fill_inode(inode);
283 goto out_unlock;
284 }
285 inode->i_mapping->a_ops = &erofs_raw_access_aops;
286
Gao Xiang431339b2018-07-26 20:21:48 +0800287out_unlock:
288 unlock_page(page);
289 put_page(page);
290 return err;
291}
292
Gao Xiang2abd7812018-10-09 22:07:13 +0800293/*
294 * erofs nid is 64bits, but i_ino is 'unsigned long', therefore
295 * we should do more for 32-bit platform to find the right inode.
296 */
Gao Xiang2abd7812018-10-09 22:07:13 +0800297static int erofs_ilookup_test_actor(struct inode *inode, void *opaque)
298{
299 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
300
Gao Xianga5876e22019-09-04 10:08:56 +0800301 return EROFS_I(inode)->nid == nid;
Gao Xiang2abd7812018-10-09 22:07:13 +0800302}
303
304static int erofs_iget_set_actor(struct inode *inode, void *opaque)
305{
306 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
307
308 inode->i_ino = erofs_inode_hash(nid);
309 return 0;
310}
Gao Xiang2abd7812018-10-09 22:07:13 +0800311
312static inline struct inode *erofs_iget_locked(struct super_block *sb,
313 erofs_nid_t nid)
314{
315 const unsigned long hashval = erofs_inode_hash(nid);
316
Gao Xiang2abd7812018-10-09 22:07:13 +0800317 return iget5_locked(sb, hashval, erofs_ilookup_test_actor,
318 erofs_iget_set_actor, &nid);
Gao Xiang2abd7812018-10-09 22:07:13 +0800319}
320
Gao Xiang431339b2018-07-26 20:21:48 +0800321struct inode *erofs_iget(struct super_block *sb,
Julian Merida447a3622019-03-18 20:58:41 -0300322 erofs_nid_t nid,
323 bool isdir)
Gao Xiang431339b2018-07-26 20:21:48 +0800324{
Gao Xiang2abd7812018-10-09 22:07:13 +0800325 struct inode *inode = erofs_iget_locked(sb, nid);
Gao Xiang431339b2018-07-26 20:21:48 +0800326
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800327 if (!inode)
Gao Xiang431339b2018-07-26 20:21:48 +0800328 return ERR_PTR(-ENOMEM);
329
330 if (inode->i_state & I_NEW) {
331 int err;
Gao Xianga5876e22019-09-04 10:08:56 +0800332 struct erofs_inode *vi = EROFS_I(inode);
Julio Bianco8af36472019-03-09 14:08:53 -0300333
Gao Xiang431339b2018-07-26 20:21:48 +0800334 vi->nid = nid;
335
Gao Xiang99634bf2019-09-04 10:09:05 +0800336 err = erofs_fill_inode(inode, isdir);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800337 if (!err)
Gao Xiang431339b2018-07-26 20:21:48 +0800338 unlock_new_inode(inode);
339 else {
340 iget_failed(inode);
341 inode = ERR_PTR(err);
342 }
343 }
344 return inode;
345}
346
Christian Brauner549c7292021-01-21 14:19:43 +0100347int erofs_getattr(struct user_namespace *mnt_userns, const struct path *path,
348 struct kstat *stat, u32 request_mask,
349 unsigned int query_flags)
Gao Xiang89f27ed2019-05-28 11:19:42 +0800350{
351 struct inode *const inode = d_inode(path->dentry);
Gao Xiang89f27ed2019-05-28 11:19:42 +0800352
Gao Xianga5876e22019-09-04 10:08:56 +0800353 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout))
Gao Xiang89f27ed2019-05-28 11:19:42 +0800354 stat->attributes |= STATX_ATTR_COMPRESSED;
355
356 stat->attributes |= STATX_ATTR_IMMUTABLE;
357 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
358 STATX_ATTR_IMMUTABLE);
359
Christian Brauner0d56a452021-01-21 14:19:30 +0100360 generic_fillattr(&init_user_ns, inode, stat);
Gao Xiang89f27ed2019-05-28 11:19:42 +0800361 return 0;
362}
363
Gao Xiang60939822019-01-14 19:40:24 +0800364const struct inode_operations erofs_generic_iops = {
Gao Xiang89f27ed2019-05-28 11:19:42 +0800365 .getattr = erofs_getattr,
Gao Xiangb17500a2018-07-26 20:21:52 +0800366 .listxattr = erofs_listxattr,
Gao Xiang516c115c2019-01-29 16:35:20 +0800367 .get_acl = erofs_get_acl,
Gao Xiangeadcd6b2021-08-13 13:29:31 +0800368 .fiemap = erofs_fiemap,
Gao Xiang60939822019-01-14 19:40:24 +0800369};
370
371const struct inode_operations erofs_symlink_iops = {
372 .get_link = page_get_link,
Gao Xiang89f27ed2019-05-28 11:19:42 +0800373 .getattr = erofs_getattr,
Gao Xiang60939822019-01-14 19:40:24 +0800374 .listxattr = erofs_listxattr,
Gao Xiang516c115c2019-01-29 16:35:20 +0800375 .get_acl = erofs_get_acl,
Gao Xiang60939822019-01-14 19:40:24 +0800376};
377
378const struct inode_operations erofs_fast_symlink_iops = {
379 .get_link = simple_get_link,
Gao Xiang89f27ed2019-05-28 11:19:42 +0800380 .getattr = erofs_getattr,
Gao Xiang60939822019-01-14 19:40:24 +0800381 .listxattr = erofs_listxattr,
Gao Xiang516c115c2019-01-29 16:35:20 +0800382 .get_acl = erofs_get_acl,
Gao Xiang60939822019-01-14 19:40:24 +0800383};