super.c 25 KB

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