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>
17 #include <trace/events/f2fs.h>
19 struct f2fs_compress_ops
{
20 int (*init_compress_ctx
)(struct compress_ctx
*cc
);
21 void (*destroy_compress_ctx
)(struct compress_ctx
*cc
);
22 int (*compress_pages
)(struct compress_ctx
*cc
);
23 int (*decompress_pages
)(struct decompress_io_ctx
*dic
);
26 static unsigned int offset_in_cluster(struct compress_ctx
*cc
, pgoff_t index
)
28 return index
& (cc
->cluster_size
- 1);
31 static pgoff_t
cluster_idx(struct compress_ctx
*cc
, pgoff_t index
)
33 return index
>> cc
->log_cluster_size
;
36 static pgoff_t
start_idx_of_cluster(struct compress_ctx
*cc
)
38 return cc
->cluster_idx
<< cc
->log_cluster_size
;
41 bool f2fs_is_compressed_page(struct page
*page
)
43 if (!PagePrivate(page
))
45 if (!page_private(page
))
47 if (IS_ATOMIC_WRITTEN_PAGE(page
) || IS_DUMMY_WRITTEN_PAGE(page
))
49 f2fs_bug_on(F2FS_M_SB(page
->mapping
),
50 *((u32
*)page_private(page
)) != F2FS_COMPRESSED_PAGE_MAGIC
);
54 static void f2fs_set_compressed_page(struct page
*page
,
55 struct inode
*inode
, pgoff_t index
, void *data
, refcount_t
*r
)
58 set_page_private(page
, (unsigned long)data
);
60 /* i_crypto_info and iv index */
62 page
->mapping
= inode
->i_mapping
;
67 static void f2fs_put_compressed_page(struct page
*page
)
69 set_page_private(page
, (unsigned long)NULL
);
70 ClearPagePrivate(page
);
76 static void f2fs_drop_rpages(struct compress_ctx
*cc
, int len
, bool unlock
)
80 for (i
= 0; i
< len
; i
++) {
84 unlock_page(cc
->rpages
[i
]);
86 put_page(cc
->rpages
[i
]);
90 static void f2fs_put_rpages(struct compress_ctx
*cc
)
92 f2fs_drop_rpages(cc
, cc
->cluster_size
, false);
95 static void f2fs_unlock_rpages(struct compress_ctx
*cc
, int len
)
97 f2fs_drop_rpages(cc
, len
, true);
100 static void f2fs_put_rpages_mapping(struct compress_ctx
*cc
,
101 struct address_space
*mapping
,
102 pgoff_t start
, int len
)
106 for (i
= 0; i
< len
; i
++) {
107 struct page
*page
= find_get_page(mapping
, start
+ i
);
114 static void f2fs_put_rpages_wbc(struct compress_ctx
*cc
,
115 struct writeback_control
*wbc
, bool redirty
, int unlock
)
119 for (i
= 0; i
< cc
->cluster_size
; i
++) {
123 redirty_page_for_writepage(wbc
, cc
->rpages
[i
]);
124 f2fs_put_page(cc
->rpages
[i
], unlock
);
128 struct page
*f2fs_compress_control_page(struct page
*page
)
130 return ((struct compress_io_ctx
*)page_private(page
))->rpages
[0];
133 int f2fs_init_compress_ctx(struct compress_ctx
*cc
)
135 struct f2fs_sb_info
*sbi
= F2FS_I_SB(cc
->inode
);
140 cc
->rpages
= f2fs_kzalloc(sbi
, sizeof(struct page
*) <<
141 cc
->log_cluster_size
, GFP_NOFS
);
142 return cc
->rpages
? 0 : -ENOMEM
;
145 void f2fs_destroy_compress_ctx(struct compress_ctx
*cc
)
151 cc
->cluster_idx
= NULL_CLUSTER
;
154 void f2fs_compress_ctx_add_page(struct compress_ctx
*cc
, struct page
*page
)
156 unsigned int cluster_ofs
;
158 if (!f2fs_cluster_can_merge_page(cc
, page
->index
))
159 f2fs_bug_on(F2FS_I_SB(cc
->inode
), 1);
161 cluster_ofs
= offset_in_cluster(cc
, page
->index
);
162 cc
->rpages
[cluster_ofs
] = page
;
164 cc
->cluster_idx
= cluster_idx(cc
, page
->index
);
167 #ifdef CONFIG_F2FS_FS_LZO
168 static int lzo_init_compress_ctx(struct compress_ctx
*cc
)
170 cc
->private = f2fs_kvmalloc(F2FS_I_SB(cc
->inode
),
171 LZO1X_MEM_COMPRESS
, GFP_NOFS
);
175 cc
->clen
= lzo1x_worst_compress(PAGE_SIZE
<< cc
->log_cluster_size
);
179 static void lzo_destroy_compress_ctx(struct compress_ctx
*cc
)
185 static int lzo_compress_pages(struct compress_ctx
*cc
)
189 ret
= lzo1x_1_compress(cc
->rbuf
, cc
->rlen
, cc
->cbuf
->cdata
,
190 &cc
->clen
, cc
->private);
191 if (ret
!= LZO_E_OK
) {
192 printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
193 KERN_ERR
, F2FS_I_SB(cc
->inode
)->sb
->s_id
, ret
);
199 static int lzo_decompress_pages(struct decompress_io_ctx
*dic
)
203 ret
= lzo1x_decompress_safe(dic
->cbuf
->cdata
, dic
->clen
,
204 dic
->rbuf
, &dic
->rlen
);
205 if (ret
!= LZO_E_OK
) {
206 printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
207 KERN_ERR
, F2FS_I_SB(dic
->inode
)->sb
->s_id
, ret
);
211 if (dic
->rlen
!= PAGE_SIZE
<< dic
->log_cluster_size
) {
212 printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
213 "expected:%lu\n", KERN_ERR
,
214 F2FS_I_SB(dic
->inode
)->sb
->s_id
,
216 PAGE_SIZE
<< dic
->log_cluster_size
);
222 static const struct f2fs_compress_ops f2fs_lzo_ops
= {
223 .init_compress_ctx
= lzo_init_compress_ctx
,
224 .destroy_compress_ctx
= lzo_destroy_compress_ctx
,
225 .compress_pages
= lzo_compress_pages
,
226 .decompress_pages
= lzo_decompress_pages
,
230 #ifdef CONFIG_F2FS_FS_LZ4
231 static int lz4_init_compress_ctx(struct compress_ctx
*cc
)
233 cc
->private = f2fs_kvmalloc(F2FS_I_SB(cc
->inode
),
234 LZ4_MEM_COMPRESS
, GFP_NOFS
);
238 cc
->clen
= LZ4_compressBound(PAGE_SIZE
<< cc
->log_cluster_size
);
242 static void lz4_destroy_compress_ctx(struct compress_ctx
*cc
)
248 static int lz4_compress_pages(struct compress_ctx
*cc
)
252 len
= LZ4_compress_default(cc
->rbuf
, cc
->cbuf
->cdata
, cc
->rlen
,
253 cc
->clen
, cc
->private);
255 printk_ratelimited("%sF2FS-fs (%s): lz4 compress failed\n",
256 KERN_ERR
, F2FS_I_SB(cc
->inode
)->sb
->s_id
);
263 static int lz4_decompress_pages(struct decompress_io_ctx
*dic
)
267 ret
= LZ4_decompress_safe(dic
->cbuf
->cdata
, dic
->rbuf
,
268 dic
->clen
, dic
->rlen
);
270 printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
271 KERN_ERR
, F2FS_I_SB(dic
->inode
)->sb
->s_id
, ret
);
275 if (ret
!= PAGE_SIZE
<< dic
->log_cluster_size
) {
276 printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, "
277 "expected:%lu\n", KERN_ERR
,
278 F2FS_I_SB(dic
->inode
)->sb
->s_id
,
280 PAGE_SIZE
<< dic
->log_cluster_size
);
286 static const struct f2fs_compress_ops f2fs_lz4_ops
= {
287 .init_compress_ctx
= lz4_init_compress_ctx
,
288 .destroy_compress_ctx
= lz4_destroy_compress_ctx
,
289 .compress_pages
= lz4_compress_pages
,
290 .decompress_pages
= lz4_decompress_pages
,
294 static const struct f2fs_compress_ops
*f2fs_cops
[COMPRESS_MAX
] = {
295 #ifdef CONFIG_F2FS_FS_LZO
300 #ifdef CONFIG_F2FS_FS_LZ4
307 bool f2fs_is_compress_backend_ready(struct inode
*inode
)
309 if (!f2fs_compressed_file(inode
))
311 return f2fs_cops
[F2FS_I(inode
)->i_compress_algorithm
];
314 static struct page
*f2fs_grab_page(void)
318 page
= alloc_page(GFP_NOFS
);
325 static int f2fs_compress_pages(struct compress_ctx
*cc
)
327 struct f2fs_sb_info
*sbi
= F2FS_I_SB(cc
->inode
);
328 struct f2fs_inode_info
*fi
= F2FS_I(cc
->inode
);
329 const struct f2fs_compress_ops
*cops
=
330 f2fs_cops
[fi
->i_compress_algorithm
];
331 unsigned int max_len
, nr_cpages
;
334 trace_f2fs_compress_pages_start(cc
->inode
, cc
->cluster_idx
,
335 cc
->cluster_size
, fi
->i_compress_algorithm
);
337 ret
= cops
->init_compress_ctx(cc
);
341 max_len
= COMPRESS_HEADER_SIZE
+ cc
->clen
;
342 cc
->nr_cpages
= DIV_ROUND_UP(max_len
, PAGE_SIZE
);
344 cc
->cpages
= f2fs_kzalloc(sbi
, sizeof(struct page
*) *
345 cc
->nr_cpages
, GFP_NOFS
);
348 goto destroy_compress_ctx
;
351 for (i
= 0; i
< cc
->nr_cpages
; i
++) {
352 cc
->cpages
[i
] = f2fs_grab_page();
353 if (!cc
->cpages
[i
]) {
355 goto out_free_cpages
;
359 cc
->rbuf
= vmap(cc
->rpages
, cc
->cluster_size
, VM_MAP
, PAGE_KERNEL_RO
);
362 goto out_free_cpages
;
365 cc
->cbuf
= vmap(cc
->cpages
, cc
->nr_cpages
, VM_MAP
, PAGE_KERNEL
);
368 goto out_vunmap_rbuf
;
371 ret
= cops
->compress_pages(cc
);
373 goto out_vunmap_cbuf
;
375 max_len
= PAGE_SIZE
* (cc
->cluster_size
- 1) - COMPRESS_HEADER_SIZE
;
377 if (cc
->clen
> max_len
) {
379 goto out_vunmap_cbuf
;
382 cc
->cbuf
->clen
= cpu_to_le32(cc
->clen
);
383 cc
->cbuf
->chksum
= cpu_to_le32(0);
385 for (i
= 0; i
< COMPRESS_DATA_RESERVED_SIZE
; i
++)
386 cc
->cbuf
->reserved
[i
] = cpu_to_le32(0);
391 nr_cpages
= DIV_ROUND_UP(cc
->clen
+ COMPRESS_HEADER_SIZE
, PAGE_SIZE
);
393 for (i
= nr_cpages
; i
< cc
->nr_cpages
; i
++) {
394 f2fs_put_compressed_page(cc
->cpages
[i
]);
395 cc
->cpages
[i
] = NULL
;
398 cc
->nr_cpages
= nr_cpages
;
400 trace_f2fs_compress_pages_end(cc
->inode
, cc
->cluster_idx
,
409 for (i
= 0; i
< cc
->nr_cpages
; i
++) {
411 f2fs_put_compressed_page(cc
->cpages
[i
]);
415 destroy_compress_ctx
:
416 cops
->destroy_compress_ctx(cc
);
418 trace_f2fs_compress_pages_end(cc
->inode
, cc
->cluster_idx
,
423 void f2fs_decompress_pages(struct bio
*bio
, struct page
*page
, bool verity
)
425 struct decompress_io_ctx
*dic
=
426 (struct decompress_io_ctx
*)page_private(page
);
427 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dic
->inode
);
428 struct f2fs_inode_info
*fi
= F2FS_I(dic
->inode
);
429 const struct f2fs_compress_ops
*cops
=
430 f2fs_cops
[fi
->i_compress_algorithm
];
433 dec_page_count(sbi
, F2FS_RD_DATA
);
435 if (bio
->bi_status
|| PageError(page
))
438 if (refcount_dec_not_one(&dic
->ref
))
441 trace_f2fs_decompress_pages_start(dic
->inode
, dic
->cluster_idx
,
442 dic
->cluster_size
, fi
->i_compress_algorithm
);
444 /* submit partial compressed pages */
450 dic
->rbuf
= vmap(dic
->tpages
, dic
->cluster_size
, VM_MAP
, PAGE_KERNEL
);
456 dic
->cbuf
= vmap(dic
->cpages
, dic
->nr_cpages
, VM_MAP
, PAGE_KERNEL_RO
);
459 goto out_vunmap_rbuf
;
462 dic
->clen
= le32_to_cpu(dic
->cbuf
->clen
);
463 dic
->rlen
= PAGE_SIZE
<< dic
->log_cluster_size
;
465 if (dic
->clen
> PAGE_SIZE
* dic
->nr_cpages
- COMPRESS_HEADER_SIZE
) {
467 goto out_vunmap_cbuf
;
470 ret
= cops
->decompress_pages(dic
);
478 f2fs_decompress_end_io(dic
->rpages
, dic
->cluster_size
,
481 trace_f2fs_decompress_pages_end(dic
->inode
, dic
->cluster_idx
,
487 static bool is_page_in_cluster(struct compress_ctx
*cc
, pgoff_t index
)
489 if (cc
->cluster_idx
== NULL_CLUSTER
)
491 return cc
->cluster_idx
== cluster_idx(cc
, index
);
494 bool f2fs_cluster_is_empty(struct compress_ctx
*cc
)
496 return cc
->nr_rpages
== 0;
499 static bool f2fs_cluster_is_full(struct compress_ctx
*cc
)
501 return cc
->cluster_size
== cc
->nr_rpages
;
504 bool f2fs_cluster_can_merge_page(struct compress_ctx
*cc
, pgoff_t index
)
506 if (f2fs_cluster_is_empty(cc
))
508 return is_page_in_cluster(cc
, index
);
511 static bool __cluster_may_compress(struct compress_ctx
*cc
)
513 struct f2fs_sb_info
*sbi
= F2FS_I_SB(cc
->inode
);
514 loff_t i_size
= i_size_read(cc
->inode
);
515 unsigned nr_pages
= DIV_ROUND_UP(i_size
, PAGE_SIZE
);
518 for (i
= 0; i
< cc
->cluster_size
; i
++) {
519 struct page
*page
= cc
->rpages
[i
];
521 f2fs_bug_on(sbi
, !page
);
523 if (unlikely(f2fs_cp_error(sbi
)))
525 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
529 if (page
->index
>= nr_pages
)
535 /* return # of compressed block addresses */
536 static int f2fs_compressed_blocks(struct compress_ctx
*cc
)
538 struct dnode_of_data dn
;
541 set_new_dnode(&dn
, cc
->inode
, NULL
, NULL
, 0);
542 ret
= f2fs_get_dnode_of_data(&dn
, start_idx_of_cluster(cc
),
550 if (dn
.data_blkaddr
== COMPRESS_ADDR
) {
554 for (i
= 1; i
< cc
->cluster_size
; i
++) {
557 blkaddr
= datablock_addr(dn
.inode
,
558 dn
.node_page
, dn
.ofs_in_node
+ i
);
559 if (blkaddr
!= NULL_ADDR
)
568 int f2fs_is_compressed_cluster(struct inode
*inode
, pgoff_t index
)
570 struct compress_ctx cc
= {
572 .log_cluster_size
= F2FS_I(inode
)->i_log_cluster_size
,
573 .cluster_size
= F2FS_I(inode
)->i_cluster_size
,
574 .cluster_idx
= index
>> F2FS_I(inode
)->i_log_cluster_size
,
577 return f2fs_compressed_blocks(&cc
);
580 static bool cluster_may_compress(struct compress_ctx
*cc
)
582 if (!f2fs_compressed_file(cc
->inode
))
584 if (f2fs_is_atomic_file(cc
->inode
))
586 if (f2fs_is_mmap_file(cc
->inode
))
588 if (!f2fs_cluster_is_full(cc
))
590 return __cluster_may_compress(cc
);
593 static void set_cluster_writeback(struct compress_ctx
*cc
)
597 for (i
= 0; i
< cc
->cluster_size
; i
++) {
599 set_page_writeback(cc
->rpages
[i
]);
603 static void set_cluster_dirty(struct compress_ctx
*cc
)
607 for (i
= 0; i
< cc
->cluster_size
; i
++)
609 set_page_dirty(cc
->rpages
[i
]);
612 static int prepare_compress_overwrite(struct compress_ctx
*cc
,
613 struct page
**pagep
, pgoff_t index
, void **fsdata
)
615 struct f2fs_sb_info
*sbi
= F2FS_I_SB(cc
->inode
);
616 struct address_space
*mapping
= cc
->inode
->i_mapping
;
618 struct dnode_of_data dn
;
619 sector_t last_block_in_bio
;
620 unsigned fgp_flag
= FGP_LOCK
| FGP_WRITE
| FGP_CREAT
;
621 pgoff_t start_idx
= start_idx_of_cluster(cc
);
626 ret
= f2fs_compressed_blocks(cc
);
630 /* compressed case */
631 prealloc
= (ret
< cc
->cluster_size
);
633 ret
= f2fs_init_compress_ctx(cc
);
637 /* keep page reference to avoid page reclaim */
638 for (i
= 0; i
< cc
->cluster_size
; i
++) {
639 page
= f2fs_pagecache_get_page(mapping
, start_idx
+ i
,
646 if (PageUptodate(page
))
649 f2fs_compress_ctx_add_page(cc
, page
);
652 if (!f2fs_cluster_is_empty(cc
)) {
653 struct bio
*bio
= NULL
;
655 ret
= f2fs_read_multi_pages(cc
, &bio
, cc
->cluster_size
,
656 &last_block_in_bio
, false);
657 f2fs_destroy_compress_ctx(cc
);
661 f2fs_submit_bio(sbi
, bio
, DATA
);
663 ret
= f2fs_init_compress_ctx(cc
);
668 for (i
= 0; i
< cc
->cluster_size
; i
++) {
669 f2fs_bug_on(sbi
, cc
->rpages
[i
]);
671 page
= find_lock_page(mapping
, start_idx
+ i
);
672 f2fs_bug_on(sbi
, !page
);
674 f2fs_wait_on_page_writeback(page
, DATA
, true, true);
676 f2fs_compress_ctx_add_page(cc
, page
);
677 f2fs_put_page(page
, 0);
679 if (!PageUptodate(page
)) {
680 f2fs_unlock_rpages(cc
, i
+ 1);
681 f2fs_put_rpages_mapping(cc
, mapping
, start_idx
,
683 f2fs_destroy_compress_ctx(cc
);
689 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
, true);
691 set_new_dnode(&dn
, cc
->inode
, NULL
, NULL
, 0);
693 for (i
= cc
->cluster_size
- 1; i
> 0; i
--) {
694 ret
= f2fs_get_block(&dn
, start_idx
+ i
);
696 i
= cc
->cluster_size
;
700 if (dn
.data_blkaddr
!= NEW_ADDR
)
704 __do_map_lock(sbi
, F2FS_GET_BLOCK_PRE_AIO
, false);
708 *fsdata
= cc
->rpages
;
709 *pagep
= cc
->rpages
[offset_in_cluster(cc
, index
)];
710 return cc
->cluster_size
;
714 f2fs_unlock_rpages(cc
, i
);
716 f2fs_put_rpages_mapping(cc
, mapping
, start_idx
, i
);
717 f2fs_destroy_compress_ctx(cc
);
721 int f2fs_prepare_compress_overwrite(struct inode
*inode
,
722 struct page
**pagep
, pgoff_t index
, void **fsdata
)
724 struct compress_ctx cc
= {
726 .log_cluster_size
= F2FS_I(inode
)->i_log_cluster_size
,
727 .cluster_size
= F2FS_I(inode
)->i_cluster_size
,
728 .cluster_idx
= index
>> F2FS_I(inode
)->i_log_cluster_size
,
733 return prepare_compress_overwrite(&cc
, pagep
, index
, fsdata
);
736 bool f2fs_compress_write_end(struct inode
*inode
, void *fsdata
,
737 pgoff_t index
, unsigned copied
)
740 struct compress_ctx cc
= {
741 .log_cluster_size
= F2FS_I(inode
)->i_log_cluster_size
,
742 .cluster_size
= F2FS_I(inode
)->i_cluster_size
,
745 bool first_index
= (index
== cc
.rpages
[0]->index
);
748 set_cluster_dirty(&cc
);
750 f2fs_put_rpages_wbc(&cc
, NULL
, false, 1);
751 f2fs_destroy_compress_ctx(&cc
);
756 static int f2fs_write_compressed_pages(struct compress_ctx
*cc
,
758 struct writeback_control
*wbc
,
759 enum iostat_type io_type
)
761 struct inode
*inode
= cc
->inode
;
762 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
763 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
764 struct f2fs_io_info fio
= {
766 .ino
= cc
->inode
->i_ino
,
769 .op_flags
= wbc_to_write_flags(wbc
),
770 .old_blkaddr
= NEW_ADDR
,
772 .encrypted_page
= NULL
,
773 .compressed_page
= NULL
,
775 .need_lock
= LOCK_RETRY
,
778 .encrypted
= f2fs_encrypted_file(cc
->inode
),
780 struct dnode_of_data dn
;
782 struct compress_io_ctx
*cic
;
783 pgoff_t start_idx
= start_idx_of_cluster(cc
);
784 unsigned int last_index
= cc
->cluster_size
- 1;
788 set_new_dnode(&dn
, cc
->inode
, NULL
, NULL
, 0);
792 err
= f2fs_get_dnode_of_data(&dn
, start_idx
, LOOKUP_NODE
);
796 for (i
= 0; i
< cc
->cluster_size
; i
++) {
797 if (datablock_addr(dn
.inode
, dn
.node_page
,
798 dn
.ofs_in_node
+ i
) == NULL_ADDR
)
802 psize
= (loff_t
)(cc
->rpages
[last_index
]->index
+ 1) << PAGE_SHIFT
;
804 err
= f2fs_get_node_info(fio
.sbi
, dn
.nid
, &ni
);
808 fio
.version
= ni
.version
;
810 cic
= f2fs_kzalloc(sbi
, sizeof(struct compress_io_ctx
), GFP_NOFS
);
814 cic
->magic
= F2FS_COMPRESSED_PAGE_MAGIC
;
816 refcount_set(&cic
->ref
, 1);
817 cic
->rpages
= f2fs_kzalloc(sbi
, sizeof(struct page
*) <<
818 cc
->log_cluster_size
, GFP_NOFS
);
822 cic
->nr_rpages
= cc
->cluster_size
;
824 for (i
= 0; i
< cc
->nr_cpages
; i
++) {
825 f2fs_set_compressed_page(cc
->cpages
[i
], inode
,
826 cc
->rpages
[i
+ 1]->index
,
827 cic
, i
? &cic
->ref
: NULL
);
828 fio
.compressed_page
= cc
->cpages
[i
];
830 fio
.page
= cc
->rpages
[i
+ 1];
831 err
= f2fs_encrypt_one_page(&fio
);
833 goto out_destroy_crypt
;
834 cc
->cpages
[i
] = fio
.encrypted_page
;
838 set_cluster_writeback(cc
);
840 for (i
= 0; i
< cc
->cluster_size
; i
++)
841 cic
->rpages
[i
] = cc
->rpages
[i
];
843 for (i
= 0; i
< cc
->cluster_size
; i
++, dn
.ofs_in_node
++) {
846 blkaddr
= datablock_addr(dn
.inode
, dn
.node_page
,
848 fio
.page
= cic
->rpages
[i
];
849 fio
.old_blkaddr
= blkaddr
;
853 if (blkaddr
== COMPRESS_ADDR
)
855 if (__is_valid_data_blkaddr(blkaddr
))
856 f2fs_invalidate_blocks(sbi
, blkaddr
);
857 f2fs_update_data_blkaddr(&dn
, COMPRESS_ADDR
);
858 goto unlock_continue
;
861 if (fio
.compr_blocks
&& __is_valid_data_blkaddr(blkaddr
))
864 if (i
> cc
->nr_cpages
) {
865 if (__is_valid_data_blkaddr(blkaddr
)) {
866 f2fs_invalidate_blocks(sbi
, blkaddr
);
867 f2fs_update_data_blkaddr(&dn
, NEW_ADDR
);
869 goto unlock_continue
;
872 f2fs_bug_on(fio
.sbi
, blkaddr
== NULL_ADDR
);
875 fio
.encrypted_page
= cc
->cpages
[i
- 1];
877 fio
.compressed_page
= cc
->cpages
[i
- 1];
879 cc
->cpages
[i
- 1] = NULL
;
880 f2fs_outplace_write_data(&dn
, &fio
);
883 inode_dec_dirty_pages(cc
->inode
);
884 unlock_page(fio
.page
);
887 if (fio
.compr_blocks
)
888 f2fs_i_compr_blocks_update(inode
, fio
.compr_blocks
- 1, false);
889 f2fs_i_compr_blocks_update(inode
, cc
->nr_cpages
, true);
891 set_inode_flag(cc
->inode
, FI_APPEND_WRITE
);
892 if (cc
->cluster_idx
== 0)
893 set_inode_flag(inode
, FI_FIRST_BLOCK_WRITTEN
);
898 down_write(&fi
->i_sem
);
899 if (fi
->last_disk_size
< psize
)
900 fi
->last_disk_size
= psize
;
901 up_write(&fi
->i_sem
);
904 f2fs_destroy_compress_ctx(cc
);
910 for (--i
; i
>= 0; i
--)
911 fscrypt_finalize_bounce_page(&cc
->cpages
[i
]);
912 for (i
= 0; i
< cc
->nr_cpages
; i
++) {
915 f2fs_put_page(cc
->cpages
[i
], 1);
926 void f2fs_compress_write_end_io(struct bio
*bio
, struct page
*page
)
928 struct f2fs_sb_info
*sbi
= bio
->bi_private
;
929 struct compress_io_ctx
*cic
=
930 (struct compress_io_ctx
*)page_private(page
);
933 if (unlikely(bio
->bi_status
))
934 mapping_set_error(cic
->inode
->i_mapping
, -EIO
);
936 f2fs_put_compressed_page(page
);
938 dec_page_count(sbi
, F2FS_WB_DATA
);
940 if (refcount_dec_not_one(&cic
->ref
))
943 for (i
= 0; i
< cic
->nr_rpages
; i
++) {
944 WARN_ON(!cic
->rpages
[i
]);
945 clear_cold_data(cic
->rpages
[i
]);
946 end_page_writeback(cic
->rpages
[i
]);
953 static int f2fs_write_raw_pages(struct compress_ctx
*cc
,
955 struct writeback_control
*wbc
,
956 enum iostat_type io_type
)
958 struct address_space
*mapping
= cc
->inode
->i_mapping
;
959 int _submitted
, compr_blocks
, ret
;
962 compr_blocks
= f2fs_compressed_blocks(cc
);
963 if (compr_blocks
< 0) {
968 for (i
= 0; i
< cc
->cluster_size
; i
++) {
972 if (cc
->rpages
[i
]->mapping
!= mapping
) {
973 unlock_page(cc
->rpages
[i
]);
977 BUG_ON(!PageLocked(cc
->rpages
[i
]));
979 ret
= f2fs_write_single_data_page(cc
->rpages
[i
], &_submitted
,
980 NULL
, NULL
, wbc
, io_type
,
983 if (ret
== AOP_WRITEPAGE_ACTIVATE
) {
984 unlock_page(cc
->rpages
[i
]);
986 } else if (ret
== -EAGAIN
) {
989 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
990 lock_page(cc
->rpages
[i
]);
991 clear_page_dirty_for_io(cc
->rpages
[i
]);
998 *submitted
+= _submitted
;
1003 /* TODO: revoke partially updated block addresses */
1004 BUG_ON(compr_blocks
);
1006 for (++i
; i
< cc
->cluster_size
; i
++) {
1009 redirty_page_for_writepage(wbc
, cc
->rpages
[i
]);
1010 unlock_page(cc
->rpages
[i
]);
1015 int f2fs_write_multi_pages(struct compress_ctx
*cc
,
1017 struct writeback_control
*wbc
,
1018 enum iostat_type io_type
)
1020 struct f2fs_inode_info
*fi
= F2FS_I(cc
->inode
);
1021 const struct f2fs_compress_ops
*cops
=
1022 f2fs_cops
[fi
->i_compress_algorithm
];
1026 if (cluster_may_compress(cc
)) {
1027 err
= f2fs_compress_pages(cc
);
1028 if (err
== -EAGAIN
) {
1031 f2fs_put_rpages_wbc(cc
, wbc
, true, 1);
1035 err
= f2fs_write_compressed_pages(cc
, submitted
,
1037 cops
->destroy_compress_ctx(cc
);
1040 f2fs_bug_on(F2FS_I_SB(cc
->inode
), err
!= -EAGAIN
);
1043 f2fs_bug_on(F2FS_I_SB(cc
->inode
), *submitted
);
1045 err
= f2fs_write_raw_pages(cc
, submitted
, wbc
, io_type
);
1046 f2fs_put_rpages_wbc(cc
, wbc
, false, 0);
1048 f2fs_destroy_compress_ctx(cc
);
1052 struct decompress_io_ctx
*f2fs_alloc_dic(struct compress_ctx
*cc
)
1054 struct f2fs_sb_info
*sbi
= F2FS_I_SB(cc
->inode
);
1055 struct decompress_io_ctx
*dic
;
1056 pgoff_t start_idx
= start_idx_of_cluster(cc
);
1059 dic
= f2fs_kzalloc(sbi
, sizeof(struct decompress_io_ctx
), GFP_NOFS
);
1061 return ERR_PTR(-ENOMEM
);
1063 dic
->rpages
= f2fs_kzalloc(sbi
, sizeof(struct page
*) <<
1064 cc
->log_cluster_size
, GFP_NOFS
);
1067 return ERR_PTR(-ENOMEM
);
1070 dic
->magic
= F2FS_COMPRESSED_PAGE_MAGIC
;
1071 dic
->inode
= cc
->inode
;
1072 refcount_set(&dic
->ref
, 1);
1073 dic
->cluster_idx
= cc
->cluster_idx
;
1074 dic
->cluster_size
= cc
->cluster_size
;
1075 dic
->log_cluster_size
= cc
->log_cluster_size
;
1076 dic
->nr_cpages
= cc
->nr_cpages
;
1077 dic
->failed
= false;
1079 for (i
= 0; i
< dic
->cluster_size
; i
++)
1080 dic
->rpages
[i
] = cc
->rpages
[i
];
1081 dic
->nr_rpages
= cc
->cluster_size
;
1083 dic
->cpages
= f2fs_kzalloc(sbi
, sizeof(struct page
*) *
1084 dic
->nr_cpages
, GFP_NOFS
);
1088 for (i
= 0; i
< dic
->nr_cpages
; i
++) {
1091 page
= f2fs_grab_page();
1095 f2fs_set_compressed_page(page
, cc
->inode
,
1097 dic
, i
? &dic
->ref
: NULL
);
1098 dic
->cpages
[i
] = page
;
1101 dic
->tpages
= f2fs_kzalloc(sbi
, sizeof(struct page
*) *
1102 dic
->cluster_size
, GFP_NOFS
);
1106 for (i
= 0; i
< dic
->cluster_size
; i
++) {
1110 dic
->tpages
[i
] = f2fs_grab_page();
1111 if (!dic
->tpages
[i
])
1115 for (i
= 0; i
< dic
->cluster_size
; i
++) {
1118 dic
->tpages
[i
] = cc
->rpages
[i
];
1125 return ERR_PTR(-ENOMEM
);
1128 void f2fs_free_dic(struct decompress_io_ctx
*dic
)
1133 for (i
= 0; i
< dic
->cluster_size
; i
++) {
1136 f2fs_put_page(dic
->tpages
[i
], 1);
1142 for (i
= 0; i
< dic
->nr_cpages
; i
++) {
1143 if (!dic
->cpages
[i
])
1145 f2fs_put_compressed_page(dic
->cpages
[i
]);
1154 void f2fs_decompress_end_io(struct page
**rpages
,
1155 unsigned int cluster_size
, bool err
, bool verity
)
1159 for (i
= 0; i
< cluster_size
; i
++) {
1160 struct page
*rpage
= rpages
[i
];
1165 if (err
|| PageError(rpage
)) {
1166 ClearPageUptodate(rpage
);
1167 ClearPageError(rpage
);
1169 if (!verity
|| fsverity_verify_page(rpage
))
1170 SetPageUptodate(rpage
);
1172 SetPageError(rpage
);