compress.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -*- linux-c -*- ------------------------------------------------------- *
  3. *
  4. * Copyright 2001 H. Peter Anvin - All Rights Reserved
  5. *
  6. * ----------------------------------------------------------------------- */
  7. /*
  8. * linux/fs/isofs/compress.c
  9. *
  10. * Transparent decompression of files on an iso9660 filesystem
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/bio.h>
  15. #include <linux/slab.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/zlib.h>
  18. #include "isofs.h"
  19. #include "zisofs.h"
  20. /* This should probably be global. */
  21. static char zisofs_sink_page[PAGE_SIZE];
  22. /*
  23. * This contains the zlib memory allocation and the mutex for the
  24. * allocation; this avoids failures at block-decompression time.
  25. */
  26. static void *zisofs_zlib_workspace;
  27. static DEFINE_MUTEX(zisofs_zlib_lock);
  28. /*
  29. * Read data of @inode from @block_start to @block_end and uncompress
  30. * to one zisofs block. Store the data in the @pages array with @pcount
  31. * entries. Start storing at offset @poffset of the first page.
  32. */
  33. static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,
  34. loff_t block_end, int pcount,
  35. struct page **pages, unsigned poffset,
  36. int *errp)
  37. {
  38. unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
  39. unsigned int bufsize = ISOFS_BUFFER_SIZE(inode);
  40. unsigned int bufshift = ISOFS_BUFFER_BITS(inode);
  41. unsigned int bufmask = bufsize - 1;
  42. int i, block_size = block_end - block_start;
  43. z_stream stream = { .total_out = 0,
  44. .avail_in = 0,
  45. .avail_out = 0, };
  46. int zerr;
  47. int needblocks = (block_size + (block_start & bufmask) + bufmask)
  48. >> bufshift;
  49. int haveblocks;
  50. blkcnt_t blocknum;
  51. struct buffer_head **bhs;
  52. int curbh, curpage;
  53. if (block_size > deflateBound(1UL << zisofs_block_shift)) {
  54. *errp = -EIO;
  55. return 0;
  56. }
  57. /* Empty block? */
  58. if (block_size == 0) {
  59. for ( i = 0 ; i < pcount ; i++ ) {
  60. if (!pages[i])
  61. continue;
  62. memzero_page(pages[i], 0, PAGE_SIZE);
  63. SetPageUptodate(pages[i]);
  64. }
  65. return ((loff_t)pcount) << PAGE_SHIFT;
  66. }
  67. /* Because zlib is not thread-safe, do all the I/O at the top. */
  68. blocknum = block_start >> bufshift;
  69. bhs = kcalloc(needblocks + 1, sizeof(*bhs), GFP_KERNEL);
  70. if (!bhs) {
  71. *errp = -ENOMEM;
  72. return 0;
  73. }
  74. haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks);
  75. bh_read_batch(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 = kmap_local_page(pages[curpage])
  108. + poffset;
  109. stream.avail_out = PAGE_SIZE - poffset;
  110. poffset = 0;
  111. } else {
  112. stream.next_out = (void *)&zisofs_sink_page;
  113. stream.avail_out = PAGE_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 = %ld,"
  146. " avail_out = %ld\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. if (stream.next_out != (unsigned char *)zisofs_sink_page) {
  162. kunmap_local(stream.next_out);
  163. stream.next_out = NULL;
  164. }
  165. curpage++;
  166. }
  167. if (!stream.avail_in)
  168. curbh++;
  169. }
  170. inflate_out:
  171. zlib_inflateEnd(&stream);
  172. if (stream.next_out && stream.next_out != (unsigned char *)zisofs_sink_page)
  173. kunmap_local(stream.next_out);
  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. memzero_page(*pages, poffset, PAGE_SIZE - poffset);
  262. SetPageUptodate(*pages);
  263. }
  264. return 0;
  265. }
  266. /*
  267. * When decompressing, we typically obtain more than one page
  268. * per reference. We inject the additional pages into the page
  269. * cache as a form of readahead.
  270. */
  271. static int zisofs_read_folio(struct file *file, struct folio *folio)
  272. {
  273. struct page *page = &folio->page;
  274. struct inode *inode = file_inode(file);
  275. struct address_space *mapping = inode->i_mapping;
  276. int err;
  277. int i, pcount, full_page;
  278. unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
  279. unsigned int zisofs_pages_per_cblock =
  280. PAGE_SHIFT <= zisofs_block_shift ?
  281. (1 << (zisofs_block_shift - PAGE_SHIFT)) : 0;
  282. struct page **pages;
  283. pgoff_t index = page->index, end_index;
  284. end_index = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  285. /*
  286. * If this page is wholly outside i_size we just return zero;
  287. * do_generic_file_read() will handle this for us
  288. */
  289. if (index >= end_index) {
  290. SetPageUptodate(page);
  291. unlock_page(page);
  292. return 0;
  293. }
  294. if (PAGE_SHIFT <= zisofs_block_shift) {
  295. /* We have already been given one page, this is the one
  296. we must do. */
  297. full_page = index & (zisofs_pages_per_cblock - 1);
  298. pcount = min_t(int, zisofs_pages_per_cblock,
  299. end_index - (index & ~(zisofs_pages_per_cblock - 1)));
  300. index -= full_page;
  301. } else {
  302. full_page = 0;
  303. pcount = 1;
  304. }
  305. pages = kcalloc(max_t(unsigned int, zisofs_pages_per_cblock, 1),
  306. sizeof(*pages), GFP_KERNEL);
  307. if (!pages) {
  308. unlock_page(page);
  309. return -ENOMEM;
  310. }
  311. pages[full_page] = page;
  312. for (i = 0; i < pcount; i++, index++) {
  313. if (i != full_page)
  314. pages[i] = grab_cache_page_nowait(mapping, index);
  315. }
  316. err = zisofs_fill_pages(inode, full_page, pcount, pages);
  317. /* Release any residual pages, do not SetPageUptodate */
  318. for (i = 0; i < pcount; i++) {
  319. if (pages[i]) {
  320. flush_dcache_page(pages[i]);
  321. unlock_page(pages[i]);
  322. if (i != full_page)
  323. put_page(pages[i]);
  324. }
  325. }
  326. /* At this point, err contains 0 or -EIO depending on the "critical" page */
  327. kfree(pages);
  328. return err;
  329. }
  330. const struct address_space_operations zisofs_aops = {
  331. .read_folio = zisofs_read_folio,
  332. /* No bmap operation supported */
  333. };
  334. int __init zisofs_init(void)
  335. {
  336. zisofs_zlib_workspace = vmalloc(zlib_inflate_workspacesize());
  337. if ( !zisofs_zlib_workspace )
  338. return -ENOMEM;
  339. return 0;
  340. }
  341. void zisofs_cleanup(void)
  342. {
  343. vfree(zisofs_zlib_workspace);
  344. }