super.c 13 KB

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