original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
10 KiB

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