blob: 88b4f8606652b8e675621a2f545aff357ea176ae [file] [log] [blame]
Phillip Lougher0aa66612009-01-05 08:46:25 +00001/*
2 * Squashfs - a compressed read only filesystem for Linux
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 * Phillip Lougher <phillip@lougher.demon.co.uk>
6 *
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.
11 *
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.
16 *
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.
20 *
21 * super.c
22 */
23
24/*
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
27 * the filesystem.
28 */
29
30#include <linux/fs.h>
31#include <linux/vfs.h>
32#include <linux/slab.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040033#include <linux/smp_lock.h>
Phillip Lougher0aa66612009-01-05 08:46:25 +000034#include <linux/mutex.h>
35#include <linux/pagemap.h>
36#include <linux/init.h>
37#include <linux/module.h>
Qinghuang Feng1bcbf312009-01-15 13:51:03 -080038#include <linux/magic.h>
Phillip Lougher4b5397d2010-05-14 20:48:47 +010039#include <linux/xattr.h>
Phillip Lougher0aa66612009-01-05 08:46:25 +000040
41#include "squashfs_fs.h"
42#include "squashfs_fs_sb.h"
43#include "squashfs_fs_i.h"
44#include "squashfs.h"
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010045#include "decompressor.h"
Phillip Lougher01e5b4e2010-05-17 19:39:02 +010046#include "xattr.h"
Phillip Lougher0aa66612009-01-05 08:46:25 +000047
48static struct file_system_type squashfs_fs_type;
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070049static const struct super_operations squashfs_super_ops;
Phillip Lougher0aa66612009-01-05 08:46:25 +000050
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010051static const struct squashfs_decompressor *supported_squashfs_filesystem(short
52 major, short minor, short id)
Phillip Lougher0aa66612009-01-05 08:46:25 +000053{
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010054 const struct squashfs_decompressor *decompressor;
55
Phillip Lougher0aa66612009-01-05 08:46:25 +000056 if (major < SQUASHFS_MAJOR) {
57 ERROR("Major/Minor mismatch, older Squashfs %d.%d "
58 "filesystems are unsupported\n", major, minor);
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010059 return NULL;
Phillip Lougher0aa66612009-01-05 08:46:25 +000060 } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
61 ERROR("Major/Minor mismatch, trying to mount newer "
62 "%d.%d filesystem\n", major, minor);
63 ERROR("Please update your kernel\n");
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010064 return NULL;
Phillip Lougher0aa66612009-01-05 08:46:25 +000065 }
66
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010067 decompressor = squashfs_lookup_decompressor(id);
68 if (!decompressor->supported) {
69 ERROR("Filesystem uses \"%s\" compression. This is not "
70 "supported\n", decompressor->name);
71 return NULL;
72 }
Phillip Lougher0aa66612009-01-05 08:46:25 +000073
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010074 return decompressor;
Phillip Lougher0aa66612009-01-05 08:46:25 +000075}
76
77
78static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
79{
80 struct squashfs_sb_info *msblk;
81 struct squashfs_super_block *sblk = NULL;
82 char b[BDEVNAME_SIZE];
83 struct inode *root;
84 long long root_inode;
85 unsigned short flags;
86 unsigned int fragments;
Phillip Lougher4b5397d2010-05-14 20:48:47 +010087 u64 lookup_table_start, xattr_id_table_start;
Phillip Lougher0aa66612009-01-05 08:46:25 +000088 int err;
89
90 TRACE("Entered squashfs_fill_superblock\n");
91
92 sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
93 if (sb->s_fs_info == NULL) {
94 ERROR("Failed to allocate squashfs_sb_info\n");
95 return -ENOMEM;
96 }
97 msblk = sb->s_fs_info;
98
Phillip Lougher0aa66612009-01-05 08:46:25 +000099 sblk = kzalloc(sizeof(*sblk), GFP_KERNEL);
100 if (sblk == NULL) {
101 ERROR("Failed to allocate squashfs_super_block\n");
102 goto failure;
103 }
104
105 msblk->devblksize = sb_min_blocksize(sb, BLOCK_SIZE);
106 msblk->devblksize_log2 = ffz(~msblk->devblksize);
107
108 mutex_init(&msblk->read_data_mutex);
109 mutex_init(&msblk->meta_index_mutex);
110
111 /*
112 * msblk->bytes_used is checked in squashfs_read_table to ensure reads
113 * are not beyond filesystem end. But as we're using
114 * squashfs_read_table here to read the superblock (including the value
115 * of bytes_used) we need to set it to an initial sensible dummy value
116 */
117 msblk->bytes_used = sizeof(*sblk);
118 err = squashfs_read_table(sb, sblk, SQUASHFS_START, sizeof(*sblk));
119
120 if (err < 0) {
121 ERROR("unable to read squashfs_super_block\n");
122 goto failed_mount;
123 }
124
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100125 err = -EINVAL;
126
Phillip Lougher0aa66612009-01-05 08:46:25 +0000127 /* Check it is a SQUASHFS superblock */
128 sb->s_magic = le32_to_cpu(sblk->s_magic);
129 if (sb->s_magic != SQUASHFS_MAGIC) {
130 if (!silent)
131 ERROR("Can't find a SQUASHFS superblock on %s\n",
132 bdevname(sb->s_bdev, b));
Phillip Lougher0aa66612009-01-05 08:46:25 +0000133 goto failed_mount;
134 }
135
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100136 /* Check the MAJOR & MINOR versions and lookup compression type */
137 msblk->decompressor = supported_squashfs_filesystem(
138 le16_to_cpu(sblk->s_major),
Phillip Lougher0aa66612009-01-05 08:46:25 +0000139 le16_to_cpu(sblk->s_minor),
140 le16_to_cpu(sblk->compression));
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100141 if (msblk->decompressor == NULL)
Phillip Lougher0aa66612009-01-05 08:46:25 +0000142 goto failed_mount;
143
Phillip Lougher0aa66612009-01-05 08:46:25 +0000144 /* Check the filesystem does not extend beyond the end of the
145 block device */
146 msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
147 if (msblk->bytes_used < 0 || msblk->bytes_used >
148 i_size_read(sb->s_bdev->bd_inode))
149 goto failed_mount;
150
151 /* Check block size for sanity */
152 msblk->block_size = le32_to_cpu(sblk->block_size);
153 if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
154 goto failed_mount;
155
Phillip Lougherfffb47b2009-05-13 02:59:26 +0100156 /*
157 * Check the system page size is not larger than the filesystem
158 * block size (by default 128K). This is currently not supported.
159 */
160 if (PAGE_CACHE_SIZE > msblk->block_size) {
161 ERROR("Page size > filesystem block size (%d). This is "
162 "currently not supported!\n", msblk->block_size);
163 goto failed_mount;
164 }
165
Phillip Lougher0aa66612009-01-05 08:46:25 +0000166 msblk->block_log = le16_to_cpu(sblk->block_log);
167 if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
168 goto failed_mount;
169
170 /* Check the root inode for sanity */
171 root_inode = le64_to_cpu(sblk->root_inode);
172 if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
173 goto failed_mount;
174
175 msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
176 msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
177 msblk->inodes = le32_to_cpu(sblk->inodes);
178 flags = le16_to_cpu(sblk->flags);
179
180 TRACE("Found valid superblock on %s\n", bdevname(sb->s_bdev, b));
181 TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
182 ? "un" : "");
183 TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
184 ? "un" : "");
185 TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
186 TRACE("Block size %d\n", msblk->block_size);
187 TRACE("Number of inodes %d\n", msblk->inodes);
188 TRACE("Number of fragments %d\n", le32_to_cpu(sblk->fragments));
189 TRACE("Number of ids %d\n", le16_to_cpu(sblk->no_ids));
190 TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
191 TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
192 TRACE("sblk->fragment_table_start %llx\n",
193 (u64) le64_to_cpu(sblk->fragment_table_start));
194 TRACE("sblk->id_table_start %llx\n",
195 (u64) le64_to_cpu(sblk->id_table_start));
196
197 sb->s_maxbytes = MAX_LFS_FILESIZE;
198 sb->s_flags |= MS_RDONLY;
199 sb->s_op = &squashfs_super_ops;
200
201 err = -ENOMEM;
202
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100203 msblk->stream = squashfs_decompressor_init(msblk);
204 if (msblk->stream == NULL)
205 goto failed_mount;
206
Phillip Lougher0aa66612009-01-05 08:46:25 +0000207 msblk->block_cache = squashfs_cache_init("metadata",
208 SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
209 if (msblk->block_cache == NULL)
210 goto failed_mount;
211
212 /* Allocate read_page block */
213 msblk->read_page = squashfs_cache_init("data", 1, msblk->block_size);
214 if (msblk->read_page == NULL) {
215 ERROR("Failed to allocate read_page block\n");
216 goto failed_mount;
217 }
218
219 /* Allocate and read id index table */
220 msblk->id_table = squashfs_read_id_index_table(sb,
221 le64_to_cpu(sblk->id_table_start), le16_to_cpu(sblk->no_ids));
222 if (IS_ERR(msblk->id_table)) {
223 err = PTR_ERR(msblk->id_table);
224 msblk->id_table = NULL;
225 goto failed_mount;
226 }
227
228 fragments = le32_to_cpu(sblk->fragments);
229 if (fragments == 0)
230 goto allocate_lookup_table;
231
232 msblk->fragment_cache = squashfs_cache_init("fragment",
233 SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
234 if (msblk->fragment_cache == NULL) {
235 err = -ENOMEM;
236 goto failed_mount;
237 }
238
239 /* Allocate and read fragment index table */
240 msblk->fragment_index = squashfs_read_fragment_index_table(sb,
241 le64_to_cpu(sblk->fragment_table_start), fragments);
242 if (IS_ERR(msblk->fragment_index)) {
243 err = PTR_ERR(msblk->fragment_index);
244 msblk->fragment_index = NULL;
245 goto failed_mount;
246 }
247
248allocate_lookup_table:
249 lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
250 if (lookup_table_start == SQUASHFS_INVALID_BLK)
Phillip Lougher4b5397d2010-05-14 20:48:47 +0100251 goto allocate_xattr_table;
Phillip Lougher0aa66612009-01-05 08:46:25 +0000252
253 /* Allocate and read inode lookup table */
254 msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
255 lookup_table_start, msblk->inodes);
256 if (IS_ERR(msblk->inode_lookup_table)) {
257 err = PTR_ERR(msblk->inode_lookup_table);
258 msblk->inode_lookup_table = NULL;
259 goto failed_mount;
260 }
261
262 sb->s_export_op = &squashfs_export_ops;
263
Phillip Lougher4b5397d2010-05-14 20:48:47 +0100264allocate_xattr_table:
Phillip Lougher67f66cc2010-05-17 04:06:56 +0100265 sb->s_xattr = squashfs_xattr_handlers;
Phillip Lougher4b5397d2010-05-14 20:48:47 +0100266 xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
267 if (xattr_id_table_start == SQUASHFS_INVALID_BLK)
268 goto allocate_root;
269
270 /* Allocate and read xattr id lookup table */
271 msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
272 xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
273 if (IS_ERR(msblk->xattr_id_table)) {
274 err = PTR_ERR(msblk->xattr_id_table);
275 msblk->xattr_id_table = NULL;
Phillip Lougher01e5b4e2010-05-17 19:39:02 +0100276 if (err != -ENOTSUPP)
277 goto failed_mount;
Phillip Lougher4b5397d2010-05-14 20:48:47 +0100278 }
Phillip Lougher0aa66612009-01-05 08:46:25 +0000279allocate_root:
280 root = new_inode(sb);
281 if (!root) {
282 err = -ENOMEM;
283 goto failed_mount;
284 }
285
286 err = squashfs_read_inode(root, root_inode);
287 if (err) {
Phillip Lougher1cb08e92010-04-16 01:01:36 +0100288 make_bad_inode(root);
289 iput(root);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000290 goto failed_mount;
291 }
292 insert_inode_hash(root);
293
294 sb->s_root = d_alloc_root(root);
295 if (sb->s_root == NULL) {
296 ERROR("Root inode create failed\n");
297 err = -ENOMEM;
298 iput(root);
299 goto failed_mount;
300 }
301
302 TRACE("Leaving squashfs_fill_super\n");
303 kfree(sblk);
304 return 0;
305
306failed_mount:
307 squashfs_cache_delete(msblk->block_cache);
308 squashfs_cache_delete(msblk->fragment_cache);
309 squashfs_cache_delete(msblk->read_page);
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100310 squashfs_decompressor_free(msblk, msblk->stream);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000311 kfree(msblk->inode_lookup_table);
312 kfree(msblk->fragment_index);
313 kfree(msblk->id_table);
Phillip Lougher4b5397d2010-05-14 20:48:47 +0100314 kfree(msblk->xattr_id_table);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000315 kfree(sb->s_fs_info);
316 sb->s_fs_info = NULL;
317 kfree(sblk);
318 return err;
319
320failure:
Phillip Lougher0aa66612009-01-05 08:46:25 +0000321 kfree(sb->s_fs_info);
322 sb->s_fs_info = NULL;
323 return -ENOMEM;
324}
325
326
327static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
328{
329 struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
Coly Li2fc7f562009-04-02 16:59:42 -0700330 u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000331
332 TRACE("Entered squashfs_statfs\n");
333
334 buf->f_type = SQUASHFS_MAGIC;
335 buf->f_bsize = msblk->block_size;
336 buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
337 buf->f_bfree = buf->f_bavail = 0;
338 buf->f_files = msblk->inodes;
339 buf->f_ffree = 0;
340 buf->f_namelen = SQUASHFS_NAME_LEN;
Coly Li2fc7f562009-04-02 16:59:42 -0700341 buf->f_fsid.val[0] = (u32)id;
342 buf->f_fsid.val[1] = (u32)(id >> 32);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000343
344 return 0;
345}
346
347
348static int squashfs_remount(struct super_block *sb, int *flags, char *data)
349{
350 *flags |= MS_RDONLY;
351 return 0;
352}
353
354
355static void squashfs_put_super(struct super_block *sb)
356{
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200357 lock_kernel();
358
Phillip Lougher0aa66612009-01-05 08:46:25 +0000359 if (sb->s_fs_info) {
360 struct squashfs_sb_info *sbi = sb->s_fs_info;
361 squashfs_cache_delete(sbi->block_cache);
362 squashfs_cache_delete(sbi->fragment_cache);
363 squashfs_cache_delete(sbi->read_page);
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100364 squashfs_decompressor_free(sbi, sbi->stream);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000365 kfree(sbi->id_table);
366 kfree(sbi->fragment_index);
367 kfree(sbi->meta_index);
Phillip Lougher370ec3d2010-04-23 00:24:22 +0100368 kfree(sbi->inode_lookup_table);
Phillip Lougher4b5397d2010-05-14 20:48:47 +0100369 kfree(sbi->xattr_id_table);
Phillip Lougher0aa66612009-01-05 08:46:25 +0000370 kfree(sb->s_fs_info);
371 sb->s_fs_info = NULL;
372 }
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200373
374 unlock_kernel();
Phillip Lougher0aa66612009-01-05 08:46:25 +0000375}
376
377
378static int squashfs_get_sb(struct file_system_type *fs_type, int flags,
379 const char *dev_name, void *data,
380 struct vfsmount *mnt)
381{
382 return get_sb_bdev(fs_type, flags, dev_name, data, squashfs_fill_super,
383 mnt);
384}
385
386
387static struct kmem_cache *squashfs_inode_cachep;
388
389
390static void init_once(void *foo)
391{
392 struct squashfs_inode_info *ei = foo;
393
394 inode_init_once(&ei->vfs_inode);
395}
396
397
398static int __init init_inodecache(void)
399{
400 squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
401 sizeof(struct squashfs_inode_info), 0,
402 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, init_once);
403
404 return squashfs_inode_cachep ? 0 : -ENOMEM;
405}
406
407
408static void destroy_inodecache(void)
409{
410 kmem_cache_destroy(squashfs_inode_cachep);
411}
412
413
414static int __init init_squashfs_fs(void)
415{
416 int err = init_inodecache();
417
418 if (err)
419 return err;
420
421 err = register_filesystem(&squashfs_fs_type);
422 if (err) {
423 destroy_inodecache();
424 return err;
425 }
426
Phillip Lougher118e1ef2009-03-05 00:31:12 +0000427 printk(KERN_INFO "squashfs: version 4.0 (2009/01/31) "
Phillip Lougher0aa66612009-01-05 08:46:25 +0000428 "Phillip Lougher\n");
429
430 return 0;
431}
432
433
434static void __exit exit_squashfs_fs(void)
435{
436 unregister_filesystem(&squashfs_fs_type);
437 destroy_inodecache();
438}
439
440
441static struct inode *squashfs_alloc_inode(struct super_block *sb)
442{
443 struct squashfs_inode_info *ei =
444 kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
445
446 return ei ? &ei->vfs_inode : NULL;
447}
448
449
450static void squashfs_destroy_inode(struct inode *inode)
451{
452 kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
453}
454
455
456static struct file_system_type squashfs_fs_type = {
457 .owner = THIS_MODULE,
458 .name = "squashfs",
459 .get_sb = squashfs_get_sb,
460 .kill_sb = kill_block_super,
461 .fs_flags = FS_REQUIRES_DEV
462};
463
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700464static const struct super_operations squashfs_super_ops = {
Phillip Lougher0aa66612009-01-05 08:46:25 +0000465 .alloc_inode = squashfs_alloc_inode,
466 .destroy_inode = squashfs_destroy_inode,
467 .statfs = squashfs_statfs,
468 .put_super = squashfs_put_super,
469 .remount_fs = squashfs_remount
470};
471
472module_init(init_squashfs_fs);
473module_exit(exit_squashfs_fs);
474MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
475MODULE_AUTHOR("Phillip Lougher <phillip@lougher.demon.co.uk>");
476MODULE_LICENSE("GPL");