Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2017-2018 HUAWEI, Inc.
0004  *             https://www.huawei.com/
0005  * Copyright (C) 2021, Alibaba Cloud
0006  */
0007 #include <linux/module.h>
0008 #include <linux/buffer_head.h>
0009 #include <linux/statfs.h>
0010 #include <linux/parser.h>
0011 #include <linux/seq_file.h>
0012 #include <linux/crc32c.h>
0013 #include <linux/fs_context.h>
0014 #include <linux/fs_parser.h>
0015 #include <linux/dax.h>
0016 #include <linux/exportfs.h>
0017 #include "xattr.h"
0018 
0019 #define CREATE_TRACE_POINTS
0020 #include <trace/events/erofs.h>
0021 
0022 static struct kmem_cache *erofs_inode_cachep __read_mostly;
0023 
0024 void _erofs_err(struct super_block *sb, const char *function,
0025         const char *fmt, ...)
0026 {
0027     struct va_format vaf;
0028     va_list args;
0029 
0030     va_start(args, fmt);
0031 
0032     vaf.fmt = fmt;
0033     vaf.va = &args;
0034 
0035     pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
0036     va_end(args);
0037 }
0038 
0039 void _erofs_info(struct super_block *sb, const char *function,
0040          const char *fmt, ...)
0041 {
0042     struct va_format vaf;
0043     va_list args;
0044 
0045     va_start(args, fmt);
0046 
0047     vaf.fmt = fmt;
0048     vaf.va = &args;
0049 
0050     pr_info("(device %s): %pV", sb->s_id, &vaf);
0051     va_end(args);
0052 }
0053 
0054 static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
0055 {
0056     struct erofs_super_block *dsb;
0057     u32 expected_crc, crc;
0058 
0059     dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET,
0060               EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL);
0061     if (!dsb)
0062         return -ENOMEM;
0063 
0064     expected_crc = le32_to_cpu(dsb->checksum);
0065     dsb->checksum = 0;
0066     /* to allow for x86 boot sectors and other oddities. */
0067     crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET);
0068     kfree(dsb);
0069 
0070     if (crc != expected_crc) {
0071         erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
0072               crc, expected_crc);
0073         return -EBADMSG;
0074     }
0075     return 0;
0076 }
0077 
0078 static void erofs_inode_init_once(void *ptr)
0079 {
0080     struct erofs_inode *vi = ptr;
0081 
0082     inode_init_once(&vi->vfs_inode);
0083 }
0084 
0085 static struct inode *erofs_alloc_inode(struct super_block *sb)
0086 {
0087     struct erofs_inode *vi =
0088         alloc_inode_sb(sb, erofs_inode_cachep, GFP_KERNEL);
0089 
0090     if (!vi)
0091         return NULL;
0092 
0093     /* zero out everything except vfs_inode */
0094     memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
0095     return &vi->vfs_inode;
0096 }
0097 
0098 static void erofs_free_inode(struct inode *inode)
0099 {
0100     struct erofs_inode *vi = EROFS_I(inode);
0101 
0102     /* be careful of RCU symlink path */
0103     if (inode->i_op == &erofs_fast_symlink_iops)
0104         kfree(inode->i_link);
0105     kfree(vi->xattr_shared_xattrs);
0106 
0107     kmem_cache_free(erofs_inode_cachep, vi);
0108 }
0109 
0110 static bool check_layout_compatibility(struct super_block *sb,
0111                        struct erofs_super_block *dsb)
0112 {
0113     const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
0114 
0115     EROFS_SB(sb)->feature_incompat = feature;
0116 
0117     /* check if current kernel meets all mandatory requirements */
0118     if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
0119         erofs_err(sb,
0120               "unidentified incompatible feature %x, please upgrade kernel version",
0121                feature & ~EROFS_ALL_FEATURE_INCOMPAT);
0122         return false;
0123     }
0124     return true;
0125 }
0126 
0127 #ifdef CONFIG_EROFS_FS_ZIP
0128 /* read variable-sized metadata, offset will be aligned by 4-byte */
0129 static void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
0130                  erofs_off_t *offset, int *lengthp)
0131 {
0132     u8 *buffer, *ptr;
0133     int len, i, cnt;
0134 
0135     *offset = round_up(*offset, 4);
0136     ptr = erofs_read_metabuf(buf, sb, erofs_blknr(*offset), EROFS_KMAP);
0137     if (IS_ERR(ptr))
0138         return ptr;
0139 
0140     len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(*offset)]);
0141     if (!len)
0142         len = U16_MAX + 1;
0143     buffer = kmalloc(len, GFP_KERNEL);
0144     if (!buffer)
0145         return ERR_PTR(-ENOMEM);
0146     *offset += sizeof(__le16);
0147     *lengthp = len;
0148 
0149     for (i = 0; i < len; i += cnt) {
0150         cnt = min(EROFS_BLKSIZ - (int)erofs_blkoff(*offset), len - i);
0151         ptr = erofs_read_metabuf(buf, sb, erofs_blknr(*offset),
0152                      EROFS_KMAP);
0153         if (IS_ERR(ptr)) {
0154             kfree(buffer);
0155             return ptr;
0156         }
0157         memcpy(buffer + i, ptr + erofs_blkoff(*offset), cnt);
0158         *offset += cnt;
0159     }
0160     return buffer;
0161 }
0162 
0163 static int erofs_load_compr_cfgs(struct super_block *sb,
0164                  struct erofs_super_block *dsb)
0165 {
0166     struct erofs_sb_info *sbi = EROFS_SB(sb);
0167     struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
0168     unsigned int algs, alg;
0169     erofs_off_t offset;
0170     int size, ret = 0;
0171 
0172     sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
0173     if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
0174         erofs_err(sb, "try to load compressed fs with unsupported algorithms %x",
0175               sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
0176         return -EINVAL;
0177     }
0178 
0179     offset = EROFS_SUPER_OFFSET + sbi->sb_size;
0180     alg = 0;
0181     for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
0182         void *data;
0183 
0184         if (!(algs & 1))
0185             continue;
0186 
0187         data = erofs_read_metadata(sb, &buf, &offset, &size);
0188         if (IS_ERR(data)) {
0189             ret = PTR_ERR(data);
0190             break;
0191         }
0192 
0193         switch (alg) {
0194         case Z_EROFS_COMPRESSION_LZ4:
0195             ret = z_erofs_load_lz4_config(sb, dsb, data, size);
0196             break;
0197         case Z_EROFS_COMPRESSION_LZMA:
0198             ret = z_erofs_load_lzma_config(sb, dsb, data, size);
0199             break;
0200         default:
0201             DBG_BUGON(1);
0202             ret = -EFAULT;
0203         }
0204         kfree(data);
0205         if (ret)
0206             break;
0207     }
0208     erofs_put_metabuf(&buf);
0209     return ret;
0210 }
0211 #else
0212 static int erofs_load_compr_cfgs(struct super_block *sb,
0213                  struct erofs_super_block *dsb)
0214 {
0215     if (dsb->u1.available_compr_algs) {
0216         erofs_err(sb, "try to load compressed fs when compression is disabled");
0217         return -EINVAL;
0218     }
0219     return 0;
0220 }
0221 #endif
0222 
0223 static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
0224                  struct erofs_device_info *dif, erofs_off_t *pos)
0225 {
0226     struct erofs_sb_info *sbi = EROFS_SB(sb);
0227     struct erofs_deviceslot *dis;
0228     struct block_device *bdev;
0229     void *ptr;
0230     int ret;
0231 
0232     ptr = erofs_read_metabuf(buf, sb, erofs_blknr(*pos), EROFS_KMAP);
0233     if (IS_ERR(ptr))
0234         return PTR_ERR(ptr);
0235     dis = ptr + erofs_blkoff(*pos);
0236 
0237     if (!dif->path) {
0238         if (!dis->tag[0]) {
0239             erofs_err(sb, "empty device tag @ pos %llu", *pos);
0240             return -EINVAL;
0241         }
0242         dif->path = kmemdup_nul(dis->tag, sizeof(dis->tag), GFP_KERNEL);
0243         if (!dif->path)
0244             return -ENOMEM;
0245     }
0246 
0247     if (erofs_is_fscache_mode(sb)) {
0248         ret = erofs_fscache_register_cookie(sb, &dif->fscache,
0249                 dif->path, false);
0250         if (ret)
0251             return ret;
0252     } else {
0253         bdev = blkdev_get_by_path(dif->path, FMODE_READ | FMODE_EXCL,
0254                       sb->s_type);
0255         if (IS_ERR(bdev))
0256             return PTR_ERR(bdev);
0257         dif->bdev = bdev;
0258         dif->dax_dev = fs_dax_get_by_bdev(bdev, &dif->dax_part_off,
0259                           NULL, NULL);
0260     }
0261 
0262     dif->blocks = le32_to_cpu(dis->blocks);
0263     dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr);
0264     sbi->total_blocks += dif->blocks;
0265     *pos += EROFS_DEVT_SLOT_SIZE;
0266     return 0;
0267 }
0268 
0269 static int erofs_scan_devices(struct super_block *sb,
0270                   struct erofs_super_block *dsb)
0271 {
0272     struct erofs_sb_info *sbi = EROFS_SB(sb);
0273     unsigned int ondisk_extradevs;
0274     erofs_off_t pos;
0275     struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
0276     struct erofs_device_info *dif;
0277     int id, err = 0;
0278 
0279     sbi->total_blocks = sbi->primarydevice_blocks;
0280     if (!erofs_sb_has_device_table(sbi))
0281         ondisk_extradevs = 0;
0282     else
0283         ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
0284 
0285     if (sbi->devs->extra_devices &&
0286         ondisk_extradevs != sbi->devs->extra_devices) {
0287         erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
0288               ondisk_extradevs, sbi->devs->extra_devices);
0289         return -EINVAL;
0290     }
0291     if (!ondisk_extradevs)
0292         return 0;
0293 
0294     sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
0295     pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
0296     down_read(&sbi->devs->rwsem);
0297     if (sbi->devs->extra_devices) {
0298         idr_for_each_entry(&sbi->devs->tree, dif, id) {
0299             err = erofs_init_device(&buf, sb, dif, &pos);
0300             if (err)
0301                 break;
0302         }
0303     } else {
0304         for (id = 0; id < ondisk_extradevs; id++) {
0305             dif = kzalloc(sizeof(*dif), GFP_KERNEL);
0306             if (!dif) {
0307                 err = -ENOMEM;
0308                 break;
0309             }
0310 
0311             err = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
0312             if (err < 0) {
0313                 kfree(dif);
0314                 break;
0315             }
0316             ++sbi->devs->extra_devices;
0317 
0318             err = erofs_init_device(&buf, sb, dif, &pos);
0319             if (err)
0320                 break;
0321         }
0322     }
0323     up_read(&sbi->devs->rwsem);
0324     erofs_put_metabuf(&buf);
0325     return err;
0326 }
0327 
0328 static int erofs_read_superblock(struct super_block *sb)
0329 {
0330     struct erofs_sb_info *sbi;
0331     struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
0332     struct erofs_super_block *dsb;
0333     unsigned int blkszbits;
0334     void *data;
0335     int ret;
0336 
0337     data = erofs_read_metabuf(&buf, sb, 0, EROFS_KMAP);
0338     if (IS_ERR(data)) {
0339         erofs_err(sb, "cannot read erofs superblock");
0340         return PTR_ERR(data);
0341     }
0342 
0343     sbi = EROFS_SB(sb);
0344     dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
0345 
0346     ret = -EINVAL;
0347     if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
0348         erofs_err(sb, "cannot find valid erofs superblock");
0349         goto out;
0350     }
0351 
0352     sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
0353     if (erofs_sb_has_sb_chksum(sbi)) {
0354         ret = erofs_superblock_csum_verify(sb, data);
0355         if (ret)
0356             goto out;
0357     }
0358 
0359     ret = -EINVAL;
0360     blkszbits = dsb->blkszbits;
0361     /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
0362     if (blkszbits != LOG_BLOCK_SIZE) {
0363         erofs_err(sb, "blkszbits %u isn't supported on this platform",
0364               blkszbits);
0365         goto out;
0366     }
0367 
0368     if (!check_layout_compatibility(sb, dsb))
0369         goto out;
0370 
0371     sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
0372     if (sbi->sb_size > EROFS_BLKSIZ) {
0373         erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
0374               sbi->sb_size);
0375         goto out;
0376     }
0377     sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
0378     sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
0379 #ifdef CONFIG_EROFS_FS_XATTR
0380     sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
0381 #endif
0382     sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
0383     sbi->root_nid = le16_to_cpu(dsb->root_nid);
0384     sbi->inos = le64_to_cpu(dsb->inos);
0385 
0386     sbi->build_time = le64_to_cpu(dsb->build_time);
0387     sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
0388 
0389     memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
0390 
0391     ret = strscpy(sbi->volume_name, dsb->volume_name,
0392               sizeof(dsb->volume_name));
0393     if (ret < 0) {  /* -E2BIG */
0394         erofs_err(sb, "bad volume name without NIL terminator");
0395         ret = -EFSCORRUPTED;
0396         goto out;
0397     }
0398 
0399     /* parse on-disk compression configurations */
0400     if (erofs_sb_has_compr_cfgs(sbi))
0401         ret = erofs_load_compr_cfgs(sb, dsb);
0402     else
0403         ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
0404     if (ret < 0)
0405         goto out;
0406 
0407     /* handle multiple devices */
0408     ret = erofs_scan_devices(sb, dsb);
0409 
0410     if (erofs_sb_has_ztailpacking(sbi))
0411         erofs_info(sb, "EXPERIMENTAL compressed inline data feature in use. Use at your own risk!");
0412     if (erofs_is_fscache_mode(sb))
0413         erofs_info(sb, "EXPERIMENTAL fscache-based on-demand read feature in use. Use at your own risk!");
0414 out:
0415     erofs_put_metabuf(&buf);
0416     return ret;
0417 }
0418 
0419 /* set up default EROFS parameters */
0420 static void erofs_default_options(struct erofs_fs_context *ctx)
0421 {
0422 #ifdef CONFIG_EROFS_FS_ZIP
0423     ctx->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
0424     ctx->opt.max_sync_decompress_pages = 3;
0425     ctx->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
0426 #endif
0427 #ifdef CONFIG_EROFS_FS_XATTR
0428     set_opt(&ctx->opt, XATTR_USER);
0429 #endif
0430 #ifdef CONFIG_EROFS_FS_POSIX_ACL
0431     set_opt(&ctx->opt, POSIX_ACL);
0432 #endif
0433 }
0434 
0435 enum {
0436     Opt_user_xattr,
0437     Opt_acl,
0438     Opt_cache_strategy,
0439     Opt_dax,
0440     Opt_dax_enum,
0441     Opt_device,
0442     Opt_fsid,
0443     Opt_err
0444 };
0445 
0446 static const struct constant_table erofs_param_cache_strategy[] = {
0447     {"disabled",    EROFS_ZIP_CACHE_DISABLED},
0448     {"readahead",   EROFS_ZIP_CACHE_READAHEAD},
0449     {"readaround",  EROFS_ZIP_CACHE_READAROUND},
0450     {}
0451 };
0452 
0453 static const struct constant_table erofs_dax_param_enums[] = {
0454     {"always",  EROFS_MOUNT_DAX_ALWAYS},
0455     {"never",   EROFS_MOUNT_DAX_NEVER},
0456     {}
0457 };
0458 
0459 static const struct fs_parameter_spec erofs_fs_parameters[] = {
0460     fsparam_flag_no("user_xattr",   Opt_user_xattr),
0461     fsparam_flag_no("acl",      Opt_acl),
0462     fsparam_enum("cache_strategy",  Opt_cache_strategy,
0463              erofs_param_cache_strategy),
0464     fsparam_flag("dax",             Opt_dax),
0465     fsparam_enum("dax",     Opt_dax_enum, erofs_dax_param_enums),
0466     fsparam_string("device",    Opt_device),
0467     fsparam_string("fsid",      Opt_fsid),
0468     {}
0469 };
0470 
0471 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
0472 {
0473 #ifdef CONFIG_FS_DAX
0474     struct erofs_fs_context *ctx = fc->fs_private;
0475 
0476     switch (mode) {
0477     case EROFS_MOUNT_DAX_ALWAYS:
0478         warnfc(fc, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
0479         set_opt(&ctx->opt, DAX_ALWAYS);
0480         clear_opt(&ctx->opt, DAX_NEVER);
0481         return true;
0482     case EROFS_MOUNT_DAX_NEVER:
0483         set_opt(&ctx->opt, DAX_NEVER);
0484         clear_opt(&ctx->opt, DAX_ALWAYS);
0485         return true;
0486     default:
0487         DBG_BUGON(1);
0488         return false;
0489     }
0490 #else
0491     errorfc(fc, "dax options not supported");
0492     return false;
0493 #endif
0494 }
0495 
0496 static int erofs_fc_parse_param(struct fs_context *fc,
0497                 struct fs_parameter *param)
0498 {
0499     struct erofs_fs_context *ctx = fc->fs_private;
0500     struct fs_parse_result result;
0501     struct erofs_device_info *dif;
0502     int opt, ret;
0503 
0504     opt = fs_parse(fc, erofs_fs_parameters, param, &result);
0505     if (opt < 0)
0506         return opt;
0507 
0508     switch (opt) {
0509     case Opt_user_xattr:
0510 #ifdef CONFIG_EROFS_FS_XATTR
0511         if (result.boolean)
0512             set_opt(&ctx->opt, XATTR_USER);
0513         else
0514             clear_opt(&ctx->opt, XATTR_USER);
0515 #else
0516         errorfc(fc, "{,no}user_xattr options not supported");
0517 #endif
0518         break;
0519     case Opt_acl:
0520 #ifdef CONFIG_EROFS_FS_POSIX_ACL
0521         if (result.boolean)
0522             set_opt(&ctx->opt, POSIX_ACL);
0523         else
0524             clear_opt(&ctx->opt, POSIX_ACL);
0525 #else
0526         errorfc(fc, "{,no}acl options not supported");
0527 #endif
0528         break;
0529     case Opt_cache_strategy:
0530 #ifdef CONFIG_EROFS_FS_ZIP
0531         ctx->opt.cache_strategy = result.uint_32;
0532 #else
0533         errorfc(fc, "compression not supported, cache_strategy ignored");
0534 #endif
0535         break;
0536     case Opt_dax:
0537         if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
0538             return -EINVAL;
0539         break;
0540     case Opt_dax_enum:
0541         if (!erofs_fc_set_dax_mode(fc, result.uint_32))
0542             return -EINVAL;
0543         break;
0544     case Opt_device:
0545         dif = kzalloc(sizeof(*dif), GFP_KERNEL);
0546         if (!dif)
0547             return -ENOMEM;
0548         dif->path = kstrdup(param->string, GFP_KERNEL);
0549         if (!dif->path) {
0550             kfree(dif);
0551             return -ENOMEM;
0552         }
0553         down_write(&ctx->devs->rwsem);
0554         ret = idr_alloc(&ctx->devs->tree, dif, 0, 0, GFP_KERNEL);
0555         up_write(&ctx->devs->rwsem);
0556         if (ret < 0) {
0557             kfree(dif->path);
0558             kfree(dif);
0559             return ret;
0560         }
0561         ++ctx->devs->extra_devices;
0562         break;
0563     case Opt_fsid:
0564 #ifdef CONFIG_EROFS_FS_ONDEMAND
0565         kfree(ctx->opt.fsid);
0566         ctx->opt.fsid = kstrdup(param->string, GFP_KERNEL);
0567         if (!ctx->opt.fsid)
0568             return -ENOMEM;
0569 #else
0570         errorfc(fc, "fsid option not supported");
0571 #endif
0572         break;
0573     default:
0574         return -ENOPARAM;
0575     }
0576     return 0;
0577 }
0578 
0579 #ifdef CONFIG_EROFS_FS_ZIP
0580 static const struct address_space_operations managed_cache_aops;
0581 
0582 static bool erofs_managed_cache_release_folio(struct folio *folio, gfp_t gfp)
0583 {
0584     bool ret = true;
0585     struct address_space *const mapping = folio->mapping;
0586 
0587     DBG_BUGON(!folio_test_locked(folio));
0588     DBG_BUGON(mapping->a_ops != &managed_cache_aops);
0589 
0590     if (folio_test_private(folio))
0591         ret = erofs_try_to_free_cached_page(&folio->page);
0592 
0593     return ret;
0594 }
0595 
0596 /*
0597  * It will be called only on inode eviction. In case that there are still some
0598  * decompression requests in progress, wait with rescheduling for a bit here.
0599  * We could introduce an extra locking instead but it seems unnecessary.
0600  */
0601 static void erofs_managed_cache_invalidate_folio(struct folio *folio,
0602                            size_t offset, size_t length)
0603 {
0604     const size_t stop = length + offset;
0605 
0606     DBG_BUGON(!folio_test_locked(folio));
0607 
0608     /* Check for potential overflow in debug mode */
0609     DBG_BUGON(stop > folio_size(folio) || stop < length);
0610 
0611     if (offset == 0 && stop == folio_size(folio))
0612         while (!erofs_managed_cache_release_folio(folio, GFP_NOFS))
0613             cond_resched();
0614 }
0615 
0616 static const struct address_space_operations managed_cache_aops = {
0617     .release_folio = erofs_managed_cache_release_folio,
0618     .invalidate_folio = erofs_managed_cache_invalidate_folio,
0619 };
0620 
0621 static int erofs_init_managed_cache(struct super_block *sb)
0622 {
0623     struct erofs_sb_info *const sbi = EROFS_SB(sb);
0624     struct inode *const inode = new_inode(sb);
0625 
0626     if (!inode)
0627         return -ENOMEM;
0628 
0629     set_nlink(inode, 1);
0630     inode->i_size = OFFSET_MAX;
0631 
0632     inode->i_mapping->a_ops = &managed_cache_aops;
0633     mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
0634     sbi->managed_cache = inode;
0635     return 0;
0636 }
0637 #else
0638 static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
0639 #endif
0640 
0641 static struct inode *erofs_nfs_get_inode(struct super_block *sb,
0642                      u64 ino, u32 generation)
0643 {
0644     return erofs_iget(sb, ino, false);
0645 }
0646 
0647 static struct dentry *erofs_fh_to_dentry(struct super_block *sb,
0648         struct fid *fid, int fh_len, int fh_type)
0649 {
0650     return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
0651                     erofs_nfs_get_inode);
0652 }
0653 
0654 static struct dentry *erofs_fh_to_parent(struct super_block *sb,
0655         struct fid *fid, int fh_len, int fh_type)
0656 {
0657     return generic_fh_to_parent(sb, fid, fh_len, fh_type,
0658                     erofs_nfs_get_inode);
0659 }
0660 
0661 static struct dentry *erofs_get_parent(struct dentry *child)
0662 {
0663     erofs_nid_t nid;
0664     unsigned int d_type;
0665     int err;
0666 
0667     err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type);
0668     if (err)
0669         return ERR_PTR(err);
0670     return d_obtain_alias(erofs_iget(child->d_sb, nid, d_type == FT_DIR));
0671 }
0672 
0673 static const struct export_operations erofs_export_ops = {
0674     .fh_to_dentry = erofs_fh_to_dentry,
0675     .fh_to_parent = erofs_fh_to_parent,
0676     .get_parent = erofs_get_parent,
0677 };
0678 
0679 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
0680 {
0681     struct inode *inode;
0682     struct erofs_sb_info *sbi;
0683     struct erofs_fs_context *ctx = fc->fs_private;
0684     int err;
0685 
0686     sb->s_magic = EROFS_SUPER_MAGIC;
0687     sb->s_flags |= SB_RDONLY | SB_NOATIME;
0688     sb->s_maxbytes = MAX_LFS_FILESIZE;
0689     sb->s_op = &erofs_sops;
0690 
0691     sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
0692     if (!sbi)
0693         return -ENOMEM;
0694 
0695     sb->s_fs_info = sbi;
0696     sbi->opt = ctx->opt;
0697     ctx->opt.fsid = NULL;
0698     sbi->devs = ctx->devs;
0699     ctx->devs = NULL;
0700 
0701     if (erofs_is_fscache_mode(sb)) {
0702         sb->s_blocksize = EROFS_BLKSIZ;
0703         sb->s_blocksize_bits = LOG_BLOCK_SIZE;
0704 
0705         err = erofs_fscache_register_fs(sb);
0706         if (err)
0707             return err;
0708 
0709         err = erofs_fscache_register_cookie(sb, &sbi->s_fscache,
0710                             sbi->opt.fsid, true);
0711         if (err)
0712             return err;
0713 
0714         err = super_setup_bdi(sb);
0715         if (err)
0716             return err;
0717     } else {
0718         if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
0719             erofs_err(sb, "failed to set erofs blksize");
0720             return -EINVAL;
0721         }
0722 
0723         sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev,
0724                           &sbi->dax_part_off,
0725                           NULL, NULL);
0726     }
0727 
0728     err = erofs_read_superblock(sb);
0729     if (err)
0730         return err;
0731 
0732     if (test_opt(&sbi->opt, DAX_ALWAYS)) {
0733         BUILD_BUG_ON(EROFS_BLKSIZ != PAGE_SIZE);
0734 
0735         if (!sbi->dax_dev) {
0736             errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
0737             clear_opt(&sbi->opt, DAX_ALWAYS);
0738         }
0739     }
0740 
0741     sb->s_time_gran = 1;
0742     sb->s_xattr = erofs_xattr_handlers;
0743     sb->s_export_op = &erofs_export_ops;
0744 
0745     if (test_opt(&sbi->opt, POSIX_ACL))
0746         sb->s_flags |= SB_POSIXACL;
0747     else
0748         sb->s_flags &= ~SB_POSIXACL;
0749 
0750 #ifdef CONFIG_EROFS_FS_ZIP
0751     xa_init(&sbi->managed_pslots);
0752 #endif
0753 
0754     /* get the root inode */
0755     inode = erofs_iget(sb, ROOT_NID(sbi), true);
0756     if (IS_ERR(inode))
0757         return PTR_ERR(inode);
0758 
0759     if (!S_ISDIR(inode->i_mode)) {
0760         erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
0761               ROOT_NID(sbi), inode->i_mode);
0762         iput(inode);
0763         return -EINVAL;
0764     }
0765 
0766     sb->s_root = d_make_root(inode);
0767     if (!sb->s_root)
0768         return -ENOMEM;
0769 
0770     erofs_shrinker_register(sb);
0771     /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
0772     err = erofs_init_managed_cache(sb);
0773     if (err)
0774         return err;
0775 
0776     err = erofs_register_sysfs(sb);
0777     if (err)
0778         return err;
0779 
0780     erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
0781     return 0;
0782 }
0783 
0784 static int erofs_fc_get_tree(struct fs_context *fc)
0785 {
0786     struct erofs_fs_context *ctx = fc->fs_private;
0787 
0788     if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && ctx->opt.fsid)
0789         return get_tree_nodev(fc, erofs_fc_fill_super);
0790 
0791     return get_tree_bdev(fc, erofs_fc_fill_super);
0792 }
0793 
0794 static int erofs_fc_reconfigure(struct fs_context *fc)
0795 {
0796     struct super_block *sb = fc->root->d_sb;
0797     struct erofs_sb_info *sbi = EROFS_SB(sb);
0798     struct erofs_fs_context *ctx = fc->fs_private;
0799 
0800     DBG_BUGON(!sb_rdonly(sb));
0801 
0802     if (test_opt(&ctx->opt, POSIX_ACL))
0803         fc->sb_flags |= SB_POSIXACL;
0804     else
0805         fc->sb_flags &= ~SB_POSIXACL;
0806 
0807     sbi->opt = ctx->opt;
0808 
0809     fc->sb_flags |= SB_RDONLY;
0810     return 0;
0811 }
0812 
0813 static int erofs_release_device_info(int id, void *ptr, void *data)
0814 {
0815     struct erofs_device_info *dif = ptr;
0816 
0817     fs_put_dax(dif->dax_dev, NULL);
0818     if (dif->bdev)
0819         blkdev_put(dif->bdev, FMODE_READ | FMODE_EXCL);
0820     erofs_fscache_unregister_cookie(&dif->fscache);
0821     kfree(dif->path);
0822     kfree(dif);
0823     return 0;
0824 }
0825 
0826 static void erofs_free_dev_context(struct erofs_dev_context *devs)
0827 {
0828     if (!devs)
0829         return;
0830     idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
0831     idr_destroy(&devs->tree);
0832     kfree(devs);
0833 }
0834 
0835 static void erofs_fc_free(struct fs_context *fc)
0836 {
0837     struct erofs_fs_context *ctx = fc->fs_private;
0838 
0839     erofs_free_dev_context(ctx->devs);
0840     kfree(ctx->opt.fsid);
0841     kfree(ctx);
0842 }
0843 
0844 static const struct fs_context_operations erofs_context_ops = {
0845     .parse_param    = erofs_fc_parse_param,
0846     .get_tree       = erofs_fc_get_tree,
0847     .reconfigure    = erofs_fc_reconfigure,
0848     .free       = erofs_fc_free,
0849 };
0850 
0851 static int erofs_init_fs_context(struct fs_context *fc)
0852 {
0853     struct erofs_fs_context *ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
0854 
0855     if (!ctx)
0856         return -ENOMEM;
0857     ctx->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
0858     if (!ctx->devs) {
0859         kfree(ctx);
0860         return -ENOMEM;
0861     }
0862     fc->fs_private = ctx;
0863 
0864     idr_init(&ctx->devs->tree);
0865     init_rwsem(&ctx->devs->rwsem);
0866     erofs_default_options(ctx);
0867     fc->ops = &erofs_context_ops;
0868     return 0;
0869 }
0870 
0871 /*
0872  * could be triggered after deactivate_locked_super()
0873  * is called, thus including umount and failed to initialize.
0874  */
0875 static void erofs_kill_sb(struct super_block *sb)
0876 {
0877     struct erofs_sb_info *sbi;
0878 
0879     WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
0880 
0881     if (erofs_is_fscache_mode(sb))
0882         generic_shutdown_super(sb);
0883     else
0884         kill_block_super(sb);
0885 
0886     sbi = EROFS_SB(sb);
0887     if (!sbi)
0888         return;
0889 
0890     erofs_free_dev_context(sbi->devs);
0891     fs_put_dax(sbi->dax_dev, NULL);
0892     erofs_fscache_unregister_cookie(&sbi->s_fscache);
0893     erofs_fscache_unregister_fs(sb);
0894     kfree(sbi->opt.fsid);
0895     kfree(sbi);
0896     sb->s_fs_info = NULL;
0897 }
0898 
0899 /* called when ->s_root is non-NULL */
0900 static void erofs_put_super(struct super_block *sb)
0901 {
0902     struct erofs_sb_info *const sbi = EROFS_SB(sb);
0903 
0904     DBG_BUGON(!sbi);
0905 
0906     erofs_unregister_sysfs(sb);
0907     erofs_shrinker_unregister(sb);
0908 #ifdef CONFIG_EROFS_FS_ZIP
0909     iput(sbi->managed_cache);
0910     sbi->managed_cache = NULL;
0911 #endif
0912     erofs_fscache_unregister_cookie(&sbi->s_fscache);
0913 }
0914 
0915 static struct file_system_type erofs_fs_type = {
0916     .owner          = THIS_MODULE,
0917     .name           = "erofs",
0918     .init_fs_context = erofs_init_fs_context,
0919     .kill_sb        = erofs_kill_sb,
0920     .fs_flags       = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
0921 };
0922 MODULE_ALIAS_FS("erofs");
0923 
0924 static int __init erofs_module_init(void)
0925 {
0926     int err;
0927 
0928     erofs_check_ondisk_layout_definitions();
0929 
0930     erofs_inode_cachep = kmem_cache_create("erofs_inode",
0931                            sizeof(struct erofs_inode), 0,
0932                            SLAB_RECLAIM_ACCOUNT,
0933                            erofs_inode_init_once);
0934     if (!erofs_inode_cachep) {
0935         err = -ENOMEM;
0936         goto icache_err;
0937     }
0938 
0939     err = erofs_init_shrinker();
0940     if (err)
0941         goto shrinker_err;
0942 
0943     err = z_erofs_lzma_init();
0944     if (err)
0945         goto lzma_err;
0946 
0947     erofs_pcpubuf_init();
0948     err = z_erofs_init_zip_subsystem();
0949     if (err)
0950         goto zip_err;
0951 
0952     err = erofs_init_sysfs();
0953     if (err)
0954         goto sysfs_err;
0955 
0956     err = register_filesystem(&erofs_fs_type);
0957     if (err)
0958         goto fs_err;
0959 
0960     return 0;
0961 
0962 fs_err:
0963     erofs_exit_sysfs();
0964 sysfs_err:
0965     z_erofs_exit_zip_subsystem();
0966 zip_err:
0967     z_erofs_lzma_exit();
0968 lzma_err:
0969     erofs_exit_shrinker();
0970 shrinker_err:
0971     kmem_cache_destroy(erofs_inode_cachep);
0972 icache_err:
0973     return err;
0974 }
0975 
0976 static void __exit erofs_module_exit(void)
0977 {
0978     unregister_filesystem(&erofs_fs_type);
0979 
0980     /* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
0981     rcu_barrier();
0982 
0983     erofs_exit_sysfs();
0984     z_erofs_exit_zip_subsystem();
0985     z_erofs_lzma_exit();
0986     erofs_exit_shrinker();
0987     kmem_cache_destroy(erofs_inode_cachep);
0988     erofs_pcpubuf_exit();
0989 }
0990 
0991 /* get filesystem statistics */
0992 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
0993 {
0994     struct super_block *sb = dentry->d_sb;
0995     struct erofs_sb_info *sbi = EROFS_SB(sb);
0996     u64 id = 0;
0997 
0998     if (!erofs_is_fscache_mode(sb))
0999         id = huge_encode_dev(sb->s_bdev->bd_dev);
1000 
1001     buf->f_type = sb->s_magic;
1002     buf->f_bsize = EROFS_BLKSIZ;
1003     buf->f_blocks = sbi->total_blocks;
1004     buf->f_bfree = buf->f_bavail = 0;
1005 
1006     buf->f_files = ULLONG_MAX;
1007     buf->f_ffree = ULLONG_MAX - sbi->inos;
1008 
1009     buf->f_namelen = EROFS_NAME_LEN;
1010 
1011     buf->f_fsid    = u64_to_fsid(id);
1012     return 0;
1013 }
1014 
1015 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
1016 {
1017     struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
1018     struct erofs_mount_opts *opt = &sbi->opt;
1019 
1020 #ifdef CONFIG_EROFS_FS_XATTR
1021     if (test_opt(opt, XATTR_USER))
1022         seq_puts(seq, ",user_xattr");
1023     else
1024         seq_puts(seq, ",nouser_xattr");
1025 #endif
1026 #ifdef CONFIG_EROFS_FS_POSIX_ACL
1027     if (test_opt(opt, POSIX_ACL))
1028         seq_puts(seq, ",acl");
1029     else
1030         seq_puts(seq, ",noacl");
1031 #endif
1032 #ifdef CONFIG_EROFS_FS_ZIP
1033     if (opt->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
1034         seq_puts(seq, ",cache_strategy=disabled");
1035     else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
1036         seq_puts(seq, ",cache_strategy=readahead");
1037     else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
1038         seq_puts(seq, ",cache_strategy=readaround");
1039 #endif
1040     if (test_opt(opt, DAX_ALWAYS))
1041         seq_puts(seq, ",dax=always");
1042     if (test_opt(opt, DAX_NEVER))
1043         seq_puts(seq, ",dax=never");
1044 #ifdef CONFIG_EROFS_FS_ONDEMAND
1045     if (opt->fsid)
1046         seq_printf(seq, ",fsid=%s", opt->fsid);
1047 #endif
1048     return 0;
1049 }
1050 
1051 const struct super_operations erofs_sops = {
1052     .put_super = erofs_put_super,
1053     .alloc_inode = erofs_alloc_inode,
1054     .free_inode = erofs_free_inode,
1055     .statfs = erofs_statfs,
1056     .show_options = erofs_show_options,
1057 };
1058 
1059 module_init(erofs_module_init);
1060 module_exit(erofs_module_exit);
1061 
1062 MODULE_DESCRIPTION("Enhanced ROM File System");
1063 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
1064 MODULE_LICENSE("GPL");