]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/squashfs/super.c
Squashfs: factor out remaining zlib dependencies into separate wrapper file
[net-next-2.6.git] / fs / squashfs / super.c
CommitLineData
0aa66619
PL
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>
405f5571 33#include <linux/smp_lock.h>
0aa66619
PL
34#include <linux/mutex.h>
35#include <linux/pagemap.h>
36#include <linux/init.h>
37#include <linux/module.h>
1bcbf313 38#include <linux/magic.h>
0aa66619
PL
39
40#include "squashfs_fs.h"
41#include "squashfs_fs_sb.h"
42#include "squashfs_fs_i.h"
43#include "squashfs.h"
44
45static struct file_system_type squashfs_fs_type;
b87221de 46static const struct super_operations squashfs_super_ops;
0aa66619
PL
47
48static int supported_squashfs_filesystem(short major, short minor, short comp)
49{
50 if (major < SQUASHFS_MAJOR) {
51 ERROR("Major/Minor mismatch, older Squashfs %d.%d "
52 "filesystems are unsupported\n", major, minor);
53 return -EINVAL;
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");
58 return -EINVAL;
59 }
60
61 if (comp != ZLIB_COMPRESSION)
62 return -EINVAL;
63
64 return 0;
65}
66
67
68static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
69{
70 struct squashfs_sb_info *msblk;
71 struct squashfs_super_block *sblk = NULL;
72 char b[BDEVNAME_SIZE];
73 struct inode *root;
74 long long root_inode;
75 unsigned short flags;
76 unsigned int fragments;
77 u64 lookup_table_start;
78 int err;
79
80 TRACE("Entered squashfs_fill_superblock\n");
81
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");
85 return -ENOMEM;
86 }
87 msblk = sb->s_fs_info;
88
f1a40359
PL
89 msblk->stream = squashfs_zlib_init();
90 if (msblk->stream == NULL)
0aa66619 91 goto failure;
0aa66619
PL
92
93 sblk = kzalloc(sizeof(*sblk), GFP_KERNEL);
94 if (sblk == NULL) {
95 ERROR("Failed to allocate squashfs_super_block\n");
96 goto failure;
97 }
98
99 msblk->devblksize = sb_min_blocksize(sb, BLOCK_SIZE);
100 msblk->devblksize_log2 = ffz(~msblk->devblksize);
101
102 mutex_init(&msblk->read_data_mutex);
103 mutex_init(&msblk->meta_index_mutex);
104
105 /*
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
110 */
111 msblk->bytes_used = sizeof(*sblk);
112 err = squashfs_read_table(sb, sblk, SQUASHFS_START, sizeof(*sblk));
113
114 if (err < 0) {
115 ERROR("unable to read squashfs_super_block\n");
116 goto failed_mount;
117 }
118
119 /* Check it is a SQUASHFS superblock */
120 sb->s_magic = le32_to_cpu(sblk->s_magic);
121 if (sb->s_magic != SQUASHFS_MAGIC) {
122 if (!silent)
123 ERROR("Can't find a SQUASHFS superblock on %s\n",
124 bdevname(sb->s_bdev, b));
125 err = -EINVAL;
126 goto failed_mount;
127 }
128
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));
133 if (err < 0)
134 goto failed_mount;
135
136 err = -EINVAL;
137
138 /*
139 * Check if there's xattrs in the filesystem. These are not
140 * supported in this version, so warn that they will be ignored.
141 */
142 if (le64_to_cpu(sblk->xattr_table_start) != SQUASHFS_INVALID_BLK)
143 ERROR("Xattrs in filesystem, these will be ignored\n");
144
145 /* Check the filesystem does not extend beyond the end of the
146 block device */
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))
150 goto failed_mount;
151
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)
155 goto failed_mount;
156
fffb47b8
PL
157 /*
158 * Check the system page size is not larger than the filesystem
159 * block size (by default 128K). This is currently not supported.
160 */
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);
164 goto failed_mount;
165 }
166
0aa66619
PL
167 msblk->block_log = le16_to_cpu(sblk->block_log);
168 if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
169 goto failed_mount;
170
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)
174 goto failed_mount;
175
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);
180
181 TRACE("Found valid superblock on %s\n", bdevname(sb->s_bdev, b));
182 TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
183 ? "un" : "");
184 TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
185 ? "un" : "");
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));
197
198 sb->s_maxbytes = MAX_LFS_FILESIZE;
199 sb->s_flags |= MS_RDONLY;
200 sb->s_op = &squashfs_super_ops;
201
202 err = -ENOMEM;
203
204 msblk->block_cache = squashfs_cache_init("metadata",
205 SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
206 if (msblk->block_cache == NULL)
207 goto failed_mount;
208
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");
213 goto failed_mount;
214 }
215
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;
222 goto failed_mount;
223 }
224
225 fragments = le32_to_cpu(sblk->fragments);
226 if (fragments == 0)
227 goto allocate_lookup_table;
228
229 msblk->fragment_cache = squashfs_cache_init("fragment",
230 SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
231 if (msblk->fragment_cache == NULL) {
232 err = -ENOMEM;
233 goto failed_mount;
234 }
235
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;
242 goto failed_mount;
243 }
244
245allocate_lookup_table:
246 lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
247 if (lookup_table_start == SQUASHFS_INVALID_BLK)
248 goto allocate_root;
249
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;
256 goto failed_mount;
257 }
258
259 sb->s_export_op = &squashfs_export_ops;
260
261allocate_root:
262 root = new_inode(sb);
263 if (!root) {
264 err = -ENOMEM;
265 goto failed_mount;
266 }
267
268 err = squashfs_read_inode(root, root_inode);
269 if (err) {
270 iget_failed(root);
271 goto failed_mount;
272 }
273 insert_inode_hash(root);
274
275 sb->s_root = d_alloc_root(root);
276 if (sb->s_root == NULL) {
277 ERROR("Root inode create failed\n");
278 err = -ENOMEM;
279 iput(root);
280 goto failed_mount;
281 }
282
283 TRACE("Leaving squashfs_fill_super\n");
284 kfree(sblk);
285 return 0;
286
287failed_mount:
288 squashfs_cache_delete(msblk->block_cache);
289 squashfs_cache_delete(msblk->fragment_cache);
290 squashfs_cache_delete(msblk->read_page);
f1a40359 291 squashfs_zlib_free(msblk->stream);
0aa66619
PL
292 kfree(msblk->inode_lookup_table);
293 kfree(msblk->fragment_index);
294 kfree(msblk->id_table);
0aa66619
PL
295 kfree(sb->s_fs_info);
296 sb->s_fs_info = NULL;
297 kfree(sblk);
298 return err;
299
300failure:
f1a40359 301 squashfs_zlib_free(msblk->stream);
0aa66619
PL
302 kfree(sb->s_fs_info);
303 sb->s_fs_info = NULL;
304 return -ENOMEM;
305}
306
307
308static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
309{
310 struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
2fc7f562 311 u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
0aa66619
PL
312
313 TRACE("Entered squashfs_statfs\n");
314
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;
320 buf->f_ffree = 0;
321 buf->f_namelen = SQUASHFS_NAME_LEN;
2fc7f562
CL
322 buf->f_fsid.val[0] = (u32)id;
323 buf->f_fsid.val[1] = (u32)(id >> 32);
0aa66619
PL
324
325 return 0;
326}
327
328
329static int squashfs_remount(struct super_block *sb, int *flags, char *data)
330{
331 *flags |= MS_RDONLY;
332 return 0;
333}
334
335
336static void squashfs_put_super(struct super_block *sb)
337{
6cfd0148
CH
338 lock_kernel();
339
0aa66619
PL
340 if (sb->s_fs_info) {
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);
f1a40359 345 squashfs_zlib_free(sbi->stream);
0aa66619
PL
346 kfree(sbi->id_table);
347 kfree(sbi->fragment_index);
348 kfree(sbi->meta_index);
0aa66619
PL
349 kfree(sb->s_fs_info);
350 sb->s_fs_info = NULL;
351 }
6cfd0148
CH
352
353 unlock_kernel();
0aa66619
PL
354}
355
356
357static int squashfs_get_sb(struct file_system_type *fs_type, int flags,
358 const char *dev_name, void *data,
359 struct vfsmount *mnt)
360{
361 return get_sb_bdev(fs_type, flags, dev_name, data, squashfs_fill_super,
362 mnt);
363}
364
365
366static struct kmem_cache *squashfs_inode_cachep;
367
368
369static void init_once(void *foo)
370{
371 struct squashfs_inode_info *ei = foo;
372
373 inode_init_once(&ei->vfs_inode);
374}
375
376
377static int __init init_inodecache(void)
378{
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);
382
383 return squashfs_inode_cachep ? 0 : -ENOMEM;
384}
385
386
387static void destroy_inodecache(void)
388{
389 kmem_cache_destroy(squashfs_inode_cachep);
390}
391
392
393static int __init init_squashfs_fs(void)
394{
395 int err = init_inodecache();
396
397 if (err)
398 return err;
399
400 err = register_filesystem(&squashfs_fs_type);
401 if (err) {
402 destroy_inodecache();
403 return err;
404 }
405
118e1ef6 406 printk(KERN_INFO "squashfs: version 4.0 (2009/01/31) "
0aa66619
PL
407 "Phillip Lougher\n");
408
409 return 0;
410}
411
412
413static void __exit exit_squashfs_fs(void)
414{
415 unregister_filesystem(&squashfs_fs_type);
416 destroy_inodecache();
417}
418
419
420static struct inode *squashfs_alloc_inode(struct super_block *sb)
421{
422 struct squashfs_inode_info *ei =
423 kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
424
425 return ei ? &ei->vfs_inode : NULL;
426}
427
428
429static void squashfs_destroy_inode(struct inode *inode)
430{
431 kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
432}
433
434
435static struct file_system_type squashfs_fs_type = {
436 .owner = THIS_MODULE,
437 .name = "squashfs",
438 .get_sb = squashfs_get_sb,
439 .kill_sb = kill_block_super,
440 .fs_flags = FS_REQUIRES_DEV
441};
442
b87221de 443static const struct super_operations squashfs_super_ops = {
0aa66619
PL
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
449};
450
451module_init(init_squashfs_fs);
452module_exit(exit_squashfs_fs);
453MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
454MODULE_AUTHOR("Phillip Lougher <phillip@lougher.demon.co.uk>");
455MODULE_LICENSE("GPL");