compress.c 10 KB

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