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