• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/fs/squashfs/
1/* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */
2/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6 * Phillip Lougher <phillip@lougher.demon.co.uk>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2,
11 * or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 *
22 * super.c
23 */
24
25/*
26 * This file implements code to read the superblock, read and initialise
27 * in-memory structures at mount time, and all the VFS glue code to register
28 * the filesystem.
29 */
30
31#include <linux/fs.h>
32#include <linux/vfs.h>
33#include <linux/slab.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
48static struct file_system_type squashfs_fs_type;
49static const struct super_operations squashfs_super_ops;
50
51static 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
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;
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->block_cache = squashfs_cache_init("metadata",
204			SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
205	if (msblk->block_cache == NULL)
206		goto failed_mount;
207
208	/* Allocate read_page block */
209	msblk->read_page = squashfs_cache_init("data", 1, msblk->block_size);
210	if (msblk->read_page == NULL) {
211		ERROR("Failed to allocate read_page block\n");
212		goto failed_mount;
213	}
214
215	msblk->stream = squashfs_decompressor_init(sb, flags);
216	if (IS_ERR(msblk->stream)) {
217		err = PTR_ERR(msblk->stream);
218		msblk->stream = NULL;
219		goto failed_mount;
220	}
221
222	/* Allocate and read id index table */
223	msblk->id_table = squashfs_read_id_index_table(sb,
224		le64_to_cpu(sblk->id_table_start), le16_to_cpu(sblk->no_ids));
225	if (IS_ERR(msblk->id_table)) {
226		err = PTR_ERR(msblk->id_table);
227		msblk->id_table = NULL;
228		goto failed_mount;
229	}
230
231	fragments = le32_to_cpu(sblk->fragments);
232	if (fragments == 0)
233		goto allocate_lookup_table;
234
235	msblk->fragment_cache = squashfs_cache_init("fragment",
236		SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
237	if (msblk->fragment_cache == NULL) {
238		err = -ENOMEM;
239		goto failed_mount;
240	}
241
242	/* Allocate and read fragment index table */
243	msblk->fragment_index = squashfs_read_fragment_index_table(sb,
244		le64_to_cpu(sblk->fragment_table_start), fragments);
245	if (IS_ERR(msblk->fragment_index)) {
246		err = PTR_ERR(msblk->fragment_index);
247		msblk->fragment_index = NULL;
248		goto failed_mount;
249	}
250
251allocate_lookup_table:
252	lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
253	if (lookup_table_start == SQUASHFS_INVALID_BLK)
254		goto allocate_xattr_table;
255
256	/* Allocate and read inode lookup table */
257	msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
258		lookup_table_start, msblk->inodes);
259	if (IS_ERR(msblk->inode_lookup_table)) {
260		err = PTR_ERR(msblk->inode_lookup_table);
261		msblk->inode_lookup_table = NULL;
262		goto failed_mount;
263	}
264
265	sb->s_export_op = &squashfs_export_ops;
266
267allocate_xattr_table:
268	sb->s_xattr = squashfs_xattr_handlers;
269	xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
270	if (xattr_id_table_start == SQUASHFS_INVALID_BLK)
271		goto allocate_root;
272
273	/* Allocate and read xattr id lookup table */
274	msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
275		xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
276	if (IS_ERR(msblk->xattr_id_table)) {
277		err = PTR_ERR(msblk->xattr_id_table);
278		msblk->xattr_id_table = NULL;
279		if (err != -ENOTSUPP)
280			goto failed_mount;
281	}
282allocate_root:
283	root = new_inode(sb);
284	if (!root) {
285		err = -ENOMEM;
286		goto failed_mount;
287	}
288
289	err = squashfs_read_inode(root, root_inode);
290	if (err) {
291		make_bad_inode(root);
292		iput(root);
293		goto failed_mount;
294	}
295	insert_inode_hash(root);
296
297	sb->s_root = d_alloc_root(root);
298	if (sb->s_root == NULL) {
299		ERROR("Root inode create failed\n");
300		err = -ENOMEM;
301		iput(root);
302		goto failed_mount;
303	}
304
305	TRACE("Leaving squashfs_fill_super\n");
306	kfree(sblk);
307	return 0;
308
309failed_mount:
310	squashfs_cache_delete(msblk->block_cache);
311	squashfs_cache_delete(msblk->fragment_cache);
312	squashfs_cache_delete(msblk->read_page);
313	squashfs_decompressor_free(msblk, msblk->stream);
314	kfree(msblk->inode_lookup_table);
315	kfree(msblk->fragment_index);
316	kfree(msblk->id_table);
317	kfree(msblk->xattr_id_table);
318	kfree(sb->s_fs_info);
319	sb->s_fs_info = NULL;
320	kfree(sblk);
321	return err;
322
323failure:
324	kfree(sb->s_fs_info);
325	sb->s_fs_info = NULL;
326	return -ENOMEM;
327}
328
329
330static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
331{
332	struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
333	u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
334
335	TRACE("Entered squashfs_statfs\n");
336
337	buf->f_type = SQUASHFS_MAGIC;
338	buf->f_bsize = msblk->block_size;
339	buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
340	buf->f_bfree = buf->f_bavail = 0;
341	buf->f_files = msblk->inodes;
342	buf->f_ffree = 0;
343	buf->f_namelen = SQUASHFS_NAME_LEN;
344	buf->f_fsid.val[0] = (u32)id;
345	buf->f_fsid.val[1] = (u32)(id >> 32);
346
347	return 0;
348}
349
350
351static int squashfs_remount(struct super_block *sb, int *flags, char *data)
352{
353	*flags |= MS_RDONLY;
354	return 0;
355}
356
357
358static void squashfs_put_super(struct super_block *sb)
359{
360	if (sb->s_fs_info) {
361		struct squashfs_sb_info *sbi = sb->s_fs_info;
362		squashfs_cache_delete(sbi->block_cache);
363		squashfs_cache_delete(sbi->fragment_cache);
364		squashfs_cache_delete(sbi->read_page);
365		squashfs_decompressor_free(sbi, sbi->stream);
366		kfree(sbi->id_table);
367		kfree(sbi->fragment_index);
368		kfree(sbi->meta_index);
369		kfree(sbi->inode_lookup_table);
370		kfree(sbi->xattr_id_table);
371		kfree(sb->s_fs_info);
372		sb->s_fs_info = NULL;
373	}
374}
375
376
377static int squashfs_get_sb(struct file_system_type *fs_type, int flags,
378				const char *dev_name, void *data,
379				struct vfsmount *mnt)
380{
381	return get_sb_bdev(fs_type, flags, dev_name, data, squashfs_fill_super,
382				mnt);
383}
384
385
386static struct kmem_cache *squashfs_inode_cachep;
387
388
389static void init_once(void *foo)
390{
391	struct squashfs_inode_info *ei = foo;
392
393	inode_init_once(&ei->vfs_inode);
394}
395
396
397static int __init init_inodecache(void)
398{
399	squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
400		sizeof(struct squashfs_inode_info), 0,
401		SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, init_once);
402
403	return squashfs_inode_cachep ? 0 : -ENOMEM;
404}
405
406
407static void destroy_inodecache(void)
408{
409	kmem_cache_destroy(squashfs_inode_cachep);
410}
411
412
413static int __init init_squashfs_fs(void)
414{
415	int err = init_inodecache();
416
417	if (err)
418		return err;
419
420	err = register_filesystem(&squashfs_fs_type);
421	if (err) {
422		destroy_inodecache();
423		return err;
424	}
425
426	printk(KERN_INFO "squashfs: version 4.0 (2009/01/31) "
427		"Phillip Lougher\n");
428
429	return 0;
430}
431
432
433static void __exit exit_squashfs_fs(void)
434{
435	unregister_filesystem(&squashfs_fs_type);
436	destroy_inodecache();
437}
438
439
440static struct inode *squashfs_alloc_inode(struct super_block *sb)
441{
442	struct squashfs_inode_info *ei =
443		kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
444
445	return ei ? &ei->vfs_inode : NULL;
446}
447
448
449static void squashfs_destroy_inode(struct inode *inode)
450{
451	kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
452}
453
454
455static struct file_system_type squashfs_fs_type = {
456	.owner = THIS_MODULE,
457	.name = "squashfs",
458	.get_sb = squashfs_get_sb,
459	.kill_sb = kill_block_super,
460	.fs_flags = FS_REQUIRES_DEV
461};
462
463static const struct super_operations squashfs_super_ops = {
464	.alloc_inode = squashfs_alloc_inode,
465	.destroy_inode = squashfs_destroy_inode,
466	.statfs = squashfs_statfs,
467	.put_super = squashfs_put_super,
468	.remount_fs = squashfs_remount
469};
470
471module_init(init_squashfs_fs);
472module_exit(exit_squashfs_fs);
473MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
474MODULE_AUTHOR("Phillip Lougher <phillip@lougher.demon.co.uk>");
475MODULE_LICENSE("GPL");
476