Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Squashfs - a compressed read only filesystem for Linux
0004  *
0005  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
0006  * Phillip Lougher <phillip@squashfs.org.uk>
0007  *
0008  * super.c
0009  */
0010 
0011 /*
0012  * This file implements code to read the superblock, read and initialise
0013  * in-memory structures at mount time, and all the VFS glue code to register
0014  * the filesystem.
0015  */
0016 
0017 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0018 
0019 #include <linux/blkdev.h>
0020 #include <linux/fs.h>
0021 #include <linux/fs_context.h>
0022 #include <linux/fs_parser.h>
0023 #include <linux/vfs.h>
0024 #include <linux/slab.h>
0025 #include <linux/mutex.h>
0026 #include <linux/seq_file.h>
0027 #include <linux/pagemap.h>
0028 #include <linux/init.h>
0029 #include <linux/module.h>
0030 #include <linux/magic.h>
0031 #include <linux/xattr.h>
0032 
0033 #include "squashfs_fs.h"
0034 #include "squashfs_fs_sb.h"
0035 #include "squashfs_fs_i.h"
0036 #include "squashfs.h"
0037 #include "decompressor.h"
0038 #include "xattr.h"
0039 
0040 static struct file_system_type squashfs_fs_type;
0041 static const struct super_operations squashfs_super_ops;
0042 
0043 enum Opt_errors {
0044     Opt_errors_continue,
0045     Opt_errors_panic,
0046 };
0047 
0048 enum squashfs_param {
0049     Opt_errors,
0050 };
0051 
0052 struct squashfs_mount_opts {
0053     enum Opt_errors errors;
0054 };
0055 
0056 static const struct constant_table squashfs_param_errors[] = {
0057     {"continue",   Opt_errors_continue },
0058     {"panic",      Opt_errors_panic },
0059     {}
0060 };
0061 
0062 static const struct fs_parameter_spec squashfs_fs_parameters[] = {
0063     fsparam_enum("errors", Opt_errors, squashfs_param_errors),
0064     {}
0065 };
0066 
0067 static int squashfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
0068 {
0069     struct squashfs_mount_opts *opts = fc->fs_private;
0070     struct fs_parse_result result;
0071     int opt;
0072 
0073     opt = fs_parse(fc, squashfs_fs_parameters, param, &result);
0074     if (opt < 0)
0075         return opt;
0076 
0077     switch (opt) {
0078     case Opt_errors:
0079         opts->errors = result.uint_32;
0080         break;
0081     default:
0082         return -EINVAL;
0083     }
0084 
0085     return 0;
0086 }
0087 
0088 static const struct squashfs_decompressor *supported_squashfs_filesystem(
0089     struct fs_context *fc,
0090     short major, short minor, short id)
0091 {
0092     const struct squashfs_decompressor *decompressor;
0093 
0094     if (major < SQUASHFS_MAJOR) {
0095         errorf(fc, "Major/Minor mismatch, older Squashfs %d.%d "
0096                "filesystems are unsupported", major, minor);
0097         return NULL;
0098     } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
0099         errorf(fc, "Major/Minor mismatch, trying to mount newer "
0100                "%d.%d filesystem", major, minor);
0101         errorf(fc, "Please update your kernel");
0102         return NULL;
0103     }
0104 
0105     decompressor = squashfs_lookup_decompressor(id);
0106     if (!decompressor->supported) {
0107         errorf(fc, "Filesystem uses \"%s\" compression. This is not supported",
0108                decompressor->name);
0109         return NULL;
0110     }
0111 
0112     return decompressor;
0113 }
0114 
0115 
0116 static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)
0117 {
0118     struct squashfs_mount_opts *opts = fc->fs_private;
0119     struct squashfs_sb_info *msblk;
0120     struct squashfs_super_block *sblk = NULL;
0121     struct inode *root;
0122     long long root_inode;
0123     unsigned short flags;
0124     unsigned int fragments;
0125     u64 lookup_table_start, xattr_id_table_start, next_table;
0126     int err;
0127 
0128     TRACE("Entered squashfs_fill_superblock\n");
0129 
0130     sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
0131     if (sb->s_fs_info == NULL) {
0132         ERROR("Failed to allocate squashfs_sb_info\n");
0133         return -ENOMEM;
0134     }
0135     msblk = sb->s_fs_info;
0136 
0137     msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
0138 
0139     msblk->devblksize = sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE);
0140     msblk->devblksize_log2 = ffz(~msblk->devblksize);
0141 
0142     mutex_init(&msblk->meta_index_mutex);
0143 
0144     /*
0145      * msblk->bytes_used is checked in squashfs_read_table to ensure reads
0146      * are not beyond filesystem end.  But as we're using
0147      * squashfs_read_table here to read the superblock (including the value
0148      * of bytes_used) we need to set it to an initial sensible dummy value
0149      */
0150     msblk->bytes_used = sizeof(*sblk);
0151     sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
0152 
0153     if (IS_ERR(sblk)) {
0154         errorf(fc, "unable to read squashfs_super_block");
0155         err = PTR_ERR(sblk);
0156         sblk = NULL;
0157         goto failed_mount;
0158     }
0159 
0160     err = -EINVAL;
0161 
0162     /* Check it is a SQUASHFS superblock */
0163     sb->s_magic = le32_to_cpu(sblk->s_magic);
0164     if (sb->s_magic != SQUASHFS_MAGIC) {
0165         if (!(fc->sb_flags & SB_SILENT))
0166             errorf(fc, "Can't find a SQUASHFS superblock on %pg",
0167                    sb->s_bdev);
0168         goto failed_mount;
0169     }
0170 
0171     /* Check the MAJOR & MINOR versions and lookup compression type */
0172     msblk->decompressor = supported_squashfs_filesystem(
0173             fc,
0174             le16_to_cpu(sblk->s_major),
0175             le16_to_cpu(sblk->s_minor),
0176             le16_to_cpu(sblk->compression));
0177     if (msblk->decompressor == NULL)
0178         goto failed_mount;
0179 
0180     /* Check the filesystem does not extend beyond the end of the
0181        block device */
0182     msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
0183     if (msblk->bytes_used < 0 ||
0184         msblk->bytes_used > bdev_nr_bytes(sb->s_bdev))
0185         goto failed_mount;
0186 
0187     /* Check block size for sanity */
0188     msblk->block_size = le32_to_cpu(sblk->block_size);
0189     if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
0190         goto insanity;
0191 
0192     /*
0193      * Check the system page size is not larger than the filesystem
0194      * block size (by default 128K).  This is currently not supported.
0195      */
0196     if (PAGE_SIZE > msblk->block_size) {
0197         errorf(fc, "Page size > filesystem block size (%d).  This is "
0198                "currently not supported!", msblk->block_size);
0199         goto failed_mount;
0200     }
0201 
0202     /* Check block log for sanity */
0203     msblk->block_log = le16_to_cpu(sblk->block_log);
0204     if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
0205         goto failed_mount;
0206 
0207     /* Check that block_size and block_log match */
0208     if (msblk->block_size != (1 << msblk->block_log))
0209         goto insanity;
0210 
0211     /* Check the root inode for sanity */
0212     root_inode = le64_to_cpu(sblk->root_inode);
0213     if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
0214         goto insanity;
0215 
0216     msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
0217     msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
0218     msblk->inodes = le32_to_cpu(sblk->inodes);
0219     msblk->fragments = le32_to_cpu(sblk->fragments);
0220     msblk->ids = le16_to_cpu(sblk->no_ids);
0221     flags = le16_to_cpu(sblk->flags);
0222 
0223     TRACE("Found valid superblock on %pg\n", sb->s_bdev);
0224     TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
0225                 ? "un" : "");
0226     TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
0227                 ? "un" : "");
0228     TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
0229     TRACE("Block size %d\n", msblk->block_size);
0230     TRACE("Number of inodes %d\n", msblk->inodes);
0231     TRACE("Number of fragments %d\n", msblk->fragments);
0232     TRACE("Number of ids %d\n", msblk->ids);
0233     TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
0234     TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
0235     TRACE("sblk->fragment_table_start %llx\n",
0236         (u64) le64_to_cpu(sblk->fragment_table_start));
0237     TRACE("sblk->id_table_start %llx\n",
0238         (u64) le64_to_cpu(sblk->id_table_start));
0239 
0240     sb->s_maxbytes = MAX_LFS_FILESIZE;
0241     sb->s_time_min = 0;
0242     sb->s_time_max = U32_MAX;
0243     sb->s_flags |= SB_RDONLY;
0244     sb->s_op = &squashfs_super_ops;
0245 
0246     err = -ENOMEM;
0247 
0248     msblk->block_cache = squashfs_cache_init("metadata",
0249             SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
0250     if (msblk->block_cache == NULL)
0251         goto failed_mount;
0252 
0253     /* Allocate read_page block */
0254     msblk->read_page = squashfs_cache_init("data",
0255         squashfs_max_decompressors(), msblk->block_size);
0256     if (msblk->read_page == NULL) {
0257         errorf(fc, "Failed to allocate read_page block");
0258         goto failed_mount;
0259     }
0260 
0261     msblk->stream = squashfs_decompressor_setup(sb, flags);
0262     if (IS_ERR(msblk->stream)) {
0263         err = PTR_ERR(msblk->stream);
0264         msblk->stream = NULL;
0265         goto insanity;
0266     }
0267 
0268     /* Handle xattrs */
0269     sb->s_xattr = squashfs_xattr_handlers;
0270     xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
0271     if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
0272         next_table = msblk->bytes_used;
0273         goto allocate_id_index_table;
0274     }
0275 
0276     /* Allocate and read xattr id lookup table */
0277     msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
0278         xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
0279     if (IS_ERR(msblk->xattr_id_table)) {
0280         errorf(fc, "unable to read xattr id index table");
0281         err = PTR_ERR(msblk->xattr_id_table);
0282         msblk->xattr_id_table = NULL;
0283         if (err != -ENOTSUPP)
0284             goto failed_mount;
0285     }
0286     next_table = msblk->xattr_table;
0287 
0288 allocate_id_index_table:
0289     /* Allocate and read id index table */
0290     msblk->id_table = squashfs_read_id_index_table(sb,
0291         le64_to_cpu(sblk->id_table_start), next_table, msblk->ids);
0292     if (IS_ERR(msblk->id_table)) {
0293         errorf(fc, "unable to read id index table");
0294         err = PTR_ERR(msblk->id_table);
0295         msblk->id_table = NULL;
0296         goto failed_mount;
0297     }
0298     next_table = le64_to_cpu(msblk->id_table[0]);
0299 
0300     /* Handle inode lookup table */
0301     lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
0302     if (lookup_table_start == SQUASHFS_INVALID_BLK)
0303         goto handle_fragments;
0304 
0305     /* Allocate and read inode lookup table */
0306     msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
0307         lookup_table_start, next_table, msblk->inodes);
0308     if (IS_ERR(msblk->inode_lookup_table)) {
0309         errorf(fc, "unable to read inode lookup table");
0310         err = PTR_ERR(msblk->inode_lookup_table);
0311         msblk->inode_lookup_table = NULL;
0312         goto failed_mount;
0313     }
0314     next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
0315 
0316     sb->s_export_op = &squashfs_export_ops;
0317 
0318 handle_fragments:
0319     fragments = msblk->fragments;
0320     if (fragments == 0)
0321         goto check_directory_table;
0322 
0323     msblk->fragment_cache = squashfs_cache_init("fragment",
0324         SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
0325     if (msblk->fragment_cache == NULL) {
0326         err = -ENOMEM;
0327         goto failed_mount;
0328     }
0329 
0330     /* Allocate and read fragment index table */
0331     msblk->fragment_index = squashfs_read_fragment_index_table(sb,
0332         le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
0333     if (IS_ERR(msblk->fragment_index)) {
0334         errorf(fc, "unable to read fragment index table");
0335         err = PTR_ERR(msblk->fragment_index);
0336         msblk->fragment_index = NULL;
0337         goto failed_mount;
0338     }
0339     next_table = le64_to_cpu(msblk->fragment_index[0]);
0340 
0341 check_directory_table:
0342     /* Sanity check directory_table */
0343     if (msblk->directory_table > next_table) {
0344         err = -EINVAL;
0345         goto insanity;
0346     }
0347 
0348     /* Sanity check inode_table */
0349     if (msblk->inode_table >= msblk->directory_table) {
0350         err = -EINVAL;
0351         goto insanity;
0352     }
0353 
0354     /* allocate root */
0355     root = new_inode(sb);
0356     if (!root) {
0357         err = -ENOMEM;
0358         goto failed_mount;
0359     }
0360 
0361     err = squashfs_read_inode(root, root_inode);
0362     if (err) {
0363         make_bad_inode(root);
0364         iput(root);
0365         goto failed_mount;
0366     }
0367     insert_inode_hash(root);
0368 
0369     sb->s_root = d_make_root(root);
0370     if (sb->s_root == NULL) {
0371         ERROR("Root inode create failed\n");
0372         err = -ENOMEM;
0373         goto failed_mount;
0374     }
0375 
0376     TRACE("Leaving squashfs_fill_super\n");
0377     kfree(sblk);
0378     return 0;
0379 
0380 insanity:
0381     errorf(fc, "squashfs image failed sanity check");
0382 failed_mount:
0383     squashfs_cache_delete(msblk->block_cache);
0384     squashfs_cache_delete(msblk->fragment_cache);
0385     squashfs_cache_delete(msblk->read_page);
0386     squashfs_decompressor_destroy(msblk);
0387     kfree(msblk->inode_lookup_table);
0388     kfree(msblk->fragment_index);
0389     kfree(msblk->id_table);
0390     kfree(msblk->xattr_id_table);
0391     kfree(sb->s_fs_info);
0392     sb->s_fs_info = NULL;
0393     kfree(sblk);
0394     return err;
0395 }
0396 
0397 static int squashfs_get_tree(struct fs_context *fc)
0398 {
0399     return get_tree_bdev(fc, squashfs_fill_super);
0400 }
0401 
0402 static int squashfs_reconfigure(struct fs_context *fc)
0403 {
0404     struct super_block *sb = fc->root->d_sb;
0405     struct squashfs_sb_info *msblk = sb->s_fs_info;
0406     struct squashfs_mount_opts *opts = fc->fs_private;
0407 
0408     sync_filesystem(fc->root->d_sb);
0409     fc->sb_flags |= SB_RDONLY;
0410 
0411     msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
0412 
0413     return 0;
0414 }
0415 
0416 static void squashfs_free_fs_context(struct fs_context *fc)
0417 {
0418     kfree(fc->fs_private);
0419 }
0420 
0421 static const struct fs_context_operations squashfs_context_ops = {
0422     .get_tree   = squashfs_get_tree,
0423     .free       = squashfs_free_fs_context,
0424     .parse_param    = squashfs_parse_param,
0425     .reconfigure    = squashfs_reconfigure,
0426 };
0427 
0428 static int squashfs_show_options(struct seq_file *s, struct dentry *root)
0429 {
0430     struct super_block *sb = root->d_sb;
0431     struct squashfs_sb_info *msblk = sb->s_fs_info;
0432 
0433     if (msblk->panic_on_errors)
0434         seq_puts(s, ",errors=panic");
0435     else
0436         seq_puts(s, ",errors=continue");
0437 
0438     return 0;
0439 }
0440 
0441 static int squashfs_init_fs_context(struct fs_context *fc)
0442 {
0443     struct squashfs_mount_opts *opts;
0444 
0445     opts = kzalloc(sizeof(*opts), GFP_KERNEL);
0446     if (!opts)
0447         return -ENOMEM;
0448 
0449     fc->fs_private = opts;
0450     fc->ops = &squashfs_context_ops;
0451     return 0;
0452 }
0453 
0454 static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
0455 {
0456     struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
0457     u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
0458 
0459     TRACE("Entered squashfs_statfs\n");
0460 
0461     buf->f_type = SQUASHFS_MAGIC;
0462     buf->f_bsize = msblk->block_size;
0463     buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
0464     buf->f_bfree = buf->f_bavail = 0;
0465     buf->f_files = msblk->inodes;
0466     buf->f_ffree = 0;
0467     buf->f_namelen = SQUASHFS_NAME_LEN;
0468     buf->f_fsid = u64_to_fsid(id);
0469 
0470     return 0;
0471 }
0472 
0473 
0474 static void squashfs_put_super(struct super_block *sb)
0475 {
0476     if (sb->s_fs_info) {
0477         struct squashfs_sb_info *sbi = sb->s_fs_info;
0478         squashfs_cache_delete(sbi->block_cache);
0479         squashfs_cache_delete(sbi->fragment_cache);
0480         squashfs_cache_delete(sbi->read_page);
0481         squashfs_decompressor_destroy(sbi);
0482         kfree(sbi->id_table);
0483         kfree(sbi->fragment_index);
0484         kfree(sbi->meta_index);
0485         kfree(sbi->inode_lookup_table);
0486         kfree(sbi->xattr_id_table);
0487         kfree(sb->s_fs_info);
0488         sb->s_fs_info = NULL;
0489     }
0490 }
0491 
0492 static struct kmem_cache *squashfs_inode_cachep;
0493 
0494 
0495 static void init_once(void *foo)
0496 {
0497     struct squashfs_inode_info *ei = foo;
0498 
0499     inode_init_once(&ei->vfs_inode);
0500 }
0501 
0502 
0503 static int __init init_inodecache(void)
0504 {
0505     squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
0506         sizeof(struct squashfs_inode_info), 0,
0507         SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
0508         init_once);
0509 
0510     return squashfs_inode_cachep ? 0 : -ENOMEM;
0511 }
0512 
0513 
0514 static void destroy_inodecache(void)
0515 {
0516     /*
0517      * Make sure all delayed rcu free inodes are flushed before we
0518      * destroy cache.
0519      */
0520     rcu_barrier();
0521     kmem_cache_destroy(squashfs_inode_cachep);
0522 }
0523 
0524 
0525 static int __init init_squashfs_fs(void)
0526 {
0527     int err = init_inodecache();
0528 
0529     if (err)
0530         return err;
0531 
0532     err = register_filesystem(&squashfs_fs_type);
0533     if (err) {
0534         destroy_inodecache();
0535         return err;
0536     }
0537 
0538     pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
0539 
0540     return 0;
0541 }
0542 
0543 
0544 static void __exit exit_squashfs_fs(void)
0545 {
0546     unregister_filesystem(&squashfs_fs_type);
0547     destroy_inodecache();
0548 }
0549 
0550 
0551 static struct inode *squashfs_alloc_inode(struct super_block *sb)
0552 {
0553     struct squashfs_inode_info *ei =
0554         alloc_inode_sb(sb, squashfs_inode_cachep, GFP_KERNEL);
0555 
0556     return ei ? &ei->vfs_inode : NULL;
0557 }
0558 
0559 
0560 static void squashfs_free_inode(struct inode *inode)
0561 {
0562     kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
0563 }
0564 
0565 static struct file_system_type squashfs_fs_type = {
0566     .owner = THIS_MODULE,
0567     .name = "squashfs",
0568     .init_fs_context = squashfs_init_fs_context,
0569     .parameters = squashfs_fs_parameters,
0570     .kill_sb = kill_block_super,
0571     .fs_flags = FS_REQUIRES_DEV
0572 };
0573 MODULE_ALIAS_FS("squashfs");
0574 
0575 static const struct super_operations squashfs_super_ops = {
0576     .alloc_inode = squashfs_alloc_inode,
0577     .free_inode = squashfs_free_inode,
0578     .statfs = squashfs_statfs,
0579     .put_super = squashfs_put_super,
0580     .show_options = squashfs_show_options,
0581 };
0582 
0583 module_init(init_squashfs_fs);
0584 module_exit(exit_squashfs_fs);
0585 MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
0586 MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
0587 MODULE_LICENSE("GPL");