super.c revision 5f8bb3bfd43e3795e409e9b87b50f21442ebd32e
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/vmalloc.h> 34#include <linux/mutex.h> 35#include <linux/pagemap.h> 36#include <linux/init.h> 37#include <linux/module.h> 38#include <linux/zlib.h> 39#include <linux/squashfs_fs.h> 40#include <linux/squashfs_fs_sb.h> 41#include <linux/squashfs_fs_i.h> 42 43#include "squashfs.h" 44 45static struct file_system_type squashfs_fs_type; 46static struct super_operations squashfs_super_ops; 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 0; 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 0; 59 } 60 61 if (comp != ZLIB_COMPRESSION) 62 return 0; 63 64 return 1; 65} 66 67 68static int squashfs_fill_super(struct super_block *s, 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 long long lookup_table_start; 78 int res; 79 80 TRACE("Entered squashfs_fill_superblock\n"); 81 82 s->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL); 83 if (s->s_fs_info == NULL) { 84 ERROR("Failed to allocate squashfs_sb_info\n"); 85 goto failure2; 86 } 87 msblk = s->s_fs_info; 88 89 msblk->stream.workspace = vmalloc(zlib_inflate_workspacesize()); 90 if (msblk->stream.workspace == NULL) { 91 ERROR("Failed to allocate zlib workspace\n"); 92 goto failure; 93 } 94 95 sblk = kzalloc(sizeof(*sblk), GFP_KERNEL); 96 if (sblk == NULL) { 97 ERROR("Failed to allocate squashfs_super_block\n"); 98 goto failure; 99 } 100 101 msblk->devblksize = sb_min_blocksize(s, BLOCK_SIZE); 102 msblk->devblksize_log2 = ffz(~msblk->devblksize); 103 104 mutex_init(&msblk->read_data_mutex); 105 mutex_init(&msblk->read_page_mutex); 106 mutex_init(&msblk->meta_index_mutex); 107 108 /* 109 * msblk->bytes_used is checked in squashfs_read_data to ensure reads 110 * are not beyond filesystem end. But as we're using squashfs_read_data 111 * here to read the superblock (including the value of 112 * bytes_used) we need to set it to an initial sensible dummy value 113 */ 114 msblk->bytes_used = sizeof(*sblk); 115 res = squashfs_read_data(s, sblk, SQUASHFS_START, sizeof(*sblk) | 116 SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, sizeof(*sblk)); 117 118 if (res == 0) { 119 ERROR("unable to read squashfs_super_block\n"); 120 goto failed_mount; 121 } 122 123 /* Check it is a SQUASHFS superblock */ 124 s->s_magic = le32_to_cpu(sblk->s_magic); 125 if (s->s_magic != SQUASHFS_MAGIC) { 126 if (!silent) 127 ERROR("Can't find a SQUASHFS superblock on %s\n", 128 bdevname(s->s_bdev, b)); 129 goto failed_mount; 130 } 131 132 /* Check the MAJOR & MINOR versions and compression type */ 133 if (!supported_squashfs_filesystem(le16_to_cpu(sblk->s_major), 134 le16_to_cpu(sblk->s_minor), 135 le16_to_cpu(sblk->compression))) 136 goto failed_mount; 137 138 /* Check the filesystem does not extend beyond the end of the 139 block device */ 140 msblk->bytes_used = le64_to_cpu(sblk->bytes_used); 141 if (msblk->bytes_used < 0 || msblk->bytes_used > 142 i_size_read(s->s_bdev->bd_inode)) 143 goto failed_mount; 144 145 /* Check block size for sanity */ 146 msblk->block_size = le32_to_cpu(sblk->block_size); 147 if (msblk->block_size > SQUASHFS_FILE_SIZE) 148 goto failed_mount; 149 150 msblk->block_log = le16_to_cpu(sblk->block_log); 151 if (msblk->block_log > SQUASHFS_FILE_LOG) 152 goto failed_mount; 153 154 /* Check the root inode for sanity */ 155 root_inode = le64_to_cpu(sblk->root_inode); 156 if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE) 157 goto failed_mount; 158 159 msblk->inode_table_start = le64_to_cpu(sblk->inode_table_start); 160 msblk->directory_table_start = le64_to_cpu(sblk->directory_table_start); 161 msblk->inodes = le32_to_cpu(sblk->inodes); 162 flags = le16_to_cpu(sblk->flags); 163 164 TRACE("Found valid superblock on %s\n", bdevname(s->s_bdev, b)); 165 TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags) 166 ? "un" : ""); 167 TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags) 168 ? "un" : ""); 169 TRACE("Filesystem size %lld bytes\n", msblk->bytes_used); 170 TRACE("Block size %d\n", msblk->block_size); 171 TRACE("Number of inodes %d\n", msblk->inodes); 172 TRACE("Number of fragments %d\n", le32_to_cpu(sblk->fragments)); 173 TRACE("Number of ids %d\n", le16_to_cpu(sblk->no_ids)); 174 TRACE("sblk->inode_table_start %llx\n", msblk->inode_table_start); 175 TRACE("sblk->directory_table_start %llx\n", 176 msblk->directory_table_start); 177 TRACE("sblk->fragment_table_start %llx\n", 178 le64_to_cpu(sblk->fragment_table_start)); 179 TRACE("sblk->id_table_start %llx\n", le64_to_cpu(sblk->id_table_start)); 180 181 s->s_maxbytes = MAX_LFS_FILESIZE; 182 s->s_flags |= MS_RDONLY; 183 s->s_op = &squashfs_super_ops; 184 185 msblk->block_cache = squashfs_cache_init("metadata", 186 SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE, 0); 187 if (msblk->block_cache == NULL) 188 goto failed_mount; 189 190 /* Allocate read_page block */ 191 msblk->read_page = vmalloc(msblk->block_size); 192 if (msblk->read_page == NULL) { 193 ERROR("Failed to allocate read_page block\n"); 194 goto failed_mount; 195 } 196 197 /* Allocate and read id index table */ 198 msblk->id_table = read_id_index_table(s, 199 le64_to_cpu(sblk->id_table_start), le16_to_cpu(sblk->no_ids)); 200 if (msblk->id_table == NULL) 201 goto failed_mount; 202 203 fragments = le32_to_cpu(sblk->fragments); 204 if (fragments == 0) 205 goto allocate_lookup_table; 206 207 msblk->fragment_cache = squashfs_cache_init("fragment", 208 SQUASHFS_CACHED_FRAGMENTS, msblk->block_size, 1); 209 if (msblk->fragment_cache == NULL) 210 goto failed_mount; 211 212 /* Allocate and read fragment index table */ 213 msblk->fragment_index = read_fragment_index_table(s, 214 le64_to_cpu(sblk->fragment_table_start), fragments); 215 if (msblk->fragment_index == NULL) 216 goto failed_mount; 217 218allocate_lookup_table: 219 lookup_table_start = le64_to_cpu(sblk->lookup_table_start); 220 if (lookup_table_start == SQUASHFS_INVALID_BLK) 221 goto allocate_root; 222 223 /* Allocate and read inode lookup table */ 224 msblk->inode_lookup_table = read_inode_lookup_table(s, 225 lookup_table_start, msblk->inodes); 226 if (msblk->inode_lookup_table == NULL) 227 goto failed_mount; 228 229 s->s_export_op = &squashfs_export_ops; 230 231allocate_root: 232 root = new_inode(s); 233 if (squashfs_read_inode(root, root_inode) == 0) 234 goto failed_mount; 235 insert_inode_hash(root); 236 237 s->s_root = d_alloc_root(root); 238 if (s->s_root == NULL) { 239 ERROR("Root inode create failed\n"); 240 iput(root); 241 goto failed_mount; 242 } 243 244 TRACE("Leaving squashfs_fill_super\n"); 245 kfree(sblk); 246 return 0; 247 248failed_mount: 249 squashfs_cache_delete(msblk->block_cache); 250 squashfs_cache_delete(msblk->fragment_cache); 251 kfree(msblk->inode_lookup_table); 252 kfree(msblk->fragment_index); 253 kfree(msblk->id_table); 254 vfree(msblk->read_page); 255 vfree(msblk->stream.workspace); 256 kfree(s->s_fs_info); 257 s->s_fs_info = NULL; 258 kfree(sblk); 259 return -EINVAL; 260 261failure: 262 vfree(msblk->stream.workspace); 263 kfree(s->s_fs_info); 264 s->s_fs_info = NULL; 265failure2: 266 return -ENOMEM; 267} 268 269 270static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf) 271{ 272 struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info; 273 274 TRACE("Entered squashfs_statfs\n"); 275 276 buf->f_type = SQUASHFS_MAGIC; 277 buf->f_bsize = msblk->block_size; 278 buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1; 279 buf->f_bfree = buf->f_bavail = 0; 280 buf->f_files = msblk->inodes; 281 buf->f_ffree = 0; 282 buf->f_namelen = SQUASHFS_NAME_LEN; 283 284 return 0; 285} 286 287 288static int squashfs_remount(struct super_block *s, int *flags, char *data) 289{ 290 *flags |= MS_RDONLY; 291 return 0; 292} 293 294 295static void squashfs_put_super(struct super_block *s) 296{ 297 if (s->s_fs_info) { 298 struct squashfs_sb_info *sbi = s->s_fs_info; 299 squashfs_cache_delete(sbi->block_cache); 300 squashfs_cache_delete(sbi->fragment_cache); 301 vfree(sbi->read_page); 302 kfree(sbi->id_table); 303 kfree(sbi->fragment_index); 304 kfree(sbi->meta_index); 305 vfree(sbi->stream.workspace); 306 kfree(s->s_fs_info); 307 s->s_fs_info = NULL; 308 } 309} 310 311 312static int squashfs_get_sb(struct file_system_type *fs_type, int flags, 313 const char *dev_name, void *data, 314 struct vfsmount *mnt) 315{ 316 return get_sb_bdev(fs_type, flags, dev_name, data, squashfs_fill_super, 317 mnt); 318} 319 320 321static struct kmem_cache *squashfs_inode_cachep; 322 323 324static void init_once(void *foo) 325{ 326 struct squashfs_inode_info *ei = foo; 327 328 inode_init_once(&ei->vfs_inode); 329} 330 331 332static int __init init_inodecache(void) 333{ 334 squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache", 335 sizeof(struct squashfs_inode_info), 0, 336 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, init_once); 337 338 return squashfs_inode_cachep ? 0 : -ENOMEM; 339} 340 341 342static void destroy_inodecache(void) 343{ 344 kmem_cache_destroy(squashfs_inode_cachep); 345} 346 347 348static int __init init_squashfs_fs(void) 349{ 350 int err = init_inodecache(); 351 352 if (err) 353 goto out; 354 355 err = register_filesystem(&squashfs_fs_type); 356 if (err) { 357 destroy_inodecache(); 358 goto out; 359 } 360 361 printk(KERN_INFO "squashfs: version 4.0 (2008/10/19) " 362 "Phillip Lougher\n"); 363 364out: 365 return err; 366} 367 368 369static void __exit exit_squashfs_fs(void) 370{ 371 unregister_filesystem(&squashfs_fs_type); 372 destroy_inodecache(); 373} 374 375 376static struct inode *squashfs_alloc_inode(struct super_block *sb) 377{ 378 struct squashfs_inode_info *ei = 379 kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL); 380 381 return ei ? &ei->vfs_inode : NULL; 382} 383 384 385static void squashfs_destroy_inode(struct inode *inode) 386{ 387 kmem_cache_free(squashfs_inode_cachep, SQUASHFS_I(inode)); 388} 389 390 391static struct file_system_type squashfs_fs_type = { 392 .owner = THIS_MODULE, 393 .name = "squashfs", 394 .get_sb = squashfs_get_sb, 395 .kill_sb = kill_block_super, 396 .fs_flags = FS_REQUIRES_DEV 397}; 398 399static struct super_operations squashfs_super_ops = { 400 .alloc_inode = squashfs_alloc_inode, 401 .destroy_inode = squashfs_destroy_inode, 402 .statfs = squashfs_statfs, 403 .put_super = squashfs_put_super, 404 .remount_fs = squashfs_remount 405}; 406 407module_init(init_squashfs_fs); 408module_exit(exit_squashfs_fs); 409MODULE_DESCRIPTION("squashfs 4.0-CVS, a compressed read-only filesystem"); 410MODULE_AUTHOR("Phillip Lougher <phillip@lougher.demon.co.uk>"); 411MODULE_LICENSE("GPL"); 412