1 // SPDX-License-Identifier: GPL-2.0
3 * f2fs compress support
5 * Copyright (c) 2019 Chao Yu <chao@kernel.org>
9 #include <linux/f2fs_fs.h>
10 #include <linux/writeback.h>
11 #include <linux/backing-dev.h>
12 #include <linux/lzo.h>
13 #include <linux/lz4.h>
14 #include <linux/zstd.h>
18 #include <trace/events/f2fs.h>
20 static struct kmem_cache *cic_entry_slab;
21 static struct kmem_cache *dic_entry_slab;
23 static void *page_array_alloc(struct inode *inode, int nr)
25 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
26 unsigned int size = sizeof(struct page *) * nr;
28 if (likely(size <= sbi->page_array_slab_size))
29 return kmem_cache_zalloc(sbi->page_array_slab, GFP_NOFS);
30 return f2fs_kzalloc(sbi, size, GFP_NOFS);
33 static void page_array_free(struct inode *inode, void *pages, int nr)
35 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
36 unsigned int size = sizeof(struct page *) * nr;
41 if (likely(size <= sbi->page_array_slab_size))
42 kmem_cache_free(sbi->page_array_slab, pages);
47 struct f2fs_compress_ops {
48 int (*init_compress_ctx)(struct compress_ctx *cc);
49 void (*destroy_compress_ctx)(struct compress_ctx *cc);
50 int (*compress_pages)(struct compress_ctx *cc);
51 int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
52 void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
53 int (*decompress_pages)(struct decompress_io_ctx *dic);
56 static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
58 return index & (cc->cluster_size - 1);
61 static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
63 return index >> cc->log_cluster_size;
66 static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
68 return cc->cluster_idx << cc->log_cluster_size;
71 bool f2fs_is_compressed_page(struct page *page)
73 if (!PagePrivate(page))
75 if (!page_private(page))
77 if (IS_ATOMIC_WRITTEN_PAGE(page) || IS_DUMMY_WRITTEN_PAGE(page))
80 * page->private may be set with pid.
81 * pid_max is enough to check if it is traced.
83 if (IS_IO_TRACED_PAGE(page))
86 f2fs_bug_on(F2FS_M_SB(page->mapping),
87 *((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
91 static void f2fs_set_compressed_page(struct page *page,
92 struct inode *inode, pgoff_t index, void *data)
95 set_page_private(page, (unsigned long)data);
97 /* i_crypto_info and iv index */
99 page->mapping = inode->i_mapping;
102 static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
106 for (i = 0; i < len; i++) {
110 unlock_page(cc->rpages[i]);
112 put_page(cc->rpages[i]);
116 static void f2fs_put_rpages(struct compress_ctx *cc)
118 f2fs_drop_rpages(cc, cc->cluster_size, false);
121 static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
123 f2fs_drop_rpages(cc, len, true);
126 static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
127 struct writeback_control *wbc, bool redirty, int unlock)
131 for (i = 0; i < cc->cluster_size; i++) {
135 redirty_page_for_writepage(wbc, cc->rpages[i]);
136 f2fs_put_page(cc->rpages[i], unlock);
140 struct page *f2fs_compress_control_page(struct page *page)
142 return ((struct compress_io_ctx *)page_private(page))->rpages[0];
145 int f2fs_init_compress_ctx(struct compress_ctx *cc)
150 cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
151 return cc->rpages ? 0 : -ENOMEM;
154 void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse)
156 page_array_free(cc->inode, cc->rpages, cc->cluster_size);
161 cc->cluster_idx = NULL_CLUSTER;
164 void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
166 unsigned int cluster_ofs;
168 if (!f2fs_cluster_can_merge_page(cc, page->index))
169 f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
171 cluster_ofs = offset_in_cluster(cc, page->index);
172 cc->rpages[cluster_ofs] = page;
174 cc->cluster_idx = cluster_idx(cc, page->index);
177 #ifdef CONFIG_F2FS_FS_LZO
178 static int lzo_init_compress_ctx(struct compress_ctx *cc)
180 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
181 LZO1X_MEM_COMPRESS, GFP_NOFS);
185 cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
189 static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
195 static int lzo_compress_pages(struct compress_ctx *cc)
199 ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
200 &cc->clen, cc->private);
201 if (ret != LZO_E_OK) {
202 printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
203 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
209 static int lzo_decompress_pages(struct decompress_io_ctx *dic)
213 ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
214 dic->rbuf, &dic->rlen);
215 if (ret != LZO_E_OK) {
216 printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
217 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
221 if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
222 printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
223 "expected:%lu\n", KERN_ERR,
224 F2FS_I_SB(dic->inode)->sb->s_id,
226 PAGE_SIZE << dic->log_cluster_size);
232 static const struct f2fs_compress_ops f2fs_lzo_ops = {
233 .init_compress_ctx = lzo_init_compress_ctx,
234 .destroy_compress_ctx = lzo_destroy_compress_ctx,
235 .compress_pages = lzo_compress_pages,
236 .decompress_pages = lzo_decompress_pages,
240 #ifdef CONFIG_F2FS_FS_LZ4
241 static int lz4_init_compress_ctx(struct compress_ctx *cc)
243 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
244 LZ4_MEM_COMPRESS, GFP_NOFS);
249 * we do not change cc->clen to LZ4_compressBound(inputsize) to
250 * adapt worst compress case, because lz4 compressor can handle
251 * output budget properly.
253 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
257 static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
263 static int lz4_compress_pages(struct compress_ctx *cc)
267 len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
268 cc->clen, cc->private);
276 static int lz4_decompress_pages(struct decompress_io_ctx *dic)
280 ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
281 dic->clen, dic->rlen);
283 printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
284 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
288 if (ret != PAGE_SIZE << dic->log_cluster_size) {
289 printk_ratelimited("%sF2FS-fs (%s): lz4 invalid ret:%d, "
290 "expected:%lu\n", KERN_ERR,
291 F2FS_I_SB(dic->inode)->sb->s_id, ret,
292 PAGE_SIZE << dic->log_cluster_size);
298 static const struct f2fs_compress_ops f2fs_lz4_ops = {
299 .init_compress_ctx = lz4_init_compress_ctx,
300 .destroy_compress_ctx = lz4_destroy_compress_ctx,
301 .compress_pages = lz4_compress_pages,
302 .decompress_pages = lz4_decompress_pages,
306 #ifdef CONFIG_F2FS_FS_ZSTD
307 #define F2FS_ZSTD_DEFAULT_CLEVEL 1
309 static int zstd_init_compress_ctx(struct compress_ctx *cc)
311 ZSTD_parameters params;
312 ZSTD_CStream *stream;
314 unsigned int workspace_size;
316 params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen, 0);
317 workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
319 workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
320 workspace_size, GFP_NOFS);
324 stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
326 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n",
327 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
333 cc->private = workspace;
334 cc->private2 = stream;
336 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
340 static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
347 static int zstd_compress_pages(struct compress_ctx *cc)
349 ZSTD_CStream *stream = cc->private2;
351 ZSTD_outBuffer outbuf;
352 int src_size = cc->rlen;
353 int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
357 inbuf.src = cc->rbuf;
358 inbuf.size = src_size;
361 outbuf.dst = cc->cbuf->cdata;
362 outbuf.size = dst_size;
364 ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
365 if (ZSTD_isError(ret)) {
366 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
367 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
368 __func__, ZSTD_getErrorCode(ret));
372 ret = ZSTD_endStream(stream, &outbuf);
373 if (ZSTD_isError(ret)) {
374 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n",
375 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
376 __func__, ZSTD_getErrorCode(ret));
381 * there is compressed data remained in intermediate buffer due to
382 * no more space in cbuf.cdata
387 cc->clen = outbuf.pos;
391 static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
393 ZSTD_DStream *stream;
395 unsigned int workspace_size;
396 unsigned int max_window_size =
397 MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
399 workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size);
401 workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
402 workspace_size, GFP_NOFS);
406 stream = ZSTD_initDStream(max_window_size, workspace, workspace_size);
408 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n",
409 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
415 dic->private = workspace;
416 dic->private2 = stream;
421 static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
423 kvfree(dic->private);
425 dic->private2 = NULL;
428 static int zstd_decompress_pages(struct decompress_io_ctx *dic)
430 ZSTD_DStream *stream = dic->private2;
432 ZSTD_outBuffer outbuf;
436 inbuf.src = dic->cbuf->cdata;
437 inbuf.size = dic->clen;
440 outbuf.dst = dic->rbuf;
441 outbuf.size = dic->rlen;
443 ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
444 if (ZSTD_isError(ret)) {
445 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
446 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
447 __func__, ZSTD_getErrorCode(ret));
451 if (dic->rlen != outbuf.pos) {
452 printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
453 "expected:%lu\n", KERN_ERR,
454 F2FS_I_SB(dic->inode)->sb->s_id,
456 PAGE_SIZE << dic->log_cluster_size);
463 static const struct f2fs_compress_ops f2fs_zstd_ops = {
464 .init_compress_ctx = zstd_init_compress_ctx,
465 .destroy_compress_ctx = zstd_destroy_compress_ctx,
466 .compress_pages = zstd_compress_pages,
467 .init_decompress_ctx = zstd_init_decompress_ctx,
468 .destroy_decompress_ctx = zstd_destroy_decompress_ctx,
469 .decompress_pages = zstd_decompress_pages,
473 #ifdef CONFIG_F2FS_FS_LZO
474 #ifdef CONFIG_F2FS_FS_LZORLE
475 static int lzorle_compress_pages(struct compress_ctx *cc)
479 ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
480 &cc->clen, cc->private);
481 if (ret != LZO_E_OK) {
482 printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d\n",
483 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
489 static const struct f2fs_compress_ops f2fs_lzorle_ops = {
490 .init_compress_ctx = lzo_init_compress_ctx,
491 .destroy_compress_ctx = lzo_destroy_compress_ctx,
492 .compress_pages = lzorle_compress_pages,
493 .decompress_pages = lzo_decompress_pages,
498 static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
499 #ifdef CONFIG_F2FS_FS_LZO
504 #ifdef CONFIG_F2FS_FS_LZ4
509 #ifdef CONFIG_F2FS_FS_ZSTD
514 #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
521 bool f2fs_is_compress_backend_ready(struct inode *inode)
523 if (!f2fs_compressed_file(inode))
525 return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
528 static mempool_t *compress_page_pool;
529 static int num_compress_pages = 512;
530 module_param(num_compress_pages, uint, 0444);
531 MODULE_PARM_DESC(num_compress_pages,
532 "Number of intermediate compress pages to preallocate");
534 int f2fs_init_compress_mempool(void)
536 compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
537 if (!compress_page_pool)
543 void f2fs_destroy_compress_mempool(void)
545 mempool_destroy(compress_page_pool);
548 static struct page *f2fs_compress_alloc_page(void)
552 page = mempool_alloc(compress_page_pool, GFP_NOFS);
558 static void f2fs_compress_free_page(struct page *page)
562 set_page_private(page, (unsigned long)NULL);
563 ClearPagePrivate(page);
564 page->mapping = NULL;
566 mempool_free(page, compress_page_pool);
569 #define MAX_VMAP_RETRIES 3
571 static void *f2fs_vmap(struct page **pages, unsigned int count)
576 for (i = 0; i < MAX_VMAP_RETRIES; i++) {
577 buf = vm_map_ram(pages, count, -1);
585 static int f2fs_compress_pages(struct compress_ctx *cc)
587 struct f2fs_inode_info *fi = F2FS_I(cc->inode);
588 const struct f2fs_compress_ops *cops =
589 f2fs_cops[fi->i_compress_algorithm];
590 unsigned int max_len, new_nr_cpages;
591 struct page **new_cpages;
594 trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
595 cc->cluster_size, fi->i_compress_algorithm);
597 if (cops->init_compress_ctx) {
598 ret = cops->init_compress_ctx(cc);
603 max_len = COMPRESS_HEADER_SIZE + cc->clen;
604 cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
606 cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
609 goto destroy_compress_ctx;
612 for (i = 0; i < cc->nr_cpages; i++) {
613 cc->cpages[i] = f2fs_compress_alloc_page();
614 if (!cc->cpages[i]) {
616 goto out_free_cpages;
620 cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
623 goto out_free_cpages;
626 cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
629 goto out_vunmap_rbuf;
632 ret = cops->compress_pages(cc);
634 goto out_vunmap_cbuf;
636 max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
638 if (cc->clen > max_len) {
640 goto out_vunmap_cbuf;
643 cc->cbuf->clen = cpu_to_le32(cc->clen);
645 for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
646 cc->cbuf->reserved[i] = cpu_to_le32(0);
648 new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
650 /* Now we're going to cut unnecessary tail pages */
651 new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
654 goto out_vunmap_cbuf;
657 /* zero out any unused part of the last page */
658 memset(&cc->cbuf->cdata[cc->clen], 0,
659 (new_nr_cpages * PAGE_SIZE) -
660 (cc->clen + COMPRESS_HEADER_SIZE));
662 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
663 vm_unmap_ram(cc->rbuf, cc->cluster_size);
665 for (i = 0; i < cc->nr_cpages; i++) {
666 if (i < new_nr_cpages) {
667 new_cpages[i] = cc->cpages[i];
670 f2fs_compress_free_page(cc->cpages[i]);
671 cc->cpages[i] = NULL;
674 if (cops->destroy_compress_ctx)
675 cops->destroy_compress_ctx(cc);
677 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
678 cc->cpages = new_cpages;
679 cc->nr_cpages = new_nr_cpages;
681 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
686 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
688 vm_unmap_ram(cc->rbuf, cc->cluster_size);
690 for (i = 0; i < cc->nr_cpages; i++) {
692 f2fs_compress_free_page(cc->cpages[i]);
694 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
696 destroy_compress_ctx:
697 if (cops->destroy_compress_ctx)
698 cops->destroy_compress_ctx(cc);
700 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
705 void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity)
707 struct decompress_io_ctx *dic =
708 (struct decompress_io_ctx *)page_private(page);
709 struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
710 struct f2fs_inode_info *fi= F2FS_I(dic->inode);
711 const struct f2fs_compress_ops *cops =
712 f2fs_cops[fi->i_compress_algorithm];
716 dec_page_count(sbi, F2FS_RD_DATA);
718 if (bio->bi_status || PageError(page))
721 if (atomic_dec_return(&dic->pending_pages))
724 trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
725 dic->cluster_size, fi->i_compress_algorithm);
727 /* submit partial compressed pages */
733 dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
739 for (i = 0; i < dic->cluster_size; i++) {
740 if (dic->rpages[i]) {
741 dic->tpages[i] = dic->rpages[i];
745 dic->tpages[i] = f2fs_compress_alloc_page();
746 if (!dic->tpages[i]) {
752 if (cops->init_decompress_ctx) {
753 ret = cops->init_decompress_ctx(dic);
758 dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
761 goto destroy_decompress_ctx;
764 dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
767 goto out_vunmap_rbuf;
770 dic->clen = le32_to_cpu(dic->cbuf->clen);
771 dic->rlen = PAGE_SIZE << dic->log_cluster_size;
773 if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
775 goto out_vunmap_cbuf;
778 ret = cops->decompress_pages(dic);
781 vm_unmap_ram(dic->cbuf, dic->nr_cpages);
783 vm_unmap_ram(dic->rbuf, dic->cluster_size);
784 destroy_decompress_ctx:
785 if (cops->destroy_decompress_ctx)
786 cops->destroy_decompress_ctx(dic);
789 f2fs_decompress_end_io(dic->rpages, dic->cluster_size,
792 trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
798 static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
800 if (cc->cluster_idx == NULL_CLUSTER)
802 return cc->cluster_idx == cluster_idx(cc, index);
805 bool f2fs_cluster_is_empty(struct compress_ctx *cc)
807 return cc->nr_rpages == 0;
810 static bool f2fs_cluster_is_full(struct compress_ctx *cc)
812 return cc->cluster_size == cc->nr_rpages;
815 bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
817 if (f2fs_cluster_is_empty(cc))
819 return is_page_in_cluster(cc, index);
822 static bool __cluster_may_compress(struct compress_ctx *cc)
824 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
825 loff_t i_size = i_size_read(cc->inode);
826 unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
829 for (i = 0; i < cc->cluster_size; i++) {
830 struct page *page = cc->rpages[i];
832 f2fs_bug_on(sbi, !page);
834 if (unlikely(f2fs_cp_error(sbi)))
836 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
840 if (page->index >= nr_pages)
846 static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
848 struct dnode_of_data dn;
851 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
852 ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc),
860 if (dn.data_blkaddr == COMPRESS_ADDR) {
864 for (i = 1; i < cc->cluster_size; i++) {
867 blkaddr = data_blkaddr(dn.inode,
868 dn.node_page, dn.ofs_in_node + i);
870 if (__is_valid_data_blkaddr(blkaddr))
873 if (blkaddr != NULL_ADDR)
883 /* return # of compressed blocks in compressed cluster */
884 static int f2fs_compressed_blocks(struct compress_ctx *cc)
886 return __f2fs_cluster_blocks(cc, true);
889 /* return # of valid blocks in compressed cluster */
890 static int f2fs_cluster_blocks(struct compress_ctx *cc)
892 return __f2fs_cluster_blocks(cc, false);
895 int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
897 struct compress_ctx cc = {
899 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
900 .cluster_size = F2FS_I(inode)->i_cluster_size,
901 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
904 return f2fs_cluster_blocks(&cc);
907 static bool cluster_may_compress(struct compress_ctx *cc)
909 if (!f2fs_compressed_file(cc->inode))
911 if (f2fs_is_atomic_file(cc->inode))
913 if (f2fs_is_mmap_file(cc->inode))
915 if (!f2fs_cluster_is_full(cc))
917 if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
919 return __cluster_may_compress(cc);
922 static void set_cluster_writeback(struct compress_ctx *cc)
926 for (i = 0; i < cc->cluster_size; i++) {
928 set_page_writeback(cc->rpages[i]);
932 static void set_cluster_dirty(struct compress_ctx *cc)
936 for (i = 0; i < cc->cluster_size; i++)
938 set_page_dirty(cc->rpages[i]);
941 static int prepare_compress_overwrite(struct compress_ctx *cc,
942 struct page **pagep, pgoff_t index, void **fsdata)
944 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
945 struct address_space *mapping = cc->inode->i_mapping;
947 struct dnode_of_data dn;
948 sector_t last_block_in_bio;
949 unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
950 pgoff_t start_idx = start_idx_of_cluster(cc);
955 ret = f2fs_cluster_blocks(cc);
959 /* compressed case */
960 prealloc = (ret < cc->cluster_size);
962 ret = f2fs_init_compress_ctx(cc);
966 /* keep page reference to avoid page reclaim */
967 for (i = 0; i < cc->cluster_size; i++) {
968 page = f2fs_pagecache_get_page(mapping, start_idx + i,
975 if (PageUptodate(page))
976 f2fs_put_page(page, 1);
978 f2fs_compress_ctx_add_page(cc, page);
981 if (!f2fs_cluster_is_empty(cc)) {
982 struct bio *bio = NULL;
984 ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
985 &last_block_in_bio, false, true);
987 f2fs_destroy_compress_ctx(cc, true);
991 f2fs_submit_bio(sbi, bio, DATA);
993 ret = f2fs_init_compress_ctx(cc);
998 for (i = 0; i < cc->cluster_size; i++) {
999 f2fs_bug_on(sbi, cc->rpages[i]);
1001 page = find_lock_page(mapping, start_idx + i);
1003 /* page can be truncated */
1004 goto release_and_retry;
1007 f2fs_wait_on_page_writeback(page, DATA, true, true);
1008 f2fs_compress_ctx_add_page(cc, page);
1010 if (!PageUptodate(page)) {
1012 f2fs_put_rpages(cc);
1013 f2fs_unlock_rpages(cc, i + 1);
1014 f2fs_destroy_compress_ctx(cc, true);
1020 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
1022 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1024 for (i = cc->cluster_size - 1; i > 0; i--) {
1025 ret = f2fs_get_block(&dn, start_idx + i);
1027 i = cc->cluster_size;
1031 if (dn.data_blkaddr != NEW_ADDR)
1035 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
1039 *fsdata = cc->rpages;
1040 *pagep = cc->rpages[offset_in_cluster(cc, index)];
1041 return cc->cluster_size;
1045 f2fs_put_rpages(cc);
1046 f2fs_unlock_rpages(cc, i);
1047 f2fs_destroy_compress_ctx(cc, true);
1052 int f2fs_prepare_compress_overwrite(struct inode *inode,
1053 struct page **pagep, pgoff_t index, void **fsdata)
1055 struct compress_ctx cc = {
1057 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1058 .cluster_size = F2FS_I(inode)->i_cluster_size,
1059 .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
1064 return prepare_compress_overwrite(&cc, pagep, index, fsdata);
1067 bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
1068 pgoff_t index, unsigned copied)
1071 struct compress_ctx cc = {
1073 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1074 .cluster_size = F2FS_I(inode)->i_cluster_size,
1077 bool first_index = (index == cc.rpages[0]->index);
1080 set_cluster_dirty(&cc);
1082 f2fs_put_rpages_wbc(&cc, NULL, false, 1);
1083 f2fs_destroy_compress_ctx(&cc, false);
1088 int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
1090 void *fsdata = NULL;
1092 int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
1093 pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
1097 err = f2fs_is_compressed_cluster(inode, start_idx);
1101 /* truncate normal cluster */
1103 return f2fs_do_truncate_blocks(inode, from, lock);
1105 /* truncate compressed cluster */
1106 err = f2fs_prepare_compress_overwrite(inode, &pagep,
1107 start_idx, &fsdata);
1109 /* should not be a normal cluster */
1110 f2fs_bug_on(F2FS_I_SB(inode), err == 0);
1116 struct page **rpages = fsdata;
1117 int cluster_size = F2FS_I(inode)->i_cluster_size;
1120 for (i = cluster_size - 1; i >= 0; i--) {
1121 loff_t start = rpages[i]->index << PAGE_SHIFT;
1123 if (from <= start) {
1124 zero_user_segment(rpages[i], 0, PAGE_SIZE);
1126 zero_user_segment(rpages[i], from - start,
1132 f2fs_compress_write_end(inode, fsdata, start_idx, true);
1137 static int f2fs_write_compressed_pages(struct compress_ctx *cc,
1139 struct writeback_control *wbc,
1140 enum iostat_type io_type)
1142 struct inode *inode = cc->inode;
1143 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1144 struct f2fs_inode_info *fi = F2FS_I(inode);
1145 struct f2fs_io_info fio = {
1147 .ino = cc->inode->i_ino,
1150 .op_flags = wbc_to_write_flags(wbc),
1151 .old_blkaddr = NEW_ADDR,
1153 .encrypted_page = NULL,
1154 .compressed_page = NULL,
1158 .encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode),
1160 struct dnode_of_data dn;
1161 struct node_info ni;
1162 struct compress_io_ctx *cic;
1163 pgoff_t start_idx = start_idx_of_cluster(cc);
1164 unsigned int last_index = cc->cluster_size - 1;
1168 if (IS_NOQUOTA(inode)) {
1170 * We need to wait for node_write to avoid block allocation during
1171 * checkpoint. This can only happen to quota writes which can cause
1172 * the below discard race condition.
1174 down_read(&sbi->node_write);
1175 } else if (!f2fs_trylock_op(sbi)) {
1179 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1181 err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
1185 for (i = 0; i < cc->cluster_size; i++) {
1186 if (data_blkaddr(dn.inode, dn.node_page,
1187 dn.ofs_in_node + i) == NULL_ADDR)
1191 psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
1193 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
1197 fio.version = ni.version;
1199 cic = kmem_cache_zalloc(cic_entry_slab, GFP_NOFS);
1203 cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1205 atomic_set(&cic->pending_pages, cc->nr_cpages);
1206 cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1210 cic->nr_rpages = cc->cluster_size;
1212 for (i = 0; i < cc->nr_cpages; i++) {
1213 f2fs_set_compressed_page(cc->cpages[i], inode,
1214 cc->rpages[i + 1]->index, cic);
1215 fio.compressed_page = cc->cpages[i];
1217 fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1218 dn.ofs_in_node + i + 1);
1220 /* wait for GCed page writeback via META_MAPPING */
1221 f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1223 if (fio.encrypted) {
1224 fio.page = cc->rpages[i + 1];
1225 err = f2fs_encrypt_one_page(&fio);
1227 goto out_destroy_crypt;
1228 cc->cpages[i] = fio.encrypted_page;
1232 set_cluster_writeback(cc);
1234 for (i = 0; i < cc->cluster_size; i++)
1235 cic->rpages[i] = cc->rpages[i];
1237 for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
1240 blkaddr = f2fs_data_blkaddr(&dn);
1241 fio.page = cc->rpages[i];
1242 fio.old_blkaddr = blkaddr;
1244 /* cluster header */
1246 if (blkaddr == COMPRESS_ADDR)
1248 if (__is_valid_data_blkaddr(blkaddr))
1249 f2fs_invalidate_blocks(sbi, blkaddr);
1250 f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
1251 goto unlock_continue;
1254 if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
1257 if (i > cc->nr_cpages) {
1258 if (__is_valid_data_blkaddr(blkaddr)) {
1259 f2fs_invalidate_blocks(sbi, blkaddr);
1260 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
1262 goto unlock_continue;
1265 f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
1268 fio.encrypted_page = cc->cpages[i - 1];
1270 fio.compressed_page = cc->cpages[i - 1];
1272 cc->cpages[i - 1] = NULL;
1273 f2fs_outplace_write_data(&dn, &fio);
1276 inode_dec_dirty_pages(cc->inode);
1277 unlock_page(fio.page);
1280 if (fio.compr_blocks)
1281 f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
1282 f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
1284 set_inode_flag(cc->inode, FI_APPEND_WRITE);
1285 if (cc->cluster_idx == 0)
1286 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1288 f2fs_put_dnode(&dn);
1289 if (IS_NOQUOTA(inode))
1290 up_read(&sbi->node_write);
1292 f2fs_unlock_op(sbi);
1294 spin_lock(&fi->i_size_lock);
1295 if (fi->last_disk_size < psize)
1296 fi->last_disk_size = psize;
1297 spin_unlock(&fi->i_size_lock);
1299 f2fs_put_rpages(cc);
1300 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1302 f2fs_destroy_compress_ctx(cc, false);
1306 page_array_free(cc->inode, cic->rpages, cc->cluster_size);
1308 for (--i; i >= 0; i--)
1309 fscrypt_finalize_bounce_page(&cc->cpages[i]);
1311 kmem_cache_free(cic_entry_slab, cic);
1313 f2fs_put_dnode(&dn);
1315 if (IS_NOQUOTA(inode))
1316 up_read(&sbi->node_write);
1318 f2fs_unlock_op(sbi);
1320 for (i = 0; i < cc->nr_cpages; i++) {
1323 f2fs_compress_free_page(cc->cpages[i]);
1324 cc->cpages[i] = NULL;
1326 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1331 void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
1333 struct f2fs_sb_info *sbi = bio->bi_private;
1334 struct compress_io_ctx *cic =
1335 (struct compress_io_ctx *)page_private(page);
1338 if (unlikely(bio->bi_status))
1339 mapping_set_error(cic->inode->i_mapping, -EIO);
1341 f2fs_compress_free_page(page);
1343 dec_page_count(sbi, F2FS_WB_DATA);
1345 if (atomic_dec_return(&cic->pending_pages))
1348 for (i = 0; i < cic->nr_rpages; i++) {
1349 WARN_ON(!cic->rpages[i]);
1350 clear_cold_data(cic->rpages[i]);
1351 end_page_writeback(cic->rpages[i]);
1354 page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
1355 kmem_cache_free(cic_entry_slab, cic);
1358 static int f2fs_write_raw_pages(struct compress_ctx *cc,
1360 struct writeback_control *wbc,
1361 enum iostat_type io_type)
1363 struct address_space *mapping = cc->inode->i_mapping;
1364 int _submitted, compr_blocks, ret, i;
1366 compr_blocks = f2fs_compressed_blocks(cc);
1368 for (i = 0; i < cc->cluster_size; i++) {
1372 redirty_page_for_writepage(wbc, cc->rpages[i]);
1373 unlock_page(cc->rpages[i]);
1376 if (compr_blocks < 0)
1377 return compr_blocks;
1379 for (i = 0; i < cc->cluster_size; i++) {
1383 lock_page(cc->rpages[i]);
1385 if (cc->rpages[i]->mapping != mapping) {
1387 unlock_page(cc->rpages[i]);
1391 if (!PageDirty(cc->rpages[i]))
1392 goto continue_unlock;
1394 if (PageWriteback(cc->rpages[i])) {
1395 if (wbc->sync_mode == WB_SYNC_NONE)
1396 goto continue_unlock;
1397 f2fs_wait_on_page_writeback(cc->rpages[i], DATA, true, true);
1400 if (!clear_page_dirty_for_io(cc->rpages[i]))
1401 goto continue_unlock;
1403 ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
1404 NULL, NULL, wbc, io_type,
1405 compr_blocks, false);
1407 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1408 unlock_page(cc->rpages[i]);
1410 } else if (ret == -EAGAIN) {
1412 * for quota file, just redirty left pages to
1413 * avoid deadlock caused by cluster update race
1414 * from foreground operation.
1416 if (IS_NOQUOTA(cc->inode))
1420 congestion_wait(BLK_RW_ASYNC,
1421 DEFAULT_IO_TIMEOUT);
1427 *submitted += _submitted;
1430 f2fs_balance_fs(F2FS_M_SB(mapping), true);
1435 int f2fs_write_multi_pages(struct compress_ctx *cc,
1437 struct writeback_control *wbc,
1438 enum iostat_type io_type)
1443 if (cluster_may_compress(cc)) {
1444 err = f2fs_compress_pages(cc);
1445 if (err == -EAGAIN) {
1448 f2fs_put_rpages_wbc(cc, wbc, true, 1);
1452 err = f2fs_write_compressed_pages(cc, submitted,
1456 f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1459 f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
1461 err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
1462 f2fs_put_rpages_wbc(cc, wbc, false, 0);
1464 f2fs_destroy_compress_ctx(cc, false);
1468 struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1470 struct decompress_io_ctx *dic;
1471 pgoff_t start_idx = start_idx_of_cluster(cc);
1474 dic = kmem_cache_zalloc(dic_entry_slab, GFP_NOFS);
1476 return ERR_PTR(-ENOMEM);
1478 dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1480 kmem_cache_free(dic_entry_slab, dic);
1481 return ERR_PTR(-ENOMEM);
1484 dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1485 dic->inode = cc->inode;
1486 atomic_set(&dic->pending_pages, cc->nr_cpages);
1487 dic->cluster_idx = cc->cluster_idx;
1488 dic->cluster_size = cc->cluster_size;
1489 dic->log_cluster_size = cc->log_cluster_size;
1490 dic->nr_cpages = cc->nr_cpages;
1491 dic->failed = false;
1493 for (i = 0; i < dic->cluster_size; i++)
1494 dic->rpages[i] = cc->rpages[i];
1495 dic->nr_rpages = cc->cluster_size;
1497 dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
1501 for (i = 0; i < dic->nr_cpages; i++) {
1504 page = f2fs_compress_alloc_page();
1508 f2fs_set_compressed_page(page, cc->inode,
1509 start_idx + i + 1, dic);
1510 dic->cpages[i] = page;
1517 return ERR_PTR(-ENOMEM);
1520 void f2fs_free_dic(struct decompress_io_ctx *dic)
1525 for (i = 0; i < dic->cluster_size; i++) {
1528 if (!dic->tpages[i])
1530 f2fs_compress_free_page(dic->tpages[i]);
1532 page_array_free(dic->inode, dic->tpages, dic->cluster_size);
1536 for (i = 0; i < dic->nr_cpages; i++) {
1537 if (!dic->cpages[i])
1539 f2fs_compress_free_page(dic->cpages[i]);
1541 page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
1544 page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
1545 kmem_cache_free(dic_entry_slab, dic);
1548 void f2fs_decompress_end_io(struct page **rpages,
1549 unsigned int cluster_size, bool err, bool verity)
1553 for (i = 0; i < cluster_size; i++) {
1554 struct page *rpage = rpages[i];
1559 if (err || PageError(rpage))
1560 goto clear_uptodate;
1562 if (!verity || fsverity_verify_page(rpage)) {
1563 SetPageUptodate(rpage);
1567 ClearPageUptodate(rpage);
1568 ClearPageError(rpage);
1574 int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
1576 dev_t dev = sbi->sb->s_bdev->bd_dev;
1579 sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
1581 sbi->page_array_slab_size = sizeof(struct page *) <<
1582 F2FS_OPTION(sbi).compress_log_size;
1584 sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
1585 sbi->page_array_slab_size);
1586 if (!sbi->page_array_slab)
1591 void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
1593 kmem_cache_destroy(sbi->page_array_slab);
1596 static int __init f2fs_init_cic_cache(void)
1598 cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
1599 sizeof(struct compress_io_ctx));
1600 if (!cic_entry_slab)
1605 static void f2fs_destroy_cic_cache(void)
1607 kmem_cache_destroy(cic_entry_slab);
1610 static int __init f2fs_init_dic_cache(void)
1612 dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
1613 sizeof(struct decompress_io_ctx));
1614 if (!dic_entry_slab)
1619 static void f2fs_destroy_dic_cache(void)
1621 kmem_cache_destroy(dic_entry_slab);
1624 int __init f2fs_init_compress_cache(void)
1628 err = f2fs_init_cic_cache();
1631 err = f2fs_init_dic_cache();
1636 f2fs_destroy_cic_cache();
1641 void f2fs_destroy_compress_cache(void)
1643 f2fs_destroy_dic_cache();
1644 f2fs_destroy_cic_cache();