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 "xattr.h"
15
16 #define CREATE_TRACE_POINTS
17 #include <trace/events/erofs.h>
18
19 static struct kmem_cache *erofs_inode_cachep __read_mostly;
20
_erofs_err(struct super_block * sb,const char * func,const char * fmt,...)21 void _erofs_err(struct super_block *sb, const char *func, const char *fmt, ...)
22 {
23 struct va_format vaf;
24 va_list args;
25
26 va_start(args, fmt);
27
28 vaf.fmt = fmt;
29 vaf.va = &args;
30
31 if (sb)
32 pr_err("(device %s): %s: %pV", sb->s_id, func, &vaf);
33 else
34 pr_err("%s: %pV", func, &vaf);
35 va_end(args);
36 }
37
_erofs_info(struct super_block * sb,const char * func,const char * fmt,...)38 void _erofs_info(struct super_block *sb, const char *func, const char *fmt, ...)
39 {
40 struct va_format vaf;
41 va_list args;
42
43 va_start(args, fmt);
44
45 vaf.fmt = fmt;
46 vaf.va = &args;
47
48 if (sb)
49 pr_info("(device %s): %pV", sb->s_id, &vaf);
50 else
51 pr_info("%pV", &vaf);
52 va_end(args);
53 }
54
erofs_superblock_csum_verify(struct super_block * sb,void * sbdata)55 static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
56 {
57 size_t len = 1 << EROFS_SB(sb)->blkszbits;
58 struct erofs_super_block *dsb;
59 u32 expected_crc, crc;
60
61 if (len > EROFS_SUPER_OFFSET)
62 len -= EROFS_SUPER_OFFSET;
63
64 dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET, len, GFP_KERNEL);
65 if (!dsb)
66 return -ENOMEM;
67
68 expected_crc = le32_to_cpu(dsb->checksum);
69 dsb->checksum = 0;
70 /* to allow for x86 boot sectors and other oddities. */
71 crc = crc32c(~0, dsb, len);
72 kfree(dsb);
73
74 if (crc != expected_crc) {
75 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
76 crc, expected_crc);
77 return -EBADMSG;
78 }
79 return 0;
80 }
81
erofs_inode_init_once(void * ptr)82 static void erofs_inode_init_once(void *ptr)
83 {
84 struct erofs_inode *vi = ptr;
85
86 inode_init_once(&vi->vfs_inode);
87 }
88
erofs_alloc_inode(struct super_block * sb)89 static struct inode *erofs_alloc_inode(struct super_block *sb)
90 {
91 struct erofs_inode *vi =
92 alloc_inode_sb(sb, erofs_inode_cachep, GFP_KERNEL);
93
94 if (!vi)
95 return NULL;
96
97 /* zero out everything except vfs_inode */
98 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
99 return &vi->vfs_inode;
100 }
101
erofs_free_inode(struct inode * inode)102 static void erofs_free_inode(struct inode *inode)
103 {
104 struct erofs_inode *vi = EROFS_I(inode);
105
106 if (inode->i_op == &erofs_fast_symlink_iops)
107 kfree(inode->i_link);
108 kfree(vi->xattr_shared_xattrs);
109 kmem_cache_free(erofs_inode_cachep, vi);
110 }
111
112 /* 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)113 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
114 erofs_off_t *offset, int *lengthp)
115 {
116 u8 *buffer, *ptr;
117 int len, i, cnt;
118
119 *offset = round_up(*offset, 4);
120 ptr = erofs_bread(buf, *offset, EROFS_KMAP);
121 if (IS_ERR(ptr))
122 return ptr;
123
124 len = le16_to_cpu(*(__le16 *)ptr);
125 if (!len)
126 len = U16_MAX + 1;
127 buffer = kmalloc(len, GFP_KERNEL);
128 if (!buffer)
129 return ERR_PTR(-ENOMEM);
130 *offset += sizeof(__le16);
131 *lengthp = len;
132
133 for (i = 0; i < len; i += cnt) {
134 cnt = min_t(int, sb->s_blocksize - erofs_blkoff(sb, *offset),
135 len - i);
136 ptr = erofs_bread(buf, *offset, EROFS_KMAP);
137 if (IS_ERR(ptr)) {
138 kfree(buffer);
139 return ptr;
140 }
141 memcpy(buffer + i, ptr, cnt);
142 *offset += cnt;
143 }
144 return buffer;
145 }
146
147 #ifndef CONFIG_EROFS_FS_ZIP
z_erofs_parse_cfgs(struct super_block * sb,struct erofs_super_block * dsb)148 static int z_erofs_parse_cfgs(struct super_block *sb,
149 struct erofs_super_block *dsb)
150 {
151 if (!dsb->u1.available_compr_algs)
152 return 0;
153
154 erofs_err(sb, "compression disabled, unable to mount compressed EROFS");
155 return -EOPNOTSUPP;
156 }
157 #endif
158
erofs_init_device(struct erofs_buf * buf,struct super_block * sb,struct erofs_device_info * dif,erofs_off_t * pos)159 static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
160 struct erofs_device_info *dif, erofs_off_t *pos)
161 {
162 struct erofs_sb_info *sbi = EROFS_SB(sb);
163 struct erofs_fscache *fscache;
164 struct erofs_deviceslot *dis;
165 struct file *file;
166
167 dis = erofs_read_metabuf(buf, sb, *pos, EROFS_KMAP);
168 if (IS_ERR(dis))
169 return PTR_ERR(dis);
170
171 if (!sbi->devs->flatdev && !dif->path) {
172 if (!dis->tag[0]) {
173 erofs_err(sb, "empty device tag @ pos %llu", *pos);
174 return -EINVAL;
175 }
176 dif->path = kmemdup_nul(dis->tag, sizeof(dis->tag), GFP_KERNEL);
177 if (!dif->path)
178 return -ENOMEM;
179 }
180
181 if (erofs_is_fscache_mode(sb)) {
182 fscache = erofs_fscache_register_cookie(sb, dif->path, 0);
183 if (IS_ERR(fscache))
184 return PTR_ERR(fscache);
185 dif->fscache = fscache;
186 } else if (!sbi->devs->flatdev) {
187 file = erofs_is_fileio_mode(sbi) ?
188 filp_open(dif->path, O_RDONLY | O_LARGEFILE, 0) :
189 bdev_file_open_by_path(dif->path,
190 BLK_OPEN_READ, sb->s_type, NULL);
191 if (IS_ERR(file))
192 return PTR_ERR(file);
193
194 if (!erofs_is_fileio_mode(sbi)) {
195 dif->dax_dev = fs_dax_get_by_bdev(file_bdev(file),
196 &dif->dax_part_off, NULL, NULL);
197 } else if (!S_ISREG(file_inode(file)->i_mode)) {
198 fput(file);
199 return -EINVAL;
200 }
201 dif->file = file;
202 }
203
204 dif->blocks = le32_to_cpu(dis->blocks);
205 dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr);
206 sbi->total_blocks += dif->blocks;
207 *pos += EROFS_DEVT_SLOT_SIZE;
208 return 0;
209 }
210
erofs_scan_devices(struct super_block * sb,struct erofs_super_block * dsb)211 static int erofs_scan_devices(struct super_block *sb,
212 struct erofs_super_block *dsb)
213 {
214 struct erofs_sb_info *sbi = EROFS_SB(sb);
215 unsigned int ondisk_extradevs;
216 erofs_off_t pos;
217 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
218 struct erofs_device_info *dif;
219 int id, err = 0;
220
221 sbi->total_blocks = sbi->primarydevice_blocks;
222 if (!erofs_sb_has_device_table(sbi))
223 ondisk_extradevs = 0;
224 else
225 ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
226
227 if (sbi->devs->extra_devices &&
228 ondisk_extradevs != sbi->devs->extra_devices) {
229 erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
230 ondisk_extradevs, sbi->devs->extra_devices);
231 return -EINVAL;
232 }
233 if (!ondisk_extradevs)
234 return 0;
235
236 if (!sbi->devs->extra_devices && !erofs_is_fscache_mode(sb))
237 sbi->devs->flatdev = true;
238
239 sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
240 pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
241 down_read(&sbi->devs->rwsem);
242 if (sbi->devs->extra_devices) {
243 idr_for_each_entry(&sbi->devs->tree, dif, id) {
244 err = erofs_init_device(&buf, sb, dif, &pos);
245 if (err)
246 break;
247 }
248 } else {
249 for (id = 0; id < ondisk_extradevs; id++) {
250 dif = kzalloc(sizeof(*dif), GFP_KERNEL);
251 if (!dif) {
252 err = -ENOMEM;
253 break;
254 }
255
256 err = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
257 if (err < 0) {
258 kfree(dif);
259 break;
260 }
261 ++sbi->devs->extra_devices;
262
263 err = erofs_init_device(&buf, sb, dif, &pos);
264 if (err)
265 break;
266 }
267 }
268 up_read(&sbi->devs->rwsem);
269 erofs_put_metabuf(&buf);
270 return err;
271 }
272
erofs_read_superblock(struct super_block * sb)273 static int erofs_read_superblock(struct super_block *sb)
274 {
275 struct erofs_sb_info *sbi = EROFS_SB(sb);
276 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
277 struct erofs_super_block *dsb;
278 void *data;
279 int ret;
280
281 data = erofs_read_metabuf(&buf, sb, 0, EROFS_KMAP);
282 if (IS_ERR(data)) {
283 erofs_err(sb, "cannot read erofs superblock");
284 return PTR_ERR(data);
285 }
286
287 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
288 ret = -EINVAL;
289 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
290 erofs_err(sb, "cannot find valid erofs superblock");
291 goto out;
292 }
293
294 sbi->blkszbits = dsb->blkszbits;
295 if (sbi->blkszbits < 9 || sbi->blkszbits > PAGE_SHIFT) {
296 erofs_err(sb, "blkszbits %u isn't supported", sbi->blkszbits);
297 goto out;
298 }
299 if (dsb->dirblkbits) {
300 erofs_err(sb, "dirblkbits %u isn't supported", dsb->dirblkbits);
301 goto out;
302 }
303
304 sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
305 if (erofs_sb_has_sb_chksum(sbi)) {
306 ret = erofs_superblock_csum_verify(sb, data);
307 if (ret)
308 goto out;
309 }
310
311 ret = -EINVAL;
312 sbi->feature_incompat = le32_to_cpu(dsb->feature_incompat);
313 if (sbi->feature_incompat & ~EROFS_ALL_FEATURE_INCOMPAT) {
314 erofs_err(sb, "unidentified incompatible feature %x, please upgrade kernel",
315 sbi->feature_incompat & ~EROFS_ALL_FEATURE_INCOMPAT);
316 goto out;
317 }
318
319 sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
320 if (sbi->sb_size > PAGE_SIZE - EROFS_SUPER_OFFSET) {
321 erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
322 sbi->sb_size);
323 goto out;
324 }
325 sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
326 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
327 #ifdef CONFIG_EROFS_FS_XATTR
328 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
329 sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start);
330 sbi->xattr_prefix_count = dsb->xattr_prefix_count;
331 sbi->xattr_filter_reserved = dsb->xattr_filter_reserved;
332 #endif
333 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
334 sbi->root_nid = le16_to_cpu(dsb->root_nid);
335 sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
336 sbi->inos = le64_to_cpu(dsb->inos);
337
338 sbi->build_time = le64_to_cpu(dsb->build_time);
339 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
340
341 super_set_uuid(sb, (void *)dsb->uuid, sizeof(dsb->uuid));
342
343 ret = strscpy(sbi->volume_name, dsb->volume_name,
344 sizeof(dsb->volume_name));
345 if (ret < 0) { /* -E2BIG */
346 erofs_err(sb, "bad volume name without NIL terminator");
347 ret = -EFSCORRUPTED;
348 goto out;
349 }
350
351 /* parse on-disk compression configurations */
352 ret = z_erofs_parse_cfgs(sb, dsb);
353 if (ret < 0)
354 goto out;
355
356 /* handle multiple devices */
357 ret = erofs_scan_devices(sb, dsb);
358
359 if (erofs_is_fscache_mode(sb))
360 erofs_info(sb, "[deprecated] fscache-based on-demand read feature in use. Use at your own risk!");
361 out:
362 erofs_put_metabuf(&buf);
363 return ret;
364 }
365
erofs_default_options(struct erofs_sb_info * sbi)366 static void erofs_default_options(struct erofs_sb_info *sbi)
367 {
368 #ifdef CONFIG_EROFS_FS_ZIP
369 sbi->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
370 sbi->opt.max_sync_decompress_pages = 3;
371 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
372 #endif
373 #ifdef CONFIG_EROFS_FS_XATTR
374 set_opt(&sbi->opt, XATTR_USER);
375 #endif
376 #ifdef CONFIG_EROFS_FS_POSIX_ACL
377 set_opt(&sbi->opt, POSIX_ACL);
378 #endif
379 }
380
381 enum {
382 Opt_user_xattr,
383 Opt_acl,
384 Opt_cache_strategy,
385 Opt_dax,
386 Opt_dax_enum,
387 Opt_device,
388 Opt_fsid,
389 Opt_domain_id,
390 Opt_err
391 };
392
393 static const struct constant_table erofs_param_cache_strategy[] = {
394 {"disabled", EROFS_ZIP_CACHE_DISABLED},
395 {"readahead", EROFS_ZIP_CACHE_READAHEAD},
396 {"readaround", EROFS_ZIP_CACHE_READAROUND},
397 {}
398 };
399
400 static const struct constant_table erofs_dax_param_enums[] = {
401 {"always", EROFS_MOUNT_DAX_ALWAYS},
402 {"never", EROFS_MOUNT_DAX_NEVER},
403 {}
404 };
405
406 static const struct fs_parameter_spec erofs_fs_parameters[] = {
407 fsparam_flag_no("user_xattr", Opt_user_xattr),
408 fsparam_flag_no("acl", Opt_acl),
409 fsparam_enum("cache_strategy", Opt_cache_strategy,
410 erofs_param_cache_strategy),
411 fsparam_flag("dax", Opt_dax),
412 fsparam_enum("dax", Opt_dax_enum, erofs_dax_param_enums),
413 fsparam_string("device", Opt_device),
414 fsparam_string("fsid", Opt_fsid),
415 fsparam_string("domain_id", Opt_domain_id),
416 {}
417 };
418
erofs_fc_set_dax_mode(struct fs_context * fc,unsigned int mode)419 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
420 {
421 #ifdef CONFIG_FS_DAX
422 struct erofs_sb_info *sbi = fc->s_fs_info;
423
424 switch (mode) {
425 case EROFS_MOUNT_DAX_ALWAYS:
426 set_opt(&sbi->opt, DAX_ALWAYS);
427 clear_opt(&sbi->opt, DAX_NEVER);
428 return true;
429 case EROFS_MOUNT_DAX_NEVER:
430 set_opt(&sbi->opt, DAX_NEVER);
431 clear_opt(&sbi->opt, DAX_ALWAYS);
432 return true;
433 default:
434 DBG_BUGON(1);
435 return false;
436 }
437 #else
438 errorfc(fc, "dax options not supported");
439 return false;
440 #endif
441 }
442
erofs_fc_parse_param(struct fs_context * fc,struct fs_parameter * param)443 static int erofs_fc_parse_param(struct fs_context *fc,
444 struct fs_parameter *param)
445 {
446 struct erofs_sb_info *sbi = fc->s_fs_info;
447 struct fs_parse_result result;
448 struct erofs_device_info *dif;
449 int opt, ret;
450
451 opt = fs_parse(fc, erofs_fs_parameters, param, &result);
452 if (opt < 0)
453 return opt;
454
455 switch (opt) {
456 case Opt_user_xattr:
457 #ifdef CONFIG_EROFS_FS_XATTR
458 if (result.boolean)
459 set_opt(&sbi->opt, XATTR_USER);
460 else
461 clear_opt(&sbi->opt, XATTR_USER);
462 #else
463 errorfc(fc, "{,no}user_xattr options not supported");
464 #endif
465 break;
466 case Opt_acl:
467 #ifdef CONFIG_EROFS_FS_POSIX_ACL
468 if (result.boolean)
469 set_opt(&sbi->opt, POSIX_ACL);
470 else
471 clear_opt(&sbi->opt, POSIX_ACL);
472 #else
473 errorfc(fc, "{,no}acl options not supported");
474 #endif
475 break;
476 case Opt_cache_strategy:
477 #ifdef CONFIG_EROFS_FS_ZIP
478 sbi->opt.cache_strategy = result.uint_32;
479 #else
480 errorfc(fc, "compression not supported, cache_strategy ignored");
481 #endif
482 break;
483 case Opt_dax:
484 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
485 return -EINVAL;
486 break;
487 case Opt_dax_enum:
488 if (!erofs_fc_set_dax_mode(fc, result.uint_32))
489 return -EINVAL;
490 break;
491 case Opt_device:
492 dif = kzalloc(sizeof(*dif), GFP_KERNEL);
493 if (!dif)
494 return -ENOMEM;
495 dif->path = kstrdup(param->string, GFP_KERNEL);
496 if (!dif->path) {
497 kfree(dif);
498 return -ENOMEM;
499 }
500 down_write(&sbi->devs->rwsem);
501 ret = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
502 up_write(&sbi->devs->rwsem);
503 if (ret < 0) {
504 kfree(dif->path);
505 kfree(dif);
506 return ret;
507 }
508 ++sbi->devs->extra_devices;
509 break;
510 #ifdef CONFIG_EROFS_FS_ONDEMAND
511 case Opt_fsid:
512 kfree(sbi->fsid);
513 sbi->fsid = kstrdup(param->string, GFP_KERNEL);
514 if (!sbi->fsid)
515 return -ENOMEM;
516 break;
517 case Opt_domain_id:
518 kfree(sbi->domain_id);
519 sbi->domain_id = kstrdup(param->string, GFP_KERNEL);
520 if (!sbi->domain_id)
521 return -ENOMEM;
522 break;
523 #else
524 case Opt_fsid:
525 case Opt_domain_id:
526 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
527 break;
528 #endif
529 default:
530 return -ENOPARAM;
531 }
532 return 0;
533 }
534
erofs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)535 static struct inode *erofs_nfs_get_inode(struct super_block *sb,
536 u64 ino, u32 generation)
537 {
538 return erofs_iget(sb, ino);
539 }
540
erofs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)541 static struct dentry *erofs_fh_to_dentry(struct super_block *sb,
542 struct fid *fid, int fh_len, int fh_type)
543 {
544 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
545 erofs_nfs_get_inode);
546 }
547
erofs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)548 static struct dentry *erofs_fh_to_parent(struct super_block *sb,
549 struct fid *fid, int fh_len, int fh_type)
550 {
551 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
552 erofs_nfs_get_inode);
553 }
554
erofs_get_parent(struct dentry * child)555 static struct dentry *erofs_get_parent(struct dentry *child)
556 {
557 erofs_nid_t nid;
558 unsigned int d_type;
559 int err;
560
561 err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type);
562 if (err)
563 return ERR_PTR(err);
564 return d_obtain_alias(erofs_iget(child->d_sb, nid));
565 }
566
567 static const struct export_operations erofs_export_ops = {
568 .encode_fh = generic_encode_ino32_fh,
569 .fh_to_dentry = erofs_fh_to_dentry,
570 .fh_to_parent = erofs_fh_to_parent,
571 .get_parent = erofs_get_parent,
572 };
573
erofs_set_sysfs_name(struct super_block * sb)574 static void erofs_set_sysfs_name(struct super_block *sb)
575 {
576 struct erofs_sb_info *sbi = EROFS_SB(sb);
577
578 if (sbi->domain_id)
579 super_set_sysfs_name_generic(sb, "%s,%s", sbi->domain_id,
580 sbi->fsid);
581 else if (sbi->fsid)
582 super_set_sysfs_name_generic(sb, "%s", sbi->fsid);
583 else if (erofs_is_fileio_mode(sbi))
584 super_set_sysfs_name_generic(sb, "%s",
585 bdi_dev_name(sb->s_bdi));
586 else
587 super_set_sysfs_name_id(sb);
588 }
589
erofs_fc_fill_super(struct super_block * sb,struct fs_context * fc)590 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
591 {
592 struct inode *inode;
593 struct erofs_sb_info *sbi = EROFS_SB(sb);
594 int err;
595
596 sb->s_magic = EROFS_SUPER_MAGIC;
597 sb->s_flags |= SB_RDONLY | SB_NOATIME;
598 sb->s_maxbytes = MAX_LFS_FILESIZE;
599 sb->s_op = &erofs_sops;
600
601 sbi->blkszbits = PAGE_SHIFT;
602 if (!sb->s_bdev) {
603 sb->s_blocksize = PAGE_SIZE;
604 sb->s_blocksize_bits = PAGE_SHIFT;
605
606 if (erofs_is_fscache_mode(sb)) {
607 err = erofs_fscache_register_fs(sb);
608 if (err)
609 return err;
610 }
611 err = super_setup_bdi(sb);
612 if (err)
613 return err;
614 } else {
615 if (!sb_set_blocksize(sb, PAGE_SIZE)) {
616 errorfc(fc, "failed to set initial blksize");
617 return -EINVAL;
618 }
619
620 sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev,
621 &sbi->dax_part_off,
622 NULL, NULL);
623 }
624
625 err = erofs_read_superblock(sb);
626 if (err)
627 return err;
628
629 if (sb->s_blocksize_bits != sbi->blkszbits) {
630 if (erofs_is_fscache_mode(sb)) {
631 errorfc(fc, "unsupported blksize for fscache mode");
632 return -EINVAL;
633 }
634 if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) {
635 errorfc(fc, "failed to set erofs blksize");
636 return -EINVAL;
637 }
638 }
639
640 if (test_opt(&sbi->opt, DAX_ALWAYS)) {
641 if (!sbi->dax_dev) {
642 errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
643 clear_opt(&sbi->opt, DAX_ALWAYS);
644 } else if (sbi->blkszbits != PAGE_SHIFT) {
645 errorfc(fc, "unsupported blocksize for DAX");
646 clear_opt(&sbi->opt, DAX_ALWAYS);
647 }
648 }
649
650 sb->s_time_gran = 1;
651 sb->s_xattr = erofs_xattr_handlers;
652 sb->s_export_op = &erofs_export_ops;
653
654 if (test_opt(&sbi->opt, POSIX_ACL))
655 sb->s_flags |= SB_POSIXACL;
656 else
657 sb->s_flags &= ~SB_POSIXACL;
658
659 #ifdef CONFIG_EROFS_FS_ZIP
660 xa_init(&sbi->managed_pslots);
661 #endif
662
663 inode = erofs_iget(sb, sbi->root_nid);
664 if (IS_ERR(inode))
665 return PTR_ERR(inode);
666
667 if (!S_ISDIR(inode->i_mode)) {
668 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
669 sbi->root_nid, inode->i_mode);
670 iput(inode);
671 return -EINVAL;
672 }
673
674 sb->s_root = d_make_root(inode);
675 if (!sb->s_root)
676 return -ENOMEM;
677
678 erofs_shrinker_register(sb);
679 if (erofs_sb_has_fragments(sbi) && sbi->packed_nid) {
680 sbi->packed_inode = erofs_iget(sb, sbi->packed_nid);
681 if (IS_ERR(sbi->packed_inode)) {
682 err = PTR_ERR(sbi->packed_inode);
683 sbi->packed_inode = NULL;
684 return err;
685 }
686 }
687 err = erofs_init_managed_cache(sb);
688 if (err)
689 return err;
690
691 err = erofs_xattr_prefixes_init(sb);
692 if (err)
693 return err;
694
695 erofs_set_sysfs_name(sb);
696 err = erofs_register_sysfs(sb);
697 if (err)
698 return err;
699
700 erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid);
701 return 0;
702 }
703
erofs_fc_get_tree(struct fs_context * fc)704 static int erofs_fc_get_tree(struct fs_context *fc)
705 {
706 struct erofs_sb_info *sbi = fc->s_fs_info;
707 int ret;
708
709 if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
710 return get_tree_nodev(fc, erofs_fc_fill_super);
711
712 ret = get_tree_bdev_flags(fc, erofs_fc_fill_super,
713 IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) ?
714 GET_TREE_BDEV_QUIET_LOOKUP : 0);
715 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
716 if (ret == -ENOTBLK) {
717 if (!fc->source)
718 return invalf(fc, "No source specified");
719 sbi->fdev = filp_open(fc->source, O_RDONLY | O_LARGEFILE, 0);
720 if (IS_ERR(sbi->fdev))
721 return PTR_ERR(sbi->fdev);
722
723 if (S_ISREG(file_inode(sbi->fdev)->i_mode) &&
724 sbi->fdev->f_mapping->a_ops->read_folio)
725 return get_tree_nodev(fc, erofs_fc_fill_super);
726 fput(sbi->fdev);
727 }
728 #endif
729 return ret;
730 }
731
erofs_fc_reconfigure(struct fs_context * fc)732 static int erofs_fc_reconfigure(struct fs_context *fc)
733 {
734 struct super_block *sb = fc->root->d_sb;
735 struct erofs_sb_info *sbi = EROFS_SB(sb);
736 struct erofs_sb_info *new_sbi = fc->s_fs_info;
737
738 DBG_BUGON(!sb_rdonly(sb));
739
740 if (new_sbi->fsid || new_sbi->domain_id)
741 erofs_info(sb, "ignoring reconfiguration for fsid|domain_id.");
742
743 if (test_opt(&new_sbi->opt, POSIX_ACL))
744 fc->sb_flags |= SB_POSIXACL;
745 else
746 fc->sb_flags &= ~SB_POSIXACL;
747
748 sbi->opt = new_sbi->opt;
749
750 fc->sb_flags |= SB_RDONLY;
751 return 0;
752 }
753
erofs_release_device_info(int id,void * ptr,void * data)754 static int erofs_release_device_info(int id, void *ptr, void *data)
755 {
756 struct erofs_device_info *dif = ptr;
757
758 fs_put_dax(dif->dax_dev, NULL);
759 if (dif->file)
760 fput(dif->file);
761 erofs_fscache_unregister_cookie(dif->fscache);
762 dif->fscache = NULL;
763 kfree(dif->path);
764 kfree(dif);
765 return 0;
766 }
767
erofs_free_dev_context(struct erofs_dev_context * devs)768 static void erofs_free_dev_context(struct erofs_dev_context *devs)
769 {
770 if (!devs)
771 return;
772 idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
773 idr_destroy(&devs->tree);
774 kfree(devs);
775 }
776
erofs_fc_free(struct fs_context * fc)777 static void erofs_fc_free(struct fs_context *fc)
778 {
779 struct erofs_sb_info *sbi = fc->s_fs_info;
780
781 if (!sbi)
782 return;
783
784 erofs_free_dev_context(sbi->devs);
785 kfree(sbi->fsid);
786 kfree(sbi->domain_id);
787 kfree(sbi);
788 }
789
790 static const struct fs_context_operations erofs_context_ops = {
791 .parse_param = erofs_fc_parse_param,
792 .get_tree = erofs_fc_get_tree,
793 .reconfigure = erofs_fc_reconfigure,
794 .free = erofs_fc_free,
795 };
796
erofs_init_fs_context(struct fs_context * fc)797 static int erofs_init_fs_context(struct fs_context *fc)
798 {
799 struct erofs_sb_info *sbi;
800
801 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
802 if (!sbi)
803 return -ENOMEM;
804
805 sbi->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
806 if (!sbi->devs) {
807 kfree(sbi);
808 return -ENOMEM;
809 }
810 fc->s_fs_info = sbi;
811
812 idr_init(&sbi->devs->tree);
813 init_rwsem(&sbi->devs->rwsem);
814 erofs_default_options(sbi);
815 fc->ops = &erofs_context_ops;
816 return 0;
817 }
818
erofs_kill_sb(struct super_block * sb)819 static void erofs_kill_sb(struct super_block *sb)
820 {
821 struct erofs_sb_info *sbi = EROFS_SB(sb);
822
823 if ((IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid) || sbi->fdev)
824 kill_anon_super(sb);
825 else
826 kill_block_super(sb);
827
828 erofs_free_dev_context(sbi->devs);
829 fs_put_dax(sbi->dax_dev, NULL);
830 erofs_fscache_unregister_fs(sb);
831 kfree(sbi->fsid);
832 kfree(sbi->domain_id);
833 if (sbi->fdev)
834 fput(sbi->fdev);
835 kfree(sbi);
836 sb->s_fs_info = NULL;
837 }
838
erofs_put_super(struct super_block * sb)839 static void erofs_put_super(struct super_block *sb)
840 {
841 struct erofs_sb_info *const sbi = EROFS_SB(sb);
842
843 DBG_BUGON(!sbi);
844
845 erofs_unregister_sysfs(sb);
846 erofs_shrinker_unregister(sb);
847 erofs_xattr_prefixes_cleanup(sb);
848 #ifdef CONFIG_EROFS_FS_ZIP
849 iput(sbi->managed_cache);
850 sbi->managed_cache = NULL;
851 #endif
852 iput(sbi->packed_inode);
853 sbi->packed_inode = NULL;
854 erofs_free_dev_context(sbi->devs);
855 sbi->devs = NULL;
856 erofs_fscache_unregister_fs(sb);
857 }
858
859 static struct file_system_type erofs_fs_type = {
860 .owner = THIS_MODULE,
861 .name = "erofs",
862 .init_fs_context = erofs_init_fs_context,
863 .kill_sb = erofs_kill_sb,
864 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
865 };
866 MODULE_ALIAS_FS("erofs");
867
erofs_module_init(void)868 static int __init erofs_module_init(void)
869 {
870 int err;
871
872 erofs_check_ondisk_layout_definitions();
873
874 erofs_inode_cachep = kmem_cache_create("erofs_inode",
875 sizeof(struct erofs_inode), 0,
876 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
877 erofs_inode_init_once);
878 if (!erofs_inode_cachep)
879 return -ENOMEM;
880
881 err = erofs_init_shrinker();
882 if (err)
883 goto shrinker_err;
884
885 err = z_erofs_init_subsystem();
886 if (err)
887 goto zip_err;
888
889 err = erofs_init_sysfs();
890 if (err)
891 goto sysfs_err;
892
893 err = register_filesystem(&erofs_fs_type);
894 if (err)
895 goto fs_err;
896
897 return 0;
898
899 fs_err:
900 erofs_exit_sysfs();
901 sysfs_err:
902 z_erofs_exit_subsystem();
903 zip_err:
904 erofs_exit_shrinker();
905 shrinker_err:
906 kmem_cache_destroy(erofs_inode_cachep);
907 return err;
908 }
909
erofs_module_exit(void)910 static void __exit erofs_module_exit(void)
911 {
912 unregister_filesystem(&erofs_fs_type);
913
914 /* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
915 rcu_barrier();
916
917 erofs_exit_sysfs();
918 z_erofs_exit_subsystem();
919 erofs_exit_shrinker();
920 kmem_cache_destroy(erofs_inode_cachep);
921 }
922
erofs_statfs(struct dentry * dentry,struct kstatfs * buf)923 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
924 {
925 struct super_block *sb = dentry->d_sb;
926 struct erofs_sb_info *sbi = EROFS_SB(sb);
927
928 buf->f_type = sb->s_magic;
929 buf->f_bsize = sb->s_blocksize;
930 buf->f_blocks = sbi->total_blocks;
931 buf->f_bfree = buf->f_bavail = 0;
932 buf->f_files = ULLONG_MAX;
933 buf->f_ffree = ULLONG_MAX - sbi->inos;
934 buf->f_namelen = EROFS_NAME_LEN;
935
936 if (uuid_is_null(&sb->s_uuid))
937 buf->f_fsid = u64_to_fsid(!sb->s_bdev ? 0 :
938 huge_encode_dev(sb->s_bdev->bd_dev));
939 else
940 buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
941 return 0;
942 }
943
erofs_show_options(struct seq_file * seq,struct dentry * root)944 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
945 {
946 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
947 struct erofs_mount_opts *opt = &sbi->opt;
948
949 if (IS_ENABLED(CONFIG_EROFS_FS_XATTR))
950 seq_puts(seq, test_opt(opt, XATTR_USER) ?
951 ",user_xattr" : ",nouser_xattr");
952 if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL))
953 seq_puts(seq, test_opt(opt, POSIX_ACL) ? ",acl" : ",noacl");
954 if (IS_ENABLED(CONFIG_EROFS_FS_ZIP))
955 seq_printf(seq, ",cache_strategy=%s",
956 erofs_param_cache_strategy[opt->cache_strategy].name);
957 if (test_opt(opt, DAX_ALWAYS))
958 seq_puts(seq, ",dax=always");
959 if (test_opt(opt, DAX_NEVER))
960 seq_puts(seq, ",dax=never");
961 #ifdef CONFIG_EROFS_FS_ONDEMAND
962 if (sbi->fsid)
963 seq_printf(seq, ",fsid=%s", sbi->fsid);
964 if (sbi->domain_id)
965 seq_printf(seq, ",domain_id=%s", sbi->domain_id);
966 #endif
967 return 0;
968 }
969
970 const struct super_operations erofs_sops = {
971 .put_super = erofs_put_super,
972 .alloc_inode = erofs_alloc_inode,
973 .free_inode = erofs_free_inode,
974 .statfs = erofs_statfs,
975 .show_options = erofs_show_options,
976 };
977
978 module_init(erofs_module_init);
979 module_exit(erofs_module_exit);
980
981 MODULE_DESCRIPTION("Enhanced ROM File System");
982 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
983 MODULE_LICENSE("GPL");
984