super.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. * This file implements code to read the superblock, read and initialise
  25. * in-memory structures at mount time, and all the VFS glue code to register
  26. * the filesystem.
  27. */
  28. #include <linux/fs.h>
  29. #include <linux/vfs.h>
  30. #include <linux/slab.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/mutex.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/init.h>
  35. #include <linux/module.h>
  36. #include <linux/magic.h>
  37. #include "squashfs_fs.h"
  38. #include "squashfs_fs_sb.h"
  39. #include "squashfs_fs_i.h"
  40. #include "squashfs.h"
  41. #include "decompressor.h"
  42. static struct file_system_type squashfs_fs_type;
  43. static const struct super_operations squashfs_super_ops;
  44. static const struct squashfs_decompressor *supported_squashfs_filesystem(short
  45. major, short minor, short id)
  46. {
  47. const struct squashfs_decompressor *decompressor;
  48. if (major < SQUASHFS_MAJOR) {
  49. ERROR("Major/Minor mismatch, older Squashfs %d.%d "
  50. "filesystems are unsupported\n", major, minor);
  51. return NULL;
  52. } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
  53. ERROR("Major/Minor mismatch, trying to mount newer "
  54. "%d.%d filesystem\n", major, minor);
  55. ERROR("Please update your kernel\n");
  56. return NULL;
  57. }
  58. decompressor = squashfs_lookup_decompressor(id);
  59. if (!decompressor->supported) {
  60. ERROR("Filesystem uses \"%s\" compression. This is not "
  61. "supported\n", decompressor->name);
  62. return NULL;
  63. }
  64. return decompressor;
  65. }
  66. static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
  67. {
  68. struct squashfs_sb_info *msblk;
  69. struct squashfs_super_block *sblk = NULL;
  70. char b[BDEVNAME_SIZE];
  71. struct inode *root;
  72. long long root_inode;
  73. unsigned short flags;
  74. unsigned int fragments;
  75. u64 lookup_table_start;
  76. int err;
  77. TRACE("Entered squashfs_fill_superblock\n");
  78. sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
  79. if (sb->s_fs_info == NULL) {
  80. ERROR("Failed to allocate squashfs_sb_info\n");
  81. return -ENOMEM;
  82. }
  83. msblk = sb->s_fs_info;
  84. sblk = kzalloc(sizeof(*sblk), GFP_KERNEL);
  85. if (sblk == NULL) {
  86. ERROR("Failed to allocate squashfs_super_block\n");
  87. goto failure;
  88. }
  89. msblk->devblksize = sb_min_blocksize(sb, BLOCK_SIZE);
  90. msblk->devblksize_log2 = ffz(~msblk->devblksize);
  91. mutex_init(&msblk->read_data_mutex);
  92. mutex_init(&msblk->meta_index_mutex);
  93. /*
  94. * msblk->bytes_used is checked in squashfs_read_table to ensure reads
  95. * are not beyond filesystem end. But as we're using
  96. * squashfs_read_table here to read the superblock (including the value
  97. * of bytes_used) we need to set it to an initial sensible dummy value
  98. */
  99. msblk->bytes_used = sizeof(*sblk);
  100. err = squashfs_read_table(sb, sblk, SQUASHFS_START, sizeof(*sblk));
  101. if (err < 0) {
  102. ERROR("unable to read squashfs_super_block\n");
  103. goto failed_mount;
  104. }
  105. err = -EINVAL;
  106. /* Check it is a SQUASHFS superblock */
  107. sb->s_magic = le32_to_cpu(sblk->s_magic);
  108. if (sb->s_magic != SQUASHFS_MAGIC) {
  109. if (!silent)
  110. ERROR("Can't find a SQUASHFS superblock on %s\n",
  111. bdevname(sb->s_bdev, b));
  112. goto failed_mount;
  113. }
  114. /* Check the MAJOR & MINOR versions and lookup compression type */
  115. msblk->decompressor = supported_squashfs_filesystem(
  116. le16_to_cpu(sblk->s_major),
  117. le16_to_cpu(sblk->s_minor),
  118. le16_to_cpu(sblk->compression));
  119. if (msblk->decompressor == NULL)
  120. goto failed_mount;
  121. /*
  122. * Check if there's xattrs in the filesystem. These are not
  123. * supported in this version, so warn that they will be ignored.
  124. */
  125. if (le64_to_cpu(sblk->xattr_table_start) != SQUASHFS_INVALID_BLK)
  126. ERROR("Xattrs in filesystem, these will be ignored\n");
  127. /* Check the filesystem does not extend beyond the end of the
  128. block device */
  129. msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
  130. if (msblk->bytes_used < 0 || msblk->bytes_used >
  131. i_size_read(sb->s_bdev->bd_inode))
  132. goto failed_mount;
  133. /* Check block size for sanity */
  134. msblk->block_size = le32_to_cpu(sblk->block_size);
  135. if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
  136. goto failed_mount;
  137. /*
  138. * Check the system page size is not larger than the filesystem
  139. * block size (by default 128K). This is currently not supported.
  140. */
  141. if (PAGE_CACHE_SIZE > msblk->block_size) {
  142. ERROR("Page size > filesystem block size (%d). This is "
  143. "currently not supported!\n", msblk->block_size);
  144. goto failed_mount;
  145. }
  146. msblk->block_log = le16_to_cpu(sblk->block_log);
  147. if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
  148. goto failed_mount;
  149. /* Check the root inode for sanity */
  150. root_inode = le64_to_cpu(sblk->root_inode);
  151. if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
  152. goto failed_mount;
  153. msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
  154. msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
  155. msblk->inodes = le32_to_cpu(sblk->inodes);
  156. flags = le16_to_cpu(sblk->flags);
  157. TRACE("Found valid superblock on %s\n", bdevname(sb->s_bdev, b));
  158. TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
  159. ? "un" : "");
  160. TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
  161. ? "un" : "");
  162. TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
  163. TRACE("Block size %d\n", msblk->block_size);
  164. TRACE("Number of inodes %d\n", msblk->inodes);
  165. TRACE("Number of fragments %d\n", le32_to_cpu(sblk->fragments));
  166. TRACE("Number of ids %d\n", le16_to_cpu(sblk->no_ids));
  167. TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
  168. TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
  169. TRACE("sblk->fragment_table_start %llx\n",
  170. (u64) le64_to_cpu(sblk->fragment_table_start));
  171. TRACE("sblk->id_table_start %llx\n",
  172. (u64) le64_to_cpu(sblk->id_table_start));
  173. sb->s_maxbytes = MAX_LFS_FILESIZE;
  174. sb->s_flags |= MS_RDONLY;
  175. sb->s_op = &squashfs_super_ops;
  176. err = -ENOMEM;
  177. msblk->stream = squashfs_decompressor_init(msblk);
  178. if (msblk->stream == NULL)
  179. goto failed_mount;
  180. msblk->block_cache = squashfs_cache_init("metadata",
  181. SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
  182. if (msblk->block_cache == NULL)
  183. goto failed_mount;
  184. /* Allocate read_page block */
  185. msblk->read_page = squashfs_cache_init("data", 1, msblk->block_size);
  186. if (msblk->read_page == NULL) {
  187. ERROR("Failed to allocate read_page block\n");
  188. goto failed_mount;
  189. }
  190. /* Allocate and read id index table */
  191. msblk->id_table = squashfs_read_id_index_table(sb,
  192. le64_to_cpu(sblk->id_table_start), le16_to_cpu(sblk->no_ids));
  193. if (IS_ERR(msblk->id_table)) {
  194. err = PTR_ERR(msblk->id_table);
  195. msblk->id_table = NULL;
  196. goto failed_mount;
  197. }
  198. fragments = le32_to_cpu(sblk->fragments);
  199. if (fragments == 0)
  200. goto allocate_lookup_table;
  201. msblk->fragment_cache = squashfs_cache_init("fragment",
  202. SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
  203. if (msblk->fragment_cache == NULL) {
  204. err = -ENOMEM;
  205. goto failed_mount;
  206. }
  207. /* Allocate and read fragment index table */
  208. msblk->fragment_index = squashfs_read_fragment_index_table(sb,
  209. le64_to_cpu(sblk->fragment_table_start), fragments);
  210. if (IS_ERR(msblk->fragment_index)) {
  211. err = PTR_ERR(msblk->fragment_index);
  212. msblk->fragment_index = NULL;
  213. goto failed_mount;
  214. }
  215. allocate_lookup_table:
  216. lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
  217. if (lookup_table_start == SQUASHFS_INVALID_BLK)
  218. goto allocate_root;
  219. /* Allocate and read inode lookup table */
  220. msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
  221. lookup_table_start, msblk->inodes);
  222. if (IS_ERR(msblk->inode_lookup_table)) {
  223. err = PTR_ERR(msblk->inode_lookup_table);
  224. msblk->inode_lookup_table = NULL;
  225. goto failed_mount;
  226. }
  227. sb->s_export_op = &squashfs_export_ops;
  228. allocate_root:
  229. root = new_inode(sb);
  230. if (!root) {
  231. err = -ENOMEM;
  232. goto failed_mount;
  233. }
  234. err = squashfs_read_inode(root, root_inode);
  235. if (err) {
  236. iget_failed(root);
  237. goto failed_mount;
  238. }
  239. insert_inode_hash(root);
  240. sb->s_root = d_alloc_root(root);
  241. if (sb->s_root == NULL) {
  242. ERROR("Root inode create failed\n");
  243. err = -ENOMEM;
  244. iput(root);
  245. goto failed_mount;
  246. }
  247. TRACE("Leaving squashfs_fill_super\n");
  248. kfree(sblk);
  249. return 0;
  250. failed_mount:
  251. squashfs_cache_delete(msblk->block_cache);
  252. squashfs_cache_delete(msblk->fragment_cache);
  253. squashfs_cache_delete(msblk->read_page);
  254. squashfs_decompressor_free(msblk, msblk->stream);
  255. kfree(msblk->inode_lookup_table);
  256. kfree(msblk->fragment_index);
  257. kfree(msblk->id_table);
  258. kfree(sb->s_fs_info);
  259. sb->s_fs_info = NULL;
  260. kfree(sblk);
  261. return err;
  262. failure:
  263. kfree(sb->s_fs_info);
  264. sb->s_fs_info = NULL;
  265. return -ENOMEM;
  266. }
  267. static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  268. {
  269. struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
  270. u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
  271. TRACE("Entered squashfs_statfs\n");
  272. buf->f_type = SQUASHFS_MAGIC;
  273. buf->f_bsize = msblk->block_size;
  274. buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
  275. buf->f_bfree = buf->f_bavail = 0;
  276. buf->f_files = msblk->inodes;
  277. buf->f_ffree = 0;
  278. buf->f_namelen = SQUASHFS_NAME_LEN;
  279. buf->f_fsid.val[0] = (u32)id;
  280. buf->f_fsid.val[1] = (u32)(id >> 32);
  281. return 0;
  282. }
  283. static int squashfs_remount(struct super_block *sb, int *flags, char *data)
  284. {
  285. *flags |= MS_RDONLY;
  286. return 0;
  287. }
  288. static void squashfs_put_super(struct super_block *sb)
  289. {
  290. lock_kernel();
  291. if (sb->s_fs_info) {
  292. struct squashfs_sb_info *sbi = sb->s_fs_info;
  293. squashfs_cache_delete(sbi->block_cache);
  294. squashfs_cache_delete(sbi->fragment_cache);
  295. squashfs_cache_delete(sbi->read_page);
  296. squashfs_decompressor_free(sbi, sbi->stream);
  297. kfree(sbi->id_table);
  298. kfree(sbi->fragment_index);
  299. kfree(sbi->meta_index);
  300. kfree(sb->s_fs_info);
  301. sb->s_fs_info = NULL;
  302. }
  303. unlock_kernel();
  304. }
  305. static int squashfs_get_sb(struct file_system_type *fs_type, int flags,
  306. const char *dev_name, void *data,
  307. struct vfsmount *mnt)
  308. {
  309. return get_sb_bdev(fs_type, flags, dev_name, data, squashfs_fill_super,
  310. mnt);
  311. }
  312. static struct kmem_cache *squashfs_inode_cachep;
  313. static void init_once(void *foo)
  314. {
  315. struct squashfs_inode_info *ei = foo;
  316. inode_init_once(&ei->vfs_inode);
  317. }
  318. static int __init init_inodecache(void)
  319. {
  320. squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
  321. sizeof(struct squashfs_inode_info), 0,
  322. SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, init_once);
  323. return squashfs_inode_cachep ? 0 : -ENOMEM;
  324. }
  325. static void destroy_inodecache(void)
  326. {
  327. kmem_cache_destroy(squashfs_inode_cachep);
  328. }
  329. static int __init init_squashfs_fs(void)
  330. {
  331. int err = init_inodecache();
  332. if (err)
  333. return err;
  334. err = register_filesystem(&squashfs_fs_type);
  335. if (err) {
  336. destroy_inodecache();
  337. return err;
  338. }
  339. printk(KERN_INFO "squashfs: version 4.0 (2009/01/31) "
  340. "Phillip Lougher\n");
  341. return 0;
  342. }
  343. static void __exit exit_squashfs_fs(void)
  344. {
  345. unregister_filesystem(&squashfs_fs_type);
  346. destroy_inodecache();
  347. }
  348. static struct inode *squashfs_alloc_inode(struct super_block *sb)
  349. {
  350. struct squashfs_inode_info *ei =
  351. kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
  352. return ei ? &ei->vfs_inode : NULL;
  353. }
  354. static void squashfs_destroy_inode(struct inode *inode)
  355. {
  356. kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
  357. }
  358. static struct file_system_type squashfs_fs_type = {
  359. .owner = THIS_MODULE,
  360. .name = "squashfs",
  361. .get_sb = squashfs_get_sb,
  362. .kill_sb = kill_block_super,
  363. .fs_flags = FS_REQUIRES_DEV
  364. };
  365. static const struct super_operations squashfs_super_ops = {
  366. .alloc_inode = squashfs_alloc_inode,
  367. .destroy_inode = squashfs_destroy_inode,
  368. .statfs = squashfs_statfs,
  369. .put_super = squashfs_put_super,
  370. .remount_fs = squashfs_remount
  371. };
  372. module_init(init_squashfs_fs);
  373. module_exit(exit_squashfs_fs);
  374. MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
  375. MODULE_AUTHOR("Phillip Lougher <phillip@lougher.demon.co.uk>");
  376. MODULE_LICENSE("GPL");