2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 * Phillip Lougher <phillip@lougher.demon.co.uk>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * This file implements code to read the superblock, read and initialise
26 * in-memory structures at mount time, and all the VFS glue code to register
31 #include <linux/vfs.h>
32 #include <linux/slab.h>
33 #include <linux/smp_lock.h>
34 #include <linux/mutex.h>
35 #include <linux/pagemap.h>
36 #include <linux/init.h>
37 #include <linux/module.h>
38 #include <linux/magic.h>
40 #include "squashfs_fs.h"
41 #include "squashfs_fs_sb.h"
42 #include "squashfs_fs_i.h"
45 static struct file_system_type squashfs_fs_type;
46 static const struct super_operations squashfs_super_ops;
48 static int supported_squashfs_filesystem(short major, short minor, short comp)
50 if (major < SQUASHFS_MAJOR) {
51 ERROR("Major/Minor mismatch, older Squashfs %d.%d "
52 "filesystems are unsupported\n", major, minor);
54 } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
55 ERROR("Major/Minor mismatch, trying to mount newer "
56 "%d.%d filesystem\n", major, minor);
57 ERROR("Please update your kernel\n");
61 if (comp != ZLIB_COMPRESSION)
68 static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
70 struct squashfs_sb_info *msblk;
71 struct squashfs_super_block *sblk = NULL;
72 char b[BDEVNAME_SIZE];
76 unsigned int fragments;
77 u64 lookup_table_start;
80 TRACE("Entered squashfs_fill_superblock\n");
82 sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
83 if (sb->s_fs_info == NULL) {
84 ERROR("Failed to allocate squashfs_sb_info\n");
87 msblk = sb->s_fs_info;
89 msblk->stream = squashfs_zlib_init();
90 if (msblk->stream == NULL)
93 sblk = kzalloc(sizeof(*sblk), GFP_KERNEL);
95 ERROR("Failed to allocate squashfs_super_block\n");
99 msblk->devblksize = sb_min_blocksize(sb, BLOCK_SIZE);
100 msblk->devblksize_log2 = ffz(~msblk->devblksize);
102 mutex_init(&msblk->read_data_mutex);
103 mutex_init(&msblk->meta_index_mutex);
106 * msblk->bytes_used is checked in squashfs_read_table to ensure reads
107 * are not beyond filesystem end. But as we're using
108 * squashfs_read_table here to read the superblock (including the value
109 * of bytes_used) we need to set it to an initial sensible dummy value
111 msblk->bytes_used = sizeof(*sblk);
112 err = squashfs_read_table(sb, sblk, SQUASHFS_START, sizeof(*sblk));
115 ERROR("unable to read squashfs_super_block\n");
119 /* Check it is a SQUASHFS superblock */
120 sb->s_magic = le32_to_cpu(sblk->s_magic);
121 if (sb->s_magic != SQUASHFS_MAGIC) {
123 ERROR("Can't find a SQUASHFS superblock on %s\n",
124 bdevname(sb->s_bdev, b));
129 /* Check the MAJOR & MINOR versions and compression type */
130 err = supported_squashfs_filesystem(le16_to_cpu(sblk->s_major),
131 le16_to_cpu(sblk->s_minor),
132 le16_to_cpu(sblk->compression));
139 * Check if there's xattrs in the filesystem. These are not
140 * supported in this version, so warn that they will be ignored.
142 if (le64_to_cpu(sblk->xattr_table_start) != SQUASHFS_INVALID_BLK)
143 ERROR("Xattrs in filesystem, these will be ignored\n");
145 /* Check the filesystem does not extend beyond the end of the
147 msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
148 if (msblk->bytes_used < 0 || msblk->bytes_used >
149 i_size_read(sb->s_bdev->bd_inode))
152 /* Check block size for sanity */
153 msblk->block_size = le32_to_cpu(sblk->block_size);
154 if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
158 * Check the system page size is not larger than the filesystem
159 * block size (by default 128K). This is currently not supported.
161 if (PAGE_CACHE_SIZE > msblk->block_size) {
162 ERROR("Page size > filesystem block size (%d). This is "
163 "currently not supported!\n", msblk->block_size);
167 msblk->block_log = le16_to_cpu(sblk->block_log);
168 if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
171 /* Check the root inode for sanity */
172 root_inode = le64_to_cpu(sblk->root_inode);
173 if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
176 msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
177 msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
178 msblk->inodes = le32_to_cpu(sblk->inodes);
179 flags = le16_to_cpu(sblk->flags);
181 TRACE("Found valid superblock on %s\n", bdevname(sb->s_bdev, b));
182 TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
184 TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
186 TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
187 TRACE("Block size %d\n", msblk->block_size);
188 TRACE("Number of inodes %d\n", msblk->inodes);
189 TRACE("Number of fragments %d\n", le32_to_cpu(sblk->fragments));
190 TRACE("Number of ids %d\n", le16_to_cpu(sblk->no_ids));
191 TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
192 TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
193 TRACE("sblk->fragment_table_start %llx\n",
194 (u64) le64_to_cpu(sblk->fragment_table_start));
195 TRACE("sblk->id_table_start %llx\n",
196 (u64) le64_to_cpu(sblk->id_table_start));
198 sb->s_maxbytes = MAX_LFS_FILESIZE;
199 sb->s_flags |= MS_RDONLY;
200 sb->s_op = &squashfs_super_ops;
204 msblk->block_cache = squashfs_cache_init("metadata",
205 SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
206 if (msblk->block_cache == NULL)
209 /* Allocate read_page block */
210 msblk->read_page = squashfs_cache_init("data", 1, msblk->block_size);
211 if (msblk->read_page == NULL) {
212 ERROR("Failed to allocate read_page block\n");
216 /* Allocate and read id index table */
217 msblk->id_table = squashfs_read_id_index_table(sb,
218 le64_to_cpu(sblk->id_table_start), le16_to_cpu(sblk->no_ids));
219 if (IS_ERR(msblk->id_table)) {
220 err = PTR_ERR(msblk->id_table);
221 msblk->id_table = NULL;
225 fragments = le32_to_cpu(sblk->fragments);
227 goto allocate_lookup_table;
229 msblk->fragment_cache = squashfs_cache_init("fragment",
230 SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
231 if (msblk->fragment_cache == NULL) {
236 /* Allocate and read fragment index table */
237 msblk->fragment_index = squashfs_read_fragment_index_table(sb,
238 le64_to_cpu(sblk->fragment_table_start), fragments);
239 if (IS_ERR(msblk->fragment_index)) {
240 err = PTR_ERR(msblk->fragment_index);
241 msblk->fragment_index = NULL;
245 allocate_lookup_table:
246 lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
247 if (lookup_table_start == SQUASHFS_INVALID_BLK)
250 /* Allocate and read inode lookup table */
251 msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
252 lookup_table_start, msblk->inodes);
253 if (IS_ERR(msblk->inode_lookup_table)) {
254 err = PTR_ERR(msblk->inode_lookup_table);
255 msblk->inode_lookup_table = NULL;
259 sb->s_export_op = &squashfs_export_ops;
262 root = new_inode(sb);
268 err = squashfs_read_inode(root, root_inode);
273 insert_inode_hash(root);
275 sb->s_root = d_alloc_root(root);
276 if (sb->s_root == NULL) {
277 ERROR("Root inode create failed\n");
283 TRACE("Leaving squashfs_fill_super\n");
288 squashfs_cache_delete(msblk->block_cache);
289 squashfs_cache_delete(msblk->fragment_cache);
290 squashfs_cache_delete(msblk->read_page);
291 squashfs_zlib_free(msblk->stream);
292 kfree(msblk->inode_lookup_table);
293 kfree(msblk->fragment_index);
294 kfree(msblk->id_table);
295 kfree(sb->s_fs_info);
296 sb->s_fs_info = NULL;
301 squashfs_zlib_free(msblk->stream);
302 kfree(sb->s_fs_info);
303 sb->s_fs_info = NULL;
308 static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
310 struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
311 u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
313 TRACE("Entered squashfs_statfs\n");
315 buf->f_type = SQUASHFS_MAGIC;
316 buf->f_bsize = msblk->block_size;
317 buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
318 buf->f_bfree = buf->f_bavail = 0;
319 buf->f_files = msblk->inodes;
321 buf->f_namelen = SQUASHFS_NAME_LEN;
322 buf->f_fsid.val[0] = (u32)id;
323 buf->f_fsid.val[1] = (u32)(id >> 32);
329 static int squashfs_remount(struct super_block *sb, int *flags, char *data)
336 static void squashfs_put_super(struct super_block *sb)
341 struct squashfs_sb_info *sbi = sb->s_fs_info;
342 squashfs_cache_delete(sbi->block_cache);
343 squashfs_cache_delete(sbi->fragment_cache);
344 squashfs_cache_delete(sbi->read_page);
345 squashfs_zlib_free(sbi->stream);
346 kfree(sbi->id_table);
347 kfree(sbi->fragment_index);
348 kfree(sbi->meta_index);
349 kfree(sb->s_fs_info);
350 sb->s_fs_info = NULL;
357 static int squashfs_get_sb(struct file_system_type *fs_type, int flags,
358 const char *dev_name, void *data,
359 struct vfsmount *mnt)
361 return get_sb_bdev(fs_type, flags, dev_name, data, squashfs_fill_super,
366 static struct kmem_cache *squashfs_inode_cachep;
369 static void init_once(void *foo)
371 struct squashfs_inode_info *ei = foo;
373 inode_init_once(&ei->vfs_inode);
377 static int __init init_inodecache(void)
379 squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
380 sizeof(struct squashfs_inode_info), 0,
381 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, init_once);
383 return squashfs_inode_cachep ? 0 : -ENOMEM;
387 static void destroy_inodecache(void)
389 kmem_cache_destroy(squashfs_inode_cachep);
393 static int __init init_squashfs_fs(void)
395 int err = init_inodecache();
400 err = register_filesystem(&squashfs_fs_type);
402 destroy_inodecache();
406 printk(KERN_INFO "squashfs: version 4.0 (2009/01/31) "
407 "Phillip Lougher\n");
413 static void __exit exit_squashfs_fs(void)
415 unregister_filesystem(&squashfs_fs_type);
416 destroy_inodecache();
420 static struct inode *squashfs_alloc_inode(struct super_block *sb)
422 struct squashfs_inode_info *ei =
423 kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
425 return ei ? &ei->vfs_inode : NULL;
429 static void squashfs_destroy_inode(struct inode *inode)
431 kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
435 static struct file_system_type squashfs_fs_type = {
436 .owner = THIS_MODULE,
438 .get_sb = squashfs_get_sb,
439 .kill_sb = kill_block_super,
440 .fs_flags = FS_REQUIRES_DEV
443 static const struct super_operations squashfs_super_ops = {
444 .alloc_inode = squashfs_alloc_inode,
445 .destroy_inode = squashfs_destroy_inode,
446 .statfs = squashfs_statfs,
447 .put_super = squashfs_put_super,
448 .remount_fs = squashfs_remount
451 module_init(init_squashfs_fs);
452 module_exit(exit_squashfs_fs);
453 MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
454 MODULE_AUTHOR("Phillip Lougher <phillip@lougher.demon.co.uk>");
455 MODULE_LICENSE("GPL");