super.c 12 KB

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