xref: /openbmc/linux/fs/f2fs/compress.c (revision c67ce71d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * f2fs compress support
4  *
5  * Copyright (c) 2019 Chao Yu <chao@kernel.org>
6  */
7 
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/moduleparam.h>
11 #include <linux/writeback.h>
12 #include <linux/backing-dev.h>
13 #include <linux/lzo.h>
14 #include <linux/lz4.h>
15 #include <linux/zstd.h>
16 #include <linux/pagevec.h>
17 
18 #include "f2fs.h"
19 #include "node.h"
20 #include "segment.h"
21 #include <trace/events/f2fs.h>
22 
23 static struct kmem_cache *cic_entry_slab;
24 static struct kmem_cache *dic_entry_slab;
25 
26 static void *page_array_alloc(struct inode *inode, int nr)
27 {
28 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
29 	unsigned int size = sizeof(struct page *) * nr;
30 
31 	if (likely(size <= sbi->page_array_slab_size))
32 		return f2fs_kmem_cache_alloc(sbi->page_array_slab,
33 					GFP_F2FS_ZERO, false, F2FS_I_SB(inode));
34 	return f2fs_kzalloc(sbi, size, GFP_NOFS);
35 }
36 
37 static void page_array_free(struct inode *inode, void *pages, int nr)
38 {
39 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
40 	unsigned int size = sizeof(struct page *) * nr;
41 
42 	if (!pages)
43 		return;
44 
45 	if (likely(size <= sbi->page_array_slab_size))
46 		kmem_cache_free(sbi->page_array_slab, pages);
47 	else
48 		kfree(pages);
49 }
50 
51 struct f2fs_compress_ops {
52 	int (*init_compress_ctx)(struct compress_ctx *cc);
53 	void (*destroy_compress_ctx)(struct compress_ctx *cc);
54 	int (*compress_pages)(struct compress_ctx *cc);
55 	int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
56 	void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
57 	int (*decompress_pages)(struct decompress_io_ctx *dic);
58 	bool (*is_level_valid)(int level);
59 };
60 
61 static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
62 {
63 	return index & (cc->cluster_size - 1);
64 }
65 
66 static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
67 {
68 	return index >> cc->log_cluster_size;
69 }
70 
71 static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
72 {
73 	return cc->cluster_idx << cc->log_cluster_size;
74 }
75 
76 bool f2fs_is_compressed_page(struct page *page)
77 {
78 	if (!PagePrivate(page))
79 		return false;
80 	if (!page_private(page))
81 		return false;
82 	if (page_private_nonpointer(page))
83 		return false;
84 
85 	f2fs_bug_on(F2FS_M_SB(page->mapping),
86 		*((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
87 	return true;
88 }
89 
90 static void f2fs_set_compressed_page(struct page *page,
91 		struct inode *inode, pgoff_t index, void *data)
92 {
93 	attach_page_private(page, (void *)data);
94 
95 	/* i_crypto_info and iv index */
96 	page->index = index;
97 	page->mapping = inode->i_mapping;
98 }
99 
100 static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
101 {
102 	int i;
103 
104 	for (i = 0; i < len; i++) {
105 		if (!cc->rpages[i])
106 			continue;
107 		if (unlock)
108 			unlock_page(cc->rpages[i]);
109 		else
110 			put_page(cc->rpages[i]);
111 	}
112 }
113 
114 static void f2fs_put_rpages(struct compress_ctx *cc)
115 {
116 	f2fs_drop_rpages(cc, cc->cluster_size, false);
117 }
118 
119 static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
120 {
121 	f2fs_drop_rpages(cc, len, true);
122 }
123 
124 static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
125 		struct writeback_control *wbc, bool redirty, int unlock)
126 {
127 	unsigned int i;
128 
129 	for (i = 0; i < cc->cluster_size; i++) {
130 		if (!cc->rpages[i])
131 			continue;
132 		if (redirty)
133 			redirty_page_for_writepage(wbc, cc->rpages[i]);
134 		f2fs_put_page(cc->rpages[i], unlock);
135 	}
136 }
137 
138 struct page *f2fs_compress_control_page(struct page *page)
139 {
140 	return ((struct compress_io_ctx *)page_private(page))->rpages[0];
141 }
142 
143 int f2fs_init_compress_ctx(struct compress_ctx *cc)
144 {
145 	if (cc->rpages)
146 		return 0;
147 
148 	cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
149 	return cc->rpages ? 0 : -ENOMEM;
150 }
151 
152 void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse)
153 {
154 	page_array_free(cc->inode, cc->rpages, cc->cluster_size);
155 	cc->rpages = NULL;
156 	cc->nr_rpages = 0;
157 	cc->nr_cpages = 0;
158 	cc->valid_nr_cpages = 0;
159 	if (!reuse)
160 		cc->cluster_idx = NULL_CLUSTER;
161 }
162 
163 void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
164 {
165 	unsigned int cluster_ofs;
166 
167 	if (!f2fs_cluster_can_merge_page(cc, page->index))
168 		f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
169 
170 	cluster_ofs = offset_in_cluster(cc, page->index);
171 	cc->rpages[cluster_ofs] = page;
172 	cc->nr_rpages++;
173 	cc->cluster_idx = cluster_idx(cc, page->index);
174 }
175 
176 #ifdef CONFIG_F2FS_FS_LZO
177 static int lzo_init_compress_ctx(struct compress_ctx *cc)
178 {
179 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
180 				LZO1X_MEM_COMPRESS, GFP_NOFS);
181 	if (!cc->private)
182 		return -ENOMEM;
183 
184 	cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
185 	return 0;
186 }
187 
188 static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
189 {
190 	kvfree(cc->private);
191 	cc->private = NULL;
192 }
193 
194 static int lzo_compress_pages(struct compress_ctx *cc)
195 {
196 	int ret;
197 
198 	ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
199 					&cc->clen, cc->private);
200 	if (ret != LZO_E_OK) {
201 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
202 				"lzo compress failed, ret:%d", ret);
203 		return -EIO;
204 	}
205 	return 0;
206 }
207 
208 static int lzo_decompress_pages(struct decompress_io_ctx *dic)
209 {
210 	int ret;
211 
212 	ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
213 						dic->rbuf, &dic->rlen);
214 	if (ret != LZO_E_OK) {
215 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
216 				"lzo decompress failed, ret:%d", ret);
217 		return -EIO;
218 	}
219 
220 	if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
221 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
222 				"lzo invalid rlen:%zu, expected:%lu",
223 				dic->rlen, PAGE_SIZE << dic->log_cluster_size);
224 		return -EIO;
225 	}
226 	return 0;
227 }
228 
229 static const struct f2fs_compress_ops f2fs_lzo_ops = {
230 	.init_compress_ctx	= lzo_init_compress_ctx,
231 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
232 	.compress_pages		= lzo_compress_pages,
233 	.decompress_pages	= lzo_decompress_pages,
234 };
235 #endif
236 
237 #ifdef CONFIG_F2FS_FS_LZ4
238 static int lz4_init_compress_ctx(struct compress_ctx *cc)
239 {
240 	unsigned int size = LZ4_MEM_COMPRESS;
241 
242 #ifdef CONFIG_F2FS_FS_LZ4HC
243 	if (F2FS_I(cc->inode)->i_compress_level)
244 		size = LZ4HC_MEM_COMPRESS;
245 #endif
246 
247 	cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS);
248 	if (!cc->private)
249 		return -ENOMEM;
250 
251 	/*
252 	 * we do not change cc->clen to LZ4_compressBound(inputsize) to
253 	 * adapt worst compress case, because lz4 compressor can handle
254 	 * output budget properly.
255 	 */
256 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
257 	return 0;
258 }
259 
260 static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
261 {
262 	kvfree(cc->private);
263 	cc->private = NULL;
264 }
265 
266 static int lz4_compress_pages(struct compress_ctx *cc)
267 {
268 	int len = -EINVAL;
269 	unsigned char level = F2FS_I(cc->inode)->i_compress_level;
270 
271 	if (!level)
272 		len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
273 						cc->clen, cc->private);
274 #ifdef CONFIG_F2FS_FS_LZ4HC
275 	else
276 		len = LZ4_compress_HC(cc->rbuf, cc->cbuf->cdata, cc->rlen,
277 					cc->clen, level, cc->private);
278 #endif
279 	if (len < 0)
280 		return len;
281 	if (!len)
282 		return -EAGAIN;
283 
284 	cc->clen = len;
285 	return 0;
286 }
287 
288 static int lz4_decompress_pages(struct decompress_io_ctx *dic)
289 {
290 	int ret;
291 
292 	ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
293 						dic->clen, dic->rlen);
294 	if (ret < 0) {
295 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
296 				"lz4 decompress failed, ret:%d", ret);
297 		return -EIO;
298 	}
299 
300 	if (ret != PAGE_SIZE << dic->log_cluster_size) {
301 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
302 				"lz4 invalid ret:%d, expected:%lu",
303 				ret, PAGE_SIZE << dic->log_cluster_size);
304 		return -EIO;
305 	}
306 	return 0;
307 }
308 
309 static bool lz4_is_level_valid(int lvl)
310 {
311 #ifdef CONFIG_F2FS_FS_LZ4HC
312 	return !lvl || (lvl >= LZ4HC_MIN_CLEVEL && lvl <= LZ4HC_MAX_CLEVEL);
313 #else
314 	return lvl == 0;
315 #endif
316 }
317 
318 static const struct f2fs_compress_ops f2fs_lz4_ops = {
319 	.init_compress_ctx	= lz4_init_compress_ctx,
320 	.destroy_compress_ctx	= lz4_destroy_compress_ctx,
321 	.compress_pages		= lz4_compress_pages,
322 	.decompress_pages	= lz4_decompress_pages,
323 	.is_level_valid		= lz4_is_level_valid,
324 };
325 #endif
326 
327 #ifdef CONFIG_F2FS_FS_ZSTD
328 static int zstd_init_compress_ctx(struct compress_ctx *cc)
329 {
330 	zstd_parameters params;
331 	zstd_cstream *stream;
332 	void *workspace;
333 	unsigned int workspace_size;
334 	unsigned char level = F2FS_I(cc->inode)->i_compress_level;
335 
336 	/* Need to remain this for backward compatibility */
337 	if (!level)
338 		level = F2FS_ZSTD_DEFAULT_CLEVEL;
339 
340 	params = zstd_get_params(level, cc->rlen);
341 	workspace_size = zstd_cstream_workspace_bound(&params.cParams);
342 
343 	workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
344 					workspace_size, GFP_NOFS);
345 	if (!workspace)
346 		return -ENOMEM;
347 
348 	stream = zstd_init_cstream(&params, 0, workspace, workspace_size);
349 	if (!stream) {
350 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
351 				"%s zstd_init_cstream failed", __func__);
352 		kvfree(workspace);
353 		return -EIO;
354 	}
355 
356 	cc->private = workspace;
357 	cc->private2 = stream;
358 
359 	cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
360 	return 0;
361 }
362 
363 static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
364 {
365 	kvfree(cc->private);
366 	cc->private = NULL;
367 	cc->private2 = NULL;
368 }
369 
370 static int zstd_compress_pages(struct compress_ctx *cc)
371 {
372 	zstd_cstream *stream = cc->private2;
373 	zstd_in_buffer inbuf;
374 	zstd_out_buffer outbuf;
375 	int src_size = cc->rlen;
376 	int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
377 	int ret;
378 
379 	inbuf.pos = 0;
380 	inbuf.src = cc->rbuf;
381 	inbuf.size = src_size;
382 
383 	outbuf.pos = 0;
384 	outbuf.dst = cc->cbuf->cdata;
385 	outbuf.size = dst_size;
386 
387 	ret = zstd_compress_stream(stream, &outbuf, &inbuf);
388 	if (zstd_is_error(ret)) {
389 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
390 				"%s zstd_compress_stream failed, ret: %d",
391 				__func__, zstd_get_error_code(ret));
392 		return -EIO;
393 	}
394 
395 	ret = zstd_end_stream(stream, &outbuf);
396 	if (zstd_is_error(ret)) {
397 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
398 				"%s zstd_end_stream returned %d",
399 				__func__, zstd_get_error_code(ret));
400 		return -EIO;
401 	}
402 
403 	/*
404 	 * there is compressed data remained in intermediate buffer due to
405 	 * no more space in cbuf.cdata
406 	 */
407 	if (ret)
408 		return -EAGAIN;
409 
410 	cc->clen = outbuf.pos;
411 	return 0;
412 }
413 
414 static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
415 {
416 	zstd_dstream *stream;
417 	void *workspace;
418 	unsigned int workspace_size;
419 	unsigned int max_window_size =
420 			MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
421 
422 	workspace_size = zstd_dstream_workspace_bound(max_window_size);
423 
424 	workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
425 					workspace_size, GFP_NOFS);
426 	if (!workspace)
427 		return -ENOMEM;
428 
429 	stream = zstd_init_dstream(max_window_size, workspace, workspace_size);
430 	if (!stream) {
431 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
432 				"%s zstd_init_dstream failed", __func__);
433 		kvfree(workspace);
434 		return -EIO;
435 	}
436 
437 	dic->private = workspace;
438 	dic->private2 = stream;
439 
440 	return 0;
441 }
442 
443 static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
444 {
445 	kvfree(dic->private);
446 	dic->private = NULL;
447 	dic->private2 = NULL;
448 }
449 
450 static int zstd_decompress_pages(struct decompress_io_ctx *dic)
451 {
452 	zstd_dstream *stream = dic->private2;
453 	zstd_in_buffer inbuf;
454 	zstd_out_buffer outbuf;
455 	int ret;
456 
457 	inbuf.pos = 0;
458 	inbuf.src = dic->cbuf->cdata;
459 	inbuf.size = dic->clen;
460 
461 	outbuf.pos = 0;
462 	outbuf.dst = dic->rbuf;
463 	outbuf.size = dic->rlen;
464 
465 	ret = zstd_decompress_stream(stream, &outbuf, &inbuf);
466 	if (zstd_is_error(ret)) {
467 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
468 				"%s zstd_decompress_stream failed, ret: %d",
469 				__func__, zstd_get_error_code(ret));
470 		return -EIO;
471 	}
472 
473 	if (dic->rlen != outbuf.pos) {
474 		f2fs_err_ratelimited(F2FS_I_SB(dic->inode),
475 				"%s ZSTD invalid rlen:%zu, expected:%lu",
476 				__func__, dic->rlen,
477 				PAGE_SIZE << dic->log_cluster_size);
478 		return -EIO;
479 	}
480 
481 	return 0;
482 }
483 
484 static bool zstd_is_level_valid(int lvl)
485 {
486 	return lvl >= zstd_min_clevel() && lvl <= zstd_max_clevel();
487 }
488 
489 static const struct f2fs_compress_ops f2fs_zstd_ops = {
490 	.init_compress_ctx	= zstd_init_compress_ctx,
491 	.destroy_compress_ctx	= zstd_destroy_compress_ctx,
492 	.compress_pages		= zstd_compress_pages,
493 	.init_decompress_ctx	= zstd_init_decompress_ctx,
494 	.destroy_decompress_ctx	= zstd_destroy_decompress_ctx,
495 	.decompress_pages	= zstd_decompress_pages,
496 	.is_level_valid		= zstd_is_level_valid,
497 };
498 #endif
499 
500 #ifdef CONFIG_F2FS_FS_LZO
501 #ifdef CONFIG_F2FS_FS_LZORLE
502 static int lzorle_compress_pages(struct compress_ctx *cc)
503 {
504 	int ret;
505 
506 	ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
507 					&cc->clen, cc->private);
508 	if (ret != LZO_E_OK) {
509 		f2fs_err_ratelimited(F2FS_I_SB(cc->inode),
510 				"lzo-rle compress failed, ret:%d", ret);
511 		return -EIO;
512 	}
513 	return 0;
514 }
515 
516 static const struct f2fs_compress_ops f2fs_lzorle_ops = {
517 	.init_compress_ctx	= lzo_init_compress_ctx,
518 	.destroy_compress_ctx	= lzo_destroy_compress_ctx,
519 	.compress_pages		= lzorle_compress_pages,
520 	.decompress_pages	= lzo_decompress_pages,
521 };
522 #endif
523 #endif
524 
525 static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
526 #ifdef CONFIG_F2FS_FS_LZO
527 	&f2fs_lzo_ops,
528 #else
529 	NULL,
530 #endif
531 #ifdef CONFIG_F2FS_FS_LZ4
532 	&f2fs_lz4_ops,
533 #else
534 	NULL,
535 #endif
536 #ifdef CONFIG_F2FS_FS_ZSTD
537 	&f2fs_zstd_ops,
538 #else
539 	NULL,
540 #endif
541 #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
542 	&f2fs_lzorle_ops,
543 #else
544 	NULL,
545 #endif
546 };
547 
548 bool f2fs_is_compress_backend_ready(struct inode *inode)
549 {
550 	if (!f2fs_compressed_file(inode))
551 		return true;
552 	return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
553 }
554 
555 bool f2fs_is_compress_level_valid(int alg, int lvl)
556 {
557 	const struct f2fs_compress_ops *cops = f2fs_cops[alg];
558 
559 	if (cops->is_level_valid)
560 		return cops->is_level_valid(lvl);
561 
562 	return lvl == 0;
563 }
564 
565 static mempool_t *compress_page_pool;
566 static int num_compress_pages = 512;
567 module_param(num_compress_pages, uint, 0444);
568 MODULE_PARM_DESC(num_compress_pages,
569 		"Number of intermediate compress pages to preallocate");
570 
571 int __init f2fs_init_compress_mempool(void)
572 {
573 	compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
574 	return compress_page_pool ? 0 : -ENOMEM;
575 }
576 
577 void f2fs_destroy_compress_mempool(void)
578 {
579 	mempool_destroy(compress_page_pool);
580 }
581 
582 static struct page *f2fs_compress_alloc_page(void)
583 {
584 	struct page *page;
585 
586 	page = mempool_alloc(compress_page_pool, GFP_NOFS);
587 	lock_page(page);
588 
589 	return page;
590 }
591 
592 static void f2fs_compress_free_page(struct page *page)
593 {
594 	if (!page)
595 		return;
596 	detach_page_private(page);
597 	page->mapping = NULL;
598 	unlock_page(page);
599 	mempool_free(page, compress_page_pool);
600 }
601 
602 #define MAX_VMAP_RETRIES	3
603 
604 static void *f2fs_vmap(struct page **pages, unsigned int count)
605 {
606 	int i;
607 	void *buf = NULL;
608 
609 	for (i = 0; i < MAX_VMAP_RETRIES; i++) {
610 		buf = vm_map_ram(pages, count, -1);
611 		if (buf)
612 			break;
613 		vm_unmap_aliases();
614 	}
615 	return buf;
616 }
617 
618 static int f2fs_compress_pages(struct compress_ctx *cc)
619 {
620 	struct f2fs_inode_info *fi = F2FS_I(cc->inode);
621 	const struct f2fs_compress_ops *cops =
622 				f2fs_cops[fi->i_compress_algorithm];
623 	unsigned int max_len, new_nr_cpages;
624 	u32 chksum = 0;
625 	int i, ret;
626 
627 	trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
628 				cc->cluster_size, fi->i_compress_algorithm);
629 
630 	if (cops->init_compress_ctx) {
631 		ret = cops->init_compress_ctx(cc);
632 		if (ret)
633 			goto out;
634 	}
635 
636 	max_len = COMPRESS_HEADER_SIZE + cc->clen;
637 	cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
638 	cc->valid_nr_cpages = cc->nr_cpages;
639 
640 	cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
641 	if (!cc->cpages) {
642 		ret = -ENOMEM;
643 		goto destroy_compress_ctx;
644 	}
645 
646 	for (i = 0; i < cc->nr_cpages; i++)
647 		cc->cpages[i] = f2fs_compress_alloc_page();
648 
649 	cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
650 	if (!cc->rbuf) {
651 		ret = -ENOMEM;
652 		goto out_free_cpages;
653 	}
654 
655 	cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
656 	if (!cc->cbuf) {
657 		ret = -ENOMEM;
658 		goto out_vunmap_rbuf;
659 	}
660 
661 	ret = cops->compress_pages(cc);
662 	if (ret)
663 		goto out_vunmap_cbuf;
664 
665 	max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
666 
667 	if (cc->clen > max_len) {
668 		ret = -EAGAIN;
669 		goto out_vunmap_cbuf;
670 	}
671 
672 	cc->cbuf->clen = cpu_to_le32(cc->clen);
673 
674 	if (fi->i_compress_flag & BIT(COMPRESS_CHKSUM))
675 		chksum = f2fs_crc32(F2FS_I_SB(cc->inode),
676 					cc->cbuf->cdata, cc->clen);
677 	cc->cbuf->chksum = cpu_to_le32(chksum);
678 
679 	for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
680 		cc->cbuf->reserved[i] = cpu_to_le32(0);
681 
682 	new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
683 
684 	/* zero out any unused part of the last page */
685 	memset(&cc->cbuf->cdata[cc->clen], 0,
686 			(new_nr_cpages * PAGE_SIZE) -
687 			(cc->clen + COMPRESS_HEADER_SIZE));
688 
689 	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
690 	vm_unmap_ram(cc->rbuf, cc->cluster_size);
691 
692 	for (i = new_nr_cpages; i < cc->nr_cpages; i++) {
693 		f2fs_compress_free_page(cc->cpages[i]);
694 		cc->cpages[i] = NULL;
695 	}
696 
697 	if (cops->destroy_compress_ctx)
698 		cops->destroy_compress_ctx(cc);
699 
700 	cc->valid_nr_cpages = new_nr_cpages;
701 
702 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
703 							cc->clen, ret);
704 	return 0;
705 
706 out_vunmap_cbuf:
707 	vm_unmap_ram(cc->cbuf, cc->nr_cpages);
708 out_vunmap_rbuf:
709 	vm_unmap_ram(cc->rbuf, cc->cluster_size);
710 out_free_cpages:
711 	for (i = 0; i < cc->nr_cpages; i++) {
712 		if (cc->cpages[i])
713 			f2fs_compress_free_page(cc->cpages[i]);
714 	}
715 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
716 	cc->cpages = NULL;
717 destroy_compress_ctx:
718 	if (cops->destroy_compress_ctx)
719 		cops->destroy_compress_ctx(cc);
720 out:
721 	trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
722 							cc->clen, ret);
723 	return ret;
724 }
725 
726 static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
727 		bool pre_alloc);
728 static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
729 		bool bypass_destroy_callback, bool pre_alloc);
730 
731 void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task)
732 {
733 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
734 	struct f2fs_inode_info *fi = F2FS_I(dic->inode);
735 	const struct f2fs_compress_ops *cops =
736 			f2fs_cops[fi->i_compress_algorithm];
737 	bool bypass_callback = false;
738 	int ret;
739 
740 	trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
741 				dic->cluster_size, fi->i_compress_algorithm);
742 
743 	if (dic->failed) {
744 		ret = -EIO;
745 		goto out_end_io;
746 	}
747 
748 	ret = f2fs_prepare_decomp_mem(dic, false);
749 	if (ret) {
750 		bypass_callback = true;
751 		goto out_release;
752 	}
753 
754 	dic->clen = le32_to_cpu(dic->cbuf->clen);
755 	dic->rlen = PAGE_SIZE << dic->log_cluster_size;
756 
757 	if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
758 		ret = -EFSCORRUPTED;
759 
760 		/* Avoid f2fs_commit_super in irq context */
761 		if (!in_task)
762 			f2fs_handle_error_async(sbi, ERROR_FAIL_DECOMPRESSION);
763 		else
764 			f2fs_handle_error(sbi, ERROR_FAIL_DECOMPRESSION);
765 		goto out_release;
766 	}
767 
768 	ret = cops->decompress_pages(dic);
769 
770 	if (!ret && (fi->i_compress_flag & BIT(COMPRESS_CHKSUM))) {
771 		u32 provided = le32_to_cpu(dic->cbuf->chksum);
772 		u32 calculated = f2fs_crc32(sbi, dic->cbuf->cdata, dic->clen);
773 
774 		if (provided != calculated) {
775 			if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) {
776 				set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT);
777 				f2fs_info_ratelimited(sbi,
778 					"checksum invalid, nid = %lu, %x vs %x",
779 					dic->inode->i_ino,
780 					provided, calculated);
781 			}
782 			set_sbi_flag(sbi, SBI_NEED_FSCK);
783 		}
784 	}
785 
786 out_release:
787 	f2fs_release_decomp_mem(dic, bypass_callback, false);
788 
789 out_end_io:
790 	trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
791 							dic->clen, ret);
792 	f2fs_decompress_end_io(dic, ret, in_task);
793 }
794 
795 /*
796  * This is called when a page of a compressed cluster has been read from disk
797  * (or failed to be read from disk).  It checks whether this page was the last
798  * page being waited on in the cluster, and if so, it decompresses the cluster
799  * (or in the case of a failure, cleans up without actually decompressing).
800  */
801 void f2fs_end_read_compressed_page(struct page *page, bool failed,
802 		block_t blkaddr, bool in_task)
803 {
804 	struct decompress_io_ctx *dic =
805 			(struct decompress_io_ctx *)page_private(page);
806 	struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
807 
808 	dec_page_count(sbi, F2FS_RD_DATA);
809 
810 	if (failed)
811 		WRITE_ONCE(dic->failed, true);
812 	else if (blkaddr && in_task)
813 		f2fs_cache_compressed_page(sbi, page,
814 					dic->inode->i_ino, blkaddr);
815 
816 	if (atomic_dec_and_test(&dic->remaining_pages))
817 		f2fs_decompress_cluster(dic, in_task);
818 }
819 
820 static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
821 {
822 	if (cc->cluster_idx == NULL_CLUSTER)
823 		return true;
824 	return cc->cluster_idx == cluster_idx(cc, index);
825 }
826 
827 bool f2fs_cluster_is_empty(struct compress_ctx *cc)
828 {
829 	return cc->nr_rpages == 0;
830 }
831 
832 static bool f2fs_cluster_is_full(struct compress_ctx *cc)
833 {
834 	return cc->cluster_size == cc->nr_rpages;
835 }
836 
837 bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
838 {
839 	if (f2fs_cluster_is_empty(cc))
840 		return true;
841 	return is_page_in_cluster(cc, index);
842 }
843 
844 bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
845 				int index, int nr_pages, bool uptodate)
846 {
847 	unsigned long pgidx = pages[index]->index;
848 	int i = uptodate ? 0 : 1;
849 
850 	/*
851 	 * when uptodate set to true, try to check all pages in cluster is
852 	 * uptodate or not.
853 	 */
854 	if (uptodate && (pgidx % cc->cluster_size))
855 		return false;
856 
857 	if (nr_pages - index < cc->cluster_size)
858 		return false;
859 
860 	for (; i < cc->cluster_size; i++) {
861 		if (pages[index + i]->index != pgidx + i)
862 			return false;
863 		if (uptodate && !PageUptodate(pages[index + i]))
864 			return false;
865 	}
866 
867 	return true;
868 }
869 
870 static bool cluster_has_invalid_data(struct compress_ctx *cc)
871 {
872 	loff_t i_size = i_size_read(cc->inode);
873 	unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
874 	int i;
875 
876 	for (i = 0; i < cc->cluster_size; i++) {
877 		struct page *page = cc->rpages[i];
878 
879 		f2fs_bug_on(F2FS_I_SB(cc->inode), !page);
880 
881 		/* beyond EOF */
882 		if (page->index >= nr_pages)
883 			return true;
884 	}
885 	return false;
886 }
887 
888 bool f2fs_sanity_check_cluster(struct dnode_of_data *dn)
889 {
890 #ifdef CONFIG_F2FS_CHECK_FS
891 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
892 	unsigned int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
893 	int cluster_end = 0;
894 	unsigned int count;
895 	int i;
896 	char *reason = "";
897 
898 	if (dn->data_blkaddr != COMPRESS_ADDR)
899 		return false;
900 
901 	/* [..., COMPR_ADDR, ...] */
902 	if (dn->ofs_in_node % cluster_size) {
903 		reason = "[*|C|*|*]";
904 		goto out;
905 	}
906 
907 	for (i = 1, count = 1; i < cluster_size; i++, count++) {
908 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
909 							dn->ofs_in_node + i);
910 
911 		/* [COMPR_ADDR, ..., COMPR_ADDR] */
912 		if (blkaddr == COMPRESS_ADDR) {
913 			reason = "[C|*|C|*]";
914 			goto out;
915 		}
916 		if (!__is_valid_data_blkaddr(blkaddr)) {
917 			if (!cluster_end)
918 				cluster_end = i;
919 			continue;
920 		}
921 		/* [COMPR_ADDR, NULL_ADDR or NEW_ADDR, valid_blkaddr] */
922 		if (cluster_end) {
923 			reason = "[C|N|N|V]";
924 			goto out;
925 		}
926 	}
927 
928 	f2fs_bug_on(F2FS_I_SB(dn->inode), count != cluster_size &&
929 		!is_inode_flag_set(dn->inode, FI_COMPRESS_RELEASED));
930 
931 	return false;
932 out:
933 	f2fs_warn(sbi, "access invalid cluster, ino:%lu, nid:%u, ofs_in_node:%u, reason:%s",
934 			dn->inode->i_ino, dn->nid, dn->ofs_in_node, reason);
935 	set_sbi_flag(sbi, SBI_NEED_FSCK);
936 	return true;
937 #else
938 	return false;
939 #endif
940 }
941 
942 static int __f2fs_get_cluster_blocks(struct inode *inode,
943 					struct dnode_of_data *dn)
944 {
945 	unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
946 	int count, i;
947 
948 	for (i = 0, count = 0; i < cluster_size; i++) {
949 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
950 							dn->ofs_in_node + i);
951 
952 		if (__is_valid_data_blkaddr(blkaddr))
953 			count++;
954 	}
955 
956 	return count;
957 }
958 
959 static int __f2fs_cluster_blocks(struct inode *inode, unsigned int cluster_idx,
960 				enum cluster_check_type type)
961 {
962 	struct dnode_of_data dn;
963 	unsigned int start_idx = cluster_idx <<
964 				F2FS_I(inode)->i_log_cluster_size;
965 	int ret;
966 
967 	set_new_dnode(&dn, inode, NULL, NULL, 0);
968 	ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
969 	if (ret) {
970 		if (ret == -ENOENT)
971 			ret = 0;
972 		goto fail;
973 	}
974 
975 	if (f2fs_sanity_check_cluster(&dn)) {
976 		ret = -EFSCORRUPTED;
977 		goto fail;
978 	}
979 
980 	if (dn.data_blkaddr == COMPRESS_ADDR) {
981 		if (type == CLUSTER_COMPR_BLKS)
982 			ret = 1 + __f2fs_get_cluster_blocks(inode, &dn);
983 		else if (type == CLUSTER_IS_COMPR)
984 			ret = 1;
985 	} else if (type == CLUSTER_RAW_BLKS) {
986 		ret = __f2fs_get_cluster_blocks(inode, &dn);
987 	}
988 fail:
989 	f2fs_put_dnode(&dn);
990 	return ret;
991 }
992 
993 /* return # of compressed blocks in compressed cluster */
994 static int f2fs_compressed_blocks(struct compress_ctx *cc)
995 {
996 	return __f2fs_cluster_blocks(cc->inode, cc->cluster_idx,
997 		CLUSTER_COMPR_BLKS);
998 }
999 
1000 /* return # of raw blocks in non-compressed cluster */
1001 static int f2fs_decompressed_blocks(struct inode *inode,
1002 				unsigned int cluster_idx)
1003 {
1004 	return __f2fs_cluster_blocks(inode, cluster_idx,
1005 		CLUSTER_RAW_BLKS);
1006 }
1007 
1008 /* return whether cluster is compressed one or not */
1009 int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
1010 {
1011 	return __f2fs_cluster_blocks(inode,
1012 		index >> F2FS_I(inode)->i_log_cluster_size,
1013 		CLUSTER_IS_COMPR);
1014 }
1015 
1016 /* return whether cluster contains non raw blocks or not */
1017 bool f2fs_is_sparse_cluster(struct inode *inode, pgoff_t index)
1018 {
1019 	unsigned int cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size;
1020 
1021 	return f2fs_decompressed_blocks(inode, cluster_idx) !=
1022 		F2FS_I(inode)->i_cluster_size;
1023 }
1024 
1025 static bool cluster_may_compress(struct compress_ctx *cc)
1026 {
1027 	if (!f2fs_need_compress_data(cc->inode))
1028 		return false;
1029 	if (f2fs_is_atomic_file(cc->inode))
1030 		return false;
1031 	if (!f2fs_cluster_is_full(cc))
1032 		return false;
1033 	if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
1034 		return false;
1035 	return !cluster_has_invalid_data(cc);
1036 }
1037 
1038 static void set_cluster_writeback(struct compress_ctx *cc)
1039 {
1040 	int i;
1041 
1042 	for (i = 0; i < cc->cluster_size; i++) {
1043 		if (cc->rpages[i])
1044 			set_page_writeback(cc->rpages[i]);
1045 	}
1046 }
1047 
1048 static void set_cluster_dirty(struct compress_ctx *cc)
1049 {
1050 	int i;
1051 
1052 	for (i = 0; i < cc->cluster_size; i++)
1053 		if (cc->rpages[i]) {
1054 			set_page_dirty(cc->rpages[i]);
1055 			set_page_private_gcing(cc->rpages[i]);
1056 		}
1057 }
1058 
1059 static int prepare_compress_overwrite(struct compress_ctx *cc,
1060 		struct page **pagep, pgoff_t index, void **fsdata)
1061 {
1062 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1063 	struct address_space *mapping = cc->inode->i_mapping;
1064 	struct page *page;
1065 	sector_t last_block_in_bio;
1066 	fgf_t fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
1067 	pgoff_t start_idx = start_idx_of_cluster(cc);
1068 	int i, ret;
1069 
1070 retry:
1071 	ret = f2fs_is_compressed_cluster(cc->inode, start_idx);
1072 	if (ret <= 0)
1073 		return ret;
1074 
1075 	ret = f2fs_init_compress_ctx(cc);
1076 	if (ret)
1077 		return ret;
1078 
1079 	/* keep page reference to avoid page reclaim */
1080 	for (i = 0; i < cc->cluster_size; i++) {
1081 		page = f2fs_pagecache_get_page(mapping, start_idx + i,
1082 							fgp_flag, GFP_NOFS);
1083 		if (!page) {
1084 			ret = -ENOMEM;
1085 			goto unlock_pages;
1086 		}
1087 
1088 		if (PageUptodate(page))
1089 			f2fs_put_page(page, 1);
1090 		else
1091 			f2fs_compress_ctx_add_page(cc, page);
1092 	}
1093 
1094 	if (!f2fs_cluster_is_empty(cc)) {
1095 		struct bio *bio = NULL;
1096 
1097 		ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
1098 					&last_block_in_bio, false, true);
1099 		f2fs_put_rpages(cc);
1100 		f2fs_destroy_compress_ctx(cc, true);
1101 		if (ret)
1102 			goto out;
1103 		if (bio)
1104 			f2fs_submit_read_bio(sbi, bio, DATA);
1105 
1106 		ret = f2fs_init_compress_ctx(cc);
1107 		if (ret)
1108 			goto out;
1109 	}
1110 
1111 	for (i = 0; i < cc->cluster_size; i++) {
1112 		f2fs_bug_on(sbi, cc->rpages[i]);
1113 
1114 		page = find_lock_page(mapping, start_idx + i);
1115 		if (!page) {
1116 			/* page can be truncated */
1117 			goto release_and_retry;
1118 		}
1119 
1120 		f2fs_wait_on_page_writeback(page, DATA, true, true);
1121 		f2fs_compress_ctx_add_page(cc, page);
1122 
1123 		if (!PageUptodate(page)) {
1124 release_and_retry:
1125 			f2fs_put_rpages(cc);
1126 			f2fs_unlock_rpages(cc, i + 1);
1127 			f2fs_destroy_compress_ctx(cc, true);
1128 			goto retry;
1129 		}
1130 	}
1131 
1132 	if (likely(!ret)) {
1133 		*fsdata = cc->rpages;
1134 		*pagep = cc->rpages[offset_in_cluster(cc, index)];
1135 		return cc->cluster_size;
1136 	}
1137 
1138 unlock_pages:
1139 	f2fs_put_rpages(cc);
1140 	f2fs_unlock_rpages(cc, i);
1141 	f2fs_destroy_compress_ctx(cc, true);
1142 out:
1143 	return ret;
1144 }
1145 
1146 int f2fs_prepare_compress_overwrite(struct inode *inode,
1147 		struct page **pagep, pgoff_t index, void **fsdata)
1148 {
1149 	struct compress_ctx cc = {
1150 		.inode = inode,
1151 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1152 		.cluster_size = F2FS_I(inode)->i_cluster_size,
1153 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
1154 		.rpages = NULL,
1155 		.nr_rpages = 0,
1156 	};
1157 
1158 	return prepare_compress_overwrite(&cc, pagep, index, fsdata);
1159 }
1160 
1161 bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
1162 					pgoff_t index, unsigned copied)
1163 
1164 {
1165 	struct compress_ctx cc = {
1166 		.inode = inode,
1167 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1168 		.cluster_size = F2FS_I(inode)->i_cluster_size,
1169 		.rpages = fsdata,
1170 	};
1171 	bool first_index = (index == cc.rpages[0]->index);
1172 
1173 	if (copied)
1174 		set_cluster_dirty(&cc);
1175 
1176 	f2fs_put_rpages_wbc(&cc, NULL, false, 1);
1177 	f2fs_destroy_compress_ctx(&cc, false);
1178 
1179 	return first_index;
1180 }
1181 
1182 int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
1183 {
1184 	void *fsdata = NULL;
1185 	struct page *pagep;
1186 	int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
1187 	pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
1188 							log_cluster_size;
1189 	int err;
1190 
1191 	err = f2fs_is_compressed_cluster(inode, start_idx);
1192 	if (err < 0)
1193 		return err;
1194 
1195 	/* truncate normal cluster */
1196 	if (!err)
1197 		return f2fs_do_truncate_blocks(inode, from, lock);
1198 
1199 	/* truncate compressed cluster */
1200 	err = f2fs_prepare_compress_overwrite(inode, &pagep,
1201 						start_idx, &fsdata);
1202 
1203 	/* should not be a normal cluster */
1204 	f2fs_bug_on(F2FS_I_SB(inode), err == 0);
1205 
1206 	if (err <= 0)
1207 		return err;
1208 
1209 	if (err > 0) {
1210 		struct page **rpages = fsdata;
1211 		int cluster_size = F2FS_I(inode)->i_cluster_size;
1212 		int i;
1213 
1214 		for (i = cluster_size - 1; i >= 0; i--) {
1215 			loff_t start = rpages[i]->index << PAGE_SHIFT;
1216 
1217 			if (from <= start) {
1218 				zero_user_segment(rpages[i], 0, PAGE_SIZE);
1219 			} else {
1220 				zero_user_segment(rpages[i], from - start,
1221 								PAGE_SIZE);
1222 				break;
1223 			}
1224 		}
1225 
1226 		f2fs_compress_write_end(inode, fsdata, start_idx, true);
1227 	}
1228 	return 0;
1229 }
1230 
1231 static int f2fs_write_compressed_pages(struct compress_ctx *cc,
1232 					int *submitted,
1233 					struct writeback_control *wbc,
1234 					enum iostat_type io_type)
1235 {
1236 	struct inode *inode = cc->inode;
1237 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1238 	struct f2fs_inode_info *fi = F2FS_I(inode);
1239 	struct f2fs_io_info fio = {
1240 		.sbi = sbi,
1241 		.ino = cc->inode->i_ino,
1242 		.type = DATA,
1243 		.op = REQ_OP_WRITE,
1244 		.op_flags = wbc_to_write_flags(wbc),
1245 		.old_blkaddr = NEW_ADDR,
1246 		.page = NULL,
1247 		.encrypted_page = NULL,
1248 		.compressed_page = NULL,
1249 		.submitted = 0,
1250 		.io_type = io_type,
1251 		.io_wbc = wbc,
1252 		.encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode) ?
1253 									1 : 0,
1254 	};
1255 	struct dnode_of_data dn;
1256 	struct node_info ni;
1257 	struct compress_io_ctx *cic;
1258 	pgoff_t start_idx = start_idx_of_cluster(cc);
1259 	unsigned int last_index = cc->cluster_size - 1;
1260 	loff_t psize;
1261 	int i, err;
1262 	bool quota_inode = IS_NOQUOTA(inode);
1263 
1264 	/* we should bypass data pages to proceed the kworker jobs */
1265 	if (unlikely(f2fs_cp_error(sbi))) {
1266 		mapping_set_error(cc->rpages[0]->mapping, -EIO);
1267 		goto out_free;
1268 	}
1269 
1270 	if (quota_inode) {
1271 		/*
1272 		 * We need to wait for node_write to avoid block allocation during
1273 		 * checkpoint. This can only happen to quota writes which can cause
1274 		 * the below discard race condition.
1275 		 */
1276 		f2fs_down_read(&sbi->node_write);
1277 	} else if (!f2fs_trylock_op(sbi)) {
1278 		goto out_free;
1279 	}
1280 
1281 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1282 
1283 	err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
1284 	if (err)
1285 		goto out_unlock_op;
1286 
1287 	for (i = 0; i < cc->cluster_size; i++) {
1288 		if (data_blkaddr(dn.inode, dn.node_page,
1289 					dn.ofs_in_node + i) == NULL_ADDR)
1290 			goto out_put_dnode;
1291 	}
1292 
1293 	psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
1294 
1295 	err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
1296 	if (err)
1297 		goto out_put_dnode;
1298 
1299 	fio.version = ni.version;
1300 
1301 	cic = f2fs_kmem_cache_alloc(cic_entry_slab, GFP_F2FS_ZERO, false, sbi);
1302 	if (!cic)
1303 		goto out_put_dnode;
1304 
1305 	cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1306 	cic->inode = inode;
1307 	atomic_set(&cic->pending_pages, cc->valid_nr_cpages);
1308 	cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1309 	if (!cic->rpages)
1310 		goto out_put_cic;
1311 
1312 	cic->nr_rpages = cc->cluster_size;
1313 
1314 	for (i = 0; i < cc->valid_nr_cpages; i++) {
1315 		f2fs_set_compressed_page(cc->cpages[i], inode,
1316 					cc->rpages[i + 1]->index, cic);
1317 		fio.compressed_page = cc->cpages[i];
1318 
1319 		fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1320 						dn.ofs_in_node + i + 1);
1321 
1322 		/* wait for GCed page writeback via META_MAPPING */
1323 		f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1324 
1325 		if (fio.encrypted) {
1326 			fio.page = cc->rpages[i + 1];
1327 			err = f2fs_encrypt_one_page(&fio);
1328 			if (err)
1329 				goto out_destroy_crypt;
1330 			cc->cpages[i] = fio.encrypted_page;
1331 		}
1332 	}
1333 
1334 	set_cluster_writeback(cc);
1335 
1336 	for (i = 0; i < cc->cluster_size; i++)
1337 		cic->rpages[i] = cc->rpages[i];
1338 
1339 	for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
1340 		block_t blkaddr;
1341 
1342 		blkaddr = f2fs_data_blkaddr(&dn);
1343 		fio.page = cc->rpages[i];
1344 		fio.old_blkaddr = blkaddr;
1345 
1346 		/* cluster header */
1347 		if (i == 0) {
1348 			if (blkaddr == COMPRESS_ADDR)
1349 				fio.compr_blocks++;
1350 			if (__is_valid_data_blkaddr(blkaddr))
1351 				f2fs_invalidate_blocks(sbi, blkaddr);
1352 			f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
1353 			goto unlock_continue;
1354 		}
1355 
1356 		if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
1357 			fio.compr_blocks++;
1358 
1359 		if (i > cc->valid_nr_cpages) {
1360 			if (__is_valid_data_blkaddr(blkaddr)) {
1361 				f2fs_invalidate_blocks(sbi, blkaddr);
1362 				f2fs_update_data_blkaddr(&dn, NEW_ADDR);
1363 			}
1364 			goto unlock_continue;
1365 		}
1366 
1367 		f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
1368 
1369 		if (fio.encrypted)
1370 			fio.encrypted_page = cc->cpages[i - 1];
1371 		else
1372 			fio.compressed_page = cc->cpages[i - 1];
1373 
1374 		cc->cpages[i - 1] = NULL;
1375 		f2fs_outplace_write_data(&dn, &fio);
1376 		(*submitted)++;
1377 unlock_continue:
1378 		inode_dec_dirty_pages(cc->inode);
1379 		unlock_page(fio.page);
1380 	}
1381 
1382 	if (fio.compr_blocks)
1383 		f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
1384 	f2fs_i_compr_blocks_update(inode, cc->valid_nr_cpages, true);
1385 	add_compr_block_stat(inode, cc->valid_nr_cpages);
1386 
1387 	set_inode_flag(cc->inode, FI_APPEND_WRITE);
1388 
1389 	f2fs_put_dnode(&dn);
1390 	if (quota_inode)
1391 		f2fs_up_read(&sbi->node_write);
1392 	else
1393 		f2fs_unlock_op(sbi);
1394 
1395 	spin_lock(&fi->i_size_lock);
1396 	if (fi->last_disk_size < psize)
1397 		fi->last_disk_size = psize;
1398 	spin_unlock(&fi->i_size_lock);
1399 
1400 	f2fs_put_rpages(cc);
1401 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1402 	cc->cpages = NULL;
1403 	f2fs_destroy_compress_ctx(cc, false);
1404 	return 0;
1405 
1406 out_destroy_crypt:
1407 	page_array_free(cc->inode, cic->rpages, cc->cluster_size);
1408 
1409 	for (--i; i >= 0; i--)
1410 		fscrypt_finalize_bounce_page(&cc->cpages[i]);
1411 out_put_cic:
1412 	kmem_cache_free(cic_entry_slab, cic);
1413 out_put_dnode:
1414 	f2fs_put_dnode(&dn);
1415 out_unlock_op:
1416 	if (quota_inode)
1417 		f2fs_up_read(&sbi->node_write);
1418 	else
1419 		f2fs_unlock_op(sbi);
1420 out_free:
1421 	for (i = 0; i < cc->valid_nr_cpages; i++) {
1422 		f2fs_compress_free_page(cc->cpages[i]);
1423 		cc->cpages[i] = NULL;
1424 	}
1425 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1426 	cc->cpages = NULL;
1427 	return -EAGAIN;
1428 }
1429 
1430 void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
1431 {
1432 	struct f2fs_sb_info *sbi = bio->bi_private;
1433 	struct compress_io_ctx *cic =
1434 			(struct compress_io_ctx *)page_private(page);
1435 	enum count_type type = WB_DATA_TYPE(page,
1436 				f2fs_is_compressed_page(page));
1437 	int i;
1438 
1439 	if (unlikely(bio->bi_status))
1440 		mapping_set_error(cic->inode->i_mapping, -EIO);
1441 
1442 	f2fs_compress_free_page(page);
1443 
1444 	dec_page_count(sbi, type);
1445 
1446 	if (atomic_dec_return(&cic->pending_pages))
1447 		return;
1448 
1449 	for (i = 0; i < cic->nr_rpages; i++) {
1450 		WARN_ON(!cic->rpages[i]);
1451 		clear_page_private_gcing(cic->rpages[i]);
1452 		end_page_writeback(cic->rpages[i]);
1453 	}
1454 
1455 	page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
1456 	kmem_cache_free(cic_entry_slab, cic);
1457 }
1458 
1459 static int f2fs_write_raw_pages(struct compress_ctx *cc,
1460 					int *submitted_p,
1461 					struct writeback_control *wbc,
1462 					enum iostat_type io_type)
1463 {
1464 	struct address_space *mapping = cc->inode->i_mapping;
1465 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
1466 	int submitted, compr_blocks, i;
1467 	int ret = 0;
1468 
1469 	compr_blocks = f2fs_compressed_blocks(cc);
1470 
1471 	for (i = 0; i < cc->cluster_size; i++) {
1472 		if (!cc->rpages[i])
1473 			continue;
1474 
1475 		redirty_page_for_writepage(wbc, cc->rpages[i]);
1476 		unlock_page(cc->rpages[i]);
1477 	}
1478 
1479 	if (compr_blocks < 0)
1480 		return compr_blocks;
1481 
1482 	/* overwrite compressed cluster w/ normal cluster */
1483 	if (compr_blocks > 0)
1484 		f2fs_lock_op(sbi);
1485 
1486 	for (i = 0; i < cc->cluster_size; i++) {
1487 		if (!cc->rpages[i])
1488 			continue;
1489 retry_write:
1490 		lock_page(cc->rpages[i]);
1491 
1492 		if (cc->rpages[i]->mapping != mapping) {
1493 continue_unlock:
1494 			unlock_page(cc->rpages[i]);
1495 			continue;
1496 		}
1497 
1498 		if (!PageDirty(cc->rpages[i]))
1499 			goto continue_unlock;
1500 
1501 		if (PageWriteback(cc->rpages[i])) {
1502 			if (wbc->sync_mode == WB_SYNC_NONE)
1503 				goto continue_unlock;
1504 			f2fs_wait_on_page_writeback(cc->rpages[i], DATA, true, true);
1505 		}
1506 
1507 		if (!clear_page_dirty_for_io(cc->rpages[i]))
1508 			goto continue_unlock;
1509 
1510 		ret = f2fs_write_single_data_page(cc->rpages[i], &submitted,
1511 						NULL, NULL, wbc, io_type,
1512 						compr_blocks, false);
1513 		if (ret) {
1514 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
1515 				unlock_page(cc->rpages[i]);
1516 				ret = 0;
1517 			} else if (ret == -EAGAIN) {
1518 				ret = 0;
1519 				/*
1520 				 * for quota file, just redirty left pages to
1521 				 * avoid deadlock caused by cluster update race
1522 				 * from foreground operation.
1523 				 */
1524 				if (IS_NOQUOTA(cc->inode))
1525 					goto out;
1526 				f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1527 				goto retry_write;
1528 			}
1529 			goto out;
1530 		}
1531 
1532 		*submitted_p += submitted;
1533 	}
1534 
1535 out:
1536 	if (compr_blocks > 0)
1537 		f2fs_unlock_op(sbi);
1538 
1539 	f2fs_balance_fs(sbi, true);
1540 	return ret;
1541 }
1542 
1543 int f2fs_write_multi_pages(struct compress_ctx *cc,
1544 					int *submitted,
1545 					struct writeback_control *wbc,
1546 					enum iostat_type io_type)
1547 {
1548 	int err;
1549 
1550 	*submitted = 0;
1551 	if (cluster_may_compress(cc)) {
1552 		err = f2fs_compress_pages(cc);
1553 		if (err == -EAGAIN) {
1554 			add_compr_block_stat(cc->inode, cc->cluster_size);
1555 			goto write;
1556 		} else if (err) {
1557 			f2fs_put_rpages_wbc(cc, wbc, true, 1);
1558 			goto destroy_out;
1559 		}
1560 
1561 		err = f2fs_write_compressed_pages(cc, submitted,
1562 							wbc, io_type);
1563 		if (!err)
1564 			return 0;
1565 		f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1566 	}
1567 write:
1568 	f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
1569 
1570 	err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
1571 	f2fs_put_rpages_wbc(cc, wbc, false, 0);
1572 destroy_out:
1573 	f2fs_destroy_compress_ctx(cc, false);
1574 	return err;
1575 }
1576 
1577 static inline bool allow_memalloc_for_decomp(struct f2fs_sb_info *sbi,
1578 		bool pre_alloc)
1579 {
1580 	return pre_alloc ^ f2fs_low_mem_mode(sbi);
1581 }
1582 
1583 static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
1584 		bool pre_alloc)
1585 {
1586 	const struct f2fs_compress_ops *cops =
1587 		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
1588 	int i;
1589 
1590 	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
1591 		return 0;
1592 
1593 	dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
1594 	if (!dic->tpages)
1595 		return -ENOMEM;
1596 
1597 	for (i = 0; i < dic->cluster_size; i++) {
1598 		if (dic->rpages[i]) {
1599 			dic->tpages[i] = dic->rpages[i];
1600 			continue;
1601 		}
1602 
1603 		dic->tpages[i] = f2fs_compress_alloc_page();
1604 	}
1605 
1606 	dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
1607 	if (!dic->rbuf)
1608 		return -ENOMEM;
1609 
1610 	dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
1611 	if (!dic->cbuf)
1612 		return -ENOMEM;
1613 
1614 	if (cops->init_decompress_ctx)
1615 		return cops->init_decompress_ctx(dic);
1616 
1617 	return 0;
1618 }
1619 
1620 static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
1621 		bool bypass_destroy_callback, bool pre_alloc)
1622 {
1623 	const struct f2fs_compress_ops *cops =
1624 		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
1625 
1626 	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
1627 		return;
1628 
1629 	if (!bypass_destroy_callback && cops->destroy_decompress_ctx)
1630 		cops->destroy_decompress_ctx(dic);
1631 
1632 	if (dic->cbuf)
1633 		vm_unmap_ram(dic->cbuf, dic->nr_cpages);
1634 
1635 	if (dic->rbuf)
1636 		vm_unmap_ram(dic->rbuf, dic->cluster_size);
1637 }
1638 
1639 static void f2fs_free_dic(struct decompress_io_ctx *dic,
1640 		bool bypass_destroy_callback);
1641 
1642 struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1643 {
1644 	struct decompress_io_ctx *dic;
1645 	pgoff_t start_idx = start_idx_of_cluster(cc);
1646 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1647 	int i, ret;
1648 
1649 	dic = f2fs_kmem_cache_alloc(dic_entry_slab, GFP_F2FS_ZERO, false, sbi);
1650 	if (!dic)
1651 		return ERR_PTR(-ENOMEM);
1652 
1653 	dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1654 	if (!dic->rpages) {
1655 		kmem_cache_free(dic_entry_slab, dic);
1656 		return ERR_PTR(-ENOMEM);
1657 	}
1658 
1659 	dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1660 	dic->inode = cc->inode;
1661 	atomic_set(&dic->remaining_pages, cc->nr_cpages);
1662 	dic->cluster_idx = cc->cluster_idx;
1663 	dic->cluster_size = cc->cluster_size;
1664 	dic->log_cluster_size = cc->log_cluster_size;
1665 	dic->nr_cpages = cc->nr_cpages;
1666 	refcount_set(&dic->refcnt, 1);
1667 	dic->failed = false;
1668 	dic->need_verity = f2fs_need_verity(cc->inode, start_idx);
1669 
1670 	for (i = 0; i < dic->cluster_size; i++)
1671 		dic->rpages[i] = cc->rpages[i];
1672 	dic->nr_rpages = cc->cluster_size;
1673 
1674 	dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
1675 	if (!dic->cpages) {
1676 		ret = -ENOMEM;
1677 		goto out_free;
1678 	}
1679 
1680 	for (i = 0; i < dic->nr_cpages; i++) {
1681 		struct page *page;
1682 
1683 		page = f2fs_compress_alloc_page();
1684 		f2fs_set_compressed_page(page, cc->inode,
1685 					start_idx + i + 1, dic);
1686 		dic->cpages[i] = page;
1687 	}
1688 
1689 	ret = f2fs_prepare_decomp_mem(dic, true);
1690 	if (ret)
1691 		goto out_free;
1692 
1693 	return dic;
1694 
1695 out_free:
1696 	f2fs_free_dic(dic, true);
1697 	return ERR_PTR(ret);
1698 }
1699 
1700 static void f2fs_free_dic(struct decompress_io_ctx *dic,
1701 		bool bypass_destroy_callback)
1702 {
1703 	int i;
1704 
1705 	f2fs_release_decomp_mem(dic, bypass_destroy_callback, true);
1706 
1707 	if (dic->tpages) {
1708 		for (i = 0; i < dic->cluster_size; i++) {
1709 			if (dic->rpages[i])
1710 				continue;
1711 			if (!dic->tpages[i])
1712 				continue;
1713 			f2fs_compress_free_page(dic->tpages[i]);
1714 		}
1715 		page_array_free(dic->inode, dic->tpages, dic->cluster_size);
1716 	}
1717 
1718 	if (dic->cpages) {
1719 		for (i = 0; i < dic->nr_cpages; i++) {
1720 			if (!dic->cpages[i])
1721 				continue;
1722 			f2fs_compress_free_page(dic->cpages[i]);
1723 		}
1724 		page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
1725 	}
1726 
1727 	page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
1728 	kmem_cache_free(dic_entry_slab, dic);
1729 }
1730 
1731 static void f2fs_late_free_dic(struct work_struct *work)
1732 {
1733 	struct decompress_io_ctx *dic =
1734 		container_of(work, struct decompress_io_ctx, free_work);
1735 
1736 	f2fs_free_dic(dic, false);
1737 }
1738 
1739 static void f2fs_put_dic(struct decompress_io_ctx *dic, bool in_task)
1740 {
1741 	if (refcount_dec_and_test(&dic->refcnt)) {
1742 		if (in_task) {
1743 			f2fs_free_dic(dic, false);
1744 		} else {
1745 			INIT_WORK(&dic->free_work, f2fs_late_free_dic);
1746 			queue_work(F2FS_I_SB(dic->inode)->post_read_wq,
1747 					&dic->free_work);
1748 		}
1749 	}
1750 }
1751 
1752 static void f2fs_verify_cluster(struct work_struct *work)
1753 {
1754 	struct decompress_io_ctx *dic =
1755 		container_of(work, struct decompress_io_ctx, verity_work);
1756 	int i;
1757 
1758 	/* Verify, update, and unlock the decompressed pages. */
1759 	for (i = 0; i < dic->cluster_size; i++) {
1760 		struct page *rpage = dic->rpages[i];
1761 
1762 		if (!rpage)
1763 			continue;
1764 
1765 		if (fsverity_verify_page(rpage))
1766 			SetPageUptodate(rpage);
1767 		else
1768 			ClearPageUptodate(rpage);
1769 		unlock_page(rpage);
1770 	}
1771 
1772 	f2fs_put_dic(dic, true);
1773 }
1774 
1775 /*
1776  * This is called when a compressed cluster has been decompressed
1777  * (or failed to be read and/or decompressed).
1778  */
1779 void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
1780 				bool in_task)
1781 {
1782 	int i;
1783 
1784 	if (!failed && dic->need_verity) {
1785 		/*
1786 		 * Note that to avoid deadlocks, the verity work can't be done
1787 		 * on the decompression workqueue.  This is because verifying
1788 		 * the data pages can involve reading metadata pages from the
1789 		 * file, and these metadata pages may be compressed.
1790 		 */
1791 		INIT_WORK(&dic->verity_work, f2fs_verify_cluster);
1792 		fsverity_enqueue_verify_work(&dic->verity_work);
1793 		return;
1794 	}
1795 
1796 	/* Update and unlock the cluster's pagecache pages. */
1797 	for (i = 0; i < dic->cluster_size; i++) {
1798 		struct page *rpage = dic->rpages[i];
1799 
1800 		if (!rpage)
1801 			continue;
1802 
1803 		if (failed)
1804 			ClearPageUptodate(rpage);
1805 		else
1806 			SetPageUptodate(rpage);
1807 		unlock_page(rpage);
1808 	}
1809 
1810 	/*
1811 	 * Release the reference to the decompress_io_ctx that was being held
1812 	 * for I/O completion.
1813 	 */
1814 	f2fs_put_dic(dic, in_task);
1815 }
1816 
1817 /*
1818  * Put a reference to a compressed page's decompress_io_ctx.
1819  *
1820  * This is called when the page is no longer needed and can be freed.
1821  */
1822 void f2fs_put_page_dic(struct page *page, bool in_task)
1823 {
1824 	struct decompress_io_ctx *dic =
1825 			(struct decompress_io_ctx *)page_private(page);
1826 
1827 	f2fs_put_dic(dic, in_task);
1828 }
1829 
1830 /*
1831  * check whether cluster blocks are contiguous, and add extent cache entry
1832  * only if cluster blocks are logically and physically contiguous.
1833  */
1834 unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,
1835 						unsigned int ofs_in_node)
1836 {
1837 	bool compressed = data_blkaddr(dn->inode, dn->node_page,
1838 					ofs_in_node) == COMPRESS_ADDR;
1839 	int i = compressed ? 1 : 0;
1840 	block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_page,
1841 							ofs_in_node + i);
1842 
1843 	for (i += 1; i < F2FS_I(dn->inode)->i_cluster_size; i++) {
1844 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
1845 							ofs_in_node + i);
1846 
1847 		if (!__is_valid_data_blkaddr(blkaddr))
1848 			break;
1849 		if (first_blkaddr + i - (compressed ? 1 : 0) != blkaddr)
1850 			return 0;
1851 	}
1852 
1853 	return compressed ? i - 1 : i;
1854 }
1855 
1856 const struct address_space_operations f2fs_compress_aops = {
1857 	.release_folio = f2fs_release_folio,
1858 	.invalidate_folio = f2fs_invalidate_folio,
1859 	.migrate_folio	= filemap_migrate_folio,
1860 };
1861 
1862 struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
1863 {
1864 	return sbi->compress_inode->i_mapping;
1865 }
1866 
1867 void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
1868 {
1869 	if (!sbi->compress_inode)
1870 		return;
1871 	invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
1872 }
1873 
1874 void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
1875 						nid_t ino, block_t blkaddr)
1876 {
1877 	struct page *cpage;
1878 	int ret;
1879 
1880 	if (!test_opt(sbi, COMPRESS_CACHE))
1881 		return;
1882 
1883 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
1884 		return;
1885 
1886 	if (!f2fs_available_free_memory(sbi, COMPRESS_PAGE))
1887 		return;
1888 
1889 	cpage = find_get_page(COMPRESS_MAPPING(sbi), blkaddr);
1890 	if (cpage) {
1891 		f2fs_put_page(cpage, 0);
1892 		return;
1893 	}
1894 
1895 	cpage = alloc_page(__GFP_NOWARN | __GFP_IO);
1896 	if (!cpage)
1897 		return;
1898 
1899 	ret = add_to_page_cache_lru(cpage, COMPRESS_MAPPING(sbi),
1900 						blkaddr, GFP_NOFS);
1901 	if (ret) {
1902 		f2fs_put_page(cpage, 0);
1903 		return;
1904 	}
1905 
1906 	set_page_private_data(cpage, ino);
1907 
1908 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
1909 		goto out;
1910 
1911 	memcpy(page_address(cpage), page_address(page), PAGE_SIZE);
1912 	SetPageUptodate(cpage);
1913 out:
1914 	f2fs_put_page(cpage, 1);
1915 }
1916 
1917 bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
1918 								block_t blkaddr)
1919 {
1920 	struct page *cpage;
1921 	bool hitted = false;
1922 
1923 	if (!test_opt(sbi, COMPRESS_CACHE))
1924 		return false;
1925 
1926 	cpage = f2fs_pagecache_get_page(COMPRESS_MAPPING(sbi),
1927 				blkaddr, FGP_LOCK | FGP_NOWAIT, GFP_NOFS);
1928 	if (cpage) {
1929 		if (PageUptodate(cpage)) {
1930 			atomic_inc(&sbi->compress_page_hit);
1931 			memcpy(page_address(page),
1932 				page_address(cpage), PAGE_SIZE);
1933 			hitted = true;
1934 		}
1935 		f2fs_put_page(cpage, 1);
1936 	}
1937 
1938 	return hitted;
1939 }
1940 
1941 void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino)
1942 {
1943 	struct address_space *mapping = COMPRESS_MAPPING(sbi);
1944 	struct folio_batch fbatch;
1945 	pgoff_t index = 0;
1946 	pgoff_t end = MAX_BLKADDR(sbi);
1947 
1948 	if (!mapping->nrpages)
1949 		return;
1950 
1951 	folio_batch_init(&fbatch);
1952 
1953 	do {
1954 		unsigned int nr, i;
1955 
1956 		nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
1957 		if (!nr)
1958 			break;
1959 
1960 		for (i = 0; i < nr; i++) {
1961 			struct folio *folio = fbatch.folios[i];
1962 
1963 			folio_lock(folio);
1964 			if (folio->mapping != mapping) {
1965 				folio_unlock(folio);
1966 				continue;
1967 			}
1968 
1969 			if (ino != get_page_private_data(&folio->page)) {
1970 				folio_unlock(folio);
1971 				continue;
1972 			}
1973 
1974 			generic_error_remove_page(mapping, &folio->page);
1975 			folio_unlock(folio);
1976 		}
1977 		folio_batch_release(&fbatch);
1978 		cond_resched();
1979 	} while (index < end);
1980 }
1981 
1982 int f2fs_init_compress_inode(struct f2fs_sb_info *sbi)
1983 {
1984 	struct inode *inode;
1985 
1986 	if (!test_opt(sbi, COMPRESS_CACHE))
1987 		return 0;
1988 
1989 	inode = f2fs_iget(sbi->sb, F2FS_COMPRESS_INO(sbi));
1990 	if (IS_ERR(inode))
1991 		return PTR_ERR(inode);
1992 	sbi->compress_inode = inode;
1993 
1994 	sbi->compress_percent = COMPRESS_PERCENT;
1995 	sbi->compress_watermark = COMPRESS_WATERMARK;
1996 
1997 	atomic_set(&sbi->compress_page_hit, 0);
1998 
1999 	return 0;
2000 }
2001 
2002 void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi)
2003 {
2004 	if (!sbi->compress_inode)
2005 		return;
2006 	iput(sbi->compress_inode);
2007 	sbi->compress_inode = NULL;
2008 }
2009 
2010 int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
2011 {
2012 	dev_t dev = sbi->sb->s_bdev->bd_dev;
2013 	char slab_name[35];
2014 
2015 	if (!f2fs_sb_has_compression(sbi))
2016 		return 0;
2017 
2018 	sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
2019 
2020 	sbi->page_array_slab_size = sizeof(struct page *) <<
2021 					F2FS_OPTION(sbi).compress_log_size;
2022 
2023 	sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
2024 					sbi->page_array_slab_size);
2025 	return sbi->page_array_slab ? 0 : -ENOMEM;
2026 }
2027 
2028 void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
2029 {
2030 	kmem_cache_destroy(sbi->page_array_slab);
2031 }
2032 
2033 int __init f2fs_init_compress_cache(void)
2034 {
2035 	cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
2036 					sizeof(struct compress_io_ctx));
2037 	if (!cic_entry_slab)
2038 		return -ENOMEM;
2039 	dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
2040 					sizeof(struct decompress_io_ctx));
2041 	if (!dic_entry_slab)
2042 		goto free_cic;
2043 	return 0;
2044 free_cic:
2045 	kmem_cache_destroy(cic_entry_slab);
2046 	return -ENOMEM;
2047 }
2048 
2049 void f2fs_destroy_compress_cache(void)
2050 {
2051 	kmem_cache_destroy(dic_entry_slab);
2052 	kmem_cache_destroy(cic_entry_slab);
2053 }
2054