blob: 139d0bed42f8865d705bc4e18dc16a936e2a6b8c [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 * Created by Gao Xiang <gaoxiang25@huawei.com>
Gao Xiang431339b2018-07-26 20:21:48 +08006 */
Gao Xiangb17500a2018-07-26 20:21:52 +08007#include "xattr.h"
Gao Xiang431339b2018-07-26 20:21:48 +08008
Chao Yu13f06f42018-07-26 20:21:55 +08009#include <trace/events/erofs.h>
10
Gao Xiang0dcd3c92020-07-30 01:58:01 +080011/*
12 * if inode is successfully read, return its inode page (or sometimes
13 * the inode payload page if it's an extended inode) in order to fill
14 * inline data if possible.
15 */
16static struct page *erofs_read_inode(struct inode *inode,
17 unsigned int *ofs)
Gao Xiang431339b2018-07-26 20:21:48 +080018{
Gao Xiang0dcd3c92020-07-30 01:58:01 +080019 struct super_block *sb = inode->i_sb;
20 struct erofs_sb_info *sbi = EROFS_SB(sb);
Gao Xianga5876e22019-09-04 10:08:56 +080021 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang0dcd3c92020-07-30 01:58:01 +080022 const erofs_off_t inode_loc = iloc(sbi, vi->nid);
Gao Xiang8a765682019-09-04 10:08:54 +080023
Gao Xiang0dcd3c92020-07-30 01:58:01 +080024 erofs_blk_t blkaddr, nblks = 0;
25 struct page *page;
26 struct erofs_inode_compact *dic;
27 struct erofs_inode_extended *die, *copied = NULL;
28 unsigned int ifmt;
29 int err;
30
31 blkaddr = erofs_blknr(inode_loc);
32 *ofs = erofs_blkoff(inode_loc);
33
34 erofs_dbg("%s, reading inode nid %llu at %u of blkaddr %u",
35 __func__, vi->nid, *ofs, blkaddr);
36
37 page = erofs_get_meta_page(sb, blkaddr);
38 if (IS_ERR(page)) {
39 erofs_err(sb, "failed to get inode (nid: %llu) page, err %ld",
40 vi->nid, PTR_ERR(page));
41 return page;
42 }
43
44 dic = page_address(page) + *ofs;
45 ifmt = le16_to_cpu(dic->i_format);
Gao Xiang431339b2018-07-26 20:21:48 +080046
Gao Xiang8a765682019-09-04 10:08:54 +080047 vi->datalayout = erofs_inode_datalayout(ifmt);
Gao Xiang8a765682019-09-04 10:08:54 +080048 if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080049 erofs_err(inode->i_sb, "unsupported datalayout %u of nid %llu",
50 vi->datalayout, vi->nid);
Gao Xiang0dcd3c92020-07-30 01:58:01 +080051 err = -EOPNOTSUPP;
52 goto err_out;
Gao Xiang431339b2018-07-26 20:21:48 +080053 }
54
Gao Xiang8a765682019-09-04 10:08:54 +080055 switch (erofs_inode_version(ifmt)) {
56 case EROFS_INODE_LAYOUT_EXTENDED:
Gao Xiang8a765682019-09-04 10:08:54 +080057 vi->inode_isize = sizeof(struct erofs_inode_extended);
Gao Xiang0dcd3c92020-07-30 01:58:01 +080058 /* check if the inode acrosses page boundary */
59 if (*ofs + vi->inode_isize <= PAGE_SIZE) {
60 *ofs += vi->inode_isize;
61 die = (struct erofs_inode_extended *)dic;
62 } else {
63 const unsigned int gotten = PAGE_SIZE - *ofs;
64
65 copied = kmalloc(vi->inode_isize, GFP_NOFS);
66 if (!copied) {
67 err = -ENOMEM;
68 goto err_out;
69 }
70 memcpy(copied, dic, gotten);
71 unlock_page(page);
72 put_page(page);
73
74 page = erofs_get_meta_page(sb, blkaddr + 1);
75 if (IS_ERR(page)) {
76 erofs_err(sb, "failed to get inode payload page (nid: %llu), err %ld",
77 vi->nid, PTR_ERR(page));
78 kfree(copied);
79 return page;
80 }
81 *ofs = vi->inode_isize - gotten;
82 memcpy((u8 *)copied + gotten, page_address(page), *ofs);
83 die = copied;
84 }
Gao Xiang8a765682019-09-04 10:08:54 +080085 vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
Gao Xiang431339b2018-07-26 20:21:48 +080086
Gao Xiang8a765682019-09-04 10:08:54 +080087 inode->i_mode = le16_to_cpu(die->i_mode);
88 switch (inode->i_mode & S_IFMT) {
89 case S_IFREG:
90 case S_IFDIR:
91 case S_IFLNK:
92 vi->raw_blkaddr = le32_to_cpu(die->i_u.raw_blkaddr);
93 break;
94 case S_IFCHR:
95 case S_IFBLK:
Chao Yud5beb31b2018-07-26 20:21:53 +080096 inode->i_rdev =
Gao Xiang8a765682019-09-04 10:08:54 +080097 new_decode_dev(le32_to_cpu(die->i_u.rdev));
98 break;
99 case S_IFIFO:
100 case S_IFSOCK:
Chao Yud5beb31b2018-07-26 20:21:53 +0800101 inode->i_rdev = 0;
Gao Xiang8a765682019-09-04 10:08:54 +0800102 break;
103 default:
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800104 goto bogusimode;
Gao Xiang8a765682019-09-04 10:08:54 +0800105 }
106 i_uid_write(inode, le32_to_cpu(die->i_uid));
107 i_gid_write(inode, le32_to_cpu(die->i_gid));
108 set_nlink(inode, le32_to_cpu(die->i_nlink));
Gao Xiang431339b2018-07-26 20:21:48 +0800109
110 /* ns timestamp */
111 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec =
Gao Xiang8a765682019-09-04 10:08:54 +0800112 le64_to_cpu(die->i_ctime);
Gao Xiang431339b2018-07-26 20:21:48 +0800113 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec =
Gao Xiang8a765682019-09-04 10:08:54 +0800114 le32_to_cpu(die->i_ctime_nsec);
Gao Xiang431339b2018-07-26 20:21:48 +0800115
Gao Xiang8a765682019-09-04 10:08:54 +0800116 inode->i_size = le64_to_cpu(die->i_size);
Gao Xiangfe6d9872019-05-28 11:19:43 +0800117
118 /* total blocks for compressed files */
Gao Xiang8a765682019-09-04 10:08:54 +0800119 if (erofs_inode_is_data_compressed(vi->datalayout))
120 nblks = le32_to_cpu(die->i_u.compressed_blocks);
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800121
122 kfree(copied);
Gao Xiang8a765682019-09-04 10:08:54 +0800123 break;
124 case EROFS_INODE_LAYOUT_COMPACT:
125 vi->inode_isize = sizeof(struct erofs_inode_compact);
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800126 *ofs += vi->inode_isize;
Gao Xiang8a765682019-09-04 10:08:54 +0800127 vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
Gao Xiang431339b2018-07-26 20:21:48 +0800128
Gao Xiang8a765682019-09-04 10:08:54 +0800129 inode->i_mode = le16_to_cpu(dic->i_mode);
130 switch (inode->i_mode & S_IFMT) {
131 case S_IFREG:
132 case S_IFDIR:
133 case S_IFLNK:
134 vi->raw_blkaddr = le32_to_cpu(dic->i_u.raw_blkaddr);
135 break;
136 case S_IFCHR:
137 case S_IFBLK:
Chao Yud5beb31b2018-07-26 20:21:53 +0800138 inode->i_rdev =
Gao Xiang8a765682019-09-04 10:08:54 +0800139 new_decode_dev(le32_to_cpu(dic->i_u.rdev));
140 break;
141 case S_IFIFO:
142 case S_IFSOCK:
Chao Yud5beb31b2018-07-26 20:21:53 +0800143 inode->i_rdev = 0;
Gao Xiang8a765682019-09-04 10:08:54 +0800144 break;
145 default:
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800146 goto bogusimode;
Gao Xiang8a765682019-09-04 10:08:54 +0800147 }
148 i_uid_write(inode, le16_to_cpu(dic->i_uid));
149 i_gid_write(inode, le16_to_cpu(dic->i_gid));
150 set_nlink(inode, le16_to_cpu(dic->i_nlink));
Gao Xiang431339b2018-07-26 20:21:48 +0800151
152 /* use build time to derive all file time */
153 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec =
154 sbi->build_time;
155 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec =
156 sbi->build_time_nsec;
157
Gao Xiang8a765682019-09-04 10:08:54 +0800158 inode->i_size = le32_to_cpu(dic->i_size);
159 if (erofs_inode_is_data_compressed(vi->datalayout))
160 nblks = le32_to_cpu(dic->i_u.compressed_blocks);
161 break;
162 default:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800163 erofs_err(inode->i_sb,
164 "unsupported on-disk inode version %u of nid %llu",
165 erofs_inode_version(ifmt), vi->nid);
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800166 err = -EOPNOTSUPP;
167 goto err_out;
Gao Xiang431339b2018-07-26 20:21:48 +0800168 }
169
Gao Xiangfe6d9872019-05-28 11:19:43 +0800170 if (!nblks)
171 /* measure inode.i_blocks as generic filesystems */
172 inode->i_blocks = roundup(inode->i_size, EROFS_BLKSIZ) >> 9;
173 else
174 inode->i_blocks = nblks << LOG_SECTORS_PER_BLOCK;
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800175 return page;
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800176
177bogusimode:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800178 erofs_err(inode->i_sb, "bogus i_mode (%o) @ nid %llu",
179 inode->i_mode, vi->nid);
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800180 err = -EFSCORRUPTED;
181err_out:
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800182 DBG_BUGON(1);
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800183 kfree(copied);
184 unlock_page(page);
185 put_page(page);
186 return ERR_PTR(err);
Gao Xiang431339b2018-07-26 20:21:48 +0800187}
188
Gao Xianga2c75c82019-09-04 10:08:59 +0800189static int erofs_fill_symlink(struct inode *inode, void *data,
190 unsigned int m_pofs)
Gao Xiang431339b2018-07-26 20:21:48 +0800191{
Gao Xianga5876e22019-09-04 10:08:56 +0800192 struct erofs_inode *vi = EROFS_I(inode);
Gao Xianga2c75c82019-09-04 10:08:59 +0800193 char *lnk;
Gao Xiang431339b2018-07-26 20:21:48 +0800194
Gao Xianga2c75c82019-09-04 10:08:59 +0800195 /* if it cannot be handled with fast symlink scheme */
196 if (vi->datalayout != EROFS_INODE_FLAT_INLINE ||
197 inode->i_size >= PAGE_SIZE) {
198 inode->i_op = &erofs_symlink_iops;
Gao Xiang431339b2018-07-26 20:21:48 +0800199 return 0;
Gao Xiang431339b2018-07-26 20:21:48 +0800200 }
Gao Xianga2c75c82019-09-04 10:08:59 +0800201
Gao Xiange2c71e72019-09-04 10:09:06 +0800202 lnk = kmalloc(inode->i_size + 1, GFP_KERNEL);
Gao Xianga2c75c82019-09-04 10:08:59 +0800203 if (!lnk)
204 return -ENOMEM;
205
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800206 m_pofs += vi->xattr_isize;
Gao Xianga2c75c82019-09-04 10:08:59 +0800207 /* inline symlink data shouldn't cross page boundary as well */
208 if (m_pofs + inode->i_size > PAGE_SIZE) {
209 kfree(lnk);
Gao Xiang4f761fa2019-09-04 10:09:09 +0800210 erofs_err(inode->i_sb,
211 "inline data cross block boundary @ nid %llu",
212 vi->nid);
Gao Xianga2c75c82019-09-04 10:08:59 +0800213 DBG_BUGON(1);
214 return -EFSCORRUPTED;
215 }
216
217 memcpy(lnk, data + m_pofs, inode->i_size);
218 lnk[inode->i_size] = '\0';
219
220 inode->i_link = lnk;
221 inode->i_op = &erofs_fast_symlink_iops;
Yue Hu55457452019-06-27 17:46:15 +0800222 return 0;
Gao Xiang431339b2018-07-26 20:21:48 +0800223}
224
Gao Xiang99634bf2019-09-04 10:09:05 +0800225static int erofs_fill_inode(struct inode *inode, int isdir)
Gao Xiang431339b2018-07-26 20:21:48 +0800226{
Gao Xianga5876e22019-09-04 10:08:56 +0800227 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang431339b2018-07-26 20:21:48 +0800228 struct page *page;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200229 unsigned int ofs;
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800230 int err = 0;
Gao Xiang431339b2018-07-26 20:21:48 +0800231
Chao Yu13f06f42018-07-26 20:21:55 +0800232 trace_erofs_fill_inode(inode, isdir);
Gao Xiang431339b2018-07-26 20:21:48 +0800233
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800234 /* read inode base data from disk */
235 page = erofs_read_inode(inode, &ofs);
236 if (IS_ERR(page))
Gao Xiang431339b2018-07-26 20:21:48 +0800237 return PTR_ERR(page);
Gao Xiang431339b2018-07-26 20:21:48 +0800238
Gao Xiang84947eb2019-09-04 10:09:08 +0800239 /* setup the new inode */
240 switch (inode->i_mode & S_IFMT) {
241 case S_IFREG:
242 inode->i_op = &erofs_generic_iops;
243 inode->i_fop = &generic_ro_fops;
244 break;
245 case S_IFDIR:
246 inode->i_op = &erofs_dir_iops;
247 inode->i_fop = &erofs_dir_fops;
248 break;
249 case S_IFLNK:
Gao Xiang0dcd3c92020-07-30 01:58:01 +0800250 err = erofs_fill_symlink(inode, page_address(page), ofs);
Gao Xiang84947eb2019-09-04 10:09:08 +0800251 if (err)
Gao Xiang431339b2018-07-26 20:21:48 +0800252 goto out_unlock;
Gao Xiang84947eb2019-09-04 10:09:08 +0800253 inode_nohighmem(inode);
254 break;
255 case S_IFCHR:
256 case S_IFBLK:
257 case S_IFIFO:
258 case S_IFSOCK:
259 inode->i_op = &erofs_generic_iops;
260 init_special_inode(inode, inode->i_mode, inode->i_rdev);
261 goto out_unlock;
262 default:
263 err = -EFSCORRUPTED;
264 goto out_unlock;
Gao Xiang431339b2018-07-26 20:21:48 +0800265 }
266
Gao Xiang84947eb2019-09-04 10:09:08 +0800267 if (erofs_inode_is_data_compressed(vi->datalayout)) {
268 err = z_erofs_fill_inode(inode);
269 goto out_unlock;
270 }
271 inode->i_mapping->a_ops = &erofs_raw_access_aops;
272
Gao Xiang431339b2018-07-26 20:21:48 +0800273out_unlock:
274 unlock_page(page);
275 put_page(page);
276 return err;
277}
278
Gao Xiang2abd7812018-10-09 22:07:13 +0800279/*
280 * erofs nid is 64bits, but i_ino is 'unsigned long', therefore
281 * we should do more for 32-bit platform to find the right inode.
282 */
Gao Xiang2abd7812018-10-09 22:07:13 +0800283static int erofs_ilookup_test_actor(struct inode *inode, void *opaque)
284{
285 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
286
Gao Xianga5876e22019-09-04 10:08:56 +0800287 return EROFS_I(inode)->nid == nid;
Gao Xiang2abd7812018-10-09 22:07:13 +0800288}
289
290static int erofs_iget_set_actor(struct inode *inode, void *opaque)
291{
292 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
293
294 inode->i_ino = erofs_inode_hash(nid);
295 return 0;
296}
Gao Xiang2abd7812018-10-09 22:07:13 +0800297
298static inline struct inode *erofs_iget_locked(struct super_block *sb,
299 erofs_nid_t nid)
300{
301 const unsigned long hashval = erofs_inode_hash(nid);
302
Gao Xiang2abd7812018-10-09 22:07:13 +0800303 return iget5_locked(sb, hashval, erofs_ilookup_test_actor,
304 erofs_iget_set_actor, &nid);
Gao Xiang2abd7812018-10-09 22:07:13 +0800305}
306
Gao Xiang431339b2018-07-26 20:21:48 +0800307struct inode *erofs_iget(struct super_block *sb,
Julian Merida447a3622019-03-18 20:58:41 -0300308 erofs_nid_t nid,
309 bool isdir)
Gao Xiang431339b2018-07-26 20:21:48 +0800310{
Gao Xiang2abd7812018-10-09 22:07:13 +0800311 struct inode *inode = erofs_iget_locked(sb, nid);
Gao Xiang431339b2018-07-26 20:21:48 +0800312
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800313 if (!inode)
Gao Xiang431339b2018-07-26 20:21:48 +0800314 return ERR_PTR(-ENOMEM);
315
316 if (inode->i_state & I_NEW) {
317 int err;
Gao Xianga5876e22019-09-04 10:08:56 +0800318 struct erofs_inode *vi = EROFS_I(inode);
Julio Bianco8af36472019-03-09 14:08:53 -0300319
Gao Xiang431339b2018-07-26 20:21:48 +0800320 vi->nid = nid;
321
Gao Xiang99634bf2019-09-04 10:09:05 +0800322 err = erofs_fill_inode(inode, isdir);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800323 if (!err)
Gao Xiang431339b2018-07-26 20:21:48 +0800324 unlock_new_inode(inode);
325 else {
326 iget_failed(inode);
327 inode = ERR_PTR(err);
328 }
329 }
330 return inode;
331}
332
Gao Xiang89f27ed2019-05-28 11:19:42 +0800333int erofs_getattr(const struct path *path, struct kstat *stat,
334 u32 request_mask, unsigned int query_flags)
335{
336 struct inode *const inode = d_inode(path->dentry);
Gao Xiang89f27ed2019-05-28 11:19:42 +0800337
Gao Xianga5876e22019-09-04 10:08:56 +0800338 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout))
Gao Xiang89f27ed2019-05-28 11:19:42 +0800339 stat->attributes |= STATX_ATTR_COMPRESSED;
340
341 stat->attributes |= STATX_ATTR_IMMUTABLE;
342 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
343 STATX_ATTR_IMMUTABLE);
344
345 generic_fillattr(inode, stat);
346 return 0;
347}
348
Gao Xiang60939822019-01-14 19:40:24 +0800349const struct inode_operations erofs_generic_iops = {
Gao Xiang89f27ed2019-05-28 11:19:42 +0800350 .getattr = erofs_getattr,
Gao Xiangb17500a2018-07-26 20:21:52 +0800351 .listxattr = erofs_listxattr,
Gao Xiang516c115c2019-01-29 16:35:20 +0800352 .get_acl = erofs_get_acl,
Gao Xiang60939822019-01-14 19:40:24 +0800353};
354
355const struct inode_operations erofs_symlink_iops = {
356 .get_link = page_get_link,
Gao Xiang89f27ed2019-05-28 11:19:42 +0800357 .getattr = erofs_getattr,
Gao Xiang60939822019-01-14 19:40:24 +0800358 .listxattr = erofs_listxattr,
Gao Xiang516c115c2019-01-29 16:35:20 +0800359 .get_acl = erofs_get_acl,
Gao Xiang60939822019-01-14 19:40:24 +0800360};
361
362const struct inode_operations erofs_fast_symlink_iops = {
363 .get_link = simple_get_link,
Gao Xiang89f27ed2019-05-28 11:19:42 +0800364 .getattr = erofs_getattr,
Gao Xiang60939822019-01-14 19:40:24 +0800365 .listxattr = erofs_listxattr,
Gao Xiang516c115c2019-01-29 16:35:20 +0800366 .get_acl = erofs_get_acl,
Gao Xiang60939822019-01-14 19:40:24 +0800367};
Gao Xiangb17500a2018-07-26 20:21:52 +0800368