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 #include <linux/compat.h>
9 #include <trace/events/erofs.h>
10
erofs_fill_symlink(struct inode * inode,void * kaddr,unsigned int m_pofs)11 static int erofs_fill_symlink(struct inode *inode, void *kaddr,
12 unsigned int m_pofs)
13 {
14 struct erofs_inode *vi = EROFS_I(inode);
15 loff_t off;
16
17 m_pofs += vi->xattr_isize;
18 /* check if it cannot be handled with fast symlink scheme */
19 if (vi->datalayout != EROFS_INODE_FLAT_INLINE ||
20 check_add_overflow(m_pofs, inode->i_size, &off) ||
21 off > i_blocksize(inode))
22 return 0;
23
24 inode->i_link = kmemdup_nul(kaddr + m_pofs, inode->i_size, GFP_KERNEL);
25 return inode->i_link ? 0 : -ENOMEM;
26 }
27
erofs_read_inode(struct inode * inode)28 static int erofs_read_inode(struct inode *inode)
29 {
30 struct super_block *sb = inode->i_sb;
31 erofs_blk_t blkaddr = erofs_blknr(sb, erofs_iloc(inode));
32 unsigned int ofs = erofs_blkoff(sb, erofs_iloc(inode));
33 bool in_mbox = erofs_inode_in_metabox(inode);
34 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
35 struct erofs_sb_info *sbi = EROFS_SB(sb);
36 erofs_blk_t addrmask = BIT_ULL(48) - 1;
37 struct erofs_inode *vi = EROFS_I(inode);
38 struct erofs_inode_extended *die, copied;
39 struct erofs_inode_compact *dic;
40 unsigned int ifmt;
41 void *ptr;
42 int err = 0;
43
44 ptr = erofs_read_metabuf(&buf, sb, erofs_pos(sb, blkaddr), in_mbox);
45 if (IS_ERR(ptr)) {
46 err = PTR_ERR(ptr);
47 erofs_err(sb, "failed to read inode meta block (nid: %llu): %d",
48 vi->nid, err);
49 goto err_out;
50 }
51
52 dic = ptr + ofs;
53 ifmt = le16_to_cpu(dic->i_format);
54 if (ifmt & ~EROFS_I_ALL) {
55 erofs_err(sb, "unsupported i_format %u of nid %llu",
56 ifmt, vi->nid);
57 err = -EOPNOTSUPP;
58 goto err_out;
59 }
60
61 vi->datalayout = erofs_inode_datalayout(ifmt);
62 if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
63 erofs_err(sb, "unsupported datalayout %u of nid %llu",
64 vi->datalayout, vi->nid);
65 err = -EOPNOTSUPP;
66 goto err_out;
67 }
68
69 switch (erofs_inode_version(ifmt)) {
70 case EROFS_INODE_LAYOUT_EXTENDED:
71 vi->inode_isize = sizeof(struct erofs_inode_extended);
72 /* check if the extended inode acrosses block boundary */
73 if (ofs + vi->inode_isize <= sb->s_blocksize) {
74 ofs += vi->inode_isize;
75 die = (struct erofs_inode_extended *)dic;
76 copied.i_u = die->i_u;
77 copied.i_nb = die->i_nb;
78 } else {
79 const unsigned int gotten = sb->s_blocksize - ofs;
80
81 memcpy(&copied, dic, gotten);
82 ptr = erofs_read_metabuf(&buf, sb,
83 erofs_pos(sb, blkaddr + 1), in_mbox);
84 if (IS_ERR(ptr)) {
85 err = PTR_ERR(ptr);
86 erofs_err(sb, "failed to read inode payload block (nid: %llu): %d",
87 vi->nid, err);
88 goto err_out;
89 }
90 ofs = vi->inode_isize - gotten;
91 memcpy((u8 *)&copied + gotten, ptr, ofs);
92 die = &copied;
93 }
94 vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
95
96 inode->i_mode = le16_to_cpu(die->i_mode);
97 i_uid_write(inode, le32_to_cpu(die->i_uid));
98 i_gid_write(inode, le32_to_cpu(die->i_gid));
99 set_nlink(inode, le32_to_cpu(die->i_nlink));
100 inode_set_mtime(inode, le64_to_cpu(die->i_mtime),
101 le32_to_cpu(die->i_mtime_nsec));
102
103 inode->i_size = le64_to_cpu(die->i_size);
104 break;
105 case EROFS_INODE_LAYOUT_COMPACT:
106 vi->inode_isize = sizeof(struct erofs_inode_compact);
107 ofs += vi->inode_isize;
108 vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
109
110 inode->i_mode = le16_to_cpu(dic->i_mode);
111 copied.i_u = dic->i_u;
112 i_uid_write(inode, le16_to_cpu(dic->i_uid));
113 i_gid_write(inode, le16_to_cpu(dic->i_gid));
114 if (!S_ISDIR(inode->i_mode) &&
115 ((ifmt >> EROFS_I_NLINK_1_BIT) & 1)) {
116 set_nlink(inode, 1);
117 copied.i_nb = dic->i_nb;
118 } else {
119 set_nlink(inode, le16_to_cpu(dic->i_nb.nlink));
120 copied.i_nb.startblk_hi = 0;
121 addrmask = BIT_ULL(32) - 1;
122 }
123 inode_set_mtime(inode, sbi->epoch + le32_to_cpu(dic->i_mtime),
124 sbi->fixed_nsec);
125
126 inode->i_size = le32_to_cpu(dic->i_size);
127 break;
128 default:
129 erofs_err(sb, "unsupported on-disk inode version %u of nid %llu",
130 erofs_inode_version(ifmt), vi->nid);
131 err = -EOPNOTSUPP;
132 goto err_out;
133 }
134
135 if (unlikely(inode->i_size < 0)) {
136 erofs_err(sb, "negative i_size @ nid %llu", vi->nid);
137 err = -EFSCORRUPTED;
138 goto err_out;
139 }
140 switch (inode->i_mode & S_IFMT) {
141 case S_IFDIR:
142 vi->dot_omitted = (ifmt >> EROFS_I_DOT_OMITTED_BIT) & 1;
143 fallthrough;
144 case S_IFREG:
145 case S_IFLNK:
146 vi->startblk = le32_to_cpu(copied.i_u.startblk_lo) |
147 ((u64)le16_to_cpu(copied.i_nb.startblk_hi) << 32);
148 if (vi->datalayout == EROFS_INODE_FLAT_PLAIN &&
149 !((vi->startblk ^ EROFS_NULL_ADDR) & addrmask))
150 vi->startblk = EROFS_NULL_ADDR;
151
152 if(S_ISLNK(inode->i_mode)) {
153 err = erofs_fill_symlink(inode, ptr, ofs);
154 if (err)
155 goto err_out;
156 }
157 break;
158 case S_IFCHR:
159 case S_IFBLK:
160 inode->i_rdev = new_decode_dev(le32_to_cpu(copied.i_u.rdev));
161 break;
162 case S_IFIFO:
163 case S_IFSOCK:
164 inode->i_rdev = 0;
165 break;
166 default:
167 erofs_err(sb, "bogus i_mode (%o) @ nid %llu", inode->i_mode,
168 vi->nid);
169 err = -EFSCORRUPTED;
170 goto err_out;
171 }
172
173 if (erofs_inode_is_data_compressed(vi->datalayout))
174 inode->i_blocks = le32_to_cpu(copied.i_u.blocks_lo) <<
175 (sb->s_blocksize_bits - 9);
176 else
177 inode->i_blocks = round_up(inode->i_size, sb->s_blocksize) >> 9;
178
179 if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
180 /* fill chunked inode summary info */
181 vi->chunkformat = le16_to_cpu(copied.i_u.c.format);
182 if (vi->chunkformat & ~EROFS_CHUNK_FORMAT_ALL) {
183 erofs_err(sb, "unsupported chunk format %x of nid %llu",
184 vi->chunkformat, vi->nid);
185 err = -EOPNOTSUPP;
186 goto err_out;
187 }
188 vi->chunkbits = sb->s_blocksize_bits +
189 (vi->chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
190 }
191 inode_set_atime_to_ts(inode,
192 inode_set_ctime_to_ts(inode, inode_get_mtime(inode)));
193
194 inode->i_flags &= ~S_DAX;
195 if (test_opt(&sbi->opt, DAX_ALWAYS) && S_ISREG(inode->i_mode) &&
196 (vi->datalayout == EROFS_INODE_FLAT_PLAIN ||
197 vi->datalayout == EROFS_INODE_CHUNK_BASED))
198 inode->i_flags |= S_DAX;
199 err_out:
200 erofs_put_metabuf(&buf);
201 return err;
202 }
203
erofs_fill_inode(struct inode * inode)204 static int erofs_fill_inode(struct inode *inode)
205 {
206 struct erofs_inode *vi = EROFS_I(inode);
207 int err;
208
209 trace_erofs_fill_inode(inode);
210 err = erofs_read_inode(inode);
211 if (err)
212 return err;
213
214 switch (inode->i_mode & S_IFMT) {
215 case S_IFREG:
216 inode->i_op = &erofs_generic_iops;
217 inode->i_fop = &erofs_file_fops;
218 break;
219 case S_IFDIR:
220 inode->i_op = &erofs_dir_iops;
221 inode->i_fop = &erofs_dir_fops;
222 inode_nohighmem(inode);
223 break;
224 case S_IFLNK:
225 if (inode->i_link)
226 inode->i_op = &erofs_fast_symlink_iops;
227 else
228 inode->i_op = &erofs_symlink_iops;
229 inode_nohighmem(inode);
230 break;
231 default:
232 inode->i_op = &erofs_generic_iops;
233 init_special_inode(inode, inode->i_mode, inode->i_rdev);
234 return 0;
235 }
236
237 mapping_set_large_folios(inode->i_mapping);
238 if (erofs_inode_is_data_compressed(vi->datalayout)) {
239 #ifdef CONFIG_EROFS_FS_ZIP
240 DO_ONCE_LITE_IF(inode->i_blkbits != PAGE_SHIFT,
241 erofs_info, inode->i_sb,
242 "EXPERIMENTAL EROFS subpage compressed block support in use. Use at your own risk!");
243 inode->i_mapping->a_ops = &z_erofs_aops;
244 #else
245 err = -EOPNOTSUPP;
246 #endif
247 } else {
248 inode->i_mapping->a_ops = &erofs_aops;
249 #ifdef CONFIG_EROFS_FS_ONDEMAND
250 if (erofs_is_fscache_mode(inode->i_sb))
251 inode->i_mapping->a_ops = &erofs_fscache_access_aops;
252 #endif
253 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
254 if (erofs_is_fileio_mode(EROFS_SB(inode->i_sb)))
255 inode->i_mapping->a_ops = &erofs_fileio_aops;
256 #endif
257 }
258
259 return err;
260 }
261
262 /*
263 * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
264 * so that it will fit.
265 */
erofs_squash_ino(struct super_block * sb,erofs_nid_t nid)266 static ino_t erofs_squash_ino(struct super_block *sb, erofs_nid_t nid)
267 {
268 u64 ino64 = erofs_nid_to_ino64(EROFS_SB(sb), nid);
269
270 if (sizeof(ino_t) < sizeof(erofs_nid_t))
271 ino64 ^= ino64 >> (sizeof(erofs_nid_t) - sizeof(ino_t)) * 8;
272 return (ino_t)ino64;
273 }
274
erofs_iget5_eq(struct inode * inode,void * opaque)275 static int erofs_iget5_eq(struct inode *inode, void *opaque)
276 {
277 return EROFS_I(inode)->nid == *(erofs_nid_t *)opaque;
278 }
279
erofs_iget5_set(struct inode * inode,void * opaque)280 static int erofs_iget5_set(struct inode *inode, void *opaque)
281 {
282 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
283
284 inode->i_ino = erofs_squash_ino(inode->i_sb, nid);
285 EROFS_I(inode)->nid = nid;
286 return 0;
287 }
288
erofs_iget(struct super_block * sb,erofs_nid_t nid)289 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid)
290 {
291 struct inode *inode;
292
293 inode = iget5_locked(sb, erofs_squash_ino(sb, nid), erofs_iget5_eq,
294 erofs_iget5_set, &nid);
295 if (!inode)
296 return ERR_PTR(-ENOMEM);
297
298 if (inode->i_state & I_NEW) {
299 int err = erofs_fill_inode(inode);
300
301 if (err) {
302 iget_failed(inode);
303 return ERR_PTR(err);
304 }
305 unlock_new_inode(inode);
306 }
307 return inode;
308 }
309
erofs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)310 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
311 struct kstat *stat, u32 request_mask,
312 unsigned int query_flags)
313 {
314 struct inode *const inode = d_inode(path->dentry);
315 struct block_device *bdev = inode->i_sb->s_bdev;
316 bool compressed =
317 erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout);
318
319 if (compressed)
320 stat->attributes |= STATX_ATTR_COMPRESSED;
321 stat->attributes |= STATX_ATTR_IMMUTABLE;
322 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
323 STATX_ATTR_IMMUTABLE);
324
325 /*
326 * Return the DIO alignment restrictions if requested.
327 *
328 * In EROFS, STATX_DIOALIGN is only supported in bdev-based mode
329 * and uncompressed inodes, otherwise we report no DIO support.
330 */
331 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
332 stat->result_mask |= STATX_DIOALIGN;
333 if (bdev && !compressed) {
334 stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
335 stat->dio_offset_align = bdev_logical_block_size(bdev);
336 }
337 }
338 generic_fillattr(idmap, request_mask, inode, stat);
339 return 0;
340 }
341
erofs_ioctl_get_volume_label(struct inode * inode,void __user * arg)342 static int erofs_ioctl_get_volume_label(struct inode *inode, void __user *arg)
343 {
344 struct erofs_sb_info *sbi = EROFS_I_SB(inode);
345 int ret;
346
347 if (!sbi->volume_name)
348 ret = clear_user(arg, 1);
349 else
350 ret = copy_to_user(arg, sbi->volume_name,
351 strlen(sbi->volume_name));
352 return ret ? -EFAULT : 0;
353 }
354
erofs_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)355 long erofs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
356 {
357 struct inode *inode = file_inode(filp);
358 void __user *argp = (void __user *)arg;
359
360 switch (cmd) {
361 case FS_IOC_GETFSLABEL:
362 return erofs_ioctl_get_volume_label(inode, argp);
363 default:
364 return -ENOTTY;
365 }
366 }
367
368 #ifdef CONFIG_COMPAT
erofs_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)369 long erofs_compat_ioctl(struct file *filp, unsigned int cmd,
370 unsigned long arg)
371 {
372 return erofs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
373 }
374 #endif
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