Merge branch 'for-linus' of git://git.o-hand.com/linux-rpurdie-leds
[pandora-kernel.git] / fs / squashfs / super.c
1 /*
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>
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>
39 #include <linux/xattr.h>
40
41 #include "squashfs_fs.h"
42 #include "squashfs_fs_sb.h"
43 #include "squashfs_fs_i.h"
44 #include "squashfs.h"
45 #include "decompressor.h"
46 #include "xattr.h"
47
48 static struct file_system_type squashfs_fs_type;
49 static const struct super_operations squashfs_super_ops;
50
51 static const struct squashfs_decompressor *supported_squashfs_filesystem(short
52         major, short minor, short id)
53 {
54         const struct squashfs_decompressor *decompressor;
55
56         if (major < SQUASHFS_MAJOR) {
57                 ERROR("Major/Minor mismatch, older Squashfs %d.%d "
58                         "filesystems are unsupported\n", major, minor);
59                 return NULL;
60         } 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");
64                 return NULL;
65         }
66
67         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         }
73
74         return decompressor;
75 }
76
77
78 static 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;
87         u64 lookup_table_start, xattr_id_table_start;
88         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
99         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
125         err = -EINVAL;
126
127         /* 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));
133                 goto failed_mount;
134         }
135
136         /* Check the MAJOR & MINOR versions and lookup compression type */
137         msblk->decompressor = supported_squashfs_filesystem(
138                         le16_to_cpu(sblk->s_major),
139                         le16_to_cpu(sblk->s_minor),
140                         le16_to_cpu(sblk->compression));
141         if (msblk->decompressor == NULL)
142                 goto failed_mount;
143
144         /* 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
156         /*
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
166         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
203         msblk->stream = squashfs_decompressor_init(msblk);
204         if (msblk->stream == NULL)
205                 goto failed_mount;
206
207         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
248 allocate_lookup_table:
249         lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
250         if (lookup_table_start == SQUASHFS_INVALID_BLK)
251                 goto allocate_xattr_table;
252
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
264 allocate_xattr_table:
265         sb->s_xattr = squashfs_xattr_handlers;
266         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;
276                 if (err != -ENOTSUPP)
277                         goto failed_mount;
278         }
279 allocate_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) {
288                 make_bad_inode(root);
289                 iput(root);
290                 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
306 failed_mount:
307         squashfs_cache_delete(msblk->block_cache);
308         squashfs_cache_delete(msblk->fragment_cache);
309         squashfs_cache_delete(msblk->read_page);
310         squashfs_decompressor_free(msblk, msblk->stream);
311         kfree(msblk->inode_lookup_table);
312         kfree(msblk->fragment_index);
313         kfree(msblk->id_table);
314         kfree(msblk->xattr_id_table);
315         kfree(sb->s_fs_info);
316         sb->s_fs_info = NULL;
317         kfree(sblk);
318         return err;
319
320 failure:
321         kfree(sb->s_fs_info);
322         sb->s_fs_info = NULL;
323         return -ENOMEM;
324 }
325
326
327 static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
328 {
329         struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
330         u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
331
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;
341         buf->f_fsid.val[0] = (u32)id;
342         buf->f_fsid.val[1] = (u32)(id >> 32);
343
344         return 0;
345 }
346
347
348 static int squashfs_remount(struct super_block *sb, int *flags, char *data)
349 {
350         *flags |= MS_RDONLY;
351         return 0;
352 }
353
354
355 static void squashfs_put_super(struct super_block *sb)
356 {
357         lock_kernel();
358
359         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);
364                 squashfs_decompressor_free(sbi, sbi->stream);
365                 kfree(sbi->id_table);
366                 kfree(sbi->fragment_index);
367                 kfree(sbi->meta_index);
368                 kfree(sbi->inode_lookup_table);
369                 kfree(sbi->xattr_id_table);
370                 kfree(sb->s_fs_info);
371                 sb->s_fs_info = NULL;
372         }
373
374         unlock_kernel();
375 }
376
377
378 static 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
387 static struct kmem_cache *squashfs_inode_cachep;
388
389
390 static void init_once(void *foo)
391 {
392         struct squashfs_inode_info *ei = foo;
393
394         inode_init_once(&ei->vfs_inode);
395 }
396
397
398 static 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
408 static void destroy_inodecache(void)
409 {
410         kmem_cache_destroy(squashfs_inode_cachep);
411 }
412
413
414 static 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
427         printk(KERN_INFO "squashfs: version 4.0 (2009/01/31) "
428                 "Phillip Lougher\n");
429
430         return 0;
431 }
432
433
434 static void __exit exit_squashfs_fs(void)
435 {
436         unregister_filesystem(&squashfs_fs_type);
437         destroy_inodecache();
438 }
439
440
441 static 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
450 static void squashfs_destroy_inode(struct inode *inode)
451 {
452         kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
453 }
454
455
456 static 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
464 static const struct super_operations squashfs_super_ops = {
465         .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
472 module_init(init_squashfs_fs);
473 module_exit(exit_squashfs_fs);
474 MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
475 MODULE_AUTHOR("Phillip Lougher <phillip@lougher.demon.co.uk>");
476 MODULE_LICENSE("GPL");