xref: /linux/fs/f2fs/compress.c (revision 9557b4376d02088a33e5f4116bcc324d35a3b64c)
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 = 1, count = 1; 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,
960 				unsigned int cluster_idx, bool compr_blks)
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 (compr_blks)
982 			ret = __f2fs_get_cluster_blocks(inode, &dn);
983 		else
984 			ret = 1;
985 	}
986 fail:
987 	f2fs_put_dnode(&dn);
988 	return ret;
989 }
990 
991 /* return # of compressed blocks in compressed cluster */
992 static int f2fs_compressed_blocks(struct compress_ctx *cc)
993 {
994 	return __f2fs_cluster_blocks(cc->inode, cc->cluster_idx, true);
995 }
996 
997 /* return whether cluster is compressed one or not */
998 int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
999 {
1000 	return __f2fs_cluster_blocks(inode,
1001 		index >> F2FS_I(inode)->i_log_cluster_size,
1002 		false);
1003 }
1004 
1005 static bool cluster_may_compress(struct compress_ctx *cc)
1006 {
1007 	if (!f2fs_need_compress_data(cc->inode))
1008 		return false;
1009 	if (f2fs_is_atomic_file(cc->inode))
1010 		return false;
1011 	if (!f2fs_cluster_is_full(cc))
1012 		return false;
1013 	if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
1014 		return false;
1015 	return !cluster_has_invalid_data(cc);
1016 }
1017 
1018 static void set_cluster_writeback(struct compress_ctx *cc)
1019 {
1020 	int i;
1021 
1022 	for (i = 0; i < cc->cluster_size; i++) {
1023 		if (cc->rpages[i])
1024 			set_page_writeback(cc->rpages[i]);
1025 	}
1026 }
1027 
1028 static void cancel_cluster_writeback(struct compress_ctx *cc,
1029 			struct compress_io_ctx *cic, int submitted)
1030 {
1031 	int i;
1032 
1033 	/* Wait for submitted IOs. */
1034 	if (submitted > 1) {
1035 		f2fs_submit_merged_write(F2FS_I_SB(cc->inode), DATA);
1036 		while (atomic_read(&cic->pending_pages) !=
1037 					(cc->valid_nr_cpages - submitted + 1))
1038 			f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1039 	}
1040 
1041 	/* Cancel writeback and stay locked. */
1042 	for (i = 0; i < cc->cluster_size; i++) {
1043 		if (i < submitted) {
1044 			inode_inc_dirty_pages(cc->inode);
1045 			lock_page(cc->rpages[i]);
1046 		}
1047 		clear_page_private_gcing(cc->rpages[i]);
1048 		if (folio_test_writeback(page_folio(cc->rpages[i])))
1049 			end_page_writeback(cc->rpages[i]);
1050 	}
1051 }
1052 
1053 static void set_cluster_dirty(struct compress_ctx *cc)
1054 {
1055 	int i;
1056 
1057 	for (i = 0; i < cc->cluster_size; i++)
1058 		if (cc->rpages[i]) {
1059 			set_page_dirty(cc->rpages[i]);
1060 			set_page_private_gcing(cc->rpages[i]);
1061 		}
1062 }
1063 
1064 static int prepare_compress_overwrite(struct compress_ctx *cc,
1065 		struct page **pagep, pgoff_t index, void **fsdata)
1066 {
1067 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1068 	struct address_space *mapping = cc->inode->i_mapping;
1069 	struct page *page;
1070 	sector_t last_block_in_bio;
1071 	fgf_t fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
1072 	pgoff_t start_idx = start_idx_of_cluster(cc);
1073 	int i, ret;
1074 
1075 retry:
1076 	ret = f2fs_is_compressed_cluster(cc->inode, start_idx);
1077 	if (ret <= 0)
1078 		return ret;
1079 
1080 	ret = f2fs_init_compress_ctx(cc);
1081 	if (ret)
1082 		return ret;
1083 
1084 	/* keep page reference to avoid page reclaim */
1085 	for (i = 0; i < cc->cluster_size; i++) {
1086 		page = f2fs_pagecache_get_page(mapping, start_idx + i,
1087 							fgp_flag, GFP_NOFS);
1088 		if (!page) {
1089 			ret = -ENOMEM;
1090 			goto unlock_pages;
1091 		}
1092 
1093 		if (PageUptodate(page))
1094 			f2fs_put_page(page, 1);
1095 		else
1096 			f2fs_compress_ctx_add_page(cc, page);
1097 	}
1098 
1099 	if (!f2fs_cluster_is_empty(cc)) {
1100 		struct bio *bio = NULL;
1101 
1102 		ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
1103 					&last_block_in_bio, NULL, true);
1104 		f2fs_put_rpages(cc);
1105 		f2fs_destroy_compress_ctx(cc, true);
1106 		if (ret)
1107 			goto out;
1108 		if (bio)
1109 			f2fs_submit_read_bio(sbi, bio, DATA);
1110 
1111 		ret = f2fs_init_compress_ctx(cc);
1112 		if (ret)
1113 			goto out;
1114 	}
1115 
1116 	for (i = 0; i < cc->cluster_size; i++) {
1117 		f2fs_bug_on(sbi, cc->rpages[i]);
1118 
1119 		page = find_lock_page(mapping, start_idx + i);
1120 		if (!page) {
1121 			/* page can be truncated */
1122 			goto release_and_retry;
1123 		}
1124 
1125 		f2fs_wait_on_page_writeback(page, DATA, true, true);
1126 		f2fs_compress_ctx_add_page(cc, page);
1127 
1128 		if (!PageUptodate(page)) {
1129 release_and_retry:
1130 			f2fs_put_rpages(cc);
1131 			f2fs_unlock_rpages(cc, i + 1);
1132 			f2fs_destroy_compress_ctx(cc, true);
1133 			goto retry;
1134 		}
1135 	}
1136 
1137 	if (likely(!ret)) {
1138 		*fsdata = cc->rpages;
1139 		*pagep = cc->rpages[offset_in_cluster(cc, index)];
1140 		return cc->cluster_size;
1141 	}
1142 
1143 unlock_pages:
1144 	f2fs_put_rpages(cc);
1145 	f2fs_unlock_rpages(cc, i);
1146 	f2fs_destroy_compress_ctx(cc, true);
1147 out:
1148 	return ret;
1149 }
1150 
1151 int f2fs_prepare_compress_overwrite(struct inode *inode,
1152 		struct page **pagep, pgoff_t index, void **fsdata)
1153 {
1154 	struct compress_ctx cc = {
1155 		.inode = inode,
1156 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1157 		.cluster_size = F2FS_I(inode)->i_cluster_size,
1158 		.cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
1159 		.rpages = NULL,
1160 		.nr_rpages = 0,
1161 	};
1162 
1163 	return prepare_compress_overwrite(&cc, pagep, index, fsdata);
1164 }
1165 
1166 bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
1167 					pgoff_t index, unsigned copied)
1168 
1169 {
1170 	struct compress_ctx cc = {
1171 		.inode = inode,
1172 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1173 		.cluster_size = F2FS_I(inode)->i_cluster_size,
1174 		.rpages = fsdata,
1175 	};
1176 	bool first_index = (index == cc.rpages[0]->index);
1177 
1178 	if (copied)
1179 		set_cluster_dirty(&cc);
1180 
1181 	f2fs_put_rpages_wbc(&cc, NULL, false, 1);
1182 	f2fs_destroy_compress_ctx(&cc, false);
1183 
1184 	return first_index;
1185 }
1186 
1187 int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
1188 {
1189 	void *fsdata = NULL;
1190 	struct page *pagep;
1191 	int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
1192 	pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
1193 							log_cluster_size;
1194 	int err;
1195 
1196 	err = f2fs_is_compressed_cluster(inode, start_idx);
1197 	if (err < 0)
1198 		return err;
1199 
1200 	/* truncate normal cluster */
1201 	if (!err)
1202 		return f2fs_do_truncate_blocks(inode, from, lock);
1203 
1204 	/* truncate compressed cluster */
1205 	err = f2fs_prepare_compress_overwrite(inode, &pagep,
1206 						start_idx, &fsdata);
1207 
1208 	/* should not be a normal cluster */
1209 	f2fs_bug_on(F2FS_I_SB(inode), err == 0);
1210 
1211 	if (err <= 0)
1212 		return err;
1213 
1214 	if (err > 0) {
1215 		struct page **rpages = fsdata;
1216 		int cluster_size = F2FS_I(inode)->i_cluster_size;
1217 		int i;
1218 
1219 		for (i = cluster_size - 1; i >= 0; i--) {
1220 			loff_t start = rpages[i]->index << PAGE_SHIFT;
1221 
1222 			if (from <= start) {
1223 				zero_user_segment(rpages[i], 0, PAGE_SIZE);
1224 			} else {
1225 				zero_user_segment(rpages[i], from - start,
1226 								PAGE_SIZE);
1227 				break;
1228 			}
1229 		}
1230 
1231 		f2fs_compress_write_end(inode, fsdata, start_idx, true);
1232 	}
1233 	return 0;
1234 }
1235 
1236 static int f2fs_write_compressed_pages(struct compress_ctx *cc,
1237 					int *submitted,
1238 					struct writeback_control *wbc,
1239 					enum iostat_type io_type)
1240 {
1241 	struct inode *inode = cc->inode;
1242 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1243 	struct f2fs_inode_info *fi = F2FS_I(inode);
1244 	struct f2fs_io_info fio = {
1245 		.sbi = sbi,
1246 		.ino = cc->inode->i_ino,
1247 		.type = DATA,
1248 		.op = REQ_OP_WRITE,
1249 		.op_flags = wbc_to_write_flags(wbc),
1250 		.old_blkaddr = NEW_ADDR,
1251 		.page = NULL,
1252 		.encrypted_page = NULL,
1253 		.compressed_page = NULL,
1254 		.io_type = io_type,
1255 		.io_wbc = wbc,
1256 		.encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode) ?
1257 									1 : 0,
1258 	};
1259 	struct dnode_of_data dn;
1260 	struct node_info ni;
1261 	struct compress_io_ctx *cic;
1262 	pgoff_t start_idx = start_idx_of_cluster(cc);
1263 	unsigned int last_index = cc->cluster_size - 1;
1264 	loff_t psize;
1265 	int i, err;
1266 	bool quota_inode = IS_NOQUOTA(inode);
1267 
1268 	/* we should bypass data pages to proceed the kworker jobs */
1269 	if (unlikely(f2fs_cp_error(sbi))) {
1270 		mapping_set_error(cc->rpages[0]->mapping, -EIO);
1271 		goto out_free;
1272 	}
1273 
1274 	if (quota_inode) {
1275 		/*
1276 		 * We need to wait for node_write to avoid block allocation during
1277 		 * checkpoint. This can only happen to quota writes which can cause
1278 		 * the below discard race condition.
1279 		 */
1280 		f2fs_down_read(&sbi->node_write);
1281 	} else if (!f2fs_trylock_op(sbi)) {
1282 		goto out_free;
1283 	}
1284 
1285 	set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1286 
1287 	err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
1288 	if (err)
1289 		goto out_unlock_op;
1290 
1291 	for (i = 0; i < cc->cluster_size; i++) {
1292 		if (data_blkaddr(dn.inode, dn.node_page,
1293 					dn.ofs_in_node + i) == NULL_ADDR)
1294 			goto out_put_dnode;
1295 	}
1296 
1297 	psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
1298 
1299 	err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
1300 	if (err)
1301 		goto out_put_dnode;
1302 
1303 	fio.version = ni.version;
1304 
1305 	cic = f2fs_kmem_cache_alloc(cic_entry_slab, GFP_F2FS_ZERO, false, sbi);
1306 	if (!cic)
1307 		goto out_put_dnode;
1308 
1309 	cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1310 	cic->inode = inode;
1311 	atomic_set(&cic->pending_pages, cc->valid_nr_cpages);
1312 	cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1313 	if (!cic->rpages)
1314 		goto out_put_cic;
1315 
1316 	cic->nr_rpages = cc->cluster_size;
1317 
1318 	for (i = 0; i < cc->valid_nr_cpages; i++) {
1319 		f2fs_set_compressed_page(cc->cpages[i], inode,
1320 					cc->rpages[i + 1]->index, cic);
1321 		fio.compressed_page = cc->cpages[i];
1322 
1323 		fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1324 						dn.ofs_in_node + i + 1);
1325 
1326 		/* wait for GCed page writeback via META_MAPPING */
1327 		f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1328 
1329 		if (fio.encrypted) {
1330 			fio.page = cc->rpages[i + 1];
1331 			err = f2fs_encrypt_one_page(&fio);
1332 			if (err)
1333 				goto out_destroy_crypt;
1334 			cc->cpages[i] = fio.encrypted_page;
1335 		}
1336 	}
1337 
1338 	set_cluster_writeback(cc);
1339 
1340 	for (i = 0; i < cc->cluster_size; i++)
1341 		cic->rpages[i] = cc->rpages[i];
1342 
1343 	for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
1344 		block_t blkaddr;
1345 
1346 		blkaddr = f2fs_data_blkaddr(&dn);
1347 		fio.page = cc->rpages[i];
1348 		fio.old_blkaddr = blkaddr;
1349 
1350 		/* cluster header */
1351 		if (i == 0) {
1352 			if (blkaddr == COMPRESS_ADDR)
1353 				fio.compr_blocks++;
1354 			if (__is_valid_data_blkaddr(blkaddr))
1355 				f2fs_invalidate_blocks(sbi, blkaddr);
1356 			f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
1357 			goto unlock_continue;
1358 		}
1359 
1360 		if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
1361 			fio.compr_blocks++;
1362 
1363 		if (i > cc->valid_nr_cpages) {
1364 			if (__is_valid_data_blkaddr(blkaddr)) {
1365 				f2fs_invalidate_blocks(sbi, blkaddr);
1366 				f2fs_update_data_blkaddr(&dn, NEW_ADDR);
1367 			}
1368 			goto unlock_continue;
1369 		}
1370 
1371 		f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
1372 
1373 		if (fio.encrypted)
1374 			fio.encrypted_page = cc->cpages[i - 1];
1375 		else
1376 			fio.compressed_page = cc->cpages[i - 1];
1377 
1378 		cc->cpages[i - 1] = NULL;
1379 		fio.submitted = 0;
1380 		f2fs_outplace_write_data(&dn, &fio);
1381 		if (unlikely(!fio.submitted)) {
1382 			cancel_cluster_writeback(cc, cic, i);
1383 
1384 			/* To call fscrypt_finalize_bounce_page */
1385 			i = cc->valid_nr_cpages;
1386 			*submitted = 0;
1387 			goto out_destroy_crypt;
1388 		}
1389 		(*submitted)++;
1390 unlock_continue:
1391 		inode_dec_dirty_pages(cc->inode);
1392 		unlock_page(fio.page);
1393 	}
1394 
1395 	if (fio.compr_blocks)
1396 		f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
1397 	f2fs_i_compr_blocks_update(inode, cc->valid_nr_cpages, true);
1398 	add_compr_block_stat(inode, cc->valid_nr_cpages);
1399 
1400 	set_inode_flag(cc->inode, FI_APPEND_WRITE);
1401 
1402 	f2fs_put_dnode(&dn);
1403 	if (quota_inode)
1404 		f2fs_up_read(&sbi->node_write);
1405 	else
1406 		f2fs_unlock_op(sbi);
1407 
1408 	spin_lock(&fi->i_size_lock);
1409 	if (fi->last_disk_size < psize)
1410 		fi->last_disk_size = psize;
1411 	spin_unlock(&fi->i_size_lock);
1412 
1413 	f2fs_put_rpages(cc);
1414 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1415 	cc->cpages = NULL;
1416 	f2fs_destroy_compress_ctx(cc, false);
1417 	return 0;
1418 
1419 out_destroy_crypt:
1420 	page_array_free(cc->inode, cic->rpages, cc->cluster_size);
1421 
1422 	for (--i; i >= 0; i--) {
1423 		if (!cc->cpages[i])
1424 			continue;
1425 		fscrypt_finalize_bounce_page(&cc->cpages[i]);
1426 	}
1427 out_put_cic:
1428 	kmem_cache_free(cic_entry_slab, cic);
1429 out_put_dnode:
1430 	f2fs_put_dnode(&dn);
1431 out_unlock_op:
1432 	if (quota_inode)
1433 		f2fs_up_read(&sbi->node_write);
1434 	else
1435 		f2fs_unlock_op(sbi);
1436 out_free:
1437 	for (i = 0; i < cc->valid_nr_cpages; i++) {
1438 		f2fs_compress_free_page(cc->cpages[i]);
1439 		cc->cpages[i] = NULL;
1440 	}
1441 	page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1442 	cc->cpages = NULL;
1443 	return -EAGAIN;
1444 }
1445 
1446 void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
1447 {
1448 	struct f2fs_sb_info *sbi = bio->bi_private;
1449 	struct compress_io_ctx *cic =
1450 			(struct compress_io_ctx *)page_private(page);
1451 	enum count_type type = WB_DATA_TYPE(page,
1452 				f2fs_is_compressed_page(page));
1453 	int i;
1454 
1455 	if (unlikely(bio->bi_status))
1456 		mapping_set_error(cic->inode->i_mapping, -EIO);
1457 
1458 	f2fs_compress_free_page(page);
1459 
1460 	dec_page_count(sbi, type);
1461 
1462 	if (atomic_dec_return(&cic->pending_pages))
1463 		return;
1464 
1465 	for (i = 0; i < cic->nr_rpages; i++) {
1466 		WARN_ON(!cic->rpages[i]);
1467 		clear_page_private_gcing(cic->rpages[i]);
1468 		end_page_writeback(cic->rpages[i]);
1469 	}
1470 
1471 	page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
1472 	kmem_cache_free(cic_entry_slab, cic);
1473 }
1474 
1475 static int f2fs_write_raw_pages(struct compress_ctx *cc,
1476 					int *submitted_p,
1477 					struct writeback_control *wbc,
1478 					enum iostat_type io_type)
1479 {
1480 	struct address_space *mapping = cc->inode->i_mapping;
1481 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
1482 	int submitted, compr_blocks, i;
1483 	int ret = 0;
1484 
1485 	compr_blocks = f2fs_compressed_blocks(cc);
1486 
1487 	for (i = 0; i < cc->cluster_size; i++) {
1488 		if (!cc->rpages[i])
1489 			continue;
1490 
1491 		redirty_page_for_writepage(wbc, cc->rpages[i]);
1492 		unlock_page(cc->rpages[i]);
1493 	}
1494 
1495 	if (compr_blocks < 0)
1496 		return compr_blocks;
1497 
1498 	/* overwrite compressed cluster w/ normal cluster */
1499 	if (compr_blocks > 0)
1500 		f2fs_lock_op(sbi);
1501 
1502 	for (i = 0; i < cc->cluster_size; i++) {
1503 		if (!cc->rpages[i])
1504 			continue;
1505 retry_write:
1506 		lock_page(cc->rpages[i]);
1507 
1508 		if (cc->rpages[i]->mapping != mapping) {
1509 continue_unlock:
1510 			unlock_page(cc->rpages[i]);
1511 			continue;
1512 		}
1513 
1514 		if (!PageDirty(cc->rpages[i]))
1515 			goto continue_unlock;
1516 
1517 		if (folio_test_writeback(page_folio(cc->rpages[i]))) {
1518 			if (wbc->sync_mode == WB_SYNC_NONE)
1519 				goto continue_unlock;
1520 			f2fs_wait_on_page_writeback(cc->rpages[i], DATA, true, true);
1521 		}
1522 
1523 		if (!clear_page_dirty_for_io(cc->rpages[i]))
1524 			goto continue_unlock;
1525 
1526 		ret = f2fs_write_single_data_page(cc->rpages[i], &submitted,
1527 						NULL, NULL, wbc, io_type,
1528 						compr_blocks, false);
1529 		if (ret) {
1530 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
1531 				unlock_page(cc->rpages[i]);
1532 				ret = 0;
1533 			} else if (ret == -EAGAIN) {
1534 				ret = 0;
1535 				/*
1536 				 * for quota file, just redirty left pages to
1537 				 * avoid deadlock caused by cluster update race
1538 				 * from foreground operation.
1539 				 */
1540 				if (IS_NOQUOTA(cc->inode))
1541 					goto out;
1542 				f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
1543 				goto retry_write;
1544 			}
1545 			goto out;
1546 		}
1547 
1548 		*submitted_p += submitted;
1549 	}
1550 
1551 out:
1552 	if (compr_blocks > 0)
1553 		f2fs_unlock_op(sbi);
1554 
1555 	f2fs_balance_fs(sbi, true);
1556 	return ret;
1557 }
1558 
1559 int f2fs_write_multi_pages(struct compress_ctx *cc,
1560 					int *submitted,
1561 					struct writeback_control *wbc,
1562 					enum iostat_type io_type)
1563 {
1564 	int err;
1565 
1566 	*submitted = 0;
1567 	if (cluster_may_compress(cc)) {
1568 		err = f2fs_compress_pages(cc);
1569 		if (err == -EAGAIN) {
1570 			add_compr_block_stat(cc->inode, cc->cluster_size);
1571 			goto write;
1572 		} else if (err) {
1573 			f2fs_put_rpages_wbc(cc, wbc, true, 1);
1574 			goto destroy_out;
1575 		}
1576 
1577 		err = f2fs_write_compressed_pages(cc, submitted,
1578 							wbc, io_type);
1579 		if (!err)
1580 			return 0;
1581 		f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1582 	}
1583 write:
1584 	f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
1585 
1586 	err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
1587 	f2fs_put_rpages_wbc(cc, wbc, false, 0);
1588 destroy_out:
1589 	f2fs_destroy_compress_ctx(cc, false);
1590 	return err;
1591 }
1592 
1593 static inline bool allow_memalloc_for_decomp(struct f2fs_sb_info *sbi,
1594 		bool pre_alloc)
1595 {
1596 	return pre_alloc ^ f2fs_low_mem_mode(sbi);
1597 }
1598 
1599 static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
1600 		bool pre_alloc)
1601 {
1602 	const struct f2fs_compress_ops *cops =
1603 		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
1604 	int i;
1605 
1606 	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
1607 		return 0;
1608 
1609 	dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
1610 	if (!dic->tpages)
1611 		return -ENOMEM;
1612 
1613 	for (i = 0; i < dic->cluster_size; i++) {
1614 		if (dic->rpages[i]) {
1615 			dic->tpages[i] = dic->rpages[i];
1616 			continue;
1617 		}
1618 
1619 		dic->tpages[i] = f2fs_compress_alloc_page();
1620 	}
1621 
1622 	dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
1623 	if (!dic->rbuf)
1624 		return -ENOMEM;
1625 
1626 	dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
1627 	if (!dic->cbuf)
1628 		return -ENOMEM;
1629 
1630 	if (cops->init_decompress_ctx)
1631 		return cops->init_decompress_ctx(dic);
1632 
1633 	return 0;
1634 }
1635 
1636 static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
1637 		bool bypass_destroy_callback, bool pre_alloc)
1638 {
1639 	const struct f2fs_compress_ops *cops =
1640 		f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
1641 
1642 	if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
1643 		return;
1644 
1645 	if (!bypass_destroy_callback && cops->destroy_decompress_ctx)
1646 		cops->destroy_decompress_ctx(dic);
1647 
1648 	if (dic->cbuf)
1649 		vm_unmap_ram(dic->cbuf, dic->nr_cpages);
1650 
1651 	if (dic->rbuf)
1652 		vm_unmap_ram(dic->rbuf, dic->cluster_size);
1653 }
1654 
1655 static void f2fs_free_dic(struct decompress_io_ctx *dic,
1656 		bool bypass_destroy_callback);
1657 
1658 struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1659 {
1660 	struct decompress_io_ctx *dic;
1661 	pgoff_t start_idx = start_idx_of_cluster(cc);
1662 	struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1663 	int i, ret;
1664 
1665 	dic = f2fs_kmem_cache_alloc(dic_entry_slab, GFP_F2FS_ZERO, false, sbi);
1666 	if (!dic)
1667 		return ERR_PTR(-ENOMEM);
1668 
1669 	dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1670 	if (!dic->rpages) {
1671 		kmem_cache_free(dic_entry_slab, dic);
1672 		return ERR_PTR(-ENOMEM);
1673 	}
1674 
1675 	dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1676 	dic->inode = cc->inode;
1677 	atomic_set(&dic->remaining_pages, cc->nr_cpages);
1678 	dic->cluster_idx = cc->cluster_idx;
1679 	dic->cluster_size = cc->cluster_size;
1680 	dic->log_cluster_size = cc->log_cluster_size;
1681 	dic->nr_cpages = cc->nr_cpages;
1682 	refcount_set(&dic->refcnt, 1);
1683 	dic->failed = false;
1684 	dic->need_verity = f2fs_need_verity(cc->inode, start_idx);
1685 
1686 	for (i = 0; i < dic->cluster_size; i++)
1687 		dic->rpages[i] = cc->rpages[i];
1688 	dic->nr_rpages = cc->cluster_size;
1689 
1690 	dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
1691 	if (!dic->cpages) {
1692 		ret = -ENOMEM;
1693 		goto out_free;
1694 	}
1695 
1696 	for (i = 0; i < dic->nr_cpages; i++) {
1697 		struct page *page;
1698 
1699 		page = f2fs_compress_alloc_page();
1700 		f2fs_set_compressed_page(page, cc->inode,
1701 					start_idx + i + 1, dic);
1702 		dic->cpages[i] = page;
1703 	}
1704 
1705 	ret = f2fs_prepare_decomp_mem(dic, true);
1706 	if (ret)
1707 		goto out_free;
1708 
1709 	return dic;
1710 
1711 out_free:
1712 	f2fs_free_dic(dic, true);
1713 	return ERR_PTR(ret);
1714 }
1715 
1716 static void f2fs_free_dic(struct decompress_io_ctx *dic,
1717 		bool bypass_destroy_callback)
1718 {
1719 	int i;
1720 
1721 	f2fs_release_decomp_mem(dic, bypass_destroy_callback, true);
1722 
1723 	if (dic->tpages) {
1724 		for (i = 0; i < dic->cluster_size; i++) {
1725 			if (dic->rpages[i])
1726 				continue;
1727 			if (!dic->tpages[i])
1728 				continue;
1729 			f2fs_compress_free_page(dic->tpages[i]);
1730 		}
1731 		page_array_free(dic->inode, dic->tpages, dic->cluster_size);
1732 	}
1733 
1734 	if (dic->cpages) {
1735 		for (i = 0; i < dic->nr_cpages; i++) {
1736 			if (!dic->cpages[i])
1737 				continue;
1738 			f2fs_compress_free_page(dic->cpages[i]);
1739 		}
1740 		page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
1741 	}
1742 
1743 	page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
1744 	kmem_cache_free(dic_entry_slab, dic);
1745 }
1746 
1747 static void f2fs_late_free_dic(struct work_struct *work)
1748 {
1749 	struct decompress_io_ctx *dic =
1750 		container_of(work, struct decompress_io_ctx, free_work);
1751 
1752 	f2fs_free_dic(dic, false);
1753 }
1754 
1755 static void f2fs_put_dic(struct decompress_io_ctx *dic, bool in_task)
1756 {
1757 	if (refcount_dec_and_test(&dic->refcnt)) {
1758 		if (in_task) {
1759 			f2fs_free_dic(dic, false);
1760 		} else {
1761 			INIT_WORK(&dic->free_work, f2fs_late_free_dic);
1762 			queue_work(F2FS_I_SB(dic->inode)->post_read_wq,
1763 					&dic->free_work);
1764 		}
1765 	}
1766 }
1767 
1768 static void f2fs_verify_cluster(struct work_struct *work)
1769 {
1770 	struct decompress_io_ctx *dic =
1771 		container_of(work, struct decompress_io_ctx, verity_work);
1772 	int i;
1773 
1774 	/* Verify, update, and unlock the decompressed pages. */
1775 	for (i = 0; i < dic->cluster_size; i++) {
1776 		struct page *rpage = dic->rpages[i];
1777 
1778 		if (!rpage)
1779 			continue;
1780 
1781 		if (fsverity_verify_page(rpage))
1782 			SetPageUptodate(rpage);
1783 		else
1784 			ClearPageUptodate(rpage);
1785 		unlock_page(rpage);
1786 	}
1787 
1788 	f2fs_put_dic(dic, true);
1789 }
1790 
1791 /*
1792  * This is called when a compressed cluster has been decompressed
1793  * (or failed to be read and/or decompressed).
1794  */
1795 void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
1796 				bool in_task)
1797 {
1798 	int i;
1799 
1800 	if (!failed && dic->need_verity) {
1801 		/*
1802 		 * Note that to avoid deadlocks, the verity work can't be done
1803 		 * on the decompression workqueue.  This is because verifying
1804 		 * the data pages can involve reading metadata pages from the
1805 		 * file, and these metadata pages may be compressed.
1806 		 */
1807 		INIT_WORK(&dic->verity_work, f2fs_verify_cluster);
1808 		fsverity_enqueue_verify_work(&dic->verity_work);
1809 		return;
1810 	}
1811 
1812 	/* Update and unlock the cluster's pagecache pages. */
1813 	for (i = 0; i < dic->cluster_size; i++) {
1814 		struct page *rpage = dic->rpages[i];
1815 
1816 		if (!rpage)
1817 			continue;
1818 
1819 		if (failed)
1820 			ClearPageUptodate(rpage);
1821 		else
1822 			SetPageUptodate(rpage);
1823 		unlock_page(rpage);
1824 	}
1825 
1826 	/*
1827 	 * Release the reference to the decompress_io_ctx that was being held
1828 	 * for I/O completion.
1829 	 */
1830 	f2fs_put_dic(dic, in_task);
1831 }
1832 
1833 /*
1834  * Put a reference to a compressed page's decompress_io_ctx.
1835  *
1836  * This is called when the page is no longer needed and can be freed.
1837  */
1838 void f2fs_put_page_dic(struct page *page, bool in_task)
1839 {
1840 	struct decompress_io_ctx *dic =
1841 			(struct decompress_io_ctx *)page_private(page);
1842 
1843 	f2fs_put_dic(dic, in_task);
1844 }
1845 
1846 /*
1847  * check whether cluster blocks are contiguous, and add extent cache entry
1848  * only if cluster blocks are logically and physically contiguous.
1849  */
1850 unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,
1851 						unsigned int ofs_in_node)
1852 {
1853 	bool compressed = data_blkaddr(dn->inode, dn->node_page,
1854 					ofs_in_node) == COMPRESS_ADDR;
1855 	int i = compressed ? 1 : 0;
1856 	block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_page,
1857 							ofs_in_node + i);
1858 
1859 	for (i += 1; i < F2FS_I(dn->inode)->i_cluster_size; i++) {
1860 		block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
1861 							ofs_in_node + i);
1862 
1863 		if (!__is_valid_data_blkaddr(blkaddr))
1864 			break;
1865 		if (first_blkaddr + i - (compressed ? 1 : 0) != blkaddr)
1866 			return 0;
1867 	}
1868 
1869 	return compressed ? i - 1 : i;
1870 }
1871 
1872 const struct address_space_operations f2fs_compress_aops = {
1873 	.release_folio = f2fs_release_folio,
1874 	.invalidate_folio = f2fs_invalidate_folio,
1875 	.migrate_folio	= filemap_migrate_folio,
1876 };
1877 
1878 struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
1879 {
1880 	return sbi->compress_inode->i_mapping;
1881 }
1882 
1883 void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
1884 {
1885 	if (!sbi->compress_inode)
1886 		return;
1887 	invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
1888 }
1889 
1890 void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
1891 						nid_t ino, block_t blkaddr)
1892 {
1893 	struct page *cpage;
1894 	int ret;
1895 
1896 	if (!test_opt(sbi, COMPRESS_CACHE))
1897 		return;
1898 
1899 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
1900 		return;
1901 
1902 	if (!f2fs_available_free_memory(sbi, COMPRESS_PAGE))
1903 		return;
1904 
1905 	cpage = find_get_page(COMPRESS_MAPPING(sbi), blkaddr);
1906 	if (cpage) {
1907 		f2fs_put_page(cpage, 0);
1908 		return;
1909 	}
1910 
1911 	cpage = alloc_page(__GFP_NOWARN | __GFP_IO);
1912 	if (!cpage)
1913 		return;
1914 
1915 	ret = add_to_page_cache_lru(cpage, COMPRESS_MAPPING(sbi),
1916 						blkaddr, GFP_NOFS);
1917 	if (ret) {
1918 		f2fs_put_page(cpage, 0);
1919 		return;
1920 	}
1921 
1922 	set_page_private_data(cpage, ino);
1923 
1924 	memcpy(page_address(cpage), page_address(page), PAGE_SIZE);
1925 	SetPageUptodate(cpage);
1926 	f2fs_put_page(cpage, 1);
1927 }
1928 
1929 bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
1930 								block_t blkaddr)
1931 {
1932 	struct page *cpage;
1933 	bool hitted = false;
1934 
1935 	if (!test_opt(sbi, COMPRESS_CACHE))
1936 		return false;
1937 
1938 	cpage = f2fs_pagecache_get_page(COMPRESS_MAPPING(sbi),
1939 				blkaddr, FGP_LOCK | FGP_NOWAIT, GFP_NOFS);
1940 	if (cpage) {
1941 		if (PageUptodate(cpage)) {
1942 			atomic_inc(&sbi->compress_page_hit);
1943 			memcpy(page_address(page),
1944 				page_address(cpage), PAGE_SIZE);
1945 			hitted = true;
1946 		}
1947 		f2fs_put_page(cpage, 1);
1948 	}
1949 
1950 	return hitted;
1951 }
1952 
1953 void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino)
1954 {
1955 	struct address_space *mapping = COMPRESS_MAPPING(sbi);
1956 	struct folio_batch fbatch;
1957 	pgoff_t index = 0;
1958 	pgoff_t end = MAX_BLKADDR(sbi);
1959 
1960 	if (!mapping->nrpages)
1961 		return;
1962 
1963 	folio_batch_init(&fbatch);
1964 
1965 	do {
1966 		unsigned int nr, i;
1967 
1968 		nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
1969 		if (!nr)
1970 			break;
1971 
1972 		for (i = 0; i < nr; i++) {
1973 			struct folio *folio = fbatch.folios[i];
1974 
1975 			folio_lock(folio);
1976 			if (folio->mapping != mapping) {
1977 				folio_unlock(folio);
1978 				continue;
1979 			}
1980 
1981 			if (ino != get_page_private_data(&folio->page)) {
1982 				folio_unlock(folio);
1983 				continue;
1984 			}
1985 
1986 			generic_error_remove_folio(mapping, folio);
1987 			folio_unlock(folio);
1988 		}
1989 		folio_batch_release(&fbatch);
1990 		cond_resched();
1991 	} while (index < end);
1992 }
1993 
1994 int f2fs_init_compress_inode(struct f2fs_sb_info *sbi)
1995 {
1996 	struct inode *inode;
1997 
1998 	if (!test_opt(sbi, COMPRESS_CACHE))
1999 		return 0;
2000 
2001 	inode = f2fs_iget(sbi->sb, F2FS_COMPRESS_INO(sbi));
2002 	if (IS_ERR(inode))
2003 		return PTR_ERR(inode);
2004 	sbi->compress_inode = inode;
2005 
2006 	sbi->compress_percent = COMPRESS_PERCENT;
2007 	sbi->compress_watermark = COMPRESS_WATERMARK;
2008 
2009 	atomic_set(&sbi->compress_page_hit, 0);
2010 
2011 	return 0;
2012 }
2013 
2014 void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi)
2015 {
2016 	if (!sbi->compress_inode)
2017 		return;
2018 	iput(sbi->compress_inode);
2019 	sbi->compress_inode = NULL;
2020 }
2021 
2022 int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
2023 {
2024 	dev_t dev = sbi->sb->s_bdev->bd_dev;
2025 	char slab_name[35];
2026 
2027 	if (!f2fs_sb_has_compression(sbi))
2028 		return 0;
2029 
2030 	sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
2031 
2032 	sbi->page_array_slab_size = sizeof(struct page *) <<
2033 					F2FS_OPTION(sbi).compress_log_size;
2034 
2035 	sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
2036 					sbi->page_array_slab_size);
2037 	return sbi->page_array_slab ? 0 : -ENOMEM;
2038 }
2039 
2040 void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
2041 {
2042 	kmem_cache_destroy(sbi->page_array_slab);
2043 }
2044 
2045 int __init f2fs_init_compress_cache(void)
2046 {
2047 	cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
2048 					sizeof(struct compress_io_ctx));
2049 	if (!cic_entry_slab)
2050 		return -ENOMEM;
2051 	dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
2052 					sizeof(struct decompress_io_ctx));
2053 	if (!dic_entry_slab)
2054 		goto free_cic;
2055 	return 0;
2056 free_cic:
2057 	kmem_cache_destroy(cic_entry_slab);
2058 	return -ENOMEM;
2059 }
2060 
2061 void f2fs_destroy_compress_cache(void)
2062 {
2063 	kmem_cache_destroy(dic_entry_slab);
2064 	kmem_cache_destroy(cic_entry_slab);
2065 }
2066