xref: /linux/fs/f2fs/compress.c (revision 901c12d144570ed2558f4a6806201453c5b01bea)
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);
584c8ff709SChao Yu };
594c8ff709SChao Yu 
604c8ff709SChao Yu static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
614c8ff709SChao Yu {
624c8ff709SChao Yu 	return index & (cc->cluster_size - 1);
634c8ff709SChao Yu }
644c8ff709SChao Yu 
654c8ff709SChao Yu static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
664c8ff709SChao Yu {
674c8ff709SChao Yu 	return index >> cc->log_cluster_size;
684c8ff709SChao Yu }
694c8ff709SChao Yu 
704c8ff709SChao Yu static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
714c8ff709SChao Yu {
724c8ff709SChao Yu 	return cc->cluster_idx << cc->log_cluster_size;
734c8ff709SChao Yu }
744c8ff709SChao Yu 
754c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page)
764c8ff709SChao Yu {
774c8ff709SChao Yu 	if (!PagePrivate(page))
784c8ff709SChao Yu 		return false;
794c8ff709SChao Yu 	if (!page_private(page))
804c8ff709SChao Yu 		return false;
81b763f3beSChao Yu 	if (page_private_nonpointer(page))
824c8ff709SChao Yu 		return false;
8329b993c7SYu Changchun 
844c8ff709SChao Yu 	f2fs_bug_on(F2FS_M_SB(page->mapping),
854c8ff709SChao Yu 		*((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
864c8ff709SChao Yu 	return true;
874c8ff709SChao Yu }
884c8ff709SChao Yu 
894c8ff709SChao Yu static void f2fs_set_compressed_page(struct page *page,
90887347a0SChao Yu 		struct inode *inode, pgoff_t index, void *data)
914c8ff709SChao Yu {
92b763f3beSChao Yu 	attach_page_private(page, (void *)data);
934c8ff709SChao Yu 
944c8ff709SChao Yu 	/* i_crypto_info and iv index */
954c8ff709SChao Yu 	page->index = index;
964c8ff709SChao Yu 	page->mapping = inode->i_mapping;
974c8ff709SChao Yu }
984c8ff709SChao Yu 
994c8ff709SChao Yu static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
1004c8ff709SChao Yu {
1014c8ff709SChao Yu 	int i;
1024c8ff709SChao Yu 
1034c8ff709SChao Yu 	for (i = 0; i < len; i++) {
1044c8ff709SChao Yu 		if (!cc->rpages[i])
1054c8ff709SChao Yu 			continue;
1064c8ff709SChao Yu 		if (unlock)
1074c8ff709SChao Yu 			unlock_page(cc->rpages[i]);
1084c8ff709SChao Yu 		else
1094c8ff709SChao Yu 			put_page(cc->rpages[i]);
1104c8ff709SChao Yu 	}
1114c8ff709SChao Yu }
1124c8ff709SChao Yu 
1134c8ff709SChao Yu static void f2fs_put_rpages(struct compress_ctx *cc)
1144c8ff709SChao Yu {
1154c8ff709SChao Yu 	f2fs_drop_rpages(cc, cc->cluster_size, false);
1164c8ff709SChao Yu }
1174c8ff709SChao Yu 
1184c8ff709SChao Yu static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
1194c8ff709SChao Yu {
1204c8ff709SChao Yu 	f2fs_drop_rpages(cc, len, true);
1214c8ff709SChao Yu }
1224c8ff709SChao Yu 
1234c8ff709SChao Yu static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
1244c8ff709SChao Yu 		struct writeback_control *wbc, bool redirty, int unlock)
1254c8ff709SChao Yu {
1264c8ff709SChao Yu 	unsigned int i;
1274c8ff709SChao Yu 
1284c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
1294c8ff709SChao Yu 		if (!cc->rpages[i])
1304c8ff709SChao Yu 			continue;
1314c8ff709SChao Yu 		if (redirty)
1324c8ff709SChao Yu 			redirty_page_for_writepage(wbc, cc->rpages[i]);
1334c8ff709SChao Yu 		f2fs_put_page(cc->rpages[i], unlock);
1344c8ff709SChao Yu 	}
1354c8ff709SChao Yu }
1364c8ff709SChao Yu 
1374c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page)
1384c8ff709SChao Yu {
1394c8ff709SChao Yu 	return ((struct compress_io_ctx *)page_private(page))->rpages[0];
1404c8ff709SChao Yu }
1414c8ff709SChao Yu 
1424c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc)
1434c8ff709SChao Yu {
144adfc6943SJaegeuk Kim 	if (cc->rpages)
1454c8ff709SChao Yu 		return 0;
1464c8ff709SChao Yu 
14731083031SChao Yu 	cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1484c8ff709SChao Yu 	return cc->rpages ? 0 : -ENOMEM;
1494c8ff709SChao Yu }
1504c8ff709SChao Yu 
1518bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse)
1524c8ff709SChao Yu {
15331083031SChao Yu 	page_array_free(cc->inode, cc->rpages, cc->cluster_size);
1544c8ff709SChao Yu 	cc->rpages = NULL;
1554c8ff709SChao Yu 	cc->nr_rpages = 0;
1564c8ff709SChao Yu 	cc->nr_cpages = 0;
1573271d7ebSFengnan Chang 	cc->valid_nr_cpages = 0;
1588bfbfb0dSChao Yu 	if (!reuse)
1594c8ff709SChao Yu 		cc->cluster_idx = NULL_CLUSTER;
1604c8ff709SChao Yu }
1614c8ff709SChao Yu 
1624c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
1634c8ff709SChao Yu {
1644c8ff709SChao Yu 	unsigned int cluster_ofs;
1654c8ff709SChao Yu 
1664c8ff709SChao Yu 	if (!f2fs_cluster_can_merge_page(cc, page->index))
1674c8ff709SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
1684c8ff709SChao Yu 
1694c8ff709SChao Yu 	cluster_ofs = offset_in_cluster(cc, page->index);
1704c8ff709SChao Yu 	cc->rpages[cluster_ofs] = page;
1714c8ff709SChao Yu 	cc->nr_rpages++;
1724c8ff709SChao Yu 	cc->cluster_idx = cluster_idx(cc, page->index);
1734c8ff709SChao Yu }
1744c8ff709SChao Yu 
1754c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO
1764c8ff709SChao Yu static int lzo_init_compress_ctx(struct compress_ctx *cc)
1774c8ff709SChao Yu {
1784c8ff709SChao Yu 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
1794c8ff709SChao Yu 				LZO1X_MEM_COMPRESS, GFP_NOFS);
1804c8ff709SChao Yu 	if (!cc->private)
1814c8ff709SChao Yu 		return -ENOMEM;
1824c8ff709SChao Yu 
1834c8ff709SChao Yu 	cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
1844c8ff709SChao Yu 	return 0;
1854c8ff709SChao Yu }
1864c8ff709SChao Yu 
1874c8ff709SChao Yu static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
1884c8ff709SChao Yu {
1894c8ff709SChao Yu 	kvfree(cc->private);
1904c8ff709SChao Yu 	cc->private = NULL;
1914c8ff709SChao Yu }
1924c8ff709SChao Yu 
1934c8ff709SChao Yu static int lzo_compress_pages(struct compress_ctx *cc)
1944c8ff709SChao Yu {
1954c8ff709SChao Yu 	int ret;
1964c8ff709SChao Yu 
1974c8ff709SChao Yu 	ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
1984c8ff709SChao Yu 					&cc->clen, cc->private);
1994c8ff709SChao Yu 	if (ret != LZO_E_OK) {
2004c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
2014c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
2024c8ff709SChao Yu 		return -EIO;
2034c8ff709SChao Yu 	}
2044c8ff709SChao Yu 	return 0;
2054c8ff709SChao Yu }
2064c8ff709SChao Yu 
2074c8ff709SChao Yu static int lzo_decompress_pages(struct decompress_io_ctx *dic)
2084c8ff709SChao Yu {
2094c8ff709SChao Yu 	int ret;
2104c8ff709SChao Yu 
2114c8ff709SChao Yu 	ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
2124c8ff709SChao Yu 						dic->rbuf, &dic->rlen);
2134c8ff709SChao Yu 	if (ret != LZO_E_OK) {
2144c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
2154c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
2164c8ff709SChao Yu 		return -EIO;
2174c8ff709SChao Yu 	}
2184c8ff709SChao Yu 
2194c8ff709SChao Yu 	if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
2204c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
2214c8ff709SChao Yu 					"expected:%lu\n", KERN_ERR,
2224c8ff709SChao Yu 					F2FS_I_SB(dic->inode)->sb->s_id,
2234c8ff709SChao Yu 					dic->rlen,
2244c8ff709SChao Yu 					PAGE_SIZE << dic->log_cluster_size);
2254c8ff709SChao Yu 		return -EIO;
2264c8ff709SChao Yu 	}
2274c8ff709SChao Yu 	return 0;
2284c8ff709SChao Yu }
2294c8ff709SChao Yu 
2304c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lzo_ops = {
2314c8ff709SChao Yu 	.init_compress_ctx	= lzo_init_compress_ctx,
2324c8ff709SChao Yu 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
2334c8ff709SChao Yu 	.compress_pages		= lzo_compress_pages,
2344c8ff709SChao Yu 	.decompress_pages	= lzo_decompress_pages,
2354c8ff709SChao Yu };
2364c8ff709SChao Yu #endif
2374c8ff709SChao Yu 
2384c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4
2394c8ff709SChao Yu static int lz4_init_compress_ctx(struct compress_ctx *cc)
2404c8ff709SChao Yu {
2413fde13f8SChao Yu 	unsigned int size = LZ4_MEM_COMPRESS;
2423fde13f8SChao Yu 
2433fde13f8SChao Yu #ifdef CONFIG_F2FS_FS_LZ4HC
244b90e5086SChao Yu 	if (F2FS_I(cc->inode)->i_compress_level)
2453fde13f8SChao Yu 		size = LZ4HC_MEM_COMPRESS;
2463fde13f8SChao Yu #endif
2473fde13f8SChao Yu 
2483fde13f8SChao Yu 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS);
2494c8ff709SChao Yu 	if (!cc->private)
2504c8ff709SChao Yu 		return -ENOMEM;
2514c8ff709SChao Yu 
252f6644143SChao Yu 	/*
253f6644143SChao Yu 	 * we do not change cc->clen to LZ4_compressBound(inputsize) to
254f6644143SChao Yu 	 * adapt worst compress case, because lz4 compressor can handle
255f6644143SChao Yu 	 * output budget properly.
256f6644143SChao Yu 	 */
257f6644143SChao Yu 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
2584c8ff709SChao Yu 	return 0;
2594c8ff709SChao Yu }
2604c8ff709SChao Yu 
2614c8ff709SChao Yu static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
2624c8ff709SChao Yu {
2634c8ff709SChao Yu 	kvfree(cc->private);
2644c8ff709SChao Yu 	cc->private = NULL;
2654c8ff709SChao Yu }
2664c8ff709SChao Yu 
2674c8ff709SChao Yu static int lz4_compress_pages(struct compress_ctx *cc)
2684c8ff709SChao Yu {
2693094e557SYangtao Li 	int len = -EINVAL;
2703094e557SYangtao Li 	unsigned char level = F2FS_I(cc->inode)->i_compress_level;
2714c8ff709SChao Yu 
2723094e557SYangtao Li 	if (!level)
2734c8ff709SChao Yu 		len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
2744c8ff709SChao Yu 						cc->clen, cc->private);
2753094e557SYangtao Li #ifdef CONFIG_F2FS_FS_LZ4HC
2763094e557SYangtao Li 	else
2773094e557SYangtao Li 		len = LZ4_compress_HC(cc->rbuf, cc->cbuf->cdata, cc->rlen,
2783094e557SYangtao Li 					cc->clen, level, cc->private);
2793094e557SYangtao Li #endif
2803094e557SYangtao Li 	if (len < 0)
2813094e557SYangtao Li 		return len;
282f6644143SChao Yu 	if (!len)
283f6644143SChao Yu 		return -EAGAIN;
284f6644143SChao Yu 
2854c8ff709SChao Yu 	cc->clen = len;
2864c8ff709SChao Yu 	return 0;
2874c8ff709SChao Yu }
2884c8ff709SChao Yu 
2894c8ff709SChao Yu static int lz4_decompress_pages(struct decompress_io_ctx *dic)
2904c8ff709SChao Yu {
2914c8ff709SChao Yu 	int ret;
2924c8ff709SChao Yu 
2934c8ff709SChao Yu 	ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
2944c8ff709SChao Yu 						dic->clen, dic->rlen);
2954c8ff709SChao Yu 	if (ret < 0) {
2964c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
2974c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
2984c8ff709SChao Yu 		return -EIO;
2994c8ff709SChao Yu 	}
3004c8ff709SChao Yu 
3014c8ff709SChao Yu 	if (ret != PAGE_SIZE << dic->log_cluster_size) {
302d284af43SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lz4 invalid ret:%d, "
3034c8ff709SChao Yu 					"expected:%lu\n", KERN_ERR,
304d284af43SChao Yu 					F2FS_I_SB(dic->inode)->sb->s_id, ret,
3054c8ff709SChao Yu 					PAGE_SIZE << dic->log_cluster_size);
3064c8ff709SChao Yu 		return -EIO;
3074c8ff709SChao Yu 	}
3084c8ff709SChao Yu 	return 0;
3094c8ff709SChao Yu }
3104c8ff709SChao Yu 
3114c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lz4_ops = {
3124c8ff709SChao Yu 	.init_compress_ctx	= lz4_init_compress_ctx,
3134c8ff709SChao Yu 	.destroy_compress_ctx	= lz4_destroy_compress_ctx,
3144c8ff709SChao Yu 	.compress_pages		= lz4_compress_pages,
3154c8ff709SChao Yu 	.decompress_pages	= lz4_decompress_pages,
3164c8ff709SChao Yu };
3174c8ff709SChao Yu #endif
3184c8ff709SChao Yu 
31950cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD
32050cfa66fSChao Yu #define F2FS_ZSTD_DEFAULT_CLEVEL	1
32150cfa66fSChao Yu 
32250cfa66fSChao Yu static int zstd_init_compress_ctx(struct compress_ctx *cc)
32350cfa66fSChao Yu {
324cf30f6a5SNick Terrell 	zstd_parameters params;
325cf30f6a5SNick Terrell 	zstd_cstream *stream;
32650cfa66fSChao Yu 	void *workspace;
32750cfa66fSChao Yu 	unsigned int workspace_size;
328b90e5086SChao Yu 	unsigned char level = F2FS_I(cc->inode)->i_compress_level;
32950cfa66fSChao Yu 
3303fde13f8SChao Yu 	if (!level)
3313fde13f8SChao Yu 		level = F2FS_ZSTD_DEFAULT_CLEVEL;
3323fde13f8SChao Yu 
3334ff23a65SSheng Yong 	params = zstd_get_params(level, cc->rlen);
334cf30f6a5SNick Terrell 	workspace_size = zstd_cstream_workspace_bound(&params.cParams);
33550cfa66fSChao Yu 
33650cfa66fSChao Yu 	workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
33750cfa66fSChao Yu 					workspace_size, GFP_NOFS);
33850cfa66fSChao Yu 	if (!workspace)
33950cfa66fSChao Yu 		return -ENOMEM;
34050cfa66fSChao Yu 
341cf30f6a5SNick Terrell 	stream = zstd_init_cstream(&params, 0, workspace, workspace_size);
34250cfa66fSChao Yu 	if (!stream) {
343cf30f6a5SNick Terrell 		printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_cstream failed\n",
34450cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
34550cfa66fSChao Yu 				__func__);
34650cfa66fSChao Yu 		kvfree(workspace);
34750cfa66fSChao Yu 		return -EIO;
34850cfa66fSChao Yu 	}
34950cfa66fSChao Yu 
35050cfa66fSChao Yu 	cc->private = workspace;
35150cfa66fSChao Yu 	cc->private2 = stream;
35250cfa66fSChao Yu 
35350cfa66fSChao Yu 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
35450cfa66fSChao Yu 	return 0;
35550cfa66fSChao Yu }
35650cfa66fSChao Yu 
35750cfa66fSChao Yu static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
35850cfa66fSChao Yu {
35950cfa66fSChao Yu 	kvfree(cc->private);
36050cfa66fSChao Yu 	cc->private = NULL;
36150cfa66fSChao Yu 	cc->private2 = NULL;
36250cfa66fSChao Yu }
36350cfa66fSChao Yu 
36450cfa66fSChao Yu static int zstd_compress_pages(struct compress_ctx *cc)
36550cfa66fSChao Yu {
366cf30f6a5SNick Terrell 	zstd_cstream *stream = cc->private2;
367cf30f6a5SNick Terrell 	zstd_in_buffer inbuf;
368cf30f6a5SNick Terrell 	zstd_out_buffer outbuf;
36950cfa66fSChao Yu 	int src_size = cc->rlen;
37050cfa66fSChao Yu 	int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
37150cfa66fSChao Yu 	int ret;
37250cfa66fSChao Yu 
37350cfa66fSChao Yu 	inbuf.pos = 0;
37450cfa66fSChao Yu 	inbuf.src = cc->rbuf;
37550cfa66fSChao Yu 	inbuf.size = src_size;
37650cfa66fSChao Yu 
37750cfa66fSChao Yu 	outbuf.pos = 0;
37850cfa66fSChao Yu 	outbuf.dst = cc->cbuf->cdata;
37950cfa66fSChao Yu 	outbuf.size = dst_size;
38050cfa66fSChao Yu 
381cf30f6a5SNick Terrell 	ret = zstd_compress_stream(stream, &outbuf, &inbuf);
382cf30f6a5SNick Terrell 	if (zstd_is_error(ret)) {
383cf30f6a5SNick Terrell 		printk_ratelimited("%sF2FS-fs (%s): %s zstd_compress_stream failed, ret: %d\n",
38450cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
385cf30f6a5SNick Terrell 				__func__, zstd_get_error_code(ret));
38650cfa66fSChao Yu 		return -EIO;
38750cfa66fSChao Yu 	}
38850cfa66fSChao Yu 
389cf30f6a5SNick Terrell 	ret = zstd_end_stream(stream, &outbuf);
390cf30f6a5SNick Terrell 	if (zstd_is_error(ret)) {
391cf30f6a5SNick Terrell 		printk_ratelimited("%sF2FS-fs (%s): %s zstd_end_stream returned %d\n",
39250cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
393cf30f6a5SNick Terrell 				__func__, zstd_get_error_code(ret));
39450cfa66fSChao Yu 		return -EIO;
39550cfa66fSChao Yu 	}
39650cfa66fSChao Yu 
3971454c978SChao Yu 	/*
3981454c978SChao Yu 	 * there is compressed data remained in intermediate buffer due to
3991454c978SChao Yu 	 * no more space in cbuf.cdata
4001454c978SChao Yu 	 */
4011454c978SChao Yu 	if (ret)
4021454c978SChao Yu 		return -EAGAIN;
4031454c978SChao Yu 
40450cfa66fSChao Yu 	cc->clen = outbuf.pos;
40550cfa66fSChao Yu 	return 0;
40650cfa66fSChao Yu }
40750cfa66fSChao Yu 
40850cfa66fSChao Yu static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
40950cfa66fSChao Yu {
410cf30f6a5SNick Terrell 	zstd_dstream *stream;
41150cfa66fSChao Yu 	void *workspace;
41250cfa66fSChao Yu 	unsigned int workspace_size;
4130e2b7385SChao Yu 	unsigned int max_window_size =
4140e2b7385SChao Yu 			MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
41550cfa66fSChao Yu 
416cf30f6a5SNick Terrell 	workspace_size = zstd_dstream_workspace_bound(max_window_size);
41750cfa66fSChao Yu 
41850cfa66fSChao Yu 	workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
41950cfa66fSChao Yu 					workspace_size, GFP_NOFS);
42050cfa66fSChao Yu 	if (!workspace)
42150cfa66fSChao Yu 		return -ENOMEM;
42250cfa66fSChao Yu 
423cf30f6a5SNick Terrell 	stream = zstd_init_dstream(max_window_size, workspace, workspace_size);
42450cfa66fSChao Yu 	if (!stream) {
425cf30f6a5SNick Terrell 		printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_dstream failed\n",
42650cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
42750cfa66fSChao Yu 				__func__);
42850cfa66fSChao Yu 		kvfree(workspace);
42950cfa66fSChao Yu 		return -EIO;
43050cfa66fSChao Yu 	}
43150cfa66fSChao Yu 
43250cfa66fSChao Yu 	dic->private = workspace;
43350cfa66fSChao Yu 	dic->private2 = stream;
43450cfa66fSChao Yu 
43550cfa66fSChao Yu 	return 0;
43650cfa66fSChao Yu }
43750cfa66fSChao Yu 
43850cfa66fSChao Yu static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
43950cfa66fSChao Yu {
44050cfa66fSChao Yu 	kvfree(dic->private);
44150cfa66fSChao Yu 	dic->private = NULL;
44250cfa66fSChao Yu 	dic->private2 = NULL;
44350cfa66fSChao Yu }
44450cfa66fSChao Yu 
44550cfa66fSChao Yu static int zstd_decompress_pages(struct decompress_io_ctx *dic)
44650cfa66fSChao Yu {
447cf30f6a5SNick Terrell 	zstd_dstream *stream = dic->private2;
448cf30f6a5SNick Terrell 	zstd_in_buffer inbuf;
449cf30f6a5SNick Terrell 	zstd_out_buffer outbuf;
45050cfa66fSChao Yu 	int ret;
45150cfa66fSChao Yu 
45250cfa66fSChao Yu 	inbuf.pos = 0;
45350cfa66fSChao Yu 	inbuf.src = dic->cbuf->cdata;
45450cfa66fSChao Yu 	inbuf.size = dic->clen;
45550cfa66fSChao Yu 
45650cfa66fSChao Yu 	outbuf.pos = 0;
45750cfa66fSChao Yu 	outbuf.dst = dic->rbuf;
45850cfa66fSChao Yu 	outbuf.size = dic->rlen;
45950cfa66fSChao Yu 
460cf30f6a5SNick Terrell 	ret = zstd_decompress_stream(stream, &outbuf, &inbuf);
461cf30f6a5SNick Terrell 	if (zstd_is_error(ret)) {
462cf30f6a5SNick Terrell 		printk_ratelimited("%sF2FS-fs (%s): %s zstd_decompress_stream failed, ret: %d\n",
46350cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
464cf30f6a5SNick Terrell 				__func__, zstd_get_error_code(ret));
46550cfa66fSChao Yu 		return -EIO;
46650cfa66fSChao Yu 	}
46750cfa66fSChao Yu 
46850cfa66fSChao Yu 	if (dic->rlen != outbuf.pos) {
46950cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
47050cfa66fSChao Yu 				"expected:%lu\n", KERN_ERR,
47150cfa66fSChao Yu 				F2FS_I_SB(dic->inode)->sb->s_id,
47250cfa66fSChao Yu 				__func__, dic->rlen,
47350cfa66fSChao Yu 				PAGE_SIZE << dic->log_cluster_size);
47450cfa66fSChao Yu 		return -EIO;
47550cfa66fSChao Yu 	}
47650cfa66fSChao Yu 
47750cfa66fSChao Yu 	return 0;
47850cfa66fSChao Yu }
47950cfa66fSChao Yu 
48050cfa66fSChao Yu static const struct f2fs_compress_ops f2fs_zstd_ops = {
48150cfa66fSChao Yu 	.init_compress_ctx	= zstd_init_compress_ctx,
48250cfa66fSChao Yu 	.destroy_compress_ctx	= zstd_destroy_compress_ctx,
48350cfa66fSChao Yu 	.compress_pages		= zstd_compress_pages,
48450cfa66fSChao Yu 	.init_decompress_ctx	= zstd_init_decompress_ctx,
48550cfa66fSChao Yu 	.destroy_decompress_ctx	= zstd_destroy_decompress_ctx,
48650cfa66fSChao Yu 	.decompress_pages	= zstd_decompress_pages,
48750cfa66fSChao Yu };
48850cfa66fSChao Yu #endif
48950cfa66fSChao Yu 
4906d92b201SChao Yu #ifdef CONFIG_F2FS_FS_LZO
4916d92b201SChao Yu #ifdef CONFIG_F2FS_FS_LZORLE
4926d92b201SChao Yu static int lzorle_compress_pages(struct compress_ctx *cc)
4936d92b201SChao Yu {
4946d92b201SChao Yu 	int ret;
4956d92b201SChao Yu 
4966d92b201SChao Yu 	ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
4976d92b201SChao Yu 					&cc->clen, cc->private);
4986d92b201SChao Yu 	if (ret != LZO_E_OK) {
4996d92b201SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d\n",
5006d92b201SChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
5016d92b201SChao Yu 		return -EIO;
5026d92b201SChao Yu 	}
5036d92b201SChao Yu 	return 0;
5046d92b201SChao Yu }
5056d92b201SChao Yu 
5066d92b201SChao Yu static const struct f2fs_compress_ops f2fs_lzorle_ops = {
5076d92b201SChao Yu 	.init_compress_ctx	= lzo_init_compress_ctx,
5086d92b201SChao Yu 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
5096d92b201SChao Yu 	.compress_pages		= lzorle_compress_pages,
5106d92b201SChao Yu 	.decompress_pages	= lzo_decompress_pages,
5116d92b201SChao Yu };
5126d92b201SChao Yu #endif
5136d92b201SChao Yu #endif
5146d92b201SChao Yu 
5154c8ff709SChao Yu static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
5164c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO
5174c8ff709SChao Yu 	&f2fs_lzo_ops,
5184c8ff709SChao Yu #else
5194c8ff709SChao Yu 	NULL,
5204c8ff709SChao Yu #endif
5214c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4
5224c8ff709SChao Yu 	&f2fs_lz4_ops,
5234c8ff709SChao Yu #else
5244c8ff709SChao Yu 	NULL,
5254c8ff709SChao Yu #endif
52650cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD
52750cfa66fSChao Yu 	&f2fs_zstd_ops,
52850cfa66fSChao Yu #else
52950cfa66fSChao Yu 	NULL,
53050cfa66fSChao Yu #endif
5316d92b201SChao Yu #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
5326d92b201SChao Yu 	&f2fs_lzorle_ops,
5336d92b201SChao Yu #else
5346d92b201SChao Yu 	NULL,
5356d92b201SChao Yu #endif
5364c8ff709SChao Yu };
5374c8ff709SChao Yu 
5384c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode)
5394c8ff709SChao Yu {
5404c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
5414c8ff709SChao Yu 		return true;
5424c8ff709SChao Yu 	return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
5434c8ff709SChao Yu }
5444c8ff709SChao Yu 
54599bbe307SJaegeuk Kim static mempool_t *compress_page_pool;
5465e6bbde9SChao Yu static int num_compress_pages = 512;
5475e6bbde9SChao Yu module_param(num_compress_pages, uint, 0444);
5485e6bbde9SChao Yu MODULE_PARM_DESC(num_compress_pages,
5495e6bbde9SChao Yu 		"Number of intermediate compress pages to preallocate");
5505e6bbde9SChao Yu 
551a1357a91SYangtao Li int __init f2fs_init_compress_mempool(void)
5525e6bbde9SChao Yu {
5535e6bbde9SChao Yu 	compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
554870af777SYangtao Li 	return compress_page_pool ? 0 : -ENOMEM;
5555e6bbde9SChao Yu }
5565e6bbde9SChao Yu 
5575e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void)
5585e6bbde9SChao Yu {
5595e6bbde9SChao Yu 	mempool_destroy(compress_page_pool);
5605e6bbde9SChao Yu }
5615e6bbde9SChao Yu 
5625e6bbde9SChao Yu static struct page *f2fs_compress_alloc_page(void)
5634c8ff709SChao Yu {
5644c8ff709SChao Yu 	struct page *page;
5654c8ff709SChao Yu 
5665e6bbde9SChao Yu 	page = mempool_alloc(compress_page_pool, GFP_NOFS);
5674c8ff709SChao Yu 	lock_page(page);
5685e6bbde9SChao Yu 
5694c8ff709SChao Yu 	return page;
5704c8ff709SChao Yu }
5714c8ff709SChao Yu 
5725e6bbde9SChao Yu static void f2fs_compress_free_page(struct page *page)
5735e6bbde9SChao Yu {
5745e6bbde9SChao Yu 	if (!page)
5755e6bbde9SChao Yu 		return;
576b763f3beSChao Yu 	detach_page_private(page);
5775e6bbde9SChao Yu 	page->mapping = NULL;
5785e6bbde9SChao Yu 	unlock_page(page);
5795e6bbde9SChao Yu 	mempool_free(page, compress_page_pool);
5805e6bbde9SChao Yu }
5815e6bbde9SChao Yu 
5826fcaebacSDaeho Jeong #define MAX_VMAP_RETRIES	3
5836fcaebacSDaeho Jeong 
5846fcaebacSDaeho Jeong static void *f2fs_vmap(struct page **pages, unsigned int count)
5856fcaebacSDaeho Jeong {
5866fcaebacSDaeho Jeong 	int i;
5876fcaebacSDaeho Jeong 	void *buf = NULL;
5886fcaebacSDaeho Jeong 
5896fcaebacSDaeho Jeong 	for (i = 0; i < MAX_VMAP_RETRIES; i++) {
5906fcaebacSDaeho Jeong 		buf = vm_map_ram(pages, count, -1);
5916fcaebacSDaeho Jeong 		if (buf)
5926fcaebacSDaeho Jeong 			break;
5936fcaebacSDaeho Jeong 		vm_unmap_aliases();
5946fcaebacSDaeho Jeong 	}
5956fcaebacSDaeho Jeong 	return buf;
5966fcaebacSDaeho Jeong }
5976fcaebacSDaeho Jeong 
5984c8ff709SChao Yu static int f2fs_compress_pages(struct compress_ctx *cc)
5994c8ff709SChao Yu {
6004c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
6014c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
6024c8ff709SChao Yu 				f2fs_cops[fi->i_compress_algorithm];
60331083031SChao Yu 	unsigned int max_len, new_nr_cpages;
604b28f047bSChao Yu 	u32 chksum = 0;
6054c8ff709SChao Yu 	int i, ret;
6064c8ff709SChao Yu 
6074c8ff709SChao Yu 	trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
6084c8ff709SChao Yu 				cc->cluster_size, fi->i_compress_algorithm);
6094c8ff709SChao Yu 
61023b1faaaSChao Yu 	if (cops->init_compress_ctx) {
6114c8ff709SChao Yu 		ret = cops->init_compress_ctx(cc);
6124c8ff709SChao Yu 		if (ret)
6134c8ff709SChao Yu 			goto out;
61423b1faaaSChao Yu 	}
6154c8ff709SChao Yu 
6164c8ff709SChao Yu 	max_len = COMPRESS_HEADER_SIZE + cc->clen;
6174c8ff709SChao Yu 	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
6183271d7ebSFengnan Chang 	cc->valid_nr_cpages = cc->nr_cpages;
6194c8ff709SChao Yu 
62031083031SChao Yu 	cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
6214c8ff709SChao Yu 	if (!cc->cpages) {
6224c8ff709SChao Yu 		ret = -ENOMEM;
6234c8ff709SChao Yu 		goto destroy_compress_ctx;
6244c8ff709SChao Yu 	}
6254c8ff709SChao Yu 
6264c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
6275e6bbde9SChao Yu 		cc->cpages[i] = f2fs_compress_alloc_page();
6284c8ff709SChao Yu 		if (!cc->cpages[i]) {
6294c8ff709SChao Yu 			ret = -ENOMEM;
6304c8ff709SChao Yu 			goto out_free_cpages;
6314c8ff709SChao Yu 		}
6324c8ff709SChao Yu 	}
6334c8ff709SChao Yu 
6346fcaebacSDaeho Jeong 	cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
6354c8ff709SChao Yu 	if (!cc->rbuf) {
6364c8ff709SChao Yu 		ret = -ENOMEM;
6374c8ff709SChao Yu 		goto out_free_cpages;
6384c8ff709SChao Yu 	}
6394c8ff709SChao Yu 
6406fcaebacSDaeho Jeong 	cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
6414c8ff709SChao Yu 	if (!cc->cbuf) {
6424c8ff709SChao Yu 		ret = -ENOMEM;
6434c8ff709SChao Yu 		goto out_vunmap_rbuf;
6444c8ff709SChao Yu 	}
6454c8ff709SChao Yu 
6464c8ff709SChao Yu 	ret = cops->compress_pages(cc);
6474c8ff709SChao Yu 	if (ret)
6484c8ff709SChao Yu 		goto out_vunmap_cbuf;
6494c8ff709SChao Yu 
6504c8ff709SChao Yu 	max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
6514c8ff709SChao Yu 
6524c8ff709SChao Yu 	if (cc->clen > max_len) {
6534c8ff709SChao Yu 		ret = -EAGAIN;
6544c8ff709SChao Yu 		goto out_vunmap_cbuf;
6554c8ff709SChao Yu 	}
6564c8ff709SChao Yu 
6574c8ff709SChao Yu 	cc->cbuf->clen = cpu_to_le32(cc->clen);
6584c8ff709SChao Yu 
659447286ebSYangtao Li 	if (fi->i_compress_flag & BIT(COMPRESS_CHKSUM))
660b28f047bSChao Yu 		chksum = f2fs_crc32(F2FS_I_SB(cc->inode),
661b28f047bSChao Yu 					cc->cbuf->cdata, cc->clen);
662b28f047bSChao Yu 	cc->cbuf->chksum = cpu_to_le32(chksum);
663b28f047bSChao Yu 
6644c8ff709SChao Yu 	for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
6654c8ff709SChao Yu 		cc->cbuf->reserved[i] = cpu_to_le32(0);
6664c8ff709SChao Yu 
66731083031SChao Yu 	new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
66831083031SChao Yu 
6697fa6d598SEric Biggers 	/* zero out any unused part of the last page */
6707fa6d598SEric Biggers 	memset(&cc->cbuf->cdata[cc->clen], 0,
67131083031SChao Yu 			(new_nr_cpages * PAGE_SIZE) -
67231083031SChao Yu 			(cc->clen + COMPRESS_HEADER_SIZE));
6737fa6d598SEric Biggers 
6746fcaebacSDaeho Jeong 	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
6756fcaebacSDaeho Jeong 	vm_unmap_ram(cc->rbuf, cc->cluster_size);
6764c8ff709SChao Yu 
677ebaaec35SZhang Qilong 	for (i = new_nr_cpages; i < cc->nr_cpages; i++) {
6785e6bbde9SChao Yu 		f2fs_compress_free_page(cc->cpages[i]);
6794c8ff709SChao Yu 		cc->cpages[i] = NULL;
6804c8ff709SChao Yu 	}
6814c8ff709SChao Yu 
68223b1faaaSChao Yu 	if (cops->destroy_compress_ctx)
68309ff4801SChao Yu 		cops->destroy_compress_ctx(cc);
68409ff4801SChao Yu 
6853271d7ebSFengnan Chang 	cc->valid_nr_cpages = new_nr_cpages;
6864c8ff709SChao Yu 
6874c8ff709SChao Yu 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
6884c8ff709SChao Yu 							cc->clen, ret);
6894c8ff709SChao Yu 	return 0;
6904c8ff709SChao Yu 
6914c8ff709SChao Yu out_vunmap_cbuf:
6926fcaebacSDaeho Jeong 	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
6934c8ff709SChao Yu out_vunmap_rbuf:
6946fcaebacSDaeho Jeong 	vm_unmap_ram(cc->rbuf, cc->cluster_size);
6954c8ff709SChao Yu out_free_cpages:
6964c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
6974c8ff709SChao Yu 		if (cc->cpages[i])
6985e6bbde9SChao Yu 			f2fs_compress_free_page(cc->cpages[i]);
6994c8ff709SChao Yu 	}
70031083031SChao Yu 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
7014c8ff709SChao Yu 	cc->cpages = NULL;
7024c8ff709SChao Yu destroy_compress_ctx:
70323b1faaaSChao Yu 	if (cops->destroy_compress_ctx)
7044c8ff709SChao Yu 		cops->destroy_compress_ctx(cc);
7054c8ff709SChao Yu out:
7064c8ff709SChao Yu 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
7074c8ff709SChao Yu 							cc->clen, ret);
7084c8ff709SChao Yu 	return ret;
7094c8ff709SChao Yu }
7104c8ff709SChao Yu 
711bff139b4SDaeho Jeong static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
712bff139b4SDaeho Jeong 		bool pre_alloc);
713bff139b4SDaeho Jeong static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
714bff139b4SDaeho Jeong 		bool bypass_destroy_callback, bool pre_alloc);
715bff139b4SDaeho Jeong 
716bff139b4SDaeho Jeong void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task)
7174c8ff709SChao Yu {
7184c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
7194c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(dic->inode);
7204c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
7214c8ff709SChao Yu 			f2fs_cops[fi->i_compress_algorithm];
722bff139b4SDaeho Jeong 	bool bypass_callback = false;
7234c8ff709SChao Yu 	int ret;
7244c8ff709SChao Yu 
7254c8ff709SChao Yu 	trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
7264c8ff709SChao Yu 				dic->cluster_size, fi->i_compress_algorithm);
7274c8ff709SChao Yu 
7284c8ff709SChao Yu 	if (dic->failed) {
7294c8ff709SChao Yu 		ret = -EIO;
7307f59b277SEric Biggers 		goto out_end_io;
7314c8ff709SChao Yu 	}
7324c8ff709SChao Yu 
733bff139b4SDaeho Jeong 	ret = f2fs_prepare_decomp_mem(dic, false);
734bff139b4SDaeho Jeong 	if (ret) {
735bff139b4SDaeho Jeong 		bypass_callback = true;
736bff139b4SDaeho Jeong 		goto out_release;
7374c8ff709SChao Yu 	}
7384c8ff709SChao Yu 
7394c8ff709SChao Yu 	dic->clen = le32_to_cpu(dic->cbuf->clen);
7404c8ff709SChao Yu 	dic->rlen = PAGE_SIZE << dic->log_cluster_size;
7414c8ff709SChao Yu 
7424c8ff709SChao Yu 	if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
7434c8ff709SChao Yu 		ret = -EFSCORRUPTED;
7441aa161e4SJaegeuk Kim 
7451aa161e4SJaegeuk Kim 		/* Avoid f2fs_commit_super in irq context */
746633c8b94SJaegeuk Kim 		if (!in_task)
747*901c12d1SChao Yu 			f2fs_handle_error_async(sbi, ERROR_FAIL_DECOMPRESSION);
7481aa161e4SJaegeuk Kim 		else
74995fa90c9SChao Yu 			f2fs_handle_error(sbi, ERROR_FAIL_DECOMPRESSION);
750bff139b4SDaeho Jeong 		goto out_release;
7514c8ff709SChao Yu 	}
7524c8ff709SChao Yu 
7534c8ff709SChao Yu 	ret = cops->decompress_pages(dic);
7544c8ff709SChao Yu 
755447286ebSYangtao Li 	if (!ret && (fi->i_compress_flag & BIT(COMPRESS_CHKSUM))) {
756b28f047bSChao Yu 		u32 provided = le32_to_cpu(dic->cbuf->chksum);
757b28f047bSChao Yu 		u32 calculated = f2fs_crc32(sbi, dic->cbuf->cdata, dic->clen);
758b28f047bSChao Yu 
759b28f047bSChao Yu 		if (provided != calculated) {
760b28f047bSChao Yu 			if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) {
761b28f047bSChao Yu 				set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT);
762b28f047bSChao Yu 				printk_ratelimited(
763b28f047bSChao Yu 					"%sF2FS-fs (%s): checksum invalid, nid = %lu, %x vs %x",
764b28f047bSChao Yu 					KERN_INFO, sbi->sb->s_id, dic->inode->i_ino,
765b28f047bSChao Yu 					provided, calculated);
766b28f047bSChao Yu 			}
767b28f047bSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
768b28f047bSChao Yu 		}
769b28f047bSChao Yu 	}
770b28f047bSChao Yu 
771bff139b4SDaeho Jeong out_release:
772bff139b4SDaeho Jeong 	f2fs_release_decomp_mem(dic, bypass_callback, false);
773bff139b4SDaeho Jeong 
7747f59b277SEric Biggers out_end_io:
7754c8ff709SChao Yu 	trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
7764c8ff709SChao Yu 							dic->clen, ret);
777bff139b4SDaeho Jeong 	f2fs_decompress_end_io(dic, ret, in_task);
7787f59b277SEric Biggers }
7797f59b277SEric Biggers 
7807f59b277SEric Biggers /*
7817f59b277SEric Biggers  * This is called when a page of a compressed cluster has been read from disk
7827f59b277SEric Biggers  * (or failed to be read from disk).  It checks whether this page was the last
7837f59b277SEric Biggers  * page being waited on in the cluster, and if so, it decompresses the cluster
7847f59b277SEric Biggers  * (or in the case of a failure, cleans up without actually decompressing).
7857f59b277SEric Biggers  */
7866ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
787bff139b4SDaeho Jeong 		block_t blkaddr, bool in_task)
7887f59b277SEric Biggers {
7897f59b277SEric Biggers 	struct decompress_io_ctx *dic =
7907f59b277SEric Biggers 			(struct decompress_io_ctx *)page_private(page);
7917f59b277SEric Biggers 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
7927f59b277SEric Biggers 
7937f59b277SEric Biggers 	dec_page_count(sbi, F2FS_RD_DATA);
7947f59b277SEric Biggers 
7957f59b277SEric Biggers 	if (failed)
7967f59b277SEric Biggers 		WRITE_ONCE(dic->failed, true);
797bff139b4SDaeho Jeong 	else if (blkaddr && in_task)
7986ce19affSChao Yu 		f2fs_cache_compressed_page(sbi, page,
7996ce19affSChao Yu 					dic->inode->i_ino, blkaddr);
8007f59b277SEric Biggers 
8017f59b277SEric Biggers 	if (atomic_dec_and_test(&dic->remaining_pages))
802bff139b4SDaeho Jeong 		f2fs_decompress_cluster(dic, in_task);
8034c8ff709SChao Yu }
8044c8ff709SChao Yu 
8054c8ff709SChao Yu static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
8064c8ff709SChao Yu {
8074c8ff709SChao Yu 	if (cc->cluster_idx == NULL_CLUSTER)
8084c8ff709SChao Yu 		return true;
8094c8ff709SChao Yu 	return cc->cluster_idx == cluster_idx(cc, index);
8104c8ff709SChao Yu }
8114c8ff709SChao Yu 
8124c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc)
8134c8ff709SChao Yu {
8144c8ff709SChao Yu 	return cc->nr_rpages == 0;
8154c8ff709SChao Yu }
8164c8ff709SChao Yu 
8174c8ff709SChao Yu static bool f2fs_cluster_is_full(struct compress_ctx *cc)
8184c8ff709SChao Yu {
8194c8ff709SChao Yu 	return cc->cluster_size == cc->nr_rpages;
8204c8ff709SChao Yu }
8214c8ff709SChao Yu 
8224c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
8234c8ff709SChao Yu {
8244c8ff709SChao Yu 	if (f2fs_cluster_is_empty(cc))
8254c8ff709SChao Yu 		return true;
8264c8ff709SChao Yu 	return is_page_in_cluster(cc, index);
8274c8ff709SChao Yu }
8284c8ff709SChao Yu 
82901fc4b9aSFengnan Chang bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
8304f8219f8SFengnan Chang 				int index, int nr_pages, bool uptodate)
831b368cc5eSFengnan Chang {
83201fc4b9aSFengnan Chang 	unsigned long pgidx = pages[index]->index;
8334f8219f8SFengnan Chang 	int i = uptodate ? 0 : 1;
8344f8219f8SFengnan Chang 
8354f8219f8SFengnan Chang 	/*
8364f8219f8SFengnan Chang 	 * when uptodate set to true, try to check all pages in cluster is
8374f8219f8SFengnan Chang 	 * uptodate or not.
8384f8219f8SFengnan Chang 	 */
8394f8219f8SFengnan Chang 	if (uptodate && (pgidx % cc->cluster_size))
8404f8219f8SFengnan Chang 		return false;
841b368cc5eSFengnan Chang 
842b368cc5eSFengnan Chang 	if (nr_pages - index < cc->cluster_size)
843b368cc5eSFengnan Chang 		return false;
844b368cc5eSFengnan Chang 
8454f8219f8SFengnan Chang 	for (; i < cc->cluster_size; i++) {
84601fc4b9aSFengnan Chang 		if (pages[index + i]->index != pgidx + i)
847b368cc5eSFengnan Chang 			return false;
84801fc4b9aSFengnan Chang 		if (uptodate && !PageUptodate(pages[index + i]))
849b368cc5eSFengnan Chang 			return false;
850b368cc5eSFengnan Chang 	}
851b368cc5eSFengnan Chang 
852b368cc5eSFengnan Chang 	return true;
853b368cc5eSFengnan Chang }
854b368cc5eSFengnan Chang 
8555db479f0SChao Yu static bool cluster_has_invalid_data(struct compress_ctx *cc)
8564c8ff709SChao Yu {
8574c8ff709SChao Yu 	loff_t i_size = i_size_read(cc->inode);
8584c8ff709SChao Yu 	unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
8594c8ff709SChao Yu 	int i;
8604c8ff709SChao Yu 
8614c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
8624c8ff709SChao Yu 		struct page *page = cc->rpages[i];
8634c8ff709SChao Yu 
8648af85f71SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), !page);
8654c8ff709SChao Yu 
8664c8ff709SChao Yu 		/* beyond EOF */
8674c8ff709SChao Yu 		if (page->index >= nr_pages)
8684c8ff709SChao Yu 			return true;
8694c8ff709SChao Yu 	}
8705db479f0SChao Yu 	return false;
8715db479f0SChao Yu }
8724c8ff709SChao Yu 
873bbe1da7eSChao Yu bool f2fs_sanity_check_cluster(struct dnode_of_data *dn)
874bbe1da7eSChao Yu {
875bbe1da7eSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
876bbe1da7eSChao Yu 	unsigned int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
877bbe1da7eSChao Yu 	bool compressed = dn->data_blkaddr == COMPRESS_ADDR;
878bbe1da7eSChao Yu 	int cluster_end = 0;
879bbe1da7eSChao Yu 	int i;
880bbe1da7eSChao Yu 	char *reason = "";
881bbe1da7eSChao Yu 
882bbe1da7eSChao Yu 	if (!compressed)
883bbe1da7eSChao Yu 		return false;
884bbe1da7eSChao Yu 
885bbe1da7eSChao Yu 	/* [..., COMPR_ADDR, ...] */
886bbe1da7eSChao Yu 	if (dn->ofs_in_node % cluster_size) {
887bbe1da7eSChao Yu 		reason = "[*|C|*|*]";
888bbe1da7eSChao Yu 		goto out;
889bbe1da7eSChao Yu 	}
890bbe1da7eSChao Yu 
891bbe1da7eSChao Yu 	for (i = 1; i < cluster_size; i++) {
892bbe1da7eSChao Yu 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
893bbe1da7eSChao Yu 							dn->ofs_in_node + i);
894bbe1da7eSChao Yu 
895bbe1da7eSChao Yu 		/* [COMPR_ADDR, ..., COMPR_ADDR] */
896bbe1da7eSChao Yu 		if (blkaddr == COMPRESS_ADDR) {
897bbe1da7eSChao Yu 			reason = "[C|*|C|*]";
898bbe1da7eSChao Yu 			goto out;
899bbe1da7eSChao Yu 		}
900bbe1da7eSChao Yu 		if (!__is_valid_data_blkaddr(blkaddr)) {
901bbe1da7eSChao Yu 			if (!cluster_end)
902bbe1da7eSChao Yu 				cluster_end = i;
903bbe1da7eSChao Yu 			continue;
904bbe1da7eSChao Yu 		}
905bbe1da7eSChao Yu 		/* [COMPR_ADDR, NULL_ADDR or NEW_ADDR, valid_blkaddr] */
906bbe1da7eSChao Yu 		if (cluster_end) {
907bbe1da7eSChao Yu 			reason = "[C|N|N|V]";
908bbe1da7eSChao Yu 			goto out;
909bbe1da7eSChao Yu 		}
910bbe1da7eSChao Yu 	}
911bbe1da7eSChao Yu 	return false;
912bbe1da7eSChao Yu out:
913bbe1da7eSChao Yu 	f2fs_warn(sbi, "access invalid cluster, ino:%lu, nid:%u, ofs_in_node:%u, reason:%s",
914bbe1da7eSChao Yu 			dn->inode->i_ino, dn->nid, dn->ofs_in_node, reason);
915bbe1da7eSChao Yu 	set_sbi_flag(sbi, SBI_NEED_FSCK);
916bbe1da7eSChao Yu 	return true;
917bbe1da7eSChao Yu }
918bbe1da7eSChao Yu 
91991f0fb69SChao Yu static int __f2fs_cluster_blocks(struct inode *inode,
92091f0fb69SChao Yu 				unsigned int cluster_idx, bool compr)
9214c8ff709SChao Yu {
9224c8ff709SChao Yu 	struct dnode_of_data dn;
92391f0fb69SChao Yu 	unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
92491f0fb69SChao Yu 	unsigned int start_idx = cluster_idx <<
92591f0fb69SChao Yu 				F2FS_I(inode)->i_log_cluster_size;
9264c8ff709SChao Yu 	int ret;
9274c8ff709SChao Yu 
92891f0fb69SChao Yu 	set_new_dnode(&dn, inode, NULL, NULL, 0);
92991f0fb69SChao Yu 	ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
9304c8ff709SChao Yu 	if (ret) {
9314c8ff709SChao Yu 		if (ret == -ENOENT)
9324c8ff709SChao Yu 			ret = 0;
9334c8ff709SChao Yu 		goto fail;
9344c8ff709SChao Yu 	}
9354c8ff709SChao Yu 
936bbe1da7eSChao Yu 	if (f2fs_sanity_check_cluster(&dn)) {
937bbe1da7eSChao Yu 		ret = -EFSCORRUPTED;
93895fa90c9SChao Yu 		f2fs_handle_error(F2FS_I_SB(inode), ERROR_CORRUPTED_CLUSTER);
939bbe1da7eSChao Yu 		goto fail;
940bbe1da7eSChao Yu 	}
941bbe1da7eSChao Yu 
9424c8ff709SChao Yu 	if (dn.data_blkaddr == COMPRESS_ADDR) {
9434c8ff709SChao Yu 		int i;
9444c8ff709SChao Yu 
9454c8ff709SChao Yu 		ret = 1;
94691f0fb69SChao Yu 		for (i = 1; i < cluster_size; i++) {
9474c8ff709SChao Yu 			block_t blkaddr;
9484c8ff709SChao Yu 
949a2ced1ceSChao Yu 			blkaddr = data_blkaddr(dn.inode,
9504c8ff709SChao Yu 					dn.node_page, dn.ofs_in_node + i);
9511a67cbe1SChao Yu 			if (compr) {
9521a67cbe1SChao Yu 				if (__is_valid_data_blkaddr(blkaddr))
9531a67cbe1SChao Yu 					ret++;
9541a67cbe1SChao Yu 			} else {
9554c8ff709SChao Yu 				if (blkaddr != NULL_ADDR)
9564c8ff709SChao Yu 					ret++;
9574c8ff709SChao Yu 			}
9584c8ff709SChao Yu 		}
9598f1d4983SChao Yu 
9608f1d4983SChao Yu 		f2fs_bug_on(F2FS_I_SB(inode),
961c6140415SJaegeuk Kim 			!compr && ret != cluster_size &&
962c6140415SJaegeuk Kim 			!is_inode_flag_set(inode, FI_COMPRESS_RELEASED));
9631a67cbe1SChao Yu 	}
9644c8ff709SChao Yu fail:
9654c8ff709SChao Yu 	f2fs_put_dnode(&dn);
9664c8ff709SChao Yu 	return ret;
9674c8ff709SChao Yu }
9684c8ff709SChao Yu 
9691a67cbe1SChao Yu /* return # of compressed blocks in compressed cluster */
9701a67cbe1SChao Yu static int f2fs_compressed_blocks(struct compress_ctx *cc)
9711a67cbe1SChao Yu {
97291f0fb69SChao Yu 	return __f2fs_cluster_blocks(cc->inode, cc->cluster_idx, true);
9731a67cbe1SChao Yu }
9741a67cbe1SChao Yu 
9751a67cbe1SChao Yu /* return # of valid blocks in compressed cluster */
9764c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
9774c8ff709SChao Yu {
97891f0fb69SChao Yu 	return __f2fs_cluster_blocks(inode,
97991f0fb69SChao Yu 		index >> F2FS_I(inode)->i_log_cluster_size,
98091f0fb69SChao Yu 		false);
9814c8ff709SChao Yu }
9824c8ff709SChao Yu 
9834c8ff709SChao Yu static bool cluster_may_compress(struct compress_ctx *cc)
9844c8ff709SChao Yu {
985602a16d5SDaeho Jeong 	if (!f2fs_need_compress_data(cc->inode))
9864c8ff709SChao Yu 		return false;
9874c8ff709SChao Yu 	if (f2fs_is_atomic_file(cc->inode))
9884c8ff709SChao Yu 		return false;
9894c8ff709SChao Yu 	if (!f2fs_cluster_is_full(cc))
9904c8ff709SChao Yu 		return false;
991dc35d73aSChao Yu 	if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
992dc35d73aSChao Yu 		return false;
9935db479f0SChao Yu 	return !cluster_has_invalid_data(cc);
9944c8ff709SChao Yu }
9954c8ff709SChao Yu 
9964c8ff709SChao Yu static void set_cluster_writeback(struct compress_ctx *cc)
9974c8ff709SChao Yu {
9984c8ff709SChao Yu 	int i;
9994c8ff709SChao Yu 
10004c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
10014c8ff709SChao Yu 		if (cc->rpages[i])
10024c8ff709SChao Yu 			set_page_writeback(cc->rpages[i]);
10034c8ff709SChao Yu 	}
10044c8ff709SChao Yu }
10054c8ff709SChao Yu 
10064c8ff709SChao Yu static void set_cluster_dirty(struct compress_ctx *cc)
10074c8ff709SChao Yu {
10084c8ff709SChao Yu 	int i;
10094c8ff709SChao Yu 
10104c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++)
10114c8ff709SChao Yu 		if (cc->rpages[i])
10124c8ff709SChao Yu 			set_page_dirty(cc->rpages[i]);
10134c8ff709SChao Yu }
10144c8ff709SChao Yu 
10154c8ff709SChao Yu static int prepare_compress_overwrite(struct compress_ctx *cc,
10164c8ff709SChao Yu 		struct page **pagep, pgoff_t index, void **fsdata)
10174c8ff709SChao Yu {
10184c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
10194c8ff709SChao Yu 	struct address_space *mapping = cc->inode->i_mapping;
10204c8ff709SChao Yu 	struct page *page;
10214c8ff709SChao Yu 	sector_t last_block_in_bio;
10224c8ff709SChao Yu 	unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
10234c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
10244c8ff709SChao Yu 	int i, ret;
10254c8ff709SChao Yu 
10264c8ff709SChao Yu retry:
102791f0fb69SChao Yu 	ret = f2fs_is_compressed_cluster(cc->inode, start_idx);
10284c8ff709SChao Yu 	if (ret <= 0)
10294c8ff709SChao Yu 		return ret;
10304c8ff709SChao Yu 
10314c8ff709SChao Yu 	ret = f2fs_init_compress_ctx(cc);
10324c8ff709SChao Yu 	if (ret)
10334c8ff709SChao Yu 		return ret;
10344c8ff709SChao Yu 
10354c8ff709SChao Yu 	/* keep page reference to avoid page reclaim */
10364c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
10374c8ff709SChao Yu 		page = f2fs_pagecache_get_page(mapping, start_idx + i,
10384c8ff709SChao Yu 							fgp_flag, GFP_NOFS);
10394c8ff709SChao Yu 		if (!page) {
10404c8ff709SChao Yu 			ret = -ENOMEM;
10414c8ff709SChao Yu 			goto unlock_pages;
10424c8ff709SChao Yu 		}
10434c8ff709SChao Yu 
10444c8ff709SChao Yu 		if (PageUptodate(page))
1045a949dc5fSChao Yu 			f2fs_put_page(page, 1);
10464c8ff709SChao Yu 		else
10474c8ff709SChao Yu 			f2fs_compress_ctx_add_page(cc, page);
10484c8ff709SChao Yu 	}
10494c8ff709SChao Yu 
10504c8ff709SChao Yu 	if (!f2fs_cluster_is_empty(cc)) {
10514c8ff709SChao Yu 		struct bio *bio = NULL;
10524c8ff709SChao Yu 
10534c8ff709SChao Yu 		ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
10540683728aSChao Yu 					&last_block_in_bio, false, true);
1055a949dc5fSChao Yu 		f2fs_put_rpages(cc);
10568bfbfb0dSChao Yu 		f2fs_destroy_compress_ctx(cc, true);
10574c8ff709SChao Yu 		if (ret)
1058a949dc5fSChao Yu 			goto out;
10594c8ff709SChao Yu 		if (bio)
1060bc29835aSChristoph Hellwig 			f2fs_submit_read_bio(sbi, bio, DATA);
10614c8ff709SChao Yu 
10624c8ff709SChao Yu 		ret = f2fs_init_compress_ctx(cc);
10634c8ff709SChao Yu 		if (ret)
1064a949dc5fSChao Yu 			goto out;
10654c8ff709SChao Yu 	}
10664c8ff709SChao Yu 
10674c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
10684c8ff709SChao Yu 		f2fs_bug_on(sbi, cc->rpages[i]);
10694c8ff709SChao Yu 
10704c8ff709SChao Yu 		page = find_lock_page(mapping, start_idx + i);
1071a949dc5fSChao Yu 		if (!page) {
1072a949dc5fSChao Yu 			/* page can be truncated */
1073a949dc5fSChao Yu 			goto release_and_retry;
1074a949dc5fSChao Yu 		}
10754c8ff709SChao Yu 
10764c8ff709SChao Yu 		f2fs_wait_on_page_writeback(page, DATA, true, true);
10774c8ff709SChao Yu 		f2fs_compress_ctx_add_page(cc, page);
10784c8ff709SChao Yu 
10794c8ff709SChao Yu 		if (!PageUptodate(page)) {
1080a949dc5fSChao Yu release_and_retry:
1081a949dc5fSChao Yu 			f2fs_put_rpages(cc);
10824c8ff709SChao Yu 			f2fs_unlock_rpages(cc, i + 1);
10838bfbfb0dSChao Yu 			f2fs_destroy_compress_ctx(cc, true);
10844c8ff709SChao Yu 			goto retry;
10854c8ff709SChao Yu 		}
10864c8ff709SChao Yu 	}
10874c8ff709SChao Yu 
10884c8ff709SChao Yu 	if (likely(!ret)) {
10894c8ff709SChao Yu 		*fsdata = cc->rpages;
10904c8ff709SChao Yu 		*pagep = cc->rpages[offset_in_cluster(cc, index)];
10914c8ff709SChao Yu 		return cc->cluster_size;
10924c8ff709SChao Yu 	}
10934c8ff709SChao Yu 
10944c8ff709SChao Yu unlock_pages:
1095a949dc5fSChao Yu 	f2fs_put_rpages(cc);
10964c8ff709SChao Yu 	f2fs_unlock_rpages(cc, i);
10978bfbfb0dSChao Yu 	f2fs_destroy_compress_ctx(cc, true);
1098a949dc5fSChao Yu out:
10994c8ff709SChao Yu 	return ret;
11004c8ff709SChao Yu }
11014c8ff709SChao Yu 
11024c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
11034c8ff709SChao Yu 		struct page **pagep, pgoff_t index, void **fsdata)
11044c8ff709SChao Yu {
11054c8ff709SChao Yu 	struct compress_ctx cc = {
11064c8ff709SChao Yu 		.inode = inode,
11074c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
11084c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
11094c8ff709SChao Yu 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
11104c8ff709SChao Yu 		.rpages = NULL,
11114c8ff709SChao Yu 		.nr_rpages = 0,
11124c8ff709SChao Yu 	};
11134c8ff709SChao Yu 
11144c8ff709SChao Yu 	return prepare_compress_overwrite(&cc, pagep, index, fsdata);
11154c8ff709SChao Yu }
11164c8ff709SChao Yu 
11174c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
11184c8ff709SChao Yu 					pgoff_t index, unsigned copied)
11194c8ff709SChao Yu 
11204c8ff709SChao Yu {
11214c8ff709SChao Yu 	struct compress_ctx cc = {
112231083031SChao Yu 		.inode = inode,
11234c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
11244c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
11254c8ff709SChao Yu 		.rpages = fsdata,
11264c8ff709SChao Yu 	};
11274c8ff709SChao Yu 	bool first_index = (index == cc.rpages[0]->index);
11284c8ff709SChao Yu 
11294c8ff709SChao Yu 	if (copied)
11304c8ff709SChao Yu 		set_cluster_dirty(&cc);
11314c8ff709SChao Yu 
11324c8ff709SChao Yu 	f2fs_put_rpages_wbc(&cc, NULL, false, 1);
11338bfbfb0dSChao Yu 	f2fs_destroy_compress_ctx(&cc, false);
11344c8ff709SChao Yu 
11354c8ff709SChao Yu 	return first_index;
11364c8ff709SChao Yu }
11374c8ff709SChao Yu 
11383265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
11393265d3dbSChao Yu {
11403265d3dbSChao Yu 	void *fsdata = NULL;
11413265d3dbSChao Yu 	struct page *pagep;
11423265d3dbSChao Yu 	int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
11433265d3dbSChao Yu 	pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
11443265d3dbSChao Yu 							log_cluster_size;
11453265d3dbSChao Yu 	int err;
11463265d3dbSChao Yu 
11473265d3dbSChao Yu 	err = f2fs_is_compressed_cluster(inode, start_idx);
11483265d3dbSChao Yu 	if (err < 0)
11493265d3dbSChao Yu 		return err;
11503265d3dbSChao Yu 
11513265d3dbSChao Yu 	/* truncate normal cluster */
11523265d3dbSChao Yu 	if (!err)
11533265d3dbSChao Yu 		return f2fs_do_truncate_blocks(inode, from, lock);
11543265d3dbSChao Yu 
11553265d3dbSChao Yu 	/* truncate compressed cluster */
11563265d3dbSChao Yu 	err = f2fs_prepare_compress_overwrite(inode, &pagep,
11573265d3dbSChao Yu 						start_idx, &fsdata);
11583265d3dbSChao Yu 
11593265d3dbSChao Yu 	/* should not be a normal cluster */
11603265d3dbSChao Yu 	f2fs_bug_on(F2FS_I_SB(inode), err == 0);
11613265d3dbSChao Yu 
11623265d3dbSChao Yu 	if (err <= 0)
11633265d3dbSChao Yu 		return err;
11643265d3dbSChao Yu 
11653265d3dbSChao Yu 	if (err > 0) {
11663265d3dbSChao Yu 		struct page **rpages = fsdata;
11673265d3dbSChao Yu 		int cluster_size = F2FS_I(inode)->i_cluster_size;
11683265d3dbSChao Yu 		int i;
11693265d3dbSChao Yu 
11703265d3dbSChao Yu 		for (i = cluster_size - 1; i >= 0; i--) {
11713265d3dbSChao Yu 			loff_t start = rpages[i]->index << PAGE_SHIFT;
11723265d3dbSChao Yu 
11733265d3dbSChao Yu 			if (from <= start) {
11743265d3dbSChao Yu 				zero_user_segment(rpages[i], 0, PAGE_SIZE);
11753265d3dbSChao Yu 			} else {
11763265d3dbSChao Yu 				zero_user_segment(rpages[i], from - start,
11773265d3dbSChao Yu 								PAGE_SIZE);
11783265d3dbSChao Yu 				break;
11793265d3dbSChao Yu 			}
11803265d3dbSChao Yu 		}
11813265d3dbSChao Yu 
11823265d3dbSChao Yu 		f2fs_compress_write_end(inode, fsdata, start_idx, true);
11833265d3dbSChao Yu 	}
11843265d3dbSChao Yu 	return 0;
11853265d3dbSChao Yu }
11863265d3dbSChao Yu 
11874c8ff709SChao Yu static int f2fs_write_compressed_pages(struct compress_ctx *cc,
11884c8ff709SChao Yu 					int *submitted,
11894c8ff709SChao Yu 					struct writeback_control *wbc,
11904c8ff709SChao Yu 					enum iostat_type io_type)
11914c8ff709SChao Yu {
11924c8ff709SChao Yu 	struct inode *inode = cc->inode;
11934c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
11944c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
11954c8ff709SChao Yu 	struct f2fs_io_info fio = {
11964c8ff709SChao Yu 		.sbi = sbi,
11974c8ff709SChao Yu 		.ino = cc->inode->i_ino,
11984c8ff709SChao Yu 		.type = DATA,
11994c8ff709SChao Yu 		.op = REQ_OP_WRITE,
12004c8ff709SChao Yu 		.op_flags = wbc_to_write_flags(wbc),
12014c8ff709SChao Yu 		.old_blkaddr = NEW_ADDR,
12024c8ff709SChao Yu 		.page = NULL,
12034c8ff709SChao Yu 		.encrypted_page = NULL,
12044c8ff709SChao Yu 		.compressed_page = NULL,
12052eae077eSChao Yu 		.submitted = 0,
12064c8ff709SChao Yu 		.io_type = io_type,
12074c8ff709SChao Yu 		.io_wbc = wbc,
12082eae077eSChao Yu 		.encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode) ?
12092eae077eSChao Yu 									1 : 0,
12104c8ff709SChao Yu 	};
12114c8ff709SChao Yu 	struct dnode_of_data dn;
12124c8ff709SChao Yu 	struct node_info ni;
12134c8ff709SChao Yu 	struct compress_io_ctx *cic;
12144c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
12154c8ff709SChao Yu 	unsigned int last_index = cc->cluster_size - 1;
12164c8ff709SChao Yu 	loff_t psize;
12174c8ff709SChao Yu 	int i, err;
1218f082c6b2SChao Yu 	bool quota_inode = IS_NOQUOTA(inode);
12194c8ff709SChao Yu 
1220146949deSJinyoung CHOI 	/* we should bypass data pages to proceed the kworker jobs */
1221ee68d271SChao Yu 	if (unlikely(f2fs_cp_error(sbi))) {
1222ee68d271SChao Yu 		mapping_set_error(cc->rpages[0]->mapping, -EIO);
1223ee68d271SChao Yu 		goto out_free;
1224ee68d271SChao Yu 	}
1225ee68d271SChao Yu 
1226f082c6b2SChao Yu 	if (quota_inode) {
122779963d96SChao Yu 		/*
122879963d96SChao Yu 		 * We need to wait for node_write to avoid block allocation during
122979963d96SChao Yu 		 * checkpoint. This can only happen to quota writes which can cause
123079963d96SChao Yu 		 * the below discard race condition.
123179963d96SChao Yu 		 */
1232e4544b63STim Murray 		f2fs_down_read(&sbi->node_write);
123379963d96SChao Yu 	} else if (!f2fs_trylock_op(sbi)) {
123431083031SChao Yu 		goto out_free;
123579963d96SChao Yu 	}
12364c8ff709SChao Yu 
1237df77fbd8SChao Yu 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
12384c8ff709SChao Yu 
12394c8ff709SChao Yu 	err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
12404c8ff709SChao Yu 	if (err)
12414c8ff709SChao Yu 		goto out_unlock_op;
12424c8ff709SChao Yu 
12434c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
1244a2ced1ceSChao Yu 		if (data_blkaddr(dn.inode, dn.node_page,
12454c8ff709SChao Yu 					dn.ofs_in_node + i) == NULL_ADDR)
12464c8ff709SChao Yu 			goto out_put_dnode;
12474c8ff709SChao Yu 	}
12484c8ff709SChao Yu 
12494c8ff709SChao Yu 	psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
12504c8ff709SChao Yu 
1251a9419b63SJaegeuk Kim 	err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
12524c8ff709SChao Yu 	if (err)
12534c8ff709SChao Yu 		goto out_put_dnode;
12544c8ff709SChao Yu 
12554c8ff709SChao Yu 	fio.version = ni.version;
12564c8ff709SChao Yu 
125732410577SChao Yu 	cic = f2fs_kmem_cache_alloc(cic_entry_slab, GFP_F2FS_ZERO, false, sbi);
12584c8ff709SChao Yu 	if (!cic)
12594c8ff709SChao Yu 		goto out_put_dnode;
12604c8ff709SChao Yu 
12614c8ff709SChao Yu 	cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
12624c8ff709SChao Yu 	cic->inode = inode;
12633271d7ebSFengnan Chang 	atomic_set(&cic->pending_pages, cc->valid_nr_cpages);
126431083031SChao Yu 	cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
12654c8ff709SChao Yu 	if (!cic->rpages)
12664c8ff709SChao Yu 		goto out_put_cic;
12674c8ff709SChao Yu 
12684c8ff709SChao Yu 	cic->nr_rpages = cc->cluster_size;
12694c8ff709SChao Yu 
12703271d7ebSFengnan Chang 	for (i = 0; i < cc->valid_nr_cpages; i++) {
12714c8ff709SChao Yu 		f2fs_set_compressed_page(cc->cpages[i], inode,
1272887347a0SChao Yu 					cc->rpages[i + 1]->index, cic);
12734c8ff709SChao Yu 		fio.compressed_page = cc->cpages[i];
1274f567adb0SChao Yu 
1275f567adb0SChao Yu 		fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1276f567adb0SChao Yu 						dn.ofs_in_node + i + 1);
1277f567adb0SChao Yu 
1278f567adb0SChao Yu 		/* wait for GCed page writeback via META_MAPPING */
1279f567adb0SChao Yu 		f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1280f567adb0SChao Yu 
12814c8ff709SChao Yu 		if (fio.encrypted) {
12824c8ff709SChao Yu 			fio.page = cc->rpages[i + 1];
12834c8ff709SChao Yu 			err = f2fs_encrypt_one_page(&fio);
12844c8ff709SChao Yu 			if (err)
12854c8ff709SChao Yu 				goto out_destroy_crypt;
12864c8ff709SChao Yu 			cc->cpages[i] = fio.encrypted_page;
12874c8ff709SChao Yu 		}
12884c8ff709SChao Yu 	}
12894c8ff709SChao Yu 
12904c8ff709SChao Yu 	set_cluster_writeback(cc);
12914c8ff709SChao Yu 
12924c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++)
12934c8ff709SChao Yu 		cic->rpages[i] = cc->rpages[i];
12944c8ff709SChao Yu 
12954c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
12964c8ff709SChao Yu 		block_t blkaddr;
12974c8ff709SChao Yu 
1298a2ced1ceSChao Yu 		blkaddr = f2fs_data_blkaddr(&dn);
129995978caaSChao Yu 		fio.page = cc->rpages[i];
13004c8ff709SChao Yu 		fio.old_blkaddr = blkaddr;
13014c8ff709SChao Yu 
13024c8ff709SChao Yu 		/* cluster header */
13034c8ff709SChao Yu 		if (i == 0) {
13044c8ff709SChao Yu 			if (blkaddr == COMPRESS_ADDR)
13054c8ff709SChao Yu 				fio.compr_blocks++;
13064c8ff709SChao Yu 			if (__is_valid_data_blkaddr(blkaddr))
13074c8ff709SChao Yu 				f2fs_invalidate_blocks(sbi, blkaddr);
13084c8ff709SChao Yu 			f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
13094c8ff709SChao Yu 			goto unlock_continue;
13104c8ff709SChao Yu 		}
13114c8ff709SChao Yu 
13124c8ff709SChao Yu 		if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
13134c8ff709SChao Yu 			fio.compr_blocks++;
13144c8ff709SChao Yu 
13153271d7ebSFengnan Chang 		if (i > cc->valid_nr_cpages) {
13164c8ff709SChao Yu 			if (__is_valid_data_blkaddr(blkaddr)) {
13174c8ff709SChao Yu 				f2fs_invalidate_blocks(sbi, blkaddr);
13184c8ff709SChao Yu 				f2fs_update_data_blkaddr(&dn, NEW_ADDR);
13194c8ff709SChao Yu 			}
13204c8ff709SChao Yu 			goto unlock_continue;
13214c8ff709SChao Yu 		}
13224c8ff709SChao Yu 
13234c8ff709SChao Yu 		f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
13244c8ff709SChao Yu 
13254c8ff709SChao Yu 		if (fio.encrypted)
13264c8ff709SChao Yu 			fio.encrypted_page = cc->cpages[i - 1];
13274c8ff709SChao Yu 		else
13284c8ff709SChao Yu 			fio.compressed_page = cc->cpages[i - 1];
13294c8ff709SChao Yu 
13304c8ff709SChao Yu 		cc->cpages[i - 1] = NULL;
13314c8ff709SChao Yu 		f2fs_outplace_write_data(&dn, &fio);
13324c8ff709SChao Yu 		(*submitted)++;
13334c8ff709SChao Yu unlock_continue:
13344c8ff709SChao Yu 		inode_dec_dirty_pages(cc->inode);
13354c8ff709SChao Yu 		unlock_page(fio.page);
13364c8ff709SChao Yu 	}
13374c8ff709SChao Yu 
13384c8ff709SChao Yu 	if (fio.compr_blocks)
13394c8ff709SChao Yu 		f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
13403271d7ebSFengnan Chang 	f2fs_i_compr_blocks_update(inode, cc->valid_nr_cpages, true);
13413271d7ebSFengnan Chang 	add_compr_block_stat(inode, cc->valid_nr_cpages);
13424c8ff709SChao Yu 
13434c8ff709SChao Yu 	set_inode_flag(cc->inode, FI_APPEND_WRITE);
13444c8ff709SChao Yu 	if (cc->cluster_idx == 0)
13454c8ff709SChao Yu 		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
13464c8ff709SChao Yu 
13474c8ff709SChao Yu 	f2fs_put_dnode(&dn);
1348f082c6b2SChao Yu 	if (quota_inode)
1349e4544b63STim Murray 		f2fs_up_read(&sbi->node_write);
135079963d96SChao Yu 	else
13514c8ff709SChao Yu 		f2fs_unlock_op(sbi);
13524c8ff709SChao Yu 
1353c10c9820SChao Yu 	spin_lock(&fi->i_size_lock);
13544c8ff709SChao Yu 	if (fi->last_disk_size < psize)
13554c8ff709SChao Yu 		fi->last_disk_size = psize;
1356c10c9820SChao Yu 	spin_unlock(&fi->i_size_lock);
13574c8ff709SChao Yu 
13584c8ff709SChao Yu 	f2fs_put_rpages(cc);
135931083031SChao Yu 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
136031083031SChao Yu 	cc->cpages = NULL;
13618bfbfb0dSChao Yu 	f2fs_destroy_compress_ctx(cc, false);
13624c8ff709SChao Yu 	return 0;
13634c8ff709SChao Yu 
13644c8ff709SChao Yu out_destroy_crypt:
136531083031SChao Yu 	page_array_free(cc->inode, cic->rpages, cc->cluster_size);
13664c8ff709SChao Yu 
13674c8ff709SChao Yu 	for (--i; i >= 0; i--)
13684c8ff709SChao Yu 		fscrypt_finalize_bounce_page(&cc->cpages[i]);
13694c8ff709SChao Yu out_put_cic:
1370c68d6c88SChao Yu 	kmem_cache_free(cic_entry_slab, cic);
13714c8ff709SChao Yu out_put_dnode:
13724c8ff709SChao Yu 	f2fs_put_dnode(&dn);
13734c8ff709SChao Yu out_unlock_op:
1374f082c6b2SChao Yu 	if (quota_inode)
1375e4544b63STim Murray 		f2fs_up_read(&sbi->node_write);
137679963d96SChao Yu 	else
13774c8ff709SChao Yu 		f2fs_unlock_op(sbi);
137831083031SChao Yu out_free:
13793271d7ebSFengnan Chang 	for (i = 0; i < cc->valid_nr_cpages; i++) {
1380827f0284SJaegeuk Kim 		f2fs_compress_free_page(cc->cpages[i]);
1381827f0284SJaegeuk Kim 		cc->cpages[i] = NULL;
1382827f0284SJaegeuk Kim 	}
138331083031SChao Yu 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
138431083031SChao Yu 	cc->cpages = NULL;
13854c8ff709SChao Yu 	return -EAGAIN;
13864c8ff709SChao Yu }
13874c8ff709SChao Yu 
13884c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
13894c8ff709SChao Yu {
13904c8ff709SChao Yu 	struct f2fs_sb_info *sbi = bio->bi_private;
13914c8ff709SChao Yu 	struct compress_io_ctx *cic =
13924c8ff709SChao Yu 			(struct compress_io_ctx *)page_private(page);
13934c8ff709SChao Yu 	int i;
13944c8ff709SChao Yu 
13954c8ff709SChao Yu 	if (unlikely(bio->bi_status))
13964c8ff709SChao Yu 		mapping_set_error(cic->inode->i_mapping, -EIO);
13974c8ff709SChao Yu 
13985e6bbde9SChao Yu 	f2fs_compress_free_page(page);
13994c8ff709SChao Yu 
14004c8ff709SChao Yu 	dec_page_count(sbi, F2FS_WB_DATA);
14014c8ff709SChao Yu 
1402e6c3948dSChao Yu 	if (atomic_dec_return(&cic->pending_pages))
14034c8ff709SChao Yu 		return;
14044c8ff709SChao Yu 
14054c8ff709SChao Yu 	for (i = 0; i < cic->nr_rpages; i++) {
14064c8ff709SChao Yu 		WARN_ON(!cic->rpages[i]);
1407b763f3beSChao Yu 		clear_page_private_gcing(cic->rpages[i]);
14084c8ff709SChao Yu 		end_page_writeback(cic->rpages[i]);
14094c8ff709SChao Yu 	}
14104c8ff709SChao Yu 
141131083031SChao Yu 	page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
1412c68d6c88SChao Yu 	kmem_cache_free(cic_entry_slab, cic);
14134c8ff709SChao Yu }
14144c8ff709SChao Yu 
14154c8ff709SChao Yu static int f2fs_write_raw_pages(struct compress_ctx *cc,
14164c8ff709SChao Yu 					int *submitted,
14174c8ff709SChao Yu 					struct writeback_control *wbc,
14184c8ff709SChao Yu 					enum iostat_type io_type)
14194c8ff709SChao Yu {
14204c8ff709SChao Yu 	struct address_space *mapping = cc->inode->i_mapping;
14217377e853SHyeong-Jun Kim 	int _submitted, compr_blocks, ret, i;
14224c8ff709SChao Yu 
14234c8ff709SChao Yu 	compr_blocks = f2fs_compressed_blocks(cc);
14247377e853SHyeong-Jun Kim 
14257377e853SHyeong-Jun Kim 	for (i = 0; i < cc->cluster_size; i++) {
14267377e853SHyeong-Jun Kim 		if (!cc->rpages[i])
14277377e853SHyeong-Jun Kim 			continue;
14287377e853SHyeong-Jun Kim 
14297377e853SHyeong-Jun Kim 		redirty_page_for_writepage(wbc, cc->rpages[i]);
14307377e853SHyeong-Jun Kim 		unlock_page(cc->rpages[i]);
14314c8ff709SChao Yu 	}
14324c8ff709SChao Yu 
14337377e853SHyeong-Jun Kim 	if (compr_blocks < 0)
14347377e853SHyeong-Jun Kim 		return compr_blocks;
14357377e853SHyeong-Jun Kim 
14364c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
14374c8ff709SChao Yu 		if (!cc->rpages[i])
14384c8ff709SChao Yu 			continue;
14394c8ff709SChao Yu retry_write:
14407377e853SHyeong-Jun Kim 		lock_page(cc->rpages[i]);
14417377e853SHyeong-Jun Kim 
14424c8ff709SChao Yu 		if (cc->rpages[i]->mapping != mapping) {
14437377e853SHyeong-Jun Kim continue_unlock:
14444c8ff709SChao Yu 			unlock_page(cc->rpages[i]);
14454c8ff709SChao Yu 			continue;
14464c8ff709SChao Yu 		}
14474c8ff709SChao Yu 
14487377e853SHyeong-Jun Kim 		if (!PageDirty(cc->rpages[i]))
14497377e853SHyeong-Jun Kim 			goto continue_unlock;
14507377e853SHyeong-Jun Kim 
1451babedcbaSYangtao Li 		if (PageWriteback(cc->rpages[i])) {
1452babedcbaSYangtao Li 			if (wbc->sync_mode == WB_SYNC_NONE)
1453babedcbaSYangtao Li 				goto continue_unlock;
1454babedcbaSYangtao Li 			f2fs_wait_on_page_writeback(cc->rpages[i], DATA, true, true);
1455babedcbaSYangtao Li 		}
1456babedcbaSYangtao Li 
14577377e853SHyeong-Jun Kim 		if (!clear_page_dirty_for_io(cc->rpages[i]))
14587377e853SHyeong-Jun Kim 			goto continue_unlock;
14594c8ff709SChao Yu 
14604c8ff709SChao Yu 		ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
14614c8ff709SChao Yu 						NULL, NULL, wbc, io_type,
14623afae09fSChao Yu 						compr_blocks, false);
14634c8ff709SChao Yu 		if (ret) {
14644c8ff709SChao Yu 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
14654c8ff709SChao Yu 				unlock_page(cc->rpages[i]);
14664c8ff709SChao Yu 				ret = 0;
14674c8ff709SChao Yu 			} else if (ret == -EAGAIN) {
1468466357dcSChao Yu 				/*
1469466357dcSChao Yu 				 * for quota file, just redirty left pages to
1470466357dcSChao Yu 				 * avoid deadlock caused by cluster update race
1471466357dcSChao Yu 				 * from foreground operation.
1472466357dcSChao Yu 				 */
14737377e853SHyeong-Jun Kim 				if (IS_NOQUOTA(cc->inode))
14747377e853SHyeong-Jun Kim 					return 0;
14754c8ff709SChao Yu 				ret = 0;
1476a64239d0SNeilBrown 				f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
14774c8ff709SChao Yu 				goto retry_write;
14784c8ff709SChao Yu 			}
14797377e853SHyeong-Jun Kim 			return ret;
14804c8ff709SChao Yu 		}
14814c8ff709SChao Yu 
14824c8ff709SChao Yu 		*submitted += _submitted;
14834c8ff709SChao Yu 	}
14843afae09fSChao Yu 
14853afae09fSChao Yu 	f2fs_balance_fs(F2FS_M_SB(mapping), true);
14863afae09fSChao Yu 
14874c8ff709SChao Yu 	return 0;
14884c8ff709SChao Yu }
14894c8ff709SChao Yu 
14904c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
14914c8ff709SChao Yu 					int *submitted,
14924c8ff709SChao Yu 					struct writeback_control *wbc,
14934c8ff709SChao Yu 					enum iostat_type io_type)
14944c8ff709SChao Yu {
14954c8ff709SChao Yu 	int err;
14964c8ff709SChao Yu 
14974c8ff709SChao Yu 	*submitted = 0;
14984c8ff709SChao Yu 	if (cluster_may_compress(cc)) {
14994c8ff709SChao Yu 		err = f2fs_compress_pages(cc);
15004c8ff709SChao Yu 		if (err == -EAGAIN) {
150109631cf3SDaeho Jeong 			add_compr_block_stat(cc->inode, cc->cluster_size);
15024c8ff709SChao Yu 			goto write;
15034c8ff709SChao Yu 		} else if (err) {
15044c8ff709SChao Yu 			f2fs_put_rpages_wbc(cc, wbc, true, 1);
15054c8ff709SChao Yu 			goto destroy_out;
15064c8ff709SChao Yu 		}
15074c8ff709SChao Yu 
15084c8ff709SChao Yu 		err = f2fs_write_compressed_pages(cc, submitted,
15094c8ff709SChao Yu 							wbc, io_type);
15104c8ff709SChao Yu 		if (!err)
15114c8ff709SChao Yu 			return 0;
15124c8ff709SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
15134c8ff709SChao Yu 	}
15144c8ff709SChao Yu write:
15154c8ff709SChao Yu 	f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
15164c8ff709SChao Yu 
15174c8ff709SChao Yu 	err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
15184c8ff709SChao Yu 	f2fs_put_rpages_wbc(cc, wbc, false, 0);
15194c8ff709SChao Yu destroy_out:
15208bfbfb0dSChao Yu 	f2fs_destroy_compress_ctx(cc, false);
15214c8ff709SChao Yu 	return err;
15224c8ff709SChao Yu }
15234c8ff709SChao Yu 
1524bff139b4SDaeho Jeong static inline bool allow_memalloc_for_decomp(struct f2fs_sb_info *sbi,
1525bff139b4SDaeho Jeong 		bool pre_alloc)
1526bff139b4SDaeho Jeong {
1527bff139b4SDaeho Jeong 	return pre_alloc ^ f2fs_low_mem_mode(sbi);
1528bff139b4SDaeho Jeong }
1529bff139b4SDaeho Jeong 
1530bff139b4SDaeho Jeong static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
1531bff139b4SDaeho Jeong 		bool pre_alloc)
1532bff139b4SDaeho Jeong {
1533bff139b4SDaeho Jeong 	const struct f2fs_compress_ops *cops =
1534bff139b4SDaeho Jeong 		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
1535bff139b4SDaeho Jeong 	int i;
1536bff139b4SDaeho Jeong 
1537bff139b4SDaeho Jeong 	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
1538bff139b4SDaeho Jeong 		return 0;
1539bff139b4SDaeho Jeong 
1540bff139b4SDaeho Jeong 	dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
1541bff139b4SDaeho Jeong 	if (!dic->tpages)
1542bff139b4SDaeho Jeong 		return -ENOMEM;
1543bff139b4SDaeho Jeong 
1544bff139b4SDaeho Jeong 	for (i = 0; i < dic->cluster_size; i++) {
1545bff139b4SDaeho Jeong 		if (dic->rpages[i]) {
1546bff139b4SDaeho Jeong 			dic->tpages[i] = dic->rpages[i];
1547bff139b4SDaeho Jeong 			continue;
1548bff139b4SDaeho Jeong 		}
1549bff139b4SDaeho Jeong 
1550bff139b4SDaeho Jeong 		dic->tpages[i] = f2fs_compress_alloc_page();
1551bff139b4SDaeho Jeong 		if (!dic->tpages[i])
1552bff139b4SDaeho Jeong 			return -ENOMEM;
1553bff139b4SDaeho Jeong 	}
1554bff139b4SDaeho Jeong 
1555bff139b4SDaeho Jeong 	dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
1556bff139b4SDaeho Jeong 	if (!dic->rbuf)
1557bff139b4SDaeho Jeong 		return -ENOMEM;
1558bff139b4SDaeho Jeong 
1559bff139b4SDaeho Jeong 	dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
1560bff139b4SDaeho Jeong 	if (!dic->cbuf)
1561bff139b4SDaeho Jeong 		return -ENOMEM;
1562bff139b4SDaeho Jeong 
15638140654eSZhang Qilong 	if (cops->init_decompress_ctx)
15648140654eSZhang Qilong 		return cops->init_decompress_ctx(dic);
1565bff139b4SDaeho Jeong 
1566bff139b4SDaeho Jeong 	return 0;
1567bff139b4SDaeho Jeong }
1568bff139b4SDaeho Jeong 
1569bff139b4SDaeho Jeong static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
1570bff139b4SDaeho Jeong 		bool bypass_destroy_callback, bool pre_alloc)
1571bff139b4SDaeho Jeong {
1572bff139b4SDaeho Jeong 	const struct f2fs_compress_ops *cops =
1573bff139b4SDaeho Jeong 		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
1574bff139b4SDaeho Jeong 
1575bff139b4SDaeho Jeong 	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
1576bff139b4SDaeho Jeong 		return;
1577bff139b4SDaeho Jeong 
1578bff139b4SDaeho Jeong 	if (!bypass_destroy_callback && cops->destroy_decompress_ctx)
1579bff139b4SDaeho Jeong 		cops->destroy_decompress_ctx(dic);
1580bff139b4SDaeho Jeong 
1581bff139b4SDaeho Jeong 	if (dic->cbuf)
1582bff139b4SDaeho Jeong 		vm_unmap_ram(dic->cbuf, dic->nr_cpages);
1583bff139b4SDaeho Jeong 
1584bff139b4SDaeho Jeong 	if (dic->rbuf)
1585bff139b4SDaeho Jeong 		vm_unmap_ram(dic->rbuf, dic->cluster_size);
1586bff139b4SDaeho Jeong }
1587bff139b4SDaeho Jeong 
1588bff139b4SDaeho Jeong static void f2fs_free_dic(struct decompress_io_ctx *dic,
1589bff139b4SDaeho Jeong 		bool bypass_destroy_callback);
15907f59b277SEric Biggers 
15914c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
15924c8ff709SChao Yu {
15934c8ff709SChao Yu 	struct decompress_io_ctx *dic;
15944c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
1595bff139b4SDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1596bff139b4SDaeho Jeong 	int i, ret;
15974c8ff709SChao Yu 
1598bff139b4SDaeho Jeong 	dic = f2fs_kmem_cache_alloc(dic_entry_slab, GFP_F2FS_ZERO, false, sbi);
15994c8ff709SChao Yu 	if (!dic)
16004c8ff709SChao Yu 		return ERR_PTR(-ENOMEM);
16014c8ff709SChao Yu 
160231083031SChao Yu 	dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
16034c8ff709SChao Yu 	if (!dic->rpages) {
1604c68d6c88SChao Yu 		kmem_cache_free(dic_entry_slab, dic);
16054c8ff709SChao Yu 		return ERR_PTR(-ENOMEM);
16064c8ff709SChao Yu 	}
16074c8ff709SChao Yu 
16084c8ff709SChao Yu 	dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
16094c8ff709SChao Yu 	dic->inode = cc->inode;
16107f59b277SEric Biggers 	atomic_set(&dic->remaining_pages, cc->nr_cpages);
16114c8ff709SChao Yu 	dic->cluster_idx = cc->cluster_idx;
16124c8ff709SChao Yu 	dic->cluster_size = cc->cluster_size;
16134c8ff709SChao Yu 	dic->log_cluster_size = cc->log_cluster_size;
16144c8ff709SChao Yu 	dic->nr_cpages = cc->nr_cpages;
16157f59b277SEric Biggers 	refcount_set(&dic->refcnt, 1);
16164c8ff709SChao Yu 	dic->failed = false;
16177f59b277SEric Biggers 	dic->need_verity = f2fs_need_verity(cc->inode, start_idx);
16184c8ff709SChao Yu 
16194c8ff709SChao Yu 	for (i = 0; i < dic->cluster_size; i++)
16204c8ff709SChao Yu 		dic->rpages[i] = cc->rpages[i];
16214c8ff709SChao Yu 	dic->nr_rpages = cc->cluster_size;
16224c8ff709SChao Yu 
162331083031SChao Yu 	dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
1624bff139b4SDaeho Jeong 	if (!dic->cpages) {
1625bff139b4SDaeho Jeong 		ret = -ENOMEM;
16264c8ff709SChao Yu 		goto out_free;
1627bff139b4SDaeho Jeong 	}
16284c8ff709SChao Yu 
16294c8ff709SChao Yu 	for (i = 0; i < dic->nr_cpages; i++) {
16304c8ff709SChao Yu 		struct page *page;
16314c8ff709SChao Yu 
16325e6bbde9SChao Yu 		page = f2fs_compress_alloc_page();
1633bff139b4SDaeho Jeong 		if (!page) {
1634bff139b4SDaeho Jeong 			ret = -ENOMEM;
16354c8ff709SChao Yu 			goto out_free;
1636bff139b4SDaeho Jeong 		}
16374c8ff709SChao Yu 
16384c8ff709SChao Yu 		f2fs_set_compressed_page(page, cc->inode,
1639887347a0SChao Yu 					start_idx + i + 1, dic);
16404c8ff709SChao Yu 		dic->cpages[i] = page;
16414c8ff709SChao Yu 	}
16424c8ff709SChao Yu 
1643bff139b4SDaeho Jeong 	ret = f2fs_prepare_decomp_mem(dic, true);
1644bff139b4SDaeho Jeong 	if (ret)
1645bff139b4SDaeho Jeong 		goto out_free;
1646bff139b4SDaeho Jeong 
16474c8ff709SChao Yu 	return dic;
16484c8ff709SChao Yu 
16494c8ff709SChao Yu out_free:
1650bff139b4SDaeho Jeong 	f2fs_free_dic(dic, true);
1651bff139b4SDaeho Jeong 	return ERR_PTR(ret);
16524c8ff709SChao Yu }
16534c8ff709SChao Yu 
1654bff139b4SDaeho Jeong static void f2fs_free_dic(struct decompress_io_ctx *dic,
1655bff139b4SDaeho Jeong 		bool bypass_destroy_callback)
16564c8ff709SChao Yu {
16574c8ff709SChao Yu 	int i;
16584c8ff709SChao Yu 
1659bff139b4SDaeho Jeong 	f2fs_release_decomp_mem(dic, bypass_destroy_callback, true);
1660bff139b4SDaeho Jeong 
16614c8ff709SChao Yu 	if (dic->tpages) {
16624c8ff709SChao Yu 		for (i = 0; i < dic->cluster_size; i++) {
16634c8ff709SChao Yu 			if (dic->rpages[i])
16644c8ff709SChao Yu 				continue;
16658908e753SChao Yu 			if (!dic->tpages[i])
16668908e753SChao Yu 				continue;
16675e6bbde9SChao Yu 			f2fs_compress_free_page(dic->tpages[i]);
16684c8ff709SChao Yu 		}
166931083031SChao Yu 		page_array_free(dic->inode, dic->tpages, dic->cluster_size);
16704c8ff709SChao Yu 	}
16714c8ff709SChao Yu 
16724c8ff709SChao Yu 	if (dic->cpages) {
16734c8ff709SChao Yu 		for (i = 0; i < dic->nr_cpages; i++) {
16744c8ff709SChao Yu 			if (!dic->cpages[i])
16754c8ff709SChao Yu 				continue;
16765e6bbde9SChao Yu 			f2fs_compress_free_page(dic->cpages[i]);
16774c8ff709SChao Yu 		}
167831083031SChao Yu 		page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
16794c8ff709SChao Yu 	}
16804c8ff709SChao Yu 
168131083031SChao Yu 	page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
1682c68d6c88SChao Yu 	kmem_cache_free(dic_entry_slab, dic);
16834c8ff709SChao Yu }
16844c8ff709SChao Yu 
1685bff139b4SDaeho Jeong static void f2fs_late_free_dic(struct work_struct *work)
16867f59b277SEric Biggers {
1687bff139b4SDaeho Jeong 	struct decompress_io_ctx *dic =
1688bff139b4SDaeho Jeong 		container_of(work, struct decompress_io_ctx, free_work);
1689bff139b4SDaeho Jeong 
1690bff139b4SDaeho Jeong 	f2fs_free_dic(dic, false);
1691bff139b4SDaeho Jeong }
1692bff139b4SDaeho Jeong 
1693bff139b4SDaeho Jeong static void f2fs_put_dic(struct decompress_io_ctx *dic, bool in_task)
1694bff139b4SDaeho Jeong {
1695bff139b4SDaeho Jeong 	if (refcount_dec_and_test(&dic->refcnt)) {
1696bff139b4SDaeho Jeong 		if (in_task) {
1697bff139b4SDaeho Jeong 			f2fs_free_dic(dic, false);
1698bff139b4SDaeho Jeong 		} else {
1699bff139b4SDaeho Jeong 			INIT_WORK(&dic->free_work, f2fs_late_free_dic);
1700bff139b4SDaeho Jeong 			queue_work(F2FS_I_SB(dic->inode)->post_read_wq,
1701bff139b4SDaeho Jeong 					&dic->free_work);
1702bff139b4SDaeho Jeong 		}
1703bff139b4SDaeho Jeong 	}
17047f59b277SEric Biggers }
17057f59b277SEric Biggers 
17067f59b277SEric Biggers static void f2fs_verify_cluster(struct work_struct *work)
17077f59b277SEric Biggers {
17087f59b277SEric Biggers 	struct decompress_io_ctx *dic =
17097f59b277SEric Biggers 		container_of(work, struct decompress_io_ctx, verity_work);
17107f59b277SEric Biggers 	int i;
17117f59b277SEric Biggers 
171298dc08baSEric Biggers 	/* Verify, update, and unlock the decompressed pages. */
17137f59b277SEric Biggers 	for (i = 0; i < dic->cluster_size; i++) {
17147f59b277SEric Biggers 		struct page *rpage = dic->rpages[i];
17157f59b277SEric Biggers 
171698dc08baSEric Biggers 		if (!rpage)
171798dc08baSEric Biggers 			continue;
171898dc08baSEric Biggers 
171998dc08baSEric Biggers 		if (fsverity_verify_page(rpage))
172098dc08baSEric Biggers 			SetPageUptodate(rpage);
172198dc08baSEric Biggers 		else
172298dc08baSEric Biggers 			ClearPageUptodate(rpage);
172398dc08baSEric Biggers 		unlock_page(rpage);
17247f59b277SEric Biggers 	}
17257f59b277SEric Biggers 
172698dc08baSEric Biggers 	f2fs_put_dic(dic, true);
17277f59b277SEric Biggers }
17287f59b277SEric Biggers 
17297f59b277SEric Biggers /*
17307f59b277SEric Biggers  * This is called when a compressed cluster has been decompressed
17317f59b277SEric Biggers  * (or failed to be read and/or decompressed).
17327f59b277SEric Biggers  */
1733bff139b4SDaeho Jeong void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
1734bff139b4SDaeho Jeong 				bool in_task)
17357f59b277SEric Biggers {
173698dc08baSEric Biggers 	int i;
173798dc08baSEric Biggers 
17387f59b277SEric Biggers 	if (!failed && dic->need_verity) {
17397f59b277SEric Biggers 		/*
17407f59b277SEric Biggers 		 * Note that to avoid deadlocks, the verity work can't be done
17417f59b277SEric Biggers 		 * on the decompression workqueue.  This is because verifying
17427f59b277SEric Biggers 		 * the data pages can involve reading metadata pages from the
17437f59b277SEric Biggers 		 * file, and these metadata pages may be compressed.
17447f59b277SEric Biggers 		 */
17457f59b277SEric Biggers 		INIT_WORK(&dic->verity_work, f2fs_verify_cluster);
17467f59b277SEric Biggers 		fsverity_enqueue_verify_work(&dic->verity_work);
174798dc08baSEric Biggers 		return;
17487f59b277SEric Biggers 	}
174998dc08baSEric Biggers 
175098dc08baSEric Biggers 	/* Update and unlock the cluster's pagecache pages. */
175198dc08baSEric Biggers 	for (i = 0; i < dic->cluster_size; i++) {
175298dc08baSEric Biggers 		struct page *rpage = dic->rpages[i];
175398dc08baSEric Biggers 
175498dc08baSEric Biggers 		if (!rpage)
175598dc08baSEric Biggers 			continue;
175698dc08baSEric Biggers 
175798dc08baSEric Biggers 		if (failed)
175898dc08baSEric Biggers 			ClearPageUptodate(rpage);
175998dc08baSEric Biggers 		else
176098dc08baSEric Biggers 			SetPageUptodate(rpage);
176198dc08baSEric Biggers 		unlock_page(rpage);
176298dc08baSEric Biggers 	}
176398dc08baSEric Biggers 
176498dc08baSEric Biggers 	/*
176598dc08baSEric Biggers 	 * Release the reference to the decompress_io_ctx that was being held
176698dc08baSEric Biggers 	 * for I/O completion.
176798dc08baSEric Biggers 	 */
176898dc08baSEric Biggers 	f2fs_put_dic(dic, in_task);
17697f59b277SEric Biggers }
17707f59b277SEric Biggers 
17717f59b277SEric Biggers /*
17727f59b277SEric Biggers  * Put a reference to a compressed page's decompress_io_ctx.
17737f59b277SEric Biggers  *
17747f59b277SEric Biggers  * This is called when the page is no longer needed and can be freed.
17757f59b277SEric Biggers  */
1776bff139b4SDaeho Jeong void f2fs_put_page_dic(struct page *page, bool in_task)
17777f59b277SEric Biggers {
17787f59b277SEric Biggers 	struct decompress_io_ctx *dic =
17797f59b277SEric Biggers 			(struct decompress_io_ctx *)page_private(page);
17807f59b277SEric Biggers 
1781bff139b4SDaeho Jeong 	f2fs_put_dic(dic, in_task);
17824c8ff709SChao Yu }
178331083031SChao Yu 
178494afd6d6SChao Yu /*
178594afd6d6SChao Yu  * check whether cluster blocks are contiguous, and add extent cache entry
178694afd6d6SChao Yu  * only if cluster blocks are logically and physically contiguous.
178794afd6d6SChao Yu  */
178894afd6d6SChao Yu unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn)
178994afd6d6SChao Yu {
179094afd6d6SChao Yu 	bool compressed = f2fs_data_blkaddr(dn) == COMPRESS_ADDR;
179194afd6d6SChao Yu 	int i = compressed ? 1 : 0;
179294afd6d6SChao Yu 	block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_page,
179394afd6d6SChao Yu 						dn->ofs_in_node + i);
179494afd6d6SChao Yu 
179594afd6d6SChao Yu 	for (i += 1; i < F2FS_I(dn->inode)->i_cluster_size; i++) {
179694afd6d6SChao Yu 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
179794afd6d6SChao Yu 						dn->ofs_in_node + i);
179894afd6d6SChao Yu 
179994afd6d6SChao Yu 		if (!__is_valid_data_blkaddr(blkaddr))
180094afd6d6SChao Yu 			break;
180194afd6d6SChao Yu 		if (first_blkaddr + i - (compressed ? 1 : 0) != blkaddr)
180294afd6d6SChao Yu 			return 0;
180394afd6d6SChao Yu 	}
180494afd6d6SChao Yu 
180594afd6d6SChao Yu 	return compressed ? i - 1 : i;
180694afd6d6SChao Yu }
180794afd6d6SChao Yu 
18086ce19affSChao Yu const struct address_space_operations f2fs_compress_aops = {
1809c26cd045SMatthew Wilcox (Oracle) 	.release_folio = f2fs_release_folio,
181091503996SMatthew Wilcox (Oracle) 	.invalidate_folio = f2fs_invalidate_folio,
1811f35474ecSChao Yu 	.migrate_folio	= filemap_migrate_folio,
18126ce19affSChao Yu };
18136ce19affSChao Yu 
18146ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
18156ce19affSChao Yu {
18166ce19affSChao Yu 	return sbi->compress_inode->i_mapping;
18176ce19affSChao Yu }
18186ce19affSChao Yu 
18196ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
18206ce19affSChao Yu {
18216ce19affSChao Yu 	if (!sbi->compress_inode)
18226ce19affSChao Yu 		return;
18236ce19affSChao Yu 	invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
18246ce19affSChao Yu }
18256ce19affSChao Yu 
18266ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
18276ce19affSChao Yu 						nid_t ino, block_t blkaddr)
18286ce19affSChao Yu {
18296ce19affSChao Yu 	struct page *cpage;
18306ce19affSChao Yu 	int ret;
18316ce19affSChao Yu 
18326ce19affSChao Yu 	if (!test_opt(sbi, COMPRESS_CACHE))
18336ce19affSChao Yu 		return;
18346ce19affSChao Yu 
18356ce19affSChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
18366ce19affSChao Yu 		return;
18376ce19affSChao Yu 
18386ce19affSChao Yu 	if (!f2fs_available_free_memory(sbi, COMPRESS_PAGE))
18396ce19affSChao Yu 		return;
18406ce19affSChao Yu 
18416ce19affSChao Yu 	cpage = find_get_page(COMPRESS_MAPPING(sbi), blkaddr);
18426ce19affSChao Yu 	if (cpage) {
18436ce19affSChao Yu 		f2fs_put_page(cpage, 0);
18446ce19affSChao Yu 		return;
18456ce19affSChao Yu 	}
18466ce19affSChao Yu 
18476ce19affSChao Yu 	cpage = alloc_page(__GFP_NOWARN | __GFP_IO);
18486ce19affSChao Yu 	if (!cpage)
18496ce19affSChao Yu 		return;
18506ce19affSChao Yu 
18516ce19affSChao Yu 	ret = add_to_page_cache_lru(cpage, COMPRESS_MAPPING(sbi),
18526ce19affSChao Yu 						blkaddr, GFP_NOFS);
18536ce19affSChao Yu 	if (ret) {
18546ce19affSChao Yu 		f2fs_put_page(cpage, 0);
18556ce19affSChao Yu 		return;
18566ce19affSChao Yu 	}
18576ce19affSChao Yu 
18586ce19affSChao Yu 	set_page_private_data(cpage, ino);
18596ce19affSChao Yu 
18606ce19affSChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
18616ce19affSChao Yu 		goto out;
18626ce19affSChao Yu 
18636ce19affSChao Yu 	memcpy(page_address(cpage), page_address(page), PAGE_SIZE);
18646ce19affSChao Yu 	SetPageUptodate(cpage);
18656ce19affSChao Yu out:
18666ce19affSChao Yu 	f2fs_put_page(cpage, 1);
18676ce19affSChao Yu }
18686ce19affSChao Yu 
18696ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
18706ce19affSChao Yu 								block_t blkaddr)
18716ce19affSChao Yu {
18726ce19affSChao Yu 	struct page *cpage;
18736ce19affSChao Yu 	bool hitted = false;
18746ce19affSChao Yu 
18756ce19affSChao Yu 	if (!test_opt(sbi, COMPRESS_CACHE))
18766ce19affSChao Yu 		return false;
18776ce19affSChao Yu 
18786ce19affSChao Yu 	cpage = f2fs_pagecache_get_page(COMPRESS_MAPPING(sbi),
18796ce19affSChao Yu 				blkaddr, FGP_LOCK | FGP_NOWAIT, GFP_NOFS);
18806ce19affSChao Yu 	if (cpage) {
18816ce19affSChao Yu 		if (PageUptodate(cpage)) {
18826ce19affSChao Yu 			atomic_inc(&sbi->compress_page_hit);
18836ce19affSChao Yu 			memcpy(page_address(page),
18846ce19affSChao Yu 				page_address(cpage), PAGE_SIZE);
18856ce19affSChao Yu 			hitted = true;
18866ce19affSChao Yu 		}
18876ce19affSChao Yu 		f2fs_put_page(cpage, 1);
18886ce19affSChao Yu 	}
18896ce19affSChao Yu 
18906ce19affSChao Yu 	return hitted;
18916ce19affSChao Yu }
18926ce19affSChao Yu 
18936ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino)
18946ce19affSChao Yu {
1895173cdf2cSZhang Qilong 	struct address_space *mapping = COMPRESS_MAPPING(sbi);
1896bbfe4f66SMatthew Wilcox (Oracle) 	struct folio_batch fbatch;
18976ce19affSChao Yu 	pgoff_t index = 0;
18986ce19affSChao Yu 	pgoff_t end = MAX_BLKADDR(sbi);
18996ce19affSChao Yu 
19006ce19affSChao Yu 	if (!mapping->nrpages)
19016ce19affSChao Yu 		return;
19026ce19affSChao Yu 
1903bbfe4f66SMatthew Wilcox (Oracle) 	folio_batch_init(&fbatch);
19046ce19affSChao Yu 
19056ce19affSChao Yu 	do {
1906bbfe4f66SMatthew Wilcox (Oracle) 		unsigned int nr, i;
19076ce19affSChao Yu 
1908bbfe4f66SMatthew Wilcox (Oracle) 		nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
1909bbfe4f66SMatthew Wilcox (Oracle) 		if (!nr)
19106ce19affSChao Yu 			break;
19116ce19affSChao Yu 
1912bbfe4f66SMatthew Wilcox (Oracle) 		for (i = 0; i < nr; i++) {
1913bbfe4f66SMatthew Wilcox (Oracle) 			struct folio *folio = fbatch.folios[i];
19146ce19affSChao Yu 
1915bbfe4f66SMatthew Wilcox (Oracle) 			folio_lock(folio);
1916bbfe4f66SMatthew Wilcox (Oracle) 			if (folio->mapping != mapping) {
1917bbfe4f66SMatthew Wilcox (Oracle) 				folio_unlock(folio);
19186ce19affSChao Yu 				continue;
19196ce19affSChao Yu 			}
19206ce19affSChao Yu 
1921bbfe4f66SMatthew Wilcox (Oracle) 			if (ino != get_page_private_data(&folio->page)) {
1922bbfe4f66SMatthew Wilcox (Oracle) 				folio_unlock(folio);
19236ce19affSChao Yu 				continue;
19246ce19affSChao Yu 			}
19256ce19affSChao Yu 
1926bbfe4f66SMatthew Wilcox (Oracle) 			generic_error_remove_page(mapping, &folio->page);
1927bbfe4f66SMatthew Wilcox (Oracle) 			folio_unlock(folio);
19286ce19affSChao Yu 		}
1929bbfe4f66SMatthew Wilcox (Oracle) 		folio_batch_release(&fbatch);
19306ce19affSChao Yu 		cond_resched();
19316ce19affSChao Yu 	} while (index < end);
19326ce19affSChao Yu }
19336ce19affSChao Yu 
19346ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi)
19356ce19affSChao Yu {
19366ce19affSChao Yu 	struct inode *inode;
19376ce19affSChao Yu 
19386ce19affSChao Yu 	if (!test_opt(sbi, COMPRESS_CACHE))
19396ce19affSChao Yu 		return 0;
19406ce19affSChao Yu 
19416ce19affSChao Yu 	inode = f2fs_iget(sbi->sb, F2FS_COMPRESS_INO(sbi));
19426ce19affSChao Yu 	if (IS_ERR(inode))
19436ce19affSChao Yu 		return PTR_ERR(inode);
19446ce19affSChao Yu 	sbi->compress_inode = inode;
19456ce19affSChao Yu 
19466ce19affSChao Yu 	sbi->compress_percent = COMPRESS_PERCENT;
19476ce19affSChao Yu 	sbi->compress_watermark = COMPRESS_WATERMARK;
19486ce19affSChao Yu 
19496ce19affSChao Yu 	atomic_set(&sbi->compress_page_hit, 0);
19506ce19affSChao Yu 
19516ce19affSChao Yu 	return 0;
19526ce19affSChao Yu }
19536ce19affSChao Yu 
19546ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi)
19556ce19affSChao Yu {
19566ce19affSChao Yu 	if (!sbi->compress_inode)
19576ce19affSChao Yu 		return;
19586ce19affSChao Yu 	iput(sbi->compress_inode);
19596ce19affSChao Yu 	sbi->compress_inode = NULL;
19606ce19affSChao Yu }
19616ce19affSChao Yu 
196231083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
196331083031SChao Yu {
196431083031SChao Yu 	dev_t dev = sbi->sb->s_bdev->bd_dev;
196531083031SChao Yu 	char slab_name[32];
196631083031SChao Yu 
196729be7ec3SChao Yu 	if (!f2fs_sb_has_compression(sbi))
196829be7ec3SChao Yu 		return 0;
196929be7ec3SChao Yu 
197031083031SChao Yu 	sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
197131083031SChao Yu 
197231083031SChao Yu 	sbi->page_array_slab_size = sizeof(struct page *) <<
197331083031SChao Yu 					F2FS_OPTION(sbi).compress_log_size;
197431083031SChao Yu 
197531083031SChao Yu 	sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
197631083031SChao Yu 					sbi->page_array_slab_size);
1977870af777SYangtao Li 	return sbi->page_array_slab ? 0 : -ENOMEM;
197831083031SChao Yu }
197931083031SChao Yu 
198031083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
198131083031SChao Yu {
198231083031SChao Yu 	kmem_cache_destroy(sbi->page_array_slab);
198331083031SChao Yu }
1984c68d6c88SChao Yu 
1985870af777SYangtao Li int __init f2fs_init_compress_cache(void)
1986c68d6c88SChao Yu {
1987c68d6c88SChao Yu 	cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
1988c68d6c88SChao Yu 					sizeof(struct compress_io_ctx));
1989c68d6c88SChao Yu 	if (!cic_entry_slab)
1990c68d6c88SChao Yu 		return -ENOMEM;
1991c68d6c88SChao Yu 	dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
1992c68d6c88SChao Yu 					sizeof(struct decompress_io_ctx));
1993c68d6c88SChao Yu 	if (!dic_entry_slab)
1994c68d6c88SChao Yu 		goto free_cic;
1995c68d6c88SChao Yu 	return 0;
1996c68d6c88SChao Yu free_cic:
1997870af777SYangtao Li 	kmem_cache_destroy(cic_entry_slab);
1998c68d6c88SChao Yu 	return -ENOMEM;
1999c68d6c88SChao Yu }
2000c68d6c88SChao Yu 
2001c68d6c88SChao Yu void f2fs_destroy_compress_cache(void)
2002c68d6c88SChao Yu {
2003870af777SYangtao Li 	kmem_cache_destroy(dic_entry_slab);
2004870af777SYangtao Li 	kmem_cache_destroy(cic_entry_slab);
2005c68d6c88SChao Yu }
2006