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