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