linux/fs/squashfs/super.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Squashfs - a compressed read only filesystem for Linux
   4 *
   5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
   6 * Phillip Lougher <phillip@squashfs.org.uk>
   7 *
   8 * super.c
   9 */
  10
  11/*
  12 * This file implements code to read the superblock, read and initialise
  13 * in-memory structures at mount time, and all the VFS glue code to register
  14 * the filesystem.
  15 */
  16
  17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18
  19#include <linux/fs.h>
  20#include <linux/vfs.h>
  21#include <linux/slab.h>
  22#include <linux/mutex.h>
  23#include <linux/pagemap.h>
  24#include <linux/init.h>
  25#include <linux/module.h>
  26#include <linux/magic.h>
  27#include <linux/xattr.h>
  28
  29#include "squashfs_fs.h"
  30#include "squashfs_fs_sb.h"
  31#include "squashfs_fs_i.h"
  32#include "squashfs.h"
  33#include "decompressor.h"
  34#include "xattr.h"
  35
  36static struct file_system_type squashfs_fs_type;
  37static const struct super_operations squashfs_super_ops;
  38
  39static const struct squashfs_decompressor *supported_squashfs_filesystem(short
  40        major, short minor, short id)
  41{
  42        const struct squashfs_decompressor *decompressor;
  43
  44        if (major < SQUASHFS_MAJOR) {
  45                ERROR("Major/Minor mismatch, older Squashfs %d.%d "
  46                        "filesystems are unsupported\n", major, minor);
  47                return NULL;
  48        } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
  49                ERROR("Major/Minor mismatch, trying to mount newer "
  50                        "%d.%d filesystem\n", major, minor);
  51                ERROR("Please update your kernel\n");
  52                return NULL;
  53        }
  54
  55        decompressor = squashfs_lookup_decompressor(id);
  56        if (!decompressor->supported) {
  57                ERROR("Filesystem uses \"%s\" compression. This is not "
  58                        "supported\n", decompressor->name);
  59                return NULL;
  60        }
  61
  62        return decompressor;
  63}
  64
  65
  66static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
  67{
  68        struct squashfs_sb_info *msblk;
  69        struct squashfs_super_block *sblk = NULL;
  70        struct inode *root;
  71        long long root_inode;
  72        unsigned short flags;
  73        unsigned int fragments;
  74        u64 lookup_table_start, xattr_id_table_start, next_table;
  75        int err;
  76
  77        TRACE("Entered squashfs_fill_superblock\n");
  78
  79        sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
  80        if (sb->s_fs_info == NULL) {
  81                ERROR("Failed to allocate squashfs_sb_info\n");
  82                return -ENOMEM;
  83        }
  84        msblk = sb->s_fs_info;
  85
  86        msblk->devblksize = sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE);
  87        msblk->devblksize_log2 = ffz(~msblk->devblksize);
  88
  89        mutex_init(&msblk->meta_index_mutex);
  90
  91        /*
  92         * msblk->bytes_used is checked in squashfs_read_table to ensure reads
  93         * are not beyond filesystem end.  But as we're using
  94         * squashfs_read_table here to read the superblock (including the value
  95         * of bytes_used) we need to set it to an initial sensible dummy value
  96         */
  97        msblk->bytes_used = sizeof(*sblk);
  98        sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
  99
 100        if (IS_ERR(sblk)) {
 101                ERROR("unable to read squashfs_super_block\n");
 102                err = PTR_ERR(sblk);
 103                sblk = NULL;
 104                goto failed_mount;
 105        }
 106
 107        err = -EINVAL;
 108
 109        /* Check it is a SQUASHFS superblock */
 110        sb->s_magic = le32_to_cpu(sblk->s_magic);
 111        if (sb->s_magic != SQUASHFS_MAGIC) {
 112                if (!silent)
 113                        ERROR("Can't find a SQUASHFS superblock on %pg\n",
 114                                                sb->s_bdev);
 115                goto failed_mount;
 116        }
 117
 118        /* Check the MAJOR & MINOR versions and lookup compression type */
 119        msblk->decompressor = supported_squashfs_filesystem(
 120                        le16_to_cpu(sblk->s_major),
 121                        le16_to_cpu(sblk->s_minor),
 122                        le16_to_cpu(sblk->compression));
 123        if (msblk->decompressor == NULL)
 124                goto failed_mount;
 125
 126        /* Check the filesystem does not extend beyond the end of the
 127           block device */
 128        msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
 129        if (msblk->bytes_used < 0 || msblk->bytes_used >
 130                        i_size_read(sb->s_bdev->bd_inode))
 131                goto failed_mount;
 132
 133        /* Check block size for sanity */
 134        msblk->block_size = le32_to_cpu(sblk->block_size);
 135        if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
 136                goto failed_mount;
 137
 138        /*
 139         * Check the system page size is not larger than the filesystem
 140         * block size (by default 128K).  This is currently not supported.
 141         */
 142        if (PAGE_SIZE > msblk->block_size) {
 143                ERROR("Page size > filesystem block size (%d).  This is "
 144                        "currently not supported!\n", msblk->block_size);
 145                goto failed_mount;
 146        }
 147
 148        /* Check block log for sanity */
 149        msblk->block_log = le16_to_cpu(sblk->block_log);
 150        if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
 151                goto failed_mount;
 152
 153        /* Check that block_size and block_log match */
 154        if (msblk->block_size != (1 << msblk->block_log))
 155                goto failed_mount;
 156
 157        /* Check the root inode for sanity */
 158        root_inode = le64_to_cpu(sblk->root_inode);
 159        if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
 160                goto failed_mount;
 161
 162        msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
 163        msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
 164        msblk->inodes = le32_to_cpu(sblk->inodes);
 165        msblk->fragments = le32_to_cpu(sblk->fragments);
 166        flags = le16_to_cpu(sblk->flags);
 167
 168        TRACE("Found valid superblock on %pg\n", sb->s_bdev);
 169        TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
 170                                ? "un" : "");
 171        TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
 172                                ? "un" : "");
 173        TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
 174        TRACE("Block size %d\n", msblk->block_size);
 175        TRACE("Number of inodes %d\n", msblk->inodes);
 176        TRACE("Number of fragments %d\n", msblk->fragments);
 177        TRACE("Number of ids %d\n", le16_to_cpu(sblk->no_ids));
 178        TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
 179        TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
 180        TRACE("sblk->fragment_table_start %llx\n",
 181                (u64) le64_to_cpu(sblk->fragment_table_start));
 182        TRACE("sblk->id_table_start %llx\n",
 183                (u64) le64_to_cpu(sblk->id_table_start));
 184
 185        sb->s_maxbytes = MAX_LFS_FILESIZE;
 186        sb->s_flags |= SB_RDONLY;
 187        sb->s_op = &squashfs_super_ops;
 188
 189        err = -ENOMEM;
 190
 191        msblk->block_cache = squashfs_cache_init("metadata",
 192                        SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
 193        if (msblk->block_cache == NULL)
 194                goto failed_mount;
 195
 196        /* Allocate read_page block */
 197        msblk->read_page = squashfs_cache_init("data",
 198                squashfs_max_decompressors(), msblk->block_size);
 199        if (msblk->read_page == NULL) {
 200                ERROR("Failed to allocate read_page block\n");
 201                goto failed_mount;
 202        }
 203
 204        msblk->stream = squashfs_decompressor_setup(sb, flags);
 205        if (IS_ERR(msblk->stream)) {
 206                err = PTR_ERR(msblk->stream);
 207                msblk->stream = NULL;
 208                goto failed_mount;
 209        }
 210
 211        /* Handle xattrs */
 212        sb->s_xattr = squashfs_xattr_handlers;
 213        xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
 214        if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
 215                next_table = msblk->bytes_used;
 216                goto allocate_id_index_table;
 217        }
 218
 219        /* Allocate and read xattr id lookup table */
 220        msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
 221                xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
 222        if (IS_ERR(msblk->xattr_id_table)) {
 223                ERROR("unable to read xattr id index table\n");
 224                err = PTR_ERR(msblk->xattr_id_table);
 225                msblk->xattr_id_table = NULL;
 226                if (err != -ENOTSUPP)
 227                        goto failed_mount;
 228        }
 229        next_table = msblk->xattr_table;
 230
 231allocate_id_index_table:
 232        /* Allocate and read id index table */
 233        msblk->id_table = squashfs_read_id_index_table(sb,
 234                le64_to_cpu(sblk->id_table_start), next_table,
 235                le16_to_cpu(sblk->no_ids));
 236        if (IS_ERR(msblk->id_table)) {
 237                ERROR("unable to read id index table\n");
 238                err = PTR_ERR(msblk->id_table);
 239                msblk->id_table = NULL;
 240                goto failed_mount;
 241        }
 242        next_table = le64_to_cpu(msblk->id_table[0]);
 243
 244        /* Handle inode lookup table */
 245        lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
 246        if (lookup_table_start == SQUASHFS_INVALID_BLK)
 247                goto handle_fragments;
 248
 249        /* Allocate and read inode lookup table */
 250        msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
 251                lookup_table_start, next_table, msblk->inodes);
 252        if (IS_ERR(msblk->inode_lookup_table)) {
 253                ERROR("unable to read inode lookup table\n");
 254                err = PTR_ERR(msblk->inode_lookup_table);
 255                msblk->inode_lookup_table = NULL;
 256                goto failed_mount;
 257        }
 258        next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
 259
 260        sb->s_export_op = &squashfs_export_ops;
 261
 262handle_fragments:
 263        fragments = msblk->fragments;
 264        if (fragments == 0)
 265                goto check_directory_table;
 266
 267        msblk->fragment_cache = squashfs_cache_init("fragment",
 268                SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
 269        if (msblk->fragment_cache == NULL) {
 270                err = -ENOMEM;
 271                goto failed_mount;
 272        }
 273
 274        /* Allocate and read fragment index table */
 275        msblk->fragment_index = squashfs_read_fragment_index_table(sb,
 276                le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
 277        if (IS_ERR(msblk->fragment_index)) {
 278                ERROR("unable to read fragment index table\n");
 279                err = PTR_ERR(msblk->fragment_index);
 280                msblk->fragment_index = NULL;
 281                goto failed_mount;
 282        }
 283        next_table = le64_to_cpu(msblk->fragment_index[0]);
 284
 285check_directory_table:
 286        /* Sanity check directory_table */
 287        if (msblk->directory_table > next_table) {
 288                err = -EINVAL;
 289                goto failed_mount;
 290        }
 291
 292        /* Sanity check inode_table */
 293        if (msblk->inode_table >= msblk->directory_table) {
 294                err = -EINVAL;
 295                goto failed_mount;
 296        }
 297
 298        /* allocate root */
 299        root = new_inode(sb);
 300        if (!root) {
 301                err = -ENOMEM;
 302                goto failed_mount;
 303        }
 304
 305        err = squashfs_read_inode(root, root_inode);
 306        if (err) {
 307                make_bad_inode(root);
 308                iput(root);
 309                goto failed_mount;
 310        }
 311        insert_inode_hash(root);
 312
 313        sb->s_root = d_make_root(root);
 314        if (sb->s_root == NULL) {
 315                ERROR("Root inode create failed\n");
 316                err = -ENOMEM;
 317                goto failed_mount;
 318        }
 319
 320        TRACE("Leaving squashfs_fill_super\n");
 321        kfree(sblk);
 322        return 0;
 323
 324failed_mount:
 325        squashfs_cache_delete(msblk->block_cache);
 326        squashfs_cache_delete(msblk->fragment_cache);
 327        squashfs_cache_delete(msblk->read_page);
 328        squashfs_decompressor_destroy(msblk);
 329        kfree(msblk->inode_lookup_table);
 330        kfree(msblk->fragment_index);
 331        kfree(msblk->id_table);
 332        kfree(msblk->xattr_id_table);
 333        kfree(sb->s_fs_info);
 334        sb->s_fs_info = NULL;
 335        kfree(sblk);
 336        return err;
 337}
 338
 339
 340static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 341{
 342        struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
 343        u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
 344
 345        TRACE("Entered squashfs_statfs\n");
 346
 347        buf->f_type = SQUASHFS_MAGIC;
 348        buf->f_bsize = msblk->block_size;
 349        buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
 350        buf->f_bfree = buf->f_bavail = 0;
 351        buf->f_files = msblk->inodes;
 352        buf->f_ffree = 0;
 353        buf->f_namelen = SQUASHFS_NAME_LEN;
 354        buf->f_fsid.val[0] = (u32)id;
 355        buf->f_fsid.val[1] = (u32)(id >> 32);
 356
 357        return 0;
 358}
 359
 360
 361static int squashfs_remount(struct super_block *sb, int *flags, char *data)
 362{
 363        sync_filesystem(sb);
 364        *flags |= SB_RDONLY;
 365        return 0;
 366}
 367
 368
 369static void squashfs_put_super(struct super_block *sb)
 370{
 371        if (sb->s_fs_info) {
 372                struct squashfs_sb_info *sbi = sb->s_fs_info;
 373                squashfs_cache_delete(sbi->block_cache);
 374                squashfs_cache_delete(sbi->fragment_cache);
 375                squashfs_cache_delete(sbi->read_page);
 376                squashfs_decompressor_destroy(sbi);
 377                kfree(sbi->id_table);
 378                kfree(sbi->fragment_index);
 379                kfree(sbi->meta_index);
 380                kfree(sbi->inode_lookup_table);
 381                kfree(sbi->xattr_id_table);
 382                kfree(sb->s_fs_info);
 383                sb->s_fs_info = NULL;
 384        }
 385}
 386
 387
 388static struct dentry *squashfs_mount(struct file_system_type *fs_type,
 389                                int flags, const char *dev_name, void *data)
 390{
 391        return mount_bdev(fs_type, flags, dev_name, data, squashfs_fill_super);
 392}
 393
 394
 395static struct kmem_cache *squashfs_inode_cachep;
 396
 397
 398static void init_once(void *foo)
 399{
 400        struct squashfs_inode_info *ei = foo;
 401
 402        inode_init_once(&ei->vfs_inode);
 403}
 404
 405
 406static int __init init_inodecache(void)
 407{
 408        squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
 409                sizeof(struct squashfs_inode_info), 0,
 410                SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
 411                init_once);
 412
 413        return squashfs_inode_cachep ? 0 : -ENOMEM;
 414}
 415
 416
 417static void destroy_inodecache(void)
 418{
 419        /*
 420         * Make sure all delayed rcu free inodes are flushed before we
 421         * destroy cache.
 422         */
 423        rcu_barrier();
 424        kmem_cache_destroy(squashfs_inode_cachep);
 425}
 426
 427
 428static int __init init_squashfs_fs(void)
 429{
 430        int err = init_inodecache();
 431
 432        if (err)
 433                return err;
 434
 435        err = register_filesystem(&squashfs_fs_type);
 436        if (err) {
 437                destroy_inodecache();
 438                return err;
 439        }
 440
 441        pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
 442
 443        return 0;
 444}
 445
 446
 447static void __exit exit_squashfs_fs(void)
 448{
 449        unregister_filesystem(&squashfs_fs_type);
 450        destroy_inodecache();
 451}
 452
 453
 454static struct inode *squashfs_alloc_inode(struct super_block *sb)
 455{
 456        struct squashfs_inode_info *ei =
 457                kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
 458
 459        return ei ? &ei->vfs_inode : NULL;
 460}
 461
 462
 463static void squashfs_free_inode(struct inode *inode)
 464{
 465        kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
 466}
 467
 468static struct file_system_type squashfs_fs_type = {
 469        .owner = THIS_MODULE,
 470        .name = "squashfs",
 471        .mount = squashfs_mount,
 472        .kill_sb = kill_block_super,
 473        .fs_flags = FS_REQUIRES_DEV
 474};
 475MODULE_ALIAS_FS("squashfs");
 476
 477static const struct super_operations squashfs_super_ops = {
 478        .alloc_inode = squashfs_alloc_inode,
 479        .free_inode = squashfs_free_inode,
 480        .statfs = squashfs_statfs,
 481        .put_super = squashfs_put_super,
 482        .remount_fs = squashfs_remount
 483};
 484
 485module_init(init_squashfs_fs);
 486module_exit(exit_squashfs_fs);
 487MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
 488MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
 489MODULE_LICENSE("GPL");
 490