blob: 3350ab65d892c56cc62d67249dbfdabe16fb4644 [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.
4 * http://www.huawei.com/
5 * 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 Xiang431339b2018-07-26 20:21:48 +080011/* no locking */
Gao Xiang99634bf2019-09-04 10:09:05 +080012static int erofs_read_inode(struct inode *inode, void *data)
Gao Xiang431339b2018-07-26 20:21:48 +080013{
Gao Xianga5876e22019-09-04 10:08:56 +080014 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang8a765682019-09-04 10:08:54 +080015 struct erofs_inode_compact *dic = data;
16 struct erofs_inode_extended *die;
17
18 const unsigned int ifmt = le16_to_cpu(dic->i_format);
19 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
Gao Xiangfe6d9872019-05-28 11:19:43 +080020 erofs_blk_t nblks = 0;
Gao Xiang431339b2018-07-26 20:21:48 +080021
Gao Xiang8a765682019-09-04 10:08:54 +080022 vi->datalayout = erofs_inode_datalayout(ifmt);
Gao Xiang431339b2018-07-26 20:21:48 +080023
Gao Xiang8a765682019-09-04 10:08:54 +080024 if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080025 erofs_err(inode->i_sb, "unsupported datalayout %u of nid %llu",
26 vi->datalayout, vi->nid);
Gao Xiang431339b2018-07-26 20:21:48 +080027 DBG_BUGON(1);
Gao Xiang382329a2019-08-14 18:37:04 +080028 return -EOPNOTSUPP;
Gao Xiang431339b2018-07-26 20:21:48 +080029 }
30
Gao Xiang8a765682019-09-04 10:08:54 +080031 switch (erofs_inode_version(ifmt)) {
32 case EROFS_INODE_LAYOUT_EXTENDED:
33 die = data;
Gao Xiang431339b2018-07-26 20:21:48 +080034
Gao Xiang8a765682019-09-04 10:08:54 +080035 vi->inode_isize = sizeof(struct erofs_inode_extended);
36 vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
Gao Xiang431339b2018-07-26 20:21:48 +080037
Gao Xiang8a765682019-09-04 10:08:54 +080038 inode->i_mode = le16_to_cpu(die->i_mode);
39 switch (inode->i_mode & S_IFMT) {
40 case S_IFREG:
41 case S_IFDIR:
42 case S_IFLNK:
43 vi->raw_blkaddr = le32_to_cpu(die->i_u.raw_blkaddr);
44 break;
45 case S_IFCHR:
46 case S_IFBLK:
Chao Yud5beb31b2018-07-26 20:21:53 +080047 inode->i_rdev =
Gao Xiang8a765682019-09-04 10:08:54 +080048 new_decode_dev(le32_to_cpu(die->i_u.rdev));
49 break;
50 case S_IFIFO:
51 case S_IFSOCK:
Chao Yud5beb31b2018-07-26 20:21:53 +080052 inode->i_rdev = 0;
Gao Xiang8a765682019-09-04 10:08:54 +080053 break;
54 default:
Gao Xianga6b9b1d2019-08-14 18:37:03 +080055 goto bogusimode;
Gao Xiang8a765682019-09-04 10:08:54 +080056 }
57 i_uid_write(inode, le32_to_cpu(die->i_uid));
58 i_gid_write(inode, le32_to_cpu(die->i_gid));
59 set_nlink(inode, le32_to_cpu(die->i_nlink));
Gao Xiang431339b2018-07-26 20:21:48 +080060
61 /* ns timestamp */
62 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec =
Gao Xiang8a765682019-09-04 10:08:54 +080063 le64_to_cpu(die->i_ctime);
Gao Xiang431339b2018-07-26 20:21:48 +080064 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec =
Gao Xiang8a765682019-09-04 10:08:54 +080065 le32_to_cpu(die->i_ctime_nsec);
Gao Xiang431339b2018-07-26 20:21:48 +080066
Gao Xiang8a765682019-09-04 10:08:54 +080067 inode->i_size = le64_to_cpu(die->i_size);
Gao Xiangfe6d9872019-05-28 11:19:43 +080068
69 /* total blocks for compressed files */
Gao Xiang8a765682019-09-04 10:08:54 +080070 if (erofs_inode_is_data_compressed(vi->datalayout))
71 nblks = le32_to_cpu(die->i_u.compressed_blocks);
72 break;
73 case EROFS_INODE_LAYOUT_COMPACT:
74 vi->inode_isize = sizeof(struct erofs_inode_compact);
75 vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
Gao Xiang431339b2018-07-26 20:21:48 +080076
Gao Xiang8a765682019-09-04 10:08:54 +080077 inode->i_mode = le16_to_cpu(dic->i_mode);
78 switch (inode->i_mode & S_IFMT) {
79 case S_IFREG:
80 case S_IFDIR:
81 case S_IFLNK:
82 vi->raw_blkaddr = le32_to_cpu(dic->i_u.raw_blkaddr);
83 break;
84 case S_IFCHR:
85 case S_IFBLK:
Chao Yud5beb31b2018-07-26 20:21:53 +080086 inode->i_rdev =
Gao Xiang8a765682019-09-04 10:08:54 +080087 new_decode_dev(le32_to_cpu(dic->i_u.rdev));
88 break;
89 case S_IFIFO:
90 case S_IFSOCK:
Chao Yud5beb31b2018-07-26 20:21:53 +080091 inode->i_rdev = 0;
Gao Xiang8a765682019-09-04 10:08:54 +080092 break;
93 default:
Gao Xianga6b9b1d2019-08-14 18:37:03 +080094 goto bogusimode;
Gao Xiang8a765682019-09-04 10:08:54 +080095 }
96 i_uid_write(inode, le16_to_cpu(dic->i_uid));
97 i_gid_write(inode, le16_to_cpu(dic->i_gid));
98 set_nlink(inode, le16_to_cpu(dic->i_nlink));
Gao Xiang431339b2018-07-26 20:21:48 +080099
100 /* use build time to derive all file time */
101 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec =
102 sbi->build_time;
103 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec =
104 sbi->build_time_nsec;
105
Gao Xiang8a765682019-09-04 10:08:54 +0800106 inode->i_size = le32_to_cpu(dic->i_size);
107 if (erofs_inode_is_data_compressed(vi->datalayout))
108 nblks = le32_to_cpu(dic->i_u.compressed_blocks);
109 break;
110 default:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800111 erofs_err(inode->i_sb,
112 "unsupported on-disk inode version %u of nid %llu",
113 erofs_inode_version(ifmt), vi->nid);
Gao Xiang431339b2018-07-26 20:21:48 +0800114 DBG_BUGON(1);
Gao Xiang382329a2019-08-14 18:37:04 +0800115 return -EOPNOTSUPP;
Gao Xiang431339b2018-07-26 20:21:48 +0800116 }
117
Gao Xiangfe6d9872019-05-28 11:19:43 +0800118 if (!nblks)
119 /* measure inode.i_blocks as generic filesystems */
120 inode->i_blocks = roundup(inode->i_size, EROFS_BLKSIZ) >> 9;
121 else
122 inode->i_blocks = nblks << LOG_SECTORS_PER_BLOCK;
Gao Xiang431339b2018-07-26 20:21:48 +0800123 return 0;
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800124
125bogusimode:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800126 erofs_err(inode->i_sb, "bogus i_mode (%o) @ nid %llu",
127 inode->i_mode, vi->nid);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800128 DBG_BUGON(1);
129 return -EFSCORRUPTED;
Gao Xiang431339b2018-07-26 20:21:48 +0800130}
131
Gao Xianga2c75c812019-09-04 10:08:59 +0800132static int erofs_fill_symlink(struct inode *inode, void *data,
133 unsigned int m_pofs)
Gao Xiang431339b2018-07-26 20:21:48 +0800134{
Gao Xianga5876e22019-09-04 10:08:56 +0800135 struct erofs_inode *vi = EROFS_I(inode);
Gao Xianga2c75c812019-09-04 10:08:59 +0800136 char *lnk;
Gao Xiang431339b2018-07-26 20:21:48 +0800137
Gao Xianga2c75c812019-09-04 10:08:59 +0800138 /* if it cannot be handled with fast symlink scheme */
139 if (vi->datalayout != EROFS_INODE_FLAT_INLINE ||
140 inode->i_size >= PAGE_SIZE) {
141 inode->i_op = &erofs_symlink_iops;
Gao Xiang431339b2018-07-26 20:21:48 +0800142 return 0;
Gao Xiang431339b2018-07-26 20:21:48 +0800143 }
Gao Xianga2c75c812019-09-04 10:08:59 +0800144
Gao Xiange2c71e72019-09-04 10:09:06 +0800145 lnk = kmalloc(inode->i_size + 1, GFP_KERNEL);
Gao Xianga2c75c812019-09-04 10:08:59 +0800146 if (!lnk)
147 return -ENOMEM;
148
149 m_pofs += vi->inode_isize + vi->xattr_isize;
150 /* inline symlink data shouldn't cross page boundary as well */
151 if (m_pofs + inode->i_size > PAGE_SIZE) {
152 kfree(lnk);
Gao Xiang4f761fa2019-09-04 10:09:09 +0800153 erofs_err(inode->i_sb,
154 "inline data cross block boundary @ nid %llu",
155 vi->nid);
Gao Xianga2c75c812019-09-04 10:08:59 +0800156 DBG_BUGON(1);
157 return -EFSCORRUPTED;
158 }
159
160 memcpy(lnk, data + m_pofs, inode->i_size);
161 lnk[inode->i_size] = '\0';
162
163 inode->i_link = lnk;
164 inode->i_op = &erofs_fast_symlink_iops;
Yue Hu55457452019-06-27 17:46:15 +0800165 return 0;
Gao Xiang431339b2018-07-26 20:21:48 +0800166}
167
Gao Xiang99634bf2019-09-04 10:09:05 +0800168static int erofs_fill_inode(struct inode *inode, int isdir)
Gao Xiang431339b2018-07-26 20:21:48 +0800169{
Gao Xiang4f761fa2019-09-04 10:09:09 +0800170 struct super_block *sb = inode->i_sb;
Gao Xianga5876e22019-09-04 10:08:56 +0800171 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiang431339b2018-07-26 20:21:48 +0800172 struct page *page;
173 void *data;
174 int err;
175 erofs_blk_t blkaddr;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200176 unsigned int ofs;
Pratik Shindea7255352019-08-14 02:08:40 +0530177 erofs_off_t inode_loc;
Gao Xiang431339b2018-07-26 20:21:48 +0800178
Chao Yu13f06f42018-07-26 20:21:55 +0800179 trace_erofs_fill_inode(inode, isdir);
Gao Xiang4f761fa2019-09-04 10:09:09 +0800180 inode_loc = iloc(EROFS_SB(sb), vi->nid);
Pratik Shindea7255352019-08-14 02:08:40 +0530181 blkaddr = erofs_blknr(inode_loc);
182 ofs = erofs_blkoff(inode_loc);
Gao Xiang431339b2018-07-26 20:21:48 +0800183
Gao Xiang4f761fa2019-09-04 10:09:09 +0800184 erofs_dbg("%s, reading inode nid %llu at %u of blkaddr %u",
185 __func__, vi->nid, ofs, blkaddr);
Gao Xiang431339b2018-07-26 20:21:48 +0800186
Gao Xiang4f761fa2019-09-04 10:09:09 +0800187 page = erofs_get_meta_page(sb, blkaddr);
Gao Xiang431339b2018-07-26 20:21:48 +0800188
189 if (IS_ERR(page)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800190 erofs_err(sb, "failed to get inode (nid: %llu) page, err %ld",
191 vi->nid, PTR_ERR(page));
Gao Xiang431339b2018-07-26 20:21:48 +0800192 return PTR_ERR(page);
193 }
194
Gao Xiang8b987bc2018-12-05 21:23:13 +0800195 DBG_BUGON(!PageUptodate(page));
Gao Xiang431339b2018-07-26 20:21:48 +0800196 data = page_address(page);
197
Gao Xiang99634bf2019-09-04 10:09:05 +0800198 err = erofs_read_inode(inode, data + ofs);
Gao Xiang84947eb62019-09-04 10:09:08 +0800199 if (err)
200 goto out_unlock;
Gao Xiang431339b2018-07-26 20:21:48 +0800201
Gao Xiang84947eb62019-09-04 10:09:08 +0800202 /* setup the new inode */
203 switch (inode->i_mode & S_IFMT) {
204 case S_IFREG:
205 inode->i_op = &erofs_generic_iops;
206 inode->i_fop = &generic_ro_fops;
207 break;
208 case S_IFDIR:
209 inode->i_op = &erofs_dir_iops;
210 inode->i_fop = &erofs_dir_fops;
211 break;
212 case S_IFLNK:
213 err = erofs_fill_symlink(inode, data, ofs);
214 if (err)
Gao Xiang431339b2018-07-26 20:21:48 +0800215 goto out_unlock;
Gao Xiang84947eb62019-09-04 10:09:08 +0800216 inode_nohighmem(inode);
217 break;
218 case S_IFCHR:
219 case S_IFBLK:
220 case S_IFIFO:
221 case S_IFSOCK:
222 inode->i_op = &erofs_generic_iops;
223 init_special_inode(inode, inode->i_mode, inode->i_rdev);
224 goto out_unlock;
225 default:
226 err = -EFSCORRUPTED;
227 goto out_unlock;
Gao Xiang431339b2018-07-26 20:21:48 +0800228 }
229
Gao Xiang84947eb62019-09-04 10:09:08 +0800230 if (erofs_inode_is_data_compressed(vi->datalayout)) {
231 err = z_erofs_fill_inode(inode);
232 goto out_unlock;
233 }
234 inode->i_mapping->a_ops = &erofs_raw_access_aops;
235
Gao Xiang431339b2018-07-26 20:21:48 +0800236out_unlock:
237 unlock_page(page);
238 put_page(page);
239 return err;
240}
241
Gao Xiang2abd7812018-10-09 22:07:13 +0800242/*
243 * erofs nid is 64bits, but i_ino is 'unsigned long', therefore
244 * we should do more for 32-bit platform to find the right inode.
245 */
Gao Xiang2abd7812018-10-09 22:07:13 +0800246static int erofs_ilookup_test_actor(struct inode *inode, void *opaque)
247{
248 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
249
Gao Xianga5876e22019-09-04 10:08:56 +0800250 return EROFS_I(inode)->nid == nid;
Gao Xiang2abd7812018-10-09 22:07:13 +0800251}
252
253static int erofs_iget_set_actor(struct inode *inode, void *opaque)
254{
255 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
256
257 inode->i_ino = erofs_inode_hash(nid);
258 return 0;
259}
Gao Xiang2abd7812018-10-09 22:07:13 +0800260
261static inline struct inode *erofs_iget_locked(struct super_block *sb,
262 erofs_nid_t nid)
263{
264 const unsigned long hashval = erofs_inode_hash(nid);
265
Gao Xiang2abd7812018-10-09 22:07:13 +0800266 return iget5_locked(sb, hashval, erofs_ilookup_test_actor,
267 erofs_iget_set_actor, &nid);
Gao Xiang2abd7812018-10-09 22:07:13 +0800268}
269
Gao Xiang431339b2018-07-26 20:21:48 +0800270struct inode *erofs_iget(struct super_block *sb,
Julian Merida447a3622019-03-18 20:58:41 -0300271 erofs_nid_t nid,
272 bool isdir)
Gao Xiang431339b2018-07-26 20:21:48 +0800273{
Gao Xiang2abd7812018-10-09 22:07:13 +0800274 struct inode *inode = erofs_iget_locked(sb, nid);
Gao Xiang431339b2018-07-26 20:21:48 +0800275
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800276 if (!inode)
Gao Xiang431339b2018-07-26 20:21:48 +0800277 return ERR_PTR(-ENOMEM);
278
279 if (inode->i_state & I_NEW) {
280 int err;
Gao Xianga5876e22019-09-04 10:08:56 +0800281 struct erofs_inode *vi = EROFS_I(inode);
Julio Bianco8af36472019-03-09 14:08:53 -0300282
Gao Xiang431339b2018-07-26 20:21:48 +0800283 vi->nid = nid;
284
Gao Xiang99634bf2019-09-04 10:09:05 +0800285 err = erofs_fill_inode(inode, isdir);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800286 if (!err)
Gao Xiang431339b2018-07-26 20:21:48 +0800287 unlock_new_inode(inode);
288 else {
289 iget_failed(inode);
290 inode = ERR_PTR(err);
291 }
292 }
293 return inode;
294}
295
Gao Xiang89f27ed2019-05-28 11:19:42 +0800296int erofs_getattr(const struct path *path, struct kstat *stat,
297 u32 request_mask, unsigned int query_flags)
298{
299 struct inode *const inode = d_inode(path->dentry);
Gao Xiang89f27ed2019-05-28 11:19:42 +0800300
Gao Xianga5876e22019-09-04 10:08:56 +0800301 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout))
Gao Xiang89f27ed2019-05-28 11:19:42 +0800302 stat->attributes |= STATX_ATTR_COMPRESSED;
303
304 stat->attributes |= STATX_ATTR_IMMUTABLE;
305 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
306 STATX_ATTR_IMMUTABLE);
307
308 generic_fillattr(inode, stat);
309 return 0;
310}
311
Gao Xiang60939822019-01-14 19:40:24 +0800312const struct inode_operations erofs_generic_iops = {
Gao Xiang89f27ed2019-05-28 11:19:42 +0800313 .getattr = erofs_getattr,
Gao Xiangb17500a2018-07-26 20:21:52 +0800314#ifdef CONFIG_EROFS_FS_XATTR
Gao Xiangb17500a2018-07-26 20:21:52 +0800315 .listxattr = erofs_listxattr,
Gao Xiangb17500a2018-07-26 20:21:52 +0800316#endif
Gao Xiang516c115c2019-01-29 16:35:20 +0800317 .get_acl = erofs_get_acl,
Gao Xiang60939822019-01-14 19:40:24 +0800318};
319
320const struct inode_operations erofs_symlink_iops = {
321 .get_link = page_get_link,
Gao Xiang89f27ed2019-05-28 11:19:42 +0800322 .getattr = erofs_getattr,
Gao Xiang60939822019-01-14 19:40:24 +0800323#ifdef CONFIG_EROFS_FS_XATTR
324 .listxattr = erofs_listxattr,
325#endif
Gao Xiang516c115c2019-01-29 16:35:20 +0800326 .get_acl = erofs_get_acl,
Gao Xiang60939822019-01-14 19:40:24 +0800327};
328
329const struct inode_operations erofs_fast_symlink_iops = {
330 .get_link = simple_get_link,
Gao Xiang89f27ed2019-05-28 11:19:42 +0800331 .getattr = erofs_getattr,
Gao Xiang60939822019-01-14 19:40:24 +0800332#ifdef CONFIG_EROFS_FS_XATTR
333 .listxattr = erofs_listxattr,
334#endif
Gao Xiang516c115c2019-01-29 16:35:20 +0800335 .get_acl = erofs_get_acl,
Gao Xiang60939822019-01-14 19:40:24 +0800336};
Gao Xiangb17500a2018-07-26 20:21:52 +0800337