1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Squashfs - a compressed read only filesystem for Linux
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6 * Phillip Lougher <phillip@squashfs.org.uk>
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
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/blkdev.h>
21 #include <linux/fs_context.h>
22 #include <linux/fs_parser.h>
23 #include <linux/vfs.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/seq_file.h>
27 #include <linux/pagemap.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/magic.h>
31 #include <linux/xattr.h>
32 #include <linux/backing-dev.h>
34 #include "squashfs_fs.h"
35 #include "squashfs_fs_sb.h"
36 #include "squashfs_fs_i.h"
38 #include "decompressor.h"
41 static struct file_system_type squashfs_fs_type;
42 static const struct super_operations squashfs_super_ops;
53 struct squashfs_mount_opts {
54 enum Opt_errors errors;
57 static const struct constant_table squashfs_param_errors[] = {
58 {"continue", Opt_errors_continue },
59 {"panic", Opt_errors_panic },
63 static const struct fs_parameter_spec squashfs_fs_parameters[] = {
64 fsparam_enum("errors", Opt_errors, squashfs_param_errors),
68 static int squashfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
70 struct squashfs_mount_opts *opts = fc->fs_private;
71 struct fs_parse_result result;
74 opt = fs_parse(fc, squashfs_fs_parameters, param, &result);
80 opts->errors = result.uint_32;
89 static const struct squashfs_decompressor *supported_squashfs_filesystem(
90 struct fs_context *fc,
91 short major, short minor, short id)
93 const struct squashfs_decompressor *decompressor;
95 if (major < SQUASHFS_MAJOR) {
96 errorf(fc, "Major/Minor mismatch, older Squashfs %d.%d "
97 "filesystems are unsupported", major, minor);
99 } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
100 errorf(fc, "Major/Minor mismatch, trying to mount newer "
101 "%d.%d filesystem", major, minor);
102 errorf(fc, "Please update your kernel");
106 decompressor = squashfs_lookup_decompressor(id);
107 if (!decompressor->supported) {
108 errorf(fc, "Filesystem uses \"%s\" compression. This is not supported",
116 static int squashfs_bdi_init(struct super_block *sb)
119 unsigned int major = MAJOR(sb->s_dev);
120 unsigned int minor = MINOR(sb->s_dev);
123 sb->s_bdi = &noop_backing_dev_info;
125 err = super_setup_bdi_name(sb, "squashfs_%u_%u", major, minor);
129 sb->s_bdi->ra_pages = 0;
130 sb->s_bdi->io_pages = 0;
135 static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)
137 struct squashfs_mount_opts *opts = fc->fs_private;
138 struct squashfs_sb_info *msblk;
139 struct squashfs_super_block *sblk = NULL;
141 long long root_inode;
142 unsigned short flags;
143 unsigned int fragments;
144 u64 lookup_table_start, xattr_id_table_start, next_table;
147 TRACE("Entered squashfs_fill_superblock\n");
150 * squashfs provides 'backing_dev_info' in order to disable read-ahead. For
151 * squashfs, I/O is not deferred, it is done immediately in readpage,
152 * which means the user would always have to wait their own I/O. So the effect
153 * of readahead is very weak for squashfs. squashfs_bdi_init will set
154 * sb->s_bdi->ra_pages and sb->s_bdi->io_pages to 0 and close readahead for
157 err = squashfs_bdi_init(sb);
159 errorf(fc, "squashfs init bdi failed");
163 sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
164 if (sb->s_fs_info == NULL) {
165 ERROR("Failed to allocate squashfs_sb_info\n");
168 msblk = sb->s_fs_info;
170 msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
172 msblk->devblksize = sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE);
173 msblk->devblksize_log2 = ffz(~msblk->devblksize);
175 mutex_init(&msblk->meta_index_mutex);
178 * msblk->bytes_used is checked in squashfs_read_table to ensure reads
179 * are not beyond filesystem end. But as we're using
180 * squashfs_read_table here to read the superblock (including the value
181 * of bytes_used) we need to set it to an initial sensible dummy value
183 msblk->bytes_used = sizeof(*sblk);
184 sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
187 errorf(fc, "unable to read squashfs_super_block");
195 /* Check it is a SQUASHFS superblock */
196 sb->s_magic = le32_to_cpu(sblk->s_magic);
197 if (sb->s_magic != SQUASHFS_MAGIC) {
198 if (!(fc->sb_flags & SB_SILENT))
199 errorf(fc, "Can't find a SQUASHFS superblock on %pg",
204 /* Check the MAJOR & MINOR versions and lookup compression type */
205 msblk->decompressor = supported_squashfs_filesystem(
207 le16_to_cpu(sblk->s_major),
208 le16_to_cpu(sblk->s_minor),
209 le16_to_cpu(sblk->compression));
210 if (msblk->decompressor == NULL)
213 /* Check the filesystem does not extend beyond the end of the
215 msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
216 if (msblk->bytes_used < 0 ||
217 msblk->bytes_used > bdev_nr_bytes(sb->s_bdev))
220 /* Check block size for sanity */
221 msblk->block_size = le32_to_cpu(sblk->block_size);
222 if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
226 * Check the system page size is not larger than the filesystem
227 * block size (by default 128K). This is currently not supported.
229 if (PAGE_SIZE > msblk->block_size) {
230 errorf(fc, "Page size > filesystem block size (%d). This is "
231 "currently not supported!", msblk->block_size);
235 /* Check block log for sanity */
236 msblk->block_log = le16_to_cpu(sblk->block_log);
237 if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
240 /* Check that block_size and block_log match */
241 if (msblk->block_size != (1 << msblk->block_log))
244 /* Check the root inode for sanity */
245 root_inode = le64_to_cpu(sblk->root_inode);
246 if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
249 msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
250 msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
251 msblk->inodes = le32_to_cpu(sblk->inodes);
252 msblk->fragments = le32_to_cpu(sblk->fragments);
253 msblk->ids = le16_to_cpu(sblk->no_ids);
254 flags = le16_to_cpu(sblk->flags);
256 TRACE("Found valid superblock on %pg\n", sb->s_bdev);
257 TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
259 TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
261 TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
262 TRACE("Block size %d\n", msblk->block_size);
263 TRACE("Number of inodes %d\n", msblk->inodes);
264 TRACE("Number of fragments %d\n", msblk->fragments);
265 TRACE("Number of ids %d\n", msblk->ids);
266 TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
267 TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
268 TRACE("sblk->fragment_table_start %llx\n",
269 (u64) le64_to_cpu(sblk->fragment_table_start));
270 TRACE("sblk->id_table_start %llx\n",
271 (u64) le64_to_cpu(sblk->id_table_start));
273 sb->s_maxbytes = MAX_LFS_FILESIZE;
275 sb->s_time_max = U32_MAX;
276 sb->s_flags |= SB_RDONLY;
277 sb->s_op = &squashfs_super_ops;
281 msblk->block_cache = squashfs_cache_init("metadata",
282 SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
283 if (msblk->block_cache == NULL)
286 /* Allocate read_page block */
287 msblk->read_page = squashfs_cache_init("data",
288 squashfs_max_decompressors(), msblk->block_size);
289 if (msblk->read_page == NULL) {
290 errorf(fc, "Failed to allocate read_page block");
294 msblk->stream = squashfs_decompressor_setup(sb, flags);
295 if (IS_ERR(msblk->stream)) {
296 err = PTR_ERR(msblk->stream);
297 msblk->stream = NULL;
302 sb->s_xattr = squashfs_xattr_handlers;
303 xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
304 if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
305 next_table = msblk->bytes_used;
306 goto allocate_id_index_table;
309 /* Allocate and read xattr id lookup table */
310 msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
311 xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
312 if (IS_ERR(msblk->xattr_id_table)) {
313 errorf(fc, "unable to read xattr id index table");
314 err = PTR_ERR(msblk->xattr_id_table);
315 msblk->xattr_id_table = NULL;
316 if (err != -ENOTSUPP)
319 next_table = msblk->xattr_table;
321 allocate_id_index_table:
322 /* Allocate and read id index table */
323 msblk->id_table = squashfs_read_id_index_table(sb,
324 le64_to_cpu(sblk->id_table_start), next_table, msblk->ids);
325 if (IS_ERR(msblk->id_table)) {
326 errorf(fc, "unable to read id index table");
327 err = PTR_ERR(msblk->id_table);
328 msblk->id_table = NULL;
331 next_table = le64_to_cpu(msblk->id_table[0]);
333 /* Handle inode lookup table */
334 lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
335 if (lookup_table_start == SQUASHFS_INVALID_BLK)
336 goto handle_fragments;
338 /* Allocate and read inode lookup table */
339 msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
340 lookup_table_start, next_table, msblk->inodes);
341 if (IS_ERR(msblk->inode_lookup_table)) {
342 errorf(fc, "unable to read inode lookup table");
343 err = PTR_ERR(msblk->inode_lookup_table);
344 msblk->inode_lookup_table = NULL;
347 next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
349 sb->s_export_op = &squashfs_export_ops;
352 fragments = msblk->fragments;
354 goto check_directory_table;
356 msblk->fragment_cache = squashfs_cache_init("fragment",
357 SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
358 if (msblk->fragment_cache == NULL) {
363 /* Allocate and read fragment index table */
364 msblk->fragment_index = squashfs_read_fragment_index_table(sb,
365 le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
366 if (IS_ERR(msblk->fragment_index)) {
367 errorf(fc, "unable to read fragment index table");
368 err = PTR_ERR(msblk->fragment_index);
369 msblk->fragment_index = NULL;
372 next_table = le64_to_cpu(msblk->fragment_index[0]);
374 check_directory_table:
375 /* Sanity check directory_table */
376 if (msblk->directory_table > next_table) {
381 /* Sanity check inode_table */
382 if (msblk->inode_table >= msblk->directory_table) {
388 root = new_inode(sb);
394 err = squashfs_read_inode(root, root_inode);
396 make_bad_inode(root);
400 insert_inode_hash(root);
402 sb->s_root = d_make_root(root);
403 if (sb->s_root == NULL) {
404 ERROR("Root inode create failed\n");
409 TRACE("Leaving squashfs_fill_super\n");
414 errorf(fc, "squashfs image failed sanity check");
416 squashfs_cache_delete(msblk->block_cache);
417 squashfs_cache_delete(msblk->fragment_cache);
418 squashfs_cache_delete(msblk->read_page);
419 squashfs_decompressor_destroy(msblk);
420 kfree(msblk->inode_lookup_table);
421 kfree(msblk->fragment_index);
422 kfree(msblk->id_table);
423 kfree(msblk->xattr_id_table);
424 kfree(sb->s_fs_info);
425 sb->s_fs_info = NULL;
430 static int squashfs_get_tree(struct fs_context *fc)
432 return get_tree_bdev(fc, squashfs_fill_super);
435 static int squashfs_reconfigure(struct fs_context *fc)
437 struct super_block *sb = fc->root->d_sb;
438 struct squashfs_sb_info *msblk = sb->s_fs_info;
439 struct squashfs_mount_opts *opts = fc->fs_private;
441 sync_filesystem(fc->root->d_sb);
442 fc->sb_flags |= SB_RDONLY;
444 msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
449 static void squashfs_free_fs_context(struct fs_context *fc)
451 kfree(fc->fs_private);
454 static const struct fs_context_operations squashfs_context_ops = {
455 .get_tree = squashfs_get_tree,
456 .free = squashfs_free_fs_context,
457 .parse_param = squashfs_parse_param,
458 .reconfigure = squashfs_reconfigure,
461 static int squashfs_show_options(struct seq_file *s, struct dentry *root)
463 struct super_block *sb = root->d_sb;
464 struct squashfs_sb_info *msblk = sb->s_fs_info;
466 if (msblk->panic_on_errors)
467 seq_puts(s, ",errors=panic");
469 seq_puts(s, ",errors=continue");
474 static int squashfs_init_fs_context(struct fs_context *fc)
476 struct squashfs_mount_opts *opts;
478 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
482 fc->fs_private = opts;
483 fc->ops = &squashfs_context_ops;
487 static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
489 struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
490 u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
492 TRACE("Entered squashfs_statfs\n");
494 buf->f_type = SQUASHFS_MAGIC;
495 buf->f_bsize = msblk->block_size;
496 buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
497 buf->f_bfree = buf->f_bavail = 0;
498 buf->f_files = msblk->inodes;
500 buf->f_namelen = SQUASHFS_NAME_LEN;
501 buf->f_fsid = u64_to_fsid(id);
507 static void squashfs_put_super(struct super_block *sb)
510 struct squashfs_sb_info *sbi = sb->s_fs_info;
511 squashfs_cache_delete(sbi->block_cache);
512 squashfs_cache_delete(sbi->fragment_cache);
513 squashfs_cache_delete(sbi->read_page);
514 squashfs_decompressor_destroy(sbi);
515 kfree(sbi->id_table);
516 kfree(sbi->fragment_index);
517 kfree(sbi->meta_index);
518 kfree(sbi->inode_lookup_table);
519 kfree(sbi->xattr_id_table);
520 kfree(sb->s_fs_info);
521 sb->s_fs_info = NULL;
525 static struct kmem_cache *squashfs_inode_cachep;
528 static void init_once(void *foo)
530 struct squashfs_inode_info *ei = foo;
532 inode_init_once(&ei->vfs_inode);
536 static int __init init_inodecache(void)
538 squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
539 sizeof(struct squashfs_inode_info), 0,
540 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
543 return squashfs_inode_cachep ? 0 : -ENOMEM;
547 static void destroy_inodecache(void)
550 * Make sure all delayed rcu free inodes are flushed before we
554 kmem_cache_destroy(squashfs_inode_cachep);
558 static int __init init_squashfs_fs(void)
560 int err = init_inodecache();
565 err = register_filesystem(&squashfs_fs_type);
567 destroy_inodecache();
571 pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
577 static void __exit exit_squashfs_fs(void)
579 unregister_filesystem(&squashfs_fs_type);
580 destroy_inodecache();
584 static struct inode *squashfs_alloc_inode(struct super_block *sb)
586 struct squashfs_inode_info *ei =
587 alloc_inode_sb(sb, squashfs_inode_cachep, GFP_KERNEL);
589 return ei ? &ei->vfs_inode : NULL;
593 static void squashfs_free_inode(struct inode *inode)
595 kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
598 static struct file_system_type squashfs_fs_type = {
599 .owner = THIS_MODULE,
601 .init_fs_context = squashfs_init_fs_context,
602 .parameters = squashfs_fs_parameters,
603 .kill_sb = kill_block_super,
604 .fs_flags = FS_REQUIRES_DEV
606 MODULE_ALIAS_FS("squashfs");
608 static const struct super_operations squashfs_super_ops = {
609 .alloc_inode = squashfs_alloc_inode,
610 .free_inode = squashfs_free_inode,
611 .statfs = squashfs_statfs,
612 .put_super = squashfs_put_super,
613 .show_options = squashfs_show_options,
616 module_init(init_squashfs_fs);
617 module_exit(exit_squashfs_fs);
618 MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
619 MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
620 MODULE_LICENSE("GPL");