super.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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@squashfs.org.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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/fs.h>
  30. #include <linux/vfs.h>
  31. #include <linux/slab.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 <linux/xattr.h>
  38. #include "squashfs_fs.h"
  39. #include "squashfs_fs_sb.h"
  40. #include "squashfs_fs_i.h"
  41. #include "squashfs.h"
  42. #include "decompressor.h"
  43. #include "xattr.h"
  44. static struct file_system_type squashfs_fs_type;
  45. static const struct super_operations squashfs_super_ops;
  46. static const struct squashfs_decompressor *supported_squashfs_filesystem(short
  47. major, short minor, short id)
  48. {
  49. const struct squashfs_decompressor *decompressor;
  50. if (major < SQUASHFS_MAJOR) {
  51. ERROR("Major/Minor mismatch, older Squashfs %d.%d "
  52. "filesystems are unsupported\n", major, minor);
  53. return NULL;
  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 NULL;
  59. }
  60. decompressor = squashfs_lookup_decompressor(id);
  61. if (!decompressor->supported) {
  62. ERROR("Filesystem uses \"%s\" compression. This is not "
  63. "supported\n", decompressor->name);
  64. return NULL;
  65. }
  66. return decompressor;
  67. }
  68. static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
  69. {
  70. struct squashfs_sb_info *msblk;
  71. struct squashfs_super_block *sblk = NULL;
  72. struct inode *root;
  73. long long root_inode;
  74. unsigned short flags;
  75. unsigned int fragments;
  76. u64 lookup_table_start, xattr_id_table_start, next_table;
  77. int err;
  78. TRACE("Entered squashfs_fill_superblock\n");
  79. sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
  80. if (sb->s_fs_info == NULL) {
  81. ERROR("Failed to allocate squashfs_sb_info\n");
  82. return -ENOMEM;
  83. }
  84. msblk = sb->s_fs_info;
  85. msblk->devblksize = sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE);
  86. msblk->devblksize_log2 = ffz(~msblk->devblksize);
  87. mutex_init(&msblk->meta_index_mutex);
  88. /*
  89. * msblk->bytes_used is checked in squashfs_read_table to ensure reads
  90. * are not beyond filesystem end. But as we're using
  91. * squashfs_read_table here to read the superblock (including the value
  92. * of bytes_used) we need to set it to an initial sensible dummy value
  93. */
  94. msblk->bytes_used = sizeof(*sblk);
  95. sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
  96. if (IS_ERR(sblk)) {
  97. ERROR("unable to read squashfs_super_block\n");
  98. err = PTR_ERR(sblk);
  99. sblk = NULL;
  100. goto failed_mount;
  101. }
  102. err = -EINVAL;
  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 %pg\n",
  108. sb->s_bdev);
  109. goto failed_mount;
  110. }
  111. /* Check the MAJOR & MINOR versions and lookup compression type */
  112. msblk->decompressor = supported_squashfs_filesystem(
  113. le16_to_cpu(sblk->s_major),
  114. le16_to_cpu(sblk->s_minor),
  115. le16_to_cpu(sblk->compression));
  116. if (msblk->decompressor == NULL)
  117. goto failed_mount;
  118. /* Check the filesystem does not extend beyond the end of the
  119. block device */
  120. msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
  121. if (msblk->bytes_used < 0 || msblk->bytes_used >
  122. i_size_read(sb->s_bdev->bd_inode))
  123. goto failed_mount;
  124. /* Check block size for sanity */
  125. msblk->block_size = le32_to_cpu(sblk->block_size);
  126. if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
  127. goto failed_mount;
  128. /*
  129. * Check the system page size is not larger than the filesystem
  130. * block size (by default 128K). This is currently not supported.
  131. */
  132. if (PAGE_SIZE > msblk->block_size) {
  133. ERROR("Page size > filesystem block size (%d). This is "
  134. "currently not supported!\n", msblk->block_size);
  135. goto failed_mount;
  136. }
  137. /* Check block log for sanity */
  138. msblk->block_log = le16_to_cpu(sblk->block_log);
  139. if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
  140. goto failed_mount;
  141. /* Check that block_size and block_log match */
  142. if (msblk->block_size != (1 << msblk->block_log))
  143. goto failed_mount;
  144. /* Check the root inode for sanity */
  145. root_inode = le64_to_cpu(sblk->root_inode);
  146. if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
  147. goto failed_mount;
  148. msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
  149. msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
  150. msblk->inodes = le32_to_cpu(sblk->inodes);
  151. flags = le16_to_cpu(sblk->flags);
  152. TRACE("Found valid superblock on %pg\n", sb->s_bdev);
  153. TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
  154. ? "un" : "");
  155. TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
  156. ? "un" : "");
  157. TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
  158. TRACE("Block size %d\n", msblk->block_size);
  159. TRACE("Number of inodes %d\n", msblk->inodes);
  160. TRACE("Number of fragments %d\n", le32_to_cpu(sblk->fragments));
  161. TRACE("Number of ids %d\n", le16_to_cpu(sblk->no_ids));
  162. TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
  163. TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
  164. TRACE("sblk->fragment_table_start %llx\n",
  165. (u64) le64_to_cpu(sblk->fragment_table_start));
  166. TRACE("sblk->id_table_start %llx\n",
  167. (u64) le64_to_cpu(sblk->id_table_start));
  168. sb->s_maxbytes = MAX_LFS_FILESIZE;
  169. sb->s_flags |= MS_RDONLY;
  170. sb->s_op = &squashfs_super_ops;
  171. err = -ENOMEM;
  172. msblk->block_cache = squashfs_cache_init("metadata",
  173. SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
  174. if (msblk->block_cache == NULL)
  175. goto failed_mount;
  176. /* Allocate read_page block */
  177. msblk->read_page = squashfs_cache_init("data",
  178. squashfs_max_decompressors(), msblk->block_size);
  179. if (msblk->read_page == NULL) {
  180. ERROR("Failed to allocate read_page block\n");
  181. goto failed_mount;
  182. }
  183. msblk->stream = squashfs_decompressor_setup(sb, flags);
  184. if (IS_ERR(msblk->stream)) {
  185. err = PTR_ERR(msblk->stream);
  186. msblk->stream = NULL;
  187. goto failed_mount;
  188. }
  189. /* Handle xattrs */
  190. sb->s_xattr = squashfs_xattr_handlers;
  191. xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
  192. if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
  193. next_table = msblk->bytes_used;
  194. goto allocate_id_index_table;
  195. }
  196. /* Allocate and read xattr id lookup table */
  197. msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
  198. xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
  199. if (IS_ERR(msblk->xattr_id_table)) {
  200. ERROR("unable to read xattr id index table\n");
  201. err = PTR_ERR(msblk->xattr_id_table);
  202. msblk->xattr_id_table = NULL;
  203. if (err != -ENOTSUPP)
  204. goto failed_mount;
  205. }
  206. next_table = msblk->xattr_table;
  207. allocate_id_index_table:
  208. /* Allocate and read id index table */
  209. msblk->id_table = squashfs_read_id_index_table(sb,
  210. le64_to_cpu(sblk->id_table_start), next_table,
  211. le16_to_cpu(sblk->no_ids));
  212. if (IS_ERR(msblk->id_table)) {
  213. ERROR("unable to read id index table\n");
  214. err = PTR_ERR(msblk->id_table);
  215. msblk->id_table = NULL;
  216. goto failed_mount;
  217. }
  218. next_table = le64_to_cpu(msblk->id_table[0]);
  219. /* Handle inode lookup table */
  220. lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
  221. if (lookup_table_start == SQUASHFS_INVALID_BLK)
  222. goto handle_fragments;
  223. /* Allocate and read inode lookup table */
  224. msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
  225. lookup_table_start, next_table, msblk->inodes);
  226. if (IS_ERR(msblk->inode_lookup_table)) {
  227. ERROR("unable to read inode lookup table\n");
  228. err = PTR_ERR(msblk->inode_lookup_table);
  229. msblk->inode_lookup_table = NULL;
  230. goto failed_mount;
  231. }
  232. next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
  233. sb->s_export_op = &squashfs_export_ops;
  234. handle_fragments:
  235. fragments = le32_to_cpu(sblk->fragments);
  236. if (fragments == 0)
  237. goto check_directory_table;
  238. msblk->fragment_cache = squashfs_cache_init("fragment",
  239. SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
  240. if (msblk->fragment_cache == NULL) {
  241. err = -ENOMEM;
  242. goto failed_mount;
  243. }
  244. /* Allocate and read fragment index table */
  245. msblk->fragment_index = squashfs_read_fragment_index_table(sb,
  246. le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
  247. if (IS_ERR(msblk->fragment_index)) {
  248. ERROR("unable to read fragment index table\n");
  249. err = PTR_ERR(msblk->fragment_index);
  250. msblk->fragment_index = NULL;
  251. goto failed_mount;
  252. }
  253. next_table = le64_to_cpu(msblk->fragment_index[0]);
  254. check_directory_table:
  255. /* Sanity check directory_table */
  256. if (msblk->directory_table > next_table) {
  257. err = -EINVAL;
  258. goto failed_mount;
  259. }
  260. /* Sanity check inode_table */
  261. if (msblk->inode_table >= msblk->directory_table) {
  262. err = -EINVAL;
  263. goto failed_mount;
  264. }
  265. /* allocate root */
  266. root = new_inode(sb);
  267. if (!root) {
  268. err = -ENOMEM;
  269. goto failed_mount;
  270. }
  271. err = squashfs_read_inode(root, root_inode);
  272. if (err) {
  273. make_bad_inode(root);
  274. iput(root);
  275. goto failed_mount;
  276. }
  277. insert_inode_hash(root);
  278. sb->s_root = d_make_root(root);
  279. if (sb->s_root == NULL) {
  280. ERROR("Root inode create failed\n");
  281. err = -ENOMEM;
  282. goto failed_mount;
  283. }
  284. TRACE("Leaving squashfs_fill_super\n");
  285. kfree(sblk);
  286. return 0;
  287. failed_mount:
  288. squashfs_cache_delete(msblk->block_cache);
  289. squashfs_cache_delete(msblk->fragment_cache);
  290. squashfs_cache_delete(msblk->read_page);
  291. squashfs_decompressor_destroy(msblk);
  292. kfree(msblk->inode_lookup_table);
  293. kfree(msblk->fragment_index);
  294. kfree(msblk->id_table);
  295. kfree(msblk->xattr_id_table);
  296. kfree(sb->s_fs_info);
  297. sb->s_fs_info = NULL;
  298. kfree(sblk);
  299. return err;
  300. }
  301. static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  302. {
  303. struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
  304. u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
  305. TRACE("Entered squashfs_statfs\n");
  306. buf->f_type = SQUASHFS_MAGIC;
  307. buf->f_bsize = msblk->block_size;
  308. buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
  309. buf->f_bfree = buf->f_bavail = 0;
  310. buf->f_files = msblk->inodes;
  311. buf->f_ffree = 0;
  312. buf->f_namelen = SQUASHFS_NAME_LEN;
  313. buf->f_fsid.val[0] = (u32)id;
  314. buf->f_fsid.val[1] = (u32)(id >> 32);
  315. return 0;
  316. }
  317. static int squashfs_remount(struct super_block *sb, int *flags, char *data)
  318. {
  319. sync_filesystem(sb);
  320. *flags |= MS_RDONLY;
  321. return 0;
  322. }
  323. static void squashfs_put_super(struct super_block *sb)
  324. {
  325. if (sb->s_fs_info) {
  326. struct squashfs_sb_info *sbi = sb->s_fs_info;
  327. squashfs_cache_delete(sbi->block_cache);
  328. squashfs_cache_delete(sbi->fragment_cache);
  329. squashfs_cache_delete(sbi->read_page);
  330. squashfs_decompressor_destroy(sbi);
  331. kfree(sbi->id_table);
  332. kfree(sbi->fragment_index);
  333. kfree(sbi->meta_index);
  334. kfree(sbi->inode_lookup_table);
  335. kfree(sbi->xattr_id_table);
  336. kfree(sb->s_fs_info);
  337. sb->s_fs_info = NULL;
  338. }
  339. }
  340. static struct dentry *squashfs_mount(struct file_system_type *fs_type,
  341. int flags, const char *dev_name, void *data)
  342. {
  343. return mount_bdev(fs_type, flags, dev_name, data, squashfs_fill_super);
  344. }
  345. static struct kmem_cache *squashfs_inode_cachep;
  346. static void init_once(void *foo)
  347. {
  348. struct squashfs_inode_info *ei = foo;
  349. inode_init_once(&ei->vfs_inode);
  350. }
  351. static int __init init_inodecache(void)
  352. {
  353. squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
  354. sizeof(struct squashfs_inode_info), 0,
  355. SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
  356. init_once);
  357. return squashfs_inode_cachep ? 0 : -ENOMEM;
  358. }
  359. static void destroy_inodecache(void)
  360. {
  361. /*
  362. * Make sure all delayed rcu free inodes are flushed before we
  363. * destroy cache.
  364. */
  365. rcu_barrier();
  366. kmem_cache_destroy(squashfs_inode_cachep);
  367. }
  368. static int __init init_squashfs_fs(void)
  369. {
  370. int err = init_inodecache();
  371. if (err)
  372. return err;
  373. err = register_filesystem(&squashfs_fs_type);
  374. if (err) {
  375. destroy_inodecache();
  376. return err;
  377. }
  378. pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
  379. return 0;
  380. }
  381. static void __exit exit_squashfs_fs(void)
  382. {
  383. unregister_filesystem(&squashfs_fs_type);
  384. destroy_inodecache();
  385. }
  386. static struct inode *squashfs_alloc_inode(struct super_block *sb)
  387. {
  388. struct squashfs_inode_info *ei =
  389. kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
  390. return ei ? &ei->vfs_inode : NULL;
  391. }
  392. static void squashfs_i_callback(struct rcu_head *head)
  393. {
  394. struct inode *inode = container_of(head, struct inode, i_rcu);
  395. kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
  396. }
  397. static void squashfs_destroy_inode(struct inode *inode)
  398. {
  399. call_rcu(&inode->i_rcu, squashfs_i_callback);
  400. }
  401. static struct file_system_type squashfs_fs_type = {
  402. .owner = THIS_MODULE,
  403. .name = "squashfs",
  404. .mount = squashfs_mount,
  405. .kill_sb = kill_block_super,
  406. .fs_flags = FS_REQUIRES_DEV
  407. };
  408. MODULE_ALIAS_FS("squashfs");
  409. static const struct super_operations squashfs_super_ops = {
  410. .alloc_inode = squashfs_alloc_inode,
  411. .destroy_inode = squashfs_destroy_inode,
  412. .statfs = squashfs_statfs,
  413. .put_super = squashfs_put_super,
  414. .remount_fs = squashfs_remount
  415. };
  416. module_init(init_squashfs_fs);
  417. module_exit(exit_squashfs_fs);
  418. MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
  419. MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
  420. MODULE_LICENSE("GPL");