xref: /linux/fs/f2fs/compress.c (revision 31083031709eea6530f0551d59eecdb2e68265ef)
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>
104c8ff709SChao Yu #include <linux/writeback.h>
114c8ff709SChao Yu #include <linux/backing-dev.h>
124c8ff709SChao Yu #include <linux/lzo.h>
134c8ff709SChao Yu #include <linux/lz4.h>
1450cfa66fSChao Yu #include <linux/zstd.h>
154c8ff709SChao Yu 
164c8ff709SChao Yu #include "f2fs.h"
174c8ff709SChao Yu #include "node.h"
184c8ff709SChao Yu #include <trace/events/f2fs.h>
194c8ff709SChao Yu 
20*31083031SChao Yu static void *page_array_alloc(struct inode *inode, int nr)
21*31083031SChao Yu {
22*31083031SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
23*31083031SChao Yu 	unsigned int size = sizeof(struct page *) * nr;
24*31083031SChao Yu 
25*31083031SChao Yu 	if (likely(size <= sbi->page_array_slab_size))
26*31083031SChao Yu 		return kmem_cache_zalloc(sbi->page_array_slab, GFP_NOFS);
27*31083031SChao Yu 	return f2fs_kzalloc(sbi, size, GFP_NOFS);
28*31083031SChao Yu }
29*31083031SChao Yu 
30*31083031SChao Yu static void page_array_free(struct inode *inode, void *pages, int nr)
31*31083031SChao Yu {
32*31083031SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
33*31083031SChao Yu 	unsigned int size = sizeof(struct page *) * nr;
34*31083031SChao Yu 
35*31083031SChao Yu 	if (!pages)
36*31083031SChao Yu 		return;
37*31083031SChao Yu 
38*31083031SChao Yu 	if (likely(size <= sbi->page_array_slab_size))
39*31083031SChao Yu 		kmem_cache_free(sbi->page_array_slab, pages);
40*31083031SChao Yu 	else
41*31083031SChao Yu 		kfree(pages);
42*31083031SChao Yu }
43*31083031SChao Yu 
444c8ff709SChao Yu struct f2fs_compress_ops {
454c8ff709SChao Yu 	int (*init_compress_ctx)(struct compress_ctx *cc);
464c8ff709SChao Yu 	void (*destroy_compress_ctx)(struct compress_ctx *cc);
474c8ff709SChao Yu 	int (*compress_pages)(struct compress_ctx *cc);
4823b1faaaSChao Yu 	int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
4923b1faaaSChao Yu 	void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
504c8ff709SChao Yu 	int (*decompress_pages)(struct decompress_io_ctx *dic);
514c8ff709SChao Yu };
524c8ff709SChao Yu 
534c8ff709SChao Yu static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
544c8ff709SChao Yu {
554c8ff709SChao Yu 	return index & (cc->cluster_size - 1);
564c8ff709SChao Yu }
574c8ff709SChao Yu 
584c8ff709SChao Yu static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
594c8ff709SChao Yu {
604c8ff709SChao Yu 	return index >> cc->log_cluster_size;
614c8ff709SChao Yu }
624c8ff709SChao Yu 
634c8ff709SChao Yu static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
644c8ff709SChao Yu {
654c8ff709SChao Yu 	return cc->cluster_idx << cc->log_cluster_size;
664c8ff709SChao Yu }
674c8ff709SChao Yu 
684c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page)
694c8ff709SChao Yu {
704c8ff709SChao Yu 	if (!PagePrivate(page))
714c8ff709SChao Yu 		return false;
724c8ff709SChao Yu 	if (!page_private(page))
734c8ff709SChao Yu 		return false;
744c8ff709SChao Yu 	if (IS_ATOMIC_WRITTEN_PAGE(page) || IS_DUMMY_WRITTEN_PAGE(page))
754c8ff709SChao Yu 		return false;
7629b993c7SYu Changchun 	/*
7729b993c7SYu Changchun 	 * page->private may be set with pid.
7829b993c7SYu Changchun 	 * pid_max is enough to check if it is traced.
7929b993c7SYu Changchun 	 */
8029b993c7SYu Changchun 	if (IS_IO_TRACED_PAGE(page))
8129b993c7SYu Changchun 		return false;
8229b993c7SYu Changchun 
834c8ff709SChao Yu 	f2fs_bug_on(F2FS_M_SB(page->mapping),
844c8ff709SChao Yu 		*((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
854c8ff709SChao Yu 	return true;
864c8ff709SChao Yu }
874c8ff709SChao Yu 
884c8ff709SChao Yu static void f2fs_set_compressed_page(struct page *page,
89887347a0SChao Yu 		struct inode *inode, pgoff_t index, void *data)
904c8ff709SChao Yu {
914c8ff709SChao Yu 	SetPagePrivate(page);
924c8ff709SChao Yu 	set_page_private(page, (unsigned long)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 
123bc67c5d0SChao Yu static void f2fs_put_rpages_mapping(struct address_space *mapping,
1244c8ff709SChao Yu 				pgoff_t start, int len)
1254c8ff709SChao Yu {
1264c8ff709SChao Yu 	int i;
1274c8ff709SChao Yu 
1284c8ff709SChao Yu 	for (i = 0; i < len; i++) {
1294c8ff709SChao Yu 		struct page *page = find_get_page(mapping, start + i);
1304c8ff709SChao Yu 
1314c8ff709SChao Yu 		put_page(page);
1324c8ff709SChao Yu 		put_page(page);
1334c8ff709SChao Yu 	}
1344c8ff709SChao Yu }
1354c8ff709SChao Yu 
1364c8ff709SChao Yu static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
1374c8ff709SChao Yu 		struct writeback_control *wbc, bool redirty, int unlock)
1384c8ff709SChao Yu {
1394c8ff709SChao Yu 	unsigned int i;
1404c8ff709SChao Yu 
1414c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
1424c8ff709SChao Yu 		if (!cc->rpages[i])
1434c8ff709SChao Yu 			continue;
1444c8ff709SChao Yu 		if (redirty)
1454c8ff709SChao Yu 			redirty_page_for_writepage(wbc, cc->rpages[i]);
1464c8ff709SChao Yu 		f2fs_put_page(cc->rpages[i], unlock);
1474c8ff709SChao Yu 	}
1484c8ff709SChao Yu }
1494c8ff709SChao Yu 
1504c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page)
1514c8ff709SChao Yu {
1524c8ff709SChao Yu 	return ((struct compress_io_ctx *)page_private(page))->rpages[0];
1534c8ff709SChao Yu }
1544c8ff709SChao Yu 
1554c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc)
1564c8ff709SChao Yu {
1574c8ff709SChao Yu 	if (cc->nr_rpages)
1584c8ff709SChao Yu 		return 0;
1594c8ff709SChao Yu 
160*31083031SChao Yu 	cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1614c8ff709SChao Yu 	return cc->rpages ? 0 : -ENOMEM;
1624c8ff709SChao Yu }
1634c8ff709SChao Yu 
1644c8ff709SChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc)
1654c8ff709SChao Yu {
166*31083031SChao Yu 	page_array_free(cc->inode, cc->rpages, cc->cluster_size);
1674c8ff709SChao Yu 	cc->rpages = NULL;
1684c8ff709SChao Yu 	cc->nr_rpages = 0;
1694c8ff709SChao Yu 	cc->nr_cpages = 0;
1704c8ff709SChao Yu 	cc->cluster_idx = NULL_CLUSTER;
1714c8ff709SChao Yu }
1724c8ff709SChao Yu 
1734c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
1744c8ff709SChao Yu {
1754c8ff709SChao Yu 	unsigned int cluster_ofs;
1764c8ff709SChao Yu 
1774c8ff709SChao Yu 	if (!f2fs_cluster_can_merge_page(cc, page->index))
1784c8ff709SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
1794c8ff709SChao Yu 
1804c8ff709SChao Yu 	cluster_ofs = offset_in_cluster(cc, page->index);
1814c8ff709SChao Yu 	cc->rpages[cluster_ofs] = page;
1824c8ff709SChao Yu 	cc->nr_rpages++;
1834c8ff709SChao Yu 	cc->cluster_idx = cluster_idx(cc, page->index);
1844c8ff709SChao Yu }
1854c8ff709SChao Yu 
1864c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO
1874c8ff709SChao Yu static int lzo_init_compress_ctx(struct compress_ctx *cc)
1884c8ff709SChao Yu {
1894c8ff709SChao Yu 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
1904c8ff709SChao Yu 				LZO1X_MEM_COMPRESS, GFP_NOFS);
1914c8ff709SChao Yu 	if (!cc->private)
1924c8ff709SChao Yu 		return -ENOMEM;
1934c8ff709SChao Yu 
1944c8ff709SChao Yu 	cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
1954c8ff709SChao Yu 	return 0;
1964c8ff709SChao Yu }
1974c8ff709SChao Yu 
1984c8ff709SChao Yu static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
1994c8ff709SChao Yu {
2004c8ff709SChao Yu 	kvfree(cc->private);
2014c8ff709SChao Yu 	cc->private = NULL;
2024c8ff709SChao Yu }
2034c8ff709SChao Yu 
2044c8ff709SChao Yu static int lzo_compress_pages(struct compress_ctx *cc)
2054c8ff709SChao Yu {
2064c8ff709SChao Yu 	int ret;
2074c8ff709SChao Yu 
2084c8ff709SChao Yu 	ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
2094c8ff709SChao Yu 					&cc->clen, cc->private);
2104c8ff709SChao Yu 	if (ret != LZO_E_OK) {
2114c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
2124c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
2134c8ff709SChao Yu 		return -EIO;
2144c8ff709SChao Yu 	}
2154c8ff709SChao Yu 	return 0;
2164c8ff709SChao Yu }
2174c8ff709SChao Yu 
2184c8ff709SChao Yu static int lzo_decompress_pages(struct decompress_io_ctx *dic)
2194c8ff709SChao Yu {
2204c8ff709SChao Yu 	int ret;
2214c8ff709SChao Yu 
2224c8ff709SChao Yu 	ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
2234c8ff709SChao Yu 						dic->rbuf, &dic->rlen);
2244c8ff709SChao Yu 	if (ret != LZO_E_OK) {
2254c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
2264c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
2274c8ff709SChao Yu 		return -EIO;
2284c8ff709SChao Yu 	}
2294c8ff709SChao Yu 
2304c8ff709SChao Yu 	if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
2314c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
2324c8ff709SChao Yu 					"expected:%lu\n", KERN_ERR,
2334c8ff709SChao Yu 					F2FS_I_SB(dic->inode)->sb->s_id,
2344c8ff709SChao Yu 					dic->rlen,
2354c8ff709SChao Yu 					PAGE_SIZE << dic->log_cluster_size);
2364c8ff709SChao Yu 		return -EIO;
2374c8ff709SChao Yu 	}
2384c8ff709SChao Yu 	return 0;
2394c8ff709SChao Yu }
2404c8ff709SChao Yu 
2414c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lzo_ops = {
2424c8ff709SChao Yu 	.init_compress_ctx	= lzo_init_compress_ctx,
2434c8ff709SChao Yu 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
2444c8ff709SChao Yu 	.compress_pages		= lzo_compress_pages,
2454c8ff709SChao Yu 	.decompress_pages	= lzo_decompress_pages,
2464c8ff709SChao Yu };
2474c8ff709SChao Yu #endif
2484c8ff709SChao Yu 
2494c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4
2504c8ff709SChao Yu static int lz4_init_compress_ctx(struct compress_ctx *cc)
2514c8ff709SChao Yu {
2524c8ff709SChao Yu 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
2534c8ff709SChao Yu 				LZ4_MEM_COMPRESS, GFP_NOFS);
2544c8ff709SChao Yu 	if (!cc->private)
2554c8ff709SChao Yu 		return -ENOMEM;
2564c8ff709SChao Yu 
257f6644143SChao Yu 	/*
258f6644143SChao Yu 	 * we do not change cc->clen to LZ4_compressBound(inputsize) to
259f6644143SChao Yu 	 * adapt worst compress case, because lz4 compressor can handle
260f6644143SChao Yu 	 * output budget properly.
261f6644143SChao Yu 	 */
262f6644143SChao Yu 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
2634c8ff709SChao Yu 	return 0;
2644c8ff709SChao Yu }
2654c8ff709SChao Yu 
2664c8ff709SChao Yu static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
2674c8ff709SChao Yu {
2684c8ff709SChao Yu 	kvfree(cc->private);
2694c8ff709SChao Yu 	cc->private = NULL;
2704c8ff709SChao Yu }
2714c8ff709SChao Yu 
2724c8ff709SChao Yu static int lz4_compress_pages(struct compress_ctx *cc)
2734c8ff709SChao Yu {
2744c8ff709SChao Yu 	int len;
2754c8ff709SChao Yu 
2764c8ff709SChao Yu 	len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
2774c8ff709SChao Yu 						cc->clen, cc->private);
278f6644143SChao Yu 	if (!len)
279f6644143SChao Yu 		return -EAGAIN;
280f6644143SChao Yu 
2814c8ff709SChao Yu 	cc->clen = len;
2824c8ff709SChao Yu 	return 0;
2834c8ff709SChao Yu }
2844c8ff709SChao Yu 
2854c8ff709SChao Yu static int lz4_decompress_pages(struct decompress_io_ctx *dic)
2864c8ff709SChao Yu {
2874c8ff709SChao Yu 	int ret;
2884c8ff709SChao Yu 
2894c8ff709SChao Yu 	ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
2904c8ff709SChao Yu 						dic->clen, dic->rlen);
2914c8ff709SChao Yu 	if (ret < 0) {
2924c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
2934c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
2944c8ff709SChao Yu 		return -EIO;
2954c8ff709SChao Yu 	}
2964c8ff709SChao Yu 
2974c8ff709SChao Yu 	if (ret != PAGE_SIZE << dic->log_cluster_size) {
2984c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, "
2994c8ff709SChao Yu 					"expected:%lu\n", KERN_ERR,
3004c8ff709SChao Yu 					F2FS_I_SB(dic->inode)->sb->s_id,
3014c8ff709SChao Yu 					dic->rlen,
3024c8ff709SChao Yu 					PAGE_SIZE << dic->log_cluster_size);
3034c8ff709SChao Yu 		return -EIO;
3044c8ff709SChao Yu 	}
3054c8ff709SChao Yu 	return 0;
3064c8ff709SChao Yu }
3074c8ff709SChao Yu 
3084c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lz4_ops = {
3094c8ff709SChao Yu 	.init_compress_ctx	= lz4_init_compress_ctx,
3104c8ff709SChao Yu 	.destroy_compress_ctx	= lz4_destroy_compress_ctx,
3114c8ff709SChao Yu 	.compress_pages		= lz4_compress_pages,
3124c8ff709SChao Yu 	.decompress_pages	= lz4_decompress_pages,
3134c8ff709SChao Yu };
3144c8ff709SChao Yu #endif
3154c8ff709SChao Yu 
31650cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD
31750cfa66fSChao Yu #define F2FS_ZSTD_DEFAULT_CLEVEL	1
31850cfa66fSChao Yu 
31950cfa66fSChao Yu static int zstd_init_compress_ctx(struct compress_ctx *cc)
32050cfa66fSChao Yu {
32150cfa66fSChao Yu 	ZSTD_parameters params;
32250cfa66fSChao Yu 	ZSTD_CStream *stream;
32350cfa66fSChao Yu 	void *workspace;
32450cfa66fSChao Yu 	unsigned int workspace_size;
32550cfa66fSChao Yu 
32650cfa66fSChao Yu 	params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen, 0);
32750cfa66fSChao Yu 	workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
32850cfa66fSChao Yu 
32950cfa66fSChao Yu 	workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
33050cfa66fSChao Yu 					workspace_size, GFP_NOFS);
33150cfa66fSChao Yu 	if (!workspace)
33250cfa66fSChao Yu 		return -ENOMEM;
33350cfa66fSChao Yu 
33450cfa66fSChao Yu 	stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
33550cfa66fSChao Yu 	if (!stream) {
33650cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n",
33750cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
33850cfa66fSChao Yu 				__func__);
33950cfa66fSChao Yu 		kvfree(workspace);
34050cfa66fSChao Yu 		return -EIO;
34150cfa66fSChao Yu 	}
34250cfa66fSChao Yu 
34350cfa66fSChao Yu 	cc->private = workspace;
34450cfa66fSChao Yu 	cc->private2 = stream;
34550cfa66fSChao Yu 
34650cfa66fSChao Yu 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
34750cfa66fSChao Yu 	return 0;
34850cfa66fSChao Yu }
34950cfa66fSChao Yu 
35050cfa66fSChao Yu static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
35150cfa66fSChao Yu {
35250cfa66fSChao Yu 	kvfree(cc->private);
35350cfa66fSChao Yu 	cc->private = NULL;
35450cfa66fSChao Yu 	cc->private2 = NULL;
35550cfa66fSChao Yu }
35650cfa66fSChao Yu 
35750cfa66fSChao Yu static int zstd_compress_pages(struct compress_ctx *cc)
35850cfa66fSChao Yu {
35950cfa66fSChao Yu 	ZSTD_CStream *stream = cc->private2;
36050cfa66fSChao Yu 	ZSTD_inBuffer inbuf;
36150cfa66fSChao Yu 	ZSTD_outBuffer outbuf;
36250cfa66fSChao Yu 	int src_size = cc->rlen;
36350cfa66fSChao Yu 	int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
36450cfa66fSChao Yu 	int ret;
36550cfa66fSChao Yu 
36650cfa66fSChao Yu 	inbuf.pos = 0;
36750cfa66fSChao Yu 	inbuf.src = cc->rbuf;
36850cfa66fSChao Yu 	inbuf.size = src_size;
36950cfa66fSChao Yu 
37050cfa66fSChao Yu 	outbuf.pos = 0;
37150cfa66fSChao Yu 	outbuf.dst = cc->cbuf->cdata;
37250cfa66fSChao Yu 	outbuf.size = dst_size;
37350cfa66fSChao Yu 
37450cfa66fSChao Yu 	ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
37550cfa66fSChao Yu 	if (ZSTD_isError(ret)) {
37650cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
37750cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
37850cfa66fSChao Yu 				__func__, ZSTD_getErrorCode(ret));
37950cfa66fSChao Yu 		return -EIO;
38050cfa66fSChao Yu 	}
38150cfa66fSChao Yu 
38250cfa66fSChao Yu 	ret = ZSTD_endStream(stream, &outbuf);
38350cfa66fSChao Yu 	if (ZSTD_isError(ret)) {
38450cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n",
38550cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
38650cfa66fSChao Yu 				__func__, ZSTD_getErrorCode(ret));
38750cfa66fSChao Yu 		return -EIO;
38850cfa66fSChao Yu 	}
38950cfa66fSChao Yu 
3901454c978SChao Yu 	/*
3911454c978SChao Yu 	 * there is compressed data remained in intermediate buffer due to
3921454c978SChao Yu 	 * no more space in cbuf.cdata
3931454c978SChao Yu 	 */
3941454c978SChao Yu 	if (ret)
3951454c978SChao Yu 		return -EAGAIN;
3961454c978SChao Yu 
39750cfa66fSChao Yu 	cc->clen = outbuf.pos;
39850cfa66fSChao Yu 	return 0;
39950cfa66fSChao Yu }
40050cfa66fSChao Yu 
40150cfa66fSChao Yu static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
40250cfa66fSChao Yu {
40350cfa66fSChao Yu 	ZSTD_DStream *stream;
40450cfa66fSChao Yu 	void *workspace;
40550cfa66fSChao Yu 	unsigned int workspace_size;
4060e2b7385SChao Yu 	unsigned int max_window_size =
4070e2b7385SChao Yu 			MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
40850cfa66fSChao Yu 
4090e2b7385SChao Yu 	workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size);
41050cfa66fSChao Yu 
41150cfa66fSChao Yu 	workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
41250cfa66fSChao Yu 					workspace_size, GFP_NOFS);
41350cfa66fSChao Yu 	if (!workspace)
41450cfa66fSChao Yu 		return -ENOMEM;
41550cfa66fSChao Yu 
4160e2b7385SChao Yu 	stream = ZSTD_initDStream(max_window_size, workspace, workspace_size);
41750cfa66fSChao Yu 	if (!stream) {
41850cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n",
41950cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
42050cfa66fSChao Yu 				__func__);
42150cfa66fSChao Yu 		kvfree(workspace);
42250cfa66fSChao Yu 		return -EIO;
42350cfa66fSChao Yu 	}
42450cfa66fSChao Yu 
42550cfa66fSChao Yu 	dic->private = workspace;
42650cfa66fSChao Yu 	dic->private2 = stream;
42750cfa66fSChao Yu 
42850cfa66fSChao Yu 	return 0;
42950cfa66fSChao Yu }
43050cfa66fSChao Yu 
43150cfa66fSChao Yu static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
43250cfa66fSChao Yu {
43350cfa66fSChao Yu 	kvfree(dic->private);
43450cfa66fSChao Yu 	dic->private = NULL;
43550cfa66fSChao Yu 	dic->private2 = NULL;
43650cfa66fSChao Yu }
43750cfa66fSChao Yu 
43850cfa66fSChao Yu static int zstd_decompress_pages(struct decompress_io_ctx *dic)
43950cfa66fSChao Yu {
44050cfa66fSChao Yu 	ZSTD_DStream *stream = dic->private2;
44150cfa66fSChao Yu 	ZSTD_inBuffer inbuf;
44250cfa66fSChao Yu 	ZSTD_outBuffer outbuf;
44350cfa66fSChao Yu 	int ret;
44450cfa66fSChao Yu 
44550cfa66fSChao Yu 	inbuf.pos = 0;
44650cfa66fSChao Yu 	inbuf.src = dic->cbuf->cdata;
44750cfa66fSChao Yu 	inbuf.size = dic->clen;
44850cfa66fSChao Yu 
44950cfa66fSChao Yu 	outbuf.pos = 0;
45050cfa66fSChao Yu 	outbuf.dst = dic->rbuf;
45150cfa66fSChao Yu 	outbuf.size = dic->rlen;
45250cfa66fSChao Yu 
45350cfa66fSChao Yu 	ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
45450cfa66fSChao Yu 	if (ZSTD_isError(ret)) {
45550cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
45650cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
45750cfa66fSChao Yu 				__func__, ZSTD_getErrorCode(ret));
45850cfa66fSChao Yu 		return -EIO;
45950cfa66fSChao Yu 	}
46050cfa66fSChao Yu 
46150cfa66fSChao Yu 	if (dic->rlen != outbuf.pos) {
46250cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
46350cfa66fSChao Yu 				"expected:%lu\n", KERN_ERR,
46450cfa66fSChao Yu 				F2FS_I_SB(dic->inode)->sb->s_id,
46550cfa66fSChao Yu 				__func__, dic->rlen,
46650cfa66fSChao Yu 				PAGE_SIZE << dic->log_cluster_size);
46750cfa66fSChao Yu 		return -EIO;
46850cfa66fSChao Yu 	}
46950cfa66fSChao Yu 
47050cfa66fSChao Yu 	return 0;
47150cfa66fSChao Yu }
47250cfa66fSChao Yu 
47350cfa66fSChao Yu static const struct f2fs_compress_ops f2fs_zstd_ops = {
47450cfa66fSChao Yu 	.init_compress_ctx	= zstd_init_compress_ctx,
47550cfa66fSChao Yu 	.destroy_compress_ctx	= zstd_destroy_compress_ctx,
47650cfa66fSChao Yu 	.compress_pages		= zstd_compress_pages,
47750cfa66fSChao Yu 	.init_decompress_ctx	= zstd_init_decompress_ctx,
47850cfa66fSChao Yu 	.destroy_decompress_ctx	= zstd_destroy_decompress_ctx,
47950cfa66fSChao Yu 	.decompress_pages	= zstd_decompress_pages,
48050cfa66fSChao Yu };
48150cfa66fSChao Yu #endif
48250cfa66fSChao Yu 
4836d92b201SChao Yu #ifdef CONFIG_F2FS_FS_LZO
4846d92b201SChao Yu #ifdef CONFIG_F2FS_FS_LZORLE
4856d92b201SChao Yu static int lzorle_compress_pages(struct compress_ctx *cc)
4866d92b201SChao Yu {
4876d92b201SChao Yu 	int ret;
4886d92b201SChao Yu 
4896d92b201SChao Yu 	ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
4906d92b201SChao Yu 					&cc->clen, cc->private);
4916d92b201SChao Yu 	if (ret != LZO_E_OK) {
4926d92b201SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d\n",
4936d92b201SChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
4946d92b201SChao Yu 		return -EIO;
4956d92b201SChao Yu 	}
4966d92b201SChao Yu 	return 0;
4976d92b201SChao Yu }
4986d92b201SChao Yu 
4996d92b201SChao Yu static const struct f2fs_compress_ops f2fs_lzorle_ops = {
5006d92b201SChao Yu 	.init_compress_ctx	= lzo_init_compress_ctx,
5016d92b201SChao Yu 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
5026d92b201SChao Yu 	.compress_pages		= lzorle_compress_pages,
5036d92b201SChao Yu 	.decompress_pages	= lzo_decompress_pages,
5046d92b201SChao Yu };
5056d92b201SChao Yu #endif
5066d92b201SChao Yu #endif
5076d92b201SChao Yu 
5084c8ff709SChao Yu static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
5094c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO
5104c8ff709SChao Yu 	&f2fs_lzo_ops,
5114c8ff709SChao Yu #else
5124c8ff709SChao Yu 	NULL,
5134c8ff709SChao Yu #endif
5144c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4
5154c8ff709SChao Yu 	&f2fs_lz4_ops,
5164c8ff709SChao Yu #else
5174c8ff709SChao Yu 	NULL,
5184c8ff709SChao Yu #endif
51950cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD
52050cfa66fSChao Yu 	&f2fs_zstd_ops,
52150cfa66fSChao Yu #else
52250cfa66fSChao Yu 	NULL,
52350cfa66fSChao Yu #endif
5246d92b201SChao Yu #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
5256d92b201SChao Yu 	&f2fs_lzorle_ops,
5266d92b201SChao Yu #else
5276d92b201SChao Yu 	NULL,
5286d92b201SChao Yu #endif
5294c8ff709SChao Yu };
5304c8ff709SChao Yu 
5314c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode)
5324c8ff709SChao Yu {
5334c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
5344c8ff709SChao Yu 		return true;
5354c8ff709SChao Yu 	return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
5364c8ff709SChao Yu }
5374c8ff709SChao Yu 
53899bbe307SJaegeuk Kim static mempool_t *compress_page_pool;
5395e6bbde9SChao Yu static int num_compress_pages = 512;
5405e6bbde9SChao Yu module_param(num_compress_pages, uint, 0444);
5415e6bbde9SChao Yu MODULE_PARM_DESC(num_compress_pages,
5425e6bbde9SChao Yu 		"Number of intermediate compress pages to preallocate");
5435e6bbde9SChao Yu 
5445e6bbde9SChao Yu int f2fs_init_compress_mempool(void)
5455e6bbde9SChao Yu {
5465e6bbde9SChao Yu 	compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
5475e6bbde9SChao Yu 	if (!compress_page_pool)
5485e6bbde9SChao Yu 		return -ENOMEM;
5495e6bbde9SChao Yu 
5505e6bbde9SChao Yu 	return 0;
5515e6bbde9SChao Yu }
5525e6bbde9SChao Yu 
5535e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void)
5545e6bbde9SChao Yu {
5555e6bbde9SChao Yu 	mempool_destroy(compress_page_pool);
5565e6bbde9SChao Yu }
5575e6bbde9SChao Yu 
5585e6bbde9SChao Yu static struct page *f2fs_compress_alloc_page(void)
5594c8ff709SChao Yu {
5604c8ff709SChao Yu 	struct page *page;
5614c8ff709SChao Yu 
5625e6bbde9SChao Yu 	page = mempool_alloc(compress_page_pool, GFP_NOFS);
5634c8ff709SChao Yu 	lock_page(page);
5645e6bbde9SChao Yu 
5654c8ff709SChao Yu 	return page;
5664c8ff709SChao Yu }
5674c8ff709SChao Yu 
5685e6bbde9SChao Yu static void f2fs_compress_free_page(struct page *page)
5695e6bbde9SChao Yu {
5705e6bbde9SChao Yu 	if (!page)
5715e6bbde9SChao Yu 		return;
5725e6bbde9SChao Yu 	set_page_private(page, (unsigned long)NULL);
5735e6bbde9SChao Yu 	ClearPagePrivate(page);
5745e6bbde9SChao Yu 	page->mapping = NULL;
5755e6bbde9SChao Yu 	unlock_page(page);
5765e6bbde9SChao Yu 	mempool_free(page, compress_page_pool);
5775e6bbde9SChao Yu }
5785e6bbde9SChao Yu 
5796fcaebacSDaeho Jeong #define MAX_VMAP_RETRIES	3
5806fcaebacSDaeho Jeong 
5816fcaebacSDaeho Jeong static void *f2fs_vmap(struct page **pages, unsigned int count)
5826fcaebacSDaeho Jeong {
5836fcaebacSDaeho Jeong 	int i;
5846fcaebacSDaeho Jeong 	void *buf = NULL;
5856fcaebacSDaeho Jeong 
5866fcaebacSDaeho Jeong 	for (i = 0; i < MAX_VMAP_RETRIES; i++) {
5876fcaebacSDaeho Jeong 		buf = vm_map_ram(pages, count, -1);
5886fcaebacSDaeho Jeong 		if (buf)
5896fcaebacSDaeho Jeong 			break;
5906fcaebacSDaeho Jeong 		vm_unmap_aliases();
5916fcaebacSDaeho Jeong 	}
5926fcaebacSDaeho Jeong 	return buf;
5936fcaebacSDaeho Jeong }
5946fcaebacSDaeho Jeong 
5954c8ff709SChao Yu static int f2fs_compress_pages(struct compress_ctx *cc)
5964c8ff709SChao Yu {
5974c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
5984c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
5994c8ff709SChao Yu 				f2fs_cops[fi->i_compress_algorithm];
600*31083031SChao Yu 	unsigned int max_len, new_nr_cpages;
601*31083031SChao Yu 	struct page **new_cpages;
6024c8ff709SChao Yu 	int i, ret;
6034c8ff709SChao Yu 
6044c8ff709SChao Yu 	trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
6054c8ff709SChao Yu 				cc->cluster_size, fi->i_compress_algorithm);
6064c8ff709SChao Yu 
60723b1faaaSChao Yu 	if (cops->init_compress_ctx) {
6084c8ff709SChao Yu 		ret = cops->init_compress_ctx(cc);
6094c8ff709SChao Yu 		if (ret)
6104c8ff709SChao Yu 			goto out;
61123b1faaaSChao Yu 	}
6124c8ff709SChao Yu 
6134c8ff709SChao Yu 	max_len = COMPRESS_HEADER_SIZE + cc->clen;
6144c8ff709SChao Yu 	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
6154c8ff709SChao Yu 
616*31083031SChao Yu 	cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
6174c8ff709SChao Yu 	if (!cc->cpages) {
6184c8ff709SChao Yu 		ret = -ENOMEM;
6194c8ff709SChao Yu 		goto destroy_compress_ctx;
6204c8ff709SChao Yu 	}
6214c8ff709SChao Yu 
6224c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
6235e6bbde9SChao Yu 		cc->cpages[i] = f2fs_compress_alloc_page();
6244c8ff709SChao Yu 		if (!cc->cpages[i]) {
6254c8ff709SChao Yu 			ret = -ENOMEM;
6264c8ff709SChao Yu 			goto out_free_cpages;
6274c8ff709SChao Yu 		}
6284c8ff709SChao Yu 	}
6294c8ff709SChao Yu 
6306fcaebacSDaeho Jeong 	cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
6314c8ff709SChao Yu 	if (!cc->rbuf) {
6324c8ff709SChao Yu 		ret = -ENOMEM;
6334c8ff709SChao Yu 		goto out_free_cpages;
6344c8ff709SChao Yu 	}
6354c8ff709SChao Yu 
6366fcaebacSDaeho Jeong 	cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
6374c8ff709SChao Yu 	if (!cc->cbuf) {
6384c8ff709SChao Yu 		ret = -ENOMEM;
6394c8ff709SChao Yu 		goto out_vunmap_rbuf;
6404c8ff709SChao Yu 	}
6414c8ff709SChao Yu 
6424c8ff709SChao Yu 	ret = cops->compress_pages(cc);
6434c8ff709SChao Yu 	if (ret)
6444c8ff709SChao Yu 		goto out_vunmap_cbuf;
6454c8ff709SChao Yu 
6464c8ff709SChao Yu 	max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
6474c8ff709SChao Yu 
6484c8ff709SChao Yu 	if (cc->clen > max_len) {
6494c8ff709SChao Yu 		ret = -EAGAIN;
6504c8ff709SChao Yu 		goto out_vunmap_cbuf;
6514c8ff709SChao Yu 	}
6524c8ff709SChao Yu 
6534c8ff709SChao Yu 	cc->cbuf->clen = cpu_to_le32(cc->clen);
6544c8ff709SChao Yu 
6554c8ff709SChao Yu 	for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
6564c8ff709SChao Yu 		cc->cbuf->reserved[i] = cpu_to_le32(0);
6574c8ff709SChao Yu 
658*31083031SChao Yu 	new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
659*31083031SChao Yu 
660*31083031SChao Yu 	/* Now we're going to cut unnecessary tail pages */
661*31083031SChao Yu 	new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
662*31083031SChao Yu 	if (!new_cpages) {
663*31083031SChao Yu 		ret = -ENOMEM;
664*31083031SChao Yu 		goto out_vunmap_cbuf;
665*31083031SChao Yu 	}
6667fa6d598SEric Biggers 
6677fa6d598SEric Biggers 	/* zero out any unused part of the last page */
6687fa6d598SEric Biggers 	memset(&cc->cbuf->cdata[cc->clen], 0,
669*31083031SChao Yu 			(new_nr_cpages * PAGE_SIZE) -
670*31083031SChao Yu 			(cc->clen + COMPRESS_HEADER_SIZE));
6717fa6d598SEric Biggers 
6726fcaebacSDaeho Jeong 	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
6736fcaebacSDaeho Jeong 	vm_unmap_ram(cc->rbuf, cc->cluster_size);
6744c8ff709SChao Yu 
675*31083031SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
676*31083031SChao Yu 		if (i < new_nr_cpages) {
677*31083031SChao Yu 			new_cpages[i] = cc->cpages[i];
678*31083031SChao Yu 			continue;
679*31083031SChao Yu 		}
6805e6bbde9SChao Yu 		f2fs_compress_free_page(cc->cpages[i]);
6814c8ff709SChao Yu 		cc->cpages[i] = NULL;
6824c8ff709SChao Yu 	}
6834c8ff709SChao Yu 
68423b1faaaSChao Yu 	if (cops->destroy_compress_ctx)
68509ff4801SChao Yu 		cops->destroy_compress_ctx(cc);
68609ff4801SChao Yu 
687*31083031SChao Yu 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
688*31083031SChao Yu 	cc->cpages = new_cpages;
689*31083031SChao Yu 	cc->nr_cpages = new_nr_cpages;
6904c8ff709SChao Yu 
6914c8ff709SChao Yu 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
6924c8ff709SChao Yu 							cc->clen, ret);
6934c8ff709SChao Yu 	return 0;
6944c8ff709SChao Yu 
6954c8ff709SChao Yu out_vunmap_cbuf:
6966fcaebacSDaeho Jeong 	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
6974c8ff709SChao Yu out_vunmap_rbuf:
6986fcaebacSDaeho Jeong 	vm_unmap_ram(cc->rbuf, cc->cluster_size);
6994c8ff709SChao Yu out_free_cpages:
7004c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
7014c8ff709SChao Yu 		if (cc->cpages[i])
7025e6bbde9SChao Yu 			f2fs_compress_free_page(cc->cpages[i]);
7034c8ff709SChao Yu 	}
704*31083031SChao Yu 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
7054c8ff709SChao Yu 	cc->cpages = NULL;
7064c8ff709SChao Yu destroy_compress_ctx:
70723b1faaaSChao Yu 	if (cops->destroy_compress_ctx)
7084c8ff709SChao Yu 		cops->destroy_compress_ctx(cc);
7094c8ff709SChao Yu out:
7104c8ff709SChao Yu 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
7114c8ff709SChao Yu 							cc->clen, ret);
7124c8ff709SChao Yu 	return ret;
7134c8ff709SChao Yu }
7144c8ff709SChao Yu 
7154c8ff709SChao Yu void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity)
7164c8ff709SChao Yu {
7174c8ff709SChao Yu 	struct decompress_io_ctx *dic =
7184c8ff709SChao Yu 			(struct decompress_io_ctx *)page_private(page);
7194c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
7204c8ff709SChao Yu 	struct f2fs_inode_info *fi= F2FS_I(dic->inode);
7214c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
7224c8ff709SChao Yu 			f2fs_cops[fi->i_compress_algorithm];
7234c8ff709SChao Yu 	int ret;
724b2f57a8eSChao Yu 	int i;
7254c8ff709SChao Yu 
7264c8ff709SChao Yu 	dec_page_count(sbi, F2FS_RD_DATA);
7274c8ff709SChao Yu 
7284c8ff709SChao Yu 	if (bio->bi_status || PageError(page))
7294c8ff709SChao Yu 		dic->failed = true;
7304c8ff709SChao Yu 
731e6c3948dSChao Yu 	if (atomic_dec_return(&dic->pending_pages))
7324c8ff709SChao Yu 		return;
7334c8ff709SChao Yu 
7344c8ff709SChao Yu 	trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
7354c8ff709SChao Yu 				dic->cluster_size, fi->i_compress_algorithm);
7364c8ff709SChao Yu 
7374c8ff709SChao Yu 	/* submit partial compressed pages */
7384c8ff709SChao Yu 	if (dic->failed) {
7394c8ff709SChao Yu 		ret = -EIO;
7404c8ff709SChao Yu 		goto out_free_dic;
7414c8ff709SChao Yu 	}
7424c8ff709SChao Yu 
743*31083031SChao Yu 	dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
744b2f57a8eSChao Yu 	if (!dic->tpages) {
745b2f57a8eSChao Yu 		ret = -ENOMEM;
746b2f57a8eSChao Yu 		goto out_free_dic;
747b2f57a8eSChao Yu 	}
748b2f57a8eSChao Yu 
749b2f57a8eSChao Yu 	for (i = 0; i < dic->cluster_size; i++) {
750b2f57a8eSChao Yu 		if (dic->rpages[i]) {
751b2f57a8eSChao Yu 			dic->tpages[i] = dic->rpages[i];
752b2f57a8eSChao Yu 			continue;
753b2f57a8eSChao Yu 		}
754b2f57a8eSChao Yu 
755b2f57a8eSChao Yu 		dic->tpages[i] = f2fs_compress_alloc_page();
756b2f57a8eSChao Yu 		if (!dic->tpages[i]) {
757b2f57a8eSChao Yu 			ret = -ENOMEM;
758b2f57a8eSChao Yu 			goto out_free_dic;
759b2f57a8eSChao Yu 		}
760b2f57a8eSChao Yu 	}
761b2f57a8eSChao Yu 
76223b1faaaSChao Yu 	if (cops->init_decompress_ctx) {
76323b1faaaSChao Yu 		ret = cops->init_decompress_ctx(dic);
76423b1faaaSChao Yu 		if (ret)
76523b1faaaSChao Yu 			goto out_free_dic;
76623b1faaaSChao Yu 	}
76723b1faaaSChao Yu 
7686fcaebacSDaeho Jeong 	dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
7694c8ff709SChao Yu 	if (!dic->rbuf) {
7704c8ff709SChao Yu 		ret = -ENOMEM;
77123b1faaaSChao Yu 		goto destroy_decompress_ctx;
7724c8ff709SChao Yu 	}
7734c8ff709SChao Yu 
7746fcaebacSDaeho Jeong 	dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
7754c8ff709SChao Yu 	if (!dic->cbuf) {
7764c8ff709SChao Yu 		ret = -ENOMEM;
7774c8ff709SChao Yu 		goto out_vunmap_rbuf;
7784c8ff709SChao Yu 	}
7794c8ff709SChao Yu 
7804c8ff709SChao Yu 	dic->clen = le32_to_cpu(dic->cbuf->clen);
7814c8ff709SChao Yu 	dic->rlen = PAGE_SIZE << dic->log_cluster_size;
7824c8ff709SChao Yu 
7834c8ff709SChao Yu 	if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
7844c8ff709SChao Yu 		ret = -EFSCORRUPTED;
7854c8ff709SChao Yu 		goto out_vunmap_cbuf;
7864c8ff709SChao Yu 	}
7874c8ff709SChao Yu 
7884c8ff709SChao Yu 	ret = cops->decompress_pages(dic);
7894c8ff709SChao Yu 
7904c8ff709SChao Yu out_vunmap_cbuf:
7916fcaebacSDaeho Jeong 	vm_unmap_ram(dic->cbuf, dic->nr_cpages);
7924c8ff709SChao Yu out_vunmap_rbuf:
7936fcaebacSDaeho Jeong 	vm_unmap_ram(dic->rbuf, dic->cluster_size);
79423b1faaaSChao Yu destroy_decompress_ctx:
79523b1faaaSChao Yu 	if (cops->destroy_decompress_ctx)
79623b1faaaSChao Yu 		cops->destroy_decompress_ctx(dic);
7974c8ff709SChao Yu out_free_dic:
79879bbefb1SChao Yu 	if (verity)
799e6c3948dSChao Yu 		atomic_set(&dic->pending_pages, dic->nr_cpages);
8004c8ff709SChao Yu 	if (!verity)
8014c8ff709SChao Yu 		f2fs_decompress_end_io(dic->rpages, dic->cluster_size,
8024c8ff709SChao Yu 								ret, false);
8034c8ff709SChao Yu 
8044c8ff709SChao Yu 	trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
8054c8ff709SChao Yu 							dic->clen, ret);
8064c8ff709SChao Yu 	if (!verity)
8074c8ff709SChao Yu 		f2fs_free_dic(dic);
8084c8ff709SChao Yu }
8094c8ff709SChao Yu 
8104c8ff709SChao Yu static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
8114c8ff709SChao Yu {
8124c8ff709SChao Yu 	if (cc->cluster_idx == NULL_CLUSTER)
8134c8ff709SChao Yu 		return true;
8144c8ff709SChao Yu 	return cc->cluster_idx == cluster_idx(cc, index);
8154c8ff709SChao Yu }
8164c8ff709SChao Yu 
8174c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc)
8184c8ff709SChao Yu {
8194c8ff709SChao Yu 	return cc->nr_rpages == 0;
8204c8ff709SChao Yu }
8214c8ff709SChao Yu 
8224c8ff709SChao Yu static bool f2fs_cluster_is_full(struct compress_ctx *cc)
8234c8ff709SChao Yu {
8244c8ff709SChao Yu 	return cc->cluster_size == cc->nr_rpages;
8254c8ff709SChao Yu }
8264c8ff709SChao Yu 
8274c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
8284c8ff709SChao Yu {
8294c8ff709SChao Yu 	if (f2fs_cluster_is_empty(cc))
8304c8ff709SChao Yu 		return true;
8314c8ff709SChao Yu 	return is_page_in_cluster(cc, index);
8324c8ff709SChao Yu }
8334c8ff709SChao Yu 
8344c8ff709SChao Yu static bool __cluster_may_compress(struct compress_ctx *cc)
8354c8ff709SChao Yu {
8364c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
8374c8ff709SChao Yu 	loff_t i_size = i_size_read(cc->inode);
8384c8ff709SChao Yu 	unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
8394c8ff709SChao Yu 	int i;
8404c8ff709SChao Yu 
8414c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
8424c8ff709SChao Yu 		struct page *page = cc->rpages[i];
8434c8ff709SChao Yu 
8444c8ff709SChao Yu 		f2fs_bug_on(sbi, !page);
8454c8ff709SChao Yu 
8464c8ff709SChao Yu 		if (unlikely(f2fs_cp_error(sbi)))
8474c8ff709SChao Yu 			return false;
8484c8ff709SChao Yu 		if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
8494c8ff709SChao Yu 			return false;
8504c8ff709SChao Yu 
8514c8ff709SChao Yu 		/* beyond EOF */
8524c8ff709SChao Yu 		if (page->index >= nr_pages)
8534c8ff709SChao Yu 			return false;
8544c8ff709SChao Yu 	}
8554c8ff709SChao Yu 	return true;
8564c8ff709SChao Yu }
8574c8ff709SChao Yu 
8581a67cbe1SChao Yu static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
8594c8ff709SChao Yu {
8604c8ff709SChao Yu 	struct dnode_of_data dn;
8614c8ff709SChao Yu 	int ret;
8624c8ff709SChao Yu 
8634c8ff709SChao Yu 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
8644c8ff709SChao Yu 	ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc),
8654c8ff709SChao Yu 							LOOKUP_NODE);
8664c8ff709SChao Yu 	if (ret) {
8674c8ff709SChao Yu 		if (ret == -ENOENT)
8684c8ff709SChao Yu 			ret = 0;
8694c8ff709SChao Yu 		goto fail;
8704c8ff709SChao Yu 	}
8714c8ff709SChao Yu 
8724c8ff709SChao Yu 	if (dn.data_blkaddr == COMPRESS_ADDR) {
8734c8ff709SChao Yu 		int i;
8744c8ff709SChao Yu 
8754c8ff709SChao Yu 		ret = 1;
8764c8ff709SChao Yu 		for (i = 1; i < cc->cluster_size; i++) {
8774c8ff709SChao Yu 			block_t blkaddr;
8784c8ff709SChao Yu 
879a2ced1ceSChao Yu 			blkaddr = data_blkaddr(dn.inode,
8804c8ff709SChao Yu 					dn.node_page, dn.ofs_in_node + i);
8811a67cbe1SChao Yu 			if (compr) {
8821a67cbe1SChao Yu 				if (__is_valid_data_blkaddr(blkaddr))
8831a67cbe1SChao Yu 					ret++;
8841a67cbe1SChao Yu 			} else {
8854c8ff709SChao Yu 				if (blkaddr != NULL_ADDR)
8864c8ff709SChao Yu 					ret++;
8874c8ff709SChao Yu 			}
8884c8ff709SChao Yu 		}
8891a67cbe1SChao Yu 	}
8904c8ff709SChao Yu fail:
8914c8ff709SChao Yu 	f2fs_put_dnode(&dn);
8924c8ff709SChao Yu 	return ret;
8934c8ff709SChao Yu }
8944c8ff709SChao Yu 
8951a67cbe1SChao Yu /* return # of compressed blocks in compressed cluster */
8961a67cbe1SChao Yu static int f2fs_compressed_blocks(struct compress_ctx *cc)
8971a67cbe1SChao Yu {
8981a67cbe1SChao Yu 	return __f2fs_cluster_blocks(cc, true);
8991a67cbe1SChao Yu }
9001a67cbe1SChao Yu 
9011a67cbe1SChao Yu /* return # of valid blocks in compressed cluster */
902d078319dSWang Xiaojun static int f2fs_cluster_blocks(struct compress_ctx *cc)
9031a67cbe1SChao Yu {
9041a67cbe1SChao Yu 	return __f2fs_cluster_blocks(cc, false);
9051a67cbe1SChao Yu }
9061a67cbe1SChao Yu 
9074c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
9084c8ff709SChao Yu {
9094c8ff709SChao Yu 	struct compress_ctx cc = {
9104c8ff709SChao Yu 		.inode = inode,
9114c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
9124c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
9134c8ff709SChao Yu 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
9144c8ff709SChao Yu 	};
9154c8ff709SChao Yu 
916d078319dSWang Xiaojun 	return f2fs_cluster_blocks(&cc);
9174c8ff709SChao Yu }
9184c8ff709SChao Yu 
9194c8ff709SChao Yu static bool cluster_may_compress(struct compress_ctx *cc)
9204c8ff709SChao Yu {
9214c8ff709SChao Yu 	if (!f2fs_compressed_file(cc->inode))
9224c8ff709SChao Yu 		return false;
9234c8ff709SChao Yu 	if (f2fs_is_atomic_file(cc->inode))
9244c8ff709SChao Yu 		return false;
9254c8ff709SChao Yu 	if (f2fs_is_mmap_file(cc->inode))
9264c8ff709SChao Yu 		return false;
9274c8ff709SChao Yu 	if (!f2fs_cluster_is_full(cc))
9284c8ff709SChao Yu 		return false;
929dc35d73aSChao Yu 	if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
930dc35d73aSChao Yu 		return false;
9314c8ff709SChao Yu 	return __cluster_may_compress(cc);
9324c8ff709SChao Yu }
9334c8ff709SChao Yu 
9344c8ff709SChao Yu static void set_cluster_writeback(struct compress_ctx *cc)
9354c8ff709SChao Yu {
9364c8ff709SChao Yu 	int i;
9374c8ff709SChao Yu 
9384c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
9394c8ff709SChao Yu 		if (cc->rpages[i])
9404c8ff709SChao Yu 			set_page_writeback(cc->rpages[i]);
9414c8ff709SChao Yu 	}
9424c8ff709SChao Yu }
9434c8ff709SChao Yu 
9444c8ff709SChao Yu static void set_cluster_dirty(struct compress_ctx *cc)
9454c8ff709SChao Yu {
9464c8ff709SChao Yu 	int i;
9474c8ff709SChao Yu 
9484c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++)
9494c8ff709SChao Yu 		if (cc->rpages[i])
9504c8ff709SChao Yu 			set_page_dirty(cc->rpages[i]);
9514c8ff709SChao Yu }
9524c8ff709SChao Yu 
9534c8ff709SChao Yu static int prepare_compress_overwrite(struct compress_ctx *cc,
9544c8ff709SChao Yu 		struct page **pagep, pgoff_t index, void **fsdata)
9554c8ff709SChao Yu {
9564c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
9574c8ff709SChao Yu 	struct address_space *mapping = cc->inode->i_mapping;
9584c8ff709SChao Yu 	struct page *page;
9594c8ff709SChao Yu 	struct dnode_of_data dn;
9604c8ff709SChao Yu 	sector_t last_block_in_bio;
9614c8ff709SChao Yu 	unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
9624c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
9634c8ff709SChao Yu 	int i, ret;
9644c8ff709SChao Yu 	bool prealloc;
9654c8ff709SChao Yu 
9664c8ff709SChao Yu retry:
967d078319dSWang Xiaojun 	ret = f2fs_cluster_blocks(cc);
9684c8ff709SChao Yu 	if (ret <= 0)
9694c8ff709SChao Yu 		return ret;
9704c8ff709SChao Yu 
9714c8ff709SChao Yu 	/* compressed case */
9724c8ff709SChao Yu 	prealloc = (ret < cc->cluster_size);
9734c8ff709SChao Yu 
9744c8ff709SChao Yu 	ret = f2fs_init_compress_ctx(cc);
9754c8ff709SChao Yu 	if (ret)
9764c8ff709SChao Yu 		return ret;
9774c8ff709SChao Yu 
9784c8ff709SChao Yu 	/* keep page reference to avoid page reclaim */
9794c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
9804c8ff709SChao Yu 		page = f2fs_pagecache_get_page(mapping, start_idx + i,
9814c8ff709SChao Yu 							fgp_flag, GFP_NOFS);
9824c8ff709SChao Yu 		if (!page) {
9834c8ff709SChao Yu 			ret = -ENOMEM;
9844c8ff709SChao Yu 			goto unlock_pages;
9854c8ff709SChao Yu 		}
9864c8ff709SChao Yu 
9874c8ff709SChao Yu 		if (PageUptodate(page))
9884c8ff709SChao Yu 			unlock_page(page);
9894c8ff709SChao Yu 		else
9904c8ff709SChao Yu 			f2fs_compress_ctx_add_page(cc, page);
9914c8ff709SChao Yu 	}
9924c8ff709SChao Yu 
9934c8ff709SChao Yu 	if (!f2fs_cluster_is_empty(cc)) {
9944c8ff709SChao Yu 		struct bio *bio = NULL;
9954c8ff709SChao Yu 
9964c8ff709SChao Yu 		ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
9970683728aSChao Yu 					&last_block_in_bio, false, true);
9984c8ff709SChao Yu 		f2fs_destroy_compress_ctx(cc);
9994c8ff709SChao Yu 		if (ret)
10004c8ff709SChao Yu 			goto release_pages;
10014c8ff709SChao Yu 		if (bio)
10024c8ff709SChao Yu 			f2fs_submit_bio(sbi, bio, DATA);
10034c8ff709SChao Yu 
10044c8ff709SChao Yu 		ret = f2fs_init_compress_ctx(cc);
10054c8ff709SChao Yu 		if (ret)
10064c8ff709SChao Yu 			goto release_pages;
10074c8ff709SChao Yu 	}
10084c8ff709SChao Yu 
10094c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
10104c8ff709SChao Yu 		f2fs_bug_on(sbi, cc->rpages[i]);
10114c8ff709SChao Yu 
10124c8ff709SChao Yu 		page = find_lock_page(mapping, start_idx + i);
10134c8ff709SChao Yu 		f2fs_bug_on(sbi, !page);
10144c8ff709SChao Yu 
10154c8ff709SChao Yu 		f2fs_wait_on_page_writeback(page, DATA, true, true);
10164c8ff709SChao Yu 
10174c8ff709SChao Yu 		f2fs_compress_ctx_add_page(cc, page);
10184c8ff709SChao Yu 		f2fs_put_page(page, 0);
10194c8ff709SChao Yu 
10204c8ff709SChao Yu 		if (!PageUptodate(page)) {
10214c8ff709SChao Yu 			f2fs_unlock_rpages(cc, i + 1);
1022bc67c5d0SChao Yu 			f2fs_put_rpages_mapping(mapping, start_idx,
10234c8ff709SChao Yu 					cc->cluster_size);
10244c8ff709SChao Yu 			f2fs_destroy_compress_ctx(cc);
10254c8ff709SChao Yu 			goto retry;
10264c8ff709SChao Yu 		}
10274c8ff709SChao Yu 	}
10284c8ff709SChao Yu 
10294c8ff709SChao Yu 	if (prealloc) {
10300ef81833SChao Yu 		f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
10314c8ff709SChao Yu 
10324c8ff709SChao Yu 		set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
10334c8ff709SChao Yu 
10344c8ff709SChao Yu 		for (i = cc->cluster_size - 1; i > 0; i--) {
10354c8ff709SChao Yu 			ret = f2fs_get_block(&dn, start_idx + i);
10364c8ff709SChao Yu 			if (ret) {
10374c8ff709SChao Yu 				i = cc->cluster_size;
10384c8ff709SChao Yu 				break;
10394c8ff709SChao Yu 			}
10404c8ff709SChao Yu 
10414c8ff709SChao Yu 			if (dn.data_blkaddr != NEW_ADDR)
10424c8ff709SChao Yu 				break;
10434c8ff709SChao Yu 		}
10444c8ff709SChao Yu 
10450ef81833SChao Yu 		f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
10464c8ff709SChao Yu 	}
10474c8ff709SChao Yu 
10484c8ff709SChao Yu 	if (likely(!ret)) {
10494c8ff709SChao Yu 		*fsdata = cc->rpages;
10504c8ff709SChao Yu 		*pagep = cc->rpages[offset_in_cluster(cc, index)];
10514c8ff709SChao Yu 		return cc->cluster_size;
10524c8ff709SChao Yu 	}
10534c8ff709SChao Yu 
10544c8ff709SChao Yu unlock_pages:
10554c8ff709SChao Yu 	f2fs_unlock_rpages(cc, i);
10564c8ff709SChao Yu release_pages:
1057bc67c5d0SChao Yu 	f2fs_put_rpages_mapping(mapping, start_idx, i);
10584c8ff709SChao Yu 	f2fs_destroy_compress_ctx(cc);
10594c8ff709SChao Yu 	return ret;
10604c8ff709SChao Yu }
10614c8ff709SChao Yu 
10624c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
10634c8ff709SChao Yu 		struct page **pagep, pgoff_t index, void **fsdata)
10644c8ff709SChao Yu {
10654c8ff709SChao Yu 	struct compress_ctx cc = {
10664c8ff709SChao Yu 		.inode = inode,
10674c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
10684c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
10694c8ff709SChao Yu 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
10704c8ff709SChao Yu 		.rpages = NULL,
10714c8ff709SChao Yu 		.nr_rpages = 0,
10724c8ff709SChao Yu 	};
10734c8ff709SChao Yu 
10744c8ff709SChao Yu 	return prepare_compress_overwrite(&cc, pagep, index, fsdata);
10754c8ff709SChao Yu }
10764c8ff709SChao Yu 
10774c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
10784c8ff709SChao Yu 					pgoff_t index, unsigned copied)
10794c8ff709SChao Yu 
10804c8ff709SChao Yu {
10814c8ff709SChao Yu 	struct compress_ctx cc = {
1082*31083031SChao Yu 		.inode = inode,
10834c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
10844c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
10854c8ff709SChao Yu 		.rpages = fsdata,
10864c8ff709SChao Yu 	};
10874c8ff709SChao Yu 	bool first_index = (index == cc.rpages[0]->index);
10884c8ff709SChao Yu 
10894c8ff709SChao Yu 	if (copied)
10904c8ff709SChao Yu 		set_cluster_dirty(&cc);
10914c8ff709SChao Yu 
10924c8ff709SChao Yu 	f2fs_put_rpages_wbc(&cc, NULL, false, 1);
10934c8ff709SChao Yu 	f2fs_destroy_compress_ctx(&cc);
10944c8ff709SChao Yu 
10954c8ff709SChao Yu 	return first_index;
10964c8ff709SChao Yu }
10974c8ff709SChao Yu 
10983265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
10993265d3dbSChao Yu {
11003265d3dbSChao Yu 	void *fsdata = NULL;
11013265d3dbSChao Yu 	struct page *pagep;
11023265d3dbSChao Yu 	int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
11033265d3dbSChao Yu 	pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
11043265d3dbSChao Yu 							log_cluster_size;
11053265d3dbSChao Yu 	int err;
11063265d3dbSChao Yu 
11073265d3dbSChao Yu 	err = f2fs_is_compressed_cluster(inode, start_idx);
11083265d3dbSChao Yu 	if (err < 0)
11093265d3dbSChao Yu 		return err;
11103265d3dbSChao Yu 
11113265d3dbSChao Yu 	/* truncate normal cluster */
11123265d3dbSChao Yu 	if (!err)
11133265d3dbSChao Yu 		return f2fs_do_truncate_blocks(inode, from, lock);
11143265d3dbSChao Yu 
11153265d3dbSChao Yu 	/* truncate compressed cluster */
11163265d3dbSChao Yu 	err = f2fs_prepare_compress_overwrite(inode, &pagep,
11173265d3dbSChao Yu 						start_idx, &fsdata);
11183265d3dbSChao Yu 
11193265d3dbSChao Yu 	/* should not be a normal cluster */
11203265d3dbSChao Yu 	f2fs_bug_on(F2FS_I_SB(inode), err == 0);
11213265d3dbSChao Yu 
11223265d3dbSChao Yu 	if (err <= 0)
11233265d3dbSChao Yu 		return err;
11243265d3dbSChao Yu 
11253265d3dbSChao Yu 	if (err > 0) {
11263265d3dbSChao Yu 		struct page **rpages = fsdata;
11273265d3dbSChao Yu 		int cluster_size = F2FS_I(inode)->i_cluster_size;
11283265d3dbSChao Yu 		int i;
11293265d3dbSChao Yu 
11303265d3dbSChao Yu 		for (i = cluster_size - 1; i >= 0; i--) {
11313265d3dbSChao Yu 			loff_t start = rpages[i]->index << PAGE_SHIFT;
11323265d3dbSChao Yu 
11333265d3dbSChao Yu 			if (from <= start) {
11343265d3dbSChao Yu 				zero_user_segment(rpages[i], 0, PAGE_SIZE);
11353265d3dbSChao Yu 			} else {
11363265d3dbSChao Yu 				zero_user_segment(rpages[i], from - start,
11373265d3dbSChao Yu 								PAGE_SIZE);
11383265d3dbSChao Yu 				break;
11393265d3dbSChao Yu 			}
11403265d3dbSChao Yu 		}
11413265d3dbSChao Yu 
11423265d3dbSChao Yu 		f2fs_compress_write_end(inode, fsdata, start_idx, true);
11433265d3dbSChao Yu 	}
11443265d3dbSChao Yu 	return 0;
11453265d3dbSChao Yu }
11463265d3dbSChao Yu 
11474c8ff709SChao Yu static int f2fs_write_compressed_pages(struct compress_ctx *cc,
11484c8ff709SChao Yu 					int *submitted,
11494c8ff709SChao Yu 					struct writeback_control *wbc,
11504c8ff709SChao Yu 					enum iostat_type io_type)
11514c8ff709SChao Yu {
11524c8ff709SChao Yu 	struct inode *inode = cc->inode;
11534c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
11544c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
11554c8ff709SChao Yu 	struct f2fs_io_info fio = {
11564c8ff709SChao Yu 		.sbi = sbi,
11574c8ff709SChao Yu 		.ino = cc->inode->i_ino,
11584c8ff709SChao Yu 		.type = DATA,
11594c8ff709SChao Yu 		.op = REQ_OP_WRITE,
11604c8ff709SChao Yu 		.op_flags = wbc_to_write_flags(wbc),
11614c8ff709SChao Yu 		.old_blkaddr = NEW_ADDR,
11624c8ff709SChao Yu 		.page = NULL,
11634c8ff709SChao Yu 		.encrypted_page = NULL,
11644c8ff709SChao Yu 		.compressed_page = NULL,
11654c8ff709SChao Yu 		.submitted = false,
11664c8ff709SChao Yu 		.io_type = io_type,
11674c8ff709SChao Yu 		.io_wbc = wbc,
116827aacd28SSatya Tangirala 		.encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode),
11694c8ff709SChao Yu 	};
11704c8ff709SChao Yu 	struct dnode_of_data dn;
11714c8ff709SChao Yu 	struct node_info ni;
11724c8ff709SChao Yu 	struct compress_io_ctx *cic;
11734c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
11744c8ff709SChao Yu 	unsigned int last_index = cc->cluster_size - 1;
11754c8ff709SChao Yu 	loff_t psize;
11764c8ff709SChao Yu 	int i, err;
11774c8ff709SChao Yu 
117879963d96SChao Yu 	if (IS_NOQUOTA(inode)) {
117979963d96SChao Yu 		/*
118079963d96SChao Yu 		 * We need to wait for node_write to avoid block allocation during
118179963d96SChao Yu 		 * checkpoint. This can only happen to quota writes which can cause
118279963d96SChao Yu 		 * the below discard race condition.
118379963d96SChao Yu 		 */
118479963d96SChao Yu 		down_read(&sbi->node_write);
118579963d96SChao Yu 	} else if (!f2fs_trylock_op(sbi)) {
1186*31083031SChao Yu 		goto out_free;
118779963d96SChao Yu 	}
11884c8ff709SChao Yu 
1189df77fbd8SChao Yu 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
11904c8ff709SChao Yu 
11914c8ff709SChao Yu 	err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
11924c8ff709SChao Yu 	if (err)
11934c8ff709SChao Yu 		goto out_unlock_op;
11944c8ff709SChao Yu 
11954c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
1196a2ced1ceSChao Yu 		if (data_blkaddr(dn.inode, dn.node_page,
11974c8ff709SChao Yu 					dn.ofs_in_node + i) == NULL_ADDR)
11984c8ff709SChao Yu 			goto out_put_dnode;
11994c8ff709SChao Yu 	}
12004c8ff709SChao Yu 
12014c8ff709SChao Yu 	psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
12024c8ff709SChao Yu 
12034c8ff709SChao Yu 	err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
12044c8ff709SChao Yu 	if (err)
12054c8ff709SChao Yu 		goto out_put_dnode;
12064c8ff709SChao Yu 
12074c8ff709SChao Yu 	fio.version = ni.version;
12084c8ff709SChao Yu 
12094c8ff709SChao Yu 	cic = f2fs_kzalloc(sbi, sizeof(struct compress_io_ctx), GFP_NOFS);
12104c8ff709SChao Yu 	if (!cic)
12114c8ff709SChao Yu 		goto out_put_dnode;
12124c8ff709SChao Yu 
12134c8ff709SChao Yu 	cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
12144c8ff709SChao Yu 	cic->inode = inode;
1215e6c3948dSChao Yu 	atomic_set(&cic->pending_pages, cc->nr_cpages);
1216*31083031SChao Yu 	cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
12174c8ff709SChao Yu 	if (!cic->rpages)
12184c8ff709SChao Yu 		goto out_put_cic;
12194c8ff709SChao Yu 
12204c8ff709SChao Yu 	cic->nr_rpages = cc->cluster_size;
12214c8ff709SChao Yu 
12224c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
12234c8ff709SChao Yu 		f2fs_set_compressed_page(cc->cpages[i], inode,
1224887347a0SChao Yu 					cc->rpages[i + 1]->index, cic);
12254c8ff709SChao Yu 		fio.compressed_page = cc->cpages[i];
1226f567adb0SChao Yu 
1227f567adb0SChao Yu 		fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1228f567adb0SChao Yu 						dn.ofs_in_node + i + 1);
1229f567adb0SChao Yu 
1230f567adb0SChao Yu 		/* wait for GCed page writeback via META_MAPPING */
1231f567adb0SChao Yu 		f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1232f567adb0SChao Yu 
12334c8ff709SChao Yu 		if (fio.encrypted) {
12344c8ff709SChao Yu 			fio.page = cc->rpages[i + 1];
12354c8ff709SChao Yu 			err = f2fs_encrypt_one_page(&fio);
12364c8ff709SChao Yu 			if (err)
12374c8ff709SChao Yu 				goto out_destroy_crypt;
12384c8ff709SChao Yu 			cc->cpages[i] = fio.encrypted_page;
12394c8ff709SChao Yu 		}
12404c8ff709SChao Yu 	}
12414c8ff709SChao Yu 
12424c8ff709SChao Yu 	set_cluster_writeback(cc);
12434c8ff709SChao Yu 
12444c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++)
12454c8ff709SChao Yu 		cic->rpages[i] = cc->rpages[i];
12464c8ff709SChao Yu 
12474c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
12484c8ff709SChao Yu 		block_t blkaddr;
12494c8ff709SChao Yu 
1250a2ced1ceSChao Yu 		blkaddr = f2fs_data_blkaddr(&dn);
125195978caaSChao Yu 		fio.page = cc->rpages[i];
12524c8ff709SChao Yu 		fio.old_blkaddr = blkaddr;
12534c8ff709SChao Yu 
12544c8ff709SChao Yu 		/* cluster header */
12554c8ff709SChao Yu 		if (i == 0) {
12564c8ff709SChao Yu 			if (blkaddr == COMPRESS_ADDR)
12574c8ff709SChao Yu 				fio.compr_blocks++;
12584c8ff709SChao Yu 			if (__is_valid_data_blkaddr(blkaddr))
12594c8ff709SChao Yu 				f2fs_invalidate_blocks(sbi, blkaddr);
12604c8ff709SChao Yu 			f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
12614c8ff709SChao Yu 			goto unlock_continue;
12624c8ff709SChao Yu 		}
12634c8ff709SChao Yu 
12644c8ff709SChao Yu 		if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
12654c8ff709SChao Yu 			fio.compr_blocks++;
12664c8ff709SChao Yu 
12674c8ff709SChao Yu 		if (i > cc->nr_cpages) {
12684c8ff709SChao Yu 			if (__is_valid_data_blkaddr(blkaddr)) {
12694c8ff709SChao Yu 				f2fs_invalidate_blocks(sbi, blkaddr);
12704c8ff709SChao Yu 				f2fs_update_data_blkaddr(&dn, NEW_ADDR);
12714c8ff709SChao Yu 			}
12724c8ff709SChao Yu 			goto unlock_continue;
12734c8ff709SChao Yu 		}
12744c8ff709SChao Yu 
12754c8ff709SChao Yu 		f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
12764c8ff709SChao Yu 
12774c8ff709SChao Yu 		if (fio.encrypted)
12784c8ff709SChao Yu 			fio.encrypted_page = cc->cpages[i - 1];
12794c8ff709SChao Yu 		else
12804c8ff709SChao Yu 			fio.compressed_page = cc->cpages[i - 1];
12814c8ff709SChao Yu 
12824c8ff709SChao Yu 		cc->cpages[i - 1] = NULL;
12834c8ff709SChao Yu 		f2fs_outplace_write_data(&dn, &fio);
12844c8ff709SChao Yu 		(*submitted)++;
12854c8ff709SChao Yu unlock_continue:
12864c8ff709SChao Yu 		inode_dec_dirty_pages(cc->inode);
12874c8ff709SChao Yu 		unlock_page(fio.page);
12884c8ff709SChao Yu 	}
12894c8ff709SChao Yu 
12904c8ff709SChao Yu 	if (fio.compr_blocks)
12914c8ff709SChao Yu 		f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
12924c8ff709SChao Yu 	f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
12934c8ff709SChao Yu 
12944c8ff709SChao Yu 	set_inode_flag(cc->inode, FI_APPEND_WRITE);
12954c8ff709SChao Yu 	if (cc->cluster_idx == 0)
12964c8ff709SChao Yu 		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
12974c8ff709SChao Yu 
12984c8ff709SChao Yu 	f2fs_put_dnode(&dn);
129979963d96SChao Yu 	if (IS_NOQUOTA(inode))
130079963d96SChao Yu 		up_read(&sbi->node_write);
130179963d96SChao Yu 	else
13024c8ff709SChao Yu 		f2fs_unlock_op(sbi);
13034c8ff709SChao Yu 
1304c10c9820SChao Yu 	spin_lock(&fi->i_size_lock);
13054c8ff709SChao Yu 	if (fi->last_disk_size < psize)
13064c8ff709SChao Yu 		fi->last_disk_size = psize;
1307c10c9820SChao Yu 	spin_unlock(&fi->i_size_lock);
13084c8ff709SChao Yu 
13094c8ff709SChao Yu 	f2fs_put_rpages(cc);
1310*31083031SChao Yu 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1311*31083031SChao Yu 	cc->cpages = NULL;
13124c8ff709SChao Yu 	f2fs_destroy_compress_ctx(cc);
13134c8ff709SChao Yu 	return 0;
13144c8ff709SChao Yu 
13154c8ff709SChao Yu out_destroy_crypt:
1316*31083031SChao Yu 	page_array_free(cc->inode, cic->rpages, cc->cluster_size);
13174c8ff709SChao Yu 
13184c8ff709SChao Yu 	for (--i; i >= 0; i--)
13194c8ff709SChao Yu 		fscrypt_finalize_bounce_page(&cc->cpages[i]);
13204c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
13214c8ff709SChao Yu 		if (!cc->cpages[i])
13224c8ff709SChao Yu 			continue;
13234c8ff709SChao Yu 		f2fs_put_page(cc->cpages[i], 1);
13244c8ff709SChao Yu 	}
13254c8ff709SChao Yu out_put_cic:
13264c8ff709SChao Yu 	kfree(cic);
13274c8ff709SChao Yu out_put_dnode:
13284c8ff709SChao Yu 	f2fs_put_dnode(&dn);
13294c8ff709SChao Yu out_unlock_op:
133079963d96SChao Yu 	if (IS_NOQUOTA(inode))
133179963d96SChao Yu 		up_read(&sbi->node_write);
133279963d96SChao Yu 	else
13334c8ff709SChao Yu 		f2fs_unlock_op(sbi);
1334*31083031SChao Yu out_free:
1335*31083031SChao Yu 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1336*31083031SChao Yu 	cc->cpages = NULL;
13374c8ff709SChao Yu 	return -EAGAIN;
13384c8ff709SChao Yu }
13394c8ff709SChao Yu 
13404c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
13414c8ff709SChao Yu {
13424c8ff709SChao Yu 	struct f2fs_sb_info *sbi = bio->bi_private;
13434c8ff709SChao Yu 	struct compress_io_ctx *cic =
13444c8ff709SChao Yu 			(struct compress_io_ctx *)page_private(page);
13454c8ff709SChao Yu 	int i;
13464c8ff709SChao Yu 
13474c8ff709SChao Yu 	if (unlikely(bio->bi_status))
13484c8ff709SChao Yu 		mapping_set_error(cic->inode->i_mapping, -EIO);
13494c8ff709SChao Yu 
13505e6bbde9SChao Yu 	f2fs_compress_free_page(page);
13514c8ff709SChao Yu 
13524c8ff709SChao Yu 	dec_page_count(sbi, F2FS_WB_DATA);
13534c8ff709SChao Yu 
1354e6c3948dSChao Yu 	if (atomic_dec_return(&cic->pending_pages))
13554c8ff709SChao Yu 		return;
13564c8ff709SChao Yu 
13574c8ff709SChao Yu 	for (i = 0; i < cic->nr_rpages; i++) {
13584c8ff709SChao Yu 		WARN_ON(!cic->rpages[i]);
13594c8ff709SChao Yu 		clear_cold_data(cic->rpages[i]);
13604c8ff709SChao Yu 		end_page_writeback(cic->rpages[i]);
13614c8ff709SChao Yu 	}
13624c8ff709SChao Yu 
1363*31083031SChao Yu 	page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
13644c8ff709SChao Yu 	kfree(cic);
13654c8ff709SChao Yu }
13664c8ff709SChao Yu 
13674c8ff709SChao Yu static int f2fs_write_raw_pages(struct compress_ctx *cc,
13684c8ff709SChao Yu 					int *submitted,
13694c8ff709SChao Yu 					struct writeback_control *wbc,
13704c8ff709SChao Yu 					enum iostat_type io_type)
13714c8ff709SChao Yu {
13724c8ff709SChao Yu 	struct address_space *mapping = cc->inode->i_mapping;
13734c8ff709SChao Yu 	int _submitted, compr_blocks, ret;
13744c8ff709SChao Yu 	int i = -1, err = 0;
13754c8ff709SChao Yu 
13764c8ff709SChao Yu 	compr_blocks = f2fs_compressed_blocks(cc);
13774c8ff709SChao Yu 	if (compr_blocks < 0) {
13784c8ff709SChao Yu 		err = compr_blocks;
13794c8ff709SChao Yu 		goto out_err;
13804c8ff709SChao Yu 	}
13814c8ff709SChao Yu 
13824c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
13834c8ff709SChao Yu 		if (!cc->rpages[i])
13844c8ff709SChao Yu 			continue;
13854c8ff709SChao Yu retry_write:
13864c8ff709SChao Yu 		if (cc->rpages[i]->mapping != mapping) {
13874c8ff709SChao Yu 			unlock_page(cc->rpages[i]);
13884c8ff709SChao Yu 			continue;
13894c8ff709SChao Yu 		}
13904c8ff709SChao Yu 
13914c8ff709SChao Yu 		BUG_ON(!PageLocked(cc->rpages[i]));
13924c8ff709SChao Yu 
13934c8ff709SChao Yu 		ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
13944c8ff709SChao Yu 						NULL, NULL, wbc, io_type,
13954c8ff709SChao Yu 						compr_blocks);
13964c8ff709SChao Yu 		if (ret) {
13974c8ff709SChao Yu 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
13984c8ff709SChao Yu 				unlock_page(cc->rpages[i]);
13994c8ff709SChao Yu 				ret = 0;
14004c8ff709SChao Yu 			} else if (ret == -EAGAIN) {
1401466357dcSChao Yu 				/*
1402466357dcSChao Yu 				 * for quota file, just redirty left pages to
1403466357dcSChao Yu 				 * avoid deadlock caused by cluster update race
1404466357dcSChao Yu 				 * from foreground operation.
1405466357dcSChao Yu 				 */
1406466357dcSChao Yu 				if (IS_NOQUOTA(cc->inode)) {
1407466357dcSChao Yu 					err = 0;
1408466357dcSChao Yu 					goto out_err;
1409466357dcSChao Yu 				}
14104c8ff709SChao Yu 				ret = 0;
14114c8ff709SChao Yu 				cond_resched();
14125df7731fSChao Yu 				congestion_wait(BLK_RW_ASYNC,
14135df7731fSChao Yu 						DEFAULT_IO_TIMEOUT);
14144c8ff709SChao Yu 				lock_page(cc->rpages[i]);
1415eb1353cfSChao Yu 
1416eb1353cfSChao Yu 				if (!PageDirty(cc->rpages[i])) {
1417eb1353cfSChao Yu 					unlock_page(cc->rpages[i]);
1418eb1353cfSChao Yu 					continue;
1419eb1353cfSChao Yu 				}
1420eb1353cfSChao Yu 
14214c8ff709SChao Yu 				clear_page_dirty_for_io(cc->rpages[i]);
14224c8ff709SChao Yu 				goto retry_write;
14234c8ff709SChao Yu 			}
14244c8ff709SChao Yu 			err = ret;
1425466357dcSChao Yu 			goto out_err;
14264c8ff709SChao Yu 		}
14274c8ff709SChao Yu 
14284c8ff709SChao Yu 		*submitted += _submitted;
14294c8ff709SChao Yu 	}
14304c8ff709SChao Yu 	return 0;
14314c8ff709SChao Yu out_err:
14324c8ff709SChao Yu 	for (++i; i < cc->cluster_size; i++) {
14334c8ff709SChao Yu 		if (!cc->rpages[i])
14344c8ff709SChao Yu 			continue;
14354c8ff709SChao Yu 		redirty_page_for_writepage(wbc, cc->rpages[i]);
14364c8ff709SChao Yu 		unlock_page(cc->rpages[i]);
14374c8ff709SChao Yu 	}
14384c8ff709SChao Yu 	return err;
14394c8ff709SChao Yu }
14404c8ff709SChao Yu 
14414c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
14424c8ff709SChao Yu 					int *submitted,
14434c8ff709SChao Yu 					struct writeback_control *wbc,
14444c8ff709SChao Yu 					enum iostat_type io_type)
14454c8ff709SChao Yu {
14464c8ff709SChao Yu 	int err;
14474c8ff709SChao Yu 
14484c8ff709SChao Yu 	*submitted = 0;
14494c8ff709SChao Yu 	if (cluster_may_compress(cc)) {
14504c8ff709SChao Yu 		err = f2fs_compress_pages(cc);
14514c8ff709SChao Yu 		if (err == -EAGAIN) {
14524c8ff709SChao Yu 			goto write;
14534c8ff709SChao Yu 		} else if (err) {
14544c8ff709SChao Yu 			f2fs_put_rpages_wbc(cc, wbc, true, 1);
14554c8ff709SChao Yu 			goto destroy_out;
14564c8ff709SChao Yu 		}
14574c8ff709SChao Yu 
14584c8ff709SChao Yu 		err = f2fs_write_compressed_pages(cc, submitted,
14594c8ff709SChao Yu 							wbc, io_type);
14604c8ff709SChao Yu 		if (!err)
14614c8ff709SChao Yu 			return 0;
14624c8ff709SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
14634c8ff709SChao Yu 	}
14644c8ff709SChao Yu write:
14654c8ff709SChao Yu 	f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
14664c8ff709SChao Yu 
14674c8ff709SChao Yu 	err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
14684c8ff709SChao Yu 	f2fs_put_rpages_wbc(cc, wbc, false, 0);
14694c8ff709SChao Yu destroy_out:
14704c8ff709SChao Yu 	f2fs_destroy_compress_ctx(cc);
14714c8ff709SChao Yu 	return err;
14724c8ff709SChao Yu }
14734c8ff709SChao Yu 
14744c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
14754c8ff709SChao Yu {
14764c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
14774c8ff709SChao Yu 	struct decompress_io_ctx *dic;
14784c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
14794c8ff709SChao Yu 	int i;
14804c8ff709SChao Yu 
14814c8ff709SChao Yu 	dic = f2fs_kzalloc(sbi, sizeof(struct decompress_io_ctx), GFP_NOFS);
14824c8ff709SChao Yu 	if (!dic)
14834c8ff709SChao Yu 		return ERR_PTR(-ENOMEM);
14844c8ff709SChao Yu 
1485*31083031SChao Yu 	dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
14864c8ff709SChao Yu 	if (!dic->rpages) {
14874c8ff709SChao Yu 		kfree(dic);
14884c8ff709SChao Yu 		return ERR_PTR(-ENOMEM);
14894c8ff709SChao Yu 	}
14904c8ff709SChao Yu 
14914c8ff709SChao Yu 	dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
14924c8ff709SChao Yu 	dic->inode = cc->inode;
1493e6c3948dSChao Yu 	atomic_set(&dic->pending_pages, cc->nr_cpages);
14944c8ff709SChao Yu 	dic->cluster_idx = cc->cluster_idx;
14954c8ff709SChao Yu 	dic->cluster_size = cc->cluster_size;
14964c8ff709SChao Yu 	dic->log_cluster_size = cc->log_cluster_size;
14974c8ff709SChao Yu 	dic->nr_cpages = cc->nr_cpages;
14984c8ff709SChao Yu 	dic->failed = false;
14994c8ff709SChao Yu 
15004c8ff709SChao Yu 	for (i = 0; i < dic->cluster_size; i++)
15014c8ff709SChao Yu 		dic->rpages[i] = cc->rpages[i];
15024c8ff709SChao Yu 	dic->nr_rpages = cc->cluster_size;
15034c8ff709SChao Yu 
1504*31083031SChao Yu 	dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
15054c8ff709SChao Yu 	if (!dic->cpages)
15064c8ff709SChao Yu 		goto out_free;
15074c8ff709SChao Yu 
15084c8ff709SChao Yu 	for (i = 0; i < dic->nr_cpages; i++) {
15094c8ff709SChao Yu 		struct page *page;
15104c8ff709SChao Yu 
15115e6bbde9SChao Yu 		page = f2fs_compress_alloc_page();
15124c8ff709SChao Yu 		if (!page)
15134c8ff709SChao Yu 			goto out_free;
15144c8ff709SChao Yu 
15154c8ff709SChao Yu 		f2fs_set_compressed_page(page, cc->inode,
1516887347a0SChao Yu 					start_idx + i + 1, dic);
15174c8ff709SChao Yu 		dic->cpages[i] = page;
15184c8ff709SChao Yu 	}
15194c8ff709SChao Yu 
15204c8ff709SChao Yu 	return dic;
15214c8ff709SChao Yu 
15224c8ff709SChao Yu out_free:
15234c8ff709SChao Yu 	f2fs_free_dic(dic);
15244c8ff709SChao Yu 	return ERR_PTR(-ENOMEM);
15254c8ff709SChao Yu }
15264c8ff709SChao Yu 
15274c8ff709SChao Yu void f2fs_free_dic(struct decompress_io_ctx *dic)
15284c8ff709SChao Yu {
15294c8ff709SChao Yu 	int i;
15304c8ff709SChao Yu 
15314c8ff709SChao Yu 	if (dic->tpages) {
15324c8ff709SChao Yu 		for (i = 0; i < dic->cluster_size; i++) {
15334c8ff709SChao Yu 			if (dic->rpages[i])
15344c8ff709SChao Yu 				continue;
15358908e753SChao Yu 			if (!dic->tpages[i])
15368908e753SChao Yu 				continue;
15375e6bbde9SChao Yu 			f2fs_compress_free_page(dic->tpages[i]);
15384c8ff709SChao Yu 		}
1539*31083031SChao Yu 		page_array_free(dic->inode, dic->tpages, dic->cluster_size);
15404c8ff709SChao Yu 	}
15414c8ff709SChao Yu 
15424c8ff709SChao Yu 	if (dic->cpages) {
15434c8ff709SChao Yu 		for (i = 0; i < dic->nr_cpages; i++) {
15444c8ff709SChao Yu 			if (!dic->cpages[i])
15454c8ff709SChao Yu 				continue;
15465e6bbde9SChao Yu 			f2fs_compress_free_page(dic->cpages[i]);
15474c8ff709SChao Yu 		}
1548*31083031SChao Yu 		page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
15494c8ff709SChao Yu 	}
15504c8ff709SChao Yu 
1551*31083031SChao Yu 	page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
15524c8ff709SChao Yu 	kfree(dic);
15534c8ff709SChao Yu }
15544c8ff709SChao Yu 
15554c8ff709SChao Yu void f2fs_decompress_end_io(struct page **rpages,
15564c8ff709SChao Yu 			unsigned int cluster_size, bool err, bool verity)
15574c8ff709SChao Yu {
15584c8ff709SChao Yu 	int i;
15594c8ff709SChao Yu 
15604c8ff709SChao Yu 	for (i = 0; i < cluster_size; i++) {
15614c8ff709SChao Yu 		struct page *rpage = rpages[i];
15624c8ff709SChao Yu 
15634c8ff709SChao Yu 		if (!rpage)
15644c8ff709SChao Yu 			continue;
15654c8ff709SChao Yu 
156623c51bedSChao Yu 		if (err || PageError(rpage))
156723c51bedSChao Yu 			goto clear_uptodate;
156823c51bedSChao Yu 
156923c51bedSChao Yu 		if (!verity || fsverity_verify_page(rpage)) {
157023c51bedSChao Yu 			SetPageUptodate(rpage);
157123c51bedSChao Yu 			goto unlock;
157223c51bedSChao Yu 		}
157323c51bedSChao Yu clear_uptodate:
15744c8ff709SChao Yu 		ClearPageUptodate(rpage);
15754c8ff709SChao Yu 		ClearPageError(rpage);
157623c51bedSChao Yu unlock:
15774c8ff709SChao Yu 		unlock_page(rpage);
15784c8ff709SChao Yu 	}
15794c8ff709SChao Yu }
1580*31083031SChao Yu 
1581*31083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
1582*31083031SChao Yu {
1583*31083031SChao Yu 	dev_t dev = sbi->sb->s_bdev->bd_dev;
1584*31083031SChao Yu 	char slab_name[32];
1585*31083031SChao Yu 
1586*31083031SChao Yu 	sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
1587*31083031SChao Yu 
1588*31083031SChao Yu 	sbi->page_array_slab_size = sizeof(struct page *) <<
1589*31083031SChao Yu 					F2FS_OPTION(sbi).compress_log_size;
1590*31083031SChao Yu 
1591*31083031SChao Yu 	sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
1592*31083031SChao Yu 					sbi->page_array_slab_size);
1593*31083031SChao Yu 	if (!sbi->page_array_slab)
1594*31083031SChao Yu 		return -ENOMEM;
1595*31083031SChao Yu 	return 0;
1596*31083031SChao Yu }
1597*31083031SChao Yu 
1598*31083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
1599*31083031SChao Yu {
1600*31083031SChao Yu 	kmem_cache_destroy(sbi->page_array_slab);
1601*31083031SChao Yu }
1602