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