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