003-squashfs_add_decompressor_framework.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. From 327fbf47a419befc6bff74f3ca42d2b6f0841903 Mon Sep 17 00:00:00 2001
  2. From: Phillip Lougher <[email protected]>
  3. Date: Tue, 6 Oct 2009 04:04:15 +0100
  4. Subject: [PATCH] Squashfs: add a decompressor framework
  5. This adds a decompressor framework which allows multiple compression
  6. algorithms to be cleanly supported.
  7. Also update zlib wrapper and other code to use the new framework.
  8. Signed-off-by: Phillip Lougher <[email protected]>
  9. ---
  10. fs/squashfs/Makefile | 2 +-
  11. fs/squashfs/block.c | 6 ++--
  12. fs/squashfs/decompressor.c | 58 ++++++++++++++++++++++++++++++++++++++++++
  13. fs/squashfs/decompressor.h | 55 +++++++++++++++++++++++++++++++++++++++
  14. fs/squashfs/squashfs.h | 14 +++++-----
  15. fs/squashfs/squashfs_fs_sb.h | 41 +++++++++++++++--------------
  16. fs/squashfs/super.c | 45 ++++++++++++++++++-------------
  17. fs/squashfs/zlib_wrapper.c | 17 ++++++++++--
  18. 8 files changed, 185 insertions(+), 53 deletions(-)
  19. create mode 100644 fs/squashfs/decompressor.c
  20. create mode 100644 fs/squashfs/decompressor.h
  21. --- a/fs/squashfs/Makefile
  22. +++ b/fs/squashfs/Makefile
  23. @@ -4,4 +4,4 @@
  24. obj-$(CONFIG_SQUASHFS) += squashfs.o
  25. squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o
  26. -squashfs-y += namei.o super.o symlink.o zlib_wrapper.o
  27. +squashfs-y += namei.o super.o symlink.o zlib_wrapper.o decompressor.o
  28. --- a/fs/squashfs/block.c
  29. +++ b/fs/squashfs/block.c
  30. @@ -36,7 +36,7 @@
  31. #include "squashfs_fs_sb.h"
  32. #include "squashfs_fs_i.h"
  33. #include "squashfs.h"
  34. -
  35. +#include "decompressor.h"
  36. /*
  37. * Read the metadata block length, this is stored in the first two
  38. * bytes of the metadata block.
  39. @@ -151,8 +151,8 @@ int squashfs_read_data(struct super_bloc
  40. }
  41. if (compressed) {
  42. - length = zlib_uncompress(msblk, buffer, bh, b, offset, length,
  43. - srclength, pages);
  44. + length = squashfs_decompress(msblk, buffer, bh, b, offset,
  45. + length, srclength, pages);
  46. if (length < 0)
  47. goto read_failure;
  48. } else {
  49. --- /dev/null
  50. +++ b/fs/squashfs/decompressor.c
  51. @@ -0,0 +1,58 @@
  52. +/*
  53. + * Squashfs - a compressed read only filesystem for Linux
  54. + *
  55. + * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
  56. + * Phillip Lougher <[email protected]>
  57. + *
  58. + * This program is free software; you can redistribute it and/or
  59. + * modify it under the terms of the GNU General Public License
  60. + * as published by the Free Software Foundation; either version 2,
  61. + * or (at your option) any later version.
  62. + *
  63. + * This program is distributed in the hope that it will be useful,
  64. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  65. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  66. + * GNU General Public License for more details.
  67. + *
  68. + * You should have received a copy of the GNU General Public License
  69. + * along with this program; if not, write to the Free Software
  70. + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  71. + *
  72. + * decompressor.c
  73. + */
  74. +
  75. +#include <linux/types.h>
  76. +#include <linux/mutex.h>
  77. +#include <linux/buffer_head.h>
  78. +
  79. +#include "squashfs_fs.h"
  80. +#include "squashfs_fs_sb.h"
  81. +#include "squashfs_fs_i.h"
  82. +#include "decompressor.h"
  83. +#include "squashfs.h"
  84. +
  85. +/*
  86. + * This file (and decompressor.h) implements a decompressor framework for
  87. + * Squashfs, allowing multiple decompressors to be easily supported
  88. + */
  89. +
  90. +static const struct squashfs_decompressor squashfs_unknown_comp_ops = {
  91. + NULL, NULL, NULL, 0, "unknown", 0
  92. +};
  93. +
  94. +static const struct squashfs_decompressor *decompressor[] = {
  95. + &squashfs_zlib_comp_ops,
  96. + &squashfs_unknown_comp_ops
  97. +};
  98. +
  99. +
  100. +const struct squashfs_decompressor *squashfs_lookup_decompressor(int id)
  101. +{
  102. + int i;
  103. +
  104. + for (i = 0; decompressor[i]->id; i++)
  105. + if (id == decompressor[i]->id)
  106. + break;
  107. +
  108. + return decompressor[i];
  109. +}
  110. --- /dev/null
  111. +++ b/fs/squashfs/decompressor.h
  112. @@ -0,0 +1,55 @@
  113. +#ifndef DECOMPRESSOR_H
  114. +#define DECOMPRESSOR_H
  115. +/*
  116. + * Squashfs - a compressed read only filesystem for Linux
  117. + *
  118. + * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
  119. + * Phillip Lougher <[email protected]>
  120. + *
  121. + * This program is free software; you can redistribute it and/or
  122. + * modify it under the terms of the GNU General Public License
  123. + * as published by the Free Software Foundation; either version 2,
  124. + * or (at your option) any later version.
  125. + *
  126. + * This program is distributed in the hope that it will be useful,
  127. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  128. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  129. + * GNU General Public License for more details.
  130. + *
  131. + * You should have received a copy of the GNU General Public License
  132. + * along with this program; if not, write to the Free Software
  133. + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  134. + *
  135. + * decompressor.h
  136. + */
  137. +
  138. +struct squashfs_decompressor {
  139. + void *(*init)(void);
  140. + void (*free)(void *);
  141. + int (*decompress)(struct squashfs_sb_info *, void **,
  142. + struct buffer_head **, int, int, int, int, int);
  143. + int id;
  144. + char *name;
  145. + int supported;
  146. +};
  147. +
  148. +static inline void *squashfs_decompressor_init(struct squashfs_sb_info *msblk)
  149. +{
  150. + return msblk->decompressor->init();
  151. +}
  152. +
  153. +static inline void squashfs_decompressor_free(struct squashfs_sb_info *msblk,
  154. + void *s)
  155. +{
  156. + if (msblk->decompressor)
  157. + msblk->decompressor->free(s);
  158. +}
  159. +
  160. +static inline int squashfs_decompress(struct squashfs_sb_info *msblk,
  161. + void **buffer, struct buffer_head **bh, int b, int offset, int length,
  162. + int srclength, int pages)
  163. +{
  164. + return msblk->decompressor->decompress(msblk, buffer, bh, b, offset,
  165. + length, srclength, pages);
  166. +}
  167. +#endif
  168. --- a/fs/squashfs/squashfs.h
  169. +++ b/fs/squashfs/squashfs.h
  170. @@ -51,6 +51,9 @@ extern struct squashfs_cache_entry *squa
  171. u64, int);
  172. extern int squashfs_read_table(struct super_block *, void *, u64, int);
  173. +/* decompressor.c */
  174. +extern const struct squashfs_decompressor *squashfs_lookup_decompressor(int);
  175. +
  176. /* export.c */
  177. extern __le64 *squashfs_read_inode_lookup_table(struct super_block *, u64,
  178. unsigned int);
  179. @@ -70,14 +73,8 @@ extern struct inode *squashfs_iget(struc
  180. unsigned int);
  181. extern int squashfs_read_inode(struct inode *, long long);
  182. -/* zlib_wrapper.c */
  183. -extern void *zlib_init(void);
  184. -extern void zlib_free(void *);
  185. -extern int zlib_uncompress(struct squashfs_sb_info *, void **,
  186. - struct buffer_head **, int, int, int, int, int);
  187. -
  188. /*
  189. - * Inodes and files operations
  190. + * Inodes, files and decompressor operations
  191. */
  192. /* dir.c */
  193. @@ -94,3 +91,6 @@ extern const struct inode_operations squ
  194. /* symlink.c */
  195. extern const struct address_space_operations squashfs_symlink_aops;
  196. +
  197. +/* zlib_wrapper.c */
  198. +extern const struct squashfs_decompressor squashfs_zlib_comp_ops;
  199. --- a/fs/squashfs/squashfs_fs_sb.h
  200. +++ b/fs/squashfs/squashfs_fs_sb.h
  201. @@ -52,25 +52,26 @@ struct squashfs_cache_entry {
  202. };
  203. struct squashfs_sb_info {
  204. - int devblksize;
  205. - int devblksize_log2;
  206. - struct squashfs_cache *block_cache;
  207. - struct squashfs_cache *fragment_cache;
  208. - struct squashfs_cache *read_page;
  209. - int next_meta_index;
  210. - __le64 *id_table;
  211. - __le64 *fragment_index;
  212. - unsigned int *fragment_index_2;
  213. - struct mutex read_data_mutex;
  214. - struct mutex meta_index_mutex;
  215. - struct meta_index *meta_index;
  216. - void *stream;
  217. - __le64 *inode_lookup_table;
  218. - u64 inode_table;
  219. - u64 directory_table;
  220. - unsigned int block_size;
  221. - unsigned short block_log;
  222. - long long bytes_used;
  223. - unsigned int inodes;
  224. + const struct squashfs_decompressor *decompressor;
  225. + int devblksize;
  226. + int devblksize_log2;
  227. + struct squashfs_cache *block_cache;
  228. + struct squashfs_cache *fragment_cache;
  229. + struct squashfs_cache *read_page;
  230. + int next_meta_index;
  231. + __le64 *id_table;
  232. + __le64 *fragment_index;
  233. + unsigned int *fragment_index_2;
  234. + struct mutex read_data_mutex;
  235. + struct mutex meta_index_mutex;
  236. + struct meta_index *meta_index;
  237. + void *stream;
  238. + __le64 *inode_lookup_table;
  239. + u64 inode_table;
  240. + u64 directory_table;
  241. + unsigned int block_size;
  242. + unsigned short block_log;
  243. + long long bytes_used;
  244. + unsigned int inodes;
  245. };
  246. #endif
  247. --- a/fs/squashfs/super.c
  248. +++ b/fs/squashfs/super.c
  249. @@ -41,27 +41,35 @@
  250. #include "squashfs_fs_sb.h"
  251. #include "squashfs_fs_i.h"
  252. #include "squashfs.h"
  253. +#include "decompressor.h"
  254. static struct file_system_type squashfs_fs_type;
  255. static const struct super_operations squashfs_super_ops;
  256. -static int supported_squashfs_filesystem(short major, short minor, short comp)
  257. +static const struct squashfs_decompressor *supported_squashfs_filesystem(short
  258. + major, short minor, short id)
  259. {
  260. + const struct squashfs_decompressor *decompressor;
  261. +
  262. if (major < SQUASHFS_MAJOR) {
  263. ERROR("Major/Minor mismatch, older Squashfs %d.%d "
  264. "filesystems are unsupported\n", major, minor);
  265. - return -EINVAL;
  266. + return NULL;
  267. } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
  268. ERROR("Major/Minor mismatch, trying to mount newer "
  269. "%d.%d filesystem\n", major, minor);
  270. ERROR("Please update your kernel\n");
  271. - return -EINVAL;
  272. + return NULL;
  273. }
  274. - if (comp != ZLIB_COMPRESSION)
  275. - return -EINVAL;
  276. + decompressor = squashfs_lookup_decompressor(id);
  277. + if (!decompressor->supported) {
  278. + ERROR("Filesystem uses \"%s\" compression. This is not "
  279. + "supported\n", decompressor->name);
  280. + return NULL;
  281. + }
  282. - return 0;
  283. + return decompressor;
  284. }
  285. @@ -86,10 +94,6 @@ static int squashfs_fill_super(struct su
  286. }
  287. msblk = sb->s_fs_info;
  288. - msblk->stream = zlib_init();
  289. - if (msblk->stream == NULL)
  290. - goto failure;
  291. -
  292. sblk = kzalloc(sizeof(*sblk), GFP_KERNEL);
  293. if (sblk == NULL) {
  294. ERROR("Failed to allocate squashfs_super_block\n");
  295. @@ -116,25 +120,25 @@ static int squashfs_fill_super(struct su
  296. goto failed_mount;
  297. }
  298. + err = -EINVAL;
  299. +
  300. /* Check it is a SQUASHFS superblock */
  301. sb->s_magic = le32_to_cpu(sblk->s_magic);
  302. if (sb->s_magic != SQUASHFS_MAGIC) {
  303. if (!silent)
  304. ERROR("Can't find a SQUASHFS superblock on %s\n",
  305. bdevname(sb->s_bdev, b));
  306. - err = -EINVAL;
  307. goto failed_mount;
  308. }
  309. - /* Check the MAJOR & MINOR versions and compression type */
  310. - err = supported_squashfs_filesystem(le16_to_cpu(sblk->s_major),
  311. + /* Check the MAJOR & MINOR versions and lookup compression type */
  312. + msblk->decompressor = supported_squashfs_filesystem(
  313. + le16_to_cpu(sblk->s_major),
  314. le16_to_cpu(sblk->s_minor),
  315. le16_to_cpu(sblk->compression));
  316. - if (err < 0)
  317. + if (msblk->decompressor == NULL)
  318. goto failed_mount;
  319. - err = -EINVAL;
  320. -
  321. /*
  322. * Check if there's xattrs in the filesystem. These are not
  323. * supported in this version, so warn that they will be ignored.
  324. @@ -201,6 +205,10 @@ static int squashfs_fill_super(struct su
  325. err = -ENOMEM;
  326. + msblk->stream = squashfs_decompressor_init(msblk);
  327. + if (msblk->stream == NULL)
  328. + goto failed_mount;
  329. +
  330. msblk->block_cache = squashfs_cache_init("metadata",
  331. SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
  332. if (msblk->block_cache == NULL)
  333. @@ -288,7 +296,7 @@ failed_mount:
  334. squashfs_cache_delete(msblk->block_cache);
  335. squashfs_cache_delete(msblk->fragment_cache);
  336. squashfs_cache_delete(msblk->read_page);
  337. - zlib_free(msblk->stream);
  338. + squashfs_decompressor_free(msblk, msblk->stream);
  339. kfree(msblk->inode_lookup_table);
  340. kfree(msblk->fragment_index);
  341. kfree(msblk->id_table);
  342. @@ -298,7 +306,6 @@ failed_mount:
  343. return err;
  344. failure:
  345. - zlib_free(msblk->stream);
  346. kfree(sb->s_fs_info);
  347. sb->s_fs_info = NULL;
  348. return -ENOMEM;
  349. @@ -342,7 +349,7 @@ static void squashfs_put_super(struct su
  350. squashfs_cache_delete(sbi->block_cache);
  351. squashfs_cache_delete(sbi->fragment_cache);
  352. squashfs_cache_delete(sbi->read_page);
  353. - zlib_free(sbi->stream);
  354. + squashfs_decompressor_free(sbi, sbi->stream);
  355. kfree(sbi->id_table);
  356. kfree(sbi->fragment_index);
  357. kfree(sbi->meta_index);
  358. --- a/fs/squashfs/zlib_wrapper.c
  359. +++ b/fs/squashfs/zlib_wrapper.c
  360. @@ -30,8 +30,9 @@
  361. #include "squashfs_fs_sb.h"
  362. #include "squashfs_fs_i.h"
  363. #include "squashfs.h"
  364. +#include "decompressor.h"
  365. -void *zlib_init()
  366. +static void *zlib_init(void)
  367. {
  368. z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
  369. if (stream == NULL)
  370. @@ -50,7 +51,7 @@ failed:
  371. }
  372. -void zlib_free(void *strm)
  373. +static void zlib_free(void *strm)
  374. {
  375. z_stream *stream = strm;
  376. @@ -60,7 +61,7 @@ void zlib_free(void *strm)
  377. }
  378. -int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
  379. +static int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
  380. struct buffer_head **bh, int b, int offset, int length, int srclength,
  381. int pages)
  382. {
  383. @@ -137,3 +138,13 @@ release_mutex:
  384. return -EIO;
  385. }
  386. +
  387. +const struct squashfs_decompressor squashfs_zlib_comp_ops = {
  388. + .init = zlib_init,
  389. + .free = zlib_free,
  390. + .decompress = zlib_uncompress,
  391. + .id = ZLIB_COMPRESSION,
  392. + .name = "zlib",
  393. + .supported = 1
  394. +};
  395. +