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 <linux/statfs.h>
8 #include <linux/seq_file.h>
9 #include <linux/crc32c.h>
10 #include <linux/fs_context.h>
11 #include <linux/fs_parser.h>
12 #include <linux/exportfs.h>
13 #include <linux/backing-dev.h>
14 #include <linux/pseudo_fs.h>
15 #include "xattr.h"
16
17 #define CREATE_TRACE_POINTS
18 #include <trace/events/erofs.h>
19
20 static struct kmem_cache *erofs_inode_cachep __read_mostly;
21
_erofs_printk(struct super_block * sb,const char * fmt,...)22 void _erofs_printk(struct super_block *sb, const char *fmt, ...)
23 {
24 struct va_format vaf;
25 va_list args;
26 int level;
27
28 va_start(args, fmt);
29
30 level = printk_get_level(fmt);
31 vaf.fmt = printk_skip_level(fmt);
32 vaf.va = &args;
33 if (sb)
34 printk("%c%cerofs (device %s): %pV",
35 KERN_SOH_ASCII, level, sb->s_id, &vaf);
36 else
37 printk("%c%cerofs: %pV", KERN_SOH_ASCII, level, &vaf);
38 va_end(args);
39 }
40
erofs_superblock_csum_verify(struct super_block * sb,void * sbdata)41 static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
42 {
43 struct erofs_super_block *dsb = sbdata + EROFS_SUPER_OFFSET;
44 u32 len = 1 << EROFS_SB(sb)->blkszbits, crc;
45
46 if (len > EROFS_SUPER_OFFSET)
47 len -= EROFS_SUPER_OFFSET;
48 len -= offsetof(struct erofs_super_block, checksum) +
49 sizeof(dsb->checksum);
50
51 /* skip .magic(pre-verified) and .checksum(0) fields */
52 crc = crc32c(0x5045B54A, (&dsb->checksum) + 1, len);
53 if (crc == le32_to_cpu(dsb->checksum))
54 return 0;
55 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
56 crc, le32_to_cpu(dsb->checksum));
57 return -EBADMSG;
58 }
59
erofs_inode_init_once(void * ptr)60 static void erofs_inode_init_once(void *ptr)
61 {
62 struct erofs_inode *vi = ptr;
63
64 inode_init_once(&vi->vfs_inode);
65 }
66
erofs_alloc_inode(struct super_block * sb)67 static struct inode *erofs_alloc_inode(struct super_block *sb)
68 {
69 struct erofs_inode *vi =
70 alloc_inode_sb(sb, erofs_inode_cachep, GFP_KERNEL);
71
72 if (!vi)
73 return NULL;
74
75 /* zero out everything except vfs_inode */
76 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
77 return &vi->vfs_inode;
78 }
79
erofs_free_inode(struct inode * inode)80 static void erofs_free_inode(struct inode *inode)
81 {
82 struct erofs_inode *vi = EROFS_I(inode);
83
84 if (inode->i_op == &erofs_fast_symlink_iops)
85 kfree(inode->i_link);
86 kfree(vi->xattr_shared_xattrs);
87 kmem_cache_free(erofs_inode_cachep, vi);
88 }
89
90 /* read variable-sized metadata, offset will be aligned by 4-byte */
erofs_read_metadata(struct super_block * sb,struct erofs_buf * buf,erofs_off_t * offset,int * lengthp)91 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
92 erofs_off_t *offset, int *lengthp)
93 {
94 u8 *buffer, *ptr;
95 int len, i, cnt;
96
97 *offset = round_up(*offset, 4);
98 ptr = erofs_bread(buf, *offset, true);
99 if (IS_ERR(ptr))
100 return ptr;
101
102 len = le16_to_cpu(*(__le16 *)ptr);
103 if (!len)
104 len = U16_MAX + 1;
105 buffer = kmalloc(len, GFP_KERNEL);
106 if (!buffer)
107 return ERR_PTR(-ENOMEM);
108 *offset += sizeof(__le16);
109 *lengthp = len;
110
111 for (i = 0; i < len; i += cnt) {
112 cnt = min_t(int, sb->s_blocksize - erofs_blkoff(sb, *offset),
113 len - i);
114 ptr = erofs_bread(buf, *offset, true);
115 if (IS_ERR(ptr)) {
116 kfree(buffer);
117 return ptr;
118 }
119 memcpy(buffer + i, ptr, cnt);
120 *offset += cnt;
121 }
122 return buffer;
123 }
124
erofs_init_device(struct erofs_buf * buf,struct super_block * sb,struct erofs_device_info * dif,erofs_off_t * pos)125 static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
126 struct erofs_device_info *dif, erofs_off_t *pos)
127 {
128 struct erofs_sb_info *sbi = EROFS_SB(sb);
129 struct erofs_fscache *fscache;
130 struct erofs_deviceslot *dis;
131 struct file *file;
132
133 dis = erofs_read_metabuf(buf, sb, *pos, false);
134 if (IS_ERR(dis))
135 return PTR_ERR(dis);
136
137 if (!sbi->devs->flatdev && !dif->path) {
138 if (!dis->tag[0]) {
139 erofs_err(sb, "empty device tag @ pos %llu", *pos);
140 return -EINVAL;
141 }
142 dif->path = kmemdup_nul(dis->tag, sizeof(dis->tag), GFP_KERNEL);
143 if (!dif->path)
144 return -ENOMEM;
145 }
146
147 if (erofs_is_fscache_mode(sb)) {
148 fscache = erofs_fscache_register_cookie(sb, dif->path, 0);
149 if (IS_ERR(fscache))
150 return PTR_ERR(fscache);
151 dif->fscache = fscache;
152 } else if (!sbi->devs->flatdev) {
153 file = erofs_is_fileio_mode(sbi) ?
154 filp_open(dif->path, O_RDONLY | O_LARGEFILE, 0) :
155 bdev_file_open_by_path(dif->path,
156 BLK_OPEN_READ, sb->s_type, NULL);
157 if (IS_ERR(file)) {
158 if (file == ERR_PTR(-ENOTBLK))
159 return -EINVAL;
160 return PTR_ERR(file);
161 }
162
163 if (!erofs_is_fileio_mode(sbi)) {
164 dif->dax_dev = fs_dax_get_by_bdev(file_bdev(file),
165 &dif->dax_part_off, NULL, NULL);
166 } else if (!S_ISREG(file_inode(file)->i_mode)) {
167 fput(file);
168 return -EINVAL;
169 }
170 if (!dif->dax_dev && test_opt(&sbi->opt, DAX_ALWAYS)) {
171 erofs_info(sb, "DAX unsupported by %s. Turning off DAX.",
172 dif->path);
173 clear_opt(&sbi->opt, DAX_ALWAYS);
174 }
175 dif->file = file;
176 }
177
178 dif->blocks = le32_to_cpu(dis->blocks_lo);
179 dif->uniaddr = le32_to_cpu(dis->uniaddr_lo);
180 sbi->total_blocks += dif->blocks;
181 *pos += EROFS_DEVT_SLOT_SIZE;
182 return 0;
183 }
184
erofs_scan_devices(struct super_block * sb,struct erofs_super_block * dsb)185 static int erofs_scan_devices(struct super_block *sb,
186 struct erofs_super_block *dsb)
187 {
188 struct erofs_sb_info *sbi = EROFS_SB(sb);
189 unsigned int ondisk_extradevs;
190 erofs_off_t pos;
191 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
192 struct erofs_device_info *dif;
193 int id, err = 0;
194
195 sbi->total_blocks = sbi->dif0.blocks;
196 if (!erofs_sb_has_device_table(sbi))
197 ondisk_extradevs = 0;
198 else
199 ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
200
201 if (sbi->devs->extra_devices &&
202 ondisk_extradevs != sbi->devs->extra_devices) {
203 erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
204 ondisk_extradevs, sbi->devs->extra_devices);
205 return -EINVAL;
206 }
207
208 if (test_opt(&sbi->opt, DAX_ALWAYS) && !sbi->dif0.dax_dev) {
209 erofs_info(sb, "DAX unsupported by block device. Turning off DAX.");
210 clear_opt(&sbi->opt, DAX_ALWAYS);
211 }
212 if (!ondisk_extradevs)
213 return 0;
214
215 if (!sbi->devs->extra_devices && !erofs_is_fscache_mode(sb))
216 sbi->devs->flatdev = true;
217
218 sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
219 pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
220 down_read(&sbi->devs->rwsem);
221 if (sbi->devs->extra_devices) {
222 idr_for_each_entry(&sbi->devs->tree, dif, id) {
223 err = erofs_init_device(&buf, sb, dif, &pos);
224 if (err)
225 break;
226 }
227 } else {
228 for (id = 0; id < ondisk_extradevs; id++) {
229 dif = kzalloc_obj(*dif);
230 if (!dif) {
231 err = -ENOMEM;
232 break;
233 }
234
235 err = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
236 if (err < 0) {
237 kfree(dif);
238 break;
239 }
240 ++sbi->devs->extra_devices;
241
242 err = erofs_init_device(&buf, sb, dif, &pos);
243 if (err)
244 break;
245 }
246 }
247 up_read(&sbi->devs->rwsem);
248 erofs_put_metabuf(&buf);
249 return err;
250 }
251
erofs_read_superblock(struct super_block * sb)252 static int erofs_read_superblock(struct super_block *sb)
253 {
254 struct erofs_sb_info *sbi = EROFS_SB(sb);
255 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
256 struct erofs_super_block *dsb;
257 void *data;
258 int ret;
259
260 data = erofs_read_metabuf(&buf, sb, 0, false);
261 if (IS_ERR(data)) {
262 erofs_err(sb, "cannot read erofs superblock");
263 return PTR_ERR(data);
264 }
265
266 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
267 ret = -EINVAL;
268 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
269 erofs_err(sb, "cannot find valid erofs superblock");
270 goto out;
271 }
272
273 sbi->blkszbits = dsb->blkszbits;
274 if (sbi->blkszbits < 9 || sbi->blkszbits > PAGE_SHIFT) {
275 erofs_err(sb, "blkszbits %u isn't supported", sbi->blkszbits);
276 goto out;
277 }
278 if (dsb->dirblkbits) {
279 erofs_err(sb, "dirblkbits %u isn't supported", dsb->dirblkbits);
280 goto out;
281 }
282
283 sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
284 if (erofs_sb_has_sb_chksum(sbi)) {
285 ret = erofs_superblock_csum_verify(sb, data);
286 if (ret)
287 goto out;
288 }
289
290 ret = -EINVAL;
291 sbi->feature_incompat = le32_to_cpu(dsb->feature_incompat);
292 if (sbi->feature_incompat & ~EROFS_ALL_FEATURE_INCOMPAT) {
293 erofs_err(sb, "unidentified incompatible feature %x, please upgrade kernel",
294 sbi->feature_incompat & ~EROFS_ALL_FEATURE_INCOMPAT);
295 goto out;
296 }
297
298 sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
299 if (sbi->sb_size > PAGE_SIZE - EROFS_SUPER_OFFSET) {
300 erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
301 sbi->sb_size);
302 goto out;
303 }
304 sbi->dif0.blocks = le32_to_cpu(dsb->blocks_lo);
305 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
306 #ifdef CONFIG_EROFS_FS_XATTR
307 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
308 sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start);
309 sbi->xattr_prefix_count = dsb->xattr_prefix_count;
310 sbi->xattr_filter_reserved = dsb->xattr_filter_reserved;
311 if (erofs_sb_has_ishare_xattrs(sbi)) {
312 if (dsb->ishare_xattr_prefix_id >= sbi->xattr_prefix_count) {
313 erofs_err(sb, "invalid ishare xattr prefix id %u",
314 dsb->ishare_xattr_prefix_id);
315 ret = -EFSCORRUPTED;
316 goto out;
317 }
318 sbi->ishare_xattr_prefix_id = dsb->ishare_xattr_prefix_id;
319 }
320 #endif
321 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
322 if (erofs_sb_has_48bit(sbi) && dsb->rootnid_8b) {
323 sbi->root_nid = le64_to_cpu(dsb->rootnid_8b);
324 sbi->dif0.blocks = sbi->dif0.blocks |
325 ((u64)le16_to_cpu(dsb->rb.blocks_hi) << 32);
326 } else {
327 sbi->root_nid = le16_to_cpu(dsb->rb.rootnid_2b);
328 }
329 sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
330 if (erofs_sb_has_metabox(sbi)) {
331 ret = -EFSCORRUPTED;
332 if (sbi->sb_size <= offsetof(struct erofs_super_block,
333 metabox_nid))
334 goto out;
335 sbi->metabox_nid = le64_to_cpu(dsb->metabox_nid);
336 if (sbi->metabox_nid & BIT_ULL(EROFS_DIRENT_NID_METABOX_BIT))
337 goto out; /* self-loop detection */
338 }
339 sbi->inos = le64_to_cpu(dsb->inos);
340
341 sbi->epoch = (s64)le64_to_cpu(dsb->epoch);
342 sbi->fixed_nsec = le32_to_cpu(dsb->fixed_nsec);
343 super_set_uuid(sb, (void *)dsb->uuid, sizeof(dsb->uuid));
344
345 if (dsb->volume_name[0]) {
346 sbi->volume_name = kstrndup(dsb->volume_name,
347 sizeof(dsb->volume_name), GFP_KERNEL);
348 if (!sbi->volume_name) {
349 ret = -ENOMEM;
350 goto out;
351 }
352 }
353
354 if (IS_ENABLED(CONFIG_EROFS_FS_ZIP)) {
355 ret = z_erofs_parse_cfgs(sb, dsb);
356 if (ret < 0)
357 goto out;
358 } else if (dsb->u1.available_compr_algs ||
359 erofs_sb_has_lz4_0padding(sbi)) {
360 erofs_err(sb, "compression disabled, unable to mount compressed EROFS");
361 ret = -EOPNOTSUPP;
362 goto out;
363 }
364
365 ret = erofs_scan_devices(sb, dsb);
366
367 if (erofs_sb_has_48bit(sbi))
368 erofs_info(sb, "EXPERIMENTAL 48-bit layout support in use. Use at your own risk!");
369 if (erofs_sb_has_metabox(sbi))
370 erofs_info(sb, "EXPERIMENTAL metadata compression support in use. Use at your own risk!");
371 if (erofs_is_fscache_mode(sb))
372 erofs_info(sb, "[deprecated] fscache-based on-demand read feature in use. Use at your own risk!");
373 out:
374 erofs_put_metabuf(&buf);
375 return ret;
376 }
377
erofs_default_options(struct erofs_sb_info * sbi)378 static void erofs_default_options(struct erofs_sb_info *sbi)
379 {
380 #ifdef CONFIG_EROFS_FS_ZIP
381 sbi->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
382 sbi->sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
383 #endif
384 if (IS_ENABLED(CONFIG_EROFS_FS_XATTR))
385 set_opt(&sbi->opt, XATTR_USER);
386 if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL))
387 set_opt(&sbi->opt, POSIX_ACL);
388 }
389
390 enum {
391 Opt_user_xattr, Opt_acl, Opt_cache_strategy, Opt_dax, Opt_dax_enum,
392 Opt_device, Opt_fsid, Opt_domain_id, Opt_directio, Opt_fsoffset,
393 Opt_inode_share,
394 };
395
396 static const struct constant_table erofs_param_cache_strategy[] = {
397 {"disabled", EROFS_ZIP_CACHE_DISABLED},
398 {"readahead", EROFS_ZIP_CACHE_READAHEAD},
399 {"readaround", EROFS_ZIP_CACHE_READAROUND},
400 {}
401 };
402
403 static const struct constant_table erofs_dax_param_enums[] = {
404 {"always", EROFS_MOUNT_DAX_ALWAYS},
405 {"never", EROFS_MOUNT_DAX_NEVER},
406 {}
407 };
408
409 static const struct fs_parameter_spec erofs_fs_parameters[] = {
410 fsparam_flag_no("user_xattr", Opt_user_xattr),
411 fsparam_flag_no("acl", Opt_acl),
412 fsparam_enum("cache_strategy", Opt_cache_strategy,
413 erofs_param_cache_strategy),
414 fsparam_flag("dax", Opt_dax),
415 fsparam_enum("dax", Opt_dax_enum, erofs_dax_param_enums),
416 fsparam_string("device", Opt_device),
417 fsparam_string("fsid", Opt_fsid),
418 fsparam_string("domain_id", Opt_domain_id),
419 fsparam_flag_no("directio", Opt_directio),
420 fsparam_u64("fsoffset", Opt_fsoffset),
421 fsparam_flag("inode_share", Opt_inode_share),
422 {}
423 };
424
erofs_fc_set_dax_mode(struct fs_context * fc,unsigned int mode)425 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
426 {
427 if (IS_ENABLED(CONFIG_FS_DAX)) {
428 struct erofs_sb_info *sbi = fc->s_fs_info;
429
430 if (mode == EROFS_MOUNT_DAX_ALWAYS) {
431 set_opt(&sbi->opt, DAX_ALWAYS);
432 clear_opt(&sbi->opt, DAX_NEVER);
433 return true;
434 } else if (mode == EROFS_MOUNT_DAX_NEVER) {
435 set_opt(&sbi->opt, DAX_NEVER);
436 clear_opt(&sbi->opt, DAX_ALWAYS);
437 return true;
438 }
439 DBG_BUGON(1);
440 return false;
441 }
442 errorfc(fc, "dax options not supported");
443 return false;
444 }
445
erofs_fc_parse_param(struct fs_context * fc,struct fs_parameter * param)446 static int erofs_fc_parse_param(struct fs_context *fc,
447 struct fs_parameter *param)
448 {
449 struct erofs_sb_info *sbi = fc->s_fs_info;
450 struct fs_parse_result result;
451 struct erofs_device_info *dif;
452 int opt, ret;
453
454 opt = fs_parse(fc, erofs_fs_parameters, param, &result);
455 if (opt < 0)
456 return opt;
457
458 switch (opt) {
459 case Opt_user_xattr:
460 if (!IS_ENABLED(CONFIG_EROFS_FS_XATTR))
461 errorfc(fc, "{,no}user_xattr options not supported");
462 else if (result.boolean)
463 set_opt(&sbi->opt, XATTR_USER);
464 else
465 clear_opt(&sbi->opt, XATTR_USER);
466 break;
467 case Opt_acl:
468 if (!IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL))
469 errorfc(fc, "{,no}acl options not supported");
470 else if (result.boolean)
471 set_opt(&sbi->opt, POSIX_ACL);
472 else
473 clear_opt(&sbi->opt, POSIX_ACL);
474 break;
475 case Opt_cache_strategy:
476 if (!IS_ENABLED(CONFIG_EROFS_FS_ZIP))
477 errorfc(fc, "compression not supported, cache_strategy ignored");
478 else
479 sbi->opt.cache_strategy = result.uint_32;
480 break;
481 case Opt_dax:
482 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
483 return -EINVAL;
484 break;
485 case Opt_dax_enum:
486 if (!erofs_fc_set_dax_mode(fc, result.uint_32))
487 return -EINVAL;
488 break;
489 case Opt_device:
490 dif = kzalloc_obj(*dif);
491 if (!dif)
492 return -ENOMEM;
493 dif->path = kstrdup(param->string, GFP_KERNEL);
494 if (!dif->path) {
495 kfree(dif);
496 return -ENOMEM;
497 }
498 down_write(&sbi->devs->rwsem);
499 ret = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
500 up_write(&sbi->devs->rwsem);
501 if (ret < 0) {
502 kfree(dif->path);
503 kfree(dif);
504 return ret;
505 }
506 ++sbi->devs->extra_devices;
507 break;
508 #ifdef CONFIG_EROFS_FS_ONDEMAND
509 case Opt_fsid:
510 kfree(sbi->fsid);
511 sbi->fsid = kstrdup(param->string, GFP_KERNEL);
512 if (!sbi->fsid)
513 return -ENOMEM;
514 break;
515 #endif
516 #if defined(CONFIG_EROFS_FS_ONDEMAND) || defined(CONFIG_EROFS_FS_PAGE_CACHE_SHARE)
517 case Opt_domain_id:
518 kfree_sensitive(sbi->domain_id);
519 sbi->domain_id = no_free_ptr(param->string);
520 break;
521 #else
522 case Opt_fsid:
523 case Opt_domain_id:
524 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
525 break;
526 #endif
527 case Opt_directio:
528 if (!IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE))
529 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
530 else if (result.boolean)
531 set_opt(&sbi->opt, DIRECT_IO);
532 else
533 clear_opt(&sbi->opt, DIRECT_IO);
534 break;
535 case Opt_fsoffset:
536 sbi->dif0.fsoff = result.uint_64;
537 break;
538 case Opt_inode_share:
539 if (!IS_ENABLED(CONFIG_EROFS_FS_PAGE_CACHE_SHARE))
540 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
541 else
542 set_opt(&sbi->opt, INODE_SHARE);
543 break;
544 }
545 return 0;
546 }
547
erofs_encode_fh(struct inode * inode,u32 * fh,int * max_len,struct inode * parent)548 static int erofs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
549 struct inode *parent)
550 {
551 erofs_nid_t nid = EROFS_I(inode)->nid;
552 int len = parent ? 6 : 3;
553
554 if (*max_len < len) {
555 *max_len = len;
556 return FILEID_INVALID;
557 }
558
559 fh[0] = (u32)(nid >> 32);
560 fh[1] = (u32)(nid & 0xffffffff);
561 fh[2] = inode->i_generation;
562
563 if (parent) {
564 nid = EROFS_I(parent)->nid;
565
566 fh[3] = (u32)(nid >> 32);
567 fh[4] = (u32)(nid & 0xffffffff);
568 fh[5] = parent->i_generation;
569 }
570
571 *max_len = len;
572 return parent ? FILEID_INO64_GEN_PARENT : FILEID_INO64_GEN;
573 }
574
erofs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)575 static struct dentry *erofs_fh_to_dentry(struct super_block *sb,
576 struct fid *fid, int fh_len, int fh_type)
577 {
578 if ((fh_type != FILEID_INO64_GEN &&
579 fh_type != FILEID_INO64_GEN_PARENT) || fh_len < 3)
580 return NULL;
581
582 return d_obtain_alias(erofs_iget(sb,
583 ((u64)fid->raw[0] << 32) | fid->raw[1]));
584 }
585
erofs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)586 static struct dentry *erofs_fh_to_parent(struct super_block *sb,
587 struct fid *fid, int fh_len, int fh_type)
588 {
589 if (fh_type != FILEID_INO64_GEN_PARENT || fh_len < 6)
590 return NULL;
591
592 return d_obtain_alias(erofs_iget(sb,
593 ((u64)fid->raw[3] << 32) | fid->raw[4]));
594 }
595
erofs_get_parent(struct dentry * child)596 static struct dentry *erofs_get_parent(struct dentry *child)
597 {
598 erofs_nid_t nid;
599 unsigned int d_type;
600 int err;
601
602 err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type);
603 if (err)
604 return ERR_PTR(err);
605 return d_obtain_alias(erofs_iget(child->d_sb, nid));
606 }
607
608 static const struct export_operations erofs_export_ops = {
609 .encode_fh = erofs_encode_fh,
610 .fh_to_dentry = erofs_fh_to_dentry,
611 .fh_to_parent = erofs_fh_to_parent,
612 .get_parent = erofs_get_parent,
613 };
614
erofs_set_sysfs_name(struct super_block * sb)615 static void erofs_set_sysfs_name(struct super_block *sb)
616 {
617 struct erofs_sb_info *sbi = EROFS_SB(sb);
618
619 if (sbi->domain_id && sbi->fsid)
620 super_set_sysfs_name_generic(sb, "%s,%s", sbi->domain_id,
621 sbi->fsid);
622 else if (sbi->fsid)
623 super_set_sysfs_name_generic(sb, "%s", sbi->fsid);
624 else if (erofs_is_fileio_mode(sbi))
625 super_set_sysfs_name_generic(sb, "%s",
626 bdi_dev_name(sb->s_bdi));
627 else
628 super_set_sysfs_name_id(sb);
629 }
630
erofs_fc_fill_super(struct super_block * sb,struct fs_context * fc)631 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
632 {
633 struct inode *inode;
634 struct erofs_sb_info *sbi = EROFS_SB(sb);
635 int err;
636
637 sb->s_magic = EROFS_SUPER_MAGIC;
638 sb->s_flags |= SB_RDONLY | SB_NOATIME;
639 sb->s_maxbytes = MAX_LFS_FILESIZE;
640 sb->s_op = &erofs_sops;
641
642 if (!sbi->domain_id && test_opt(&sbi->opt, INODE_SHARE)) {
643 errorfc(fc, "domain_id is needed when inode_ishare is on");
644 return -EINVAL;
645 }
646 if (test_opt(&sbi->opt, DAX_ALWAYS) && test_opt(&sbi->opt, INODE_SHARE)) {
647 errorfc(fc, "FSDAX is not allowed when inode_ishare is on");
648 return -EINVAL;
649 }
650
651 sbi->blkszbits = PAGE_SHIFT;
652 if (!sb->s_bdev) {
653 /*
654 * (File-backed mounts) EROFS claims it's safe to nest other
655 * fs contexts (including its own) due to self-controlled RO
656 * accesses/contexts and no side-effect changes that need to
657 * context save & restore so it can reuse the current thread
658 * context.
659 * However, we still need to prevent kernel stack overflow due
660 * to filesystem nesting: just ensure that s_stack_depth is 0
661 * to disallow mounting EROFS on stacked filesystems.
662 * Note: s_stack_depth is not incremented here for now, since
663 * EROFS is the only fs supporting file-backed mounts for now.
664 * It MUST change if another fs plans to support them, which
665 * may also require adjusting FILESYSTEM_MAX_STACK_DEPTH.
666 */
667 if (erofs_is_fileio_mode(sbi)) {
668 inode = file_inode(sbi->dif0.file);
669 if ((inode->i_sb->s_op == &erofs_sops &&
670 !inode->i_sb->s_bdev) ||
671 inode->i_sb->s_stack_depth) {
672 erofs_err(sb, "file-backed mounts cannot be applied to stacked fses");
673 return -ENOTBLK;
674 }
675 }
676 sb->s_blocksize = PAGE_SIZE;
677 sb->s_blocksize_bits = PAGE_SHIFT;
678
679 if (erofs_is_fscache_mode(sb)) {
680 err = erofs_fscache_register_fs(sb);
681 if (err)
682 return err;
683 }
684 err = super_setup_bdi(sb);
685 if (err)
686 return err;
687 } else {
688 if (!sb_set_blocksize(sb, PAGE_SIZE)) {
689 errorfc(fc, "failed to set initial blksize");
690 return -EINVAL;
691 }
692
693 sbi->dif0.dax_dev = fs_dax_get_by_bdev(sb->s_bdev,
694 &sbi->dif0.dax_part_off, NULL, NULL);
695 }
696
697 err = erofs_read_superblock(sb);
698 if (err)
699 return err;
700
701 if (sb->s_blocksize_bits != sbi->blkszbits) {
702 if (erofs_is_fscache_mode(sb)) {
703 errorfc(fc, "unsupported blksize for fscache mode");
704 return -EINVAL;
705 }
706
707 if (erofs_is_fileio_mode(sbi)) {
708 sb->s_blocksize = 1 << sbi->blkszbits;
709 sb->s_blocksize_bits = sbi->blkszbits;
710 } else if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) {
711 errorfc(fc, "failed to set erofs blksize");
712 return -EINVAL;
713 }
714 }
715
716 if (sbi->dif0.fsoff) {
717 if (sbi->dif0.fsoff & (sb->s_blocksize - 1))
718 return invalfc(fc, "fsoffset %llu is not aligned to block size %lu",
719 sbi->dif0.fsoff, sb->s_blocksize);
720 if (erofs_is_fscache_mode(sb))
721 return invalfc(fc, "cannot use fsoffset in fscache mode");
722 }
723
724 if (test_opt(&sbi->opt, DAX_ALWAYS) && sbi->blkszbits != PAGE_SHIFT) {
725 erofs_info(sb, "unsupported blocksize for DAX");
726 clear_opt(&sbi->opt, DAX_ALWAYS);
727 }
728 if (test_opt(&sbi->opt, INODE_SHARE) && !erofs_sb_has_ishare_xattrs(sbi)) {
729 erofs_info(sb, "on-disk ishare xattrs not found. Turning off inode_share.");
730 clear_opt(&sbi->opt, INODE_SHARE);
731 }
732 if (test_opt(&sbi->opt, INODE_SHARE))
733 erofs_info(sb, "EXPERIMENTAL EROFS page cache share support in use. Use at your own risk!");
734
735 sb->s_time_gran = 1;
736 sb->s_xattr = erofs_xattr_handlers;
737 sb->s_export_op = &erofs_export_ops;
738
739 if (test_opt(&sbi->opt, POSIX_ACL))
740 sb->s_flags |= SB_POSIXACL;
741 else
742 sb->s_flags &= ~SB_POSIXACL;
743
744 err = z_erofs_init_super(sb);
745 if (err)
746 return err;
747
748 if (erofs_sb_has_fragments(sbi) && sbi->packed_nid) {
749 inode = erofs_iget(sb, sbi->packed_nid);
750 if (IS_ERR(inode))
751 return PTR_ERR(inode);
752 sbi->packed_inode = inode;
753 }
754 if (erofs_sb_has_metabox(sbi)) {
755 inode = erofs_iget(sb, sbi->metabox_nid);
756 if (IS_ERR(inode))
757 return PTR_ERR(inode);
758 sbi->metabox_inode = inode;
759 }
760
761 inode = erofs_iget(sb, sbi->root_nid);
762 if (IS_ERR(inode))
763 return PTR_ERR(inode);
764
765 if (!S_ISDIR(inode->i_mode)) {
766 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
767 sbi->root_nid, inode->i_mode);
768 iput(inode);
769 return -EINVAL;
770 }
771 sb->s_root = d_make_root(inode);
772 if (!sb->s_root)
773 return -ENOMEM;
774
775 erofs_shrinker_register(sb);
776 err = erofs_xattr_prefixes_init(sb);
777 if (err)
778 return err;
779
780 erofs_set_sysfs_name(sb);
781 err = erofs_register_sysfs(sb);
782 if (err)
783 return err;
784
785 sbi->dir_ra_bytes = EROFS_DIR_RA_BYTES;
786 erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid);
787 return 0;
788 }
789
erofs_fc_get_tree(struct fs_context * fc)790 static int erofs_fc_get_tree(struct fs_context *fc)
791 {
792 struct erofs_sb_info *sbi = fc->s_fs_info;
793 int ret;
794
795 if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
796 return get_tree_nodev(fc, erofs_fc_fill_super);
797
798 ret = get_tree_bdev_flags(fc, erofs_fc_fill_super,
799 IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) ?
800 GET_TREE_BDEV_QUIET_LOOKUP : 0);
801 if (IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) && ret == -ENOTBLK) {
802 struct file *file;
803
804 if (!fc->source)
805 return invalf(fc, "No source specified");
806 file = filp_open(fc->source, O_RDONLY | O_LARGEFILE, 0);
807 if (IS_ERR(file))
808 return PTR_ERR(file);
809 sbi->dif0.file = file;
810
811 if (S_ISREG(file_inode(sbi->dif0.file)->i_mode) &&
812 sbi->dif0.file->f_mapping->a_ops->read_folio)
813 return get_tree_nodev(fc, erofs_fc_fill_super);
814 }
815 return ret;
816 }
817
erofs_fc_reconfigure(struct fs_context * fc)818 static int erofs_fc_reconfigure(struct fs_context *fc)
819 {
820 struct super_block *sb = fc->root->d_sb;
821 struct erofs_sb_info *sbi = EROFS_SB(sb);
822 struct erofs_sb_info *new_sbi = fc->s_fs_info;
823
824 DBG_BUGON(!sb_rdonly(sb));
825
826 if (new_sbi->fsid || new_sbi->domain_id)
827 erofs_info(sb, "ignoring reconfiguration for fsid|domain_id.");
828
829 if (test_opt(&new_sbi->opt, POSIX_ACL))
830 fc->sb_flags |= SB_POSIXACL;
831 else
832 fc->sb_flags &= ~SB_POSIXACL;
833
834 sbi->opt = new_sbi->opt;
835
836 fc->sb_flags |= SB_RDONLY;
837 return 0;
838 }
839
erofs_release_device_info(int id,void * ptr,void * data)840 static int erofs_release_device_info(int id, void *ptr, void *data)
841 {
842 struct erofs_device_info *dif = ptr;
843
844 fs_put_dax(dif->dax_dev, NULL);
845 if (dif->file)
846 fput(dif->file);
847 erofs_fscache_unregister_cookie(dif->fscache);
848 dif->fscache = NULL;
849 kfree(dif->path);
850 kfree(dif);
851 return 0;
852 }
853
erofs_free_dev_context(struct erofs_dev_context * devs)854 static void erofs_free_dev_context(struct erofs_dev_context *devs)
855 {
856 if (!devs)
857 return;
858 idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
859 idr_destroy(&devs->tree);
860 kfree(devs);
861 }
862
erofs_sb_free(struct erofs_sb_info * sbi)863 static void erofs_sb_free(struct erofs_sb_info *sbi)
864 {
865 erofs_free_dev_context(sbi->devs);
866 kfree(sbi->fsid);
867 kfree_sensitive(sbi->domain_id);
868 if (sbi->dif0.file)
869 fput(sbi->dif0.file);
870 kfree(sbi->volume_name);
871 kfree(sbi);
872 }
873
erofs_fc_free(struct fs_context * fc)874 static void erofs_fc_free(struct fs_context *fc)
875 {
876 struct erofs_sb_info *sbi = fc->s_fs_info;
877
878 if (sbi) /* free here if an error occurs before transferring to sb */
879 erofs_sb_free(sbi);
880 }
881
882 static const struct fs_context_operations erofs_context_ops = {
883 .parse_param = erofs_fc_parse_param,
884 .get_tree = erofs_fc_get_tree,
885 .reconfigure = erofs_fc_reconfigure,
886 .free = erofs_fc_free,
887 };
888
erofs_init_fs_context(struct fs_context * fc)889 static int erofs_init_fs_context(struct fs_context *fc)
890 {
891 struct erofs_sb_info *sbi;
892
893 sbi = kzalloc_obj(*sbi);
894 if (!sbi)
895 return -ENOMEM;
896
897 sbi->devs = kzalloc_obj(struct erofs_dev_context);
898 if (!sbi->devs) {
899 kfree(sbi);
900 return -ENOMEM;
901 }
902 fc->s_fs_info = sbi;
903
904 idr_init(&sbi->devs->tree);
905 init_rwsem(&sbi->devs->rwsem);
906 erofs_default_options(sbi);
907 fc->ops = &erofs_context_ops;
908 return 0;
909 }
910
erofs_drop_internal_inodes(struct erofs_sb_info * sbi)911 static void erofs_drop_internal_inodes(struct erofs_sb_info *sbi)
912 {
913 iput(sbi->packed_inode);
914 sbi->packed_inode = NULL;
915 iput(sbi->metabox_inode);
916 sbi->metabox_inode = NULL;
917 #ifdef CONFIG_EROFS_FS_ZIP
918 iput(sbi->managed_cache);
919 sbi->managed_cache = NULL;
920 #endif
921 }
922
erofs_kill_sb(struct super_block * sb)923 static void erofs_kill_sb(struct super_block *sb)
924 {
925 struct erofs_sb_info *sbi = EROFS_SB(sb);
926
927 if ((IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid) ||
928 sbi->dif0.file)
929 kill_anon_super(sb);
930 else
931 kill_block_super(sb);
932 erofs_drop_internal_inodes(sbi);
933 fs_put_dax(sbi->dif0.dax_dev, NULL);
934 erofs_fscache_unregister_fs(sb);
935 erofs_sb_free(sbi);
936 sb->s_fs_info = NULL;
937 }
938
erofs_put_super(struct super_block * sb)939 static void erofs_put_super(struct super_block *sb)
940 {
941 struct erofs_sb_info *const sbi = EROFS_SB(sb);
942
943 erofs_unregister_sysfs(sb);
944 erofs_shrinker_unregister(sb);
945 erofs_xattr_prefixes_cleanup(sb);
946 erofs_drop_internal_inodes(sbi);
947 erofs_free_dev_context(sbi->devs);
948 sbi->devs = NULL;
949 erofs_fscache_unregister_fs(sb);
950 }
951
952 static struct file_system_type erofs_fs_type = {
953 .owner = THIS_MODULE,
954 .name = "erofs",
955 .init_fs_context = erofs_init_fs_context,
956 .kill_sb = erofs_kill_sb,
957 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
958 };
959 MODULE_ALIAS_FS("erofs");
960
961 #if defined(CONFIG_EROFS_FS_ONDEMAND) || defined(CONFIG_EROFS_FS_PAGE_CACHE_SHARE)
erofs_free_anon_inode(struct inode * inode)962 static void erofs_free_anon_inode(struct inode *inode)
963 {
964 struct erofs_inode *vi = EROFS_I(inode);
965
966 #ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE
967 kfree(vi->fingerprint.opaque);
968 #endif
969 kmem_cache_free(erofs_inode_cachep, vi);
970 }
971
972 static const struct super_operations erofs_anon_sops = {
973 .alloc_inode = erofs_alloc_inode,
974 .drop_inode = inode_just_drop,
975 .free_inode = erofs_free_anon_inode,
976 };
977
erofs_anon_init_fs_context(struct fs_context * fc)978 static int erofs_anon_init_fs_context(struct fs_context *fc)
979 {
980 struct pseudo_fs_context *ctx;
981
982 ctx = init_pseudo(fc, EROFS_SUPER_MAGIC);
983 if (!ctx)
984 return -ENOMEM;
985 ctx->ops = &erofs_anon_sops;
986 return 0;
987 }
988
989 struct file_system_type erofs_anon_fs_type = {
990 .name = "pseudo_erofs",
991 .init_fs_context = erofs_anon_init_fs_context,
992 .kill_sb = kill_anon_super,
993 };
994 #endif
995
erofs_module_init(void)996 static int __init erofs_module_init(void)
997 {
998 int err;
999
1000 erofs_check_ondisk_layout_definitions();
1001
1002 erofs_inode_cachep = kmem_cache_create("erofs_inode",
1003 sizeof(struct erofs_inode), 0,
1004 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
1005 erofs_inode_init_once);
1006 if (!erofs_inode_cachep)
1007 return -ENOMEM;
1008
1009 err = erofs_init_shrinker();
1010 if (err)
1011 goto shrinker_err;
1012
1013 err = z_erofs_init_subsystem();
1014 if (err)
1015 goto zip_err;
1016
1017 err = erofs_init_sysfs();
1018 if (err)
1019 goto sysfs_err;
1020
1021 err = erofs_init_ishare();
1022 if (err)
1023 goto ishare_err;
1024
1025 err = register_filesystem(&erofs_fs_type);
1026 if (err)
1027 goto fs_err;
1028
1029 return 0;
1030
1031 fs_err:
1032 erofs_exit_ishare();
1033 ishare_err:
1034 erofs_exit_sysfs();
1035 sysfs_err:
1036 z_erofs_exit_subsystem();
1037 zip_err:
1038 erofs_exit_shrinker();
1039 shrinker_err:
1040 kmem_cache_destroy(erofs_inode_cachep);
1041 return err;
1042 }
1043
erofs_module_exit(void)1044 static void __exit erofs_module_exit(void)
1045 {
1046 unregister_filesystem(&erofs_fs_type);
1047
1048 /* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
1049 rcu_barrier();
1050
1051 erofs_exit_ishare();
1052 erofs_exit_sysfs();
1053 z_erofs_exit_subsystem();
1054 erofs_exit_shrinker();
1055 kmem_cache_destroy(erofs_inode_cachep);
1056 }
1057
erofs_statfs(struct dentry * dentry,struct kstatfs * buf)1058 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
1059 {
1060 struct super_block *sb = dentry->d_sb;
1061 struct erofs_sb_info *sbi = EROFS_SB(sb);
1062
1063 buf->f_type = sb->s_magic;
1064 buf->f_bsize = sb->s_blocksize;
1065 buf->f_blocks = sbi->total_blocks;
1066 buf->f_bfree = buf->f_bavail = 0;
1067 buf->f_files = ULLONG_MAX;
1068 buf->f_ffree = ULLONG_MAX - sbi->inos;
1069 buf->f_namelen = EROFS_NAME_LEN;
1070
1071 if (uuid_is_null(&sb->s_uuid))
1072 buf->f_fsid = u64_to_fsid(!sb->s_bdev ? 0 :
1073 huge_encode_dev(sb->s_bdev->bd_dev));
1074 else
1075 buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
1076 return 0;
1077 }
1078
erofs_show_options(struct seq_file * seq,struct dentry * root)1079 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
1080 {
1081 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
1082 struct erofs_mount_opts *opt = &sbi->opt;
1083
1084 if (IS_ENABLED(CONFIG_EROFS_FS_XATTR))
1085 seq_puts(seq, test_opt(opt, XATTR_USER) ?
1086 ",user_xattr" : ",nouser_xattr");
1087 if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL))
1088 seq_puts(seq, test_opt(opt, POSIX_ACL) ? ",acl" : ",noacl");
1089 if (IS_ENABLED(CONFIG_EROFS_FS_ZIP))
1090 seq_printf(seq, ",cache_strategy=%s",
1091 erofs_param_cache_strategy[opt->cache_strategy].name);
1092 if (test_opt(opt, DAX_ALWAYS))
1093 seq_puts(seq, ",dax=always");
1094 if (test_opt(opt, DAX_NEVER))
1095 seq_puts(seq, ",dax=never");
1096 if (erofs_is_fileio_mode(sbi) && test_opt(opt, DIRECT_IO))
1097 seq_puts(seq, ",directio");
1098 if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND)) {
1099 if (sbi->fsid)
1100 seq_printf(seq, ",fsid=%s", sbi->fsid);
1101 if (sbi->domain_id)
1102 seq_printf(seq, ",domain_id=%s", sbi->domain_id);
1103 }
1104 if (sbi->dif0.fsoff)
1105 seq_printf(seq, ",fsoffset=%llu", sbi->dif0.fsoff);
1106 if (test_opt(opt, INODE_SHARE))
1107 seq_puts(seq, ",inode_share");
1108 return 0;
1109 }
1110
erofs_evict_inode(struct inode * inode)1111 static void erofs_evict_inode(struct inode *inode)
1112 {
1113 if (IS_DAX(inode))
1114 dax_break_layout_final(inode);
1115 erofs_ishare_free_inode(inode);
1116 truncate_inode_pages_final(&inode->i_data);
1117 clear_inode(inode);
1118 }
1119
1120 const struct super_operations erofs_sops = {
1121 .put_super = erofs_put_super,
1122 .alloc_inode = erofs_alloc_inode,
1123 .free_inode = erofs_free_inode,
1124 .evict_inode = erofs_evict_inode,
1125 .statfs = erofs_statfs,
1126 .show_options = erofs_show_options,
1127 };
1128
1129 module_init(erofs_module_init);
1130 module_exit(erofs_module_exit);
1131
1132 MODULE_DESCRIPTION("Enhanced ROM File System");
1133 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
1134 MODULE_LICENSE("GPL");
1135