super.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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(sizeof(*msblk), GFP_KERNEL);
  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. err = -ENOMEM;
  275. msblk->block_cache = squashfs_cache_init("metadata",
  276. SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
  277. if (msblk->block_cache == NULL)
  278. goto failed_mount;
  279. /* Allocate read_page block */
  280. msblk->read_page = squashfs_cache_init("data",
  281. msblk->max_thread_num, msblk->block_size);
  282. if (msblk->read_page == NULL) {
  283. errorf(fc, "Failed to allocate read_page block");
  284. goto failed_mount;
  285. }
  286. if (msblk->devblksize == PAGE_SIZE) {
  287. struct inode *cache = new_inode(sb);
  288. if (cache == NULL)
  289. goto failed_mount;
  290. set_nlink(cache, 1);
  291. cache->i_size = OFFSET_MAX;
  292. mapping_set_gfp_mask(cache->i_mapping, GFP_NOFS);
  293. msblk->cache_mapping = cache->i_mapping;
  294. }
  295. msblk->stream = squashfs_decompressor_setup(sb, flags);
  296. if (IS_ERR(msblk->stream)) {
  297. err = PTR_ERR(msblk->stream);
  298. msblk->stream = NULL;
  299. goto insanity;
  300. }
  301. /* Handle xattrs */
  302. sb->s_xattr = squashfs_xattr_handlers;
  303. xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
  304. if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
  305. next_table = msblk->bytes_used;
  306. goto allocate_id_index_table;
  307. }
  308. /* Allocate and read xattr id lookup table */
  309. msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
  310. xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids);
  311. if (IS_ERR(msblk->xattr_id_table)) {
  312. errorf(fc, "unable to read xattr id index table");
  313. err = PTR_ERR(msblk->xattr_id_table);
  314. msblk->xattr_id_table = NULL;
  315. if (err != -ENOTSUPP)
  316. goto failed_mount;
  317. }
  318. next_table = msblk->xattr_table;
  319. allocate_id_index_table:
  320. /* Allocate and read id index table */
  321. msblk->id_table = squashfs_read_id_index_table(sb,
  322. le64_to_cpu(sblk->id_table_start), next_table, msblk->ids);
  323. if (IS_ERR(msblk->id_table)) {
  324. errorf(fc, "unable to read id index table");
  325. err = PTR_ERR(msblk->id_table);
  326. msblk->id_table = NULL;
  327. goto failed_mount;
  328. }
  329. next_table = le64_to_cpu(msblk->id_table[0]);
  330. /* Handle inode lookup table */
  331. lookup_table_start = le64_to_cpu(sblk->lookup_table_start);
  332. if (lookup_table_start == SQUASHFS_INVALID_BLK)
  333. goto handle_fragments;
  334. /* Allocate and read inode lookup table */
  335. msblk->inode_lookup_table = squashfs_read_inode_lookup_table(sb,
  336. lookup_table_start, next_table, msblk->inodes);
  337. if (IS_ERR(msblk->inode_lookup_table)) {
  338. errorf(fc, "unable to read inode lookup table");
  339. err = PTR_ERR(msblk->inode_lookup_table);
  340. msblk->inode_lookup_table = NULL;
  341. goto failed_mount;
  342. }
  343. next_table = le64_to_cpu(msblk->inode_lookup_table[0]);
  344. sb->s_export_op = &squashfs_export_ops;
  345. handle_fragments:
  346. fragments = msblk->fragments;
  347. if (fragments == 0)
  348. goto check_directory_table;
  349. msblk->fragment_cache = squashfs_cache_init("fragment",
  350. SQUASHFS_CACHED_FRAGMENTS, msblk->block_size);
  351. if (msblk->fragment_cache == NULL) {
  352. err = -ENOMEM;
  353. goto failed_mount;
  354. }
  355. /* Allocate and read fragment index table */
  356. msblk->fragment_index = squashfs_read_fragment_index_table(sb,
  357. le64_to_cpu(sblk->fragment_table_start), next_table, fragments);
  358. if (IS_ERR(msblk->fragment_index)) {
  359. errorf(fc, "unable to read fragment index table");
  360. err = PTR_ERR(msblk->fragment_index);
  361. msblk->fragment_index = NULL;
  362. goto failed_mount;
  363. }
  364. next_table = le64_to_cpu(msblk->fragment_index[0]);
  365. check_directory_table:
  366. /* Sanity check directory_table */
  367. if (msblk->directory_table > next_table) {
  368. err = -EINVAL;
  369. goto insanity;
  370. }
  371. /* Sanity check inode_table */
  372. if (msblk->inode_table >= msblk->directory_table) {
  373. err = -EINVAL;
  374. goto insanity;
  375. }
  376. /* allocate root */
  377. root = new_inode(sb);
  378. if (!root) {
  379. err = -ENOMEM;
  380. goto failed_mount;
  381. }
  382. err = squashfs_read_inode(root, root_inode);
  383. if (err) {
  384. make_bad_inode(root);
  385. iput(root);
  386. goto failed_mount;
  387. }
  388. insert_inode_hash(root);
  389. sb->s_root = d_make_root(root);
  390. if (sb->s_root == NULL) {
  391. ERROR("Root inode create failed\n");
  392. err = -ENOMEM;
  393. goto failed_mount;
  394. }
  395. TRACE("Leaving squashfs_fill_super\n");
  396. kfree(sblk);
  397. return 0;
  398. insanity:
  399. errorf(fc, "squashfs image failed sanity check");
  400. failed_mount:
  401. squashfs_cache_delete(msblk->block_cache);
  402. squashfs_cache_delete(msblk->fragment_cache);
  403. squashfs_cache_delete(msblk->read_page);
  404. if (msblk->cache_mapping)
  405. iput(msblk->cache_mapping->host);
  406. msblk->thread_ops->destroy(msblk);
  407. kfree(msblk->inode_lookup_table);
  408. kfree(msblk->fragment_index);
  409. kfree(msblk->id_table);
  410. kfree(msblk->xattr_id_table);
  411. kfree(sb->s_fs_info);
  412. sb->s_fs_info = NULL;
  413. kfree(sblk);
  414. return err;
  415. }
  416. static int squashfs_get_tree(struct fs_context *fc)
  417. {
  418. return get_tree_bdev(fc, squashfs_fill_super);
  419. }
  420. static int squashfs_reconfigure(struct fs_context *fc)
  421. {
  422. struct super_block *sb = fc->root->d_sb;
  423. struct squashfs_sb_info *msblk = sb->s_fs_info;
  424. struct squashfs_mount_opts *opts = fc->fs_private;
  425. sync_filesystem(fc->root->d_sb);
  426. fc->sb_flags |= SB_RDONLY;
  427. msblk->panic_on_errors = (opts->errors == Opt_errors_panic);
  428. return 0;
  429. }
  430. static void squashfs_free_fs_context(struct fs_context *fc)
  431. {
  432. kfree(fc->fs_private);
  433. }
  434. static const struct fs_context_operations squashfs_context_ops = {
  435. .get_tree = squashfs_get_tree,
  436. .free = squashfs_free_fs_context,
  437. .parse_param = squashfs_parse_param,
  438. .reconfigure = squashfs_reconfigure,
  439. };
  440. static int squashfs_show_options(struct seq_file *s, struct dentry *root)
  441. {
  442. struct super_block *sb = root->d_sb;
  443. struct squashfs_sb_info *msblk = sb->s_fs_info;
  444. if (msblk->panic_on_errors)
  445. seq_puts(s, ",errors=panic");
  446. else
  447. seq_puts(s, ",errors=continue");
  448. #ifdef CONFIG_SQUASHFS_CHOICE_DECOMP_BY_MOUNT
  449. if (msblk->thread_ops == &squashfs_decompressor_single) {
  450. seq_puts(s, ",threads=single");
  451. return 0;
  452. }
  453. if (msblk->thread_ops == &squashfs_decompressor_percpu) {
  454. seq_puts(s, ",threads=percpu");
  455. return 0;
  456. }
  457. #endif
  458. #ifdef CONFIG_SQUASHFS_MOUNT_DECOMP_THREADS
  459. seq_printf(s, ",threads=%d", msblk->max_thread_num);
  460. #endif
  461. return 0;
  462. }
  463. static int squashfs_init_fs_context(struct fs_context *fc)
  464. {
  465. struct squashfs_mount_opts *opts;
  466. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  467. if (!opts)
  468. return -ENOMEM;
  469. #ifdef CONFIG_SQUASHFS_DECOMP_SINGLE
  470. opts->thread_ops = &squashfs_decompressor_single;
  471. #elif defined(CONFIG_SQUASHFS_DECOMP_MULTI)
  472. opts->thread_ops = &squashfs_decompressor_multi;
  473. #elif defined(CONFIG_SQUASHFS_DECOMP_MULTI_PERCPU)
  474. opts->thread_ops = &squashfs_decompressor_percpu;
  475. #else
  476. #error "fail: unknown squashfs decompression thread mode?"
  477. #endif
  478. opts->thread_num = 0;
  479. fc->fs_private = opts;
  480. fc->ops = &squashfs_context_ops;
  481. return 0;
  482. }
  483. static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  484. {
  485. struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
  486. u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
  487. TRACE("Entered squashfs_statfs\n");
  488. buf->f_type = SQUASHFS_MAGIC;
  489. buf->f_bsize = msblk->block_size;
  490. buf->f_blocks = ((msblk->bytes_used - 1) >> msblk->block_log) + 1;
  491. buf->f_bfree = buf->f_bavail = 0;
  492. buf->f_files = msblk->inodes;
  493. buf->f_ffree = 0;
  494. buf->f_namelen = SQUASHFS_NAME_LEN;
  495. buf->f_fsid = u64_to_fsid(id);
  496. return 0;
  497. }
  498. static void squashfs_put_super(struct super_block *sb)
  499. {
  500. if (sb->s_fs_info) {
  501. struct squashfs_sb_info *sbi = sb->s_fs_info;
  502. squashfs_cache_delete(sbi->block_cache);
  503. squashfs_cache_delete(sbi->fragment_cache);
  504. squashfs_cache_delete(sbi->read_page);
  505. if (sbi->cache_mapping)
  506. iput(sbi->cache_mapping->host);
  507. sbi->thread_ops->destroy(sbi);
  508. kfree(sbi->id_table);
  509. kfree(sbi->fragment_index);
  510. kfree(sbi->meta_index);
  511. kfree(sbi->inode_lookup_table);
  512. kfree(sbi->xattr_id_table);
  513. kfree(sb->s_fs_info);
  514. sb->s_fs_info = NULL;
  515. }
  516. }
  517. static struct kmem_cache *squashfs_inode_cachep;
  518. static void init_once(void *foo)
  519. {
  520. struct squashfs_inode_info *ei = foo;
  521. inode_init_once(&ei->vfs_inode);
  522. }
  523. static int __init init_inodecache(void)
  524. {
  525. squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
  526. sizeof(struct squashfs_inode_info), 0,
  527. SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
  528. init_once);
  529. return squashfs_inode_cachep ? 0 : -ENOMEM;
  530. }
  531. static void destroy_inodecache(void)
  532. {
  533. /*
  534. * Make sure all delayed rcu free inodes are flushed before we
  535. * destroy cache.
  536. */
  537. rcu_barrier();
  538. kmem_cache_destroy(squashfs_inode_cachep);
  539. }
  540. static int __init init_squashfs_fs(void)
  541. {
  542. int err = init_inodecache();
  543. if (err)
  544. return err;
  545. err = register_filesystem(&squashfs_fs_type);
  546. if (err) {
  547. destroy_inodecache();
  548. return err;
  549. }
  550. pr_info("version 4.0 (2009/01/31) Phillip Lougher\n");
  551. return 0;
  552. }
  553. static void __exit exit_squashfs_fs(void)
  554. {
  555. unregister_filesystem(&squashfs_fs_type);
  556. destroy_inodecache();
  557. }
  558. static struct inode *squashfs_alloc_inode(struct super_block *sb)
  559. {
  560. struct squashfs_inode_info *ei =
  561. alloc_inode_sb(sb, squashfs_inode_cachep, GFP_KERNEL);
  562. return ei ? &ei->vfs_inode : NULL;
  563. }
  564. static void squashfs_free_inode(struct inode *inode)
  565. {
  566. kmem_cache_free(squashfs_inode_cachep, squashfs_i(inode));
  567. }
  568. static struct file_system_type squashfs_fs_type = {
  569. .owner = THIS_MODULE,
  570. .name = "squashfs",
  571. .init_fs_context = squashfs_init_fs_context,
  572. .parameters = squashfs_fs_parameters,
  573. .kill_sb = kill_block_super,
  574. .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
  575. };
  576. MODULE_ALIAS_FS("squashfs");
  577. static const struct super_operations squashfs_super_ops = {
  578. .alloc_inode = squashfs_alloc_inode,
  579. .free_inode = squashfs_free_inode,
  580. .statfs = squashfs_statfs,
  581. .put_super = squashfs_put_super,
  582. .show_options = squashfs_show_options,
  583. };
  584. module_init(init_squashfs_fs);
  585. module_exit(exit_squashfs_fs);
  586. MODULE_DESCRIPTION("squashfs 4.0, a compressed read-only filesystem");
  587. MODULE_AUTHOR("Phillip Lougher <phillip@squashfs.org.uk>");
  588. MODULE_LICENSE("GPL");