xref: /linux/fs/f2fs/compress.c (revision 79963d967b492876fa17c8c2c2c17b7438683d9b)
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 
204c8ff709SChao Yu struct f2fs_compress_ops {
214c8ff709SChao Yu 	int (*init_compress_ctx)(struct compress_ctx *cc);
224c8ff709SChao Yu 	void (*destroy_compress_ctx)(struct compress_ctx *cc);
234c8ff709SChao Yu 	int (*compress_pages)(struct compress_ctx *cc);
2423b1faaaSChao Yu 	int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
2523b1faaaSChao Yu 	void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
264c8ff709SChao Yu 	int (*decompress_pages)(struct decompress_io_ctx *dic);
274c8ff709SChao Yu };
284c8ff709SChao Yu 
294c8ff709SChao Yu static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
304c8ff709SChao Yu {
314c8ff709SChao Yu 	return index & (cc->cluster_size - 1);
324c8ff709SChao Yu }
334c8ff709SChao Yu 
344c8ff709SChao Yu static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
354c8ff709SChao Yu {
364c8ff709SChao Yu 	return index >> cc->log_cluster_size;
374c8ff709SChao Yu }
384c8ff709SChao Yu 
394c8ff709SChao Yu static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
404c8ff709SChao Yu {
414c8ff709SChao Yu 	return cc->cluster_idx << cc->log_cluster_size;
424c8ff709SChao Yu }
434c8ff709SChao Yu 
444c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page)
454c8ff709SChao Yu {
464c8ff709SChao Yu 	if (!PagePrivate(page))
474c8ff709SChao Yu 		return false;
484c8ff709SChao Yu 	if (!page_private(page))
494c8ff709SChao Yu 		return false;
504c8ff709SChao Yu 	if (IS_ATOMIC_WRITTEN_PAGE(page) || IS_DUMMY_WRITTEN_PAGE(page))
514c8ff709SChao Yu 		return false;
524c8ff709SChao Yu 	f2fs_bug_on(F2FS_M_SB(page->mapping),
534c8ff709SChao Yu 		*((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
544c8ff709SChao Yu 	return true;
554c8ff709SChao Yu }
564c8ff709SChao Yu 
574c8ff709SChao Yu static void f2fs_set_compressed_page(struct page *page,
58887347a0SChao Yu 		struct inode *inode, pgoff_t index, void *data)
594c8ff709SChao Yu {
604c8ff709SChao Yu 	SetPagePrivate(page);
614c8ff709SChao Yu 	set_page_private(page, (unsigned long)data);
624c8ff709SChao Yu 
634c8ff709SChao Yu 	/* i_crypto_info and iv index */
644c8ff709SChao Yu 	page->index = index;
654c8ff709SChao Yu 	page->mapping = inode->i_mapping;
664c8ff709SChao Yu }
674c8ff709SChao Yu 
684c8ff709SChao Yu static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
694c8ff709SChao Yu {
704c8ff709SChao Yu 	int i;
714c8ff709SChao Yu 
724c8ff709SChao Yu 	for (i = 0; i < len; i++) {
734c8ff709SChao Yu 		if (!cc->rpages[i])
744c8ff709SChao Yu 			continue;
754c8ff709SChao Yu 		if (unlock)
764c8ff709SChao Yu 			unlock_page(cc->rpages[i]);
774c8ff709SChao Yu 		else
784c8ff709SChao Yu 			put_page(cc->rpages[i]);
794c8ff709SChao Yu 	}
804c8ff709SChao Yu }
814c8ff709SChao Yu 
824c8ff709SChao Yu static void f2fs_put_rpages(struct compress_ctx *cc)
834c8ff709SChao Yu {
844c8ff709SChao Yu 	f2fs_drop_rpages(cc, cc->cluster_size, false);
854c8ff709SChao Yu }
864c8ff709SChao Yu 
874c8ff709SChao Yu static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
884c8ff709SChao Yu {
894c8ff709SChao Yu 	f2fs_drop_rpages(cc, len, true);
904c8ff709SChao Yu }
914c8ff709SChao Yu 
92bc67c5d0SChao Yu static void f2fs_put_rpages_mapping(struct address_space *mapping,
934c8ff709SChao Yu 				pgoff_t start, int len)
944c8ff709SChao Yu {
954c8ff709SChao Yu 	int i;
964c8ff709SChao Yu 
974c8ff709SChao Yu 	for (i = 0; i < len; i++) {
984c8ff709SChao Yu 		struct page *page = find_get_page(mapping, start + i);
994c8ff709SChao Yu 
1004c8ff709SChao Yu 		put_page(page);
1014c8ff709SChao Yu 		put_page(page);
1024c8ff709SChao Yu 	}
1034c8ff709SChao Yu }
1044c8ff709SChao Yu 
1054c8ff709SChao Yu static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
1064c8ff709SChao Yu 		struct writeback_control *wbc, bool redirty, int unlock)
1074c8ff709SChao Yu {
1084c8ff709SChao Yu 	unsigned int i;
1094c8ff709SChao Yu 
1104c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
1114c8ff709SChao Yu 		if (!cc->rpages[i])
1124c8ff709SChao Yu 			continue;
1134c8ff709SChao Yu 		if (redirty)
1144c8ff709SChao Yu 			redirty_page_for_writepage(wbc, cc->rpages[i]);
1154c8ff709SChao Yu 		f2fs_put_page(cc->rpages[i], unlock);
1164c8ff709SChao Yu 	}
1174c8ff709SChao Yu }
1184c8ff709SChao Yu 
1194c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page)
1204c8ff709SChao Yu {
1214c8ff709SChao Yu 	return ((struct compress_io_ctx *)page_private(page))->rpages[0];
1224c8ff709SChao Yu }
1234c8ff709SChao Yu 
1244c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc)
1254c8ff709SChao Yu {
1264c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1274c8ff709SChao Yu 
1284c8ff709SChao Yu 	if (cc->nr_rpages)
1294c8ff709SChao Yu 		return 0;
1304c8ff709SChao Yu 
1314c8ff709SChao Yu 	cc->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
1324c8ff709SChao Yu 					cc->log_cluster_size, GFP_NOFS);
1334c8ff709SChao Yu 	return cc->rpages ? 0 : -ENOMEM;
1344c8ff709SChao Yu }
1354c8ff709SChao Yu 
1364c8ff709SChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc)
1374c8ff709SChao Yu {
1384c8ff709SChao Yu 	kfree(cc->rpages);
1394c8ff709SChao Yu 	cc->rpages = NULL;
1404c8ff709SChao Yu 	cc->nr_rpages = 0;
1414c8ff709SChao Yu 	cc->nr_cpages = 0;
1424c8ff709SChao Yu 	cc->cluster_idx = NULL_CLUSTER;
1434c8ff709SChao Yu }
1444c8ff709SChao Yu 
1454c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
1464c8ff709SChao Yu {
1474c8ff709SChao Yu 	unsigned int cluster_ofs;
1484c8ff709SChao Yu 
1494c8ff709SChao Yu 	if (!f2fs_cluster_can_merge_page(cc, page->index))
1504c8ff709SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
1514c8ff709SChao Yu 
1524c8ff709SChao Yu 	cluster_ofs = offset_in_cluster(cc, page->index);
1534c8ff709SChao Yu 	cc->rpages[cluster_ofs] = page;
1544c8ff709SChao Yu 	cc->nr_rpages++;
1554c8ff709SChao Yu 	cc->cluster_idx = cluster_idx(cc, page->index);
1564c8ff709SChao Yu }
1574c8ff709SChao Yu 
1584c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO
1594c8ff709SChao Yu static int lzo_init_compress_ctx(struct compress_ctx *cc)
1604c8ff709SChao Yu {
1614c8ff709SChao Yu 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
1624c8ff709SChao Yu 				LZO1X_MEM_COMPRESS, GFP_NOFS);
1634c8ff709SChao Yu 	if (!cc->private)
1644c8ff709SChao Yu 		return -ENOMEM;
1654c8ff709SChao Yu 
1664c8ff709SChao Yu 	cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
1674c8ff709SChao Yu 	return 0;
1684c8ff709SChao Yu }
1694c8ff709SChao Yu 
1704c8ff709SChao Yu static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
1714c8ff709SChao Yu {
1724c8ff709SChao Yu 	kvfree(cc->private);
1734c8ff709SChao Yu 	cc->private = NULL;
1744c8ff709SChao Yu }
1754c8ff709SChao Yu 
1764c8ff709SChao Yu static int lzo_compress_pages(struct compress_ctx *cc)
1774c8ff709SChao Yu {
1784c8ff709SChao Yu 	int ret;
1794c8ff709SChao Yu 
1804c8ff709SChao Yu 	ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
1814c8ff709SChao Yu 					&cc->clen, cc->private);
1824c8ff709SChao Yu 	if (ret != LZO_E_OK) {
1834c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
1844c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
1854c8ff709SChao Yu 		return -EIO;
1864c8ff709SChao Yu 	}
1874c8ff709SChao Yu 	return 0;
1884c8ff709SChao Yu }
1894c8ff709SChao Yu 
1904c8ff709SChao Yu static int lzo_decompress_pages(struct decompress_io_ctx *dic)
1914c8ff709SChao Yu {
1924c8ff709SChao Yu 	int ret;
1934c8ff709SChao Yu 
1944c8ff709SChao Yu 	ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
1954c8ff709SChao Yu 						dic->rbuf, &dic->rlen);
1964c8ff709SChao Yu 	if (ret != LZO_E_OK) {
1974c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
1984c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
1994c8ff709SChao Yu 		return -EIO;
2004c8ff709SChao Yu 	}
2014c8ff709SChao Yu 
2024c8ff709SChao Yu 	if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
2034c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
2044c8ff709SChao Yu 					"expected:%lu\n", KERN_ERR,
2054c8ff709SChao Yu 					F2FS_I_SB(dic->inode)->sb->s_id,
2064c8ff709SChao Yu 					dic->rlen,
2074c8ff709SChao Yu 					PAGE_SIZE << dic->log_cluster_size);
2084c8ff709SChao Yu 		return -EIO;
2094c8ff709SChao Yu 	}
2104c8ff709SChao Yu 	return 0;
2114c8ff709SChao Yu }
2124c8ff709SChao Yu 
2134c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lzo_ops = {
2144c8ff709SChao Yu 	.init_compress_ctx	= lzo_init_compress_ctx,
2154c8ff709SChao Yu 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
2164c8ff709SChao Yu 	.compress_pages		= lzo_compress_pages,
2174c8ff709SChao Yu 	.decompress_pages	= lzo_decompress_pages,
2184c8ff709SChao Yu };
2194c8ff709SChao Yu #endif
2204c8ff709SChao Yu 
2214c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4
2224c8ff709SChao Yu static int lz4_init_compress_ctx(struct compress_ctx *cc)
2234c8ff709SChao Yu {
2244c8ff709SChao Yu 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
2254c8ff709SChao Yu 				LZ4_MEM_COMPRESS, GFP_NOFS);
2264c8ff709SChao Yu 	if (!cc->private)
2274c8ff709SChao Yu 		return -ENOMEM;
2284c8ff709SChao Yu 
229f6644143SChao Yu 	/*
230f6644143SChao Yu 	 * we do not change cc->clen to LZ4_compressBound(inputsize) to
231f6644143SChao Yu 	 * adapt worst compress case, because lz4 compressor can handle
232f6644143SChao Yu 	 * output budget properly.
233f6644143SChao Yu 	 */
234f6644143SChao Yu 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
2354c8ff709SChao Yu 	return 0;
2364c8ff709SChao Yu }
2374c8ff709SChao Yu 
2384c8ff709SChao Yu static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
2394c8ff709SChao Yu {
2404c8ff709SChao Yu 	kvfree(cc->private);
2414c8ff709SChao Yu 	cc->private = NULL;
2424c8ff709SChao Yu }
2434c8ff709SChao Yu 
2444c8ff709SChao Yu static int lz4_compress_pages(struct compress_ctx *cc)
2454c8ff709SChao Yu {
2464c8ff709SChao Yu 	int len;
2474c8ff709SChao Yu 
2484c8ff709SChao Yu 	len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
2494c8ff709SChao Yu 						cc->clen, cc->private);
250f6644143SChao Yu 	if (!len)
251f6644143SChao Yu 		return -EAGAIN;
252f6644143SChao Yu 
2534c8ff709SChao Yu 	cc->clen = len;
2544c8ff709SChao Yu 	return 0;
2554c8ff709SChao Yu }
2564c8ff709SChao Yu 
2574c8ff709SChao Yu static int lz4_decompress_pages(struct decompress_io_ctx *dic)
2584c8ff709SChao Yu {
2594c8ff709SChao Yu 	int ret;
2604c8ff709SChao Yu 
2614c8ff709SChao Yu 	ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
2624c8ff709SChao Yu 						dic->clen, dic->rlen);
2634c8ff709SChao Yu 	if (ret < 0) {
2644c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
2654c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
2664c8ff709SChao Yu 		return -EIO;
2674c8ff709SChao Yu 	}
2684c8ff709SChao Yu 
2694c8ff709SChao Yu 	if (ret != PAGE_SIZE << dic->log_cluster_size) {
2704c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, "
2714c8ff709SChao Yu 					"expected:%lu\n", KERN_ERR,
2724c8ff709SChao Yu 					F2FS_I_SB(dic->inode)->sb->s_id,
2734c8ff709SChao Yu 					dic->rlen,
2744c8ff709SChao Yu 					PAGE_SIZE << dic->log_cluster_size);
2754c8ff709SChao Yu 		return -EIO;
2764c8ff709SChao Yu 	}
2774c8ff709SChao Yu 	return 0;
2784c8ff709SChao Yu }
2794c8ff709SChao Yu 
2804c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lz4_ops = {
2814c8ff709SChao Yu 	.init_compress_ctx	= lz4_init_compress_ctx,
2824c8ff709SChao Yu 	.destroy_compress_ctx	= lz4_destroy_compress_ctx,
2834c8ff709SChao Yu 	.compress_pages		= lz4_compress_pages,
2844c8ff709SChao Yu 	.decompress_pages	= lz4_decompress_pages,
2854c8ff709SChao Yu };
2864c8ff709SChao Yu #endif
2874c8ff709SChao Yu 
28850cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD
28950cfa66fSChao Yu #define F2FS_ZSTD_DEFAULT_CLEVEL	1
29050cfa66fSChao Yu 
29150cfa66fSChao Yu static int zstd_init_compress_ctx(struct compress_ctx *cc)
29250cfa66fSChao Yu {
29350cfa66fSChao Yu 	ZSTD_parameters params;
29450cfa66fSChao Yu 	ZSTD_CStream *stream;
29550cfa66fSChao Yu 	void *workspace;
29650cfa66fSChao Yu 	unsigned int workspace_size;
29750cfa66fSChao Yu 
29850cfa66fSChao Yu 	params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen, 0);
29950cfa66fSChao Yu 	workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
30050cfa66fSChao Yu 
30150cfa66fSChao Yu 	workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
30250cfa66fSChao Yu 					workspace_size, GFP_NOFS);
30350cfa66fSChao Yu 	if (!workspace)
30450cfa66fSChao Yu 		return -ENOMEM;
30550cfa66fSChao Yu 
30650cfa66fSChao Yu 	stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
30750cfa66fSChao Yu 	if (!stream) {
30850cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n",
30950cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
31050cfa66fSChao Yu 				__func__);
31150cfa66fSChao Yu 		kvfree(workspace);
31250cfa66fSChao Yu 		return -EIO;
31350cfa66fSChao Yu 	}
31450cfa66fSChao Yu 
31550cfa66fSChao Yu 	cc->private = workspace;
31650cfa66fSChao Yu 	cc->private2 = stream;
31750cfa66fSChao Yu 
31850cfa66fSChao Yu 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
31950cfa66fSChao Yu 	return 0;
32050cfa66fSChao Yu }
32150cfa66fSChao Yu 
32250cfa66fSChao Yu static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
32350cfa66fSChao Yu {
32450cfa66fSChao Yu 	kvfree(cc->private);
32550cfa66fSChao Yu 	cc->private = NULL;
32650cfa66fSChao Yu 	cc->private2 = NULL;
32750cfa66fSChao Yu }
32850cfa66fSChao Yu 
32950cfa66fSChao Yu static int zstd_compress_pages(struct compress_ctx *cc)
33050cfa66fSChao Yu {
33150cfa66fSChao Yu 	ZSTD_CStream *stream = cc->private2;
33250cfa66fSChao Yu 	ZSTD_inBuffer inbuf;
33350cfa66fSChao Yu 	ZSTD_outBuffer outbuf;
33450cfa66fSChao Yu 	int src_size = cc->rlen;
33550cfa66fSChao Yu 	int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
33650cfa66fSChao Yu 	int ret;
33750cfa66fSChao Yu 
33850cfa66fSChao Yu 	inbuf.pos = 0;
33950cfa66fSChao Yu 	inbuf.src = cc->rbuf;
34050cfa66fSChao Yu 	inbuf.size = src_size;
34150cfa66fSChao Yu 
34250cfa66fSChao Yu 	outbuf.pos = 0;
34350cfa66fSChao Yu 	outbuf.dst = cc->cbuf->cdata;
34450cfa66fSChao Yu 	outbuf.size = dst_size;
34550cfa66fSChao Yu 
34650cfa66fSChao Yu 	ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
34750cfa66fSChao Yu 	if (ZSTD_isError(ret)) {
34850cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
34950cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
35050cfa66fSChao Yu 				__func__, ZSTD_getErrorCode(ret));
35150cfa66fSChao Yu 		return -EIO;
35250cfa66fSChao Yu 	}
35350cfa66fSChao Yu 
35450cfa66fSChao Yu 	ret = ZSTD_endStream(stream, &outbuf);
35550cfa66fSChao Yu 	if (ZSTD_isError(ret)) {
35650cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n",
35750cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
35850cfa66fSChao Yu 				__func__, ZSTD_getErrorCode(ret));
35950cfa66fSChao Yu 		return -EIO;
36050cfa66fSChao Yu 	}
36150cfa66fSChao Yu 
3621454c978SChao Yu 	/*
3631454c978SChao Yu 	 * there is compressed data remained in intermediate buffer due to
3641454c978SChao Yu 	 * no more space in cbuf.cdata
3651454c978SChao Yu 	 */
3661454c978SChao Yu 	if (ret)
3671454c978SChao Yu 		return -EAGAIN;
3681454c978SChao Yu 
36950cfa66fSChao Yu 	cc->clen = outbuf.pos;
37050cfa66fSChao Yu 	return 0;
37150cfa66fSChao Yu }
37250cfa66fSChao Yu 
37350cfa66fSChao Yu static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
37450cfa66fSChao Yu {
37550cfa66fSChao Yu 	ZSTD_DStream *stream;
37650cfa66fSChao Yu 	void *workspace;
37750cfa66fSChao Yu 	unsigned int workspace_size;
37850cfa66fSChao Yu 
37950cfa66fSChao Yu 	workspace_size = ZSTD_DStreamWorkspaceBound(MAX_COMPRESS_WINDOW_SIZE);
38050cfa66fSChao Yu 
38150cfa66fSChao Yu 	workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
38250cfa66fSChao Yu 					workspace_size, GFP_NOFS);
38350cfa66fSChao Yu 	if (!workspace)
38450cfa66fSChao Yu 		return -ENOMEM;
38550cfa66fSChao Yu 
38650cfa66fSChao Yu 	stream = ZSTD_initDStream(MAX_COMPRESS_WINDOW_SIZE,
38750cfa66fSChao Yu 					workspace, workspace_size);
38850cfa66fSChao Yu 	if (!stream) {
38950cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n",
39050cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
39150cfa66fSChao Yu 				__func__);
39250cfa66fSChao Yu 		kvfree(workspace);
39350cfa66fSChao Yu 		return -EIO;
39450cfa66fSChao Yu 	}
39550cfa66fSChao Yu 
39650cfa66fSChao Yu 	dic->private = workspace;
39750cfa66fSChao Yu 	dic->private2 = stream;
39850cfa66fSChao Yu 
39950cfa66fSChao Yu 	return 0;
40050cfa66fSChao Yu }
40150cfa66fSChao Yu 
40250cfa66fSChao Yu static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
40350cfa66fSChao Yu {
40450cfa66fSChao Yu 	kvfree(dic->private);
40550cfa66fSChao Yu 	dic->private = NULL;
40650cfa66fSChao Yu 	dic->private2 = NULL;
40750cfa66fSChao Yu }
40850cfa66fSChao Yu 
40950cfa66fSChao Yu static int zstd_decompress_pages(struct decompress_io_ctx *dic)
41050cfa66fSChao Yu {
41150cfa66fSChao Yu 	ZSTD_DStream *stream = dic->private2;
41250cfa66fSChao Yu 	ZSTD_inBuffer inbuf;
41350cfa66fSChao Yu 	ZSTD_outBuffer outbuf;
41450cfa66fSChao Yu 	int ret;
41550cfa66fSChao Yu 
41650cfa66fSChao Yu 	inbuf.pos = 0;
41750cfa66fSChao Yu 	inbuf.src = dic->cbuf->cdata;
41850cfa66fSChao Yu 	inbuf.size = dic->clen;
41950cfa66fSChao Yu 
42050cfa66fSChao Yu 	outbuf.pos = 0;
42150cfa66fSChao Yu 	outbuf.dst = dic->rbuf;
42250cfa66fSChao Yu 	outbuf.size = dic->rlen;
42350cfa66fSChao Yu 
42450cfa66fSChao Yu 	ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
42550cfa66fSChao Yu 	if (ZSTD_isError(ret)) {
42650cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
42750cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
42850cfa66fSChao Yu 				__func__, ZSTD_getErrorCode(ret));
42950cfa66fSChao Yu 		return -EIO;
43050cfa66fSChao Yu 	}
43150cfa66fSChao Yu 
43250cfa66fSChao Yu 	if (dic->rlen != outbuf.pos) {
43350cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
43450cfa66fSChao Yu 				"expected:%lu\n", KERN_ERR,
43550cfa66fSChao Yu 				F2FS_I_SB(dic->inode)->sb->s_id,
43650cfa66fSChao Yu 				__func__, dic->rlen,
43750cfa66fSChao Yu 				PAGE_SIZE << dic->log_cluster_size);
43850cfa66fSChao Yu 		return -EIO;
43950cfa66fSChao Yu 	}
44050cfa66fSChao Yu 
44150cfa66fSChao Yu 	return 0;
44250cfa66fSChao Yu }
44350cfa66fSChao Yu 
44450cfa66fSChao Yu static const struct f2fs_compress_ops f2fs_zstd_ops = {
44550cfa66fSChao Yu 	.init_compress_ctx	= zstd_init_compress_ctx,
44650cfa66fSChao Yu 	.destroy_compress_ctx	= zstd_destroy_compress_ctx,
44750cfa66fSChao Yu 	.compress_pages		= zstd_compress_pages,
44850cfa66fSChao Yu 	.init_decompress_ctx	= zstd_init_decompress_ctx,
44950cfa66fSChao Yu 	.destroy_decompress_ctx	= zstd_destroy_decompress_ctx,
45050cfa66fSChao Yu 	.decompress_pages	= zstd_decompress_pages,
45150cfa66fSChao Yu };
45250cfa66fSChao Yu #endif
45350cfa66fSChao Yu 
4546d92b201SChao Yu #ifdef CONFIG_F2FS_FS_LZO
4556d92b201SChao Yu #ifdef CONFIG_F2FS_FS_LZORLE
4566d92b201SChao Yu static int lzorle_compress_pages(struct compress_ctx *cc)
4576d92b201SChao Yu {
4586d92b201SChao Yu 	int ret;
4596d92b201SChao Yu 
4606d92b201SChao Yu 	ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
4616d92b201SChao Yu 					&cc->clen, cc->private);
4626d92b201SChao Yu 	if (ret != LZO_E_OK) {
4636d92b201SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d\n",
4646d92b201SChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
4656d92b201SChao Yu 		return -EIO;
4666d92b201SChao Yu 	}
4676d92b201SChao Yu 	return 0;
4686d92b201SChao Yu }
4696d92b201SChao Yu 
4706d92b201SChao Yu static const struct f2fs_compress_ops f2fs_lzorle_ops = {
4716d92b201SChao Yu 	.init_compress_ctx	= lzo_init_compress_ctx,
4726d92b201SChao Yu 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
4736d92b201SChao Yu 	.compress_pages		= lzorle_compress_pages,
4746d92b201SChao Yu 	.decompress_pages	= lzo_decompress_pages,
4756d92b201SChao Yu };
4766d92b201SChao Yu #endif
4776d92b201SChao Yu #endif
4786d92b201SChao Yu 
4794c8ff709SChao Yu static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
4804c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO
4814c8ff709SChao Yu 	&f2fs_lzo_ops,
4824c8ff709SChao Yu #else
4834c8ff709SChao Yu 	NULL,
4844c8ff709SChao Yu #endif
4854c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4
4864c8ff709SChao Yu 	&f2fs_lz4_ops,
4874c8ff709SChao Yu #else
4884c8ff709SChao Yu 	NULL,
4894c8ff709SChao Yu #endif
49050cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD
49150cfa66fSChao Yu 	&f2fs_zstd_ops,
49250cfa66fSChao Yu #else
49350cfa66fSChao Yu 	NULL,
49450cfa66fSChao Yu #endif
4956d92b201SChao Yu #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
4966d92b201SChao Yu 	&f2fs_lzorle_ops,
4976d92b201SChao Yu #else
4986d92b201SChao Yu 	NULL,
4996d92b201SChao Yu #endif
5004c8ff709SChao Yu };
5014c8ff709SChao Yu 
5024c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode)
5034c8ff709SChao Yu {
5044c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
5054c8ff709SChao Yu 		return true;
5064c8ff709SChao Yu 	return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
5074c8ff709SChao Yu }
5084c8ff709SChao Yu 
50999bbe307SJaegeuk Kim static mempool_t *compress_page_pool;
5105e6bbde9SChao Yu static int num_compress_pages = 512;
5115e6bbde9SChao Yu module_param(num_compress_pages, uint, 0444);
5125e6bbde9SChao Yu MODULE_PARM_DESC(num_compress_pages,
5135e6bbde9SChao Yu 		"Number of intermediate compress pages to preallocate");
5145e6bbde9SChao Yu 
5155e6bbde9SChao Yu int f2fs_init_compress_mempool(void)
5165e6bbde9SChao Yu {
5175e6bbde9SChao Yu 	compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
5185e6bbde9SChao Yu 	if (!compress_page_pool)
5195e6bbde9SChao Yu 		return -ENOMEM;
5205e6bbde9SChao Yu 
5215e6bbde9SChao Yu 	return 0;
5225e6bbde9SChao Yu }
5235e6bbde9SChao Yu 
5245e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void)
5255e6bbde9SChao Yu {
5265e6bbde9SChao Yu 	mempool_destroy(compress_page_pool);
5275e6bbde9SChao Yu }
5285e6bbde9SChao Yu 
5295e6bbde9SChao Yu static struct page *f2fs_compress_alloc_page(void)
5304c8ff709SChao Yu {
5314c8ff709SChao Yu 	struct page *page;
5324c8ff709SChao Yu 
5335e6bbde9SChao Yu 	page = mempool_alloc(compress_page_pool, GFP_NOFS);
5344c8ff709SChao Yu 	lock_page(page);
5355e6bbde9SChao Yu 
5364c8ff709SChao Yu 	return page;
5374c8ff709SChao Yu }
5384c8ff709SChao Yu 
5395e6bbde9SChao Yu static void f2fs_compress_free_page(struct page *page)
5405e6bbde9SChao Yu {
5415e6bbde9SChao Yu 	if (!page)
5425e6bbde9SChao Yu 		return;
5435e6bbde9SChao Yu 	set_page_private(page, (unsigned long)NULL);
5445e6bbde9SChao Yu 	ClearPagePrivate(page);
5455e6bbde9SChao Yu 	page->mapping = NULL;
5465e6bbde9SChao Yu 	unlock_page(page);
5475e6bbde9SChao Yu 	mempool_free(page, compress_page_pool);
5485e6bbde9SChao Yu }
5495e6bbde9SChao Yu 
5504c8ff709SChao Yu static int f2fs_compress_pages(struct compress_ctx *cc)
5514c8ff709SChao Yu {
5524c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
5534c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
5544c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
5554c8ff709SChao Yu 				f2fs_cops[fi->i_compress_algorithm];
5564c8ff709SChao Yu 	unsigned int max_len, nr_cpages;
5574c8ff709SChao Yu 	int i, ret;
5584c8ff709SChao Yu 
5594c8ff709SChao Yu 	trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
5604c8ff709SChao Yu 				cc->cluster_size, fi->i_compress_algorithm);
5614c8ff709SChao Yu 
56223b1faaaSChao Yu 	if (cops->init_compress_ctx) {
5634c8ff709SChao Yu 		ret = cops->init_compress_ctx(cc);
5644c8ff709SChao Yu 		if (ret)
5654c8ff709SChao Yu 			goto out;
56623b1faaaSChao Yu 	}
5674c8ff709SChao Yu 
5684c8ff709SChao Yu 	max_len = COMPRESS_HEADER_SIZE + cc->clen;
5694c8ff709SChao Yu 	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
5704c8ff709SChao Yu 
5714c8ff709SChao Yu 	cc->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
5724c8ff709SChao Yu 					cc->nr_cpages, GFP_NOFS);
5734c8ff709SChao Yu 	if (!cc->cpages) {
5744c8ff709SChao Yu 		ret = -ENOMEM;
5754c8ff709SChao Yu 		goto destroy_compress_ctx;
5764c8ff709SChao Yu 	}
5774c8ff709SChao Yu 
5784c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
5795e6bbde9SChao Yu 		cc->cpages[i] = f2fs_compress_alloc_page();
5804c8ff709SChao Yu 		if (!cc->cpages[i]) {
5814c8ff709SChao Yu 			ret = -ENOMEM;
5824c8ff709SChao Yu 			goto out_free_cpages;
5834c8ff709SChao Yu 		}
5844c8ff709SChao Yu 	}
5854c8ff709SChao Yu 
5864c8ff709SChao Yu 	cc->rbuf = vmap(cc->rpages, cc->cluster_size, VM_MAP, PAGE_KERNEL_RO);
5874c8ff709SChao Yu 	if (!cc->rbuf) {
5884c8ff709SChao Yu 		ret = -ENOMEM;
5894c8ff709SChao Yu 		goto out_free_cpages;
5904c8ff709SChao Yu 	}
5914c8ff709SChao Yu 
5924c8ff709SChao Yu 	cc->cbuf = vmap(cc->cpages, cc->nr_cpages, VM_MAP, PAGE_KERNEL);
5934c8ff709SChao Yu 	if (!cc->cbuf) {
5944c8ff709SChao Yu 		ret = -ENOMEM;
5954c8ff709SChao Yu 		goto out_vunmap_rbuf;
5964c8ff709SChao Yu 	}
5974c8ff709SChao Yu 
5984c8ff709SChao Yu 	ret = cops->compress_pages(cc);
5994c8ff709SChao Yu 	if (ret)
6004c8ff709SChao Yu 		goto out_vunmap_cbuf;
6014c8ff709SChao Yu 
6024c8ff709SChao Yu 	max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
6034c8ff709SChao Yu 
6044c8ff709SChao Yu 	if (cc->clen > max_len) {
6054c8ff709SChao Yu 		ret = -EAGAIN;
6064c8ff709SChao Yu 		goto out_vunmap_cbuf;
6074c8ff709SChao Yu 	}
6084c8ff709SChao Yu 
6094c8ff709SChao Yu 	cc->cbuf->clen = cpu_to_le32(cc->clen);
6104c8ff709SChao Yu 
6114c8ff709SChao Yu 	for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
6124c8ff709SChao Yu 		cc->cbuf->reserved[i] = cpu_to_le32(0);
6134c8ff709SChao Yu 
6147fa6d598SEric Biggers 	nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
6157fa6d598SEric Biggers 
6167fa6d598SEric Biggers 	/* zero out any unused part of the last page */
6177fa6d598SEric Biggers 	memset(&cc->cbuf->cdata[cc->clen], 0,
6187fa6d598SEric Biggers 	       (nr_cpages * PAGE_SIZE) - (cc->clen + COMPRESS_HEADER_SIZE));
6197fa6d598SEric Biggers 
6204c8ff709SChao Yu 	vunmap(cc->cbuf);
6214c8ff709SChao Yu 	vunmap(cc->rbuf);
6224c8ff709SChao Yu 
6234c8ff709SChao Yu 	for (i = nr_cpages; i < cc->nr_cpages; i++) {
6245e6bbde9SChao Yu 		f2fs_compress_free_page(cc->cpages[i]);
6254c8ff709SChao Yu 		cc->cpages[i] = NULL;
6264c8ff709SChao Yu 	}
6274c8ff709SChao Yu 
62823b1faaaSChao Yu 	if (cops->destroy_compress_ctx)
62909ff4801SChao Yu 		cops->destroy_compress_ctx(cc);
63009ff4801SChao Yu 
6314c8ff709SChao Yu 	cc->nr_cpages = nr_cpages;
6324c8ff709SChao Yu 
6334c8ff709SChao Yu 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
6344c8ff709SChao Yu 							cc->clen, ret);
6354c8ff709SChao Yu 	return 0;
6364c8ff709SChao Yu 
6374c8ff709SChao Yu out_vunmap_cbuf:
6384c8ff709SChao Yu 	vunmap(cc->cbuf);
6394c8ff709SChao Yu out_vunmap_rbuf:
6404c8ff709SChao Yu 	vunmap(cc->rbuf);
6414c8ff709SChao Yu out_free_cpages:
6424c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
6434c8ff709SChao Yu 		if (cc->cpages[i])
6445e6bbde9SChao Yu 			f2fs_compress_free_page(cc->cpages[i]);
6454c8ff709SChao Yu 	}
6464c8ff709SChao Yu 	kfree(cc->cpages);
6474c8ff709SChao Yu 	cc->cpages = NULL;
6484c8ff709SChao Yu destroy_compress_ctx:
64923b1faaaSChao Yu 	if (cops->destroy_compress_ctx)
6504c8ff709SChao Yu 		cops->destroy_compress_ctx(cc);
6514c8ff709SChao Yu out:
6524c8ff709SChao Yu 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
6534c8ff709SChao Yu 							cc->clen, ret);
6544c8ff709SChao Yu 	return ret;
6554c8ff709SChao Yu }
6564c8ff709SChao Yu 
6574c8ff709SChao Yu void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity)
6584c8ff709SChao Yu {
6594c8ff709SChao Yu 	struct decompress_io_ctx *dic =
6604c8ff709SChao Yu 			(struct decompress_io_ctx *)page_private(page);
6614c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
6624c8ff709SChao Yu 	struct f2fs_inode_info *fi= F2FS_I(dic->inode);
6634c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
6644c8ff709SChao Yu 			f2fs_cops[fi->i_compress_algorithm];
6654c8ff709SChao Yu 	int ret;
6664c8ff709SChao Yu 
6674c8ff709SChao Yu 	dec_page_count(sbi, F2FS_RD_DATA);
6684c8ff709SChao Yu 
6694c8ff709SChao Yu 	if (bio->bi_status || PageError(page))
6704c8ff709SChao Yu 		dic->failed = true;
6714c8ff709SChao Yu 
6724c8ff709SChao Yu 	if (refcount_dec_not_one(&dic->ref))
6734c8ff709SChao Yu 		return;
6744c8ff709SChao Yu 
6754c8ff709SChao Yu 	trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
6764c8ff709SChao Yu 				dic->cluster_size, fi->i_compress_algorithm);
6774c8ff709SChao Yu 
6784c8ff709SChao Yu 	/* submit partial compressed pages */
6794c8ff709SChao Yu 	if (dic->failed) {
6804c8ff709SChao Yu 		ret = -EIO;
6814c8ff709SChao Yu 		goto out_free_dic;
6824c8ff709SChao Yu 	}
6834c8ff709SChao Yu 
68423b1faaaSChao Yu 	if (cops->init_decompress_ctx) {
68523b1faaaSChao Yu 		ret = cops->init_decompress_ctx(dic);
68623b1faaaSChao Yu 		if (ret)
68723b1faaaSChao Yu 			goto out_free_dic;
68823b1faaaSChao Yu 	}
68923b1faaaSChao Yu 
6904c8ff709SChao Yu 	dic->rbuf = vmap(dic->tpages, dic->cluster_size, VM_MAP, PAGE_KERNEL);
6914c8ff709SChao Yu 	if (!dic->rbuf) {
6924c8ff709SChao Yu 		ret = -ENOMEM;
69323b1faaaSChao Yu 		goto destroy_decompress_ctx;
6944c8ff709SChao Yu 	}
6954c8ff709SChao Yu 
6964c8ff709SChao Yu 	dic->cbuf = vmap(dic->cpages, dic->nr_cpages, VM_MAP, PAGE_KERNEL_RO);
6974c8ff709SChao Yu 	if (!dic->cbuf) {
6984c8ff709SChao Yu 		ret = -ENOMEM;
6994c8ff709SChao Yu 		goto out_vunmap_rbuf;
7004c8ff709SChao Yu 	}
7014c8ff709SChao Yu 
7024c8ff709SChao Yu 	dic->clen = le32_to_cpu(dic->cbuf->clen);
7034c8ff709SChao Yu 	dic->rlen = PAGE_SIZE << dic->log_cluster_size;
7044c8ff709SChao Yu 
7054c8ff709SChao Yu 	if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
7064c8ff709SChao Yu 		ret = -EFSCORRUPTED;
7074c8ff709SChao Yu 		goto out_vunmap_cbuf;
7084c8ff709SChao Yu 	}
7094c8ff709SChao Yu 
7104c8ff709SChao Yu 	ret = cops->decompress_pages(dic);
7114c8ff709SChao Yu 
7124c8ff709SChao Yu out_vunmap_cbuf:
7134c8ff709SChao Yu 	vunmap(dic->cbuf);
7144c8ff709SChao Yu out_vunmap_rbuf:
7154c8ff709SChao Yu 	vunmap(dic->rbuf);
71623b1faaaSChao Yu destroy_decompress_ctx:
71723b1faaaSChao Yu 	if (cops->destroy_decompress_ctx)
71823b1faaaSChao Yu 		cops->destroy_decompress_ctx(dic);
7194c8ff709SChao Yu out_free_dic:
72079bbefb1SChao Yu 	if (verity)
721887347a0SChao Yu 		refcount_set(&dic->ref, dic->nr_cpages);
7224c8ff709SChao Yu 	if (!verity)
7234c8ff709SChao Yu 		f2fs_decompress_end_io(dic->rpages, dic->cluster_size,
7244c8ff709SChao Yu 								ret, false);
7254c8ff709SChao Yu 
7264c8ff709SChao Yu 	trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
7274c8ff709SChao Yu 							dic->clen, ret);
7284c8ff709SChao Yu 	if (!verity)
7294c8ff709SChao Yu 		f2fs_free_dic(dic);
7304c8ff709SChao Yu }
7314c8ff709SChao Yu 
7324c8ff709SChao Yu static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
7334c8ff709SChao Yu {
7344c8ff709SChao Yu 	if (cc->cluster_idx == NULL_CLUSTER)
7354c8ff709SChao Yu 		return true;
7364c8ff709SChao Yu 	return cc->cluster_idx == cluster_idx(cc, index);
7374c8ff709SChao Yu }
7384c8ff709SChao Yu 
7394c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc)
7404c8ff709SChao Yu {
7414c8ff709SChao Yu 	return cc->nr_rpages == 0;
7424c8ff709SChao Yu }
7434c8ff709SChao Yu 
7444c8ff709SChao Yu static bool f2fs_cluster_is_full(struct compress_ctx *cc)
7454c8ff709SChao Yu {
7464c8ff709SChao Yu 	return cc->cluster_size == cc->nr_rpages;
7474c8ff709SChao Yu }
7484c8ff709SChao Yu 
7494c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
7504c8ff709SChao Yu {
7514c8ff709SChao Yu 	if (f2fs_cluster_is_empty(cc))
7524c8ff709SChao Yu 		return true;
7534c8ff709SChao Yu 	return is_page_in_cluster(cc, index);
7544c8ff709SChao Yu }
7554c8ff709SChao Yu 
7564c8ff709SChao Yu static bool __cluster_may_compress(struct compress_ctx *cc)
7574c8ff709SChao Yu {
7584c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
7594c8ff709SChao Yu 	loff_t i_size = i_size_read(cc->inode);
7604c8ff709SChao Yu 	unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
7614c8ff709SChao Yu 	int i;
7624c8ff709SChao Yu 
7634c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
7644c8ff709SChao Yu 		struct page *page = cc->rpages[i];
7654c8ff709SChao Yu 
7664c8ff709SChao Yu 		f2fs_bug_on(sbi, !page);
7674c8ff709SChao Yu 
7684c8ff709SChao Yu 		if (unlikely(f2fs_cp_error(sbi)))
7694c8ff709SChao Yu 			return false;
7704c8ff709SChao Yu 		if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
7714c8ff709SChao Yu 			return false;
7724c8ff709SChao Yu 
7734c8ff709SChao Yu 		/* beyond EOF */
7744c8ff709SChao Yu 		if (page->index >= nr_pages)
7754c8ff709SChao Yu 			return false;
7764c8ff709SChao Yu 	}
7774c8ff709SChao Yu 	return true;
7784c8ff709SChao Yu }
7794c8ff709SChao Yu 
7801a67cbe1SChao Yu static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
7814c8ff709SChao Yu {
7824c8ff709SChao Yu 	struct dnode_of_data dn;
7834c8ff709SChao Yu 	int ret;
7844c8ff709SChao Yu 
7854c8ff709SChao Yu 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
7864c8ff709SChao Yu 	ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc),
7874c8ff709SChao Yu 							LOOKUP_NODE);
7884c8ff709SChao Yu 	if (ret) {
7894c8ff709SChao Yu 		if (ret == -ENOENT)
7904c8ff709SChao Yu 			ret = 0;
7914c8ff709SChao Yu 		goto fail;
7924c8ff709SChao Yu 	}
7934c8ff709SChao Yu 
7944c8ff709SChao Yu 	if (dn.data_blkaddr == COMPRESS_ADDR) {
7954c8ff709SChao Yu 		int i;
7964c8ff709SChao Yu 
7974c8ff709SChao Yu 		ret = 1;
7984c8ff709SChao Yu 		for (i = 1; i < cc->cluster_size; i++) {
7994c8ff709SChao Yu 			block_t blkaddr;
8004c8ff709SChao Yu 
801a2ced1ceSChao Yu 			blkaddr = data_blkaddr(dn.inode,
8024c8ff709SChao Yu 					dn.node_page, dn.ofs_in_node + i);
8031a67cbe1SChao Yu 			if (compr) {
8041a67cbe1SChao Yu 				if (__is_valid_data_blkaddr(blkaddr))
8051a67cbe1SChao Yu 					ret++;
8061a67cbe1SChao Yu 			} else {
8074c8ff709SChao Yu 				if (blkaddr != NULL_ADDR)
8084c8ff709SChao Yu 					ret++;
8094c8ff709SChao Yu 			}
8104c8ff709SChao Yu 		}
8111a67cbe1SChao Yu 	}
8124c8ff709SChao Yu fail:
8134c8ff709SChao Yu 	f2fs_put_dnode(&dn);
8144c8ff709SChao Yu 	return ret;
8154c8ff709SChao Yu }
8164c8ff709SChao Yu 
8171a67cbe1SChao Yu /* return # of compressed blocks in compressed cluster */
8181a67cbe1SChao Yu static int f2fs_compressed_blocks(struct compress_ctx *cc)
8191a67cbe1SChao Yu {
8201a67cbe1SChao Yu 	return __f2fs_cluster_blocks(cc, true);
8211a67cbe1SChao Yu }
8221a67cbe1SChao Yu 
8231a67cbe1SChao Yu /* return # of valid blocks in compressed cluster */
8241a67cbe1SChao Yu static int f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
8251a67cbe1SChao Yu {
8261a67cbe1SChao Yu 	return __f2fs_cluster_blocks(cc, false);
8271a67cbe1SChao Yu }
8281a67cbe1SChao Yu 
8294c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
8304c8ff709SChao Yu {
8314c8ff709SChao Yu 	struct compress_ctx cc = {
8324c8ff709SChao Yu 		.inode = inode,
8334c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
8344c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
8354c8ff709SChao Yu 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
8364c8ff709SChao Yu 	};
8374c8ff709SChao Yu 
8381a67cbe1SChao Yu 	return f2fs_cluster_blocks(&cc, false);
8394c8ff709SChao Yu }
8404c8ff709SChao Yu 
8414c8ff709SChao Yu static bool cluster_may_compress(struct compress_ctx *cc)
8424c8ff709SChao Yu {
8434c8ff709SChao Yu 	if (!f2fs_compressed_file(cc->inode))
8444c8ff709SChao Yu 		return false;
8454c8ff709SChao Yu 	if (f2fs_is_atomic_file(cc->inode))
8464c8ff709SChao Yu 		return false;
8474c8ff709SChao Yu 	if (f2fs_is_mmap_file(cc->inode))
8484c8ff709SChao Yu 		return false;
8494c8ff709SChao Yu 	if (!f2fs_cluster_is_full(cc))
8504c8ff709SChao Yu 		return false;
851dc35d73aSChao Yu 	if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
852dc35d73aSChao Yu 		return false;
8534c8ff709SChao Yu 	return __cluster_may_compress(cc);
8544c8ff709SChao Yu }
8554c8ff709SChao Yu 
8564c8ff709SChao Yu static void set_cluster_writeback(struct compress_ctx *cc)
8574c8ff709SChao Yu {
8584c8ff709SChao Yu 	int i;
8594c8ff709SChao Yu 
8604c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
8614c8ff709SChao Yu 		if (cc->rpages[i])
8624c8ff709SChao Yu 			set_page_writeback(cc->rpages[i]);
8634c8ff709SChao Yu 	}
8644c8ff709SChao Yu }
8654c8ff709SChao Yu 
8664c8ff709SChao Yu static void set_cluster_dirty(struct compress_ctx *cc)
8674c8ff709SChao Yu {
8684c8ff709SChao Yu 	int i;
8694c8ff709SChao Yu 
8704c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++)
8714c8ff709SChao Yu 		if (cc->rpages[i])
8724c8ff709SChao Yu 			set_page_dirty(cc->rpages[i]);
8734c8ff709SChao Yu }
8744c8ff709SChao Yu 
8754c8ff709SChao Yu static int prepare_compress_overwrite(struct compress_ctx *cc,
8764c8ff709SChao Yu 		struct page **pagep, pgoff_t index, void **fsdata)
8774c8ff709SChao Yu {
8784c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
8794c8ff709SChao Yu 	struct address_space *mapping = cc->inode->i_mapping;
8804c8ff709SChao Yu 	struct page *page;
8814c8ff709SChao Yu 	struct dnode_of_data dn;
8824c8ff709SChao Yu 	sector_t last_block_in_bio;
8834c8ff709SChao Yu 	unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
8844c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
8854c8ff709SChao Yu 	int i, ret;
8864c8ff709SChao Yu 	bool prealloc;
8874c8ff709SChao Yu 
8884c8ff709SChao Yu retry:
8891a67cbe1SChao Yu 	ret = f2fs_cluster_blocks(cc, false);
8904c8ff709SChao Yu 	if (ret <= 0)
8914c8ff709SChao Yu 		return ret;
8924c8ff709SChao Yu 
8934c8ff709SChao Yu 	/* compressed case */
8944c8ff709SChao Yu 	prealloc = (ret < cc->cluster_size);
8954c8ff709SChao Yu 
8964c8ff709SChao Yu 	ret = f2fs_init_compress_ctx(cc);
8974c8ff709SChao Yu 	if (ret)
8984c8ff709SChao Yu 		return ret;
8994c8ff709SChao Yu 
9004c8ff709SChao Yu 	/* keep page reference to avoid page reclaim */
9014c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
9024c8ff709SChao Yu 		page = f2fs_pagecache_get_page(mapping, start_idx + i,
9034c8ff709SChao Yu 							fgp_flag, GFP_NOFS);
9044c8ff709SChao Yu 		if (!page) {
9054c8ff709SChao Yu 			ret = -ENOMEM;
9064c8ff709SChao Yu 			goto unlock_pages;
9074c8ff709SChao Yu 		}
9084c8ff709SChao Yu 
9094c8ff709SChao Yu 		if (PageUptodate(page))
9104c8ff709SChao Yu 			unlock_page(page);
9114c8ff709SChao Yu 		else
9124c8ff709SChao Yu 			f2fs_compress_ctx_add_page(cc, page);
9134c8ff709SChao Yu 	}
9144c8ff709SChao Yu 
9154c8ff709SChao Yu 	if (!f2fs_cluster_is_empty(cc)) {
9164c8ff709SChao Yu 		struct bio *bio = NULL;
9174c8ff709SChao Yu 
9184c8ff709SChao Yu 		ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
9190683728aSChao Yu 					&last_block_in_bio, false, true);
9204c8ff709SChao Yu 		f2fs_destroy_compress_ctx(cc);
9214c8ff709SChao Yu 		if (ret)
9224c8ff709SChao Yu 			goto release_pages;
9234c8ff709SChao Yu 		if (bio)
9244c8ff709SChao Yu 			f2fs_submit_bio(sbi, bio, DATA);
9254c8ff709SChao Yu 
9264c8ff709SChao Yu 		ret = f2fs_init_compress_ctx(cc);
9274c8ff709SChao Yu 		if (ret)
9284c8ff709SChao Yu 			goto release_pages;
9294c8ff709SChao Yu 	}
9304c8ff709SChao Yu 
9314c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
9324c8ff709SChao Yu 		f2fs_bug_on(sbi, cc->rpages[i]);
9334c8ff709SChao Yu 
9344c8ff709SChao Yu 		page = find_lock_page(mapping, start_idx + i);
9354c8ff709SChao Yu 		f2fs_bug_on(sbi, !page);
9364c8ff709SChao Yu 
9374c8ff709SChao Yu 		f2fs_wait_on_page_writeback(page, DATA, true, true);
9384c8ff709SChao Yu 
9394c8ff709SChao Yu 		f2fs_compress_ctx_add_page(cc, page);
9404c8ff709SChao Yu 		f2fs_put_page(page, 0);
9414c8ff709SChao Yu 
9424c8ff709SChao Yu 		if (!PageUptodate(page)) {
9434c8ff709SChao Yu 			f2fs_unlock_rpages(cc, i + 1);
944bc67c5d0SChao Yu 			f2fs_put_rpages_mapping(mapping, start_idx,
9454c8ff709SChao Yu 					cc->cluster_size);
9464c8ff709SChao Yu 			f2fs_destroy_compress_ctx(cc);
9474c8ff709SChao Yu 			goto retry;
9484c8ff709SChao Yu 		}
9494c8ff709SChao Yu 	}
9504c8ff709SChao Yu 
9514c8ff709SChao Yu 	if (prealloc) {
9520ef81833SChao Yu 		f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
9534c8ff709SChao Yu 
9544c8ff709SChao Yu 		set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
9554c8ff709SChao Yu 
9564c8ff709SChao Yu 		for (i = cc->cluster_size - 1; i > 0; i--) {
9574c8ff709SChao Yu 			ret = f2fs_get_block(&dn, start_idx + i);
9584c8ff709SChao Yu 			if (ret) {
9594c8ff709SChao Yu 				i = cc->cluster_size;
9604c8ff709SChao Yu 				break;
9614c8ff709SChao Yu 			}
9624c8ff709SChao Yu 
9634c8ff709SChao Yu 			if (dn.data_blkaddr != NEW_ADDR)
9644c8ff709SChao Yu 				break;
9654c8ff709SChao Yu 		}
9664c8ff709SChao Yu 
9670ef81833SChao Yu 		f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
9684c8ff709SChao Yu 	}
9694c8ff709SChao Yu 
9704c8ff709SChao Yu 	if (likely(!ret)) {
9714c8ff709SChao Yu 		*fsdata = cc->rpages;
9724c8ff709SChao Yu 		*pagep = cc->rpages[offset_in_cluster(cc, index)];
9734c8ff709SChao Yu 		return cc->cluster_size;
9744c8ff709SChao Yu 	}
9754c8ff709SChao Yu 
9764c8ff709SChao Yu unlock_pages:
9774c8ff709SChao Yu 	f2fs_unlock_rpages(cc, i);
9784c8ff709SChao Yu release_pages:
979bc67c5d0SChao Yu 	f2fs_put_rpages_mapping(mapping, start_idx, i);
9804c8ff709SChao Yu 	f2fs_destroy_compress_ctx(cc);
9814c8ff709SChao Yu 	return ret;
9824c8ff709SChao Yu }
9834c8ff709SChao Yu 
9844c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
9854c8ff709SChao Yu 		struct page **pagep, pgoff_t index, void **fsdata)
9864c8ff709SChao Yu {
9874c8ff709SChao Yu 	struct compress_ctx cc = {
9884c8ff709SChao Yu 		.inode = inode,
9894c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
9904c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
9914c8ff709SChao Yu 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
9924c8ff709SChao Yu 		.rpages = NULL,
9934c8ff709SChao Yu 		.nr_rpages = 0,
9944c8ff709SChao Yu 	};
9954c8ff709SChao Yu 
9964c8ff709SChao Yu 	return prepare_compress_overwrite(&cc, pagep, index, fsdata);
9974c8ff709SChao Yu }
9984c8ff709SChao Yu 
9994c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
10004c8ff709SChao Yu 					pgoff_t index, unsigned copied)
10014c8ff709SChao Yu 
10024c8ff709SChao Yu {
10034c8ff709SChao Yu 	struct compress_ctx cc = {
10044c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
10054c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
10064c8ff709SChao Yu 		.rpages = fsdata,
10074c8ff709SChao Yu 	};
10084c8ff709SChao Yu 	bool first_index = (index == cc.rpages[0]->index);
10094c8ff709SChao Yu 
10104c8ff709SChao Yu 	if (copied)
10114c8ff709SChao Yu 		set_cluster_dirty(&cc);
10124c8ff709SChao Yu 
10134c8ff709SChao Yu 	f2fs_put_rpages_wbc(&cc, NULL, false, 1);
10144c8ff709SChao Yu 	f2fs_destroy_compress_ctx(&cc);
10154c8ff709SChao Yu 
10164c8ff709SChao Yu 	return first_index;
10174c8ff709SChao Yu }
10184c8ff709SChao Yu 
10193265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
10203265d3dbSChao Yu {
10213265d3dbSChao Yu 	void *fsdata = NULL;
10223265d3dbSChao Yu 	struct page *pagep;
10233265d3dbSChao Yu 	int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
10243265d3dbSChao Yu 	pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
10253265d3dbSChao Yu 							log_cluster_size;
10263265d3dbSChao Yu 	int err;
10273265d3dbSChao Yu 
10283265d3dbSChao Yu 	err = f2fs_is_compressed_cluster(inode, start_idx);
10293265d3dbSChao Yu 	if (err < 0)
10303265d3dbSChao Yu 		return err;
10313265d3dbSChao Yu 
10323265d3dbSChao Yu 	/* truncate normal cluster */
10333265d3dbSChao Yu 	if (!err)
10343265d3dbSChao Yu 		return f2fs_do_truncate_blocks(inode, from, lock);
10353265d3dbSChao Yu 
10363265d3dbSChao Yu 	/* truncate compressed cluster */
10373265d3dbSChao Yu 	err = f2fs_prepare_compress_overwrite(inode, &pagep,
10383265d3dbSChao Yu 						start_idx, &fsdata);
10393265d3dbSChao Yu 
10403265d3dbSChao Yu 	/* should not be a normal cluster */
10413265d3dbSChao Yu 	f2fs_bug_on(F2FS_I_SB(inode), err == 0);
10423265d3dbSChao Yu 
10433265d3dbSChao Yu 	if (err <= 0)
10443265d3dbSChao Yu 		return err;
10453265d3dbSChao Yu 
10463265d3dbSChao Yu 	if (err > 0) {
10473265d3dbSChao Yu 		struct page **rpages = fsdata;
10483265d3dbSChao Yu 		int cluster_size = F2FS_I(inode)->i_cluster_size;
10493265d3dbSChao Yu 		int i;
10503265d3dbSChao Yu 
10513265d3dbSChao Yu 		for (i = cluster_size - 1; i >= 0; i--) {
10523265d3dbSChao Yu 			loff_t start = rpages[i]->index << PAGE_SHIFT;
10533265d3dbSChao Yu 
10543265d3dbSChao Yu 			if (from <= start) {
10553265d3dbSChao Yu 				zero_user_segment(rpages[i], 0, PAGE_SIZE);
10563265d3dbSChao Yu 			} else {
10573265d3dbSChao Yu 				zero_user_segment(rpages[i], from - start,
10583265d3dbSChao Yu 								PAGE_SIZE);
10593265d3dbSChao Yu 				break;
10603265d3dbSChao Yu 			}
10613265d3dbSChao Yu 		}
10623265d3dbSChao Yu 
10633265d3dbSChao Yu 		f2fs_compress_write_end(inode, fsdata, start_idx, true);
10643265d3dbSChao Yu 	}
10653265d3dbSChao Yu 	return 0;
10663265d3dbSChao Yu }
10673265d3dbSChao Yu 
10684c8ff709SChao Yu static int f2fs_write_compressed_pages(struct compress_ctx *cc,
10694c8ff709SChao Yu 					int *submitted,
10704c8ff709SChao Yu 					struct writeback_control *wbc,
10714c8ff709SChao Yu 					enum iostat_type io_type)
10724c8ff709SChao Yu {
10734c8ff709SChao Yu 	struct inode *inode = cc->inode;
10744c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
10754c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
10764c8ff709SChao Yu 	struct f2fs_io_info fio = {
10774c8ff709SChao Yu 		.sbi = sbi,
10784c8ff709SChao Yu 		.ino = cc->inode->i_ino,
10794c8ff709SChao Yu 		.type = DATA,
10804c8ff709SChao Yu 		.op = REQ_OP_WRITE,
10814c8ff709SChao Yu 		.op_flags = wbc_to_write_flags(wbc),
10824c8ff709SChao Yu 		.old_blkaddr = NEW_ADDR,
10834c8ff709SChao Yu 		.page = NULL,
10844c8ff709SChao Yu 		.encrypted_page = NULL,
10854c8ff709SChao Yu 		.compressed_page = NULL,
10864c8ff709SChao Yu 		.submitted = false,
10874c8ff709SChao Yu 		.io_type = io_type,
10884c8ff709SChao Yu 		.io_wbc = wbc,
10894c8ff709SChao Yu 		.encrypted = f2fs_encrypted_file(cc->inode),
10904c8ff709SChao Yu 	};
10914c8ff709SChao Yu 	struct dnode_of_data dn;
10924c8ff709SChao Yu 	struct node_info ni;
10934c8ff709SChao Yu 	struct compress_io_ctx *cic;
10944c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
10954c8ff709SChao Yu 	unsigned int last_index = cc->cluster_size - 1;
10964c8ff709SChao Yu 	loff_t psize;
10974c8ff709SChao Yu 	int i, err;
10984c8ff709SChao Yu 
1099*79963d96SChao Yu 	if (IS_NOQUOTA(inode)) {
1100*79963d96SChao Yu 		/*
1101*79963d96SChao Yu 		 * We need to wait for node_write to avoid block allocation during
1102*79963d96SChao Yu 		 * checkpoint. This can only happen to quota writes which can cause
1103*79963d96SChao Yu 		 * the below discard race condition.
1104*79963d96SChao Yu 		 */
1105*79963d96SChao Yu 		down_read(&sbi->node_write);
1106*79963d96SChao Yu 	} else if (!f2fs_trylock_op(sbi)) {
1107df77fbd8SChao Yu 		return -EAGAIN;
1108*79963d96SChao Yu 	}
11094c8ff709SChao Yu 
1110df77fbd8SChao Yu 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
11114c8ff709SChao Yu 
11124c8ff709SChao Yu 	err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
11134c8ff709SChao Yu 	if (err)
11144c8ff709SChao Yu 		goto out_unlock_op;
11154c8ff709SChao Yu 
11164c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
1117a2ced1ceSChao Yu 		if (data_blkaddr(dn.inode, dn.node_page,
11184c8ff709SChao Yu 					dn.ofs_in_node + i) == NULL_ADDR)
11194c8ff709SChao Yu 			goto out_put_dnode;
11204c8ff709SChao Yu 	}
11214c8ff709SChao Yu 
11224c8ff709SChao Yu 	psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
11234c8ff709SChao Yu 
11244c8ff709SChao Yu 	err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
11254c8ff709SChao Yu 	if (err)
11264c8ff709SChao Yu 		goto out_put_dnode;
11274c8ff709SChao Yu 
11284c8ff709SChao Yu 	fio.version = ni.version;
11294c8ff709SChao Yu 
11304c8ff709SChao Yu 	cic = f2fs_kzalloc(sbi, sizeof(struct compress_io_ctx), GFP_NOFS);
11314c8ff709SChao Yu 	if (!cic)
11324c8ff709SChao Yu 		goto out_put_dnode;
11334c8ff709SChao Yu 
11344c8ff709SChao Yu 	cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
11354c8ff709SChao Yu 	cic->inode = inode;
1136887347a0SChao Yu 	refcount_set(&cic->ref, cc->nr_cpages);
11374c8ff709SChao Yu 	cic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
11384c8ff709SChao Yu 			cc->log_cluster_size, GFP_NOFS);
11394c8ff709SChao Yu 	if (!cic->rpages)
11404c8ff709SChao Yu 		goto out_put_cic;
11414c8ff709SChao Yu 
11424c8ff709SChao Yu 	cic->nr_rpages = cc->cluster_size;
11434c8ff709SChao Yu 
11444c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
11454c8ff709SChao Yu 		f2fs_set_compressed_page(cc->cpages[i], inode,
1146887347a0SChao Yu 					cc->rpages[i + 1]->index, cic);
11474c8ff709SChao Yu 		fio.compressed_page = cc->cpages[i];
11484c8ff709SChao Yu 		if (fio.encrypted) {
11494c8ff709SChao Yu 			fio.page = cc->rpages[i + 1];
11504c8ff709SChao Yu 			err = f2fs_encrypt_one_page(&fio);
11514c8ff709SChao Yu 			if (err)
11524c8ff709SChao Yu 				goto out_destroy_crypt;
11534c8ff709SChao Yu 			cc->cpages[i] = fio.encrypted_page;
11544c8ff709SChao Yu 		}
11554c8ff709SChao Yu 	}
11564c8ff709SChao Yu 
11574c8ff709SChao Yu 	set_cluster_writeback(cc);
11584c8ff709SChao Yu 
11594c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++)
11604c8ff709SChao Yu 		cic->rpages[i] = cc->rpages[i];
11614c8ff709SChao Yu 
11624c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
11634c8ff709SChao Yu 		block_t blkaddr;
11644c8ff709SChao Yu 
1165a2ced1ceSChao Yu 		blkaddr = f2fs_data_blkaddr(&dn);
116695978caaSChao Yu 		fio.page = cc->rpages[i];
11674c8ff709SChao Yu 		fio.old_blkaddr = blkaddr;
11684c8ff709SChao Yu 
11694c8ff709SChao Yu 		/* cluster header */
11704c8ff709SChao Yu 		if (i == 0) {
11714c8ff709SChao Yu 			if (blkaddr == COMPRESS_ADDR)
11724c8ff709SChao Yu 				fio.compr_blocks++;
11734c8ff709SChao Yu 			if (__is_valid_data_blkaddr(blkaddr))
11744c8ff709SChao Yu 				f2fs_invalidate_blocks(sbi, blkaddr);
11754c8ff709SChao Yu 			f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
11764c8ff709SChao Yu 			goto unlock_continue;
11774c8ff709SChao Yu 		}
11784c8ff709SChao Yu 
11794c8ff709SChao Yu 		if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
11804c8ff709SChao Yu 			fio.compr_blocks++;
11814c8ff709SChao Yu 
11824c8ff709SChao Yu 		if (i > cc->nr_cpages) {
11834c8ff709SChao Yu 			if (__is_valid_data_blkaddr(blkaddr)) {
11844c8ff709SChao Yu 				f2fs_invalidate_blocks(sbi, blkaddr);
11854c8ff709SChao Yu 				f2fs_update_data_blkaddr(&dn, NEW_ADDR);
11864c8ff709SChao Yu 			}
11874c8ff709SChao Yu 			goto unlock_continue;
11884c8ff709SChao Yu 		}
11894c8ff709SChao Yu 
11904c8ff709SChao Yu 		f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
11914c8ff709SChao Yu 
11924c8ff709SChao Yu 		if (fio.encrypted)
11934c8ff709SChao Yu 			fio.encrypted_page = cc->cpages[i - 1];
11944c8ff709SChao Yu 		else
11954c8ff709SChao Yu 			fio.compressed_page = cc->cpages[i - 1];
11964c8ff709SChao Yu 
11974c8ff709SChao Yu 		cc->cpages[i - 1] = NULL;
11984c8ff709SChao Yu 		f2fs_outplace_write_data(&dn, &fio);
11994c8ff709SChao Yu 		(*submitted)++;
12004c8ff709SChao Yu unlock_continue:
12014c8ff709SChao Yu 		inode_dec_dirty_pages(cc->inode);
12024c8ff709SChao Yu 		unlock_page(fio.page);
12034c8ff709SChao Yu 	}
12044c8ff709SChao Yu 
12054c8ff709SChao Yu 	if (fio.compr_blocks)
12064c8ff709SChao Yu 		f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
12074c8ff709SChao Yu 	f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
12084c8ff709SChao Yu 
12094c8ff709SChao Yu 	set_inode_flag(cc->inode, FI_APPEND_WRITE);
12104c8ff709SChao Yu 	if (cc->cluster_idx == 0)
12114c8ff709SChao Yu 		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
12124c8ff709SChao Yu 
12134c8ff709SChao Yu 	f2fs_put_dnode(&dn);
1214*79963d96SChao Yu 	if (IS_NOQUOTA(inode))
1215*79963d96SChao Yu 		up_read(&sbi->node_write);
1216*79963d96SChao Yu 	else
12174c8ff709SChao Yu 		f2fs_unlock_op(sbi);
12184c8ff709SChao Yu 
1219c10c9820SChao Yu 	spin_lock(&fi->i_size_lock);
12204c8ff709SChao Yu 	if (fi->last_disk_size < psize)
12214c8ff709SChao Yu 		fi->last_disk_size = psize;
1222c10c9820SChao Yu 	spin_unlock(&fi->i_size_lock);
12234c8ff709SChao Yu 
12244c8ff709SChao Yu 	f2fs_put_rpages(cc);
12254c8ff709SChao Yu 	f2fs_destroy_compress_ctx(cc);
12264c8ff709SChao Yu 	return 0;
12274c8ff709SChao Yu 
12284c8ff709SChao Yu out_destroy_crypt:
12294c8ff709SChao Yu 	kfree(cic->rpages);
12304c8ff709SChao Yu 
12314c8ff709SChao Yu 	for (--i; i >= 0; i--)
12324c8ff709SChao Yu 		fscrypt_finalize_bounce_page(&cc->cpages[i]);
12334c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
12344c8ff709SChao Yu 		if (!cc->cpages[i])
12354c8ff709SChao Yu 			continue;
12364c8ff709SChao Yu 		f2fs_put_page(cc->cpages[i], 1);
12374c8ff709SChao Yu 	}
12384c8ff709SChao Yu out_put_cic:
12394c8ff709SChao Yu 	kfree(cic);
12404c8ff709SChao Yu out_put_dnode:
12414c8ff709SChao Yu 	f2fs_put_dnode(&dn);
12424c8ff709SChao Yu out_unlock_op:
1243*79963d96SChao Yu 	if (IS_NOQUOTA(inode))
1244*79963d96SChao Yu 		up_read(&sbi->node_write);
1245*79963d96SChao Yu 	else
12464c8ff709SChao Yu 		f2fs_unlock_op(sbi);
12474c8ff709SChao Yu 	return -EAGAIN;
12484c8ff709SChao Yu }
12494c8ff709SChao Yu 
12504c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
12514c8ff709SChao Yu {
12524c8ff709SChao Yu 	struct f2fs_sb_info *sbi = bio->bi_private;
12534c8ff709SChao Yu 	struct compress_io_ctx *cic =
12544c8ff709SChao Yu 			(struct compress_io_ctx *)page_private(page);
12554c8ff709SChao Yu 	int i;
12564c8ff709SChao Yu 
12574c8ff709SChao Yu 	if (unlikely(bio->bi_status))
12584c8ff709SChao Yu 		mapping_set_error(cic->inode->i_mapping, -EIO);
12594c8ff709SChao Yu 
12605e6bbde9SChao Yu 	f2fs_compress_free_page(page);
12614c8ff709SChao Yu 
12624c8ff709SChao Yu 	dec_page_count(sbi, F2FS_WB_DATA);
12634c8ff709SChao Yu 
12644c8ff709SChao Yu 	if (refcount_dec_not_one(&cic->ref))
12654c8ff709SChao Yu 		return;
12664c8ff709SChao Yu 
12674c8ff709SChao Yu 	for (i = 0; i < cic->nr_rpages; i++) {
12684c8ff709SChao Yu 		WARN_ON(!cic->rpages[i]);
12694c8ff709SChao Yu 		clear_cold_data(cic->rpages[i]);
12704c8ff709SChao Yu 		end_page_writeback(cic->rpages[i]);
12714c8ff709SChao Yu 	}
12724c8ff709SChao Yu 
12734c8ff709SChao Yu 	kfree(cic->rpages);
12744c8ff709SChao Yu 	kfree(cic);
12754c8ff709SChao Yu }
12764c8ff709SChao Yu 
12774c8ff709SChao Yu static int f2fs_write_raw_pages(struct compress_ctx *cc,
12784c8ff709SChao Yu 					int *submitted,
12794c8ff709SChao Yu 					struct writeback_control *wbc,
12804c8ff709SChao Yu 					enum iostat_type io_type)
12814c8ff709SChao Yu {
12824c8ff709SChao Yu 	struct address_space *mapping = cc->inode->i_mapping;
12834c8ff709SChao Yu 	int _submitted, compr_blocks, ret;
12844c8ff709SChao Yu 	int i = -1, err = 0;
12854c8ff709SChao Yu 
12864c8ff709SChao Yu 	compr_blocks = f2fs_compressed_blocks(cc);
12874c8ff709SChao Yu 	if (compr_blocks < 0) {
12884c8ff709SChao Yu 		err = compr_blocks;
12894c8ff709SChao Yu 		goto out_err;
12904c8ff709SChao Yu 	}
12914c8ff709SChao Yu 
12924c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
12934c8ff709SChao Yu 		if (!cc->rpages[i])
12944c8ff709SChao Yu 			continue;
12954c8ff709SChao Yu retry_write:
12964c8ff709SChao Yu 		if (cc->rpages[i]->mapping != mapping) {
12974c8ff709SChao Yu 			unlock_page(cc->rpages[i]);
12984c8ff709SChao Yu 			continue;
12994c8ff709SChao Yu 		}
13004c8ff709SChao Yu 
13014c8ff709SChao Yu 		BUG_ON(!PageLocked(cc->rpages[i]));
13024c8ff709SChao Yu 
13034c8ff709SChao Yu 		ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
13044c8ff709SChao Yu 						NULL, NULL, wbc, io_type,
13054c8ff709SChao Yu 						compr_blocks);
13064c8ff709SChao Yu 		if (ret) {
13074c8ff709SChao Yu 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
13084c8ff709SChao Yu 				unlock_page(cc->rpages[i]);
13094c8ff709SChao Yu 				ret = 0;
13104c8ff709SChao Yu 			} else if (ret == -EAGAIN) {
1311466357dcSChao Yu 				/*
1312466357dcSChao Yu 				 * for quota file, just redirty left pages to
1313466357dcSChao Yu 				 * avoid deadlock caused by cluster update race
1314466357dcSChao Yu 				 * from foreground operation.
1315466357dcSChao Yu 				 */
1316466357dcSChao Yu 				if (IS_NOQUOTA(cc->inode)) {
1317466357dcSChao Yu 					err = 0;
1318466357dcSChao Yu 					goto out_err;
1319466357dcSChao Yu 				}
13204c8ff709SChao Yu 				ret = 0;
13214c8ff709SChao Yu 				cond_resched();
13225df7731fSChao Yu 				congestion_wait(BLK_RW_ASYNC,
13235df7731fSChao Yu 						DEFAULT_IO_TIMEOUT);
13244c8ff709SChao Yu 				lock_page(cc->rpages[i]);
13254c8ff709SChao Yu 				clear_page_dirty_for_io(cc->rpages[i]);
13264c8ff709SChao Yu 				goto retry_write;
13274c8ff709SChao Yu 			}
13284c8ff709SChao Yu 			err = ret;
1329466357dcSChao Yu 			goto out_err;
13304c8ff709SChao Yu 		}
13314c8ff709SChao Yu 
13324c8ff709SChao Yu 		*submitted += _submitted;
13334c8ff709SChao Yu 	}
13344c8ff709SChao Yu 	return 0;
13354c8ff709SChao Yu out_err:
13364c8ff709SChao Yu 	for (++i; i < cc->cluster_size; i++) {
13374c8ff709SChao Yu 		if (!cc->rpages[i])
13384c8ff709SChao Yu 			continue;
13394c8ff709SChao Yu 		redirty_page_for_writepage(wbc, cc->rpages[i]);
13404c8ff709SChao Yu 		unlock_page(cc->rpages[i]);
13414c8ff709SChao Yu 	}
13424c8ff709SChao Yu 	return err;
13434c8ff709SChao Yu }
13444c8ff709SChao Yu 
13454c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
13464c8ff709SChao Yu 					int *submitted,
13474c8ff709SChao Yu 					struct writeback_control *wbc,
13484c8ff709SChao Yu 					enum iostat_type io_type)
13494c8ff709SChao Yu {
13504c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
13514c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
13524c8ff709SChao Yu 			f2fs_cops[fi->i_compress_algorithm];
13534c8ff709SChao Yu 	int err;
13544c8ff709SChao Yu 
13554c8ff709SChao Yu 	*submitted = 0;
13564c8ff709SChao Yu 	if (cluster_may_compress(cc)) {
13574c8ff709SChao Yu 		err = f2fs_compress_pages(cc);
13584c8ff709SChao Yu 		if (err == -EAGAIN) {
13594c8ff709SChao Yu 			goto write;
13604c8ff709SChao Yu 		} else if (err) {
13614c8ff709SChao Yu 			f2fs_put_rpages_wbc(cc, wbc, true, 1);
13624c8ff709SChao Yu 			goto destroy_out;
13634c8ff709SChao Yu 		}
13644c8ff709SChao Yu 
13654c8ff709SChao Yu 		err = f2fs_write_compressed_pages(cc, submitted,
13664c8ff709SChao Yu 							wbc, io_type);
13674c8ff709SChao Yu 		cops->destroy_compress_ctx(cc);
13684c8ff709SChao Yu 		if (!err)
13694c8ff709SChao Yu 			return 0;
13704c8ff709SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
13714c8ff709SChao Yu 	}
13724c8ff709SChao Yu write:
13734c8ff709SChao Yu 	f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
13744c8ff709SChao Yu 
13754c8ff709SChao Yu 	err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
13764c8ff709SChao Yu 	f2fs_put_rpages_wbc(cc, wbc, false, 0);
13774c8ff709SChao Yu destroy_out:
13784c8ff709SChao Yu 	f2fs_destroy_compress_ctx(cc);
13794c8ff709SChao Yu 	return err;
13804c8ff709SChao Yu }
13814c8ff709SChao Yu 
13824c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
13834c8ff709SChao Yu {
13844c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
13854c8ff709SChao Yu 	struct decompress_io_ctx *dic;
13864c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
13874c8ff709SChao Yu 	int i;
13884c8ff709SChao Yu 
13894c8ff709SChao Yu 	dic = f2fs_kzalloc(sbi, sizeof(struct decompress_io_ctx), GFP_NOFS);
13904c8ff709SChao Yu 	if (!dic)
13914c8ff709SChao Yu 		return ERR_PTR(-ENOMEM);
13924c8ff709SChao Yu 
13934c8ff709SChao Yu 	dic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
13944c8ff709SChao Yu 			cc->log_cluster_size, GFP_NOFS);
13954c8ff709SChao Yu 	if (!dic->rpages) {
13964c8ff709SChao Yu 		kfree(dic);
13974c8ff709SChao Yu 		return ERR_PTR(-ENOMEM);
13984c8ff709SChao Yu 	}
13994c8ff709SChao Yu 
14004c8ff709SChao Yu 	dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
14014c8ff709SChao Yu 	dic->inode = cc->inode;
1402887347a0SChao Yu 	refcount_set(&dic->ref, cc->nr_cpages);
14034c8ff709SChao Yu 	dic->cluster_idx = cc->cluster_idx;
14044c8ff709SChao Yu 	dic->cluster_size = cc->cluster_size;
14054c8ff709SChao Yu 	dic->log_cluster_size = cc->log_cluster_size;
14064c8ff709SChao Yu 	dic->nr_cpages = cc->nr_cpages;
14074c8ff709SChao Yu 	dic->failed = false;
14084c8ff709SChao Yu 
14094c8ff709SChao Yu 	for (i = 0; i < dic->cluster_size; i++)
14104c8ff709SChao Yu 		dic->rpages[i] = cc->rpages[i];
14114c8ff709SChao Yu 	dic->nr_rpages = cc->cluster_size;
14124c8ff709SChao Yu 
14134c8ff709SChao Yu 	dic->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
14144c8ff709SChao Yu 					dic->nr_cpages, GFP_NOFS);
14154c8ff709SChao Yu 	if (!dic->cpages)
14164c8ff709SChao Yu 		goto out_free;
14174c8ff709SChao Yu 
14184c8ff709SChao Yu 	for (i = 0; i < dic->nr_cpages; i++) {
14194c8ff709SChao Yu 		struct page *page;
14204c8ff709SChao Yu 
14215e6bbde9SChao Yu 		page = f2fs_compress_alloc_page();
14224c8ff709SChao Yu 		if (!page)
14234c8ff709SChao Yu 			goto out_free;
14244c8ff709SChao Yu 
14254c8ff709SChao Yu 		f2fs_set_compressed_page(page, cc->inode,
1426887347a0SChao Yu 					start_idx + i + 1, dic);
14274c8ff709SChao Yu 		dic->cpages[i] = page;
14284c8ff709SChao Yu 	}
14294c8ff709SChao Yu 
14304c8ff709SChao Yu 	dic->tpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
14314c8ff709SChao Yu 					dic->cluster_size, GFP_NOFS);
14324c8ff709SChao Yu 	if (!dic->tpages)
14334c8ff709SChao Yu 		goto out_free;
14344c8ff709SChao Yu 
14354c8ff709SChao Yu 	for (i = 0; i < dic->cluster_size; i++) {
143680d0d45aSChao Yu 		if (cc->rpages[i]) {
143780d0d45aSChao Yu 			dic->tpages[i] = cc->rpages[i];
14384c8ff709SChao Yu 			continue;
143980d0d45aSChao Yu 		}
14404c8ff709SChao Yu 
14415e6bbde9SChao Yu 		dic->tpages[i] = f2fs_compress_alloc_page();
14424c8ff709SChao Yu 		if (!dic->tpages[i])
14434c8ff709SChao Yu 			goto out_free;
14444c8ff709SChao Yu 	}
14454c8ff709SChao Yu 
14464c8ff709SChao Yu 	return dic;
14474c8ff709SChao Yu 
14484c8ff709SChao Yu out_free:
14494c8ff709SChao Yu 	f2fs_free_dic(dic);
14504c8ff709SChao Yu 	return ERR_PTR(-ENOMEM);
14514c8ff709SChao Yu }
14524c8ff709SChao Yu 
14534c8ff709SChao Yu void f2fs_free_dic(struct decompress_io_ctx *dic)
14544c8ff709SChao Yu {
14554c8ff709SChao Yu 	int i;
14564c8ff709SChao Yu 
14574c8ff709SChao Yu 	if (dic->tpages) {
14584c8ff709SChao Yu 		for (i = 0; i < dic->cluster_size; i++) {
14594c8ff709SChao Yu 			if (dic->rpages[i])
14604c8ff709SChao Yu 				continue;
14618908e753SChao Yu 			if (!dic->tpages[i])
14628908e753SChao Yu 				continue;
14635e6bbde9SChao Yu 			f2fs_compress_free_page(dic->tpages[i]);
14644c8ff709SChao Yu 		}
14654c8ff709SChao Yu 		kfree(dic->tpages);
14664c8ff709SChao Yu 	}
14674c8ff709SChao Yu 
14684c8ff709SChao Yu 	if (dic->cpages) {
14694c8ff709SChao Yu 		for (i = 0; i < dic->nr_cpages; i++) {
14704c8ff709SChao Yu 			if (!dic->cpages[i])
14714c8ff709SChao Yu 				continue;
14725e6bbde9SChao Yu 			f2fs_compress_free_page(dic->cpages[i]);
14734c8ff709SChao Yu 		}
14744c8ff709SChao Yu 		kfree(dic->cpages);
14754c8ff709SChao Yu 	}
14764c8ff709SChao Yu 
14774c8ff709SChao Yu 	kfree(dic->rpages);
14784c8ff709SChao Yu 	kfree(dic);
14794c8ff709SChao Yu }
14804c8ff709SChao Yu 
14814c8ff709SChao Yu void f2fs_decompress_end_io(struct page **rpages,
14824c8ff709SChao Yu 			unsigned int cluster_size, bool err, bool verity)
14834c8ff709SChao Yu {
14844c8ff709SChao Yu 	int i;
14854c8ff709SChao Yu 
14864c8ff709SChao Yu 	for (i = 0; i < cluster_size; i++) {
14874c8ff709SChao Yu 		struct page *rpage = rpages[i];
14884c8ff709SChao Yu 
14894c8ff709SChao Yu 		if (!rpage)
14904c8ff709SChao Yu 			continue;
14914c8ff709SChao Yu 
149223c51bedSChao Yu 		if (err || PageError(rpage))
149323c51bedSChao Yu 			goto clear_uptodate;
149423c51bedSChao Yu 
149523c51bedSChao Yu 		if (!verity || fsverity_verify_page(rpage)) {
149623c51bedSChao Yu 			SetPageUptodate(rpage);
149723c51bedSChao Yu 			goto unlock;
149823c51bedSChao Yu 		}
149923c51bedSChao Yu clear_uptodate:
15004c8ff709SChao Yu 		ClearPageUptodate(rpage);
15014c8ff709SChao Yu 		ClearPageError(rpage);
150223c51bedSChao Yu unlock:
15034c8ff709SChao Yu 		unlock_page(rpage);
15044c8ff709SChao Yu 	}
15054c8ff709SChao Yu }
1506