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