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