xref: /linux/fs/f2fs/compress.c (revision 5e6bbde95982300d66d78fb282d4ee39df78fc33)
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 
924c8ff709SChao Yu static void f2fs_put_rpages_mapping(struct compress_ctx *cc,
934c8ff709SChao Yu 				struct address_space *mapping,
944c8ff709SChao Yu 				pgoff_t start, int len)
954c8ff709SChao Yu {
964c8ff709SChao Yu 	int i;
974c8ff709SChao Yu 
984c8ff709SChao Yu 	for (i = 0; i < len; i++) {
994c8ff709SChao Yu 		struct page *page = find_get_page(mapping, start + i);
1004c8ff709SChao Yu 
1014c8ff709SChao Yu 		put_page(page);
1024c8ff709SChao Yu 		put_page(page);
1034c8ff709SChao Yu 	}
1044c8ff709SChao Yu }
1054c8ff709SChao Yu 
1064c8ff709SChao Yu static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
1074c8ff709SChao Yu 		struct writeback_control *wbc, bool redirty, int unlock)
1084c8ff709SChao Yu {
1094c8ff709SChao Yu 	unsigned int i;
1104c8ff709SChao Yu 
1114c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
1124c8ff709SChao Yu 		if (!cc->rpages[i])
1134c8ff709SChao Yu 			continue;
1144c8ff709SChao Yu 		if (redirty)
1154c8ff709SChao Yu 			redirty_page_for_writepage(wbc, cc->rpages[i]);
1164c8ff709SChao Yu 		f2fs_put_page(cc->rpages[i], unlock);
1174c8ff709SChao Yu 	}
1184c8ff709SChao Yu }
1194c8ff709SChao Yu 
1204c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page)
1214c8ff709SChao Yu {
1224c8ff709SChao Yu 	return ((struct compress_io_ctx *)page_private(page))->rpages[0];
1234c8ff709SChao Yu }
1244c8ff709SChao Yu 
1254c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc)
1264c8ff709SChao Yu {
1274c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1284c8ff709SChao Yu 
1294c8ff709SChao Yu 	if (cc->nr_rpages)
1304c8ff709SChao Yu 		return 0;
1314c8ff709SChao Yu 
1324c8ff709SChao Yu 	cc->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
1334c8ff709SChao Yu 					cc->log_cluster_size, GFP_NOFS);
1344c8ff709SChao Yu 	return cc->rpages ? 0 : -ENOMEM;
1354c8ff709SChao Yu }
1364c8ff709SChao Yu 
1374c8ff709SChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc)
1384c8ff709SChao Yu {
1394c8ff709SChao Yu 	kfree(cc->rpages);
1404c8ff709SChao Yu 	cc->rpages = NULL;
1414c8ff709SChao Yu 	cc->nr_rpages = 0;
1424c8ff709SChao Yu 	cc->nr_cpages = 0;
1434c8ff709SChao Yu 	cc->cluster_idx = NULL_CLUSTER;
1444c8ff709SChao Yu }
1454c8ff709SChao Yu 
1464c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
1474c8ff709SChao Yu {
1484c8ff709SChao Yu 	unsigned int cluster_ofs;
1494c8ff709SChao Yu 
1504c8ff709SChao Yu 	if (!f2fs_cluster_can_merge_page(cc, page->index))
1514c8ff709SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
1524c8ff709SChao Yu 
1534c8ff709SChao Yu 	cluster_ofs = offset_in_cluster(cc, page->index);
1544c8ff709SChao Yu 	cc->rpages[cluster_ofs] = page;
1554c8ff709SChao Yu 	cc->nr_rpages++;
1564c8ff709SChao Yu 	cc->cluster_idx = cluster_idx(cc, page->index);
1574c8ff709SChao Yu }
1584c8ff709SChao Yu 
1594c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO
1604c8ff709SChao Yu static int lzo_init_compress_ctx(struct compress_ctx *cc)
1614c8ff709SChao Yu {
1624c8ff709SChao Yu 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
1634c8ff709SChao Yu 				LZO1X_MEM_COMPRESS, GFP_NOFS);
1644c8ff709SChao Yu 	if (!cc->private)
1654c8ff709SChao Yu 		return -ENOMEM;
1664c8ff709SChao Yu 
1674c8ff709SChao Yu 	cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
1684c8ff709SChao Yu 	return 0;
1694c8ff709SChao Yu }
1704c8ff709SChao Yu 
1714c8ff709SChao Yu static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
1724c8ff709SChao Yu {
1734c8ff709SChao Yu 	kvfree(cc->private);
1744c8ff709SChao Yu 	cc->private = NULL;
1754c8ff709SChao Yu }
1764c8ff709SChao Yu 
1774c8ff709SChao Yu static int lzo_compress_pages(struct compress_ctx *cc)
1784c8ff709SChao Yu {
1794c8ff709SChao Yu 	int ret;
1804c8ff709SChao Yu 
1814c8ff709SChao Yu 	ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
1824c8ff709SChao Yu 					&cc->clen, cc->private);
1834c8ff709SChao Yu 	if (ret != LZO_E_OK) {
1844c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
1854c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
1864c8ff709SChao Yu 		return -EIO;
1874c8ff709SChao Yu 	}
1884c8ff709SChao Yu 	return 0;
1894c8ff709SChao Yu }
1904c8ff709SChao Yu 
1914c8ff709SChao Yu static int lzo_decompress_pages(struct decompress_io_ctx *dic)
1924c8ff709SChao Yu {
1934c8ff709SChao Yu 	int ret;
1944c8ff709SChao Yu 
1954c8ff709SChao Yu 	ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
1964c8ff709SChao Yu 						dic->rbuf, &dic->rlen);
1974c8ff709SChao Yu 	if (ret != LZO_E_OK) {
1984c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
1994c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
2004c8ff709SChao Yu 		return -EIO;
2014c8ff709SChao Yu 	}
2024c8ff709SChao Yu 
2034c8ff709SChao Yu 	if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
2044c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
2054c8ff709SChao Yu 					"expected:%lu\n", KERN_ERR,
2064c8ff709SChao Yu 					F2FS_I_SB(dic->inode)->sb->s_id,
2074c8ff709SChao Yu 					dic->rlen,
2084c8ff709SChao Yu 					PAGE_SIZE << dic->log_cluster_size);
2094c8ff709SChao Yu 		return -EIO;
2104c8ff709SChao Yu 	}
2114c8ff709SChao Yu 	return 0;
2124c8ff709SChao Yu }
2134c8ff709SChao Yu 
2144c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lzo_ops = {
2154c8ff709SChao Yu 	.init_compress_ctx	= lzo_init_compress_ctx,
2164c8ff709SChao Yu 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
2174c8ff709SChao Yu 	.compress_pages		= lzo_compress_pages,
2184c8ff709SChao Yu 	.decompress_pages	= lzo_decompress_pages,
2194c8ff709SChao Yu };
2204c8ff709SChao Yu #endif
2214c8ff709SChao Yu 
2224c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4
2234c8ff709SChao Yu static int lz4_init_compress_ctx(struct compress_ctx *cc)
2244c8ff709SChao Yu {
2254c8ff709SChao Yu 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
2264c8ff709SChao Yu 				LZ4_MEM_COMPRESS, GFP_NOFS);
2274c8ff709SChao Yu 	if (!cc->private)
2284c8ff709SChao Yu 		return -ENOMEM;
2294c8ff709SChao Yu 
2304c8ff709SChao Yu 	cc->clen = LZ4_compressBound(PAGE_SIZE << cc->log_cluster_size);
2314c8ff709SChao Yu 	return 0;
2324c8ff709SChao Yu }
2334c8ff709SChao Yu 
2344c8ff709SChao Yu static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
2354c8ff709SChao Yu {
2364c8ff709SChao Yu 	kvfree(cc->private);
2374c8ff709SChao Yu 	cc->private = NULL;
2384c8ff709SChao Yu }
2394c8ff709SChao Yu 
2404c8ff709SChao Yu static int lz4_compress_pages(struct compress_ctx *cc)
2414c8ff709SChao Yu {
2424c8ff709SChao Yu 	int len;
2434c8ff709SChao Yu 
2444c8ff709SChao Yu 	len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
2454c8ff709SChao Yu 						cc->clen, cc->private);
2464c8ff709SChao Yu 	if (!len) {
2474c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lz4 compress failed\n",
2484c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id);
2494c8ff709SChao Yu 		return -EIO;
2504c8ff709SChao Yu 	}
2514c8ff709SChao Yu 	cc->clen = len;
2524c8ff709SChao Yu 	return 0;
2534c8ff709SChao Yu }
2544c8ff709SChao Yu 
2554c8ff709SChao Yu static int lz4_decompress_pages(struct decompress_io_ctx *dic)
2564c8ff709SChao Yu {
2574c8ff709SChao Yu 	int ret;
2584c8ff709SChao Yu 
2594c8ff709SChao Yu 	ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
2604c8ff709SChao Yu 						dic->clen, dic->rlen);
2614c8ff709SChao Yu 	if (ret < 0) {
2624c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
2634c8ff709SChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
2644c8ff709SChao Yu 		return -EIO;
2654c8ff709SChao Yu 	}
2664c8ff709SChao Yu 
2674c8ff709SChao Yu 	if (ret != PAGE_SIZE << dic->log_cluster_size) {
2684c8ff709SChao Yu 		printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, "
2694c8ff709SChao Yu 					"expected:%lu\n", KERN_ERR,
2704c8ff709SChao Yu 					F2FS_I_SB(dic->inode)->sb->s_id,
2714c8ff709SChao Yu 					dic->rlen,
2724c8ff709SChao Yu 					PAGE_SIZE << dic->log_cluster_size);
2734c8ff709SChao Yu 		return -EIO;
2744c8ff709SChao Yu 	}
2754c8ff709SChao Yu 	return 0;
2764c8ff709SChao Yu }
2774c8ff709SChao Yu 
2784c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lz4_ops = {
2794c8ff709SChao Yu 	.init_compress_ctx	= lz4_init_compress_ctx,
2804c8ff709SChao Yu 	.destroy_compress_ctx	= lz4_destroy_compress_ctx,
2814c8ff709SChao Yu 	.compress_pages		= lz4_compress_pages,
2824c8ff709SChao Yu 	.decompress_pages	= lz4_decompress_pages,
2834c8ff709SChao Yu };
2844c8ff709SChao Yu #endif
2854c8ff709SChao Yu 
28650cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD
28750cfa66fSChao Yu #define F2FS_ZSTD_DEFAULT_CLEVEL	1
28850cfa66fSChao Yu 
28950cfa66fSChao Yu static int zstd_init_compress_ctx(struct compress_ctx *cc)
29050cfa66fSChao Yu {
29150cfa66fSChao Yu 	ZSTD_parameters params;
29250cfa66fSChao Yu 	ZSTD_CStream *stream;
29350cfa66fSChao Yu 	void *workspace;
29450cfa66fSChao Yu 	unsigned int workspace_size;
29550cfa66fSChao Yu 
29650cfa66fSChao Yu 	params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen, 0);
29750cfa66fSChao Yu 	workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
29850cfa66fSChao Yu 
29950cfa66fSChao Yu 	workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
30050cfa66fSChao Yu 					workspace_size, GFP_NOFS);
30150cfa66fSChao Yu 	if (!workspace)
30250cfa66fSChao Yu 		return -ENOMEM;
30350cfa66fSChao Yu 
30450cfa66fSChao Yu 	stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
30550cfa66fSChao Yu 	if (!stream) {
30650cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n",
30750cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
30850cfa66fSChao Yu 				__func__);
30950cfa66fSChao Yu 		kvfree(workspace);
31050cfa66fSChao Yu 		return -EIO;
31150cfa66fSChao Yu 	}
31250cfa66fSChao Yu 
31350cfa66fSChao Yu 	cc->private = workspace;
31450cfa66fSChao Yu 	cc->private2 = stream;
31550cfa66fSChao Yu 
31650cfa66fSChao Yu 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
31750cfa66fSChao Yu 	return 0;
31850cfa66fSChao Yu }
31950cfa66fSChao Yu 
32050cfa66fSChao Yu static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
32150cfa66fSChao Yu {
32250cfa66fSChao Yu 	kvfree(cc->private);
32350cfa66fSChao Yu 	cc->private = NULL;
32450cfa66fSChao Yu 	cc->private2 = NULL;
32550cfa66fSChao Yu }
32650cfa66fSChao Yu 
32750cfa66fSChao Yu static int zstd_compress_pages(struct compress_ctx *cc)
32850cfa66fSChao Yu {
32950cfa66fSChao Yu 	ZSTD_CStream *stream = cc->private2;
33050cfa66fSChao Yu 	ZSTD_inBuffer inbuf;
33150cfa66fSChao Yu 	ZSTD_outBuffer outbuf;
33250cfa66fSChao Yu 	int src_size = cc->rlen;
33350cfa66fSChao Yu 	int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
33450cfa66fSChao Yu 	int ret;
33550cfa66fSChao Yu 
33650cfa66fSChao Yu 	inbuf.pos = 0;
33750cfa66fSChao Yu 	inbuf.src = cc->rbuf;
33850cfa66fSChao Yu 	inbuf.size = src_size;
33950cfa66fSChao Yu 
34050cfa66fSChao Yu 	outbuf.pos = 0;
34150cfa66fSChao Yu 	outbuf.dst = cc->cbuf->cdata;
34250cfa66fSChao Yu 	outbuf.size = dst_size;
34350cfa66fSChao Yu 
34450cfa66fSChao Yu 	ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
34550cfa66fSChao Yu 	if (ZSTD_isError(ret)) {
34650cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
34750cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
34850cfa66fSChao Yu 				__func__, ZSTD_getErrorCode(ret));
34950cfa66fSChao Yu 		return -EIO;
35050cfa66fSChao Yu 	}
35150cfa66fSChao Yu 
35250cfa66fSChao Yu 	ret = ZSTD_endStream(stream, &outbuf);
35350cfa66fSChao Yu 	if (ZSTD_isError(ret)) {
35450cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n",
35550cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
35650cfa66fSChao Yu 				__func__, ZSTD_getErrorCode(ret));
35750cfa66fSChao Yu 		return -EIO;
35850cfa66fSChao Yu 	}
35950cfa66fSChao Yu 
36050cfa66fSChao Yu 	cc->clen = outbuf.pos;
36150cfa66fSChao Yu 	return 0;
36250cfa66fSChao Yu }
36350cfa66fSChao Yu 
36450cfa66fSChao Yu static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
36550cfa66fSChao Yu {
36650cfa66fSChao Yu 	ZSTD_DStream *stream;
36750cfa66fSChao Yu 	void *workspace;
36850cfa66fSChao Yu 	unsigned int workspace_size;
36950cfa66fSChao Yu 
37050cfa66fSChao Yu 	workspace_size = ZSTD_DStreamWorkspaceBound(MAX_COMPRESS_WINDOW_SIZE);
37150cfa66fSChao Yu 
37250cfa66fSChao Yu 	workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
37350cfa66fSChao Yu 					workspace_size, GFP_NOFS);
37450cfa66fSChao Yu 	if (!workspace)
37550cfa66fSChao Yu 		return -ENOMEM;
37650cfa66fSChao Yu 
37750cfa66fSChao Yu 	stream = ZSTD_initDStream(MAX_COMPRESS_WINDOW_SIZE,
37850cfa66fSChao Yu 					workspace, workspace_size);
37950cfa66fSChao Yu 	if (!stream) {
38050cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n",
38150cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
38250cfa66fSChao Yu 				__func__);
38350cfa66fSChao Yu 		kvfree(workspace);
38450cfa66fSChao Yu 		return -EIO;
38550cfa66fSChao Yu 	}
38650cfa66fSChao Yu 
38750cfa66fSChao Yu 	dic->private = workspace;
38850cfa66fSChao Yu 	dic->private2 = stream;
38950cfa66fSChao Yu 
39050cfa66fSChao Yu 	return 0;
39150cfa66fSChao Yu }
39250cfa66fSChao Yu 
39350cfa66fSChao Yu static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
39450cfa66fSChao Yu {
39550cfa66fSChao Yu 	kvfree(dic->private);
39650cfa66fSChao Yu 	dic->private = NULL;
39750cfa66fSChao Yu 	dic->private2 = NULL;
39850cfa66fSChao Yu }
39950cfa66fSChao Yu 
40050cfa66fSChao Yu static int zstd_decompress_pages(struct decompress_io_ctx *dic)
40150cfa66fSChao Yu {
40250cfa66fSChao Yu 	ZSTD_DStream *stream = dic->private2;
40350cfa66fSChao Yu 	ZSTD_inBuffer inbuf;
40450cfa66fSChao Yu 	ZSTD_outBuffer outbuf;
40550cfa66fSChao Yu 	int ret;
40650cfa66fSChao Yu 
40750cfa66fSChao Yu 	inbuf.pos = 0;
40850cfa66fSChao Yu 	inbuf.src = dic->cbuf->cdata;
40950cfa66fSChao Yu 	inbuf.size = dic->clen;
41050cfa66fSChao Yu 
41150cfa66fSChao Yu 	outbuf.pos = 0;
41250cfa66fSChao Yu 	outbuf.dst = dic->rbuf;
41350cfa66fSChao Yu 	outbuf.size = dic->rlen;
41450cfa66fSChao Yu 
41550cfa66fSChao Yu 	ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
41650cfa66fSChao Yu 	if (ZSTD_isError(ret)) {
41750cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
41850cfa66fSChao Yu 				KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
41950cfa66fSChao Yu 				__func__, ZSTD_getErrorCode(ret));
42050cfa66fSChao Yu 		return -EIO;
42150cfa66fSChao Yu 	}
42250cfa66fSChao Yu 
42350cfa66fSChao Yu 	if (dic->rlen != outbuf.pos) {
42450cfa66fSChao Yu 		printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
42550cfa66fSChao Yu 				"expected:%lu\n", KERN_ERR,
42650cfa66fSChao Yu 				F2FS_I_SB(dic->inode)->sb->s_id,
42750cfa66fSChao Yu 				__func__, dic->rlen,
42850cfa66fSChao Yu 				PAGE_SIZE << dic->log_cluster_size);
42950cfa66fSChao Yu 		return -EIO;
43050cfa66fSChao Yu 	}
43150cfa66fSChao Yu 
43250cfa66fSChao Yu 	return 0;
43350cfa66fSChao Yu }
43450cfa66fSChao Yu 
43550cfa66fSChao Yu static const struct f2fs_compress_ops f2fs_zstd_ops = {
43650cfa66fSChao Yu 	.init_compress_ctx	= zstd_init_compress_ctx,
43750cfa66fSChao Yu 	.destroy_compress_ctx	= zstd_destroy_compress_ctx,
43850cfa66fSChao Yu 	.compress_pages		= zstd_compress_pages,
43950cfa66fSChao Yu 	.init_decompress_ctx	= zstd_init_decompress_ctx,
44050cfa66fSChao Yu 	.destroy_decompress_ctx	= zstd_destroy_decompress_ctx,
44150cfa66fSChao Yu 	.decompress_pages	= zstd_decompress_pages,
44250cfa66fSChao Yu };
44350cfa66fSChao Yu #endif
44450cfa66fSChao Yu 
4454c8ff709SChao Yu static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
4464c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO
4474c8ff709SChao Yu 	&f2fs_lzo_ops,
4484c8ff709SChao Yu #else
4494c8ff709SChao Yu 	NULL,
4504c8ff709SChao Yu #endif
4514c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4
4524c8ff709SChao Yu 	&f2fs_lz4_ops,
4534c8ff709SChao Yu #else
4544c8ff709SChao Yu 	NULL,
4554c8ff709SChao Yu #endif
45650cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD
45750cfa66fSChao Yu 	&f2fs_zstd_ops,
45850cfa66fSChao Yu #else
45950cfa66fSChao Yu 	NULL,
46050cfa66fSChao Yu #endif
4614c8ff709SChao Yu };
4624c8ff709SChao Yu 
4634c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode)
4644c8ff709SChao Yu {
4654c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
4664c8ff709SChao Yu 		return true;
4674c8ff709SChao Yu 	return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
4684c8ff709SChao Yu }
4694c8ff709SChao Yu 
470*5e6bbde9SChao Yu static mempool_t *compress_page_pool = NULL;
471*5e6bbde9SChao Yu static int num_compress_pages = 512;
472*5e6bbde9SChao Yu module_param(num_compress_pages, uint, 0444);
473*5e6bbde9SChao Yu MODULE_PARM_DESC(num_compress_pages,
474*5e6bbde9SChao Yu 		"Number of intermediate compress pages to preallocate");
475*5e6bbde9SChao Yu 
476*5e6bbde9SChao Yu int f2fs_init_compress_mempool(void)
477*5e6bbde9SChao Yu {
478*5e6bbde9SChao Yu 	compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
479*5e6bbde9SChao Yu 	if (!compress_page_pool)
480*5e6bbde9SChao Yu 		return -ENOMEM;
481*5e6bbde9SChao Yu 
482*5e6bbde9SChao Yu 	return 0;
483*5e6bbde9SChao Yu }
484*5e6bbde9SChao Yu 
485*5e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void)
486*5e6bbde9SChao Yu {
487*5e6bbde9SChao Yu 	mempool_destroy(compress_page_pool);
488*5e6bbde9SChao Yu }
489*5e6bbde9SChao Yu 
490*5e6bbde9SChao Yu static struct page *f2fs_compress_alloc_page(void)
4914c8ff709SChao Yu {
4924c8ff709SChao Yu 	struct page *page;
4934c8ff709SChao Yu 
494*5e6bbde9SChao Yu 	page = mempool_alloc(compress_page_pool, GFP_NOFS);
4954c8ff709SChao Yu 	lock_page(page);
496*5e6bbde9SChao Yu 
4974c8ff709SChao Yu 	return page;
4984c8ff709SChao Yu }
4994c8ff709SChao Yu 
500*5e6bbde9SChao Yu static void f2fs_compress_free_page(struct page *page)
501*5e6bbde9SChao Yu {
502*5e6bbde9SChao Yu 	if (!page)
503*5e6bbde9SChao Yu 		return;
504*5e6bbde9SChao Yu 	set_page_private(page, (unsigned long)NULL);
505*5e6bbde9SChao Yu 	ClearPagePrivate(page);
506*5e6bbde9SChao Yu 	page->mapping = NULL;
507*5e6bbde9SChao Yu 	unlock_page(page);
508*5e6bbde9SChao Yu 	mempool_free(page, compress_page_pool);
509*5e6bbde9SChao Yu }
510*5e6bbde9SChao Yu 
5114c8ff709SChao Yu static int f2fs_compress_pages(struct compress_ctx *cc)
5124c8ff709SChao Yu {
5134c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
5144c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
5154c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
5164c8ff709SChao Yu 				f2fs_cops[fi->i_compress_algorithm];
5174c8ff709SChao Yu 	unsigned int max_len, nr_cpages;
5184c8ff709SChao Yu 	int i, ret;
5194c8ff709SChao Yu 
5204c8ff709SChao Yu 	trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
5214c8ff709SChao Yu 				cc->cluster_size, fi->i_compress_algorithm);
5224c8ff709SChao Yu 
52323b1faaaSChao Yu 	if (cops->init_compress_ctx) {
5244c8ff709SChao Yu 		ret = cops->init_compress_ctx(cc);
5254c8ff709SChao Yu 		if (ret)
5264c8ff709SChao Yu 			goto out;
52723b1faaaSChao Yu 	}
5284c8ff709SChao Yu 
5294c8ff709SChao Yu 	max_len = COMPRESS_HEADER_SIZE + cc->clen;
5304c8ff709SChao Yu 	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
5314c8ff709SChao Yu 
5324c8ff709SChao Yu 	cc->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
5334c8ff709SChao Yu 					cc->nr_cpages, GFP_NOFS);
5344c8ff709SChao Yu 	if (!cc->cpages) {
5354c8ff709SChao Yu 		ret = -ENOMEM;
5364c8ff709SChao Yu 		goto destroy_compress_ctx;
5374c8ff709SChao Yu 	}
5384c8ff709SChao Yu 
5394c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
540*5e6bbde9SChao Yu 		cc->cpages[i] = f2fs_compress_alloc_page();
5414c8ff709SChao Yu 		if (!cc->cpages[i]) {
5424c8ff709SChao Yu 			ret = -ENOMEM;
5434c8ff709SChao Yu 			goto out_free_cpages;
5444c8ff709SChao Yu 		}
5454c8ff709SChao Yu 	}
5464c8ff709SChao Yu 
5474c8ff709SChao Yu 	cc->rbuf = vmap(cc->rpages, cc->cluster_size, VM_MAP, PAGE_KERNEL_RO);
5484c8ff709SChao Yu 	if (!cc->rbuf) {
5494c8ff709SChao Yu 		ret = -ENOMEM;
5504c8ff709SChao Yu 		goto out_free_cpages;
5514c8ff709SChao Yu 	}
5524c8ff709SChao Yu 
5534c8ff709SChao Yu 	cc->cbuf = vmap(cc->cpages, cc->nr_cpages, VM_MAP, PAGE_KERNEL);
5544c8ff709SChao Yu 	if (!cc->cbuf) {
5554c8ff709SChao Yu 		ret = -ENOMEM;
5564c8ff709SChao Yu 		goto out_vunmap_rbuf;
5574c8ff709SChao Yu 	}
5584c8ff709SChao Yu 
5594c8ff709SChao Yu 	ret = cops->compress_pages(cc);
5604c8ff709SChao Yu 	if (ret)
5614c8ff709SChao Yu 		goto out_vunmap_cbuf;
5624c8ff709SChao Yu 
5634c8ff709SChao Yu 	max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
5644c8ff709SChao Yu 
5654c8ff709SChao Yu 	if (cc->clen > max_len) {
5664c8ff709SChao Yu 		ret = -EAGAIN;
5674c8ff709SChao Yu 		goto out_vunmap_cbuf;
5684c8ff709SChao Yu 	}
5694c8ff709SChao Yu 
5704c8ff709SChao Yu 	cc->cbuf->clen = cpu_to_le32(cc->clen);
5714c8ff709SChao Yu 
5724c8ff709SChao Yu 	for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
5734c8ff709SChao Yu 		cc->cbuf->reserved[i] = cpu_to_le32(0);
5744c8ff709SChao Yu 
5757fa6d598SEric Biggers 	nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
5767fa6d598SEric Biggers 
5777fa6d598SEric Biggers 	/* zero out any unused part of the last page */
5787fa6d598SEric Biggers 	memset(&cc->cbuf->cdata[cc->clen], 0,
5797fa6d598SEric Biggers 	       (nr_cpages * PAGE_SIZE) - (cc->clen + COMPRESS_HEADER_SIZE));
5807fa6d598SEric Biggers 
5814c8ff709SChao Yu 	vunmap(cc->cbuf);
5824c8ff709SChao Yu 	vunmap(cc->rbuf);
5834c8ff709SChao Yu 
5844c8ff709SChao Yu 	for (i = nr_cpages; i < cc->nr_cpages; i++) {
585*5e6bbde9SChao Yu 		f2fs_compress_free_page(cc->cpages[i]);
5864c8ff709SChao Yu 		cc->cpages[i] = NULL;
5874c8ff709SChao Yu 	}
5884c8ff709SChao Yu 
58923b1faaaSChao Yu 	if (cops->destroy_compress_ctx)
59009ff4801SChao Yu 		cops->destroy_compress_ctx(cc);
59109ff4801SChao Yu 
5924c8ff709SChao Yu 	cc->nr_cpages = nr_cpages;
5934c8ff709SChao Yu 
5944c8ff709SChao Yu 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
5954c8ff709SChao Yu 							cc->clen, ret);
5964c8ff709SChao Yu 	return 0;
5974c8ff709SChao Yu 
5984c8ff709SChao Yu out_vunmap_cbuf:
5994c8ff709SChao Yu 	vunmap(cc->cbuf);
6004c8ff709SChao Yu out_vunmap_rbuf:
6014c8ff709SChao Yu 	vunmap(cc->rbuf);
6024c8ff709SChao Yu out_free_cpages:
6034c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
6044c8ff709SChao Yu 		if (cc->cpages[i])
605*5e6bbde9SChao Yu 			f2fs_compress_free_page(cc->cpages[i]);
6064c8ff709SChao Yu 	}
6074c8ff709SChao Yu 	kfree(cc->cpages);
6084c8ff709SChao Yu 	cc->cpages = NULL;
6094c8ff709SChao Yu destroy_compress_ctx:
61023b1faaaSChao Yu 	if (cops->destroy_compress_ctx)
6114c8ff709SChao Yu 		cops->destroy_compress_ctx(cc);
6124c8ff709SChao Yu out:
6134c8ff709SChao Yu 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
6144c8ff709SChao Yu 							cc->clen, ret);
6154c8ff709SChao Yu 	return ret;
6164c8ff709SChao Yu }
6174c8ff709SChao Yu 
6184c8ff709SChao Yu void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity)
6194c8ff709SChao Yu {
6204c8ff709SChao Yu 	struct decompress_io_ctx *dic =
6214c8ff709SChao Yu 			(struct decompress_io_ctx *)page_private(page);
6224c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
6234c8ff709SChao Yu 	struct f2fs_inode_info *fi= F2FS_I(dic->inode);
6244c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
6254c8ff709SChao Yu 			f2fs_cops[fi->i_compress_algorithm];
6264c8ff709SChao Yu 	int ret;
6274c8ff709SChao Yu 
6284c8ff709SChao Yu 	dec_page_count(sbi, F2FS_RD_DATA);
6294c8ff709SChao Yu 
6304c8ff709SChao Yu 	if (bio->bi_status || PageError(page))
6314c8ff709SChao Yu 		dic->failed = true;
6324c8ff709SChao Yu 
6334c8ff709SChao Yu 	if (refcount_dec_not_one(&dic->ref))
6344c8ff709SChao Yu 		return;
6354c8ff709SChao Yu 
6364c8ff709SChao Yu 	trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
6374c8ff709SChao Yu 				dic->cluster_size, fi->i_compress_algorithm);
6384c8ff709SChao Yu 
6394c8ff709SChao Yu 	/* submit partial compressed pages */
6404c8ff709SChao Yu 	if (dic->failed) {
6414c8ff709SChao Yu 		ret = -EIO;
6424c8ff709SChao Yu 		goto out_free_dic;
6434c8ff709SChao Yu 	}
6444c8ff709SChao Yu 
64523b1faaaSChao Yu 	if (cops->init_decompress_ctx) {
64623b1faaaSChao Yu 		ret = cops->init_decompress_ctx(dic);
64723b1faaaSChao Yu 		if (ret)
64823b1faaaSChao Yu 			goto out_free_dic;
64923b1faaaSChao Yu 	}
65023b1faaaSChao Yu 
6514c8ff709SChao Yu 	dic->rbuf = vmap(dic->tpages, dic->cluster_size, VM_MAP, PAGE_KERNEL);
6524c8ff709SChao Yu 	if (!dic->rbuf) {
6534c8ff709SChao Yu 		ret = -ENOMEM;
65423b1faaaSChao Yu 		goto destroy_decompress_ctx;
6554c8ff709SChao Yu 	}
6564c8ff709SChao Yu 
6574c8ff709SChao Yu 	dic->cbuf = vmap(dic->cpages, dic->nr_cpages, VM_MAP, PAGE_KERNEL_RO);
6584c8ff709SChao Yu 	if (!dic->cbuf) {
6594c8ff709SChao Yu 		ret = -ENOMEM;
6604c8ff709SChao Yu 		goto out_vunmap_rbuf;
6614c8ff709SChao Yu 	}
6624c8ff709SChao Yu 
6634c8ff709SChao Yu 	dic->clen = le32_to_cpu(dic->cbuf->clen);
6644c8ff709SChao Yu 	dic->rlen = PAGE_SIZE << dic->log_cluster_size;
6654c8ff709SChao Yu 
6664c8ff709SChao Yu 	if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
6674c8ff709SChao Yu 		ret = -EFSCORRUPTED;
6684c8ff709SChao Yu 		goto out_vunmap_cbuf;
6694c8ff709SChao Yu 	}
6704c8ff709SChao Yu 
6714c8ff709SChao Yu 	ret = cops->decompress_pages(dic);
6724c8ff709SChao Yu 
6734c8ff709SChao Yu out_vunmap_cbuf:
6744c8ff709SChao Yu 	vunmap(dic->cbuf);
6754c8ff709SChao Yu out_vunmap_rbuf:
6764c8ff709SChao Yu 	vunmap(dic->rbuf);
67723b1faaaSChao Yu destroy_decompress_ctx:
67823b1faaaSChao Yu 	if (cops->destroy_decompress_ctx)
67923b1faaaSChao Yu 		cops->destroy_decompress_ctx(dic);
6804c8ff709SChao Yu out_free_dic:
68179bbefb1SChao Yu 	if (verity)
682887347a0SChao Yu 		refcount_set(&dic->ref, dic->nr_cpages);
6834c8ff709SChao Yu 	if (!verity)
6844c8ff709SChao Yu 		f2fs_decompress_end_io(dic->rpages, dic->cluster_size,
6854c8ff709SChao Yu 								ret, false);
6864c8ff709SChao Yu 
6874c8ff709SChao Yu 	trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
6884c8ff709SChao Yu 							dic->clen, ret);
6894c8ff709SChao Yu 	if (!verity)
6904c8ff709SChao Yu 		f2fs_free_dic(dic);
6914c8ff709SChao Yu }
6924c8ff709SChao Yu 
6934c8ff709SChao Yu static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
6944c8ff709SChao Yu {
6954c8ff709SChao Yu 	if (cc->cluster_idx == NULL_CLUSTER)
6964c8ff709SChao Yu 		return true;
6974c8ff709SChao Yu 	return cc->cluster_idx == cluster_idx(cc, index);
6984c8ff709SChao Yu }
6994c8ff709SChao Yu 
7004c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc)
7014c8ff709SChao Yu {
7024c8ff709SChao Yu 	return cc->nr_rpages == 0;
7034c8ff709SChao Yu }
7044c8ff709SChao Yu 
7054c8ff709SChao Yu static bool f2fs_cluster_is_full(struct compress_ctx *cc)
7064c8ff709SChao Yu {
7074c8ff709SChao Yu 	return cc->cluster_size == cc->nr_rpages;
7084c8ff709SChao Yu }
7094c8ff709SChao Yu 
7104c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
7114c8ff709SChao Yu {
7124c8ff709SChao Yu 	if (f2fs_cluster_is_empty(cc))
7134c8ff709SChao Yu 		return true;
7144c8ff709SChao Yu 	return is_page_in_cluster(cc, index);
7154c8ff709SChao Yu }
7164c8ff709SChao Yu 
7174c8ff709SChao Yu static bool __cluster_may_compress(struct compress_ctx *cc)
7184c8ff709SChao Yu {
7194c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
7204c8ff709SChao Yu 	loff_t i_size = i_size_read(cc->inode);
7214c8ff709SChao Yu 	unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
7224c8ff709SChao Yu 	int i;
7234c8ff709SChao Yu 
7244c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
7254c8ff709SChao Yu 		struct page *page = cc->rpages[i];
7264c8ff709SChao Yu 
7274c8ff709SChao Yu 		f2fs_bug_on(sbi, !page);
7284c8ff709SChao Yu 
7294c8ff709SChao Yu 		if (unlikely(f2fs_cp_error(sbi)))
7304c8ff709SChao Yu 			return false;
7314c8ff709SChao Yu 		if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
7324c8ff709SChao Yu 			return false;
7334c8ff709SChao Yu 
7344c8ff709SChao Yu 		/* beyond EOF */
7354c8ff709SChao Yu 		if (page->index >= nr_pages)
7364c8ff709SChao Yu 			return false;
7374c8ff709SChao Yu 	}
7384c8ff709SChao Yu 	return true;
7394c8ff709SChao Yu }
7404c8ff709SChao Yu 
7411a67cbe1SChao Yu static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
7424c8ff709SChao Yu {
7434c8ff709SChao Yu 	struct dnode_of_data dn;
7444c8ff709SChao Yu 	int ret;
7454c8ff709SChao Yu 
7464c8ff709SChao Yu 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
7474c8ff709SChao Yu 	ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc),
7484c8ff709SChao Yu 							LOOKUP_NODE);
7494c8ff709SChao Yu 	if (ret) {
7504c8ff709SChao Yu 		if (ret == -ENOENT)
7514c8ff709SChao Yu 			ret = 0;
7524c8ff709SChao Yu 		goto fail;
7534c8ff709SChao Yu 	}
7544c8ff709SChao Yu 
7554c8ff709SChao Yu 	if (dn.data_blkaddr == COMPRESS_ADDR) {
7564c8ff709SChao Yu 		int i;
7574c8ff709SChao Yu 
7584c8ff709SChao Yu 		ret = 1;
7594c8ff709SChao Yu 		for (i = 1; i < cc->cluster_size; i++) {
7604c8ff709SChao Yu 			block_t blkaddr;
7614c8ff709SChao Yu 
762a2ced1ceSChao Yu 			blkaddr = data_blkaddr(dn.inode,
7634c8ff709SChao Yu 					dn.node_page, dn.ofs_in_node + i);
7641a67cbe1SChao Yu 			if (compr) {
7651a67cbe1SChao Yu 				if (__is_valid_data_blkaddr(blkaddr))
7661a67cbe1SChao Yu 					ret++;
7671a67cbe1SChao Yu 			} else {
7684c8ff709SChao Yu 				if (blkaddr != NULL_ADDR)
7694c8ff709SChao Yu 					ret++;
7704c8ff709SChao Yu 			}
7714c8ff709SChao Yu 		}
7721a67cbe1SChao Yu 	}
7734c8ff709SChao Yu fail:
7744c8ff709SChao Yu 	f2fs_put_dnode(&dn);
7754c8ff709SChao Yu 	return ret;
7764c8ff709SChao Yu }
7774c8ff709SChao Yu 
7781a67cbe1SChao Yu /* return # of compressed blocks in compressed cluster */
7791a67cbe1SChao Yu static int f2fs_compressed_blocks(struct compress_ctx *cc)
7801a67cbe1SChao Yu {
7811a67cbe1SChao Yu 	return __f2fs_cluster_blocks(cc, true);
7821a67cbe1SChao Yu }
7831a67cbe1SChao Yu 
7841a67cbe1SChao Yu /* return # of valid blocks in compressed cluster */
7851a67cbe1SChao Yu static int f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
7861a67cbe1SChao Yu {
7871a67cbe1SChao Yu 	return __f2fs_cluster_blocks(cc, false);
7881a67cbe1SChao Yu }
7891a67cbe1SChao Yu 
7904c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
7914c8ff709SChao Yu {
7924c8ff709SChao Yu 	struct compress_ctx cc = {
7934c8ff709SChao Yu 		.inode = inode,
7944c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
7954c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
7964c8ff709SChao Yu 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
7974c8ff709SChao Yu 	};
7984c8ff709SChao Yu 
7991a67cbe1SChao Yu 	return f2fs_cluster_blocks(&cc, false);
8004c8ff709SChao Yu }
8014c8ff709SChao Yu 
8024c8ff709SChao Yu static bool cluster_may_compress(struct compress_ctx *cc)
8034c8ff709SChao Yu {
8044c8ff709SChao Yu 	if (!f2fs_compressed_file(cc->inode))
8054c8ff709SChao Yu 		return false;
8064c8ff709SChao Yu 	if (f2fs_is_atomic_file(cc->inode))
8074c8ff709SChao Yu 		return false;
8084c8ff709SChao Yu 	if (f2fs_is_mmap_file(cc->inode))
8094c8ff709SChao Yu 		return false;
8104c8ff709SChao Yu 	if (!f2fs_cluster_is_full(cc))
8114c8ff709SChao Yu 		return false;
8124c8ff709SChao Yu 	return __cluster_may_compress(cc);
8134c8ff709SChao Yu }
8144c8ff709SChao Yu 
8154c8ff709SChao Yu static void set_cluster_writeback(struct compress_ctx *cc)
8164c8ff709SChao Yu {
8174c8ff709SChao Yu 	int i;
8184c8ff709SChao Yu 
8194c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
8204c8ff709SChao Yu 		if (cc->rpages[i])
8214c8ff709SChao Yu 			set_page_writeback(cc->rpages[i]);
8224c8ff709SChao Yu 	}
8234c8ff709SChao Yu }
8244c8ff709SChao Yu 
8254c8ff709SChao Yu static void set_cluster_dirty(struct compress_ctx *cc)
8264c8ff709SChao Yu {
8274c8ff709SChao Yu 	int i;
8284c8ff709SChao Yu 
8294c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++)
8304c8ff709SChao Yu 		if (cc->rpages[i])
8314c8ff709SChao Yu 			set_page_dirty(cc->rpages[i]);
8324c8ff709SChao Yu }
8334c8ff709SChao Yu 
8344c8ff709SChao Yu static int prepare_compress_overwrite(struct compress_ctx *cc,
8354c8ff709SChao Yu 		struct page **pagep, pgoff_t index, void **fsdata)
8364c8ff709SChao Yu {
8374c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
8384c8ff709SChao Yu 	struct address_space *mapping = cc->inode->i_mapping;
8394c8ff709SChao Yu 	struct page *page;
8404c8ff709SChao Yu 	struct dnode_of_data dn;
8414c8ff709SChao Yu 	sector_t last_block_in_bio;
8424c8ff709SChao Yu 	unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
8434c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
8444c8ff709SChao Yu 	int i, ret;
8454c8ff709SChao Yu 	bool prealloc;
8464c8ff709SChao Yu 
8474c8ff709SChao Yu retry:
8481a67cbe1SChao Yu 	ret = f2fs_cluster_blocks(cc, false);
8494c8ff709SChao Yu 	if (ret <= 0)
8504c8ff709SChao Yu 		return ret;
8514c8ff709SChao Yu 
8524c8ff709SChao Yu 	/* compressed case */
8534c8ff709SChao Yu 	prealloc = (ret < cc->cluster_size);
8544c8ff709SChao Yu 
8554c8ff709SChao Yu 	ret = f2fs_init_compress_ctx(cc);
8564c8ff709SChao Yu 	if (ret)
8574c8ff709SChao Yu 		return ret;
8584c8ff709SChao Yu 
8594c8ff709SChao Yu 	/* keep page reference to avoid page reclaim */
8604c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
8614c8ff709SChao Yu 		page = f2fs_pagecache_get_page(mapping, start_idx + i,
8624c8ff709SChao Yu 							fgp_flag, GFP_NOFS);
8634c8ff709SChao Yu 		if (!page) {
8644c8ff709SChao Yu 			ret = -ENOMEM;
8654c8ff709SChao Yu 			goto unlock_pages;
8664c8ff709SChao Yu 		}
8674c8ff709SChao Yu 
8684c8ff709SChao Yu 		if (PageUptodate(page))
8694c8ff709SChao Yu 			unlock_page(page);
8704c8ff709SChao Yu 		else
8714c8ff709SChao Yu 			f2fs_compress_ctx_add_page(cc, page);
8724c8ff709SChao Yu 	}
8734c8ff709SChao Yu 
8744c8ff709SChao Yu 	if (!f2fs_cluster_is_empty(cc)) {
8754c8ff709SChao Yu 		struct bio *bio = NULL;
8764c8ff709SChao Yu 
8774c8ff709SChao Yu 		ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
8780683728aSChao Yu 					&last_block_in_bio, false, true);
8794c8ff709SChao Yu 		f2fs_destroy_compress_ctx(cc);
8804c8ff709SChao Yu 		if (ret)
8814c8ff709SChao Yu 			goto release_pages;
8824c8ff709SChao Yu 		if (bio)
8834c8ff709SChao Yu 			f2fs_submit_bio(sbi, bio, DATA);
8844c8ff709SChao Yu 
8854c8ff709SChao Yu 		ret = f2fs_init_compress_ctx(cc);
8864c8ff709SChao Yu 		if (ret)
8874c8ff709SChao Yu 			goto release_pages;
8884c8ff709SChao Yu 	}
8894c8ff709SChao Yu 
8904c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
8914c8ff709SChao Yu 		f2fs_bug_on(sbi, cc->rpages[i]);
8924c8ff709SChao Yu 
8934c8ff709SChao Yu 		page = find_lock_page(mapping, start_idx + i);
8944c8ff709SChao Yu 		f2fs_bug_on(sbi, !page);
8954c8ff709SChao Yu 
8964c8ff709SChao Yu 		f2fs_wait_on_page_writeback(page, DATA, true, true);
8974c8ff709SChao Yu 
8984c8ff709SChao Yu 		f2fs_compress_ctx_add_page(cc, page);
8994c8ff709SChao Yu 		f2fs_put_page(page, 0);
9004c8ff709SChao Yu 
9014c8ff709SChao Yu 		if (!PageUptodate(page)) {
9024c8ff709SChao Yu 			f2fs_unlock_rpages(cc, i + 1);
9034c8ff709SChao Yu 			f2fs_put_rpages_mapping(cc, mapping, start_idx,
9044c8ff709SChao Yu 					cc->cluster_size);
9054c8ff709SChao Yu 			f2fs_destroy_compress_ctx(cc);
9064c8ff709SChao Yu 			goto retry;
9074c8ff709SChao Yu 		}
9084c8ff709SChao Yu 	}
9094c8ff709SChao Yu 
9104c8ff709SChao Yu 	if (prealloc) {
9114c8ff709SChao Yu 		__do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
9124c8ff709SChao Yu 
9134c8ff709SChao Yu 		set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
9144c8ff709SChao Yu 
9154c8ff709SChao Yu 		for (i = cc->cluster_size - 1; i > 0; i--) {
9164c8ff709SChao Yu 			ret = f2fs_get_block(&dn, start_idx + i);
9174c8ff709SChao Yu 			if (ret) {
9184c8ff709SChao Yu 				i = cc->cluster_size;
9194c8ff709SChao Yu 				break;
9204c8ff709SChao Yu 			}
9214c8ff709SChao Yu 
9224c8ff709SChao Yu 			if (dn.data_blkaddr != NEW_ADDR)
9234c8ff709SChao Yu 				break;
9244c8ff709SChao Yu 		}
9254c8ff709SChao Yu 
9264c8ff709SChao Yu 		__do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
9274c8ff709SChao Yu 	}
9284c8ff709SChao Yu 
9294c8ff709SChao Yu 	if (likely(!ret)) {
9304c8ff709SChao Yu 		*fsdata = cc->rpages;
9314c8ff709SChao Yu 		*pagep = cc->rpages[offset_in_cluster(cc, index)];
9324c8ff709SChao Yu 		return cc->cluster_size;
9334c8ff709SChao Yu 	}
9344c8ff709SChao Yu 
9354c8ff709SChao Yu unlock_pages:
9364c8ff709SChao Yu 	f2fs_unlock_rpages(cc, i);
9374c8ff709SChao Yu release_pages:
9384c8ff709SChao Yu 	f2fs_put_rpages_mapping(cc, mapping, start_idx, i);
9394c8ff709SChao Yu 	f2fs_destroy_compress_ctx(cc);
9404c8ff709SChao Yu 	return ret;
9414c8ff709SChao Yu }
9424c8ff709SChao Yu 
9434c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
9444c8ff709SChao Yu 		struct page **pagep, pgoff_t index, void **fsdata)
9454c8ff709SChao Yu {
9464c8ff709SChao Yu 	struct compress_ctx cc = {
9474c8ff709SChao Yu 		.inode = inode,
9484c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
9494c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
9504c8ff709SChao Yu 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
9514c8ff709SChao Yu 		.rpages = NULL,
9524c8ff709SChao Yu 		.nr_rpages = 0,
9534c8ff709SChao Yu 	};
9544c8ff709SChao Yu 
9554c8ff709SChao Yu 	return prepare_compress_overwrite(&cc, pagep, index, fsdata);
9564c8ff709SChao Yu }
9574c8ff709SChao Yu 
9584c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
9594c8ff709SChao Yu 					pgoff_t index, unsigned copied)
9604c8ff709SChao Yu 
9614c8ff709SChao Yu {
9624c8ff709SChao Yu 	struct compress_ctx cc = {
9634c8ff709SChao Yu 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
9644c8ff709SChao Yu 		.cluster_size = F2FS_I(inode)->i_cluster_size,
9654c8ff709SChao Yu 		.rpages = fsdata,
9664c8ff709SChao Yu 	};
9674c8ff709SChao Yu 	bool first_index = (index == cc.rpages[0]->index);
9684c8ff709SChao Yu 
9694c8ff709SChao Yu 	if (copied)
9704c8ff709SChao Yu 		set_cluster_dirty(&cc);
9714c8ff709SChao Yu 
9724c8ff709SChao Yu 	f2fs_put_rpages_wbc(&cc, NULL, false, 1);
9734c8ff709SChao Yu 	f2fs_destroy_compress_ctx(&cc);
9744c8ff709SChao Yu 
9754c8ff709SChao Yu 	return first_index;
9764c8ff709SChao Yu }
9774c8ff709SChao Yu 
9783265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
9793265d3dbSChao Yu {
9803265d3dbSChao Yu 	void *fsdata = NULL;
9813265d3dbSChao Yu 	struct page *pagep;
9823265d3dbSChao Yu 	int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
9833265d3dbSChao Yu 	pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
9843265d3dbSChao Yu 							log_cluster_size;
9853265d3dbSChao Yu 	int err;
9863265d3dbSChao Yu 
9873265d3dbSChao Yu 	err = f2fs_is_compressed_cluster(inode, start_idx);
9883265d3dbSChao Yu 	if (err < 0)
9893265d3dbSChao Yu 		return err;
9903265d3dbSChao Yu 
9913265d3dbSChao Yu 	/* truncate normal cluster */
9923265d3dbSChao Yu 	if (!err)
9933265d3dbSChao Yu 		return f2fs_do_truncate_blocks(inode, from, lock);
9943265d3dbSChao Yu 
9953265d3dbSChao Yu 	/* truncate compressed cluster */
9963265d3dbSChao Yu 	err = f2fs_prepare_compress_overwrite(inode, &pagep,
9973265d3dbSChao Yu 						start_idx, &fsdata);
9983265d3dbSChao Yu 
9993265d3dbSChao Yu 	/* should not be a normal cluster */
10003265d3dbSChao Yu 	f2fs_bug_on(F2FS_I_SB(inode), err == 0);
10013265d3dbSChao Yu 
10023265d3dbSChao Yu 	if (err <= 0)
10033265d3dbSChao Yu 		return err;
10043265d3dbSChao Yu 
10053265d3dbSChao Yu 	if (err > 0) {
10063265d3dbSChao Yu 		struct page **rpages = fsdata;
10073265d3dbSChao Yu 		int cluster_size = F2FS_I(inode)->i_cluster_size;
10083265d3dbSChao Yu 		int i;
10093265d3dbSChao Yu 
10103265d3dbSChao Yu 		for (i = cluster_size - 1; i >= 0; i--) {
10113265d3dbSChao Yu 			loff_t start = rpages[i]->index << PAGE_SHIFT;
10123265d3dbSChao Yu 
10133265d3dbSChao Yu 			if (from <= start) {
10143265d3dbSChao Yu 				zero_user_segment(rpages[i], 0, PAGE_SIZE);
10153265d3dbSChao Yu 			} else {
10163265d3dbSChao Yu 				zero_user_segment(rpages[i], from - start,
10173265d3dbSChao Yu 								PAGE_SIZE);
10183265d3dbSChao Yu 				break;
10193265d3dbSChao Yu 			}
10203265d3dbSChao Yu 		}
10213265d3dbSChao Yu 
10223265d3dbSChao Yu 		f2fs_compress_write_end(inode, fsdata, start_idx, true);
10233265d3dbSChao Yu 	}
10243265d3dbSChao Yu 	return 0;
10253265d3dbSChao Yu }
10263265d3dbSChao Yu 
10274c8ff709SChao Yu static int f2fs_write_compressed_pages(struct compress_ctx *cc,
10284c8ff709SChao Yu 					int *submitted,
10294c8ff709SChao Yu 					struct writeback_control *wbc,
10304c8ff709SChao Yu 					enum iostat_type io_type)
10314c8ff709SChao Yu {
10324c8ff709SChao Yu 	struct inode *inode = cc->inode;
10334c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
10344c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
10354c8ff709SChao Yu 	struct f2fs_io_info fio = {
10364c8ff709SChao Yu 		.sbi = sbi,
10374c8ff709SChao Yu 		.ino = cc->inode->i_ino,
10384c8ff709SChao Yu 		.type = DATA,
10394c8ff709SChao Yu 		.op = REQ_OP_WRITE,
10404c8ff709SChao Yu 		.op_flags = wbc_to_write_flags(wbc),
10414c8ff709SChao Yu 		.old_blkaddr = NEW_ADDR,
10424c8ff709SChao Yu 		.page = NULL,
10434c8ff709SChao Yu 		.encrypted_page = NULL,
10444c8ff709SChao Yu 		.compressed_page = NULL,
10454c8ff709SChao Yu 		.submitted = false,
10464c8ff709SChao Yu 		.io_type = io_type,
10474c8ff709SChao Yu 		.io_wbc = wbc,
10484c8ff709SChao Yu 		.encrypted = f2fs_encrypted_file(cc->inode),
10494c8ff709SChao Yu 	};
10504c8ff709SChao Yu 	struct dnode_of_data dn;
10514c8ff709SChao Yu 	struct node_info ni;
10524c8ff709SChao Yu 	struct compress_io_ctx *cic;
10534c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
10544c8ff709SChao Yu 	unsigned int last_index = cc->cluster_size - 1;
10554c8ff709SChao Yu 	loff_t psize;
10564c8ff709SChao Yu 	int i, err;
10574c8ff709SChao Yu 
1058435cbab9SJaegeuk Kim 	if (!IS_NOQUOTA(inode) && !f2fs_trylock_op(sbi))
1059df77fbd8SChao Yu 		return -EAGAIN;
10604c8ff709SChao Yu 
1061df77fbd8SChao Yu 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
10624c8ff709SChao Yu 
10634c8ff709SChao Yu 	err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
10644c8ff709SChao Yu 	if (err)
10654c8ff709SChao Yu 		goto out_unlock_op;
10664c8ff709SChao Yu 
10674c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
1068a2ced1ceSChao Yu 		if (data_blkaddr(dn.inode, dn.node_page,
10694c8ff709SChao Yu 					dn.ofs_in_node + i) == NULL_ADDR)
10704c8ff709SChao Yu 			goto out_put_dnode;
10714c8ff709SChao Yu 	}
10724c8ff709SChao Yu 
10734c8ff709SChao Yu 	psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
10744c8ff709SChao Yu 
10754c8ff709SChao Yu 	err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
10764c8ff709SChao Yu 	if (err)
10774c8ff709SChao Yu 		goto out_put_dnode;
10784c8ff709SChao Yu 
10794c8ff709SChao Yu 	fio.version = ni.version;
10804c8ff709SChao Yu 
10814c8ff709SChao Yu 	cic = f2fs_kzalloc(sbi, sizeof(struct compress_io_ctx), GFP_NOFS);
10824c8ff709SChao Yu 	if (!cic)
10834c8ff709SChao Yu 		goto out_put_dnode;
10844c8ff709SChao Yu 
10854c8ff709SChao Yu 	cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
10864c8ff709SChao Yu 	cic->inode = inode;
1087887347a0SChao Yu 	refcount_set(&cic->ref, cc->nr_cpages);
10884c8ff709SChao Yu 	cic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
10894c8ff709SChao Yu 			cc->log_cluster_size, GFP_NOFS);
10904c8ff709SChao Yu 	if (!cic->rpages)
10914c8ff709SChao Yu 		goto out_put_cic;
10924c8ff709SChao Yu 
10934c8ff709SChao Yu 	cic->nr_rpages = cc->cluster_size;
10944c8ff709SChao Yu 
10954c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
10964c8ff709SChao Yu 		f2fs_set_compressed_page(cc->cpages[i], inode,
1097887347a0SChao Yu 					cc->rpages[i + 1]->index, cic);
10984c8ff709SChao Yu 		fio.compressed_page = cc->cpages[i];
10994c8ff709SChao Yu 		if (fio.encrypted) {
11004c8ff709SChao Yu 			fio.page = cc->rpages[i + 1];
11014c8ff709SChao Yu 			err = f2fs_encrypt_one_page(&fio);
11024c8ff709SChao Yu 			if (err)
11034c8ff709SChao Yu 				goto out_destroy_crypt;
11044c8ff709SChao Yu 			cc->cpages[i] = fio.encrypted_page;
11054c8ff709SChao Yu 		}
11064c8ff709SChao Yu 	}
11074c8ff709SChao Yu 
11084c8ff709SChao Yu 	set_cluster_writeback(cc);
11094c8ff709SChao Yu 
11104c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++)
11114c8ff709SChao Yu 		cic->rpages[i] = cc->rpages[i];
11124c8ff709SChao Yu 
11134c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
11144c8ff709SChao Yu 		block_t blkaddr;
11154c8ff709SChao Yu 
1116a2ced1ceSChao Yu 		blkaddr = f2fs_data_blkaddr(&dn);
111795978caaSChao Yu 		fio.page = cc->rpages[i];
11184c8ff709SChao Yu 		fio.old_blkaddr = blkaddr;
11194c8ff709SChao Yu 
11204c8ff709SChao Yu 		/* cluster header */
11214c8ff709SChao Yu 		if (i == 0) {
11224c8ff709SChao Yu 			if (blkaddr == COMPRESS_ADDR)
11234c8ff709SChao Yu 				fio.compr_blocks++;
11244c8ff709SChao Yu 			if (__is_valid_data_blkaddr(blkaddr))
11254c8ff709SChao Yu 				f2fs_invalidate_blocks(sbi, blkaddr);
11264c8ff709SChao Yu 			f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
11274c8ff709SChao Yu 			goto unlock_continue;
11284c8ff709SChao Yu 		}
11294c8ff709SChao Yu 
11304c8ff709SChao Yu 		if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
11314c8ff709SChao Yu 			fio.compr_blocks++;
11324c8ff709SChao Yu 
11334c8ff709SChao Yu 		if (i > cc->nr_cpages) {
11344c8ff709SChao Yu 			if (__is_valid_data_blkaddr(blkaddr)) {
11354c8ff709SChao Yu 				f2fs_invalidate_blocks(sbi, blkaddr);
11364c8ff709SChao Yu 				f2fs_update_data_blkaddr(&dn, NEW_ADDR);
11374c8ff709SChao Yu 			}
11384c8ff709SChao Yu 			goto unlock_continue;
11394c8ff709SChao Yu 		}
11404c8ff709SChao Yu 
11414c8ff709SChao Yu 		f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
11424c8ff709SChao Yu 
11434c8ff709SChao Yu 		if (fio.encrypted)
11444c8ff709SChao Yu 			fio.encrypted_page = cc->cpages[i - 1];
11454c8ff709SChao Yu 		else
11464c8ff709SChao Yu 			fio.compressed_page = cc->cpages[i - 1];
11474c8ff709SChao Yu 
11484c8ff709SChao Yu 		cc->cpages[i - 1] = NULL;
11494c8ff709SChao Yu 		f2fs_outplace_write_data(&dn, &fio);
11504c8ff709SChao Yu 		(*submitted)++;
11514c8ff709SChao Yu unlock_continue:
11524c8ff709SChao Yu 		inode_dec_dirty_pages(cc->inode);
11534c8ff709SChao Yu 		unlock_page(fio.page);
11544c8ff709SChao Yu 	}
11554c8ff709SChao Yu 
11564c8ff709SChao Yu 	if (fio.compr_blocks)
11574c8ff709SChao Yu 		f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
11584c8ff709SChao Yu 	f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
11594c8ff709SChao Yu 
11604c8ff709SChao Yu 	set_inode_flag(cc->inode, FI_APPEND_WRITE);
11614c8ff709SChao Yu 	if (cc->cluster_idx == 0)
11624c8ff709SChao Yu 		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
11634c8ff709SChao Yu 
11644c8ff709SChao Yu 	f2fs_put_dnode(&dn);
1165435cbab9SJaegeuk Kim 	if (!IS_NOQUOTA(inode))
11664c8ff709SChao Yu 		f2fs_unlock_op(sbi);
11674c8ff709SChao Yu 
1168c10c9820SChao Yu 	spin_lock(&fi->i_size_lock);
11694c8ff709SChao Yu 	if (fi->last_disk_size < psize)
11704c8ff709SChao Yu 		fi->last_disk_size = psize;
1171c10c9820SChao Yu 	spin_unlock(&fi->i_size_lock);
11724c8ff709SChao Yu 
11734c8ff709SChao Yu 	f2fs_put_rpages(cc);
11744c8ff709SChao Yu 	f2fs_destroy_compress_ctx(cc);
11754c8ff709SChao Yu 	return 0;
11764c8ff709SChao Yu 
11774c8ff709SChao Yu out_destroy_crypt:
11784c8ff709SChao Yu 	kfree(cic->rpages);
11794c8ff709SChao Yu 
11804c8ff709SChao Yu 	for (--i; i >= 0; i--)
11814c8ff709SChao Yu 		fscrypt_finalize_bounce_page(&cc->cpages[i]);
11824c8ff709SChao Yu 	for (i = 0; i < cc->nr_cpages; i++) {
11834c8ff709SChao Yu 		if (!cc->cpages[i])
11844c8ff709SChao Yu 			continue;
11854c8ff709SChao Yu 		f2fs_put_page(cc->cpages[i], 1);
11864c8ff709SChao Yu 	}
11874c8ff709SChao Yu out_put_cic:
11884c8ff709SChao Yu 	kfree(cic);
11894c8ff709SChao Yu out_put_dnode:
11904c8ff709SChao Yu 	f2fs_put_dnode(&dn);
11914c8ff709SChao Yu out_unlock_op:
1192435cbab9SJaegeuk Kim 	if (!IS_NOQUOTA(inode))
11934c8ff709SChao Yu 		f2fs_unlock_op(sbi);
11944c8ff709SChao Yu 	return -EAGAIN;
11954c8ff709SChao Yu }
11964c8ff709SChao Yu 
11974c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
11984c8ff709SChao Yu {
11994c8ff709SChao Yu 	struct f2fs_sb_info *sbi = bio->bi_private;
12004c8ff709SChao Yu 	struct compress_io_ctx *cic =
12014c8ff709SChao Yu 			(struct compress_io_ctx *)page_private(page);
12024c8ff709SChao Yu 	int i;
12034c8ff709SChao Yu 
12044c8ff709SChao Yu 	if (unlikely(bio->bi_status))
12054c8ff709SChao Yu 		mapping_set_error(cic->inode->i_mapping, -EIO);
12064c8ff709SChao Yu 
1207*5e6bbde9SChao Yu 	f2fs_compress_free_page(page);
12084c8ff709SChao Yu 
12094c8ff709SChao Yu 	dec_page_count(sbi, F2FS_WB_DATA);
12104c8ff709SChao Yu 
12114c8ff709SChao Yu 	if (refcount_dec_not_one(&cic->ref))
12124c8ff709SChao Yu 		return;
12134c8ff709SChao Yu 
12144c8ff709SChao Yu 	for (i = 0; i < cic->nr_rpages; i++) {
12154c8ff709SChao Yu 		WARN_ON(!cic->rpages[i]);
12164c8ff709SChao Yu 		clear_cold_data(cic->rpages[i]);
12174c8ff709SChao Yu 		end_page_writeback(cic->rpages[i]);
12184c8ff709SChao Yu 	}
12194c8ff709SChao Yu 
12204c8ff709SChao Yu 	kfree(cic->rpages);
12214c8ff709SChao Yu 	kfree(cic);
12224c8ff709SChao Yu }
12234c8ff709SChao Yu 
12244c8ff709SChao Yu static int f2fs_write_raw_pages(struct compress_ctx *cc,
12254c8ff709SChao Yu 					int *submitted,
12264c8ff709SChao Yu 					struct writeback_control *wbc,
12274c8ff709SChao Yu 					enum iostat_type io_type)
12284c8ff709SChao Yu {
12294c8ff709SChao Yu 	struct address_space *mapping = cc->inode->i_mapping;
12304c8ff709SChao Yu 	int _submitted, compr_blocks, ret;
12314c8ff709SChao Yu 	int i = -1, err = 0;
12324c8ff709SChao Yu 
12334c8ff709SChao Yu 	compr_blocks = f2fs_compressed_blocks(cc);
12344c8ff709SChao Yu 	if (compr_blocks < 0) {
12354c8ff709SChao Yu 		err = compr_blocks;
12364c8ff709SChao Yu 		goto out_err;
12374c8ff709SChao Yu 	}
12384c8ff709SChao Yu 
12394c8ff709SChao Yu 	for (i = 0; i < cc->cluster_size; i++) {
12404c8ff709SChao Yu 		if (!cc->rpages[i])
12414c8ff709SChao Yu 			continue;
12424c8ff709SChao Yu retry_write:
12434c8ff709SChao Yu 		if (cc->rpages[i]->mapping != mapping) {
12444c8ff709SChao Yu 			unlock_page(cc->rpages[i]);
12454c8ff709SChao Yu 			continue;
12464c8ff709SChao Yu 		}
12474c8ff709SChao Yu 
12484c8ff709SChao Yu 		BUG_ON(!PageLocked(cc->rpages[i]));
12494c8ff709SChao Yu 
12504c8ff709SChao Yu 		ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
12514c8ff709SChao Yu 						NULL, NULL, wbc, io_type,
12524c8ff709SChao Yu 						compr_blocks);
12534c8ff709SChao Yu 		if (ret) {
12544c8ff709SChao Yu 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
12554c8ff709SChao Yu 				unlock_page(cc->rpages[i]);
12564c8ff709SChao Yu 				ret = 0;
12574c8ff709SChao Yu 			} else if (ret == -EAGAIN) {
1258466357dcSChao Yu 				/*
1259466357dcSChao Yu 				 * for quota file, just redirty left pages to
1260466357dcSChao Yu 				 * avoid deadlock caused by cluster update race
1261466357dcSChao Yu 				 * from foreground operation.
1262466357dcSChao Yu 				 */
1263466357dcSChao Yu 				if (IS_NOQUOTA(cc->inode)) {
1264466357dcSChao Yu 					err = 0;
1265466357dcSChao Yu 					goto out_err;
1266466357dcSChao Yu 				}
12674c8ff709SChao Yu 				ret = 0;
12684c8ff709SChao Yu 				cond_resched();
12695df7731fSChao Yu 				congestion_wait(BLK_RW_ASYNC,
12705df7731fSChao Yu 						DEFAULT_IO_TIMEOUT);
12714c8ff709SChao Yu 				lock_page(cc->rpages[i]);
12724c8ff709SChao Yu 				clear_page_dirty_for_io(cc->rpages[i]);
12734c8ff709SChao Yu 				goto retry_write;
12744c8ff709SChao Yu 			}
12754c8ff709SChao Yu 			err = ret;
1276466357dcSChao Yu 			goto out_err;
12774c8ff709SChao Yu 		}
12784c8ff709SChao Yu 
12794c8ff709SChao Yu 		*submitted += _submitted;
12804c8ff709SChao Yu 	}
12814c8ff709SChao Yu 	return 0;
12824c8ff709SChao Yu out_err:
12834c8ff709SChao Yu 	for (++i; i < cc->cluster_size; i++) {
12844c8ff709SChao Yu 		if (!cc->rpages[i])
12854c8ff709SChao Yu 			continue;
12864c8ff709SChao Yu 		redirty_page_for_writepage(wbc, cc->rpages[i]);
12874c8ff709SChao Yu 		unlock_page(cc->rpages[i]);
12884c8ff709SChao Yu 	}
12894c8ff709SChao Yu 	return err;
12904c8ff709SChao Yu }
12914c8ff709SChao Yu 
12924c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
12934c8ff709SChao Yu 					int *submitted,
12944c8ff709SChao Yu 					struct writeback_control *wbc,
12954c8ff709SChao Yu 					enum iostat_type io_type)
12964c8ff709SChao Yu {
12974c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
12984c8ff709SChao Yu 	const struct f2fs_compress_ops *cops =
12994c8ff709SChao Yu 			f2fs_cops[fi->i_compress_algorithm];
13004c8ff709SChao Yu 	int err;
13014c8ff709SChao Yu 
13024c8ff709SChao Yu 	*submitted = 0;
13034c8ff709SChao Yu 	if (cluster_may_compress(cc)) {
13044c8ff709SChao Yu 		err = f2fs_compress_pages(cc);
13054c8ff709SChao Yu 		if (err == -EAGAIN) {
13064c8ff709SChao Yu 			goto write;
13074c8ff709SChao Yu 		} else if (err) {
13084c8ff709SChao Yu 			f2fs_put_rpages_wbc(cc, wbc, true, 1);
13094c8ff709SChao Yu 			goto destroy_out;
13104c8ff709SChao Yu 		}
13114c8ff709SChao Yu 
13124c8ff709SChao Yu 		err = f2fs_write_compressed_pages(cc, submitted,
13134c8ff709SChao Yu 							wbc, io_type);
13144c8ff709SChao Yu 		cops->destroy_compress_ctx(cc);
13154c8ff709SChao Yu 		if (!err)
13164c8ff709SChao Yu 			return 0;
13174c8ff709SChao Yu 		f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
13184c8ff709SChao Yu 	}
13194c8ff709SChao Yu write:
13204c8ff709SChao Yu 	f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
13214c8ff709SChao Yu 
13224c8ff709SChao Yu 	err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
13234c8ff709SChao Yu 	f2fs_put_rpages_wbc(cc, wbc, false, 0);
13244c8ff709SChao Yu destroy_out:
13254c8ff709SChao Yu 	f2fs_destroy_compress_ctx(cc);
13264c8ff709SChao Yu 	return err;
13274c8ff709SChao Yu }
13284c8ff709SChao Yu 
13294c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
13304c8ff709SChao Yu {
13314c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
13324c8ff709SChao Yu 	struct decompress_io_ctx *dic;
13334c8ff709SChao Yu 	pgoff_t start_idx = start_idx_of_cluster(cc);
13344c8ff709SChao Yu 	int i;
13354c8ff709SChao Yu 
13364c8ff709SChao Yu 	dic = f2fs_kzalloc(sbi, sizeof(struct decompress_io_ctx), GFP_NOFS);
13374c8ff709SChao Yu 	if (!dic)
13384c8ff709SChao Yu 		return ERR_PTR(-ENOMEM);
13394c8ff709SChao Yu 
13404c8ff709SChao Yu 	dic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
13414c8ff709SChao Yu 			cc->log_cluster_size, GFP_NOFS);
13424c8ff709SChao Yu 	if (!dic->rpages) {
13434c8ff709SChao Yu 		kfree(dic);
13444c8ff709SChao Yu 		return ERR_PTR(-ENOMEM);
13454c8ff709SChao Yu 	}
13464c8ff709SChao Yu 
13474c8ff709SChao Yu 	dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
13484c8ff709SChao Yu 	dic->inode = cc->inode;
1349887347a0SChao Yu 	refcount_set(&dic->ref, cc->nr_cpages);
13504c8ff709SChao Yu 	dic->cluster_idx = cc->cluster_idx;
13514c8ff709SChao Yu 	dic->cluster_size = cc->cluster_size;
13524c8ff709SChao Yu 	dic->log_cluster_size = cc->log_cluster_size;
13534c8ff709SChao Yu 	dic->nr_cpages = cc->nr_cpages;
13544c8ff709SChao Yu 	dic->failed = false;
13554c8ff709SChao Yu 
13564c8ff709SChao Yu 	for (i = 0; i < dic->cluster_size; i++)
13574c8ff709SChao Yu 		dic->rpages[i] = cc->rpages[i];
13584c8ff709SChao Yu 	dic->nr_rpages = cc->cluster_size;
13594c8ff709SChao Yu 
13604c8ff709SChao Yu 	dic->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
13614c8ff709SChao Yu 					dic->nr_cpages, GFP_NOFS);
13624c8ff709SChao Yu 	if (!dic->cpages)
13634c8ff709SChao Yu 		goto out_free;
13644c8ff709SChao Yu 
13654c8ff709SChao Yu 	for (i = 0; i < dic->nr_cpages; i++) {
13664c8ff709SChao Yu 		struct page *page;
13674c8ff709SChao Yu 
1368*5e6bbde9SChao Yu 		page = f2fs_compress_alloc_page();
13694c8ff709SChao Yu 		if (!page)
13704c8ff709SChao Yu 			goto out_free;
13714c8ff709SChao Yu 
13724c8ff709SChao Yu 		f2fs_set_compressed_page(page, cc->inode,
1373887347a0SChao Yu 					start_idx + i + 1, dic);
13744c8ff709SChao Yu 		dic->cpages[i] = page;
13754c8ff709SChao Yu 	}
13764c8ff709SChao Yu 
13774c8ff709SChao Yu 	dic->tpages = f2fs_kzalloc(sbi, sizeof(struct page *) *
13784c8ff709SChao Yu 					dic->cluster_size, GFP_NOFS);
13794c8ff709SChao Yu 	if (!dic->tpages)
13804c8ff709SChao Yu 		goto out_free;
13814c8ff709SChao Yu 
13824c8ff709SChao Yu 	for (i = 0; i < dic->cluster_size; i++) {
138380d0d45aSChao Yu 		if (cc->rpages[i]) {
138480d0d45aSChao Yu 			dic->tpages[i] = cc->rpages[i];
13854c8ff709SChao Yu 			continue;
138680d0d45aSChao Yu 		}
13874c8ff709SChao Yu 
1388*5e6bbde9SChao Yu 		dic->tpages[i] = f2fs_compress_alloc_page();
13894c8ff709SChao Yu 		if (!dic->tpages[i])
13904c8ff709SChao Yu 			goto out_free;
13914c8ff709SChao Yu 	}
13924c8ff709SChao Yu 
13934c8ff709SChao Yu 	return dic;
13944c8ff709SChao Yu 
13954c8ff709SChao Yu out_free:
13964c8ff709SChao Yu 	f2fs_free_dic(dic);
13974c8ff709SChao Yu 	return ERR_PTR(-ENOMEM);
13984c8ff709SChao Yu }
13994c8ff709SChao Yu 
14004c8ff709SChao Yu void f2fs_free_dic(struct decompress_io_ctx *dic)
14014c8ff709SChao Yu {
14024c8ff709SChao Yu 	int i;
14034c8ff709SChao Yu 
14044c8ff709SChao Yu 	if (dic->tpages) {
14054c8ff709SChao Yu 		for (i = 0; i < dic->cluster_size; i++) {
14064c8ff709SChao Yu 			if (dic->rpages[i])
14074c8ff709SChao Yu 				continue;
14088908e753SChao Yu 			if (!dic->tpages[i])
14098908e753SChao Yu 				continue;
1410*5e6bbde9SChao Yu 			f2fs_compress_free_page(dic->tpages[i]);
14114c8ff709SChao Yu 		}
14124c8ff709SChao Yu 		kfree(dic->tpages);
14134c8ff709SChao Yu 	}
14144c8ff709SChao Yu 
14154c8ff709SChao Yu 	if (dic->cpages) {
14164c8ff709SChao Yu 		for (i = 0; i < dic->nr_cpages; i++) {
14174c8ff709SChao Yu 			if (!dic->cpages[i])
14184c8ff709SChao Yu 				continue;
1419*5e6bbde9SChao Yu 			f2fs_compress_free_page(dic->cpages[i]);
14204c8ff709SChao Yu 		}
14214c8ff709SChao Yu 		kfree(dic->cpages);
14224c8ff709SChao Yu 	}
14234c8ff709SChao Yu 
14244c8ff709SChao Yu 	kfree(dic->rpages);
14254c8ff709SChao Yu 	kfree(dic);
14264c8ff709SChao Yu }
14274c8ff709SChao Yu 
14284c8ff709SChao Yu void f2fs_decompress_end_io(struct page **rpages,
14294c8ff709SChao Yu 			unsigned int cluster_size, bool err, bool verity)
14304c8ff709SChao Yu {
14314c8ff709SChao Yu 	int i;
14324c8ff709SChao Yu 
14334c8ff709SChao Yu 	for (i = 0; i < cluster_size; i++) {
14344c8ff709SChao Yu 		struct page *rpage = rpages[i];
14354c8ff709SChao Yu 
14364c8ff709SChao Yu 		if (!rpage)
14374c8ff709SChao Yu 			continue;
14384c8ff709SChao Yu 
143923c51bedSChao Yu 		if (err || PageError(rpage))
144023c51bedSChao Yu 			goto clear_uptodate;
144123c51bedSChao Yu 
144223c51bedSChao Yu 		if (!verity || fsverity_verify_page(rpage)) {
144323c51bedSChao Yu 			SetPageUptodate(rpage);
144423c51bedSChao Yu 			goto unlock;
144523c51bedSChao Yu 		}
144623c51bedSChao Yu clear_uptodate:
14474c8ff709SChao Yu 		ClearPageUptodate(rpage);
14484c8ff709SChao Yu 		ClearPageError(rpage);
144923c51bedSChao Yu unlock:
14504c8ff709SChao Yu 		unlock_page(rpage);
14514c8ff709SChao Yu 	}
14524c8ff709SChao Yu }
1453