super.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017-2018 HUAWEI, Inc.
  4. * https://www.huawei.com/
  5. * Created by Gao Xiang <gaoxiang25@huawei.com>
  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 "xattr.h"
  16. #define CREATE_TRACE_POINTS
  17. #include <trace/events/erofs.h>
  18. static struct kmem_cache *erofs_inode_cachep __read_mostly;
  19. void _erofs_err(struct super_block *sb, const char *function,
  20. const char *fmt, ...)
  21. {
  22. struct va_format vaf;
  23. va_list args;
  24. va_start(args, fmt);
  25. vaf.fmt = fmt;
  26. vaf.va = &args;
  27. pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
  28. va_end(args);
  29. }
  30. void _erofs_info(struct super_block *sb, const char *function,
  31. const char *fmt, ...)
  32. {
  33. struct va_format vaf;
  34. va_list args;
  35. va_start(args, fmt);
  36. vaf.fmt = fmt;
  37. vaf.va = &args;
  38. pr_info("(device %s): %pV", sb->s_id, &vaf);
  39. va_end(args);
  40. }
  41. static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
  42. {
  43. struct erofs_super_block *dsb;
  44. u32 expected_crc, crc;
  45. dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET,
  46. EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL);
  47. if (!dsb)
  48. return -ENOMEM;
  49. expected_crc = le32_to_cpu(dsb->checksum);
  50. dsb->checksum = 0;
  51. /* to allow for x86 boot sectors and other oddities. */
  52. crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET);
  53. kfree(dsb);
  54. if (crc != expected_crc) {
  55. erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
  56. crc, expected_crc);
  57. return -EBADMSG;
  58. }
  59. return 0;
  60. }
  61. static void erofs_inode_init_once(void *ptr)
  62. {
  63. struct erofs_inode *vi = ptr;
  64. inode_init_once(&vi->vfs_inode);
  65. }
  66. static struct inode *erofs_alloc_inode(struct super_block *sb)
  67. {
  68. struct erofs_inode *vi =
  69. kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
  70. if (!vi)
  71. return NULL;
  72. /* zero out everything except vfs_inode */
  73. memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
  74. return &vi->vfs_inode;
  75. }
  76. static void erofs_free_inode(struct inode *inode)
  77. {
  78. struct erofs_inode *vi = EROFS_I(inode);
  79. /* be careful of RCU symlink path */
  80. if (inode->i_op == &erofs_fast_symlink_iops)
  81. kfree(inode->i_link);
  82. kfree(vi->xattr_shared_xattrs);
  83. kmem_cache_free(erofs_inode_cachep, vi);
  84. }
  85. static bool check_layout_compatibility(struct super_block *sb,
  86. struct erofs_super_block *dsb)
  87. {
  88. const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
  89. EROFS_SB(sb)->feature_incompat = feature;
  90. /* check if current kernel meets all mandatory requirements */
  91. if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
  92. erofs_err(sb,
  93. "unidentified incompatible feature %x, please upgrade kernel version",
  94. feature & ~EROFS_ALL_FEATURE_INCOMPAT);
  95. return false;
  96. }
  97. return true;
  98. }
  99. #ifdef CONFIG_EROFS_FS_ZIP
  100. /* read variable-sized metadata, offset will be aligned by 4-byte */
  101. static void *erofs_read_metadata(struct super_block *sb, struct page **pagep,
  102. erofs_off_t *offset, int *lengthp)
  103. {
  104. struct page *page = *pagep;
  105. u8 *buffer, *ptr;
  106. int len, i, cnt;
  107. erofs_blk_t blk;
  108. *offset = round_up(*offset, 4);
  109. blk = erofs_blknr(*offset);
  110. if (!page || page->index != blk) {
  111. if (page) {
  112. unlock_page(page);
  113. put_page(page);
  114. }
  115. page = erofs_get_meta_page(sb, blk);
  116. if (IS_ERR(page))
  117. goto err_nullpage;
  118. }
  119. ptr = kmap(page);
  120. len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(*offset)]);
  121. if (!len)
  122. len = U16_MAX + 1;
  123. buffer = kmalloc(len, GFP_KERNEL);
  124. if (!buffer) {
  125. buffer = ERR_PTR(-ENOMEM);
  126. goto out;
  127. }
  128. *offset += sizeof(__le16);
  129. *lengthp = len;
  130. for (i = 0; i < len; i += cnt) {
  131. cnt = min(EROFS_BLKSIZ - (int)erofs_blkoff(*offset), len - i);
  132. blk = erofs_blknr(*offset);
  133. if (!page || page->index != blk) {
  134. if (page) {
  135. kunmap(page);
  136. unlock_page(page);
  137. put_page(page);
  138. }
  139. page = erofs_get_meta_page(sb, blk);
  140. if (IS_ERR(page)) {
  141. kfree(buffer);
  142. goto err_nullpage;
  143. }
  144. ptr = kmap(page);
  145. }
  146. memcpy(buffer + i, ptr + erofs_blkoff(*offset), cnt);
  147. *offset += cnt;
  148. }
  149. out:
  150. kunmap(page);
  151. *pagep = page;
  152. return buffer;
  153. err_nullpage:
  154. *pagep = NULL;
  155. return page;
  156. }
  157. static int erofs_load_compr_cfgs(struct super_block *sb,
  158. struct erofs_super_block *dsb)
  159. {
  160. struct erofs_sb_info *sbi;
  161. struct page *page;
  162. unsigned int algs, alg;
  163. erofs_off_t offset;
  164. int size, ret;
  165. sbi = EROFS_SB(sb);
  166. sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
  167. if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
  168. erofs_err(sb, "try to load compressed fs with unsupported algorithms %x",
  169. sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
  170. return -EINVAL;
  171. }
  172. offset = EROFS_SUPER_OFFSET + sbi->sb_size;
  173. page = NULL;
  174. alg = 0;
  175. ret = 0;
  176. for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
  177. void *data;
  178. if (!(algs & 1))
  179. continue;
  180. data = erofs_read_metadata(sb, &page, &offset, &size);
  181. if (IS_ERR(data)) {
  182. ret = PTR_ERR(data);
  183. goto err;
  184. }
  185. switch (alg) {
  186. case Z_EROFS_COMPRESSION_LZ4:
  187. ret = z_erofs_load_lz4_config(sb, dsb, data, size);
  188. break;
  189. default:
  190. DBG_BUGON(1);
  191. ret = -EFAULT;
  192. }
  193. kfree(data);
  194. if (ret)
  195. goto err;
  196. }
  197. err:
  198. if (page) {
  199. unlock_page(page);
  200. put_page(page);
  201. }
  202. return ret;
  203. }
  204. #else
  205. static int erofs_load_compr_cfgs(struct super_block *sb,
  206. struct erofs_super_block *dsb)
  207. {
  208. if (dsb->u1.available_compr_algs) {
  209. erofs_err(sb, "try to load compressed fs when compression is disabled");
  210. return -EINVAL;
  211. }
  212. return 0;
  213. }
  214. #endif
  215. static int erofs_read_superblock(struct super_block *sb)
  216. {
  217. struct erofs_sb_info *sbi;
  218. struct page *page;
  219. struct erofs_super_block *dsb;
  220. unsigned int blkszbits;
  221. void *data;
  222. int ret;
  223. page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL);
  224. if (IS_ERR(page)) {
  225. erofs_err(sb, "cannot read erofs superblock");
  226. return PTR_ERR(page);
  227. }
  228. sbi = EROFS_SB(sb);
  229. data = kmap(page);
  230. dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
  231. ret = -EINVAL;
  232. if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
  233. erofs_err(sb, "cannot find valid erofs superblock");
  234. goto out;
  235. }
  236. sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
  237. if (erofs_sb_has_sb_chksum(sbi)) {
  238. ret = erofs_superblock_csum_verify(sb, data);
  239. if (ret)
  240. goto out;
  241. }
  242. ret = -EINVAL;
  243. blkszbits = dsb->blkszbits;
  244. /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
  245. if (blkszbits != LOG_BLOCK_SIZE) {
  246. erofs_err(sb, "blkszbits %u isn't supported on this platform",
  247. blkszbits);
  248. goto out;
  249. }
  250. if (!check_layout_compatibility(sb, dsb))
  251. goto out;
  252. sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
  253. if (sbi->sb_size > EROFS_BLKSIZ) {
  254. erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
  255. sbi->sb_size);
  256. goto out;
  257. }
  258. sbi->blocks = le32_to_cpu(dsb->blocks);
  259. sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
  260. #ifdef CONFIG_EROFS_FS_XATTR
  261. sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
  262. #endif
  263. sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
  264. sbi->root_nid = le16_to_cpu(dsb->root_nid);
  265. sbi->inos = le64_to_cpu(dsb->inos);
  266. sbi->build_time = le64_to_cpu(dsb->build_time);
  267. sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
  268. memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
  269. ret = strscpy(sbi->volume_name, dsb->volume_name,
  270. sizeof(dsb->volume_name));
  271. if (ret < 0) { /* -E2BIG */
  272. erofs_err(sb, "bad volume name without NIL terminator");
  273. ret = -EFSCORRUPTED;
  274. goto out;
  275. }
  276. /* parse on-disk compression configurations */
  277. if (erofs_sb_has_compr_cfgs(sbi))
  278. ret = erofs_load_compr_cfgs(sb, dsb);
  279. else
  280. ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
  281. out:
  282. kunmap(page);
  283. put_page(page);
  284. return ret;
  285. }
  286. /* set up default EROFS parameters */
  287. static void erofs_default_options(struct erofs_fs_context *ctx)
  288. {
  289. #ifdef CONFIG_EROFS_FS_ZIP
  290. ctx->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
  291. ctx->max_sync_decompress_pages = 3;
  292. ctx->readahead_sync_decompress = false;
  293. #endif
  294. #ifdef CONFIG_EROFS_FS_XATTR
  295. set_opt(ctx, XATTR_USER);
  296. #endif
  297. #ifdef CONFIG_EROFS_FS_POSIX_ACL
  298. set_opt(ctx, POSIX_ACL);
  299. #endif
  300. }
  301. enum {
  302. Opt_user_xattr,
  303. Opt_acl,
  304. Opt_cache_strategy,
  305. Opt_err
  306. };
  307. static const struct constant_table erofs_param_cache_strategy[] = {
  308. {"disabled", EROFS_ZIP_CACHE_DISABLED},
  309. {"readahead", EROFS_ZIP_CACHE_READAHEAD},
  310. {"readaround", EROFS_ZIP_CACHE_READAROUND},
  311. {}
  312. };
  313. static const struct fs_parameter_spec erofs_fs_parameters[] = {
  314. fsparam_flag_no("user_xattr", Opt_user_xattr),
  315. fsparam_flag_no("acl", Opt_acl),
  316. fsparam_enum("cache_strategy", Opt_cache_strategy,
  317. erofs_param_cache_strategy),
  318. {}
  319. };
  320. static int erofs_fc_parse_param(struct fs_context *fc,
  321. struct fs_parameter *param)
  322. {
  323. struct erofs_fs_context *ctx __maybe_unused = fc->fs_private;
  324. struct fs_parse_result result;
  325. int opt;
  326. opt = fs_parse(fc, erofs_fs_parameters, param, &result);
  327. if (opt < 0)
  328. return opt;
  329. switch (opt) {
  330. case Opt_user_xattr:
  331. #ifdef CONFIG_EROFS_FS_XATTR
  332. if (result.boolean)
  333. set_opt(ctx, XATTR_USER);
  334. else
  335. clear_opt(ctx, XATTR_USER);
  336. #else
  337. errorfc(fc, "{,no}user_xattr options not supported");
  338. #endif
  339. break;
  340. case Opt_acl:
  341. #ifdef CONFIG_EROFS_FS_POSIX_ACL
  342. if (result.boolean)
  343. set_opt(ctx, POSIX_ACL);
  344. else
  345. clear_opt(ctx, POSIX_ACL);
  346. #else
  347. errorfc(fc, "{,no}acl options not supported");
  348. #endif
  349. break;
  350. case Opt_cache_strategy:
  351. #ifdef CONFIG_EROFS_FS_ZIP
  352. ctx->cache_strategy = result.uint_32;
  353. #else
  354. errorfc(fc, "compression not supported, cache_strategy ignored");
  355. #endif
  356. break;
  357. default:
  358. return -ENOPARAM;
  359. }
  360. return 0;
  361. }
  362. #ifdef CONFIG_EROFS_FS_ZIP
  363. static const struct address_space_operations managed_cache_aops;
  364. static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
  365. {
  366. int ret = 1; /* 0 - busy */
  367. struct address_space *const mapping = page->mapping;
  368. DBG_BUGON(!PageLocked(page));
  369. DBG_BUGON(mapping->a_ops != &managed_cache_aops);
  370. if (PagePrivate(page))
  371. ret = erofs_try_to_free_cached_page(mapping, page);
  372. return ret;
  373. }
  374. static void erofs_managed_cache_invalidatepage(struct page *page,
  375. unsigned int offset,
  376. unsigned int length)
  377. {
  378. const unsigned int stop = length + offset;
  379. DBG_BUGON(!PageLocked(page));
  380. /* Check for potential overflow in debug mode */
  381. DBG_BUGON(stop > PAGE_SIZE || stop < length);
  382. if (offset == 0 && stop == PAGE_SIZE)
  383. while (!erofs_managed_cache_releasepage(page, GFP_NOFS))
  384. cond_resched();
  385. }
  386. static const struct address_space_operations managed_cache_aops = {
  387. .releasepage = erofs_managed_cache_releasepage,
  388. .invalidatepage = erofs_managed_cache_invalidatepage,
  389. };
  390. static int erofs_init_managed_cache(struct super_block *sb)
  391. {
  392. struct erofs_sb_info *const sbi = EROFS_SB(sb);
  393. struct inode *const inode = new_inode(sb);
  394. if (!inode)
  395. return -ENOMEM;
  396. set_nlink(inode, 1);
  397. inode->i_size = OFFSET_MAX;
  398. inode->i_mapping->a_ops = &managed_cache_aops;
  399. mapping_set_gfp_mask(inode->i_mapping,
  400. GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
  401. sbi->managed_cache = inode;
  402. return 0;
  403. }
  404. #else
  405. static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
  406. #endif
  407. static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
  408. {
  409. struct inode *inode;
  410. struct erofs_sb_info *sbi;
  411. struct erofs_fs_context *ctx = fc->fs_private;
  412. int err;
  413. sb->s_magic = EROFS_SUPER_MAGIC;
  414. if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
  415. erofs_err(sb, "failed to set erofs blksize");
  416. return -EINVAL;
  417. }
  418. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  419. if (!sbi)
  420. return -ENOMEM;
  421. sb->s_fs_info = sbi;
  422. err = erofs_read_superblock(sb);
  423. if (err)
  424. return err;
  425. sb->s_flags |= SB_RDONLY | SB_NOATIME;
  426. sb->s_maxbytes = MAX_LFS_FILESIZE;
  427. sb->s_time_gran = 1;
  428. sb->s_op = &erofs_sops;
  429. sb->s_xattr = erofs_xattr_handlers;
  430. if (test_opt(ctx, POSIX_ACL))
  431. sb->s_flags |= SB_POSIXACL;
  432. else
  433. sb->s_flags &= ~SB_POSIXACL;
  434. sbi->ctx = *ctx;
  435. #ifdef CONFIG_EROFS_FS_ZIP
  436. xa_init(&sbi->managed_pslots);
  437. #endif
  438. /* get the root inode */
  439. inode = erofs_iget(sb, ROOT_NID(sbi), true);
  440. if (IS_ERR(inode))
  441. return PTR_ERR(inode);
  442. if (!S_ISDIR(inode->i_mode)) {
  443. erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
  444. ROOT_NID(sbi), inode->i_mode);
  445. iput(inode);
  446. return -EINVAL;
  447. }
  448. sb->s_root = d_make_root(inode);
  449. if (!sb->s_root)
  450. return -ENOMEM;
  451. erofs_shrinker_register(sb);
  452. /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
  453. err = erofs_init_managed_cache(sb);
  454. if (err)
  455. return err;
  456. erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
  457. return 0;
  458. }
  459. static int erofs_fc_get_tree(struct fs_context *fc)
  460. {
  461. return get_tree_bdev(fc, erofs_fc_fill_super);
  462. }
  463. static int erofs_fc_reconfigure(struct fs_context *fc)
  464. {
  465. struct super_block *sb = fc->root->d_sb;
  466. struct erofs_sb_info *sbi = EROFS_SB(sb);
  467. struct erofs_fs_context *ctx = fc->fs_private;
  468. DBG_BUGON(!sb_rdonly(sb));
  469. if (test_opt(ctx, POSIX_ACL))
  470. fc->sb_flags |= SB_POSIXACL;
  471. else
  472. fc->sb_flags &= ~SB_POSIXACL;
  473. sbi->ctx = *ctx;
  474. fc->sb_flags |= SB_RDONLY;
  475. return 0;
  476. }
  477. static void erofs_fc_free(struct fs_context *fc)
  478. {
  479. kfree(fc->fs_private);
  480. }
  481. static const struct fs_context_operations erofs_context_ops = {
  482. .parse_param = erofs_fc_parse_param,
  483. .get_tree = erofs_fc_get_tree,
  484. .reconfigure = erofs_fc_reconfigure,
  485. .free = erofs_fc_free,
  486. };
  487. static int erofs_init_fs_context(struct fs_context *fc)
  488. {
  489. fc->fs_private = kzalloc(sizeof(struct erofs_fs_context), GFP_KERNEL);
  490. if (!fc->fs_private)
  491. return -ENOMEM;
  492. /* set default mount options */
  493. erofs_default_options(fc->fs_private);
  494. fc->ops = &erofs_context_ops;
  495. return 0;
  496. }
  497. /*
  498. * could be triggered after deactivate_locked_super()
  499. * is called, thus including umount and failed to initialize.
  500. */
  501. static void erofs_kill_sb(struct super_block *sb)
  502. {
  503. struct erofs_sb_info *sbi;
  504. WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
  505. kill_block_super(sb);
  506. sbi = EROFS_SB(sb);
  507. if (!sbi)
  508. return;
  509. kfree(sbi);
  510. sb->s_fs_info = NULL;
  511. }
  512. /* called when ->s_root is non-NULL */
  513. static void erofs_put_super(struct super_block *sb)
  514. {
  515. struct erofs_sb_info *const sbi = EROFS_SB(sb);
  516. DBG_BUGON(!sbi);
  517. erofs_shrinker_unregister(sb);
  518. #ifdef CONFIG_EROFS_FS_ZIP
  519. iput(sbi->managed_cache);
  520. sbi->managed_cache = NULL;
  521. #endif
  522. }
  523. static struct file_system_type erofs_fs_type = {
  524. .owner = THIS_MODULE,
  525. .name = "erofs",
  526. .init_fs_context = erofs_init_fs_context,
  527. .kill_sb = erofs_kill_sb,
  528. .fs_flags = FS_REQUIRES_DEV,
  529. };
  530. MODULE_ALIAS_FS("erofs");
  531. static int __init erofs_module_init(void)
  532. {
  533. int err;
  534. erofs_check_ondisk_layout_definitions();
  535. erofs_inode_cachep = kmem_cache_create("erofs_inode",
  536. sizeof(struct erofs_inode), 0,
  537. SLAB_RECLAIM_ACCOUNT,
  538. erofs_inode_init_once);
  539. if (!erofs_inode_cachep) {
  540. err = -ENOMEM;
  541. goto icache_err;
  542. }
  543. err = erofs_init_shrinker();
  544. if (err)
  545. goto shrinker_err;
  546. erofs_pcpubuf_init();
  547. err = z_erofs_init_zip_subsystem();
  548. if (err)
  549. goto zip_err;
  550. err = register_filesystem(&erofs_fs_type);
  551. if (err)
  552. goto fs_err;
  553. return 0;
  554. fs_err:
  555. z_erofs_exit_zip_subsystem();
  556. zip_err:
  557. erofs_exit_shrinker();
  558. shrinker_err:
  559. kmem_cache_destroy(erofs_inode_cachep);
  560. icache_err:
  561. return err;
  562. }
  563. static void __exit erofs_module_exit(void)
  564. {
  565. unregister_filesystem(&erofs_fs_type);
  566. z_erofs_exit_zip_subsystem();
  567. erofs_exit_shrinker();
  568. /* Ensure all RCU free inodes are safe before cache is destroyed. */
  569. rcu_barrier();
  570. kmem_cache_destroy(erofs_inode_cachep);
  571. erofs_pcpubuf_exit();
  572. }
  573. /* get filesystem statistics */
  574. static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
  575. {
  576. struct super_block *sb = dentry->d_sb;
  577. struct erofs_sb_info *sbi = EROFS_SB(sb);
  578. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  579. buf->f_type = sb->s_magic;
  580. buf->f_bsize = EROFS_BLKSIZ;
  581. buf->f_blocks = sbi->blocks;
  582. buf->f_bfree = buf->f_bavail = 0;
  583. buf->f_files = ULLONG_MAX;
  584. buf->f_ffree = ULLONG_MAX - sbi->inos;
  585. buf->f_namelen = EROFS_NAME_LEN;
  586. buf->f_fsid = u64_to_fsid(id);
  587. return 0;
  588. }
  589. static int erofs_show_options(struct seq_file *seq, struct dentry *root)
  590. {
  591. struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
  592. struct erofs_fs_context *ctx __maybe_unused = &sbi->ctx;
  593. #ifdef CONFIG_EROFS_FS_XATTR
  594. if (test_opt(ctx, XATTR_USER))
  595. seq_puts(seq, ",user_xattr");
  596. else
  597. seq_puts(seq, ",nouser_xattr");
  598. #endif
  599. #ifdef CONFIG_EROFS_FS_POSIX_ACL
  600. if (test_opt(ctx, POSIX_ACL))
  601. seq_puts(seq, ",acl");
  602. else
  603. seq_puts(seq, ",noacl");
  604. #endif
  605. #ifdef CONFIG_EROFS_FS_ZIP
  606. if (ctx->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
  607. seq_puts(seq, ",cache_strategy=disabled");
  608. else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
  609. seq_puts(seq, ",cache_strategy=readahead");
  610. else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
  611. seq_puts(seq, ",cache_strategy=readaround");
  612. #endif
  613. return 0;
  614. }
  615. const struct super_operations erofs_sops = {
  616. .put_super = erofs_put_super,
  617. .alloc_inode = erofs_alloc_inode,
  618. .free_inode = erofs_free_inode,
  619. .statfs = erofs_statfs,
  620. .show_options = erofs_show_options,
  621. };
  622. module_init(erofs_module_init);
  623. module_exit(erofs_module_exit);
  624. MODULE_DESCRIPTION("Enhanced ROM File System");
  625. MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
  626. MODULE_LICENSE("GPL");
  627. MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);