super.c 26 KB

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