xref: /linux/fs/f2fs/compress.c (revision f785cec298c95d00058560c0715233294a04b8f3)
14c8ff709SChao Yu // SPDX-License-Identifier: GPL-2.0
24c8ff709SChao Yu /*
34c8ff709SChao Yu  * f2fs compress support
44c8ff709SChao Yu  *
54c8ff709SChao Yu  * Copyright (c) 2019 Chao Yu <chao@kernel.org>
64c8ff709SChao Yu  */
74c8ff709SChao Yu 
84c8ff709SChao Yu #include <linux/fs.h>
94c8ff709SChao Yu #include <linux/f2fs_fs.h>
10e41d12f5SChristoph Hellwig #include <linux/moduleparam.h>
114c8ff709SChao Yu #include <linux/writeback.h>
124c8ff709SChao Yu #include <linux/backing-dev.h>
134c8ff709SChao Yu #include <linux/lzo.h>
144c8ff709SChao Yu #include <linux/lz4.h>
1550cfa66fSChao Yu #include <linux/zstd.h>
166ce19affSChao Yu #include <linux/pagevec.h>
174c8ff709SChao Yu 
184c8ff709SChao Yu #include "f2fs.h"
194c8ff709SChao Yu #include "node.h"
206ce19affSChao Yu #include "segment.h"
214c8ff709SChao Yu #include <trace/events/f2fs.h>
224c8ff709SChao Yu 
23c68d6c88SChao Yu static struct kmem_cache *cic_entry_slab;
24c68d6c88SChao Yu static struct kmem_cache *dic_entry_slab;
25c68d6c88SChao Yu 
2631083031SChao Yu static void *page_array_alloc(struct inode *inode, int nr)
2731083031SChao Yu {
2831083031SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2931083031SChao Yu 	unsigned int size = sizeof(struct page *) * nr;
3031083031SChao Yu 
3131083031SChao Yu 	if (likely(size <= sbi->page_array_slab_size))
3232410577SChao Yu 		return f2fs_kmem_cache_alloc(sbi->page_array_slab,
3332410577SChao Yu 					GFP_F2FS_ZERO, false, F2FS_I_SB(inode));
3431083031SChao Yu 	return f2fs_kzalloc(sbi, size, GFP_NOFS);
3531083031SChao Yu }
3631083031SChao Yu 
3731083031SChao Yu static void page_array_free(struct inode *inode, void *pages, int nr)
3831083031SChao Yu {
3931083031SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4031083031SChao Yu 	unsigned int size = sizeof(struct page *) * nr;
4131083031SChao Yu 
4231083031SChao Yu 	if (!pages)
4331083031SChao Yu 		return;
4431083031SChao Yu 
4531083031SChao Yu 	if (likely(size <= sbi->page_array_slab_size))
4631083031SChao Yu 		kmem_cache_free(sbi->page_array_slab, pages);
4731083031SChao Yu 	else
4831083031SChao Yu 		kfree(pages);
4931083031SChao Yu }
5031083031SChao Yu 
514c8ff709SChao Yu struct f2fs_compress_ops {
524c8ff709SChao Yu 	int (*init_compress_ctx)(struct compress_ctx *cc);
534c8ff709SChao Yu 	void (*destroy_compress_ctx)(struct compress_ctx *cc);
544c8ff709SChao Yu 	int (*compress_pages)(struct compress_ctx *cc);
5523b1faaaSChao Yu 	int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
5623b1faaaSChao Yu 	void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
574c8ff709SChao Yu 	int (*decompress_pages)(struct decompress_io_ctx *dic);
58c571fbb5SSheng Yong 	bool (*is_level_valid)(int level);
594c8ff709SChao Yu };
604c8ff709SChao Yu 
614c8ff709SChao Yu static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
624c8ff709SChao Yu {
634c8ff709SChao Yu 	return index & (cc->cluster_size - 1);
644c8ff709SChao Yu }
654c8ff709SChao Yu 
664c8ff709SChao Yu static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
674c8ff709SChao Yu {
684c8ff709SChao Yu 	return index >> cc->log_cluster_size;
694c8ff709SChao Yu }
704c8ff709SChao Yu 
714c8ff709SChao Yu static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
724c8ff709SChao Yu {
734c8ff709SChao Yu 	return cc->cluster_idx << cc->log_cluster_size;
744c8ff709SChao Yu }
754c8ff709SChao Yu 
764c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page)
774c8ff709SChao Yu {
784c8ff709SChao Yu 	if (!PagePrivate(page))
794c8ff709SChao Yu 		return false;
804c8ff709SChao Yu 	if (!page_private(page))
814c8ff709SChao Yu 		return false;
82b763f3beSChao Yu 	if (page_private_nonpointer(page))
834c8ff709SChao Yu 		return false;
8429b993c7SYu Changchun 
854c8ff709SChao Yu 	f2fs_bug_on(F2FS_M_SB(page->mapping),
864c8ff709SChao Yu 		*((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
874c8ff709SChao Yu 	return true;
884c8ff709SChao Yu }
894c8ff709SChao Yu 
904c8ff709SChao Yu static void f2fs_set_compressed_page(struct page *page,
91887347a0SChao Yu 		struct inode *inode, pgoff_t index, void *data)
924c8ff709SChao Yu {
93b763f3beSChao Yu 	attach_page_private(page, (void *)data);
944c8ff709SChao Yu 
954c8ff709SChao Yu 	/* i_crypto_info and iv index */
964c8ff709SChao Yu 	page->index = index;
974c8ff709SChao Yu 	page->mapping = inode->i_mapping;
984c8ff709SChao Yu }
994c8ff709SChao Yu 
1004c8ff709SChao Yu static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
1014c8ff709SChao Yu {
1024c8ff709SChao Yu 	int i;
1034c8ff709SChao Yu 
1044c8ff709SChao Yu 	for (i = 0; i < len; i++) {
1054c8ff709SChao Yu 		if (!cc->rpages[i])
1064c8ff709SChao Yu 			continue;
1074c8ff709SChao Yu 		if (unlock)
1084c8ff709SChao Yu 			unlock_page(cc->rpages[i]);
1094c8ff709SChao Yu 		else
1104c8ff709SChao Yu 			put_page(cc->rpages[i]);
1114c8ff709SChao Yu 	}
1124c8ff709SChao Yu }
1134c8ff709SChao Yu 
1144c8ff709SChao Yu static void f2fs_put_rpages(struct compress_ctx *cc)
1154c8ff709SChao Yu {
1164c8ff709SChao Yu 	f2fs_drop_rpages(cc, cc->cluster_size, false);
1174c8ff709SChao Yu }
1184c8ff709SChao Yu 
1194c8ff709SChao Yu static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
1204c8ff709SChao Yu {
1214c8ff709SChao Yu 	f2fs_drop_rpages(cc, len, true);
1224c8ff709SChao Yu }
1234c8ff709SChao Yu 
1244c8ff709SChao Yu static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
1254c8ff709SChao Yu 		struct writeback_control *wbc, bool redirty, int unlock)
1264c8ff709SChao Yu {
1274c8ff709SChao Yu 	unsigned int i;
1284c8ff709SChao Yu 
1294c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
1304c8ff709SChao Yu 		if (!cc->rpages[i])
1314c8ff709SChao Yu 			continue;
1324c8ff709SChao Yu 		if (redirty)
1334c8ff709SChao Yu 			redirty_page_for_writepage(wbc, cc->rpages[i]);
1344c8ff709SChao Yu 		f2fs_put_page(cc->rpages[i], unlock);
1354c8ff709SChao Yu 	}
1364c8ff709SChao Yu }
1374c8ff709SChao Yu 
1384c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page)
1394c8ff709SChao Yu {
1404c8ff709SChao Yu 	return ((struct compress_io_ctx *)page_private(page))->rpages[0];
1414c8ff709SChao Yu }
1424c8ff709SChao Yu 
1434c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc)
1444c8ff709SChao Yu {
145adfc6943SJaegeuk Kim 	if (cc->rpages)
1464c8ff709SChao Yu 		return 0;
1474c8ff709SChao Yu 
14831083031SChao Yu 	cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1494c8ff709SChao Yu 	return cc->rpages ? 0 : -ENOMEM;
1504c8ff709SChao Yu }
1514c8ff709SChao Yu 
1528bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse)
1534c8ff709SChao Yu {
15431083031SChao Yu 	page_array_free(cc->inode, cc->rpages, cc->cluster_size);
1554c8ff709SChao Yu 	cc->rpages = NULL;
1564c8ff709SChao Yu 	cc->nr_rpages = 0;
1574c8ff709SChao Yu 	cc->nr_cpages = 0;
1583271d7ebSFengnan Chang 	cc->valid_nr_cpages = 0;
1598bfbfb0dSChao Yu 	if (!reuse)
1604c8ff709SChao Yu 		cc->cluster_idx = NULL_CLUSTER;
1614c8ff709SChao Yu }
1624c8ff709SChao Yu 
1634c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
1644c8ff709SChao Yu {
1654c8ff709SChao Yu 	unsigned int cluster_ofs;
1664c8ff709SChao Yu 
1674c8ff709SChao Yu 	if (!f2fs_cluster_can_merge_page(cc, page->index))
1684c8ff709SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
1694c8ff709SChao Yu 
1704c8ff709SChao Yu 	cluster_ofs = offset_in_cluster(cc, page->index);
1714c8ff709SChao Yu 	cc->rpages[cluster_ofs] = page;
1724c8ff709SChao Yu 	cc->nr_rpages++;
1734c8ff709SChao Yu 	cc->cluster_idx = cluster_idx(cc, page->index);
1744c8ff709SChao Yu }
1754c8ff709SChao Yu 
1764c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO
1774c8ff709SChao Yu static int lzo_init_compress_ctx(struct compress_ctx *cc)
1784c8ff709SChao Yu {
1794c8ff709SChao Yu 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
1804c8ff709SChao Yu 				LZO1X_MEM_COMPRESS, GFP_NOFS);
1814c8ff709SChao Yu 	if (!cc->private)
1824c8ff709SChao Yu 		return -ENOMEM;
1834c8ff709SChao Yu 
1844c8ff709SChao Yu 	cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
1854c8ff709SChao Yu 	return 0;
1864c8ff709SChao Yu }
1874c8ff709SChao Yu 
1884c8ff709SChao Yu static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
1894c8ff709SChao Yu {
1904c8ff709SChao Yu 	kvfree(cc->private);
1914c8ff709SChao Yu 	cc->private = NULL;
1924c8ff709SChao Yu }
1934c8ff709SChao Yu 
1944c8ff709SChao Yu static int lzo_compress_pages(struct compress_ctx *cc)
1954c8ff709SChao Yu {
1964c8ff709SChao Yu 	int ret;
1974c8ff709SChao Yu 
1984c8ff709SChao Yu 	ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
1994c8ff709SChao Yu 					&cc->clen, cc->private);
2004c8ff709SChao Yu 	if (ret != LZO_E_OK) {
201a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
202a7811840SChao Yu 				"lzo compress failed, ret:%d", ret);
2034c8ff709SChao Yu 		return -EIO;
2044c8ff709SChao Yu 	}
2054c8ff709SChao Yu 	return 0;
2064c8ff709SChao Yu }
2074c8ff709SChao Yu 
2084c8ff709SChao Yu static int lzo_decompress_pages(struct decompress_io_ctx *dic)
2094c8ff709SChao Yu {
2104c8ff709SChao Yu 	int ret;
2114c8ff709SChao Yu 
2124c8ff709SChao Yu 	ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
2134c8ff709SChao Yu 						dic->rbuf, &dic->rlen);
2144c8ff709SChao Yu 	if (ret != LZO_E_OK) {
215a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
216a7811840SChao Yu 				"lzo decompress failed, ret:%d", ret);
2174c8ff709SChao Yu 		return -EIO;
2184c8ff709SChao Yu 	}
2194c8ff709SChao Yu 
2204c8ff709SChao Yu 	if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
221a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
222a7811840SChao Yu 				"lzo invalid rlen:%zu, expected:%lu",
223a7811840SChao Yu 				dic->rlen, PAGE_SIZE << dic->log_cluster_size);
2244c8ff709SChao Yu 		return -EIO;
2254c8ff709SChao Yu 	}
2264c8ff709SChao Yu 	return 0;
2274c8ff709SChao Yu }
2284c8ff709SChao Yu 
2294c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lzo_ops = {
2304c8ff709SChao Yu 	.init_compress_ctx	= lzo_init_compress_ctx,
2314c8ff709SChao Yu 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
2324c8ff709SChao Yu 	.compress_pages		= lzo_compress_pages,
2334c8ff709SChao Yu 	.decompress_pages	= lzo_decompress_pages,
2344c8ff709SChao Yu };
2354c8ff709SChao Yu #endif
2364c8ff709SChao Yu 
2374c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4
2384c8ff709SChao Yu static int lz4_init_compress_ctx(struct compress_ctx *cc)
2394c8ff709SChao Yu {
2403fde13f8SChao Yu 	unsigned int size = LZ4_MEM_COMPRESS;
2413fde13f8SChao Yu 
2423fde13f8SChao Yu #ifdef CONFIG_F2FS_FS_LZ4HC
243b90e5086SChao Yu 	if (F2FS_I(cc->inode)->i_compress_level)
2443fde13f8SChao Yu 		size = LZ4HC_MEM_COMPRESS;
2453fde13f8SChao Yu #endif
2463fde13f8SChao Yu 
2473fde13f8SChao Yu 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS);
2484c8ff709SChao Yu 	if (!cc->private)
2494c8ff709SChao Yu 		return -ENOMEM;
2504c8ff709SChao Yu 
251f6644143SChao Yu 	/*
252f6644143SChao Yu 	 * we do not change cc->clen to LZ4_compressBound(inputsize) to
253f6644143SChao Yu 	 * adapt worst compress case, because lz4 compressor can handle
254f6644143SChao Yu 	 * output budget properly.
255f6644143SChao Yu 	 */
256f6644143SChao Yu 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
2574c8ff709SChao Yu 	return 0;
2584c8ff709SChao Yu }
2594c8ff709SChao Yu 
2604c8ff709SChao Yu static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
2614c8ff709SChao Yu {
2624c8ff709SChao Yu 	kvfree(cc->private);
2634c8ff709SChao Yu 	cc->private = NULL;
2644c8ff709SChao Yu }
2654c8ff709SChao Yu 
2664c8ff709SChao Yu static int lz4_compress_pages(struct compress_ctx *cc)
2674c8ff709SChao Yu {
2683094e557SYangtao Li 	int len = -EINVAL;
2693094e557SYangtao Li 	unsigned char level = F2FS_I(cc->inode)->i_compress_level;
2704c8ff709SChao Yu 
2713094e557SYangtao Li 	if (!level)
2724c8ff709SChao Yu 		len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
2734c8ff709SChao Yu 						cc->clen, cc->private);
2743094e557SYangtao Li #ifdef CONFIG_F2FS_FS_LZ4HC
2753094e557SYangtao Li 	else
2763094e557SYangtao Li 		len = LZ4_compress_HC(cc->rbuf, cc->cbuf->cdata, cc->rlen,
2773094e557SYangtao Li 					cc->clen, level, cc->private);
2783094e557SYangtao Li #endif
2793094e557SYangtao Li 	if (len < 0)
2803094e557SYangtao Li 		return len;
281f6644143SChao Yu 	if (!len)
282f6644143SChao Yu 		return -EAGAIN;
283f6644143SChao Yu 
2844c8ff709SChao Yu 	cc->clen = len;
2854c8ff709SChao Yu 	return 0;
2864c8ff709SChao Yu }
2874c8ff709SChao Yu 
2884c8ff709SChao Yu static int lz4_decompress_pages(struct decompress_io_ctx *dic)
2894c8ff709SChao Yu {
2904c8ff709SChao Yu 	int ret;
2914c8ff709SChao Yu 
2924c8ff709SChao Yu 	ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
2934c8ff709SChao Yu 						dic->clen, dic->rlen);
2944c8ff709SChao Yu 	if (ret < 0) {
295a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
296a7811840SChao Yu 				"lz4 decompress failed, ret:%d", ret);
2974c8ff709SChao Yu 		return -EIO;
2984c8ff709SChao Yu 	}
2994c8ff709SChao Yu 
3004c8ff709SChao Yu 	if (ret != PAGE_SIZE << dic->log_cluster_size) {
301a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
302a7811840SChao Yu 				"lz4 invalid ret:%d, expected:%lu",
303a7811840SChao Yu 				ret, PAGE_SIZE << dic->log_cluster_size);
3044c8ff709SChao Yu 		return -EIO;
3054c8ff709SChao Yu 	}
3064c8ff709SChao Yu 	return 0;
3074c8ff709SChao Yu }
3084c8ff709SChao Yu 
309c571fbb5SSheng Yong static bool lz4_is_level_valid(int lvl)
310c571fbb5SSheng Yong {
311c571fbb5SSheng Yong #ifdef CONFIG_F2FS_FS_LZ4HC
312c571fbb5SSheng Yong 	return !lvl || (lvl >= LZ4HC_MIN_CLEVEL && lvl <= LZ4HC_MAX_CLEVEL);
313c571fbb5SSheng Yong #else
314c571fbb5SSheng Yong 	return lvl == 0;
315c571fbb5SSheng Yong #endif
316c571fbb5SSheng Yong }
317c571fbb5SSheng Yong 
3184c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lz4_ops = {
3194c8ff709SChao Yu 	.init_compress_ctx	= lz4_init_compress_ctx,
3204c8ff709SChao Yu 	.destroy_compress_ctx	= lz4_destroy_compress_ctx,
3214c8ff709SChao Yu 	.compress_pages		= lz4_compress_pages,
3224c8ff709SChao Yu 	.decompress_pages	= lz4_decompress_pages,
323c571fbb5SSheng Yong 	.is_level_valid		= lz4_is_level_valid,
3244c8ff709SChao Yu };
3254c8ff709SChao Yu #endif
3264c8ff709SChao Yu 
32750cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD
32850cfa66fSChao Yu static int zstd_init_compress_ctx(struct compress_ctx *cc)
32950cfa66fSChao Yu {
330cf30f6a5SNick Terrell 	zstd_parameters params;
331cf30f6a5SNick Terrell 	zstd_cstream *stream;
33250cfa66fSChao Yu 	void *workspace;
33350cfa66fSChao Yu 	unsigned int workspace_size;
334b90e5086SChao Yu 	unsigned char level = F2FS_I(cc->inode)->i_compress_level;
33550cfa66fSChao Yu 
33600e120b5SJaegeuk Kim 	/* Need to remain this for backward compatibility */
3373fde13f8SChao Yu 	if (!level)
3383fde13f8SChao Yu 		level = F2FS_ZSTD_DEFAULT_CLEVEL;
3393fde13f8SChao Yu 
3404ff23a65SSheng Yong 	params = zstd_get_params(level, cc->rlen);
341cf30f6a5SNick Terrell 	workspace_size = zstd_cstream_workspace_bound(&params.cParams);
34250cfa66fSChao Yu 
34350cfa66fSChao Yu 	workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
34450cfa66fSChao Yu 					workspace_size, GFP_NOFS);
34550cfa66fSChao Yu 	if (!workspace)
34650cfa66fSChao Yu 		return -ENOMEM;
34750cfa66fSChao Yu 
348cf30f6a5SNick Terrell 	stream = zstd_init_cstream(&params, 0, workspace, workspace_size);
34950cfa66fSChao Yu 	if (!stream) {
350a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
351a7811840SChao Yu 				"%s zstd_init_cstream failed", __func__);
35250cfa66fSChao Yu 		kvfree(workspace);
35350cfa66fSChao Yu 		return -EIO;
35450cfa66fSChao Yu 	}
35550cfa66fSChao Yu 
35650cfa66fSChao Yu 	cc->private = workspace;
35750cfa66fSChao Yu 	cc->private2 = stream;
35850cfa66fSChao Yu 
35950cfa66fSChao Yu 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
36050cfa66fSChao Yu 	return 0;
36150cfa66fSChao Yu }
36250cfa66fSChao Yu 
36350cfa66fSChao Yu static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
36450cfa66fSChao Yu {
36550cfa66fSChao Yu 	kvfree(cc->private);
36650cfa66fSChao Yu 	cc->private = NULL;
36750cfa66fSChao Yu 	cc->private2 = NULL;
36850cfa66fSChao Yu }
36950cfa66fSChao Yu 
37050cfa66fSChao Yu static int zstd_compress_pages(struct compress_ctx *cc)
37150cfa66fSChao Yu {
372cf30f6a5SNick Terrell 	zstd_cstream *stream = cc->private2;
373cf30f6a5SNick Terrell 	zstd_in_buffer inbuf;
374cf30f6a5SNick Terrell 	zstd_out_buffer outbuf;
37550cfa66fSChao Yu 	int src_size = cc->rlen;
37650cfa66fSChao Yu 	int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
37750cfa66fSChao Yu 	int ret;
37850cfa66fSChao Yu 
37950cfa66fSChao Yu 	inbuf.pos = 0;
38050cfa66fSChao Yu 	inbuf.src = cc->rbuf;
38150cfa66fSChao Yu 	inbuf.size = src_size;
38250cfa66fSChao Yu 
38350cfa66fSChao Yu 	outbuf.pos = 0;
38450cfa66fSChao Yu 	outbuf.dst = cc->cbuf->cdata;
38550cfa66fSChao Yu 	outbuf.size = dst_size;
38650cfa66fSChao Yu 
387cf30f6a5SNick Terrell 	ret = zstd_compress_stream(stream, &outbuf, &inbuf);
388cf30f6a5SNick Terrell 	if (zstd_is_error(ret)) {
389a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
390a7811840SChao Yu 				"%s zstd_compress_stream failed, ret: %d",
391cf30f6a5SNick Terrell 				__func__, zstd_get_error_code(ret));
39250cfa66fSChao Yu 		return -EIO;
39350cfa66fSChao Yu 	}
39450cfa66fSChao Yu 
395cf30f6a5SNick Terrell 	ret = zstd_end_stream(stream, &outbuf);
396cf30f6a5SNick Terrell 	if (zstd_is_error(ret)) {
397a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
398a7811840SChao Yu 				"%s zstd_end_stream returned %d",
399cf30f6a5SNick Terrell 				__func__, zstd_get_error_code(ret));
40050cfa66fSChao Yu 		return -EIO;
40150cfa66fSChao Yu 	}
40250cfa66fSChao Yu 
4031454c978SChao Yu 	/*
4041454c978SChao Yu 	 * there is compressed data remained in intermediate buffer due to
4051454c978SChao Yu 	 * no more space in cbuf.cdata
4061454c978SChao Yu 	 */
4071454c978SChao Yu 	if (ret)
4081454c978SChao Yu 		return -EAGAIN;
4091454c978SChao Yu 
41050cfa66fSChao Yu 	cc->clen = outbuf.pos;
41150cfa66fSChao Yu 	return 0;
41250cfa66fSChao Yu }
41350cfa66fSChao Yu 
41450cfa66fSChao Yu static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
41550cfa66fSChao Yu {
416cf30f6a5SNick Terrell 	zstd_dstream *stream;
41750cfa66fSChao Yu 	void *workspace;
41850cfa66fSChao Yu 	unsigned int workspace_size;
4190e2b7385SChao Yu 	unsigned int max_window_size =
4200e2b7385SChao Yu 			MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
42150cfa66fSChao Yu 
422cf30f6a5SNick Terrell 	workspace_size = zstd_dstream_workspace_bound(max_window_size);
42350cfa66fSChao Yu 
42450cfa66fSChao Yu 	workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
42550cfa66fSChao Yu 					workspace_size, GFP_NOFS);
42650cfa66fSChao Yu 	if (!workspace)
42750cfa66fSChao Yu 		return -ENOMEM;
42850cfa66fSChao Yu 
429cf30f6a5SNick Terrell 	stream = zstd_init_dstream(max_window_size, workspace, workspace_size);
43050cfa66fSChao Yu 	if (!stream) {
431a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
432a7811840SChao Yu 				"%s zstd_init_dstream failed", __func__);
43350cfa66fSChao Yu 		kvfree(workspace);
43450cfa66fSChao Yu 		return -EIO;
43550cfa66fSChao Yu 	}
43650cfa66fSChao Yu 
43750cfa66fSChao Yu 	dic->private = workspace;
43850cfa66fSChao Yu 	dic->private2 = stream;
43950cfa66fSChao Yu 
44050cfa66fSChao Yu 	return 0;
44150cfa66fSChao Yu }
44250cfa66fSChao Yu 
44350cfa66fSChao Yu static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
44450cfa66fSChao Yu {
44550cfa66fSChao Yu 	kvfree(dic->private);
44650cfa66fSChao Yu 	dic->private = NULL;
44750cfa66fSChao Yu 	dic->private2 = NULL;
44850cfa66fSChao Yu }
44950cfa66fSChao Yu 
45050cfa66fSChao Yu static int zstd_decompress_pages(struct decompress_io_ctx *dic)
45150cfa66fSChao Yu {
452cf30f6a5SNick Terrell 	zstd_dstream *stream = dic->private2;
453cf30f6a5SNick Terrell 	zstd_in_buffer inbuf;
454cf30f6a5SNick Terrell 	zstd_out_buffer outbuf;
45550cfa66fSChao Yu 	int ret;
45650cfa66fSChao Yu 
45750cfa66fSChao Yu 	inbuf.pos = 0;
45850cfa66fSChao Yu 	inbuf.src = dic->cbuf->cdata;
45950cfa66fSChao Yu 	inbuf.size = dic->clen;
46050cfa66fSChao Yu 
46150cfa66fSChao Yu 	outbuf.pos = 0;
46250cfa66fSChao Yu 	outbuf.dst = dic->rbuf;
46350cfa66fSChao Yu 	outbuf.size = dic->rlen;
46450cfa66fSChao Yu 
465cf30f6a5SNick Terrell 	ret = zstd_decompress_stream(stream, &outbuf, &inbuf);
466cf30f6a5SNick Terrell 	if (zstd_is_error(ret)) {
467a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
468a7811840SChao Yu 				"%s zstd_decompress_stream failed, ret: %d",
469cf30f6a5SNick Terrell 				__func__, zstd_get_error_code(ret));
47050cfa66fSChao Yu 		return -EIO;
47150cfa66fSChao Yu 	}
47250cfa66fSChao Yu 
47350cfa66fSChao Yu 	if (dic->rlen != outbuf.pos) {
474a7811840SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
475a7811840SChao Yu 				"%s ZSTD invalid rlen:%zu, expected:%lu",
47650cfa66fSChao Yu 				__func__, dic->rlen,
47750cfa66fSChao Yu 				PAGE_SIZE << dic->log_cluster_size);
47850cfa66fSChao Yu 		return -EIO;
47950cfa66fSChao Yu 	}
48050cfa66fSChao Yu 
48150cfa66fSChao Yu 	return 0;
48250cfa66fSChao Yu }
48350cfa66fSChao Yu 
484c571fbb5SSheng Yong static bool zstd_is_level_valid(int lvl)
485c571fbb5SSheng Yong {
486c571fbb5SSheng Yong 	return lvl >= zstd_min_clevel() && lvl <= zstd_max_clevel();
487c571fbb5SSheng Yong }
488c571fbb5SSheng Yong 
48950cfa66fSChao Yu static const struct f2fs_compress_ops f2fs_zstd_ops = {
49050cfa66fSChao Yu 	.init_compress_ctx	= zstd_init_compress_ctx,
49150cfa66fSChao Yu 	.destroy_compress_ctx	= zstd_destroy_compress_ctx,
49250cfa66fSChao Yu 	.compress_pages		= zstd_compress_pages,
49350cfa66fSChao Yu 	.init_decompress_ctx	= zstd_init_decompress_ctx,
49450cfa66fSChao Yu 	.destroy_decompress_ctx	= zstd_destroy_decompress_ctx,
49550cfa66fSChao Yu 	.decompress_pages	= zstd_decompress_pages,
496c571fbb5SSheng Yong 	.is_level_valid		= zstd_is_level_valid,
49750cfa66fSChao Yu };
49850cfa66fSChao Yu #endif
49950cfa66fSChao Yu 
5006d92b201SChao Yu #ifdef CONFIG_F2FS_FS_LZO
5016d92b201SChao Yu #ifdef CONFIG_F2FS_FS_LZORLE
5026d92b201SChao Yu static int lzorle_compress_pages(struct compress_ctx *cc)
5036d92b201SChao Yu {
5046d92b201SChao Yu 	int ret;
5056d92b201SChao Yu 
5066d92b201SChao Yu 	ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
5076d92b201SChao Yu 					&cc->clen, cc->private);
5086d92b201SChao Yu 	if (ret != LZO_E_OK) {
509b1c9d3f8SChao Yu 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
510b1c9d3f8SChao Yu 				"lzo-rle compress failed, ret:%d", ret);
5116d92b201SChao Yu 		return -EIO;
5126d92b201SChao Yu 	}
5136d92b201SChao Yu 	return 0;
5146d92b201SChao Yu }
5156d92b201SChao Yu 
5166d92b201SChao Yu static const struct f2fs_compress_ops f2fs_lzorle_ops = {
5176d92b201SChao Yu 	.init_compress_ctx	= lzo_init_compress_ctx,
5186d92b201SChao Yu 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
5196d92b201SChao Yu 	.compress_pages		= lzorle_compress_pages,
5206d92b201SChao Yu 	.decompress_pages	= lzo_decompress_pages,
5216d92b201SChao Yu };
5226d92b201SChao Yu #endif
5236d92b201SChao Yu #endif
5246d92b201SChao Yu 
5254c8ff709SChao Yu static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
5264c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO
5274c8ff709SChao Yu 	&f2fs_lzo_ops,
5284c8ff709SChao Yu #else
5294c8ff709SChao Yu 	NULL,
5304c8ff709SChao Yu #endif
5314c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4
5324c8ff709SChao Yu 	&f2fs_lz4_ops,
5334c8ff709SChao Yu #else
5344c8ff709SChao Yu 	NULL,
5354c8ff709SChao Yu #endif
53650cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD
53750cfa66fSChao Yu 	&f2fs_zstd_ops,
53850cfa66fSChao Yu #else
53950cfa66fSChao Yu 	NULL,
54050cfa66fSChao Yu #endif
5416d92b201SChao Yu #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
5426d92b201SChao Yu 	&f2fs_lzorle_ops,
5436d92b201SChao Yu #else
5446d92b201SChao Yu 	NULL,
5456d92b201SChao Yu #endif
5464c8ff709SChao Yu };
5474c8ff709SChao Yu 
5484c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode)
5494c8ff709SChao Yu {
5504c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
5514c8ff709SChao Yu 		return true;
5524c8ff709SChao Yu 	return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
5534c8ff709SChao Yu }
5544c8ff709SChao Yu 
555c571fbb5SSheng Yong bool f2fs_is_compress_level_valid(int alg, int lvl)
556c571fbb5SSheng Yong {
557c571fbb5SSheng Yong 	const struct f2fs_compress_ops *cops = f2fs_cops[alg];
558c571fbb5SSheng Yong 
559c571fbb5SSheng Yong 	if (cops->is_level_valid)
560c571fbb5SSheng Yong 		return cops->is_level_valid(lvl);
561c571fbb5SSheng Yong 
562c571fbb5SSheng Yong 	return lvl == 0;
563c571fbb5SSheng Yong }
564c571fbb5SSheng Yong 
56599bbe307SJaegeuk Kim static mempool_t *compress_page_pool;
5665e6bbde9SChao Yu static int num_compress_pages = 512;
5675e6bbde9SChao Yu module_param(num_compress_pages, uint, 0444);
5685e6bbde9SChao Yu MODULE_PARM_DESC(num_compress_pages,
5695e6bbde9SChao Yu 		"Number of intermediate compress pages to preallocate");
5705e6bbde9SChao Yu 
571a1357a91SYangtao Li int __init f2fs_init_compress_mempool(void)
5725e6bbde9SChao Yu {
5735e6bbde9SChao Yu 	compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
574870af777SYangtao Li 	return compress_page_pool ? 0 : -ENOMEM;
5755e6bbde9SChao Yu }
5765e6bbde9SChao Yu 
5775e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void)
5785e6bbde9SChao Yu {
5795e6bbde9SChao Yu 	mempool_destroy(compress_page_pool);
5805e6bbde9SChao Yu }
5815e6bbde9SChao Yu 
5825e6bbde9SChao Yu static struct page *f2fs_compress_alloc_page(void)
5834c8ff709SChao Yu {
5844c8ff709SChao Yu 	struct page *page;
5854c8ff709SChao Yu 
5865e6bbde9SChao Yu 	page = mempool_alloc(compress_page_pool, GFP_NOFS);
5874c8ff709SChao Yu 	lock_page(page);
5885e6bbde9SChao Yu 
5894c8ff709SChao Yu 	return page;
5904c8ff709SChao Yu }
5914c8ff709SChao Yu 
5925e6bbde9SChao Yu static void f2fs_compress_free_page(struct page *page)
5935e6bbde9SChao Yu {
5945e6bbde9SChao Yu 	if (!page)
5955e6bbde9SChao Yu 		return;
596b763f3beSChao Yu 	detach_page_private(page);
5975e6bbde9SChao Yu 	page->mapping = NULL;
5985e6bbde9SChao Yu 	unlock_page(page);
5995e6bbde9SChao Yu 	mempool_free(page, compress_page_pool);
6005e6bbde9SChao Yu }
6015e6bbde9SChao Yu 
6026fcaebacSDaeho Jeong #define MAX_VMAP_RETRIES	3
6036fcaebacSDaeho Jeong 
6046fcaebacSDaeho Jeong static void *f2fs_vmap(struct page **pages, unsigned int count)
6056fcaebacSDaeho Jeong {
6066fcaebacSDaeho Jeong 	int i;
6076fcaebacSDaeho Jeong 	void *buf = NULL;
6086fcaebacSDaeho Jeong 
6096fcaebacSDaeho Jeong 	for (i = 0; i < MAX_VMAP_RETRIES; i++) {
6106fcaebacSDaeho Jeong 		buf = vm_map_ram(pages, count, -1);
6116fcaebacSDaeho Jeong 		if (buf)
6126fcaebacSDaeho Jeong 			break;
6136fcaebacSDaeho Jeong 		vm_unmap_aliases();
6146fcaebacSDaeho Jeong 	}
6156fcaebacSDaeho Jeong 	return buf;
6166fcaebacSDaeho Jeong }
6176fcaebacSDaeho Jeong 
6184c8ff709SChao Yu static int f2fs_compress_pages(struct compress_ctx *cc)
6194c8ff709SChao Yu {
6204c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
6214c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
6224c8ff709SChao Yu 				f2fs_cops[fi->i_compress_algorithm];
62331083031SChao Yu 	unsigned int max_len, new_nr_cpages;
624b28f047bSChao Yu 	u32 chksum = 0;
6254c8ff709SChao Yu 	int i, ret;
6264c8ff709SChao Yu 
6274c8ff709SChao Yu 	trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
6284c8ff709SChao Yu 				cc->cluster_size, fi->i_compress_algorithm);
6294c8ff709SChao Yu 
63023b1faaaSChao Yu 	if (cops->init_compress_ctx) {
6314c8ff709SChao Yu 		ret = cops->init_compress_ctx(cc);
6324c8ff709SChao Yu 		if (ret)
6334c8ff709SChao Yu 			goto out;
63423b1faaaSChao Yu 	}
6354c8ff709SChao Yu 
6364c8ff709SChao Yu 	max_len = COMPRESS_HEADER_SIZE + cc->clen;
6374c8ff709SChao Yu 	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
6383271d7ebSFengnan Chang 	cc->valid_nr_cpages = cc->nr_cpages;
6394c8ff709SChao Yu 
64031083031SChao Yu 	cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
6414c8ff709SChao Yu 	if (!cc->cpages) {
6424c8ff709SChao Yu 		ret = -ENOMEM;
6434c8ff709SChao Yu 		goto destroy_compress_ctx;
6444c8ff709SChao Yu 	}
6454c8ff709SChao Yu 
646863907a4SChao Yu 	for (i = 0; i < cc->nr_cpages; i++)
6475e6bbde9SChao Yu 		cc->cpages[i] = f2fs_compress_alloc_page();
6484c8ff709SChao Yu 
6496fcaebacSDaeho Jeong 	cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
6504c8ff709SChao Yu 	if (!cc->rbuf) {
6514c8ff709SChao Yu 		ret = -ENOMEM;
6524c8ff709SChao Yu 		goto out_free_cpages;
6534c8ff709SChao Yu 	}
6544c8ff709SChao Yu 
6556fcaebacSDaeho Jeong 	cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
6564c8ff709SChao Yu 	if (!cc->cbuf) {
6574c8ff709SChao Yu 		ret = -ENOMEM;
6584c8ff709SChao Yu 		goto out_vunmap_rbuf;
6594c8ff709SChao Yu 	}
6604c8ff709SChao Yu 
6614c8ff709SChao Yu 	ret = cops->compress_pages(cc);
6624c8ff709SChao Yu 	if (ret)
6634c8ff709SChao Yu 		goto out_vunmap_cbuf;
6644c8ff709SChao Yu 
6654c8ff709SChao Yu 	max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
6664c8ff709SChao Yu 
6674c8ff709SChao Yu 	if (cc->clen > max_len) {
6684c8ff709SChao Yu 		ret = -EAGAIN;
6694c8ff709SChao Yu 		goto out_vunmap_cbuf;
6704c8ff709SChao Yu 	}
6714c8ff709SChao Yu 
6724c8ff709SChao Yu 	cc->cbuf->clen = cpu_to_le32(cc->clen);
6734c8ff709SChao Yu 
674447286ebSYangtao Li 	if (fi->i_compress_flag & BIT(COMPRESS_CHKSUM))
675b28f047bSChao Yu 		chksum = f2fs_crc32(F2FS_I_SB(cc->inode),
676b28f047bSChao Yu 					cc->cbuf->cdata, cc->clen);
677b28f047bSChao Yu 	cc->cbuf->chksum = cpu_to_le32(chksum);
678b28f047bSChao Yu 
6794c8ff709SChao Yu 	for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
6804c8ff709SChao Yu 		cc->cbuf->reserved[i] = cpu_to_le32(0);
6814c8ff709SChao Yu 
68231083031SChao Yu 	new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
68331083031SChao Yu 
6847fa6d598SEric Biggers 	/* zero out any unused part of the last page */
6857fa6d598SEric Biggers 	memset(&cc->cbuf->cdata[cc->clen], 0,
68631083031SChao Yu 			(new_nr_cpages * PAGE_SIZE) -
68731083031SChao Yu 			(cc->clen + COMPRESS_HEADER_SIZE));
6887fa6d598SEric Biggers 
6896fcaebacSDaeho Jeong 	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
6906fcaebacSDaeho Jeong 	vm_unmap_ram(cc->rbuf, cc->cluster_size);
6914c8ff709SChao Yu 
692ebaaec35SZhang Qilong 	for (i = new_nr_cpages; i < cc->nr_cpages; i++) {
6935e6bbde9SChao Yu 		f2fs_compress_free_page(cc->cpages[i]);
6944c8ff709SChao Yu 		cc->cpages[i] = NULL;
6954c8ff709SChao Yu 	}
6964c8ff709SChao Yu 
69723b1faaaSChao Yu 	if (cops->destroy_compress_ctx)
69809ff4801SChao Yu 		cops->destroy_compress_ctx(cc);
69909ff4801SChao Yu 
7003271d7ebSFengnan Chang 	cc->valid_nr_cpages = new_nr_cpages;
7014c8ff709SChao Yu 
7024c8ff709SChao Yu 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
7034c8ff709SChao Yu 							cc->clen, ret);
7044c8ff709SChao Yu 	return 0;
7054c8ff709SChao Yu 
7064c8ff709SChao Yu out_vunmap_cbuf:
7076fcaebacSDaeho Jeong 	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
7084c8ff709SChao Yu out_vunmap_rbuf:
7096fcaebacSDaeho Jeong 	vm_unmap_ram(cc->rbuf, cc->cluster_size);
7104c8ff709SChao Yu out_free_cpages:
7114c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
7124c8ff709SChao Yu 		if (cc->cpages[i])
7135e6bbde9SChao Yu 			f2fs_compress_free_page(cc->cpages[i]);
7144c8ff709SChao Yu 	}
71531083031SChao Yu 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
7164c8ff709SChao Yu 	cc->cpages = NULL;
7174c8ff709SChao Yu destroy_compress_ctx:
71823b1faaaSChao Yu 	if (cops->destroy_compress_ctx)
7194c8ff709SChao Yu 		cops->destroy_compress_ctx(cc);
7204c8ff709SChao Yu out:
7214c8ff709SChao Yu 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
7224c8ff709SChao Yu 							cc->clen, ret);
7234c8ff709SChao Yu 	return ret;
7244c8ff709SChao Yu }
7254c8ff709SChao Yu 
726bff139b4SDaeho Jeong static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
727bff139b4SDaeho Jeong 		bool pre_alloc);
728bff139b4SDaeho Jeong static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
729bff139b4SDaeho Jeong 		bool bypass_destroy_callback, bool pre_alloc);
730bff139b4SDaeho Jeong 
731bff139b4SDaeho Jeong void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task)
7324c8ff709SChao Yu {
7334c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
7344c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(dic->inode);
7354c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
7364c8ff709SChao Yu 			f2fs_cops[fi->i_compress_algorithm];
737bff139b4SDaeho Jeong 	bool bypass_callback = false;
7384c8ff709SChao Yu 	int ret;
7394c8ff709SChao Yu 
7404c8ff709SChao Yu 	trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
7414c8ff709SChao Yu 				dic->cluster_size, fi->i_compress_algorithm);
7424c8ff709SChao Yu 
7434c8ff709SChao Yu 	if (dic->failed) {
7444c8ff709SChao Yu 		ret = -EIO;
7457f59b277SEric Biggers 		goto out_end_io;
7464c8ff709SChao Yu 	}
7474c8ff709SChao Yu 
748bff139b4SDaeho Jeong 	ret = f2fs_prepare_decomp_mem(dic, false);
749bff139b4SDaeho Jeong 	if (ret) {
750bff139b4SDaeho Jeong 		bypass_callback = true;
751bff139b4SDaeho Jeong 		goto out_release;
7524c8ff709SChao Yu 	}
7534c8ff709SChao Yu 
7544c8ff709SChao Yu 	dic->clen = le32_to_cpu(dic->cbuf->clen);
7554c8ff709SChao Yu 	dic->rlen = PAGE_SIZE << dic->log_cluster_size;
7564c8ff709SChao Yu 
7574c8ff709SChao Yu 	if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
7584c8ff709SChao Yu 		ret = -EFSCORRUPTED;
7591aa161e4SJaegeuk Kim 
7601aa161e4SJaegeuk Kim 		/* Avoid f2fs_commit_super in irq context */
761633c8b94SJaegeuk Kim 		if (!in_task)
762901c12d1SChao Yu 			f2fs_handle_error_async(sbi, ERROR_FAIL_DECOMPRESSION);
7631aa161e4SJaegeuk Kim 		else
76495fa90c9SChao Yu 			f2fs_handle_error(sbi, ERROR_FAIL_DECOMPRESSION);
765bff139b4SDaeho Jeong 		goto out_release;
7664c8ff709SChao Yu 	}
7674c8ff709SChao Yu 
7684c8ff709SChao Yu 	ret = cops->decompress_pages(dic);
7694c8ff709SChao Yu 
770447286ebSYangtao Li 	if (!ret && (fi->i_compress_flag & BIT(COMPRESS_CHKSUM))) {
771b28f047bSChao Yu 		u32 provided = le32_to_cpu(dic->cbuf->chksum);
772b28f047bSChao Yu 		u32 calculated = f2fs_crc32(sbi, dic->cbuf->cdata, dic->clen);
773b28f047bSChao Yu 
774b28f047bSChao Yu 		if (provided != calculated) {
775b28f047bSChao Yu 			if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) {
776b28f047bSChao Yu 				set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT);
777b1c9d3f8SChao Yu 				f2fs_info_ratelimited(sbi,
778b1c9d3f8SChao Yu 					"checksum invalid, nid = %lu, %x vs %x",
779b1c9d3f8SChao Yu 					dic->inode->i_ino,
780b28f047bSChao Yu 					provided, calculated);
781b28f047bSChao Yu 			}
782b28f047bSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
783b28f047bSChao Yu 		}
784b28f047bSChao Yu 	}
785b28f047bSChao Yu 
786bff139b4SDaeho Jeong out_release:
787bff139b4SDaeho Jeong 	f2fs_release_decomp_mem(dic, bypass_callback, false);
788bff139b4SDaeho Jeong 
7897f59b277SEric Biggers out_end_io:
7904c8ff709SChao Yu 	trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
7914c8ff709SChao Yu 							dic->clen, ret);
792bff139b4SDaeho Jeong 	f2fs_decompress_end_io(dic, ret, in_task);
7937f59b277SEric Biggers }
7947f59b277SEric Biggers 
7957f59b277SEric Biggers /*
7967f59b277SEric Biggers  * This is called when a page of a compressed cluster has been read from disk
7977f59b277SEric Biggers  * (or failed to be read from disk).  It checks whether this page was the last
7987f59b277SEric Biggers  * page being waited on in the cluster, and if so, it decompresses the cluster
7997f59b277SEric Biggers  * (or in the case of a failure, cleans up without actually decompressing).
8007f59b277SEric Biggers  */
8016ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
802bff139b4SDaeho Jeong 		block_t blkaddr, bool in_task)
8037f59b277SEric Biggers {
8047f59b277SEric Biggers 	struct decompress_io_ctx *dic =
8057f59b277SEric Biggers 			(struct decompress_io_ctx *)page_private(page);
8067f59b277SEric Biggers 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
8077f59b277SEric Biggers 
8087f59b277SEric Biggers 	dec_page_count(sbi, F2FS_RD_DATA);
8097f59b277SEric Biggers 
8107f59b277SEric Biggers 	if (failed)
8117f59b277SEric Biggers 		WRITE_ONCE(dic->failed, true);
812bff139b4SDaeho Jeong 	else if (blkaddr && in_task)
8136ce19affSChao Yu 		f2fs_cache_compressed_page(sbi, page,
8146ce19affSChao Yu 					dic->inode->i_ino, blkaddr);
8157f59b277SEric Biggers 
8167f59b277SEric Biggers 	if (atomic_dec_and_test(&dic->remaining_pages))
817bff139b4SDaeho Jeong 		f2fs_decompress_cluster(dic, in_task);
8184c8ff709SChao Yu }
8194c8ff709SChao Yu 
8204c8ff709SChao Yu static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
8214c8ff709SChao Yu {
8224c8ff709SChao Yu 	if (cc->cluster_idx == NULL_CLUSTER)
8234c8ff709SChao Yu 		return true;
8244c8ff709SChao Yu 	return cc->cluster_idx == cluster_idx(cc, index);
8254c8ff709SChao Yu }
8264c8ff709SChao Yu 
8274c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc)
8284c8ff709SChao Yu {
8294c8ff709SChao Yu 	return cc->nr_rpages == 0;
8304c8ff709SChao Yu }
8314c8ff709SChao Yu 
8324c8ff709SChao Yu static bool f2fs_cluster_is_full(struct compress_ctx *cc)
8334c8ff709SChao Yu {
8344c8ff709SChao Yu 	return cc->cluster_size == cc->nr_rpages;
8354c8ff709SChao Yu }
8364c8ff709SChao Yu 
8374c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
8384c8ff709SChao Yu {
8394c8ff709SChao Yu 	if (f2fs_cluster_is_empty(cc))
8404c8ff709SChao Yu 		return true;
8414c8ff709SChao Yu 	return is_page_in_cluster(cc, index);
8424c8ff709SChao Yu }
8434c8ff709SChao Yu 
84401fc4b9aSFengnan Chang bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
8454f8219f8SFengnan Chang 				int index, int nr_pages, bool uptodate)
846b368cc5eSFengnan Chang {
84701fc4b9aSFengnan Chang 	unsigned long pgidx = pages[index]->index;
8484f8219f8SFengnan Chang 	int i = uptodate ? 0 : 1;
8494f8219f8SFengnan Chang 
8504f8219f8SFengnan Chang 	/*
8514f8219f8SFengnan Chang 	 * when uptodate set to true, try to check all pages in cluster is
8524f8219f8SFengnan Chang 	 * uptodate or not.
8534f8219f8SFengnan Chang 	 */
8544f8219f8SFengnan Chang 	if (uptodate && (pgidx % cc->cluster_size))
8554f8219f8SFengnan Chang 		return false;
856b368cc5eSFengnan Chang 
857b368cc5eSFengnan Chang 	if (nr_pages - index < cc->cluster_size)
858b368cc5eSFengnan Chang 		return false;
859b368cc5eSFengnan Chang 
8604f8219f8SFengnan Chang 	for (; i < cc->cluster_size; i++) {
86101fc4b9aSFengnan Chang 		if (pages[index + i]->index != pgidx + i)
862b368cc5eSFengnan Chang 			return false;
86301fc4b9aSFengnan Chang 		if (uptodate && !PageUptodate(pages[index + i]))
864b368cc5eSFengnan Chang 			return false;
865b368cc5eSFengnan Chang 	}
866b368cc5eSFengnan Chang 
867b368cc5eSFengnan Chang 	return true;
868b368cc5eSFengnan Chang }
869b368cc5eSFengnan Chang 
8705db479f0SChao Yu static bool cluster_has_invalid_data(struct compress_ctx *cc)
8714c8ff709SChao Yu {
8724c8ff709SChao Yu 	loff_t i_size = i_size_read(cc->inode);
8734c8ff709SChao Yu 	unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
8744c8ff709SChao Yu 	int i;
8754c8ff709SChao Yu 
8764c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
8774c8ff709SChao Yu 		struct page *page = cc->rpages[i];
8784c8ff709SChao Yu 
8798af85f71SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), !page);
8804c8ff709SChao Yu 
8814c8ff709SChao Yu 		/* beyond EOF */
8824c8ff709SChao Yu 		if (page->index >= nr_pages)
8834c8ff709SChao Yu 			return true;
8844c8ff709SChao Yu 	}
8855db479f0SChao Yu 	return false;
8865db479f0SChao Yu }
8874c8ff709SChao Yu 
888bbe1da7eSChao Yu bool f2fs_sanity_check_cluster(struct dnode_of_data *dn)
889bbe1da7eSChao Yu {
8902aaea533SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
891bbe1da7eSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
892bbe1da7eSChao Yu 	unsigned int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
893bbe1da7eSChao Yu 	int cluster_end = 0;
8942aaea533SChao Yu 	unsigned int count;
895bbe1da7eSChao Yu 	int i;
896bbe1da7eSChao Yu 	char *reason = "";
897bbe1da7eSChao Yu 
8982aaea533SChao Yu 	if (dn->data_blkaddr != COMPRESS_ADDR)
899bbe1da7eSChao Yu 		return false;
900bbe1da7eSChao Yu 
901bbe1da7eSChao Yu 	/* [..., COMPR_ADDR, ...] */
902bbe1da7eSChao Yu 	if (dn->ofs_in_node % cluster_size) {
903bbe1da7eSChao Yu 		reason = "[*|C|*|*]";
904bbe1da7eSChao Yu 		goto out;
905bbe1da7eSChao Yu 	}
906bbe1da7eSChao Yu 
9072aaea533SChao Yu 	for (i = 1, count = 1; i < cluster_size; i++, count++) {
908bbe1da7eSChao Yu 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
909bbe1da7eSChao Yu 							dn->ofs_in_node + i);
910bbe1da7eSChao Yu 
911bbe1da7eSChao Yu 		/* [COMPR_ADDR, ..., COMPR_ADDR] */
912bbe1da7eSChao Yu 		if (blkaddr == COMPRESS_ADDR) {
913bbe1da7eSChao Yu 			reason = "[C|*|C|*]";
914bbe1da7eSChao Yu 			goto out;
915bbe1da7eSChao Yu 		}
916bbe1da7eSChao Yu 		if (!__is_valid_data_blkaddr(blkaddr)) {
917bbe1da7eSChao Yu 			if (!cluster_end)
918bbe1da7eSChao Yu 				cluster_end = i;
919bbe1da7eSChao Yu 			continue;
920bbe1da7eSChao Yu 		}
921bbe1da7eSChao Yu 		/* [COMPR_ADDR, NULL_ADDR or NEW_ADDR, valid_blkaddr] */
922bbe1da7eSChao Yu 		if (cluster_end) {
923bbe1da7eSChao Yu 			reason = "[C|N|N|V]";
924bbe1da7eSChao Yu 			goto out;
925bbe1da7eSChao Yu 		}
926bbe1da7eSChao Yu 	}
9272aaea533SChao Yu 
9282aaea533SChao Yu 	f2fs_bug_on(F2FS_I_SB(dn->inode), count != cluster_size &&
9292aaea533SChao Yu 		!is_inode_flag_set(dn->inode, FI_COMPRESS_RELEASED));
9302aaea533SChao Yu 
931bbe1da7eSChao Yu 	return false;
932bbe1da7eSChao Yu out:
933bbe1da7eSChao Yu 	f2fs_warn(sbi, "access invalid cluster, ino:%lu, nid:%u, ofs_in_node:%u, reason:%s",
934bbe1da7eSChao Yu 			dn->inode->i_ino, dn->nid, dn->ofs_in_node, reason);
935bbe1da7eSChao Yu 	set_sbi_flag(sbi, SBI_NEED_FSCK);
936bbe1da7eSChao Yu 	return true;
9372aaea533SChao Yu #else
9382aaea533SChao Yu 	return false;
9392aaea533SChao Yu #endif
9402aaea533SChao Yu }
9412aaea533SChao Yu 
9422aaea533SChao Yu static int __f2fs_get_cluster_blocks(struct inode *inode,
9432aaea533SChao Yu 					struct dnode_of_data *dn)
9442aaea533SChao Yu {
9452aaea533SChao Yu 	unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
9462aaea533SChao Yu 	int count, i;
9472aaea533SChao Yu 
948*f785cec2SYeongjin Gil 	for (i = 0, count = 0; i < cluster_size; i++) {
9492aaea533SChao Yu 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
9502aaea533SChao Yu 							dn->ofs_in_node + i);
9512aaea533SChao Yu 
9522aaea533SChao Yu 		if (__is_valid_data_blkaddr(blkaddr))
9532aaea533SChao Yu 			count++;
9542aaea533SChao Yu 	}
9552aaea533SChao Yu 
9562aaea533SChao Yu 	return count;
957bbe1da7eSChao Yu }
958bbe1da7eSChao Yu 
959*f785cec2SYeongjin Gil static int __f2fs_cluster_blocks(struct inode *inode, unsigned int cluster_idx,
960*f785cec2SYeongjin Gil 				enum cluster_check_type type)
9614c8ff709SChao Yu {
9624c8ff709SChao Yu 	struct dnode_of_data dn;
96391f0fb69SChao Yu 	unsigned int start_idx = cluster_idx <<
96491f0fb69SChao Yu 				F2FS_I(inode)->i_log_cluster_size;
9654c8ff709SChao Yu 	int ret;
9664c8ff709SChao Yu 
96791f0fb69SChao Yu 	set_new_dnode(&dn, inode, NULL, NULL, 0);
96891f0fb69SChao Yu 	ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
9694c8ff709SChao Yu 	if (ret) {
9704c8ff709SChao Yu 		if (ret == -ENOENT)
9714c8ff709SChao Yu 			ret = 0;
9724c8ff709SChao Yu 		goto fail;
9734c8ff709SChao Yu 	}
9744c8ff709SChao Yu 
975bbe1da7eSChao Yu 	if (f2fs_sanity_check_cluster(&dn)) {
976bbe1da7eSChao Yu 		ret = -EFSCORRUPTED;
977bbe1da7eSChao Yu 		goto fail;
978bbe1da7eSChao Yu 	}
979bbe1da7eSChao Yu 
9804c8ff709SChao Yu 	if (dn.data_blkaddr == COMPRESS_ADDR) {
981*f785cec2SYeongjin Gil 		if (type == CLUSTER_COMPR_BLKS)
982*f785cec2SYeongjin Gil 			ret = 1 + __f2fs_get_cluster_blocks(inode, &dn);
983*f785cec2SYeongjin Gil 		else if (type == CLUSTER_IS_COMPR)
9844c8ff709SChao Yu 			ret = 1;
985*f785cec2SYeongjin Gil 	} else if (type == CLUSTER_RAW_BLKS) {
986*f785cec2SYeongjin Gil 		ret = __f2fs_get_cluster_blocks(inode, &dn);
9871a67cbe1SChao Yu 	}
9884c8ff709SChao Yu fail:
9894c8ff709SChao Yu 	f2fs_put_dnode(&dn);
9904c8ff709SChao Yu 	return ret;
9914c8ff709SChao Yu }
9924c8ff709SChao Yu 
9931a67cbe1SChao Yu /* return # of compressed blocks in compressed cluster */
9941a67cbe1SChao Yu static int f2fs_compressed_blocks(struct compress_ctx *cc)
9951a67cbe1SChao Yu {
996*f785cec2SYeongjin Gil 	return __f2fs_cluster_blocks(cc->inode, cc->cluster_idx,
997*f785cec2SYeongjin Gil 		CLUSTER_COMPR_BLKS);
998*f785cec2SYeongjin Gil }
999*f785cec2SYeongjin Gil 
1000*f785cec2SYeongjin Gil /* return # of raw blocks in non-compressed cluster */
1001*f785cec2SYeongjin Gil static int f2fs_decompressed_blocks(struct inode *inode,
1002*f785cec2SYeongjin Gil 				unsigned int cluster_idx)
1003*f785cec2SYeongjin Gil {
1004*f785cec2SYeongjin Gil 	return __f2fs_cluster_blocks(inode, cluster_idx,
1005*f785cec2SYeongjin Gil 		CLUSTER_RAW_BLKS);
10061a67cbe1SChao Yu }
10071a67cbe1SChao Yu 
10082aaea533SChao Yu /* return whether cluster is compressed one or not */
10094c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
10104c8ff709SChao Yu {
101191f0fb69SChao Yu 	return __f2fs_cluster_blocks(inode,
101291f0fb69SChao Yu 		index >> F2FS_I(inode)->i_log_cluster_size,
1013*f785cec2SYeongjin Gil 		CLUSTER_IS_COMPR);
1014*f785cec2SYeongjin Gil }
1015*f785cec2SYeongjin Gil 
1016*f785cec2SYeongjin Gil /* return whether cluster contains non raw blocks or not */
1017*f785cec2SYeongjin Gil bool f2fs_is_sparse_cluster(struct inode *inode, pgoff_t index)
1018*f785cec2SYeongjin Gil {
1019*f785cec2SYeongjin Gil 	unsigned int cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size;
1020*f785cec2SYeongjin Gil 
1021*f785cec2SYeongjin Gil 	return f2fs_decompressed_blocks(inode, cluster_idx) !=
1022*f785cec2SYeongjin Gil 		F2FS_I(inode)->i_cluster_size;
10234c8ff709SChao Yu }
10244c8ff709SChao Yu 
10254c8ff709SChao Yu static bool cluster_may_compress(struct compress_ctx *cc)
10264c8ff709SChao Yu {
1027602a16d5SDaeho Jeong 	if (!f2fs_need_compress_data(cc->inode))
10284c8ff709SChao Yu 		return false;
10294c8ff709SChao Yu 	if (f2fs_is_atomic_file(cc->inode))
10304c8ff709SChao Yu 		return false;
10314c8ff709SChao Yu 	if (!f2fs_cluster_is_full(cc))
10324c8ff709SChao Yu 		return false;
1033dc35d73aSChao Yu 	if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
1034dc35d73aSChao Yu 		return false;
10355db479f0SChao Yu 	return !cluster_has_invalid_data(cc);
10364c8ff709SChao Yu }
10374c8ff709SChao Yu 
10384c8ff709SChao Yu static void set_cluster_writeback(struct compress_ctx *cc)
10394c8ff709SChao Yu {
10404c8ff709SChao Yu 	int i;
10414c8ff709SChao Yu 
10424c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
10434c8ff709SChao Yu 		if (cc->rpages[i])
10444c8ff709SChao Yu 			set_page_writeback(cc->rpages[i]);
10454c8ff709SChao Yu 	}
10464c8ff709SChao Yu }
10474c8ff709SChao Yu 
10482174035aSJaegeuk Kim static void cancel_cluster_writeback(struct compress_ctx *cc,
10492174035aSJaegeuk Kim 			struct compress_io_ctx *cic, int submitted)
10502174035aSJaegeuk Kim {
10512174035aSJaegeuk Kim 	int i;
10522174035aSJaegeuk Kim 
10532174035aSJaegeuk Kim 	/* Wait for submitted IOs. */
10542174035aSJaegeuk Kim 	if (submitted > 1) {
10552174035aSJaegeuk Kim 		f2fs_submit_merged_write(F2FS_I_SB(cc->inode), DATA);
10562174035aSJaegeuk Kim 		while (atomic_read(&cic->pending_pages) !=
10572174035aSJaegeuk Kim 					(cc->valid_nr_cpages - submitted + 1))
10582174035aSJaegeuk Kim 			f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
10592174035aSJaegeuk Kim 	}
10602174035aSJaegeuk Kim 
10612174035aSJaegeuk Kim 	/* Cancel writeback and stay locked. */
10622174035aSJaegeuk Kim 	for (i = 0; i < cc->cluster_size; i++) {
10632174035aSJaegeuk Kim 		if (i < submitted) {
10642174035aSJaegeuk Kim 			inode_inc_dirty_pages(cc->inode);
10652174035aSJaegeuk Kim 			lock_page(cc->rpages[i]);
10662174035aSJaegeuk Kim 		}
10672174035aSJaegeuk Kim 		clear_page_private_gcing(cc->rpages[i]);
10682174035aSJaegeuk Kim 		if (folio_test_writeback(page_folio(cc->rpages[i])))
10692174035aSJaegeuk Kim 			end_page_writeback(cc->rpages[i]);
10702174035aSJaegeuk Kim 	}
10712174035aSJaegeuk Kim }
10722174035aSJaegeuk Kim 
10734c8ff709SChao Yu static void set_cluster_dirty(struct compress_ctx *cc)
10744c8ff709SChao Yu {
10754c8ff709SChao Yu 	int i;
10764c8ff709SChao Yu 
10774c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++)
10784961acddSChao Yu 		if (cc->rpages[i]) {
10794c8ff709SChao Yu 			set_page_dirty(cc->rpages[i]);
10804961acddSChao Yu 			set_page_private_gcing(cc->rpages[i]);
10814961acddSChao Yu 		}
10824c8ff709SChao Yu }
10834c8ff709SChao Yu 
10844c8ff709SChao Yu static int prepare_compress_overwrite(struct compress_ctx *cc,
10854c8ff709SChao Yu 		struct page **pagep, pgoff_t index, void **fsdata)
10864c8ff709SChao Yu {
10874c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
10884c8ff709SChao Yu 	struct address_space *mapping = cc->inode->i_mapping;
10894c8ff709SChao Yu 	struct page *page;
10904c8ff709SChao Yu 	sector_t last_block_in_bio;
1091ffc143dbSMatthew Wilcox (Oracle) 	fgf_t fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
10924c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
10934c8ff709SChao Yu 	int i, ret;
10944c8ff709SChao Yu 
10954c8ff709SChao Yu retry:
109691f0fb69SChao Yu 	ret = f2fs_is_compressed_cluster(cc->inode, start_idx);
10974c8ff709SChao Yu 	if (ret <= 0)
10984c8ff709SChao Yu 		return ret;
10994c8ff709SChao Yu 
11004c8ff709SChao Yu 	ret = f2fs_init_compress_ctx(cc);
11014c8ff709SChao Yu 	if (ret)
11024c8ff709SChao Yu 		return ret;
11034c8ff709SChao Yu 
11044c8ff709SChao Yu 	/* keep page reference to avoid page reclaim */
11054c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
11064c8ff709SChao Yu 		page = f2fs_pagecache_get_page(mapping, start_idx + i,
11074c8ff709SChao Yu 							fgp_flag, GFP_NOFS);
11084c8ff709SChao Yu 		if (!page) {
11094c8ff709SChao Yu 			ret = -ENOMEM;
11104c8ff709SChao Yu 			goto unlock_pages;
11114c8ff709SChao Yu 		}
11124c8ff709SChao Yu 
11134c8ff709SChao Yu 		if (PageUptodate(page))
1114a949dc5fSChao Yu 			f2fs_put_page(page, 1);
11154c8ff709SChao Yu 		else
11164c8ff709SChao Yu 			f2fs_compress_ctx_add_page(cc, page);
11174c8ff709SChao Yu 	}
11184c8ff709SChao Yu 
11194c8ff709SChao Yu 	if (!f2fs_cluster_is_empty(cc)) {
11204c8ff709SChao Yu 		struct bio *bio = NULL;
11214c8ff709SChao Yu 
11224c8ff709SChao Yu 		ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
11236aeb084fSJaegeuk Kim 					&last_block_in_bio, NULL, true);
1124a949dc5fSChao Yu 		f2fs_put_rpages(cc);
11258bfbfb0dSChao Yu 		f2fs_destroy_compress_ctx(cc, true);
11264c8ff709SChao Yu 		if (ret)
1127a949dc5fSChao Yu 			goto out;
11284c8ff709SChao Yu 		if (bio)
1129bc29835aSChristoph Hellwig 			f2fs_submit_read_bio(sbi, bio, DATA);
11304c8ff709SChao Yu 
11314c8ff709SChao Yu 		ret = f2fs_init_compress_ctx(cc);
11324c8ff709SChao Yu 		if (ret)
1133a949dc5fSChao Yu 			goto out;
11344c8ff709SChao Yu 	}
11354c8ff709SChao Yu 
11364c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
11374c8ff709SChao Yu 		f2fs_bug_on(sbi, cc->rpages[i]);
11384c8ff709SChao Yu 
11394c8ff709SChao Yu 		page = find_lock_page(mapping, start_idx + i);
1140a949dc5fSChao Yu 		if (!page) {
1141a949dc5fSChao Yu 			/* page can be truncated */
1142a949dc5fSChao Yu 			goto release_and_retry;
1143a949dc5fSChao Yu 		}
11444c8ff709SChao Yu 
11454c8ff709SChao Yu 		f2fs_wait_on_page_writeback(page, DATA, true, true);
11464c8ff709SChao Yu 		f2fs_compress_ctx_add_page(cc, page);
11474c8ff709SChao Yu 
11484c8ff709SChao Yu 		if (!PageUptodate(page)) {
1149a949dc5fSChao Yu release_and_retry:
1150a949dc5fSChao Yu 			f2fs_put_rpages(cc);
11514c8ff709SChao Yu 			f2fs_unlock_rpages(cc, i + 1);
11528bfbfb0dSChao Yu 			f2fs_destroy_compress_ctx(cc, true);
11534c8ff709SChao Yu 			goto retry;
11544c8ff709SChao Yu 		}
11554c8ff709SChao Yu 	}
11564c8ff709SChao Yu 
11574c8ff709SChao Yu 	if (likely(!ret)) {
11584c8ff709SChao Yu 		*fsdata = cc->rpages;
11594c8ff709SChao Yu 		*pagep = cc->rpages[offset_in_cluster(cc, index)];
11604c8ff709SChao Yu 		return cc->cluster_size;
11614c8ff709SChao Yu 	}
11624c8ff709SChao Yu 
11634c8ff709SChao Yu unlock_pages:
1164a949dc5fSChao Yu 	f2fs_put_rpages(cc);
11654c8ff709SChao Yu 	f2fs_unlock_rpages(cc, i);
11668bfbfb0dSChao Yu 	f2fs_destroy_compress_ctx(cc, true);
1167a949dc5fSChao Yu out:
11684c8ff709SChao Yu 	return ret;
11694c8ff709SChao Yu }
11704c8ff709SChao Yu 
11714c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
11724c8ff709SChao Yu 		struct page **pagep, pgoff_t index, void **fsdata)
11734c8ff709SChao Yu {
11744c8ff709SChao Yu 	struct compress_ctx cc = {
11754c8ff709SChao Yu 		.inode = inode,
11764c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
11774c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
11784c8ff709SChao Yu 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
11794c8ff709SChao Yu 		.rpages = NULL,
11804c8ff709SChao Yu 		.nr_rpages = 0,
11814c8ff709SChao Yu 	};
11824c8ff709SChao Yu 
11834c8ff709SChao Yu 	return prepare_compress_overwrite(&cc, pagep, index, fsdata);
11844c8ff709SChao Yu }
11854c8ff709SChao Yu 
11864c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
11874c8ff709SChao Yu 					pgoff_t index, unsigned copied)
11884c8ff709SChao Yu 
11894c8ff709SChao Yu {
11904c8ff709SChao Yu 	struct compress_ctx cc = {
119131083031SChao Yu 		.inode = inode,
11924c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
11934c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
11944c8ff709SChao Yu 		.rpages = fsdata,
11954c8ff709SChao Yu 	};
11964c8ff709SChao Yu 	bool first_index = (index == cc.rpages[0]->index);
11974c8ff709SChao Yu 
11984c8ff709SChao Yu 	if (copied)
11994c8ff709SChao Yu 		set_cluster_dirty(&cc);
12004c8ff709SChao Yu 
12014c8ff709SChao Yu 	f2fs_put_rpages_wbc(&cc, NULL, false, 1);
12028bfbfb0dSChao Yu 	f2fs_destroy_compress_ctx(&cc, false);
12034c8ff709SChao Yu 
12044c8ff709SChao Yu 	return first_index;
12054c8ff709SChao Yu }
12064c8ff709SChao Yu 
12073265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
12083265d3dbSChao Yu {
12093265d3dbSChao Yu 	void *fsdata = NULL;
12103265d3dbSChao Yu 	struct page *pagep;
12113265d3dbSChao Yu 	int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
12123265d3dbSChao Yu 	pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
12133265d3dbSChao Yu 							log_cluster_size;
12143265d3dbSChao Yu 	int err;
12153265d3dbSChao Yu 
12163265d3dbSChao Yu 	err = f2fs_is_compressed_cluster(inode, start_idx);
12173265d3dbSChao Yu 	if (err < 0)
12183265d3dbSChao Yu 		return err;
12193265d3dbSChao Yu 
12203265d3dbSChao Yu 	/* truncate normal cluster */
12213265d3dbSChao Yu 	if (!err)
12223265d3dbSChao Yu 		return f2fs_do_truncate_blocks(inode, from, lock);
12233265d3dbSChao Yu 
12243265d3dbSChao Yu 	/* truncate compressed cluster */
12253265d3dbSChao Yu 	err = f2fs_prepare_compress_overwrite(inode, &pagep,
12263265d3dbSChao Yu 						start_idx, &fsdata);
12273265d3dbSChao Yu 
12283265d3dbSChao Yu 	/* should not be a normal cluster */
12293265d3dbSChao Yu 	f2fs_bug_on(F2FS_I_SB(inode), err == 0);
12303265d3dbSChao Yu 
12313265d3dbSChao Yu 	if (err <= 0)
12323265d3dbSChao Yu 		return err;
12333265d3dbSChao Yu 
12343265d3dbSChao Yu 	if (err > 0) {
12353265d3dbSChao Yu 		struct page **rpages = fsdata;
12363265d3dbSChao Yu 		int cluster_size = F2FS_I(inode)->i_cluster_size;
12373265d3dbSChao Yu 		int i;
12383265d3dbSChao Yu 
12393265d3dbSChao Yu 		for (i = cluster_size - 1; i >= 0; i--) {
12403265d3dbSChao Yu 			loff_t start = rpages[i]->index << PAGE_SHIFT;
12413265d3dbSChao Yu 
12423265d3dbSChao Yu 			if (from <= start) {
12433265d3dbSChao Yu 				zero_user_segment(rpages[i], 0, PAGE_SIZE);
12443265d3dbSChao Yu 			} else {
12453265d3dbSChao Yu 				zero_user_segment(rpages[i], from - start,
12463265d3dbSChao Yu 								PAGE_SIZE);
12473265d3dbSChao Yu 				break;
12483265d3dbSChao Yu 			}
12493265d3dbSChao Yu 		}
12503265d3dbSChao Yu 
12513265d3dbSChao Yu 		f2fs_compress_write_end(inode, fsdata, start_idx, true);
12523265d3dbSChao Yu 	}
12533265d3dbSChao Yu 	return 0;
12543265d3dbSChao Yu }
12553265d3dbSChao Yu 
12564c8ff709SChao Yu static int f2fs_write_compressed_pages(struct compress_ctx *cc,
12574c8ff709SChao Yu 					int *submitted,
12584c8ff709SChao Yu 					struct writeback_control *wbc,
12594c8ff709SChao Yu 					enum iostat_type io_type)
12604c8ff709SChao Yu {
12614c8ff709SChao Yu 	struct inode *inode = cc->inode;
12624c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
12634c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
12644c8ff709SChao Yu 	struct f2fs_io_info fio = {
12654c8ff709SChao Yu 		.sbi = sbi,
12664c8ff709SChao Yu 		.ino = cc->inode->i_ino,
12674c8ff709SChao Yu 		.type = DATA,
12684c8ff709SChao Yu 		.op = REQ_OP_WRITE,
12694c8ff709SChao Yu 		.op_flags = wbc_to_write_flags(wbc),
12704c8ff709SChao Yu 		.old_blkaddr = NEW_ADDR,
12714c8ff709SChao Yu 		.page = NULL,
12724c8ff709SChao Yu 		.encrypted_page = NULL,
12734c8ff709SChao Yu 		.compressed_page = NULL,
12744c8ff709SChao Yu 		.io_type = io_type,
12754c8ff709SChao Yu 		.io_wbc = wbc,
12762eae077eSChao Yu 		.encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode) ?
12772eae077eSChao Yu 									1 : 0,
12784c8ff709SChao Yu 	};
12794c8ff709SChao Yu 	struct dnode_of_data dn;
12804c8ff709SChao Yu 	struct node_info ni;
12814c8ff709SChao Yu 	struct compress_io_ctx *cic;
12824c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
12834c8ff709SChao Yu 	unsigned int last_index = cc->cluster_size - 1;
12844c8ff709SChao Yu 	loff_t psize;
12854c8ff709SChao Yu 	int i, err;
1286f082c6b2SChao Yu 	bool quota_inode = IS_NOQUOTA(inode);
12874c8ff709SChao Yu 
1288146949deSJinyoung CHOI 	/* we should bypass data pages to proceed the kworker jobs */
1289ee68d271SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1290ee68d271SChao Yu 		mapping_set_error(cc->rpages[0]->mapping, -EIO);
1291ee68d271SChao Yu 		goto out_free;
1292ee68d271SChao Yu 	}
1293ee68d271SChao Yu 
1294f082c6b2SChao Yu 	if (quota_inode) {
129579963d96SChao Yu 		/*
129679963d96SChao Yu 		 * We need to wait for node_write to avoid block allocation during
129779963d96SChao Yu 		 * checkpoint. This can only happen to quota writes which can cause
129879963d96SChao Yu 		 * the below discard race condition.
129979963d96SChao Yu 		 */
1300e4544b63STim Murray 		f2fs_down_read(&sbi->node_write);
130179963d96SChao Yu 	} else if (!f2fs_trylock_op(sbi)) {
130231083031SChao Yu 		goto out_free;
130379963d96SChao Yu 	}
13044c8ff709SChao Yu 
1305df77fbd8SChao Yu 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
13064c8ff709SChao Yu 
13074c8ff709SChao Yu 	err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
13084c8ff709SChao Yu 	if (err)
13094c8ff709SChao Yu 		goto out_unlock_op;
13104c8ff709SChao Yu 
13114c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
1312a2ced1ceSChao Yu 		if (data_blkaddr(dn.inode, dn.node_page,
13134c8ff709SChao Yu 					dn.ofs_in_node + i) == NULL_ADDR)
13144c8ff709SChao Yu 			goto out_put_dnode;
13154c8ff709SChao Yu 	}
13164c8ff709SChao Yu 
13174c8ff709SChao Yu 	psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
13184c8ff709SChao Yu 
1319a9419b63SJaegeuk Kim 	err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
13204c8ff709SChao Yu 	if (err)
13214c8ff709SChao Yu 		goto out_put_dnode;
13224c8ff709SChao Yu 
13234c8ff709SChao Yu 	fio.version = ni.version;
13244c8ff709SChao Yu 
132532410577SChao Yu 	cic = f2fs_kmem_cache_alloc(cic_entry_slab, GFP_F2FS_ZERO, false, sbi);
13264c8ff709SChao Yu 	if (!cic)
13274c8ff709SChao Yu 		goto out_put_dnode;
13284c8ff709SChao Yu 
13294c8ff709SChao Yu 	cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
13304c8ff709SChao Yu 	cic->inode = inode;
13313271d7ebSFengnan Chang 	atomic_set(&cic->pending_pages, cc->valid_nr_cpages);
133231083031SChao Yu 	cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
13334c8ff709SChao Yu 	if (!cic->rpages)
13344c8ff709SChao Yu 		goto out_put_cic;
13354c8ff709SChao Yu 
13364c8ff709SChao Yu 	cic->nr_rpages = cc->cluster_size;
13374c8ff709SChao Yu 
13383271d7ebSFengnan Chang 	for (i = 0; i < cc->valid_nr_cpages; i++) {
13394c8ff709SChao Yu 		f2fs_set_compressed_page(cc->cpages[i], inode,
1340887347a0SChao Yu 					cc->rpages[i + 1]->index, cic);
13414c8ff709SChao Yu 		fio.compressed_page = cc->cpages[i];
1342f567adb0SChao Yu 
1343f567adb0SChao Yu 		fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1344f567adb0SChao Yu 						dn.ofs_in_node + i + 1);
1345f567adb0SChao Yu 
1346f567adb0SChao Yu 		/* wait for GCed page writeback via META_MAPPING */
1347f567adb0SChao Yu 		f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1348f567adb0SChao Yu 
13494c8ff709SChao Yu 		if (fio.encrypted) {
13504c8ff709SChao Yu 			fio.page = cc->rpages[i + 1];
13514c8ff709SChao Yu 			err = f2fs_encrypt_one_page(&fio);
13524c8ff709SChao Yu 			if (err)
13534c8ff709SChao Yu 				goto out_destroy_crypt;
13544c8ff709SChao Yu 			cc->cpages[i] = fio.encrypted_page;
13554c8ff709SChao Yu 		}
13564c8ff709SChao Yu 	}
13574c8ff709SChao Yu 
13584c8ff709SChao Yu 	set_cluster_writeback(cc);
13594c8ff709SChao Yu 
13604c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++)
13614c8ff709SChao Yu 		cic->rpages[i] = cc->rpages[i];
13624c8ff709SChao Yu 
13634c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
13644c8ff709SChao Yu 		block_t blkaddr;
13654c8ff709SChao Yu 
1366a2ced1ceSChao Yu 		blkaddr = f2fs_data_blkaddr(&dn);
136795978caaSChao Yu 		fio.page = cc->rpages[i];
13684c8ff709SChao Yu 		fio.old_blkaddr = blkaddr;
13694c8ff709SChao Yu 
13704c8ff709SChao Yu 		/* cluster header */
13714c8ff709SChao Yu 		if (i == 0) {
13724c8ff709SChao Yu 			if (blkaddr == COMPRESS_ADDR)
13734c8ff709SChao Yu 				fio.compr_blocks++;
13744c8ff709SChao Yu 			if (__is_valid_data_blkaddr(blkaddr))
13754c8ff709SChao Yu 				f2fs_invalidate_blocks(sbi, blkaddr);
13764c8ff709SChao Yu 			f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
13774c8ff709SChao Yu 			goto unlock_continue;
13784c8ff709SChao Yu 		}
13794c8ff709SChao Yu 
13804c8ff709SChao Yu 		if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
13814c8ff709SChao Yu 			fio.compr_blocks++;
13824c8ff709SChao Yu 
13833271d7ebSFengnan Chang 		if (i > cc->valid_nr_cpages) {
13844c8ff709SChao Yu 			if (__is_valid_data_blkaddr(blkaddr)) {
13854c8ff709SChao Yu 				f2fs_invalidate_blocks(sbi, blkaddr);
13864c8ff709SChao Yu 				f2fs_update_data_blkaddr(&dn, NEW_ADDR);
13874c8ff709SChao Yu 			}
13884c8ff709SChao Yu 			goto unlock_continue;
13894c8ff709SChao Yu 		}
13904c8ff709SChao Yu 
13914c8ff709SChao Yu 		f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
13924c8ff709SChao Yu 
13934c8ff709SChao Yu 		if (fio.encrypted)
13944c8ff709SChao Yu 			fio.encrypted_page = cc->cpages[i - 1];
13954c8ff709SChao Yu 		else
13964c8ff709SChao Yu 			fio.compressed_page = cc->cpages[i - 1];
13974c8ff709SChao Yu 
13984c8ff709SChao Yu 		cc->cpages[i - 1] = NULL;
13992174035aSJaegeuk Kim 		fio.submitted = 0;
14004c8ff709SChao Yu 		f2fs_outplace_write_data(&dn, &fio);
14012174035aSJaegeuk Kim 		if (unlikely(!fio.submitted)) {
14022174035aSJaegeuk Kim 			cancel_cluster_writeback(cc, cic, i);
14032174035aSJaegeuk Kim 
14042174035aSJaegeuk Kim 			/* To call fscrypt_finalize_bounce_page */
14052174035aSJaegeuk Kim 			i = cc->valid_nr_cpages;
14062174035aSJaegeuk Kim 			*submitted = 0;
14072174035aSJaegeuk Kim 			goto out_destroy_crypt;
14082174035aSJaegeuk Kim 		}
14094c8ff709SChao Yu 		(*submitted)++;
14104c8ff709SChao Yu unlock_continue:
14114c8ff709SChao Yu 		inode_dec_dirty_pages(cc->inode);
14124c8ff709SChao Yu 		unlock_page(fio.page);
14134c8ff709SChao Yu 	}
14144c8ff709SChao Yu 
14154c8ff709SChao Yu 	if (fio.compr_blocks)
14164c8ff709SChao Yu 		f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
14173271d7ebSFengnan Chang 	f2fs_i_compr_blocks_update(inode, cc->valid_nr_cpages, true);
14183271d7ebSFengnan Chang 	add_compr_block_stat(inode, cc->valid_nr_cpages);
14194c8ff709SChao Yu 
14204c8ff709SChao Yu 	set_inode_flag(cc->inode, FI_APPEND_WRITE);
14214c8ff709SChao Yu 
14224c8ff709SChao Yu 	f2fs_put_dnode(&dn);
1423f082c6b2SChao Yu 	if (quota_inode)
1424e4544b63STim Murray 		f2fs_up_read(&sbi->node_write);
142579963d96SChao Yu 	else
14264c8ff709SChao Yu 		f2fs_unlock_op(sbi);
14274c8ff709SChao Yu 
1428c10c9820SChao Yu 	spin_lock(&fi->i_size_lock);
14294c8ff709SChao Yu 	if (fi->last_disk_size < psize)
14304c8ff709SChao Yu 		fi->last_disk_size = psize;
1431c10c9820SChao Yu 	spin_unlock(&fi->i_size_lock);
14324c8ff709SChao Yu 
14334c8ff709SChao Yu 	f2fs_put_rpages(cc);
143431083031SChao Yu 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
143531083031SChao Yu 	cc->cpages = NULL;
14368bfbfb0dSChao Yu 	f2fs_destroy_compress_ctx(cc, false);
14374c8ff709SChao Yu 	return 0;
14384c8ff709SChao Yu 
14394c8ff709SChao Yu out_destroy_crypt:
144031083031SChao Yu 	page_array_free(cc->inode, cic->rpages, cc->cluster_size);
14414c8ff709SChao Yu 
14422174035aSJaegeuk Kim 	for (--i; i >= 0; i--) {
14432174035aSJaegeuk Kim 		if (!cc->cpages[i])
14442174035aSJaegeuk Kim 			continue;
14454c8ff709SChao Yu 		fscrypt_finalize_bounce_page(&cc->cpages[i]);
14462174035aSJaegeuk Kim 	}
14474c8ff709SChao Yu out_put_cic:
1448c68d6c88SChao Yu 	kmem_cache_free(cic_entry_slab, cic);
14494c8ff709SChao Yu out_put_dnode:
14504c8ff709SChao Yu 	f2fs_put_dnode(&dn);
14514c8ff709SChao Yu out_unlock_op:
1452f082c6b2SChao Yu 	if (quota_inode)
1453e4544b63STim Murray 		f2fs_up_read(&sbi->node_write);
145479963d96SChao Yu 	else
14554c8ff709SChao Yu 		f2fs_unlock_op(sbi);
145631083031SChao Yu out_free:
14573271d7ebSFengnan Chang 	for (i = 0; i < cc->valid_nr_cpages; i++) {
1458827f0284SJaegeuk Kim 		f2fs_compress_free_page(cc->cpages[i]);
1459827f0284SJaegeuk Kim 		cc->cpages[i] = NULL;
1460827f0284SJaegeuk Kim 	}
146131083031SChao Yu 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
146231083031SChao Yu 	cc->cpages = NULL;
14634c8ff709SChao Yu 	return -EAGAIN;
14644c8ff709SChao Yu }
14654c8ff709SChao Yu 
14664c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
14674c8ff709SChao Yu {
14684c8ff709SChao Yu 	struct f2fs_sb_info *sbi = bio->bi_private;
14694c8ff709SChao Yu 	struct compress_io_ctx *cic =
14704c8ff709SChao Yu 			(struct compress_io_ctx *)page_private(page);
14718a430dd4SChao Yu 	enum count_type type = WB_DATA_TYPE(page,
14728a430dd4SChao Yu 				f2fs_is_compressed_page(page));
14734c8ff709SChao Yu 	int i;
14744c8ff709SChao Yu 
14754c8ff709SChao Yu 	if (unlikely(bio->bi_status))
14764c8ff709SChao Yu 		mapping_set_error(cic->inode->i_mapping, -EIO);
14774c8ff709SChao Yu 
14785e6bbde9SChao Yu 	f2fs_compress_free_page(page);
14794c8ff709SChao Yu 
14808a430dd4SChao Yu 	dec_page_count(sbi, type);
14814c8ff709SChao Yu 
1482e6c3948dSChao Yu 	if (atomic_dec_return(&cic->pending_pages))
14834c8ff709SChao Yu 		return;
14844c8ff709SChao Yu 
14854c8ff709SChao Yu 	for (i = 0; i < cic->nr_rpages; i++) {
14864c8ff709SChao Yu 		WARN_ON(!cic->rpages[i]);
1487b763f3beSChao Yu 		clear_page_private_gcing(cic->rpages[i]);
14884c8ff709SChao Yu 		end_page_writeback(cic->rpages[i]);
14894c8ff709SChao Yu 	}
14904c8ff709SChao Yu 
149131083031SChao Yu 	page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
1492c68d6c88SChao Yu 	kmem_cache_free(cic_entry_slab, cic);
14934c8ff709SChao Yu }
14944c8ff709SChao Yu 
14954c8ff709SChao Yu static int f2fs_write_raw_pages(struct compress_ctx *cc,
1496fd244524SChao Yu 					int *submitted_p,
14974c8ff709SChao Yu 					struct writeback_control *wbc,
14984c8ff709SChao Yu 					enum iostat_type io_type)
14994c8ff709SChao Yu {
15004c8ff709SChao Yu 	struct address_space *mapping = cc->inode->i_mapping;
1501fd244524SChao Yu 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
1502fd244524SChao Yu 	int submitted, compr_blocks, i;
1503fd244524SChao Yu 	int ret = 0;
15044c8ff709SChao Yu 
15054c8ff709SChao Yu 	compr_blocks = f2fs_compressed_blocks(cc);
15067377e853SHyeong-Jun Kim 
15077377e853SHyeong-Jun Kim 	for (i = 0; i < cc->cluster_size; i++) {
15087377e853SHyeong-Jun Kim 		if (!cc->rpages[i])
15097377e853SHyeong-Jun Kim 			continue;
15107377e853SHyeong-Jun Kim 
15117377e853SHyeong-Jun Kim 		redirty_page_for_writepage(wbc, cc->rpages[i]);
15127377e853SHyeong-Jun Kim 		unlock_page(cc->rpages[i]);
15134c8ff709SChao Yu 	}
15144c8ff709SChao Yu 
15157377e853SHyeong-Jun Kim 	if (compr_blocks < 0)
15167377e853SHyeong-Jun Kim 		return compr_blocks;
15177377e853SHyeong-Jun Kim 
1518fd244524SChao Yu 	/* overwrite compressed cluster w/ normal cluster */
1519fd244524SChao Yu 	if (compr_blocks > 0)
1520fd244524SChao Yu 		f2fs_lock_op(sbi);
1521fd244524SChao Yu 
15224c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
15234c8ff709SChao Yu 		if (!cc->rpages[i])
15244c8ff709SChao Yu 			continue;
15254c8ff709SChao Yu retry_write:
15267377e853SHyeong-Jun Kim 		lock_page(cc->rpages[i]);
15277377e853SHyeong-Jun Kim 
15284c8ff709SChao Yu 		if (cc->rpages[i]->mapping != mapping) {
15297377e853SHyeong-Jun Kim continue_unlock:
15304c8ff709SChao Yu 			unlock_page(cc->rpages[i]);
15314c8ff709SChao Yu 			continue;
15324c8ff709SChao Yu 		}
15334c8ff709SChao Yu 
15347377e853SHyeong-Jun Kim 		if (!PageDirty(cc->rpages[i]))
15357377e853SHyeong-Jun Kim 			goto continue_unlock;
15367377e853SHyeong-Jun Kim 
153716778aeaSJaegeuk Kim 		if (folio_test_writeback(page_folio(cc->rpages[i]))) {
1538babedcbaSYangtao Li 			if (wbc->sync_mode == WB_SYNC_NONE)
1539babedcbaSYangtao Li 				goto continue_unlock;
1540babedcbaSYangtao Li 			f2fs_wait_on_page_writeback(cc->rpages[i], DATA, true, true);
1541babedcbaSYangtao Li 		}
1542babedcbaSYangtao Li 
15437377e853SHyeong-Jun Kim 		if (!clear_page_dirty_for_io(cc->rpages[i]))
15447377e853SHyeong-Jun Kim 			goto continue_unlock;
15454c8ff709SChao Yu 
1546fd244524SChao Yu 		ret = f2fs_write_single_data_page(cc->rpages[i], &submitted,
15474c8ff709SChao Yu 						NULL, NULL, wbc, io_type,
15483afae09fSChao Yu 						compr_blocks, false);
15494c8ff709SChao Yu 		if (ret) {
15504c8ff709SChao Yu 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
15514c8ff709SChao Yu 				unlock_page(cc->rpages[i]);
15524c8ff709SChao Yu 				ret = 0;
15534c8ff709SChao Yu 			} else if (ret == -EAGAIN) {
1554fd244524SChao Yu 				ret = 0;
1555466357dcSChao Yu 				/*
1556466357dcSChao Yu 				 * for quota file, just redirty left pages to
1557466357dcSChao Yu 				 * avoid deadlock caused by cluster update race
1558466357dcSChao Yu 				 * from foreground operation.
1559466357dcSChao Yu 				 */
15607377e853SHyeong-Jun Kim 				if (IS_NOQUOTA(cc->inode))
1561fd244524SChao Yu 					goto out;
1562a64239d0SNeilBrown 				f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
15634c8ff709SChao Yu 				goto retry_write;
15644c8ff709SChao Yu 			}
1565fd244524SChao Yu 			goto out;
1566fd244524SChao Yu 		}
1567fd244524SChao Yu 
1568fd244524SChao Yu 		*submitted_p += submitted;
1569fd244524SChao Yu 	}
1570fd244524SChao Yu 
1571fd244524SChao Yu out:
1572fd244524SChao Yu 	if (compr_blocks > 0)
1573fd244524SChao Yu 		f2fs_unlock_op(sbi);
1574fd244524SChao Yu 
1575fd244524SChao Yu 	f2fs_balance_fs(sbi, true);
15767377e853SHyeong-Jun Kim 	return ret;
15774c8ff709SChao Yu }
15784c8ff709SChao Yu 
15794c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
15804c8ff709SChao Yu 					int *submitted,
15814c8ff709SChao Yu 					struct writeback_control *wbc,
15824c8ff709SChao Yu 					enum iostat_type io_type)
15834c8ff709SChao Yu {
15844c8ff709SChao Yu 	int err;
15854c8ff709SChao Yu 
15864c8ff709SChao Yu 	*submitted = 0;
15874c8ff709SChao Yu 	if (cluster_may_compress(cc)) {
15884c8ff709SChao Yu 		err = f2fs_compress_pages(cc);
15894c8ff709SChao Yu 		if (err == -EAGAIN) {
159009631cf3SDaeho Jeong 			add_compr_block_stat(cc->inode, cc->cluster_size);
15914c8ff709SChao Yu 			goto write;
15924c8ff709SChao Yu 		} else if (err) {
15934c8ff709SChao Yu 			f2fs_put_rpages_wbc(cc, wbc, true, 1);
15944c8ff709SChao Yu 			goto destroy_out;
15954c8ff709SChao Yu 		}
15964c8ff709SChao Yu 
15974c8ff709SChao Yu 		err = f2fs_write_compressed_pages(cc, submitted,
15984c8ff709SChao Yu 							wbc, io_type);
15994c8ff709SChao Yu 		if (!err)
16004c8ff709SChao Yu 			return 0;
16014c8ff709SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
16024c8ff709SChao Yu 	}
16034c8ff709SChao Yu write:
16044c8ff709SChao Yu 	f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
16054c8ff709SChao Yu 
16064c8ff709SChao Yu 	err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
16074c8ff709SChao Yu 	f2fs_put_rpages_wbc(cc, wbc, false, 0);
16084c8ff709SChao Yu destroy_out:
16098bfbfb0dSChao Yu 	f2fs_destroy_compress_ctx(cc, false);
16104c8ff709SChao Yu 	return err;
16114c8ff709SChao Yu }
16124c8ff709SChao Yu 
1613bff139b4SDaeho Jeong static inline bool allow_memalloc_for_decomp(struct f2fs_sb_info *sbi,
1614bff139b4SDaeho Jeong 		bool pre_alloc)
1615bff139b4SDaeho Jeong {
1616bff139b4SDaeho Jeong 	return pre_alloc ^ f2fs_low_mem_mode(sbi);
1617bff139b4SDaeho Jeong }
1618bff139b4SDaeho Jeong 
1619bff139b4SDaeho Jeong static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
1620bff139b4SDaeho Jeong 		bool pre_alloc)
1621bff139b4SDaeho Jeong {
1622bff139b4SDaeho Jeong 	const struct f2fs_compress_ops *cops =
1623bff139b4SDaeho Jeong 		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
1624bff139b4SDaeho Jeong 	int i;
1625bff139b4SDaeho Jeong 
1626bff139b4SDaeho Jeong 	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
1627bff139b4SDaeho Jeong 		return 0;
1628bff139b4SDaeho Jeong 
1629bff139b4SDaeho Jeong 	dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
1630bff139b4SDaeho Jeong 	if (!dic->tpages)
1631bff139b4SDaeho Jeong 		return -ENOMEM;
1632bff139b4SDaeho Jeong 
1633bff139b4SDaeho Jeong 	for (i = 0; i < dic->cluster_size; i++) {
1634bff139b4SDaeho Jeong 		if (dic->rpages[i]) {
1635bff139b4SDaeho Jeong 			dic->tpages[i] = dic->rpages[i];
1636bff139b4SDaeho Jeong 			continue;
1637bff139b4SDaeho Jeong 		}
1638bff139b4SDaeho Jeong 
1639bff139b4SDaeho Jeong 		dic->tpages[i] = f2fs_compress_alloc_page();
1640bff139b4SDaeho Jeong 	}
1641bff139b4SDaeho Jeong 
1642bff139b4SDaeho Jeong 	dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
1643bff139b4SDaeho Jeong 	if (!dic->rbuf)
1644bff139b4SDaeho Jeong 		return -ENOMEM;
1645bff139b4SDaeho Jeong 
1646bff139b4SDaeho Jeong 	dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
1647bff139b4SDaeho Jeong 	if (!dic->cbuf)
1648bff139b4SDaeho Jeong 		return -ENOMEM;
1649bff139b4SDaeho Jeong 
16508140654eSZhang Qilong 	if (cops->init_decompress_ctx)
16518140654eSZhang Qilong 		return cops->init_decompress_ctx(dic);
1652bff139b4SDaeho Jeong 
1653bff139b4SDaeho Jeong 	return 0;
1654bff139b4SDaeho Jeong }
1655bff139b4SDaeho Jeong 
1656bff139b4SDaeho Jeong static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
1657bff139b4SDaeho Jeong 		bool bypass_destroy_callback, bool pre_alloc)
1658bff139b4SDaeho Jeong {
1659bff139b4SDaeho Jeong 	const struct f2fs_compress_ops *cops =
1660bff139b4SDaeho Jeong 		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
1661bff139b4SDaeho Jeong 
1662bff139b4SDaeho Jeong 	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
1663bff139b4SDaeho Jeong 		return;
1664bff139b4SDaeho Jeong 
1665bff139b4SDaeho Jeong 	if (!bypass_destroy_callback && cops->destroy_decompress_ctx)
1666bff139b4SDaeho Jeong 		cops->destroy_decompress_ctx(dic);
1667bff139b4SDaeho Jeong 
1668bff139b4SDaeho Jeong 	if (dic->cbuf)
1669bff139b4SDaeho Jeong 		vm_unmap_ram(dic->cbuf, dic->nr_cpages);
1670bff139b4SDaeho Jeong 
1671bff139b4SDaeho Jeong 	if (dic->rbuf)
1672bff139b4SDaeho Jeong 		vm_unmap_ram(dic->rbuf, dic->cluster_size);
1673bff139b4SDaeho Jeong }
1674bff139b4SDaeho Jeong 
1675bff139b4SDaeho Jeong static void f2fs_free_dic(struct decompress_io_ctx *dic,
1676bff139b4SDaeho Jeong 		bool bypass_destroy_callback);
16777f59b277SEric Biggers 
16784c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
16794c8ff709SChao Yu {
16804c8ff709SChao Yu 	struct decompress_io_ctx *dic;
16814c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
1682bff139b4SDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1683bff139b4SDaeho Jeong 	int i, ret;
16844c8ff709SChao Yu 
1685bff139b4SDaeho Jeong 	dic = f2fs_kmem_cache_alloc(dic_entry_slab, GFP_F2FS_ZERO, false, sbi);
16864c8ff709SChao Yu 	if (!dic)
16874c8ff709SChao Yu 		return ERR_PTR(-ENOMEM);
16884c8ff709SChao Yu 
168931083031SChao Yu 	dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
16904c8ff709SChao Yu 	if (!dic->rpages) {
1691c68d6c88SChao Yu 		kmem_cache_free(dic_entry_slab, dic);
16924c8ff709SChao Yu 		return ERR_PTR(-ENOMEM);
16934c8ff709SChao Yu 	}
16944c8ff709SChao Yu 
16954c8ff709SChao Yu 	dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
16964c8ff709SChao Yu 	dic->inode = cc->inode;
16977f59b277SEric Biggers 	atomic_set(&dic->remaining_pages, cc->nr_cpages);
16984c8ff709SChao Yu 	dic->cluster_idx = cc->cluster_idx;
16994c8ff709SChao Yu 	dic->cluster_size = cc->cluster_size;
17004c8ff709SChao Yu 	dic->log_cluster_size = cc->log_cluster_size;
17014c8ff709SChao Yu 	dic->nr_cpages = cc->nr_cpages;
17027f59b277SEric Biggers 	refcount_set(&dic->refcnt, 1);
17034c8ff709SChao Yu 	dic->failed = false;
17047f59b277SEric Biggers 	dic->need_verity = f2fs_need_verity(cc->inode, start_idx);
17054c8ff709SChao Yu 
17064c8ff709SChao Yu 	for (i = 0; i < dic->cluster_size; i++)
17074c8ff709SChao Yu 		dic->rpages[i] = cc->rpages[i];
17084c8ff709SChao Yu 	dic->nr_rpages = cc->cluster_size;
17094c8ff709SChao Yu 
171031083031SChao Yu 	dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
1711bff139b4SDaeho Jeong 	if (!dic->cpages) {
1712bff139b4SDaeho Jeong 		ret = -ENOMEM;
17134c8ff709SChao Yu 		goto out_free;
1714bff139b4SDaeho Jeong 	}
17154c8ff709SChao Yu 
17164c8ff709SChao Yu 	for (i = 0; i < dic->nr_cpages; i++) {
17174c8ff709SChao Yu 		struct page *page;
17184c8ff709SChao Yu 
17195e6bbde9SChao Yu 		page = f2fs_compress_alloc_page();
17204c8ff709SChao Yu 		f2fs_set_compressed_page(page, cc->inode,
1721887347a0SChao Yu 					start_idx + i + 1, dic);
17224c8ff709SChao Yu 		dic->cpages[i] = page;
17234c8ff709SChao Yu 	}
17244c8ff709SChao Yu 
1725bff139b4SDaeho Jeong 	ret = f2fs_prepare_decomp_mem(dic, true);
1726bff139b4SDaeho Jeong 	if (ret)
1727bff139b4SDaeho Jeong 		goto out_free;
1728bff139b4SDaeho Jeong 
17294c8ff709SChao Yu 	return dic;
17304c8ff709SChao Yu 
17314c8ff709SChao Yu out_free:
1732bff139b4SDaeho Jeong 	f2fs_free_dic(dic, true);
1733bff139b4SDaeho Jeong 	return ERR_PTR(ret);
17344c8ff709SChao Yu }
17354c8ff709SChao Yu 
1736bff139b4SDaeho Jeong static void f2fs_free_dic(struct decompress_io_ctx *dic,
1737bff139b4SDaeho Jeong 		bool bypass_destroy_callback)
17384c8ff709SChao Yu {
17394c8ff709SChao Yu 	int i;
17404c8ff709SChao Yu 
1741bff139b4SDaeho Jeong 	f2fs_release_decomp_mem(dic, bypass_destroy_callback, true);
1742bff139b4SDaeho Jeong 
17434c8ff709SChao Yu 	if (dic->tpages) {
17444c8ff709SChao Yu 		for (i = 0; i < dic->cluster_size; i++) {
17454c8ff709SChao Yu 			if (dic->rpages[i])
17464c8ff709SChao Yu 				continue;
17478908e753SChao Yu 			if (!dic->tpages[i])
17488908e753SChao Yu 				continue;
17495e6bbde9SChao Yu 			f2fs_compress_free_page(dic->tpages[i]);
17504c8ff709SChao Yu 		}
175131083031SChao Yu 		page_array_free(dic->inode, dic->tpages, dic->cluster_size);
17524c8ff709SChao Yu 	}
17534c8ff709SChao Yu 
17544c8ff709SChao Yu 	if (dic->cpages) {
17554c8ff709SChao Yu 		for (i = 0; i < dic->nr_cpages; i++) {
17564c8ff709SChao Yu 			if (!dic->cpages[i])
17574c8ff709SChao Yu 				continue;
17585e6bbde9SChao Yu 			f2fs_compress_free_page(dic->cpages[i]);
17594c8ff709SChao Yu 		}
176031083031SChao Yu 		page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
17614c8ff709SChao Yu 	}
17624c8ff709SChao Yu 
176331083031SChao Yu 	page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
1764c68d6c88SChao Yu 	kmem_cache_free(dic_entry_slab, dic);
17654c8ff709SChao Yu }
17664c8ff709SChao Yu 
1767bff139b4SDaeho Jeong static void f2fs_late_free_dic(struct work_struct *work)
17687f59b277SEric Biggers {
1769bff139b4SDaeho Jeong 	struct decompress_io_ctx *dic =
1770bff139b4SDaeho Jeong 		container_of(work, struct decompress_io_ctx, free_work);
1771bff139b4SDaeho Jeong 
1772bff139b4SDaeho Jeong 	f2fs_free_dic(dic, false);
1773bff139b4SDaeho Jeong }
1774bff139b4SDaeho Jeong 
1775bff139b4SDaeho Jeong static void f2fs_put_dic(struct decompress_io_ctx *dic, bool in_task)
1776bff139b4SDaeho Jeong {
1777bff139b4SDaeho Jeong 	if (refcount_dec_and_test(&dic->refcnt)) {
1778bff139b4SDaeho Jeong 		if (in_task) {
1779bff139b4SDaeho Jeong 			f2fs_free_dic(dic, false);
1780bff139b4SDaeho Jeong 		} else {
1781bff139b4SDaeho Jeong 			INIT_WORK(&dic->free_work, f2fs_late_free_dic);
1782bff139b4SDaeho Jeong 			queue_work(F2FS_I_SB(dic->inode)->post_read_wq,
1783bff139b4SDaeho Jeong 					&dic->free_work);
1784bff139b4SDaeho Jeong 		}
1785bff139b4SDaeho Jeong 	}
17867f59b277SEric Biggers }
17877f59b277SEric Biggers 
17887f59b277SEric Biggers static void f2fs_verify_cluster(struct work_struct *work)
17897f59b277SEric Biggers {
17907f59b277SEric Biggers 	struct decompress_io_ctx *dic =
17917f59b277SEric Biggers 		container_of(work, struct decompress_io_ctx, verity_work);
17927f59b277SEric Biggers 	int i;
17937f59b277SEric Biggers 
179498dc08baSEric Biggers 	/* Verify, update, and unlock the decompressed pages. */
17957f59b277SEric Biggers 	for (i = 0; i < dic->cluster_size; i++) {
17967f59b277SEric Biggers 		struct page *rpage = dic->rpages[i];
17977f59b277SEric Biggers 
179898dc08baSEric Biggers 		if (!rpage)
179998dc08baSEric Biggers 			continue;
180098dc08baSEric Biggers 
180198dc08baSEric Biggers 		if (fsverity_verify_page(rpage))
180298dc08baSEric Biggers 			SetPageUptodate(rpage);
180398dc08baSEric Biggers 		else
180498dc08baSEric Biggers 			ClearPageUptodate(rpage);
180598dc08baSEric Biggers 		unlock_page(rpage);
18067f59b277SEric Biggers 	}
18077f59b277SEric Biggers 
180898dc08baSEric Biggers 	f2fs_put_dic(dic, true);
18097f59b277SEric Biggers }
18107f59b277SEric Biggers 
18117f59b277SEric Biggers /*
18127f59b277SEric Biggers  * This is called when a compressed cluster has been decompressed
18137f59b277SEric Biggers  * (or failed to be read and/or decompressed).
18147f59b277SEric Biggers  */
1815bff139b4SDaeho Jeong void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
1816bff139b4SDaeho Jeong 				bool in_task)
18177f59b277SEric Biggers {
181898dc08baSEric Biggers 	int i;
181998dc08baSEric Biggers 
18207f59b277SEric Biggers 	if (!failed && dic->need_verity) {
18217f59b277SEric Biggers 		/*
18227f59b277SEric Biggers 		 * Note that to avoid deadlocks, the verity work can't be done
18237f59b277SEric Biggers 		 * on the decompression workqueue.  This is because verifying
18247f59b277SEric Biggers 		 * the data pages can involve reading metadata pages from the
18257f59b277SEric Biggers 		 * file, and these metadata pages may be compressed.
18267f59b277SEric Biggers 		 */
18277f59b277SEric Biggers 		INIT_WORK(&dic->verity_work, f2fs_verify_cluster);
18287f59b277SEric Biggers 		fsverity_enqueue_verify_work(&dic->verity_work);
182998dc08baSEric Biggers 		return;
18307f59b277SEric Biggers 	}
183198dc08baSEric Biggers 
183298dc08baSEric Biggers 	/* Update and unlock the cluster's pagecache pages. */
183398dc08baSEric Biggers 	for (i = 0; i < dic->cluster_size; i++) {
183498dc08baSEric Biggers 		struct page *rpage = dic->rpages[i];
183598dc08baSEric Biggers 
183698dc08baSEric Biggers 		if (!rpage)
183798dc08baSEric Biggers 			continue;
183898dc08baSEric Biggers 
183998dc08baSEric Biggers 		if (failed)
184098dc08baSEric Biggers 			ClearPageUptodate(rpage);
184198dc08baSEric Biggers 		else
184298dc08baSEric Biggers 			SetPageUptodate(rpage);
184398dc08baSEric Biggers 		unlock_page(rpage);
184498dc08baSEric Biggers 	}
184598dc08baSEric Biggers 
184698dc08baSEric Biggers 	/*
184798dc08baSEric Biggers 	 * Release the reference to the decompress_io_ctx that was being held
184898dc08baSEric Biggers 	 * for I/O completion.
184998dc08baSEric Biggers 	 */
185098dc08baSEric Biggers 	f2fs_put_dic(dic, in_task);
18517f59b277SEric Biggers }
18527f59b277SEric Biggers 
18537f59b277SEric Biggers /*
18547f59b277SEric Biggers  * Put a reference to a compressed page's decompress_io_ctx.
18557f59b277SEric Biggers  *
18567f59b277SEric Biggers  * This is called when the page is no longer needed and can be freed.
18577f59b277SEric Biggers  */
1858bff139b4SDaeho Jeong void f2fs_put_page_dic(struct page *page, bool in_task)
18597f59b277SEric Biggers {
18607f59b277SEric Biggers 	struct decompress_io_ctx *dic =
18617f59b277SEric Biggers 			(struct decompress_io_ctx *)page_private(page);
18627f59b277SEric Biggers 
1863bff139b4SDaeho Jeong 	f2fs_put_dic(dic, in_task);
18644c8ff709SChao Yu }
186531083031SChao Yu 
186694afd6d6SChao Yu /*
186794afd6d6SChao Yu  * check whether cluster blocks are contiguous, and add extent cache entry
186894afd6d6SChao Yu  * only if cluster blocks are logically and physically contiguous.
186994afd6d6SChao Yu  */
18704b99ecd3SChao Yu unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,
18714b99ecd3SChao Yu 						unsigned int ofs_in_node)
187294afd6d6SChao Yu {
18734b99ecd3SChao Yu 	bool compressed = data_blkaddr(dn->inode, dn->node_page,
18744b99ecd3SChao Yu 					ofs_in_node) == COMPRESS_ADDR;
187594afd6d6SChao Yu 	int i = compressed ? 1 : 0;
187694afd6d6SChao Yu 	block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_page,
18774b99ecd3SChao Yu 							ofs_in_node + i);
187894afd6d6SChao Yu 
187994afd6d6SChao Yu 	for (i += 1; i < F2FS_I(dn->inode)->i_cluster_size; i++) {
188094afd6d6SChao Yu 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
18814b99ecd3SChao Yu 							ofs_in_node + i);
188294afd6d6SChao Yu 
188394afd6d6SChao Yu 		if (!__is_valid_data_blkaddr(blkaddr))
188494afd6d6SChao Yu 			break;
188594afd6d6SChao Yu 		if (first_blkaddr + i - (compressed ? 1 : 0) != blkaddr)
188694afd6d6SChao Yu 			return 0;
188794afd6d6SChao Yu 	}
188894afd6d6SChao Yu 
188994afd6d6SChao Yu 	return compressed ? i - 1 : i;
189094afd6d6SChao Yu }
189194afd6d6SChao Yu 
18926ce19affSChao Yu const struct address_space_operations f2fs_compress_aops = {
1893c26cd045SMatthew Wilcox (Oracle) 	.release_folio = f2fs_release_folio,
189491503996SMatthew Wilcox (Oracle) 	.invalidate_folio = f2fs_invalidate_folio,
1895f35474ecSChao Yu 	.migrate_folio	= filemap_migrate_folio,
18966ce19affSChao Yu };
18976ce19affSChao Yu 
18986ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
18996ce19affSChao Yu {
19006ce19affSChao Yu 	return sbi->compress_inode->i_mapping;
19016ce19affSChao Yu }
19026ce19affSChao Yu 
19036ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
19046ce19affSChao Yu {
19056ce19affSChao Yu 	if (!sbi->compress_inode)
19066ce19affSChao Yu 		return;
19076ce19affSChao Yu 	invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
19086ce19affSChao Yu }
19096ce19affSChao Yu 
19106ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
19116ce19affSChao Yu 						nid_t ino, block_t blkaddr)
19126ce19affSChao Yu {
19136ce19affSChao Yu 	struct page *cpage;
19146ce19affSChao Yu 	int ret;
19156ce19affSChao Yu 
19166ce19affSChao Yu 	if (!test_opt(sbi, COMPRESS_CACHE))
19176ce19affSChao Yu 		return;
19186ce19affSChao Yu 
19196ce19affSChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
19206ce19affSChao Yu 		return;
19216ce19affSChao Yu 
19226ce19affSChao Yu 	if (!f2fs_available_free_memory(sbi, COMPRESS_PAGE))
19236ce19affSChao Yu 		return;
19246ce19affSChao Yu 
19256ce19affSChao Yu 	cpage = find_get_page(COMPRESS_MAPPING(sbi), blkaddr);
19266ce19affSChao Yu 	if (cpage) {
19276ce19affSChao Yu 		f2fs_put_page(cpage, 0);
19286ce19affSChao Yu 		return;
19296ce19affSChao Yu 	}
19306ce19affSChao Yu 
19316ce19affSChao Yu 	cpage = alloc_page(__GFP_NOWARN | __GFP_IO);
19326ce19affSChao Yu 	if (!cpage)
19336ce19affSChao Yu 		return;
19346ce19affSChao Yu 
19356ce19affSChao Yu 	ret = add_to_page_cache_lru(cpage, COMPRESS_MAPPING(sbi),
19366ce19affSChao Yu 						blkaddr, GFP_NOFS);
19376ce19affSChao Yu 	if (ret) {
19386ce19affSChao Yu 		f2fs_put_page(cpage, 0);
19396ce19affSChao Yu 		return;
19406ce19affSChao Yu 	}
19416ce19affSChao Yu 
19426ce19affSChao Yu 	set_page_private_data(cpage, ino);
19436ce19affSChao Yu 
19446ce19affSChao Yu 	memcpy(page_address(cpage), page_address(page), PAGE_SIZE);
19456ce19affSChao Yu 	SetPageUptodate(cpage);
19466ce19affSChao Yu 	f2fs_put_page(cpage, 1);
19476ce19affSChao Yu }
19486ce19affSChao Yu 
19496ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
19506ce19affSChao Yu 								block_t blkaddr)
19516ce19affSChao Yu {
19526ce19affSChao Yu 	struct page *cpage;
19536ce19affSChao Yu 	bool hitted = false;
19546ce19affSChao Yu 
19556ce19affSChao Yu 	if (!test_opt(sbi, COMPRESS_CACHE))
19566ce19affSChao Yu 		return false;
19576ce19affSChao Yu 
19586ce19affSChao Yu 	cpage = f2fs_pagecache_get_page(COMPRESS_MAPPING(sbi),
19596ce19affSChao Yu 				blkaddr, FGP_LOCK | FGP_NOWAIT, GFP_NOFS);
19606ce19affSChao Yu 	if (cpage) {
19616ce19affSChao Yu 		if (PageUptodate(cpage)) {
19626ce19affSChao Yu 			atomic_inc(&sbi->compress_page_hit);
19636ce19affSChao Yu 			memcpy(page_address(page),
19646ce19affSChao Yu 				page_address(cpage), PAGE_SIZE);
19656ce19affSChao Yu 			hitted = true;
19666ce19affSChao Yu 		}
19676ce19affSChao Yu 		f2fs_put_page(cpage, 1);
19686ce19affSChao Yu 	}
19696ce19affSChao Yu 
19706ce19affSChao Yu 	return hitted;
19716ce19affSChao Yu }
19726ce19affSChao Yu 
19736ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino)
19746ce19affSChao Yu {
1975173cdf2cSZhang Qilong 	struct address_space *mapping = COMPRESS_MAPPING(sbi);
1976bbfe4f66SMatthew Wilcox (Oracle) 	struct folio_batch fbatch;
19776ce19affSChao Yu 	pgoff_t index = 0;
19786ce19affSChao Yu 	pgoff_t end = MAX_BLKADDR(sbi);
19796ce19affSChao Yu 
19806ce19affSChao Yu 	if (!mapping->nrpages)
19816ce19affSChao Yu 		return;
19826ce19affSChao Yu 
1983bbfe4f66SMatthew Wilcox (Oracle) 	folio_batch_init(&fbatch);
19846ce19affSChao Yu 
19856ce19affSChao Yu 	do {
1986bbfe4f66SMatthew Wilcox (Oracle) 		unsigned int nr, i;
19876ce19affSChao Yu 
1988bbfe4f66SMatthew Wilcox (Oracle) 		nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
1989bbfe4f66SMatthew Wilcox (Oracle) 		if (!nr)
19906ce19affSChao Yu 			break;
19916ce19affSChao Yu 
1992bbfe4f66SMatthew Wilcox (Oracle) 		for (i = 0; i < nr; i++) {
1993bbfe4f66SMatthew Wilcox (Oracle) 			struct folio *folio = fbatch.folios[i];
19946ce19affSChao Yu 
1995bbfe4f66SMatthew Wilcox (Oracle) 			folio_lock(folio);
1996bbfe4f66SMatthew Wilcox (Oracle) 			if (folio->mapping != mapping) {
1997bbfe4f66SMatthew Wilcox (Oracle) 				folio_unlock(folio);
19986ce19affSChao Yu 				continue;
19996ce19affSChao Yu 			}
20006ce19affSChao Yu 
2001bbfe4f66SMatthew Wilcox (Oracle) 			if (ino != get_page_private_data(&folio->page)) {
2002bbfe4f66SMatthew Wilcox (Oracle) 				folio_unlock(folio);
20036ce19affSChao Yu 				continue;
20046ce19affSChao Yu 			}
20056ce19affSChao Yu 
2006af7628d6SMatthew Wilcox (Oracle) 			generic_error_remove_folio(mapping, folio);
2007bbfe4f66SMatthew Wilcox (Oracle) 			folio_unlock(folio);
20086ce19affSChao Yu 		}
2009bbfe4f66SMatthew Wilcox (Oracle) 		folio_batch_release(&fbatch);
20106ce19affSChao Yu 		cond_resched();
20116ce19affSChao Yu 	} while (index < end);
20126ce19affSChao Yu }
20136ce19affSChao Yu 
20146ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi)
20156ce19affSChao Yu {
20166ce19affSChao Yu 	struct inode *inode;
20176ce19affSChao Yu 
20186ce19affSChao Yu 	if (!test_opt(sbi, COMPRESS_CACHE))
20196ce19affSChao Yu 		return 0;
20206ce19affSChao Yu 
20216ce19affSChao Yu 	inode = f2fs_iget(sbi->sb, F2FS_COMPRESS_INO(sbi));
20226ce19affSChao Yu 	if (IS_ERR(inode))
20236ce19affSChao Yu 		return PTR_ERR(inode);
20246ce19affSChao Yu 	sbi->compress_inode = inode;
20256ce19affSChao Yu 
20266ce19affSChao Yu 	sbi->compress_percent = COMPRESS_PERCENT;
20276ce19affSChao Yu 	sbi->compress_watermark = COMPRESS_WATERMARK;
20286ce19affSChao Yu 
20296ce19affSChao Yu 	atomic_set(&sbi->compress_page_hit, 0);
20306ce19affSChao Yu 
20316ce19affSChao Yu 	return 0;
20326ce19affSChao Yu }
20336ce19affSChao Yu 
20346ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi)
20356ce19affSChao Yu {
20366ce19affSChao Yu 	if (!sbi->compress_inode)
20376ce19affSChao Yu 		return;
20386ce19affSChao Yu 	iput(sbi->compress_inode);
20396ce19affSChao Yu 	sbi->compress_inode = NULL;
20406ce19affSChao Yu }
20416ce19affSChao Yu 
204231083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
204331083031SChao Yu {
204431083031SChao Yu 	dev_t dev = sbi->sb->s_bdev->bd_dev;
2045e0d4e8acSSu Hui 	char slab_name[35];
204631083031SChao Yu 
204729be7ec3SChao Yu 	if (!f2fs_sb_has_compression(sbi))
204829be7ec3SChao Yu 		return 0;
204929be7ec3SChao Yu 
205031083031SChao Yu 	sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
205131083031SChao Yu 
205231083031SChao Yu 	sbi->page_array_slab_size = sizeof(struct page *) <<
205331083031SChao Yu 					F2FS_OPTION(sbi).compress_log_size;
205431083031SChao Yu 
205531083031SChao Yu 	sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
205631083031SChao Yu 					sbi->page_array_slab_size);
2057870af777SYangtao Li 	return sbi->page_array_slab ? 0 : -ENOMEM;
205831083031SChao Yu }
205931083031SChao Yu 
206031083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
206131083031SChao Yu {
206231083031SChao Yu 	kmem_cache_destroy(sbi->page_array_slab);
206331083031SChao Yu }
2064c68d6c88SChao Yu 
2065870af777SYangtao Li int __init f2fs_init_compress_cache(void)
2066c68d6c88SChao Yu {
2067c68d6c88SChao Yu 	cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
2068c68d6c88SChao Yu 					sizeof(struct compress_io_ctx));
2069c68d6c88SChao Yu 	if (!cic_entry_slab)
2070c68d6c88SChao Yu 		return -ENOMEM;
2071c68d6c88SChao Yu 	dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
2072c68d6c88SChao Yu 					sizeof(struct decompress_io_ctx));
2073c68d6c88SChao Yu 	if (!dic_entry_slab)
2074c68d6c88SChao Yu 		goto free_cic;
2075c68d6c88SChao Yu 	return 0;
2076c68d6c88SChao Yu free_cic:
2077870af777SYangtao Li 	kmem_cache_destroy(cic_entry_slab);
2078c68d6c88SChao Yu 	return -ENOMEM;
2079c68d6c88SChao Yu }
2080c68d6c88SChao Yu 
2081c68d6c88SChao Yu void f2fs_destroy_compress_cache(void)
2082c68d6c88SChao Yu {
2083870af777SYangtao Li 	kmem_cache_destroy(dic_entry_slab);
2084870af777SYangtao Li 	kmem_cache_destroy(cic_entry_slab);
2085c68d6c88SChao Yu }
2086