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 50aa66619SPhillip Lougher * Phillip Lougher <phillip@lougher.demon.co.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> 33405f5571SAlexey Dobriyan #include <linux/smp_lock.h> 340aa66619SPhillip Lougher #include <linux/mutex.h> 350aa66619SPhillip Lougher #include <linux/pagemap.h> 360aa66619SPhillip Lougher #include <linux/init.h> 370aa66619SPhillip Lougher #include <linux/module.h> 381bcbf313SQinghuang Feng #include <linux/magic.h> 394b5397dcSPhillip Lougher #include <linux/xattr.h> 400aa66619SPhillip Lougher 410aa66619SPhillip Lougher #include "squashfs_fs.h" 420aa66619SPhillip Lougher #include "squashfs_fs_sb.h" 430aa66619SPhillip Lougher #include "squashfs_fs_i.h" 440aa66619SPhillip Lougher #include "squashfs.h" 454c0f0bb2SPhillip Lougher #include "decompressor.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; 864b5397dcSPhillip Lougher u64 lookup_table_start, xattr_id_table_start; 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 sblk = kzalloc(sizeof(*sblk), GFP_KERNEL); 990aa66619SPhillip Lougher if (sblk == NULL) { 1000aa66619SPhillip Lougher ERROR("Failed to allocate squashfs_super_block\n"); 1010aa66619SPhillip Lougher goto failure; 1020aa66619SPhillip Lougher } 1030aa66619SPhillip Lougher 1040aa66619SPhillip Lougher msblk->devblksize = sb_min_blocksize(sb, BLOCK_SIZE); 1050aa66619SPhillip Lougher msblk->devblksize_log2 = ffz(~msblk->devblksize); 1060aa66619SPhillip Lougher 1070aa66619SPhillip Lougher mutex_init(&msblk->read_data_mutex); 1080aa66619SPhillip Lougher mutex_init(&msblk->meta_index_mutex); 1090aa66619SPhillip Lougher 1100aa66619SPhillip Lougher /* 1110aa66619SPhillip Lougher * msblk->bytes_used is checked in squashfs_read_table to ensure reads 1120aa66619SPhillip Lougher * are not beyond filesystem end. But as we're using 1130aa66619SPhillip Lougher * squashfs_read_table here to read the superblock (including the value 1140aa66619SPhillip Lougher * of bytes_used) we need to set it to an initial sensible dummy value 1150aa66619SPhillip Lougher */ 1160aa66619SPhillip Lougher msblk->bytes_used = sizeof(*sblk); 1170aa66619SPhillip Lougher err = squashfs_read_table(sb, sblk, SQUASHFS_START, sizeof(*sblk)); 1180aa66619SPhillip Lougher 1190aa66619SPhillip Lougher if (err < 0) { 1200aa66619SPhillip Lougher ERROR("unable to read squashfs_super_block\n"); 1210aa66619SPhillip Lougher goto failed_mount; 1220aa66619SPhillip Lougher } 1230aa66619SPhillip Lougher 1244c0f0bb2SPhillip Lougher err = -EINVAL; 1254c0f0bb2SPhillip Lougher 1260aa66619SPhillip Lougher /* Check it is a SQUASHFS superblock */ 1270aa66619SPhillip Lougher sb->s_magic = le32_to_cpu(sblk->s_magic); 1280aa66619SPhillip Lougher if (sb->s_magic != SQUASHFS_MAGIC) { 1290aa66619SPhillip Lougher if (!silent) 1300aa66619SPhillip Lougher ERROR("Can't find a SQUASHFS superblock on %s\n", 1310aa66619SPhillip Lougher bdevname(sb->s_bdev, b)); 1320aa66619SPhillip Lougher goto failed_mount; 1330aa66619SPhillip Lougher } 1340aa66619SPhillip Lougher 1354c0f0bb2SPhillip Lougher /* Check the MAJOR & MINOR versions and lookup compression type */ 1364c0f0bb2SPhillip Lougher msblk->decompressor = supported_squashfs_filesystem( 1374c0f0bb2SPhillip Lougher le16_to_cpu(sblk->s_major), 1380aa66619SPhillip Lougher le16_to_cpu(sblk->s_minor), 1390aa66619SPhillip Lougher le16_to_cpu(sblk->compression)); 1404c0f0bb2SPhillip Lougher if (msblk->decompressor == NULL) 1410aa66619SPhillip Lougher goto failed_mount; 1420aa66619SPhillip Lougher 1430aa66619SPhillip Lougher /* 1440aa66619SPhillip Lougher * Check if there's xattrs in the filesystem. These are not 1450aa66619SPhillip Lougher * supported in this version, so warn that they will be ignored. 1460aa66619SPhillip Lougher */ 1474b5397dcSPhillip Lougher if (le64_to_cpu(sblk->xattr_id_table_start) != SQUASHFS_INVALID_BLK) 1480aa66619SPhillip Lougher ERROR("Xattrs in filesystem, these will be ignored\n"); 1490aa66619SPhillip Lougher 1500aa66619SPhillip Lougher /* Check the filesystem does not extend beyond the end of the 1510aa66619SPhillip Lougher block device */ 1520aa66619SPhillip Lougher msblk->bytes_used = le64_to_cpu(sblk->bytes_used); 1530aa66619SPhillip Lougher if (msblk->bytes_used < 0 || msblk->bytes_used > 1540aa66619SPhillip Lougher i_size_read(sb->s_bdev->bd_inode)) 1550aa66619SPhillip Lougher goto failed_mount; 1560aa66619SPhillip Lougher 1570aa66619SPhillip Lougher /* Check block size for sanity */ 1580aa66619SPhillip Lougher msblk->block_size = le32_to_cpu(sblk->block_size); 1590aa66619SPhillip Lougher if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE) 1600aa66619SPhillip Lougher goto failed_mount; 1610aa66619SPhillip Lougher 162fffb47b8SPhillip Lougher /* 163fffb47b8SPhillip Lougher * Check the system page size is not larger than the filesystem 164fffb47b8SPhillip Lougher * block size (by default 128K). This is currently not supported. 165fffb47b8SPhillip Lougher */ 166fffb47b8SPhillip Lougher if (PAGE_CACHE_SIZE > msblk->block_size) { 167fffb47b8SPhillip Lougher ERROR("Page size > filesystem block size (%d). This is " 168fffb47b8SPhillip Lougher "currently not supported!\n", msblk->block_size); 169fffb47b8SPhillip Lougher goto failed_mount; 170fffb47b8SPhillip Lougher } 171fffb47b8SPhillip Lougher 1720aa66619SPhillip Lougher msblk->block_log = le16_to_cpu(sblk->block_log); 1730aa66619SPhillip Lougher if (msblk->block_log > SQUASHFS_FILE_MAX_LOG) 1740aa66619SPhillip Lougher goto failed_mount; 1750aa66619SPhillip Lougher 1760aa66619SPhillip Lougher /* Check the root inode for sanity */ 1770aa66619SPhillip Lougher root_inode = le64_to_cpu(sblk->root_inode); 1780aa66619SPhillip Lougher if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE) 1790aa66619SPhillip Lougher goto failed_mount; 1800aa66619SPhillip Lougher 1810aa66619SPhillip Lougher msblk->inode_table = le64_to_cpu(sblk->inode_table_start); 1820aa66619SPhillip Lougher msblk->directory_table = le64_to_cpu(sblk->directory_table_start); 1830aa66619SPhillip Lougher msblk->inodes = le32_to_cpu(sblk->inodes); 1840aa66619SPhillip Lougher flags = le16_to_cpu(sblk->flags); 1850aa66619SPhillip Lougher 1860aa66619SPhillip Lougher TRACE("Found valid superblock on %s\n", bdevname(sb->s_bdev, b)); 1870aa66619SPhillip Lougher TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags) 1880aa66619SPhillip Lougher ? "un" : ""); 1890aa66619SPhillip Lougher TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags) 1900aa66619SPhillip Lougher ? "un" : ""); 1910aa66619SPhillip Lougher TRACE("Filesystem size %lld bytes\n", msblk->bytes_used); 1920aa66619SPhillip Lougher TRACE("Block size %d\n", msblk->block_size); 1930aa66619SPhillip Lougher TRACE("Number of inodes %d\n", msblk->inodes); 1940aa66619SPhillip Lougher TRACE("Number of fragments %d\n", le32_to_cpu(sblk->fragments)); 1950aa66619SPhillip Lougher TRACE("Number of ids %d\n", le16_to_cpu(sblk->no_ids)); 1960aa66619SPhillip Lougher TRACE("sblk->inode_table_start %llx\n", msblk->inode_table); 1970aa66619SPhillip Lougher TRACE("sblk->directory_table_start %llx\n", msblk->directory_table); 1980aa66619SPhillip Lougher TRACE("sblk->fragment_table_start %llx\n", 1990aa66619SPhillip Lougher (u64) le64_to_cpu(sblk->fragment_table_start)); 2000aa66619SPhillip Lougher TRACE("sblk->id_table_start %llx\n", 2010aa66619SPhillip Lougher (u64) le64_to_cpu(sblk->id_table_start)); 2020aa66619SPhillip Lougher 2030aa66619SPhillip Lougher sb->s_maxbytes = MAX_LFS_FILESIZE; 2040aa66619SPhillip Lougher sb->s_flags |= MS_RDONLY; 2050aa66619SPhillip Lougher sb->s_op = &squashfs_super_ops; 2060aa66619SPhillip Lougher 2070aa66619SPhillip Lougher err = -ENOMEM; 2080aa66619SPhillip Lougher 2094c0f0bb2SPhillip Lougher msblk->stream = squashfs_decompressor_init(msblk); 2104c0f0bb2SPhillip Lougher if (msblk->stream == NULL) 2114c0f0bb2SPhillip Lougher goto failed_mount; 2124c0f0bb2SPhillip Lougher 2130aa66619SPhillip Lougher msblk->block_cache = squashfs_cache_init("metadata", 2140aa66619SPhillip Lougher SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE); 2150aa66619SPhillip Lougher if (msblk->block_cache == NULL) 2160aa66619SPhillip Lougher goto failed_mount; 2170aa66619SPhillip Lougher 2180aa66619SPhillip Lougher /* Allocate read_page block */ 2190aa66619SPhillip Lougher msblk->read_page = squashfs_cache_init("data", 1, msblk->block_size); 2200aa66619SPhillip Lougher if (msblk->read_page == NULL) { 2210aa66619SPhillip Lougher ERROR("Failed to allocate read_page block\n"); 2220aa66619SPhillip Lougher goto failed_mount; 2230aa66619SPhillip Lougher } 2240aa66619SPhillip Lougher 2250aa66619SPhillip Lougher /* Allocate and read id index table */ 2260aa66619SPhillip Lougher msblk->id_table = squashfs_read_id_index_table(sb, 2270aa66619SPhillip Lougher le64_to_cpu(sblk->id_table_start), le16_to_cpu(sblk->no_ids)); 2280aa66619SPhillip Lougher if (IS_ERR(msblk->id_table)) { 2290aa66619SPhillip Lougher err = PTR_ERR(msblk->id_table); 2300aa66619SPhillip Lougher msblk->id_table = NULL; 2310aa66619SPhillip Lougher goto failed_mount; 2320aa66619SPhillip Lougher } 2330aa66619SPhillip Lougher 2340aa66619SPhillip Lougher fragments = le32_to_cpu(sblk->fragments); 2350aa66619SPhillip Lougher if (fragments == 0) 2360aa66619SPhillip Lougher goto allocate_lookup_table; 2370aa66619SPhillip Lougher 2380aa66619SPhillip Lougher msblk->fragment_cache = squashfs_cache_init("fragment", 2390aa66619SPhillip Lougher SQUASHFS_CACHED_FRAGMENTS, msblk->block_size); 2400aa66619SPhillip Lougher if (msblk->fragment_cache == NULL) { 2410aa66619SPhillip Lougher err = -ENOMEM; 2420aa66619SPhillip Lougher goto failed_mount; 2430aa66619SPhillip Lougher } 2440aa66619SPhillip Lougher 2450aa66619SPhillip Lougher /* Allocate and read fragment index table */ 2460aa66619SPhillip Lougher msblk->fragment_index = squashfs_read_fragment_index_table(sb, 2470aa66619SPhillip Lougher le64_to_cpu(sblk->fragment_table_start), fragments); 2480aa66619SPhillip Lougher if (IS_ERR(msblk->fragment_index)) { 2490aa66619SPhillip Lougher err = PTR_ERR(msblk->fragment_index); 2500aa66619SPhillip Lougher msblk->fragment_index = NULL; 2510aa66619SPhillip Lougher goto failed_mount; 2520aa66619SPhillip Lougher } 2530aa66619SPhillip Lougher 2540aa66619SPhillip Lougher allocate_lookup_table: 2550aa66619SPhillip Lougher lookup_table_start = le64_to_cpu(sblk->lookup_table_start); 2560aa66619SPhillip Lougher if (lookup_table_start == SQUASHFS_INVALID_BLK) 2574b5397dcSPhillip Lougher goto allocate_xattr_table; 2580aa66619SPhillip Lougher 2590aa66619SPhillip Lougher /* Allocate and read inode lookup table */ 2600aa66619SPhillip Lougher msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb, 2610aa66619SPhillip Lougher lookup_table_start, msblk->inodes); 2620aa66619SPhillip Lougher if (IS_ERR(msblk->inode_lookup_table)) { 2630aa66619SPhillip Lougher err = PTR_ERR(msblk->inode_lookup_table); 2640aa66619SPhillip Lougher msblk->inode_lookup_table = NULL; 2650aa66619SPhillip Lougher goto failed_mount; 2660aa66619SPhillip Lougher } 2670aa66619SPhillip Lougher 2680aa66619SPhillip Lougher sb->s_export_op = &squashfs_export_ops; 2690aa66619SPhillip Lougher 2704b5397dcSPhillip Lougher allocate_xattr_table: 2714b5397dcSPhillip Lougher xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start); 2724b5397dcSPhillip Lougher if (xattr_id_table_start == SQUASHFS_INVALID_BLK) 2734b5397dcSPhillip Lougher goto allocate_root; 2744b5397dcSPhillip Lougher 2754b5397dcSPhillip Lougher /* Allocate and read xattr id lookup table */ 2764b5397dcSPhillip Lougher msblk->xattr_id_table = squashfs_read_xattr_id_table(sb, 2774b5397dcSPhillip Lougher xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids); 2784b5397dcSPhillip Lougher if (IS_ERR(msblk->xattr_id_table)) { 2794b5397dcSPhillip Lougher err = PTR_ERR(msblk->xattr_id_table); 2804b5397dcSPhillip Lougher msblk->xattr_id_table = NULL; 2814b5397dcSPhillip Lougher goto failed_mount; 2824b5397dcSPhillip Lougher } 2830aa66619SPhillip Lougher allocate_root: 2840aa66619SPhillip Lougher root = new_inode(sb); 2850aa66619SPhillip Lougher if (!root) { 2860aa66619SPhillip Lougher err = -ENOMEM; 2870aa66619SPhillip Lougher goto failed_mount; 2880aa66619SPhillip Lougher } 2890aa66619SPhillip Lougher 2900aa66619SPhillip Lougher err = squashfs_read_inode(root, root_inode); 2910aa66619SPhillip Lougher if (err) { 2921cb08e97SPhillip Lougher make_bad_inode(root); 2931cb08e97SPhillip Lougher iput(root); 2940aa66619SPhillip Lougher goto failed_mount; 2950aa66619SPhillip Lougher } 2960aa66619SPhillip Lougher insert_inode_hash(root); 2970aa66619SPhillip Lougher 2980aa66619SPhillip Lougher sb->s_root = d_alloc_root(root); 2990aa66619SPhillip Lougher if (sb->s_root == NULL) { 3000aa66619SPhillip Lougher ERROR("Root inode create failed\n"); 3010aa66619SPhillip Lougher err = -ENOMEM; 3020aa66619SPhillip Lougher iput(root); 3030aa66619SPhillip Lougher goto failed_mount; 3040aa66619SPhillip Lougher } 3050aa66619SPhillip Lougher 3060aa66619SPhillip Lougher TRACE("Leaving squashfs_fill_super\n"); 3070aa66619SPhillip Lougher kfree(sblk); 3080aa66619SPhillip Lougher return 0; 3090aa66619SPhillip Lougher 3100aa66619SPhillip Lougher failed_mount: 3110aa66619SPhillip Lougher squashfs_cache_delete(msblk->block_cache); 3120aa66619SPhillip Lougher squashfs_cache_delete(msblk->fragment_cache); 3130aa66619SPhillip Lougher squashfs_cache_delete(msblk->read_page); 3144c0f0bb2SPhillip Lougher squashfs_decompressor_free(msblk, msblk->stream); 3150aa66619SPhillip Lougher kfree(msblk->inode_lookup_table); 3160aa66619SPhillip Lougher kfree(msblk->fragment_index); 3170aa66619SPhillip Lougher kfree(msblk->id_table); 3184b5397dcSPhillip Lougher kfree(msblk->xattr_id_table); 3190aa66619SPhillip Lougher kfree(sb->s_fs_info); 3200aa66619SPhillip Lougher sb->s_fs_info = NULL; 3210aa66619SPhillip Lougher kfree(sblk); 3220aa66619SPhillip Lougher return err; 3230aa66619SPhillip Lougher 3240aa66619SPhillip Lougher failure: 3250aa66619SPhillip Lougher kfree(sb->s_fs_info); 3260aa66619SPhillip Lougher sb->s_fs_info = NULL; 3270aa66619SPhillip Lougher return -ENOMEM; 3280aa66619SPhillip Lougher } 3290aa66619SPhillip Lougher 3300aa66619SPhillip Lougher 3310aa66619SPhillip Lougher static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf) 3320aa66619SPhillip Lougher { 3330aa66619SPhillip Lougher struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info; 3342fc7f562SColy Li u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev); 3350aa66619SPhillip Lougher 3360aa66619SPhillip Lougher TRACE("Entered squashfs_statfs\n"); 3370aa66619SPhillip Lougher 3380aa66619SPhillip Lougher buf->f_type = SQUASHFS_MAGIC; 3390aa66619SPhillip Lougher buf->f_bsize = msblk->block_size; 3400aa66619SPhillip Lougher buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1; 3410aa66619SPhillip Lougher buf->f_bfree = buf->f_bavail = 0; 3420aa66619SPhillip Lougher buf->f_files = msblk->inodes; 3430aa66619SPhillip Lougher buf->f_ffree = 0; 3440aa66619SPhillip Lougher buf->f_namelen = SQUASHFS_NAME_LEN; 3452fc7f562SColy Li buf->f_fsid.val[0] = (u32)id; 3462fc7f562SColy Li buf->f_fsid.val[1] = (u32)(id >> 32); 3470aa66619SPhillip Lougher 3480aa66619SPhillip Lougher return 0; 3490aa66619SPhillip Lougher } 3500aa66619SPhillip Lougher 3510aa66619SPhillip Lougher 3520aa66619SPhillip Lougher static int squashfs_remount(struct super_block *sb, int *flags, char *data) 3530aa66619SPhillip Lougher { 3540aa66619SPhillip Lougher *flags |= MS_RDONLY; 3550aa66619SPhillip Lougher return 0; 3560aa66619SPhillip Lougher } 3570aa66619SPhillip Lougher 3580aa66619SPhillip Lougher 3590aa66619SPhillip Lougher static void squashfs_put_super(struct super_block *sb) 3600aa66619SPhillip Lougher { 3616cfd0148SChristoph Hellwig lock_kernel(); 3626cfd0148SChristoph Hellwig 3630aa66619SPhillip Lougher if (sb->s_fs_info) { 3640aa66619SPhillip Lougher struct squashfs_sb_info *sbi = sb->s_fs_info; 3650aa66619SPhillip Lougher squashfs_cache_delete(sbi->block_cache); 3660aa66619SPhillip Lougher squashfs_cache_delete(sbi->fragment_cache); 3670aa66619SPhillip Lougher squashfs_cache_delete(sbi->read_page); 3684c0f0bb2SPhillip Lougher squashfs_decompressor_free(sbi, sbi->stream); 3690aa66619SPhillip Lougher kfree(sbi->id_table); 3700aa66619SPhillip Lougher kfree(sbi->fragment_index); 3710aa66619SPhillip Lougher kfree(sbi->meta_index); 372370ec3d1SPhillip Lougher kfree(sbi->inode_lookup_table); 3734b5397dcSPhillip Lougher kfree(sbi->xattr_id_table); 3740aa66619SPhillip Lougher kfree(sb->s_fs_info); 3750aa66619SPhillip Lougher sb->s_fs_info = NULL; 3760aa66619SPhillip Lougher } 3776cfd0148SChristoph Hellwig 3786cfd0148SChristoph Hellwig unlock_kernel(); 3790aa66619SPhillip Lougher } 3800aa66619SPhillip Lougher 3810aa66619SPhillip Lougher 3820aa66619SPhillip Lougher static int squashfs_get_sb(struct file_system_type *fs_type, int flags, 3830aa66619SPhillip Lougher const char *dev_name, void *data, 3840aa66619SPhillip Lougher struct vfsmount *mnt) 3850aa66619SPhillip Lougher { 3860aa66619SPhillip Lougher return get_sb_bdev(fs_type, flags, dev_name, data, squashfs_fill_super, 3870aa66619SPhillip Lougher mnt); 3880aa66619SPhillip Lougher } 3890aa66619SPhillip Lougher 3900aa66619SPhillip Lougher 3910aa66619SPhillip Lougher static struct kmem_cache *squashfs_inode_cachep; 3920aa66619SPhillip Lougher 3930aa66619SPhillip Lougher 3940aa66619SPhillip Lougher static void init_once(void *foo) 3950aa66619SPhillip Lougher { 3960aa66619SPhillip Lougher struct squashfs_inode_info *ei = foo; 3970aa66619SPhillip Lougher 3980aa66619SPhillip Lougher inode_init_once(&ei->vfs_inode); 3990aa66619SPhillip Lougher } 4000aa66619SPhillip Lougher 4010aa66619SPhillip Lougher 4020aa66619SPhillip Lougher static int __init init_inodecache(void) 4030aa66619SPhillip Lougher { 4040aa66619SPhillip Lougher squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache", 4050aa66619SPhillip Lougher sizeof(struct squashfs_inode_info), 0, 4060aa66619SPhillip Lougher SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, init_once); 4070aa66619SPhillip Lougher 4080aa66619SPhillip Lougher return squashfs_inode_cachep ? 0 : -ENOMEM; 4090aa66619SPhillip Lougher } 4100aa66619SPhillip Lougher 4110aa66619SPhillip Lougher 4120aa66619SPhillip Lougher static void destroy_inodecache(void) 4130aa66619SPhillip Lougher { 4140aa66619SPhillip Lougher kmem_cache_destroy(squashfs_inode_cachep); 4150aa66619SPhillip Lougher } 4160aa66619SPhillip Lougher 4170aa66619SPhillip Lougher 4180aa66619SPhillip Lougher static int __init init_squashfs_fs(void) 4190aa66619SPhillip Lougher { 4200aa66619SPhillip Lougher int err = init_inodecache(); 4210aa66619SPhillip Lougher 4220aa66619SPhillip Lougher if (err) 4230aa66619SPhillip Lougher return err; 4240aa66619SPhillip Lougher 4250aa66619SPhillip Lougher err = register_filesystem(&squashfs_fs_type); 4260aa66619SPhillip Lougher if (err) { 4270aa66619SPhillip Lougher destroy_inodecache(); 4280aa66619SPhillip Lougher return err; 4290aa66619SPhillip Lougher } 4300aa66619SPhillip Lougher 431118e1ef6SPhillip Lougher printk(KERN_INFO "squashfs: version 4.0 (2009/01/31) " 4320aa66619SPhillip Lougher "Phillip Lougher\n"); 4330aa66619SPhillip Lougher 4340aa66619SPhillip Lougher return 0; 4350aa66619SPhillip Lougher } 4360aa66619SPhillip Lougher 4370aa66619SPhillip Lougher 4380aa66619SPhillip Lougher static void __exit exit_squashfs_fs(void) 4390aa66619SPhillip Lougher { 4400aa66619SPhillip Lougher unregister_filesystem(&squashfs_fs_type); 4410aa66619SPhillip Lougher destroy_inodecache(); 4420aa66619SPhillip Lougher } 4430aa66619SPhillip Lougher 4440aa66619SPhillip Lougher 4450aa66619SPhillip Lougher static struct inode *squashfs_alloc_inode(struct super_block *sb) 4460aa66619SPhillip Lougher { 4470aa66619SPhillip Lougher struct squashfs_inode_info *ei = 4480aa66619SPhillip Lougher kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL); 4490aa66619SPhillip Lougher 4500aa66619SPhillip Lougher return ei ? &ei->vfs_inode : NULL; 4510aa66619SPhillip Lougher } 4520aa66619SPhillip Lougher 4530aa66619SPhillip Lougher 4540aa66619SPhillip Lougher static void squashfs_destroy_inode(struct inode *inode) 4550aa66619SPhillip Lougher { 4560aa66619SPhillip Lougher kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode)); 4570aa66619SPhillip Lougher } 4580aa66619SPhillip Lougher 4590aa66619SPhillip Lougher 4600aa66619SPhillip Lougher static struct file_system_type squashfs_fs_type = { 4610aa66619SPhillip Lougher .owner = THIS_MODULE, 4620aa66619SPhillip Lougher .name = "squashfs", 4630aa66619SPhillip Lougher .get_sb = squashfs_get_sb, 4640aa66619SPhillip Lougher .kill_sb = kill_block_super, 4650aa66619SPhillip Lougher .fs_flags = FS_REQUIRES_DEV 4660aa66619SPhillip Lougher }; 4670aa66619SPhillip Lougher 468b87221deSAlexey Dobriyan static const struct super_operations squashfs_super_ops = { 4690aa66619SPhillip Lougher .alloc_inode = squashfs_alloc_inode, 4700aa66619SPhillip Lougher .destroy_inode = squashfs_destroy_inode, 4710aa66619SPhillip Lougher .statfs = squashfs_statfs, 4720aa66619SPhillip Lougher .put_super = squashfs_put_super, 4730aa66619SPhillip Lougher .remount_fs = squashfs_remount 4740aa66619SPhillip Lougher }; 4750aa66619SPhillip Lougher 4760aa66619SPhillip Lougher module_init(init_squashfs_fs); 4770aa66619SPhillip Lougher module_exit(exit_squashfs_fs); 4780aa66619SPhillip Lougher MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem"); 4790aa66619SPhillip Lougher MODULE_AUTHOR("Phillip Lougher <phillip@lougher.demon.co.uk>"); 4800aa66619SPhillip Lougher MODULE_LICENSE("GPL"); 481