zlib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Oracle. All rights reserved.
  4. *
  5. * Based on jffs2 zlib code:
  6. * Copyright © 2001-2007 Red Hat, Inc.
  7. * Created by David Woodhouse <dwmw2@infradead.org>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/zlib.h>
  12. #include <linux/zutil.h>
  13. #include <linux/mm.h>
  14. #include <linux/init.h>
  15. #include <linux/err.h>
  16. #include <linux/sched.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/bio.h>
  19. #include <linux/refcount.h>
  20. #include "btrfs_inode.h"
  21. #include "compression.h"
  22. #include "fs.h"
  23. #include "subpage.h"
  24. /* workspace buffer size for s390 zlib hardware support */
  25. #define ZLIB_DFLTCC_BUF_SIZE (4 * PAGE_SIZE)
  26. struct workspace {
  27. z_stream strm;
  28. char *buf;
  29. unsigned int buf_size;
  30. struct list_head list;
  31. int level;
  32. };
  33. static struct workspace_manager wsm;
  34. struct list_head *zlib_get_workspace(unsigned int level)
  35. {
  36. struct list_head *ws = btrfs_get_workspace(BTRFS_COMPRESS_ZLIB, level);
  37. struct workspace *workspace = list_entry(ws, struct workspace, list);
  38. workspace->level = level;
  39. return ws;
  40. }
  41. void zlib_free_workspace(struct list_head *ws)
  42. {
  43. struct workspace *workspace = list_entry(ws, struct workspace, list);
  44. kvfree(workspace->strm.workspace);
  45. kfree(workspace->buf);
  46. kfree(workspace);
  47. }
  48. struct list_head *zlib_alloc_workspace(unsigned int level)
  49. {
  50. struct workspace *workspace;
  51. int workspacesize;
  52. workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
  53. if (!workspace)
  54. return ERR_PTR(-ENOMEM);
  55. workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
  56. zlib_inflate_workspacesize());
  57. workspace->strm.workspace = kvzalloc(workspacesize, GFP_KERNEL | __GFP_NOWARN);
  58. workspace->level = level;
  59. workspace->buf = NULL;
  60. /*
  61. * In case of s390 zlib hardware support, allocate lager workspace
  62. * buffer. If allocator fails, fall back to a single page buffer.
  63. */
  64. if (zlib_deflate_dfltcc_enabled()) {
  65. workspace->buf = kmalloc(ZLIB_DFLTCC_BUF_SIZE,
  66. __GFP_NOMEMALLOC | __GFP_NORETRY |
  67. __GFP_NOWARN | GFP_NOIO);
  68. workspace->buf_size = ZLIB_DFLTCC_BUF_SIZE;
  69. }
  70. if (!workspace->buf) {
  71. workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  72. workspace->buf_size = PAGE_SIZE;
  73. }
  74. if (!workspace->strm.workspace || !workspace->buf)
  75. goto fail;
  76. INIT_LIST_HEAD(&workspace->list);
  77. return &workspace->list;
  78. fail:
  79. zlib_free_workspace(&workspace->list);
  80. return ERR_PTR(-ENOMEM);
  81. }
  82. int zlib_compress_folios(struct list_head *ws, struct address_space *mapping,
  83. u64 start, struct folio **folios, unsigned long *out_folios,
  84. unsigned long *total_in, unsigned long *total_out)
  85. {
  86. struct workspace *workspace = list_entry(ws, struct workspace, list);
  87. int ret;
  88. char *data_in = NULL;
  89. char *cfolio_out;
  90. int nr_folios = 0;
  91. struct folio *in_folio = NULL;
  92. struct folio *out_folio = NULL;
  93. unsigned long bytes_left;
  94. unsigned int in_buf_folios;
  95. unsigned long len = *total_out;
  96. unsigned long nr_dest_folios = *out_folios;
  97. const unsigned long max_out = nr_dest_folios * PAGE_SIZE;
  98. const u64 orig_end = start + len;
  99. *out_folios = 0;
  100. *total_out = 0;
  101. *total_in = 0;
  102. ret = zlib_deflateInit(&workspace->strm, workspace->level);
  103. if (unlikely(ret != Z_OK)) {
  104. struct btrfs_inode *inode = BTRFS_I(mapping->host);
  105. btrfs_err(inode->root->fs_info,
  106. "zlib compression init failed, error %d root %llu inode %llu offset %llu",
  107. ret, btrfs_root_id(inode->root), btrfs_ino(inode), start);
  108. ret = -EIO;
  109. goto out;
  110. }
  111. workspace->strm.total_in = 0;
  112. workspace->strm.total_out = 0;
  113. out_folio = btrfs_alloc_compr_folio();
  114. if (out_folio == NULL) {
  115. ret = -ENOMEM;
  116. goto out;
  117. }
  118. cfolio_out = folio_address(out_folio);
  119. folios[0] = out_folio;
  120. nr_folios = 1;
  121. workspace->strm.next_in = workspace->buf;
  122. workspace->strm.avail_in = 0;
  123. workspace->strm.next_out = cfolio_out;
  124. workspace->strm.avail_out = PAGE_SIZE;
  125. while (workspace->strm.total_in < len) {
  126. /*
  127. * Get next input pages and copy the contents to
  128. * the workspace buffer if required.
  129. */
  130. if (workspace->strm.avail_in == 0) {
  131. bytes_left = len - workspace->strm.total_in;
  132. in_buf_folios = min(DIV_ROUND_UP(bytes_left, PAGE_SIZE),
  133. workspace->buf_size / PAGE_SIZE);
  134. if (in_buf_folios > 1) {
  135. int i;
  136. /* S390 hardware acceleration path, not subpage. */
  137. ASSERT(!btrfs_is_subpage(
  138. inode_to_fs_info(mapping->host),
  139. mapping));
  140. for (i = 0; i < in_buf_folios; i++) {
  141. if (data_in) {
  142. kunmap_local(data_in);
  143. folio_put(in_folio);
  144. data_in = NULL;
  145. }
  146. ret = btrfs_compress_filemap_get_folio(mapping,
  147. start, &in_folio);
  148. if (ret < 0)
  149. goto out;
  150. data_in = kmap_local_folio(in_folio, 0);
  151. copy_page(workspace->buf + i * PAGE_SIZE,
  152. data_in);
  153. start += PAGE_SIZE;
  154. }
  155. workspace->strm.next_in = workspace->buf;
  156. workspace->strm.avail_in = min(bytes_left,
  157. in_buf_folios << PAGE_SHIFT);
  158. } else {
  159. unsigned int pg_off;
  160. unsigned int cur_len;
  161. if (data_in) {
  162. kunmap_local(data_in);
  163. folio_put(in_folio);
  164. data_in = NULL;
  165. }
  166. ret = btrfs_compress_filemap_get_folio(mapping,
  167. start, &in_folio);
  168. if (ret < 0)
  169. goto out;
  170. pg_off = offset_in_page(start);
  171. cur_len = btrfs_calc_input_length(orig_end, start);
  172. data_in = kmap_local_folio(in_folio, pg_off);
  173. start += PAGE_SIZE;
  174. workspace->strm.next_in = data_in;
  175. workspace->strm.avail_in = cur_len;
  176. }
  177. }
  178. ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
  179. if (unlikely(ret != Z_OK)) {
  180. struct btrfs_inode *inode = BTRFS_I(mapping->host);
  181. btrfs_warn(inode->root->fs_info,
  182. "zlib compression failed, error %d root %llu inode %llu offset %llu",
  183. ret, btrfs_root_id(inode->root), btrfs_ino(inode),
  184. start);
  185. zlib_deflateEnd(&workspace->strm);
  186. ret = -EIO;
  187. goto out;
  188. }
  189. /* we're making it bigger, give up */
  190. if (workspace->strm.total_in > 8192 &&
  191. workspace->strm.total_in <
  192. workspace->strm.total_out) {
  193. ret = -E2BIG;
  194. goto out;
  195. }
  196. /* we need another page for writing out. Test this
  197. * before the total_in so we will pull in a new page for
  198. * the stream end if required
  199. */
  200. if (workspace->strm.avail_out == 0) {
  201. if (nr_folios == nr_dest_folios) {
  202. ret = -E2BIG;
  203. goto out;
  204. }
  205. out_folio = btrfs_alloc_compr_folio();
  206. if (out_folio == NULL) {
  207. ret = -ENOMEM;
  208. goto out;
  209. }
  210. cfolio_out = folio_address(out_folio);
  211. folios[nr_folios] = out_folio;
  212. nr_folios++;
  213. workspace->strm.avail_out = PAGE_SIZE;
  214. workspace->strm.next_out = cfolio_out;
  215. }
  216. /* we're all done */
  217. if (workspace->strm.total_in >= len)
  218. break;
  219. if (workspace->strm.total_out > max_out)
  220. break;
  221. }
  222. workspace->strm.avail_in = 0;
  223. /*
  224. * Call deflate with Z_FINISH flush parameter providing more output
  225. * space but no more input data, until it returns with Z_STREAM_END.
  226. */
  227. while (ret != Z_STREAM_END) {
  228. ret = zlib_deflate(&workspace->strm, Z_FINISH);
  229. if (ret == Z_STREAM_END)
  230. break;
  231. if (ret != Z_OK && ret != Z_BUF_ERROR) {
  232. zlib_deflateEnd(&workspace->strm);
  233. ret = -EIO;
  234. goto out;
  235. } else if (workspace->strm.avail_out == 0) {
  236. /* Get another folio for the stream end. */
  237. if (nr_folios == nr_dest_folios) {
  238. ret = -E2BIG;
  239. goto out;
  240. }
  241. out_folio = btrfs_alloc_compr_folio();
  242. if (out_folio == NULL) {
  243. ret = -ENOMEM;
  244. goto out;
  245. }
  246. cfolio_out = folio_address(out_folio);
  247. folios[nr_folios] = out_folio;
  248. nr_folios++;
  249. workspace->strm.avail_out = PAGE_SIZE;
  250. workspace->strm.next_out = cfolio_out;
  251. }
  252. }
  253. zlib_deflateEnd(&workspace->strm);
  254. if (workspace->strm.total_out >= workspace->strm.total_in) {
  255. ret = -E2BIG;
  256. goto out;
  257. }
  258. ret = 0;
  259. *total_out = workspace->strm.total_out;
  260. *total_in = workspace->strm.total_in;
  261. out:
  262. *out_folios = nr_folios;
  263. if (data_in) {
  264. kunmap_local(data_in);
  265. folio_put(in_folio);
  266. }
  267. return ret;
  268. }
  269. int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
  270. {
  271. struct workspace *workspace = list_entry(ws, struct workspace, list);
  272. int ret = 0, ret2;
  273. int wbits = MAX_WBITS;
  274. char *data_in;
  275. size_t total_out = 0;
  276. unsigned long folio_in_index = 0;
  277. size_t srclen = cb->compressed_len;
  278. unsigned long total_folios_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
  279. unsigned long buf_start;
  280. struct folio **folios_in = cb->compressed_folios;
  281. data_in = kmap_local_folio(folios_in[folio_in_index], 0);
  282. workspace->strm.next_in = data_in;
  283. workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE);
  284. workspace->strm.total_in = 0;
  285. workspace->strm.total_out = 0;
  286. workspace->strm.next_out = workspace->buf;
  287. workspace->strm.avail_out = workspace->buf_size;
  288. /* If it's deflate, and it's got no preset dictionary, then
  289. we can tell zlib to skip the adler32 check. */
  290. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  291. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  292. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  293. wbits = -((data_in[0] >> 4) + 8);
  294. workspace->strm.next_in += 2;
  295. workspace->strm.avail_in -= 2;
  296. }
  297. ret = zlib_inflateInit2(&workspace->strm, wbits);
  298. if (unlikely(ret != Z_OK)) {
  299. struct btrfs_inode *inode = cb->bbio.inode;
  300. kunmap_local(data_in);
  301. btrfs_err(inode->root->fs_info,
  302. "zlib decompression init failed, error %d root %llu inode %llu offset %llu",
  303. ret, btrfs_root_id(inode->root), btrfs_ino(inode), cb->start);
  304. return -EIO;
  305. }
  306. while (workspace->strm.total_in < srclen) {
  307. ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
  308. if (ret != Z_OK && ret != Z_STREAM_END)
  309. break;
  310. buf_start = total_out;
  311. total_out = workspace->strm.total_out;
  312. /* we didn't make progress in this inflate call, we're done */
  313. if (buf_start == total_out)
  314. break;
  315. ret2 = btrfs_decompress_buf2page(workspace->buf,
  316. total_out - buf_start, cb, buf_start);
  317. if (ret2 == 0) {
  318. ret = 0;
  319. goto done;
  320. }
  321. workspace->strm.next_out = workspace->buf;
  322. workspace->strm.avail_out = workspace->buf_size;
  323. if (workspace->strm.avail_in == 0) {
  324. unsigned long tmp;
  325. kunmap_local(data_in);
  326. folio_in_index++;
  327. if (folio_in_index >= total_folios_in) {
  328. data_in = NULL;
  329. break;
  330. }
  331. data_in = kmap_local_folio(folios_in[folio_in_index], 0);
  332. workspace->strm.next_in = data_in;
  333. tmp = srclen - workspace->strm.total_in;
  334. workspace->strm.avail_in = min(tmp, PAGE_SIZE);
  335. }
  336. }
  337. if (unlikely(ret != Z_STREAM_END)) {
  338. btrfs_err(cb->bbio.inode->root->fs_info,
  339. "zlib decompression failed, error %d root %llu inode %llu offset %llu",
  340. ret, btrfs_root_id(cb->bbio.inode->root),
  341. btrfs_ino(cb->bbio.inode), cb->start);
  342. ret = -EIO;
  343. } else {
  344. ret = 0;
  345. }
  346. done:
  347. zlib_inflateEnd(&workspace->strm);
  348. if (data_in)
  349. kunmap_local(data_in);
  350. return ret;
  351. }
  352. int zlib_decompress(struct list_head *ws, const u8 *data_in,
  353. struct folio *dest_folio, unsigned long dest_pgoff, size_t srclen,
  354. size_t destlen)
  355. {
  356. struct workspace *workspace = list_entry(ws, struct workspace, list);
  357. int ret = 0;
  358. int wbits = MAX_WBITS;
  359. unsigned long to_copy;
  360. workspace->strm.next_in = data_in;
  361. workspace->strm.avail_in = srclen;
  362. workspace->strm.total_in = 0;
  363. workspace->strm.next_out = workspace->buf;
  364. workspace->strm.avail_out = workspace->buf_size;
  365. workspace->strm.total_out = 0;
  366. /* If it's deflate, and it's got no preset dictionary, then
  367. we can tell zlib to skip the adler32 check. */
  368. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  369. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  370. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  371. wbits = -((data_in[0] >> 4) + 8);
  372. workspace->strm.next_in += 2;
  373. workspace->strm.avail_in -= 2;
  374. }
  375. ret = zlib_inflateInit2(&workspace->strm, wbits);
  376. if (unlikely(ret != Z_OK)) {
  377. struct btrfs_inode *inode = folio_to_inode(dest_folio);
  378. btrfs_err(inode->root->fs_info,
  379. "zlib decompression init failed, error %d root %llu inode %llu offset %llu",
  380. ret, btrfs_root_id(inode->root), btrfs_ino(inode),
  381. folio_pos(dest_folio));
  382. return -EIO;
  383. }
  384. /*
  385. * Everything (in/out buf) should be at most one sector, there should
  386. * be no need to switch any input/output buffer.
  387. */
  388. ret = zlib_inflate(&workspace->strm, Z_FINISH);
  389. to_copy = min(workspace->strm.total_out, destlen);
  390. if (ret != Z_STREAM_END)
  391. goto out;
  392. memcpy_to_folio(dest_folio, dest_pgoff, workspace->buf, to_copy);
  393. out:
  394. if (unlikely(to_copy != destlen)) {
  395. struct btrfs_inode *inode = folio_to_inode(dest_folio);
  396. btrfs_err(inode->root->fs_info,
  397. "zlib decompression failed, error %d root %llu inode %llu offset %llu decompressed %lu expected %zu",
  398. ret, btrfs_root_id(inode->root), btrfs_ino(inode),
  399. folio_pos(dest_folio), to_copy, destlen);
  400. ret = -EIO;
  401. } else {
  402. ret = 0;
  403. }
  404. zlib_inflateEnd(&workspace->strm);
  405. if (unlikely(to_copy < destlen))
  406. folio_zero_range(dest_folio, dest_pgoff + to_copy, destlen - to_copy);
  407. return ret;
  408. }
  409. const struct btrfs_compress_op btrfs_zlib_compress = {
  410. .workspace_manager = &wsm,
  411. .max_level = 9,
  412. .default_level = BTRFS_ZLIB_DEFAULT_LEVEL,
  413. };