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
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>
29 #include "squashfs_fs.h"
30 #include "squashfs_fs_sb.h"
31 #include "squashfs_fs_i.h"
33 #include "decompressor.h"
36 static struct file_system_type squashfs_fs_type;
37 static const struct super_operations squashfs_super_ops;
39 static const struct squashfs_decompressor *supported_squashfs_filesystem(short
40 major, short minor, short id)
42 const struct squashfs_decompressor *decompressor;
44 if (major < SQUASHFS_MAJOR) {
45 ERROR("Major/Minor mismatch, older Squashfs %d.%d "
46 "filesystems are unsupported\n", major, minor);
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");
55 decompressor = squashfs_lookup_decompressor(id);
56 if (!decompressor->supported) {
57 ERROR("Filesystem uses \"%s\" compression. This is not "
58 "supported\n", decompressor->name);
66 static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
68 struct squashfs_sb_info *msblk;
69 struct squashfs_super_block *sblk = NULL;
73 unsigned int fragments;
74 u64 lookup_table_start, xattr_id_table_start, next_table;
77 TRACE("Entered squashfs_fill_superblock\n");
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");
84 msblk = sb->s_fs_info;
86 msblk->devblksize = sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE);
87 msblk->devblksize_log2 = ffz(~msblk->devblksize);
89 mutex_init(&msblk->meta_index_mutex);
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
97 msblk->bytes_used = sizeof(*sblk);
98 sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
101 ERROR("unable to read squashfs_super_block\n");
109 /* Check it is a SQUASHFS superblock */
110 sb->s_magic = le32_to_cpu(sblk->s_magic);
111 if (sb->s_magic != SQUASHFS_MAGIC) {
113 ERROR("Can't find a SQUASHFS superblock on %pg\n",
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)
126 /* Check the filesystem does not extend beyond the end of the
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))
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)
139 * Check the system page size is not larger than the filesystem
140 * block size (by default 128K). This is currently not supported.
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);
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)
153 /* Check that block_size and block_log match */
154 if (msblk->block_size != (1 << msblk->block_log))
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)
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);
168 TRACE("Found valid superblock on %pg\n", sb->s_bdev);
169 TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
171 TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
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));
185 sb->s_maxbytes = MAX_LFS_FILESIZE;
186 sb->s_flags |= SB_RDONLY;
187 sb->s_op = &squashfs_super_ops;
191 msblk->block_cache = squashfs_cache_init("metadata",
192 SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
193 if (msblk->block_cache == NULL)
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");
204 msblk->stream = squashfs_decompressor_setup(sb, flags);
205 if (IS_ERR(msblk->stream)) {
206 err = PTR_ERR(msblk->stream);
207 msblk->stream = NULL;
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;
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)
229 next_table = msblk->xattr_table;
231 allocate_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;
242 next_table = le64_to_cpu(msblk->id_table[0]);
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;
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;
258 next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
260 sb->s_export_op = &squashfs_export_ops;
263 fragments = msblk->fragments;
265 goto check_directory_table;
267 msblk->fragment_cache = squashfs_cache_init("fragment",
268 SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
269 if (msblk->fragment_cache == NULL) {
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;
283 next_table = le64_to_cpu(msblk->fragment_index[0]);
285 check_directory_table:
286 /* Sanity check directory_table */
287 if (msblk->directory_table > next_table) {
292 /* Sanity check inode_table */
293 if (msblk->inode_table >= msblk->directory_table) {
299 root = new_inode(sb);
305 err = squashfs_read_inode(root, root_inode);
307 make_bad_inode(root);
311 insert_inode_hash(root);
313 sb->s_root = d_make_root(root);
314 if (sb->s_root == NULL) {
315 ERROR("Root inode create failed\n");
320 TRACE("Leaving squashfs_fill_super\n");
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;
340 static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
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);
345 TRACE("Entered squashfs_statfs\n");
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;
353 buf->f_namelen = SQUASHFS_NAME_LEN;
354 buf->f_fsid.val[0] = (u32)id;
355 buf->f_fsid.val[1] = (u32)(id >> 32);
361 static int squashfs_remount(struct super_block *sb, int *flags, char *data)
369 static void squashfs_put_super(struct super_block *sb)
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;
388 static struct dentry *squashfs_mount(struct file_system_type *fs_type,
389 int flags, const char *dev_name, void *data)
391 return mount_bdev(fs_type, flags, dev_name, data, squashfs_fill_super);
395 static struct kmem_cache *squashfs_inode_cachep;
398 static void init_once(void *foo)
400 struct squashfs_inode_info *ei = foo;
402 inode_init_once(&ei->vfs_inode);
406 static int __init init_inodecache(void)
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,
413 return squashfs_inode_cachep ? 0 : -ENOMEM;
417 static void destroy_inodecache(void)
420 * Make sure all delayed rcu free inodes are flushed before we
424 kmem_cache_destroy(squashfs_inode_cachep);
428 static int __init init_squashfs_fs(void)
430 int err = init_inodecache();
435 err = register_filesystem(&squashfs_fs_type);
437 destroy_inodecache();
441 pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
447 static void __exit exit_squashfs_fs(void)
449 unregister_filesystem(&squashfs_fs_type);
450 destroy_inodecache();
454 static struct inode *squashfs_alloc_inode(struct super_block *sb)
456 struct squashfs_inode_info *ei =
457 kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
459 return ei ? &ei->vfs_inode : NULL;
463 static void squashfs_free_inode(struct inode *inode)
465 kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
468 static struct file_system_type squashfs_fs_type = {
469 .owner = THIS_MODULE,
471 .mount = squashfs_mount,
472 .kill_sb = kill_block_super,
473 .fs_flags = FS_REQUIRES_DEV
475 MODULE_ALIAS_FS("squashfs");
477 static 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
485 module_init(init_squashfs_fs);
486 module_exit(exit_squashfs_fs);
487 MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
488 MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
489 MODULE_LICENSE("GPL");