super.c 18 KB

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