xref: /openbmc/linux/fs/squashfs/super.c (revision d7f2ff67)
10aa66619SPhillip Lougher /*
20aa66619SPhillip Lougher  * Squashfs - a compressed read only filesystem for Linux
30aa66619SPhillip Lougher  *
40aa66619SPhillip Lougher  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5d7f2ff67SPhillip Lougher  * Phillip Lougher <phillip@squashfs.org.uk>
60aa66619SPhillip Lougher  *
70aa66619SPhillip Lougher  * This program is free software; you can redistribute it and/or
80aa66619SPhillip Lougher  * modify it under the terms of the GNU General Public License
90aa66619SPhillip Lougher  * as published by the Free Software Foundation; either version 2,
100aa66619SPhillip Lougher  * or (at your option) any later version.
110aa66619SPhillip Lougher  *
120aa66619SPhillip Lougher  * This program is distributed in the hope that it will be useful,
130aa66619SPhillip Lougher  * but WITHOUT ANY WARRANTY; without even the implied warranty of
140aa66619SPhillip Lougher  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
150aa66619SPhillip Lougher  * GNU General Public License for more details.
160aa66619SPhillip Lougher  *
170aa66619SPhillip Lougher  * You should have received a copy of the GNU General Public License
180aa66619SPhillip Lougher  * along with this program; if not, write to the Free Software
190aa66619SPhillip Lougher  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
200aa66619SPhillip Lougher  *
210aa66619SPhillip Lougher  * super.c
220aa66619SPhillip Lougher  */
230aa66619SPhillip Lougher 
240aa66619SPhillip Lougher /*
250aa66619SPhillip Lougher  * This file implements code to read the superblock, read and initialise
260aa66619SPhillip Lougher  * in-memory structures at mount time, and all the VFS glue code to register
270aa66619SPhillip Lougher  * the filesystem.
280aa66619SPhillip Lougher  */
290aa66619SPhillip Lougher 
300aa66619SPhillip Lougher #include <linux/fs.h>
310aa66619SPhillip Lougher #include <linux/vfs.h>
320aa66619SPhillip Lougher #include <linux/slab.h>
330aa66619SPhillip Lougher #include <linux/mutex.h>
340aa66619SPhillip Lougher #include <linux/pagemap.h>
350aa66619SPhillip Lougher #include <linux/init.h>
360aa66619SPhillip Lougher #include <linux/module.h>
371bcbf313SQinghuang Feng #include <linux/magic.h>
384b5397dcSPhillip Lougher #include <linux/xattr.h>
390aa66619SPhillip Lougher 
400aa66619SPhillip Lougher #include "squashfs_fs.h"
410aa66619SPhillip Lougher #include "squashfs_fs_sb.h"
420aa66619SPhillip Lougher #include "squashfs_fs_i.h"
430aa66619SPhillip Lougher #include "squashfs.h"
444c0f0bb2SPhillip Lougher #include "decompressor.h"
4501e5b4e4SPhillip Lougher #include "xattr.h"
460aa66619SPhillip Lougher 
470aa66619SPhillip Lougher static struct file_system_type squashfs_fs_type;
48b87221deSAlexey Dobriyan static const struct super_operations squashfs_super_ops;
490aa66619SPhillip Lougher 
504c0f0bb2SPhillip Lougher static const struct squashfs_decompressor *supported_squashfs_filesystem(short
514c0f0bb2SPhillip Lougher 	major, short minor, short id)
520aa66619SPhillip Lougher {
534c0f0bb2SPhillip Lougher 	const struct squashfs_decompressor *decompressor;
544c0f0bb2SPhillip Lougher 
550aa66619SPhillip Lougher 	if (major < SQUASHFS_MAJOR) {
560aa66619SPhillip Lougher 		ERROR("Major/Minor mismatch, older Squashfs %d.%d "
570aa66619SPhillip Lougher 			"filesystems are unsupported\n", major, minor);
584c0f0bb2SPhillip Lougher 		return NULL;
590aa66619SPhillip Lougher 	} else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
600aa66619SPhillip Lougher 		ERROR("Major/Minor mismatch, trying to mount newer "
610aa66619SPhillip Lougher 			"%d.%d filesystem\n", major, minor);
620aa66619SPhillip Lougher 		ERROR("Please update your kernel\n");
634c0f0bb2SPhillip Lougher 		return NULL;
640aa66619SPhillip Lougher 	}
650aa66619SPhillip Lougher 
664c0f0bb2SPhillip Lougher 	decompressor = squashfs_lookup_decompressor(id);
674c0f0bb2SPhillip Lougher 	if (!decompressor->supported) {
684c0f0bb2SPhillip Lougher 		ERROR("Filesystem uses \"%s\" compression. This is not "
694c0f0bb2SPhillip Lougher 			"supported\n", decompressor->name);
704c0f0bb2SPhillip Lougher 		return NULL;
714c0f0bb2SPhillip Lougher 	}
720aa66619SPhillip Lougher 
734c0f0bb2SPhillip Lougher 	return decompressor;
740aa66619SPhillip Lougher }
750aa66619SPhillip Lougher 
760aa66619SPhillip Lougher 
770aa66619SPhillip Lougher static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
780aa66619SPhillip Lougher {
790aa66619SPhillip Lougher 	struct squashfs_sb_info *msblk;
800aa66619SPhillip Lougher 	struct squashfs_super_block *sblk = NULL;
810aa66619SPhillip Lougher 	char b[BDEVNAME_SIZE];
820aa66619SPhillip Lougher 	struct inode *root;
830aa66619SPhillip Lougher 	long long root_inode;
840aa66619SPhillip Lougher 	unsigned short flags;
850aa66619SPhillip Lougher 	unsigned int fragments;
8637986f63SPhillip Lougher 	u64 lookup_table_start, xattr_id_table_start, next_table;
870aa66619SPhillip Lougher 	int err;
880aa66619SPhillip Lougher 
890aa66619SPhillip Lougher 	TRACE("Entered squashfs_fill_superblock\n");
900aa66619SPhillip Lougher 
910aa66619SPhillip Lougher 	sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
920aa66619SPhillip Lougher 	if (sb->s_fs_info == NULL) {
930aa66619SPhillip Lougher 		ERROR("Failed to allocate squashfs_sb_info\n");
940aa66619SPhillip Lougher 		return -ENOMEM;
950aa66619SPhillip Lougher 	}
960aa66619SPhillip Lougher 	msblk = sb->s_fs_info;
970aa66619SPhillip Lougher 
980aa66619SPhillip Lougher 	msblk->devblksize = sb_min_blocksize(sb, BLOCK_SIZE);
990aa66619SPhillip Lougher 	msblk->devblksize_log2 = ffz(~msblk->devblksize);
1000aa66619SPhillip Lougher 
1010aa66619SPhillip Lougher 	mutex_init(&msblk->read_data_mutex);
1020aa66619SPhillip Lougher 	mutex_init(&msblk->meta_index_mutex);
1030aa66619SPhillip Lougher 
1040aa66619SPhillip Lougher 	/*
1050aa66619SPhillip Lougher 	 * msblk->bytes_used is checked in squashfs_read_table to ensure reads
1060aa66619SPhillip Lougher 	 * are not beyond filesystem end.  But as we're using
1070aa66619SPhillip Lougher 	 * squashfs_read_table here to read the superblock (including the value
1080aa66619SPhillip Lougher 	 * of bytes_used) we need to set it to an initial sensible dummy value
1090aa66619SPhillip Lougher 	 */
1100aa66619SPhillip Lougher 	msblk->bytes_used = sizeof(*sblk);
11182de647eSPhillip Lougher 	sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
1120aa66619SPhillip Lougher 
11382de647eSPhillip Lougher 	if (IS_ERR(sblk)) {
1140aa66619SPhillip Lougher 		ERROR("unable to read squashfs_super_block\n");
11582de647eSPhillip Lougher 		err = PTR_ERR(sblk);
11682de647eSPhillip Lougher 		sblk = NULL;
1170aa66619SPhillip Lougher 		goto failed_mount;
1180aa66619SPhillip Lougher 	}
1190aa66619SPhillip Lougher 
1204c0f0bb2SPhillip Lougher 	err = -EINVAL;
1214c0f0bb2SPhillip Lougher 
1220aa66619SPhillip Lougher 	/* Check it is a SQUASHFS superblock */
1230aa66619SPhillip Lougher 	sb->s_magic = le32_to_cpu(sblk->s_magic);
1240aa66619SPhillip Lougher 	if (sb->s_magic != SQUASHFS_MAGIC) {
1250aa66619SPhillip Lougher 		if (!silent)
1260aa66619SPhillip Lougher 			ERROR("Can't find a SQUASHFS superblock on %s\n",
1270aa66619SPhillip Lougher 						bdevname(sb->s_bdev, b));
1280aa66619SPhillip Lougher 		goto failed_mount;
1290aa66619SPhillip Lougher 	}
1300aa66619SPhillip Lougher 
1314c0f0bb2SPhillip Lougher 	/* Check the MAJOR & MINOR versions and lookup compression type */
1324c0f0bb2SPhillip Lougher 	msblk->decompressor = supported_squashfs_filesystem(
1334c0f0bb2SPhillip Lougher 			le16_to_cpu(sblk->s_major),
1340aa66619SPhillip Lougher 			le16_to_cpu(sblk->s_minor),
1350aa66619SPhillip Lougher 			le16_to_cpu(sblk->compression));
1364c0f0bb2SPhillip Lougher 	if (msblk->decompressor == NULL)
1370aa66619SPhillip Lougher 		goto failed_mount;
1380aa66619SPhillip Lougher 
1390aa66619SPhillip Lougher 	/* Check the filesystem does not extend beyond the end of the
1400aa66619SPhillip Lougher 	   block device */
1410aa66619SPhillip Lougher 	msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
1420aa66619SPhillip Lougher 	if (msblk->bytes_used < 0 || msblk->bytes_used >
1430aa66619SPhillip Lougher 			i_size_read(sb->s_bdev->bd_inode))
1440aa66619SPhillip Lougher 		goto failed_mount;
1450aa66619SPhillip Lougher 
1460aa66619SPhillip Lougher 	/* Check block size for sanity */
1470aa66619SPhillip Lougher 	msblk->block_size = le32_to_cpu(sblk->block_size);
1480aa66619SPhillip Lougher 	if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
1490aa66619SPhillip Lougher 		goto failed_mount;
1500aa66619SPhillip Lougher 
151fffb47b8SPhillip Lougher 	/*
152fffb47b8SPhillip Lougher 	 * Check the system page size is not larger than the filesystem
153fffb47b8SPhillip Lougher 	 * block size (by default 128K).  This is currently not supported.
154fffb47b8SPhillip Lougher 	 */
155fffb47b8SPhillip Lougher 	if (PAGE_CACHE_SIZE > msblk->block_size) {
156fffb47b8SPhillip Lougher 		ERROR("Page size > filesystem block size (%d).  This is "
157fffb47b8SPhillip Lougher 			"currently not supported!\n", msblk->block_size);
158fffb47b8SPhillip Lougher 		goto failed_mount;
159fffb47b8SPhillip Lougher 	}
160fffb47b8SPhillip Lougher 
1610aa66619SPhillip Lougher 	msblk->block_log = le16_to_cpu(sblk->block_log);
1620aa66619SPhillip Lougher 	if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
1630aa66619SPhillip Lougher 		goto failed_mount;
1640aa66619SPhillip Lougher 
1650aa66619SPhillip Lougher 	/* Check the root inode for sanity */
1660aa66619SPhillip Lougher 	root_inode = le64_to_cpu(sblk->root_inode);
1670aa66619SPhillip Lougher 	if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
1680aa66619SPhillip Lougher 		goto failed_mount;
1690aa66619SPhillip Lougher 
1700aa66619SPhillip Lougher 	msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
1710aa66619SPhillip Lougher 	msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
1720aa66619SPhillip Lougher 	msblk->inodes = le32_to_cpu(sblk->inodes);
1730aa66619SPhillip Lougher 	flags = le16_to_cpu(sblk->flags);
1740aa66619SPhillip Lougher 
1750aa66619SPhillip Lougher 	TRACE("Found valid superblock on %s\n", bdevname(sb->s_bdev, b));
1760aa66619SPhillip Lougher 	TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
1770aa66619SPhillip Lougher 				? "un" : "");
1780aa66619SPhillip Lougher 	TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
1790aa66619SPhillip Lougher 				? "un" : "");
1800aa66619SPhillip Lougher 	TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
1810aa66619SPhillip Lougher 	TRACE("Block size %d\n", msblk->block_size);
1820aa66619SPhillip Lougher 	TRACE("Number of inodes %d\n", msblk->inodes);
1830aa66619SPhillip Lougher 	TRACE("Number of fragments %d\n", le32_to_cpu(sblk->fragments));
1840aa66619SPhillip Lougher 	TRACE("Number of ids %d\n", le16_to_cpu(sblk->no_ids));
1850aa66619SPhillip Lougher 	TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
1860aa66619SPhillip Lougher 	TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
1870aa66619SPhillip Lougher 	TRACE("sblk->fragment_table_start %llx\n",
1880aa66619SPhillip Lougher 		(u64) le64_to_cpu(sblk->fragment_table_start));
1890aa66619SPhillip Lougher 	TRACE("sblk->id_table_start %llx\n",
1900aa66619SPhillip Lougher 		(u64) le64_to_cpu(sblk->id_table_start));
1910aa66619SPhillip Lougher 
1920aa66619SPhillip Lougher 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1930aa66619SPhillip Lougher 	sb->s_flags |= MS_RDONLY;
1940aa66619SPhillip Lougher 	sb->s_op = &squashfs_super_ops;
1950aa66619SPhillip Lougher 
1960aa66619SPhillip Lougher 	err = -ENOMEM;
1970aa66619SPhillip Lougher 
1980aa66619SPhillip Lougher 	msblk->block_cache = squashfs_cache_init("metadata",
1990aa66619SPhillip Lougher 			SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
2000aa66619SPhillip Lougher 	if (msblk->block_cache == NULL)
2010aa66619SPhillip Lougher 		goto failed_mount;
2020aa66619SPhillip Lougher 
2030aa66619SPhillip Lougher 	/* Allocate read_page block */
2040aa66619SPhillip Lougher 	msblk->read_page = squashfs_cache_init("data", 1, msblk->block_size);
2050aa66619SPhillip Lougher 	if (msblk->read_page == NULL) {
2060aa66619SPhillip Lougher 		ERROR("Failed to allocate read_page block\n");
2070aa66619SPhillip Lougher 		goto failed_mount;
2080aa66619SPhillip Lougher 	}
2090aa66619SPhillip Lougher 
210b7fc0ff0SPhillip Lougher 	msblk->stream = squashfs_decompressor_init(sb, flags);
211b7fc0ff0SPhillip Lougher 	if (IS_ERR(msblk->stream)) {
212b7fc0ff0SPhillip Lougher 		err = PTR_ERR(msblk->stream);
213b7fc0ff0SPhillip Lougher 		msblk->stream = NULL;
214b7fc0ff0SPhillip Lougher 		goto failed_mount;
215b7fc0ff0SPhillip Lougher 	}
216b7fc0ff0SPhillip Lougher 
21776e002f7SPhillip Lougher 	/* Handle xattrs */
21876e002f7SPhillip Lougher 	sb->s_xattr = squashfs_xattr_handlers;
21976e002f7SPhillip Lougher 	xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
22037986f63SPhillip Lougher 	if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
22137986f63SPhillip Lougher 		next_table = msblk->bytes_used;
22276e002f7SPhillip Lougher 		goto allocate_id_index_table;
22337986f63SPhillip Lougher 	}
22476e002f7SPhillip Lougher 
22576e002f7SPhillip Lougher 	/* Allocate and read xattr id lookup table */
22676e002f7SPhillip Lougher 	msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
22776e002f7SPhillip Lougher 		xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
22876e002f7SPhillip Lougher 	if (IS_ERR(msblk->xattr_id_table)) {
22976e002f7SPhillip Lougher 		ERROR("unable to read xattr id index table\n");
23076e002f7SPhillip Lougher 		err = PTR_ERR(msblk->xattr_id_table);
23176e002f7SPhillip Lougher 		msblk->xattr_id_table = NULL;
23276e002f7SPhillip Lougher 		if (err != -ENOTSUPP)
23376e002f7SPhillip Lougher 			goto failed_mount;
23476e002f7SPhillip Lougher 	}
23537986f63SPhillip Lougher 	next_table = msblk->xattr_table;
23676e002f7SPhillip Lougher 
23776e002f7SPhillip Lougher allocate_id_index_table:
2380aa66619SPhillip Lougher 	/* Allocate and read id index table */
2390aa66619SPhillip Lougher 	msblk->id_table = squashfs_read_id_index_table(sb,
24037986f63SPhillip Lougher 		le64_to_cpu(sblk->id_table_start), next_table,
24137986f63SPhillip Lougher 		le16_to_cpu(sblk->no_ids));
2420aa66619SPhillip Lougher 	if (IS_ERR(msblk->id_table)) {
24382de647eSPhillip Lougher 		ERROR("unable to read id index table\n");
2440aa66619SPhillip Lougher 		err = PTR_ERR(msblk->id_table);
2450aa66619SPhillip Lougher 		msblk->id_table = NULL;
2460aa66619SPhillip Lougher 		goto failed_mount;
2470aa66619SPhillip Lougher 	}
248ac51a0a7SPhillip Lougher 	next_table = msblk->id_table[0];
2490aa66619SPhillip Lougher 
25076e002f7SPhillip Lougher 	/* Handle inode lookup table */
25176e002f7SPhillip Lougher 	lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
25276e002f7SPhillip Lougher 	if (lookup_table_start == SQUASHFS_INVALID_BLK)
25376e002f7SPhillip Lougher 		goto handle_fragments;
25476e002f7SPhillip Lougher 
25576e002f7SPhillip Lougher 	/* Allocate and read inode lookup table */
25676e002f7SPhillip Lougher 	msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
257ac51a0a7SPhillip Lougher 		lookup_table_start, next_table, msblk->inodes);
25876e002f7SPhillip Lougher 	if (IS_ERR(msblk->inode_lookup_table)) {
25976e002f7SPhillip Lougher 		ERROR("unable to read inode lookup table\n");
26076e002f7SPhillip Lougher 		err = PTR_ERR(msblk->inode_lookup_table);
26176e002f7SPhillip Lougher 		msblk->inode_lookup_table = NULL;
26276e002f7SPhillip Lougher 		goto failed_mount;
26376e002f7SPhillip Lougher 	}
2641cac63ccSPhillip Lougher 	next_table = msblk->inode_lookup_table[0];
26576e002f7SPhillip Lougher 
26676e002f7SPhillip Lougher 	sb->s_export_op = &squashfs_export_ops;
26776e002f7SPhillip Lougher 
26876e002f7SPhillip Lougher handle_fragments:
2690aa66619SPhillip Lougher 	fragments = le32_to_cpu(sblk->fragments);
2700aa66619SPhillip Lougher 	if (fragments == 0)
2711094a4a6SPhillip Lougher 		goto check_directory_table;
2720aa66619SPhillip Lougher 
2730aa66619SPhillip Lougher 	msblk->fragment_cache = squashfs_cache_init("fragment",
2740aa66619SPhillip Lougher 		SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
2750aa66619SPhillip Lougher 	if (msblk->fragment_cache == NULL) {
2760aa66619SPhillip Lougher 		err = -ENOMEM;
2770aa66619SPhillip Lougher 		goto failed_mount;
2780aa66619SPhillip Lougher 	}
2790aa66619SPhillip Lougher 
2800aa66619SPhillip Lougher 	/* Allocate and read fragment index table */
2810aa66619SPhillip Lougher 	msblk->fragment_index = squashfs_read_fragment_index_table(sb,
2821cac63ccSPhillip Lougher 		le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
2830aa66619SPhillip Lougher 	if (IS_ERR(msblk->fragment_index)) {
28482de647eSPhillip Lougher 		ERROR("unable to read fragment index table\n");
2850aa66619SPhillip Lougher 		err = PTR_ERR(msblk->fragment_index);
2860aa66619SPhillip Lougher 		msblk->fragment_index = NULL;
2870aa66619SPhillip Lougher 		goto failed_mount;
2880aa66619SPhillip Lougher 	}
2891094a4a6SPhillip Lougher 	next_table = msblk->fragment_index[0];
2900aa66619SPhillip Lougher 
2911094a4a6SPhillip Lougher check_directory_table:
2921094a4a6SPhillip Lougher 	/* Sanity check directory_table */
2931094a4a6SPhillip Lougher 	if (msblk->directory_table >= next_table) {
2941094a4a6SPhillip Lougher 		err = -EINVAL;
2951094a4a6SPhillip Lougher 		goto failed_mount;
2961094a4a6SPhillip Lougher 	}
2971094a4a6SPhillip Lougher 
2981094a4a6SPhillip Lougher 	/* Sanity check inode_table */
2991094a4a6SPhillip Lougher 	if (msblk->inode_table >= msblk->directory_table) {
3001094a4a6SPhillip Lougher 		err = -EINVAL;
3011094a4a6SPhillip Lougher 		goto failed_mount;
3021094a4a6SPhillip Lougher 	}
3031094a4a6SPhillip Lougher 
3041094a4a6SPhillip Lougher 	/* allocate root */
3050aa66619SPhillip Lougher 	root = new_inode(sb);
3060aa66619SPhillip Lougher 	if (!root) {
3070aa66619SPhillip Lougher 		err = -ENOMEM;
3080aa66619SPhillip Lougher 		goto failed_mount;
3090aa66619SPhillip Lougher 	}
3100aa66619SPhillip Lougher 
3110aa66619SPhillip Lougher 	err = squashfs_read_inode(root, root_inode);
3120aa66619SPhillip Lougher 	if (err) {
3131cb08e97SPhillip Lougher 		make_bad_inode(root);
3141cb08e97SPhillip Lougher 		iput(root);
3150aa66619SPhillip Lougher 		goto failed_mount;
3160aa66619SPhillip Lougher 	}
3170aa66619SPhillip Lougher 	insert_inode_hash(root);
3180aa66619SPhillip Lougher 
3190aa66619SPhillip Lougher 	sb->s_root = d_alloc_root(root);
3200aa66619SPhillip Lougher 	if (sb->s_root == NULL) {
3210aa66619SPhillip Lougher 		ERROR("Root inode create failed\n");
3220aa66619SPhillip Lougher 		err = -ENOMEM;
3230aa66619SPhillip Lougher 		iput(root);
3240aa66619SPhillip Lougher 		goto failed_mount;
3250aa66619SPhillip Lougher 	}
3260aa66619SPhillip Lougher 
3270aa66619SPhillip Lougher 	TRACE("Leaving squashfs_fill_super\n");
3280aa66619SPhillip Lougher 	kfree(sblk);
3290aa66619SPhillip Lougher 	return 0;
3300aa66619SPhillip Lougher 
3310aa66619SPhillip Lougher failed_mount:
3320aa66619SPhillip Lougher 	squashfs_cache_delete(msblk->block_cache);
3330aa66619SPhillip Lougher 	squashfs_cache_delete(msblk->fragment_cache);
3340aa66619SPhillip Lougher 	squashfs_cache_delete(msblk->read_page);
3354c0f0bb2SPhillip Lougher 	squashfs_decompressor_free(msblk, msblk->stream);
3360aa66619SPhillip Lougher 	kfree(msblk->inode_lookup_table);
3370aa66619SPhillip Lougher 	kfree(msblk->fragment_index);
3380aa66619SPhillip Lougher 	kfree(msblk->id_table);
3394b5397dcSPhillip Lougher 	kfree(msblk->xattr_id_table);
3400aa66619SPhillip Lougher 	kfree(sb->s_fs_info);
3410aa66619SPhillip Lougher 	sb->s_fs_info = NULL;
3420aa66619SPhillip Lougher 	kfree(sblk);
3430aa66619SPhillip Lougher 	return err;
3440aa66619SPhillip Lougher }
3450aa66619SPhillip Lougher 
3460aa66619SPhillip Lougher 
3470aa66619SPhillip Lougher static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
3480aa66619SPhillip Lougher {
3490aa66619SPhillip Lougher 	struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
3502fc7f562SColy Li 	u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
3510aa66619SPhillip Lougher 
3520aa66619SPhillip Lougher 	TRACE("Entered squashfs_statfs\n");
3530aa66619SPhillip Lougher 
3540aa66619SPhillip Lougher 	buf->f_type = SQUASHFS_MAGIC;
3550aa66619SPhillip Lougher 	buf->f_bsize = msblk->block_size;
3560aa66619SPhillip Lougher 	buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
3570aa66619SPhillip Lougher 	buf->f_bfree = buf->f_bavail = 0;
3580aa66619SPhillip Lougher 	buf->f_files = msblk->inodes;
3590aa66619SPhillip Lougher 	buf->f_ffree = 0;
3600aa66619SPhillip Lougher 	buf->f_namelen = SQUASHFS_NAME_LEN;
3612fc7f562SColy Li 	buf->f_fsid.val[0] = (u32)id;
3622fc7f562SColy Li 	buf->f_fsid.val[1] = (u32)(id >> 32);
3630aa66619SPhillip Lougher 
3640aa66619SPhillip Lougher 	return 0;
3650aa66619SPhillip Lougher }
3660aa66619SPhillip Lougher 
3670aa66619SPhillip Lougher 
3680aa66619SPhillip Lougher static int squashfs_remount(struct super_block *sb, int *flags, char *data)
3690aa66619SPhillip Lougher {
3700aa66619SPhillip Lougher 	*flags |= MS_RDONLY;
3710aa66619SPhillip Lougher 	return 0;
3720aa66619SPhillip Lougher }
3730aa66619SPhillip Lougher 
3740aa66619SPhillip Lougher 
3750aa66619SPhillip Lougher static void squashfs_put_super(struct super_block *sb)
3760aa66619SPhillip Lougher {
3770aa66619SPhillip Lougher 	if (sb->s_fs_info) {
3780aa66619SPhillip Lougher 		struct squashfs_sb_info *sbi = sb->s_fs_info;
3790aa66619SPhillip Lougher 		squashfs_cache_delete(sbi->block_cache);
3800aa66619SPhillip Lougher 		squashfs_cache_delete(sbi->fragment_cache);
3810aa66619SPhillip Lougher 		squashfs_cache_delete(sbi->read_page);
3824c0f0bb2SPhillip Lougher 		squashfs_decompressor_free(sbi, sbi->stream);
3830aa66619SPhillip Lougher 		kfree(sbi->id_table);
3840aa66619SPhillip Lougher 		kfree(sbi->fragment_index);
3850aa66619SPhillip Lougher 		kfree(sbi->meta_index);
386370ec3d1SPhillip Lougher 		kfree(sbi->inode_lookup_table);
3874b5397dcSPhillip Lougher 		kfree(sbi->xattr_id_table);
3880aa66619SPhillip Lougher 		kfree(sb->s_fs_info);
3890aa66619SPhillip Lougher 		sb->s_fs_info = NULL;
3900aa66619SPhillip Lougher 	}
3910aa66619SPhillip Lougher }
3920aa66619SPhillip Lougher 
3930aa66619SPhillip Lougher 
394003a3194SPhillip Lougher static struct dentry *squashfs_mount(struct file_system_type *fs_type,
395003a3194SPhillip Lougher 				int flags, const char *dev_name, void *data)
3960aa66619SPhillip Lougher {
397152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, squashfs_fill_super);
3980aa66619SPhillip Lougher }
3990aa66619SPhillip Lougher 
4000aa66619SPhillip Lougher 
4010aa66619SPhillip Lougher static struct kmem_cache *squashfs_inode_cachep;
4020aa66619SPhillip Lougher 
4030aa66619SPhillip Lougher 
4040aa66619SPhillip Lougher static void init_once(void *foo)
4050aa66619SPhillip Lougher {
4060aa66619SPhillip Lougher 	struct squashfs_inode_info *ei = foo;
4070aa66619SPhillip Lougher 
4080aa66619SPhillip Lougher 	inode_init_once(&ei->vfs_inode);
4090aa66619SPhillip Lougher }
4100aa66619SPhillip Lougher 
4110aa66619SPhillip Lougher 
4120aa66619SPhillip Lougher static int __init init_inodecache(void)
4130aa66619SPhillip Lougher {
4140aa66619SPhillip Lougher 	squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
4150aa66619SPhillip Lougher 		sizeof(struct squashfs_inode_info), 0,
4160aa66619SPhillip Lougher 		SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, init_once);
4170aa66619SPhillip Lougher 
4180aa66619SPhillip Lougher 	return squashfs_inode_cachep ? 0 : -ENOMEM;
4190aa66619SPhillip Lougher }
4200aa66619SPhillip Lougher 
4210aa66619SPhillip Lougher 
4220aa66619SPhillip Lougher static void destroy_inodecache(void)
4230aa66619SPhillip Lougher {
4240aa66619SPhillip Lougher 	kmem_cache_destroy(squashfs_inode_cachep);
4250aa66619SPhillip Lougher }
4260aa66619SPhillip Lougher 
4270aa66619SPhillip Lougher 
4280aa66619SPhillip Lougher static int __init init_squashfs_fs(void)
4290aa66619SPhillip Lougher {
4300aa66619SPhillip Lougher 	int err = init_inodecache();
4310aa66619SPhillip Lougher 
4320aa66619SPhillip Lougher 	if (err)
4330aa66619SPhillip Lougher 		return err;
4340aa66619SPhillip Lougher 
4350aa66619SPhillip Lougher 	err = register_filesystem(&squashfs_fs_type);
4360aa66619SPhillip Lougher 	if (err) {
4370aa66619SPhillip Lougher 		destroy_inodecache();
4380aa66619SPhillip Lougher 		return err;
4390aa66619SPhillip Lougher 	}
4400aa66619SPhillip Lougher 
441118e1ef6SPhillip Lougher 	printk(KERN_INFO "squashfs: version 4.0 (2009/01/31) "
4420aa66619SPhillip Lougher 		"Phillip Lougher\n");
4430aa66619SPhillip Lougher 
4440aa66619SPhillip Lougher 	return 0;
4450aa66619SPhillip Lougher }
4460aa66619SPhillip Lougher 
4470aa66619SPhillip Lougher 
4480aa66619SPhillip Lougher static void __exit exit_squashfs_fs(void)
4490aa66619SPhillip Lougher {
4500aa66619SPhillip Lougher 	unregister_filesystem(&squashfs_fs_type);
4510aa66619SPhillip Lougher 	destroy_inodecache();
4520aa66619SPhillip Lougher }
4530aa66619SPhillip Lougher 
4540aa66619SPhillip Lougher 
4550aa66619SPhillip Lougher static struct inode *squashfs_alloc_inode(struct super_block *sb)
4560aa66619SPhillip Lougher {
4570aa66619SPhillip Lougher 	struct squashfs_inode_info *ei =
4580aa66619SPhillip Lougher 		kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
4590aa66619SPhillip Lougher 
4600aa66619SPhillip Lougher 	return ei ? &ei->vfs_inode : NULL;
4610aa66619SPhillip Lougher }
4620aa66619SPhillip Lougher 
4630aa66619SPhillip Lougher 
464fa0d7e3dSNick Piggin static void squashfs_i_callback(struct rcu_head *head)
465fa0d7e3dSNick Piggin {
466fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
467fa0d7e3dSNick Piggin 	INIT_LIST_HEAD(&inode->i_dentry);
468fa0d7e3dSNick Piggin 	kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
469fa0d7e3dSNick Piggin }
470fa0d7e3dSNick Piggin 
4710aa66619SPhillip Lougher static void squashfs_destroy_inode(struct inode *inode)
4720aa66619SPhillip Lougher {
473fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, squashfs_i_callback);
4740aa66619SPhillip Lougher }
4750aa66619SPhillip Lougher 
4760aa66619SPhillip Lougher 
4770aa66619SPhillip Lougher static struct file_system_type squashfs_fs_type = {
4780aa66619SPhillip Lougher 	.owner = THIS_MODULE,
4790aa66619SPhillip Lougher 	.name = "squashfs",
480152a0836SAl Viro 	.mount = squashfs_mount,
4810aa66619SPhillip Lougher 	.kill_sb = kill_block_super,
4820aa66619SPhillip Lougher 	.fs_flags = FS_REQUIRES_DEV
4830aa66619SPhillip Lougher };
4840aa66619SPhillip Lougher 
485b87221deSAlexey Dobriyan static const struct super_operations squashfs_super_ops = {
4860aa66619SPhillip Lougher 	.alloc_inode = squashfs_alloc_inode,
4870aa66619SPhillip Lougher 	.destroy_inode = squashfs_destroy_inode,
4880aa66619SPhillip Lougher 	.statfs = squashfs_statfs,
4890aa66619SPhillip Lougher 	.put_super = squashfs_put_super,
4900aa66619SPhillip Lougher 	.remount_fs = squashfs_remount
4910aa66619SPhillip Lougher };
4920aa66619SPhillip Lougher 
4930aa66619SPhillip Lougher module_init(init_squashfs_fs);
4940aa66619SPhillip Lougher module_exit(exit_squashfs_fs);
4950aa66619SPhillip Lougher MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
496d7f2ff67SPhillip Lougher MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
4970aa66619SPhillip Lougher MODULE_LICENSE("GPL");
498