super.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 <[email protected]>
  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/blkdev.h>
  17. #include <linux/fs.h>
  18. #include <linux/fs_context.h>
  19. #include <linux/fs_parser.h>
  20. #include <linux/vfs.h>
  21. #include <linux/slab.h>
  22. #include <linux/mutex.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/magic.h>
  28. #include <linux/xattr.h>
  29. #include "squashfs_fs.h"
  30. #include "squashfs_fs_sb.h"
  31. #include "squashfs_fs_i.h"
  32. #include "squashfs.h"
  33. #include "decompressor.h"
  34. #include "xattr.h"
  35. static struct file_system_type squashfs_fs_type;
  36. static const struct super_operations squashfs_super_ops;
  37. enum Opt_errors {
  38. Opt_errors_continue,
  39. Opt_errors_panic,
  40. };
  41. enum squashfs_param {
  42. Opt_errors,
  43. };
  44. struct squashfs_mount_opts {
  45. enum Opt_errors errors;
  46. };
  47. static const struct constant_table squashfs_param_errors[] = {
  48. {"continue", Opt_errors_continue },
  49. {"panic", Opt_errors_panic },
  50. {}
  51. };
  52. static const struct fs_parameter_spec squashfs_fs_parameters[] = {
  53. fsparam_enum("errors", Opt_errors, squashfs_param_errors),
  54. {}
  55. };
  56. static int squashfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  57. {
  58. struct squashfs_mount_opts *opts = fc->fs_private;
  59. struct fs_parse_result result;
  60. int opt;
  61. opt = fs_parse(fc, squashfs_fs_parameters, param, &result);
  62. if (opt < 0)
  63. return opt;
  64. switch (opt) {
  65. case Opt_errors:
  66. opts->errors = result.uint_32;
  67. break;
  68. default:
  69. return -EINVAL;
  70. }
  71. return 0;
  72. }
  73. static const struct squashfs_decompressor *supported_squashfs_filesystem(
  74. struct fs_context *fc,
  75. short major, short minor, short id)
  76. {
  77. const struct squashfs_decompressor *decompressor;
  78. if (major < SQUASHFS_MAJOR) {
  79. errorf(fc, "Major/Minor mismatch, older Squashfs %d.%d "
  80. "filesystems are unsupported", major, minor);
  81. return NULL;
  82. } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
  83. errorf(fc, "Major/Minor mismatch, trying to mount newer "
  84. "%d.%d filesystem", major, minor);
  85. errorf(fc, "Please update your kernel");
  86. return NULL;
  87. }
  88. decompressor = squashfs_lookup_decompressor(id);
  89. if (!decompressor->supported) {
  90. errorf(fc, "Filesystem uses \"%s\" compression. This is not supported",
  91. decompressor->name);
  92. return NULL;
  93. }
  94. return decompressor;
  95. }
  96. static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)
  97. {
  98. struct squashfs_mount_opts *opts = fc->fs_private;
  99. struct squashfs_sb_info *msblk;
  100. struct squashfs_super_block *sblk = NULL;
  101. struct inode *root;
  102. long long root_inode;
  103. unsigned short flags;
  104. unsigned int fragments;
  105. u64 lookup_table_start, xattr_id_table_start, next_table;
  106. int err;
  107. TRACE("Entered squashfs_fill_superblock\n");
  108. sb->s_fs_info = kzalloc(sizeof(*msblk), GFP_KERNEL);
  109. if (sb->s_fs_info == NULL) {
  110. ERROR("Failed to allocate squashfs_sb_info\n");
  111. return -ENOMEM;
  112. }
  113. msblk = sb->s_fs_info;
  114. msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
  115. msblk->devblksize = sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE);
  116. msblk->devblksize_log2 = ffz(~msblk->devblksize);
  117. mutex_init(&msblk->meta_index_mutex);
  118. /*
  119. * msblk->bytes_used is checked in squashfs_read_table to ensure reads
  120. * are not beyond filesystem end. But as we're using
  121. * squashfs_read_table here to read the superblock (including the value
  122. * of bytes_used) we need to set it to an initial sensible dummy value
  123. */
  124. msblk->bytes_used = sizeof(*sblk);
  125. sblk = squashfs_read_table(sb, SQUASHFS_START, sizeof(*sblk));
  126. if (IS_ERR(sblk)) {
  127. errorf(fc, "unable to read squashfs_super_block");
  128. err = PTR_ERR(sblk);
  129. sblk = NULL;
  130. goto failed_mount;
  131. }
  132. err = -EINVAL;
  133. /* Check it is a SQUASHFS superblock */
  134. sb->s_magic = le32_to_cpu(sblk->s_magic);
  135. if (sb->s_magic != SQUASHFS_MAGIC) {
  136. if (!(fc->sb_flags & SB_SILENT))
  137. errorf(fc, "Can't find a SQUASHFS superblock on %pg",
  138. sb->s_bdev);
  139. goto failed_mount;
  140. }
  141. /* Check the MAJOR & MINOR versions and lookup compression type */
  142. msblk->decompressor = supported_squashfs_filesystem(
  143. fc,
  144. le16_to_cpu(sblk->s_major),
  145. le16_to_cpu(sblk->s_minor),
  146. le16_to_cpu(sblk->compression));
  147. if (msblk->decompressor == NULL)
  148. goto failed_mount;
  149. /* Check the filesystem does not extend beyond the end of the
  150. block device */
  151. msblk->bytes_used = le64_to_cpu(sblk->bytes_used);
  152. if (msblk->bytes_used < 0 ||
  153. msblk->bytes_used > bdev_nr_bytes(sb->s_bdev))
  154. goto failed_mount;
  155. /* Check block size for sanity */
  156. msblk->block_size = le32_to_cpu(sblk->block_size);
  157. if (msblk->block_size > SQUASHFS_FILE_MAX_SIZE)
  158. goto insanity;
  159. /*
  160. * Check the system page size is not larger than the filesystem
  161. * block size (by default 128K). This is currently not supported.
  162. */
  163. if (PAGE_SIZE > msblk->block_size) {
  164. errorf(fc, "Page size > filesystem block size (%d). This is "
  165. "currently not supported!", msblk->block_size);
  166. goto failed_mount;
  167. }
  168. /* Check block log for sanity */
  169. msblk->block_log = le16_to_cpu(sblk->block_log);
  170. if (msblk->block_log > SQUASHFS_FILE_MAX_LOG)
  171. goto failed_mount;
  172. /* Check that block_size and block_log match */
  173. if (msblk->block_size != (1 << msblk->block_log))
  174. goto insanity;
  175. /* Check the root inode for sanity */
  176. root_inode = le64_to_cpu(sblk->root_inode);
  177. if (SQUASHFS_INODE_OFFSET(root_inode) > SQUASHFS_METADATA_SIZE)
  178. goto insanity;
  179. msblk->inode_table = le64_to_cpu(sblk->inode_table_start);
  180. msblk->directory_table = le64_to_cpu(sblk->directory_table_start);
  181. msblk->inodes = le32_to_cpu(sblk->inodes);
  182. msblk->fragments = le32_to_cpu(sblk->fragments);
  183. msblk->ids = le16_to_cpu(sblk->no_ids);
  184. flags = le16_to_cpu(sblk->flags);
  185. TRACE("Found valid superblock on %pg\n", sb->s_bdev);
  186. TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(flags)
  187. ? "un" : "");
  188. TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(flags)
  189. ? "un" : "");
  190. TRACE("Filesystem size %lld bytes\n", msblk->bytes_used);
  191. TRACE("Block size %d\n", msblk->block_size);
  192. TRACE("Number of inodes %d\n", msblk->inodes);
  193. TRACE("Number of fragments %d\n", msblk->fragments);
  194. TRACE("Number of ids %d\n", msblk->ids);
  195. TRACE("sblk->inode_table_start %llx\n", msblk->inode_table);
  196. TRACE("sblk->directory_table_start %llx\n", msblk->directory_table);
  197. TRACE("sblk->fragment_table_start %llx\n",
  198. (u64) le64_to_cpu(sblk->fragment_table_start));
  199. TRACE("sblk->id_table_start %llx\n",
  200. (u64) le64_to_cpu(sblk->id_table_start));
  201. sb->s_maxbytes = MAX_LFS_FILESIZE;
  202. sb->s_time_min = 0;
  203. sb->s_time_max = U32_MAX;
  204. sb->s_flags |= SB_RDONLY;
  205. sb->s_op = &squashfs_super_ops;
  206. err = -ENOMEM;
  207. msblk->block_cache = squashfs_cache_init("metadata",
  208. SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
  209. if (msblk->block_cache == NULL)
  210. goto failed_mount;
  211. /* Allocate read_page block */
  212. msblk->read_page = squashfs_cache_init("data",
  213. squashfs_max_decompressors(), msblk->block_size);
  214. if (msblk->read_page == NULL) {
  215. errorf(fc, "Failed to allocate read_page block");
  216. goto failed_mount;
  217. }
  218. msblk->stream = squashfs_decompressor_setup(sb, flags);
  219. if (IS_ERR(msblk->stream)) {
  220. err = PTR_ERR(msblk->stream);
  221. msblk->stream = NULL;
  222. goto insanity;
  223. }
  224. /* Handle xattrs */
  225. sb->s_xattr = squashfs_xattr_handlers;
  226. xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
  227. if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
  228. next_table = msblk->bytes_used;
  229. goto allocate_id_index_table;
  230. }
  231. /* Allocate and read xattr id lookup table */
  232. msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
  233. xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
  234. if (IS_ERR(msblk->xattr_id_table)) {
  235. errorf(fc, "unable to read xattr id index table");
  236. err = PTR_ERR(msblk->xattr_id_table);
  237. msblk->xattr_id_table = NULL;
  238. if (err != -ENOTSUPP)
  239. goto failed_mount;
  240. }
  241. next_table = msblk->xattr_table;
  242. allocate_id_index_table:
  243. /* Allocate and read id index table */
  244. msblk->id_table = squashfs_read_id_index_table(sb,
  245. le64_to_cpu(sblk->id_table_start), next_table, msblk->ids);
  246. if (IS_ERR(msblk->id_table)) {
  247. errorf(fc, "unable to read id index table");
  248. err = PTR_ERR(msblk->id_table);
  249. msblk->id_table = NULL;
  250. goto failed_mount;
  251. }
  252. next_table = le64_to_cpu(msblk->id_table[0]);
  253. /* Handle inode lookup table */
  254. lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
  255. if (lookup_table_start == SQUASHFS_INVALID_BLK)
  256. goto handle_fragments;
  257. /* Allocate and read inode lookup table */
  258. msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
  259. lookup_table_start, next_table, msblk->inodes);
  260. if (IS_ERR(msblk->inode_lookup_table)) {
  261. errorf(fc, "unable to read inode lookup table");
  262. err = PTR_ERR(msblk->inode_lookup_table);
  263. msblk->inode_lookup_table = NULL;
  264. goto failed_mount;
  265. }
  266. next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
  267. sb->s_export_op = &squashfs_export_ops;
  268. handle_fragments:
  269. fragments = msblk->fragments;
  270. if (fragments == 0)
  271. goto check_directory_table;
  272. msblk->fragment_cache = squashfs_cache_init("fragment",
  273. SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
  274. if (msblk->fragment_cache == NULL) {
  275. err = -ENOMEM;
  276. goto failed_mount;
  277. }
  278. /* Allocate and read fragment index table */
  279. msblk->fragment_index = squashfs_read_fragment_index_table(sb,
  280. le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
  281. if (IS_ERR(msblk->fragment_index)) {
  282. errorf(fc, "unable to read fragment index table");
  283. err = PTR_ERR(msblk->fragment_index);
  284. msblk->fragment_index = NULL;
  285. goto failed_mount;
  286. }
  287. next_table = le64_to_cpu(msblk->fragment_index[0]);
  288. check_directory_table:
  289. /* Sanity check directory_table */
  290. if (msblk->directory_table > next_table) {
  291. err = -EINVAL;
  292. goto insanity;
  293. }
  294. /* Sanity check inode_table */
  295. if (msblk->inode_table >= msblk->directory_table) {
  296. err = -EINVAL;
  297. goto insanity;
  298. }
  299. /* allocate root */
  300. root = new_inode(sb);
  301. if (!root) {
  302. err = -ENOMEM;
  303. goto failed_mount;
  304. }
  305. err = squashfs_read_inode(root, root_inode);
  306. if (err) {
  307. make_bad_inode(root);
  308. iput(root);
  309. goto failed_mount;
  310. }
  311. insert_inode_hash(root);
  312. sb->s_root = d_make_root(root);
  313. if (sb->s_root == NULL) {
  314. ERROR("Root inode create failed\n");
  315. err = -ENOMEM;
  316. goto failed_mount;
  317. }
  318. TRACE("Leaving squashfs_fill_super\n");
  319. kfree(sblk);
  320. return 0;
  321. insanity:
  322. errorf(fc, "squashfs image failed sanity check");
  323. failed_mount:
  324. squashfs_cache_delete(msblk->block_cache);
  325. squashfs_cache_delete(msblk->fragment_cache);
  326. squashfs_cache_delete(msblk->read_page);
  327. squashfs_decompressor_destroy(msblk);
  328. kfree(msblk->inode_lookup_table);
  329. kfree(msblk->fragment_index);
  330. kfree(msblk->id_table);
  331. kfree(msblk->xattr_id_table);
  332. kfree(sb->s_fs_info);
  333. sb->s_fs_info = NULL;
  334. kfree(sblk);
  335. return err;
  336. }
  337. static int squashfs_get_tree(struct fs_context *fc)
  338. {
  339. return get_tree_bdev(fc, squashfs_fill_super);
  340. }
  341. static int squashfs_reconfigure(struct fs_context *fc)
  342. {
  343. struct super_block *sb = fc->root->d_sb;
  344. struct squashfs_sb_info *msblk = sb->s_fs_info;
  345. struct squashfs_mount_opts *opts = fc->fs_private;
  346. sync_filesystem(fc->root->d_sb);
  347. fc->sb_flags |= SB_RDONLY;
  348. msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
  349. return 0;
  350. }
  351. static void squashfs_free_fs_context(struct fs_context *fc)
  352. {
  353. kfree(fc->fs_private);
  354. }
  355. static const struct fs_context_operations squashfs_context_ops = {
  356. .get_tree = squashfs_get_tree,
  357. .free = squashfs_free_fs_context,
  358. .parse_param = squashfs_parse_param,
  359. .reconfigure = squashfs_reconfigure,
  360. };
  361. static int squashfs_show_options(struct seq_file *s, struct dentry *root)
  362. {
  363. struct super_block *sb = root->d_sb;
  364. struct squashfs_sb_info *msblk = sb->s_fs_info;
  365. if (msblk->panic_on_errors)
  366. seq_puts(s, ",errors=panic");
  367. else
  368. seq_puts(s, ",errors=continue");
  369. return 0;
  370. }
  371. static int squashfs_init_fs_context(struct fs_context *fc)
  372. {
  373. struct squashfs_mount_opts *opts;
  374. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  375. if (!opts)
  376. return -ENOMEM;
  377. fc->fs_private = opts;
  378. fc->ops = &squashfs_context_ops;
  379. return 0;
  380. }
  381. static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  382. {
  383. struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
  384. u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
  385. TRACE("Entered squashfs_statfs\n");
  386. buf->f_type = SQUASHFS_MAGIC;
  387. buf->f_bsize = msblk->block_size;
  388. buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
  389. buf->f_bfree = buf->f_bavail = 0;
  390. buf->f_files = msblk->inodes;
  391. buf->f_ffree = 0;
  392. buf->f_namelen = SQUASHFS_NAME_LEN;
  393. buf->f_fsid = u64_to_fsid(id);
  394. return 0;
  395. }
  396. static void squashfs_put_super(struct super_block *sb)
  397. {
  398. if (sb->s_fs_info) {
  399. struct squashfs_sb_info *sbi = sb->s_fs_info;
  400. squashfs_cache_delete(sbi->block_cache);
  401. squashfs_cache_delete(sbi->fragment_cache);
  402. squashfs_cache_delete(sbi->read_page);
  403. squashfs_decompressor_destroy(sbi);
  404. kfree(sbi->id_table);
  405. kfree(sbi->fragment_index);
  406. kfree(sbi->meta_index);
  407. kfree(sbi->inode_lookup_table);
  408. kfree(sbi->xattr_id_table);
  409. kfree(sb->s_fs_info);
  410. sb->s_fs_info = NULL;
  411. }
  412. }
  413. static struct kmem_cache *squashfs_inode_cachep;
  414. static void init_once(void *foo)
  415. {
  416. struct squashfs_inode_info *ei = foo;
  417. inode_init_once(&ei->vfs_inode);
  418. }
  419. static int __init init_inodecache(void)
  420. {
  421. squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
  422. sizeof(struct squashfs_inode_info), 0,
  423. SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
  424. init_once);
  425. return squashfs_inode_cachep ? 0 : -ENOMEM;
  426. }
  427. static void destroy_inodecache(void)
  428. {
  429. /*
  430. * Make sure all delayed rcu free inodes are flushed before we
  431. * destroy cache.
  432. */
  433. rcu_barrier();
  434. kmem_cache_destroy(squashfs_inode_cachep);
  435. }
  436. static int __init init_squashfs_fs(void)
  437. {
  438. int err = init_inodecache();
  439. if (err)
  440. return err;
  441. err = register_filesystem(&squashfs_fs_type);
  442. if (err) {
  443. destroy_inodecache();
  444. return err;
  445. }
  446. pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
  447. return 0;
  448. }
  449. static void __exit exit_squashfs_fs(void)
  450. {
  451. unregister_filesystem(&squashfs_fs_type);
  452. destroy_inodecache();
  453. }
  454. static struct inode *squashfs_alloc_inode(struct super_block *sb)
  455. {
  456. struct squashfs_inode_info *ei =
  457. alloc_inode_sb(sb, squashfs_inode_cachep, GFP_KERNEL);
  458. return ei ? &ei->vfs_inode : NULL;
  459. }
  460. static void squashfs_free_inode(struct inode *inode)
  461. {
  462. kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
  463. }
  464. static struct file_system_type squashfs_fs_type = {
  465. .owner = THIS_MODULE,
  466. .name = "squashfs",
  467. .init_fs_context = squashfs_init_fs_context,
  468. .parameters = squashfs_fs_parameters,
  469. .kill_sb = kill_block_super,
  470. .fs_flags = FS_REQUIRES_DEV
  471. };
  472. MODULE_ALIAS_FS("squashfs");
  473. static const struct super_operations squashfs_super_ops = {
  474. .alloc_inode = squashfs_alloc_inode,
  475. .free_inode = squashfs_free_inode,
  476. .statfs = squashfs_statfs,
  477. .put_super = squashfs_put_super,
  478. .show_options = squashfs_show_options,
  479. };
  480. module_init(init_squashfs_fs);
  481. module_exit(exit_squashfs_fs);
  482. MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
  483. MODULE_AUTHOR("Phillip Lougher <[email protected]>");
  484. MODULE_LICENSE("GPL");