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