compress.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright 2001 H. Peter Anvin - All Rights Reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
  8. * USA; either version 2 of the License, or (at your option) any later
  9. * version; incorporated herein by reference.
  10. *
  11. * ----------------------------------------------------------------------- */
  12. /*
  13. * linux/fs/isofs/compress.c
  14. *
  15. * Transparent decompression of files on an iso9660 filesystem
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/bio.h>
  20. #include <linux/slab.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/zlib.h>
  23. #include "isofs.h"
  24. #include "zisofs.h"
  25. /* This should probably be global. */
  26. static char zisofs_sink_page[PAGE_SIZE];
  27. /*
  28. * This contains the zlib memory allocation and the mutex for the
  29. * allocation; this avoids failures at block-decompression time.
  30. */
  31. static void *zisofs_zlib_workspace;
  32. static DEFINE_MUTEX(zisofs_zlib_lock);
  33. /*
  34. * Read data of @inode from @block_start to @block_end and uncompress
  35. * to one zisofs block. Store the data in the @pages array with @pcount
  36. * entries. Start storing at offset @poffset of the first page.
  37. */
  38. static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,
  39. loff_t block_end, int pcount,
  40. struct page **pages, unsigned poffset,
  41. int *errp)
  42. {
  43. unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
  44. unsigned int bufsize = ISOFS_BUFFER_SIZE(inode);
  45. unsigned int bufshift = ISOFS_BUFFER_BITS(inode);
  46. unsigned int bufmask = bufsize - 1;
  47. int i, block_size = block_end - block_start;
  48. z_stream stream = { .total_out = 0,
  49. .avail_in = 0,
  50. .avail_out = 0, };
  51. int zerr;
  52. int needblocks = (block_size + (block_start & bufmask) + bufmask)
  53. >> bufshift;
  54. int haveblocks;
  55. blkcnt_t blocknum;
  56. struct buffer_head **bhs;
  57. int curbh, curpage;
  58. if (block_size > deflateBound(1UL << zisofs_block_shift)) {
  59. *errp = -EIO;
  60. return 0;
  61. }
  62. /* Empty block? */
  63. if (block_size == 0) {
  64. for ( i = 0 ; i < pcount ; i++ ) {
  65. if (!pages[i])
  66. continue;
  67. memset(page_address(pages[i]), 0, PAGE_SIZE);
  68. flush_dcache_page(pages[i]);
  69. SetPageUptodate(pages[i]);
  70. }
  71. return ((loff_t)pcount) << PAGE_SHIFT;
  72. }
  73. /* Because zlib is not thread-safe, do all the I/O at the top. */
  74. blocknum = block_start >> bufshift;
  75. bhs = kcalloc(needblocks + 1, sizeof(*bhs), GFP_KERNEL);
  76. if (!bhs) {
  77. *errp = -ENOMEM;
  78. return 0;
  79. }
  80. haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks);
  81. ll_rw_block(REQ_OP_READ, 0, haveblocks, bhs);
  82. curbh = 0;
  83. curpage = 0;
  84. /*
  85. * First block is special since it may be fractional. We also wait for
  86. * it before grabbing the zlib mutex; odds are that the subsequent
  87. * blocks are going to come in in short order so we don't hold the zlib
  88. * mutex longer than necessary.
  89. */
  90. if (!bhs[0])
  91. goto b_eio;
  92. wait_on_buffer(bhs[0]);
  93. if (!buffer_uptodate(bhs[0])) {
  94. *errp = -EIO;
  95. goto b_eio;
  96. }
  97. stream.workspace = zisofs_zlib_workspace;
  98. mutex_lock(&zisofs_zlib_lock);
  99. zerr = zlib_inflateInit(&stream);
  100. if (zerr != Z_OK) {
  101. if (zerr == Z_MEM_ERROR)
  102. *errp = -ENOMEM;
  103. else
  104. *errp = -EIO;
  105. printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n",
  106. zerr);
  107. goto z_eio;
  108. }
  109. while (curpage < pcount && curbh < haveblocks &&
  110. zerr != Z_STREAM_END) {
  111. if (!stream.avail_out) {
  112. if (pages[curpage]) {
  113. stream.next_out = page_address(pages[curpage])
  114. + poffset;
  115. stream.avail_out = PAGE_SIZE - poffset;
  116. poffset = 0;
  117. } else {
  118. stream.next_out = (void *)&zisofs_sink_page;
  119. stream.avail_out = PAGE_SIZE;
  120. }
  121. }
  122. if (!stream.avail_in) {
  123. wait_on_buffer(bhs[curbh]);
  124. if (!buffer_uptodate(bhs[curbh])) {
  125. *errp = -EIO;
  126. break;
  127. }
  128. stream.next_in = bhs[curbh]->b_data +
  129. (block_start & bufmask);
  130. stream.avail_in = min_t(unsigned, bufsize -
  131. (block_start & bufmask),
  132. block_size);
  133. block_size -= stream.avail_in;
  134. block_start = 0;
  135. }
  136. while (stream.avail_out && stream.avail_in) {
  137. zerr = zlib_inflate(&stream, Z_SYNC_FLUSH);
  138. if (zerr == Z_BUF_ERROR && stream.avail_in == 0)
  139. break;
  140. if (zerr == Z_STREAM_END)
  141. break;
  142. if (zerr != Z_OK) {
  143. /* EOF, error, or trying to read beyond end of input */
  144. if (zerr == Z_MEM_ERROR)
  145. *errp = -ENOMEM;
  146. else {
  147. printk(KERN_DEBUG
  148. "zisofs: zisofs_inflate returned"
  149. " %d, inode = %lu,"
  150. " page idx = %d, bh idx = %d,"
  151. " avail_in = %ld,"
  152. " avail_out = %ld\n",
  153. zerr, inode->i_ino, curpage,
  154. curbh, stream.avail_in,
  155. stream.avail_out);
  156. *errp = -EIO;
  157. }
  158. goto inflate_out;
  159. }
  160. }
  161. if (!stream.avail_out) {
  162. /* This page completed */
  163. if (pages[curpage]) {
  164. flush_dcache_page(pages[curpage]);
  165. SetPageUptodate(pages[curpage]);
  166. }
  167. curpage++;
  168. }
  169. if (!stream.avail_in)
  170. curbh++;
  171. }
  172. inflate_out:
  173. zlib_inflateEnd(&stream);
  174. z_eio:
  175. mutex_unlock(&zisofs_zlib_lock);
  176. b_eio:
  177. for (i = 0; i < haveblocks; i++)
  178. brelse(bhs[i]);
  179. kfree(bhs);
  180. return stream.total_out;
  181. }
  182. /*
  183. * Uncompress data so that pages[full_page] is fully uptodate and possibly
  184. * fills in other pages if we have data for them.
  185. */
  186. static int zisofs_fill_pages(struct inode *inode, int full_page, int pcount,
  187. struct page **pages)
  188. {
  189. loff_t start_off, end_off;
  190. loff_t block_start, block_end;
  191. unsigned int header_size = ISOFS_I(inode)->i_format_parm[0];
  192. unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
  193. unsigned int blockptr;
  194. loff_t poffset = 0;
  195. blkcnt_t cstart_block, cend_block;
  196. struct buffer_head *bh;
  197. unsigned int blkbits = ISOFS_BUFFER_BITS(inode);
  198. unsigned int blksize = 1 << blkbits;
  199. int err;
  200. loff_t ret;
  201. BUG_ON(!pages[full_page]);
  202. /*
  203. * We want to read at least 'full_page' page. Because we have to
  204. * uncompress the whole compression block anyway, fill the surrounding
  205. * pages with the data we have anyway...
  206. */
  207. start_off = page_offset(pages[full_page]);
  208. end_off = min_t(loff_t, start_off + PAGE_SIZE, inode->i_size);
  209. cstart_block = start_off >> zisofs_block_shift;
  210. cend_block = (end_off + (1 << zisofs_block_shift) - 1)
  211. >> zisofs_block_shift;
  212. WARN_ON(start_off - (full_page << PAGE_SHIFT) !=
  213. ((cstart_block << zisofs_block_shift) & PAGE_MASK));
  214. /* Find the pointer to this specific chunk */
  215. /* Note: we're not using isonum_731() here because the data is known aligned */
  216. /* Note: header_size is in 32-bit words (4 bytes) */
  217. blockptr = (header_size + cstart_block) << 2;
  218. bh = isofs_bread(inode, blockptr >> blkbits);
  219. if (!bh)
  220. return -EIO;
  221. block_start = le32_to_cpu(*(__le32 *)
  222. (bh->b_data + (blockptr & (blksize - 1))));
  223. while (cstart_block < cend_block && pcount > 0) {
  224. /* Load end of the compressed block in the file */
  225. blockptr += 4;
  226. /* Traversed to next block? */
  227. if (!(blockptr & (blksize - 1))) {
  228. brelse(bh);
  229. bh = isofs_bread(inode, blockptr >> blkbits);
  230. if (!bh)
  231. return -EIO;
  232. }
  233. block_end = le32_to_cpu(*(__le32 *)
  234. (bh->b_data + (blockptr & (blksize - 1))));
  235. if (block_start > block_end) {
  236. brelse(bh);
  237. return -EIO;
  238. }
  239. err = 0;
  240. ret = zisofs_uncompress_block(inode, block_start, block_end,
  241. pcount, pages, poffset, &err);
  242. poffset += ret;
  243. pages += poffset >> PAGE_SHIFT;
  244. pcount -= poffset >> PAGE_SHIFT;
  245. full_page -= poffset >> PAGE_SHIFT;
  246. poffset &= ~PAGE_MASK;
  247. if (err) {
  248. brelse(bh);
  249. /*
  250. * Did we finish reading the page we really wanted
  251. * to read?
  252. */
  253. if (full_page < 0)
  254. return 0;
  255. return err;
  256. }
  257. block_start = block_end;
  258. cstart_block++;
  259. }
  260. if (poffset && *pages) {
  261. memset(page_address(*pages) + poffset, 0,
  262. PAGE_SIZE - poffset);
  263. flush_dcache_page(*pages);
  264. SetPageUptodate(*pages);
  265. }
  266. return 0;
  267. }
  268. /*
  269. * When decompressing, we typically obtain more than one page
  270. * per reference. We inject the additional pages into the page
  271. * cache as a form of readahead.
  272. */
  273. static int zisofs_readpage(struct file *file, struct page *page)
  274. {
  275. struct inode *inode = file_inode(file);
  276. struct address_space *mapping = inode->i_mapping;
  277. int err;
  278. int i, pcount, full_page;
  279. unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
  280. unsigned int zisofs_pages_per_cblock =
  281. PAGE_SHIFT <= zisofs_block_shift ?
  282. (1 << (zisofs_block_shift - PAGE_SHIFT)) : 0;
  283. struct page **pages;
  284. pgoff_t index = page->index, end_index;
  285. end_index = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  286. /*
  287. * If this page is wholly outside i_size we just return zero;
  288. * do_generic_file_read() will handle this for us
  289. */
  290. if (index >= end_index) {
  291. SetPageUptodate(page);
  292. unlock_page(page);
  293. return 0;
  294. }
  295. if (PAGE_SHIFT <= zisofs_block_shift) {
  296. /* We have already been given one page, this is the one
  297. we must do. */
  298. full_page = index & (zisofs_pages_per_cblock - 1);
  299. pcount = min_t(int, zisofs_pages_per_cblock,
  300. end_index - (index & ~(zisofs_pages_per_cblock - 1)));
  301. index -= full_page;
  302. } else {
  303. full_page = 0;
  304. pcount = 1;
  305. }
  306. pages = kcalloc(max_t(unsigned int, zisofs_pages_per_cblock, 1),
  307. sizeof(*pages), GFP_KERNEL);
  308. if (!pages) {
  309. unlock_page(page);
  310. return -ENOMEM;
  311. }
  312. pages[full_page] = page;
  313. for (i = 0; i < pcount; i++, index++) {
  314. if (i != full_page)
  315. pages[i] = grab_cache_page_nowait(mapping, index);
  316. if (pages[i]) {
  317. ClearPageError(pages[i]);
  318. kmap(pages[i]);
  319. }
  320. }
  321. err = zisofs_fill_pages(inode, full_page, pcount, pages);
  322. /* Release any residual pages, do not SetPageUptodate */
  323. for (i = 0; i < pcount; i++) {
  324. if (pages[i]) {
  325. flush_dcache_page(pages[i]);
  326. if (i == full_page && err)
  327. SetPageError(pages[i]);
  328. kunmap(pages[i]);
  329. unlock_page(pages[i]);
  330. if (i != full_page)
  331. put_page(pages[i]);
  332. }
  333. }
  334. /* At this point, err contains 0 or -EIO depending on the "critical" page */
  335. kfree(pages);
  336. return err;
  337. }
  338. const struct address_space_operations zisofs_aops = {
  339. .readpage = zisofs_readpage,
  340. /* No bmap operation supported */
  341. };
  342. int __init zisofs_init(void)
  343. {
  344. zisofs_zlib_workspace = vmalloc(zlib_inflate_workspacesize());
  345. if ( !zisofs_zlib_workspace )
  346. return -ENOMEM;
  347. return 0;
  348. }
  349. void zisofs_cleanup(void)
  350. {
  351. vfree(zisofs_zlib_workspace);
  352. }