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/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>
21 #include <trace/events/f2fs.h>
23 static struct kmem_cache *cic_entry_slab;
24 static struct kmem_cache *dic_entry_slab;
26 static void *page_array_alloc(struct inode *inode, int nr)
28 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
29 unsigned int size = sizeof(struct page *) * nr;
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);
37 static void page_array_free(struct inode *inode, void *pages, int nr)
39 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
40 unsigned int size = sizeof(struct page *) * nr;
45 if (likely(size <= sbi->page_array_slab_size))
46 kmem_cache_free(sbi->page_array_slab, pages);
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);
60 static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
62 return index & (cc->cluster_size - 1);
65 static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
67 return index >> cc->log_cluster_size;
70 static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
72 return cc->cluster_idx << cc->log_cluster_size;
75 bool f2fs_is_compressed_page(struct page *page)
77 if (!PagePrivate(page))
79 if (!page_private(page))
81 if (page_private_nonpointer(page))
84 f2fs_bug_on(F2FS_M_SB(page->mapping),
85 *((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
89 static void f2fs_set_compressed_page(struct page *page,
90 struct inode *inode, pgoff_t index, void *data)
92 attach_page_private(page, (void *)data);
94 /* i_crypto_info and iv index */
96 page->mapping = inode->i_mapping;
99 static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
103 for (i = 0; i < len; i++) {
107 unlock_page(cc->rpages[i]);
109 put_page(cc->rpages[i]);
113 static void f2fs_put_rpages(struct compress_ctx *cc)
115 f2fs_drop_rpages(cc, cc->cluster_size, false);
118 static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
120 f2fs_drop_rpages(cc, len, true);
123 static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
124 struct writeback_control *wbc, bool redirty, int unlock)
128 for (i = 0; i < cc->cluster_size; i++) {
132 redirty_page_for_writepage(wbc, cc->rpages[i]);
133 f2fs_put_page(cc->rpages[i], unlock);
137 struct page *f2fs_compress_control_page(struct page *page)
139 return ((struct compress_io_ctx *)page_private(page))->rpages[0];
142 int f2fs_init_compress_ctx(struct compress_ctx *cc)
147 cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
148 return cc->rpages ? 0 : -ENOMEM;
151 void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse)
153 page_array_free(cc->inode, cc->rpages, cc->cluster_size);
157 cc->valid_nr_cpages = 0;
159 cc->cluster_idx = NULL_CLUSTER;
162 void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
164 unsigned int cluster_ofs;
166 if (!f2fs_cluster_can_merge_page(cc, page->index))
167 f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
169 cluster_ofs = offset_in_cluster(cc, page->index);
170 cc->rpages[cluster_ofs] = page;
172 cc->cluster_idx = cluster_idx(cc, page->index);
175 #ifdef CONFIG_F2FS_FS_LZO
176 static int lzo_init_compress_ctx(struct compress_ctx *cc)
178 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
179 LZO1X_MEM_COMPRESS, GFP_NOFS);
183 cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
187 static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
193 static int lzo_compress_pages(struct compress_ctx *cc)
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);
207 static int lzo_decompress_pages(struct decompress_io_ctx *dic)
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);
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,
224 PAGE_SIZE << dic->log_cluster_size);
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,
238 #ifdef CONFIG_F2FS_FS_LZ4
239 static int lz4_init_compress_ctx(struct compress_ctx *cc)
241 unsigned int size = LZ4_MEM_COMPRESS;
243 #ifdef CONFIG_F2FS_FS_LZ4HC
244 if (F2FS_I(cc->inode)->i_compress_flag >> COMPRESS_LEVEL_OFFSET)
245 size = LZ4HC_MEM_COMPRESS;
248 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS);
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.
257 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
261 static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
267 #ifdef CONFIG_F2FS_FS_LZ4HC
268 static int lz4hc_compress_pages(struct compress_ctx *cc)
270 unsigned char level = F2FS_I(cc->inode)->i_compress_flag >>
271 COMPRESS_LEVEL_OFFSET;
275 len = LZ4_compress_HC(cc->rbuf, cc->cbuf->cdata, cc->rlen,
276 cc->clen, level, cc->private);
278 len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
279 cc->clen, cc->private);
288 static int lz4_compress_pages(struct compress_ctx *cc)
292 #ifdef CONFIG_F2FS_FS_LZ4HC
293 return lz4hc_compress_pages(cc);
295 len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
296 cc->clen, cc->private);
304 static int lz4_decompress_pages(struct decompress_io_ctx *dic)
308 ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
309 dic->clen, dic->rlen);
311 printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
312 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
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,
321 PAGE_SIZE << dic->log_cluster_size);
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,
335 #ifdef CONFIG_F2FS_FS_ZSTD
336 #define F2FS_ZSTD_DEFAULT_CLEVEL 1
338 static int zstd_init_compress_ctx(struct compress_ctx *cc)
340 zstd_parameters params;
341 zstd_cstream *stream;
343 unsigned int workspace_size;
344 unsigned char level = F2FS_I(cc->inode)->i_compress_flag >>
345 COMPRESS_LEVEL_OFFSET;
348 level = F2FS_ZSTD_DEFAULT_CLEVEL;
350 params = zstd_get_params(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen);
351 workspace_size = zstd_cstream_workspace_bound(¶ms.cParams);
353 workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
354 workspace_size, GFP_NOFS);
358 stream = zstd_init_cstream(¶ms, 0, workspace, workspace_size);
360 printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_cstream failed\n",
361 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
367 cc->private = workspace;
368 cc->private2 = stream;
370 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
374 static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
381 static int zstd_compress_pages(struct compress_ctx *cc)
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;
391 inbuf.src = cc->rbuf;
392 inbuf.size = src_size;
395 outbuf.dst = cc->cbuf->cdata;
396 outbuf.size = dst_size;
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));
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));
415 * there is compressed data remained in intermediate buffer due to
416 * no more space in cbuf.cdata
421 cc->clen = outbuf.pos;
425 static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
427 zstd_dstream *stream;
429 unsigned int workspace_size;
430 unsigned int max_window_size =
431 MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
433 workspace_size = zstd_dstream_workspace_bound(max_window_size);
435 workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
436 workspace_size, GFP_NOFS);
440 stream = zstd_init_dstream(max_window_size, workspace, workspace_size);
442 printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_dstream failed\n",
443 KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
449 dic->private = workspace;
450 dic->private2 = stream;
455 static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
457 kvfree(dic->private);
459 dic->private2 = NULL;
462 static int zstd_decompress_pages(struct decompress_io_ctx *dic)
464 zstd_dstream *stream = dic->private2;
465 zstd_in_buffer inbuf;
466 zstd_out_buffer outbuf;
470 inbuf.src = dic->cbuf->cdata;
471 inbuf.size = dic->clen;
474 outbuf.dst = dic->rbuf;
475 outbuf.size = dic->rlen;
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));
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,
490 PAGE_SIZE << dic->log_cluster_size);
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,
507 #ifdef CONFIG_F2FS_FS_LZO
508 #ifdef CONFIG_F2FS_FS_LZORLE
509 static int lzorle_compress_pages(struct compress_ctx *cc)
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);
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,
532 static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
533 #ifdef CONFIG_F2FS_FS_LZO
538 #ifdef CONFIG_F2FS_FS_LZ4
543 #ifdef CONFIG_F2FS_FS_ZSTD
548 #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
555 bool f2fs_is_compress_backend_ready(struct inode *inode)
557 if (!f2fs_compressed_file(inode))
559 return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
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");
568 int f2fs_init_compress_mempool(void)
570 compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
571 if (!compress_page_pool)
577 void f2fs_destroy_compress_mempool(void)
579 mempool_destroy(compress_page_pool);
582 static struct page *f2fs_compress_alloc_page(void)
586 page = mempool_alloc(compress_page_pool, GFP_NOFS);
592 static void f2fs_compress_free_page(struct page *page)
596 detach_page_private(page);
597 page->mapping = NULL;
599 mempool_free(page, compress_page_pool);
602 #define MAX_VMAP_RETRIES 3
604 static void *f2fs_vmap(struct page **pages, unsigned int count)
609 for (i = 0; i < MAX_VMAP_RETRIES; i++) {
610 buf = vm_map_ram(pages, count, -1);
618 static int f2fs_compress_pages(struct compress_ctx *cc)
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;
627 trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
628 cc->cluster_size, fi->i_compress_algorithm);
630 if (cops->init_compress_ctx) {
631 ret = cops->init_compress_ctx(cc);
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;
640 cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
643 goto destroy_compress_ctx;
646 for (i = 0; i < cc->nr_cpages; i++) {
647 cc->cpages[i] = f2fs_compress_alloc_page();
648 if (!cc->cpages[i]) {
650 goto out_free_cpages;
654 cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
657 goto out_free_cpages;
660 cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
663 goto out_vunmap_rbuf;
666 ret = cops->compress_pages(cc);
668 goto out_vunmap_cbuf;
670 max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
672 if (cc->clen > max_len) {
674 goto out_vunmap_cbuf;
677 cc->cbuf->clen = cpu_to_le32(cc->clen);
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);
684 for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
685 cc->cbuf->reserved[i] = cpu_to_le32(0);
687 new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
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));
694 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
695 vm_unmap_ram(cc->rbuf, cc->cluster_size);
697 for (i = 0; i < cc->nr_cpages; i++) {
698 if (i < new_nr_cpages)
700 f2fs_compress_free_page(cc->cpages[i]);
701 cc->cpages[i] = NULL;
704 if (cops->destroy_compress_ctx)
705 cops->destroy_compress_ctx(cc);
707 cc->valid_nr_cpages = new_nr_cpages;
709 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
714 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
716 vm_unmap_ram(cc->rbuf, cc->cluster_size);
718 for (i = 0; i < cc->nr_cpages; i++) {
720 f2fs_compress_free_page(cc->cpages[i]);
722 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
724 destroy_compress_ctx:
725 if (cops->destroy_compress_ctx)
726 cops->destroy_compress_ctx(cc);
728 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
733 void f2fs_decompress_cluster(struct decompress_io_ctx *dic)
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];
742 trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
743 dic->cluster_size, fi->i_compress_algorithm);
750 dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
756 for (i = 0; i < dic->cluster_size; i++) {
757 if (dic->rpages[i]) {
758 dic->tpages[i] = dic->rpages[i];
762 dic->tpages[i] = f2fs_compress_alloc_page();
763 if (!dic->tpages[i]) {
769 if (cops->init_decompress_ctx) {
770 ret = cops->init_decompress_ctx(dic);
775 dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
778 goto out_destroy_decompress_ctx;
781 dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
784 goto out_vunmap_rbuf;
787 dic->clen = le32_to_cpu(dic->cbuf->clen);
788 dic->rlen = PAGE_SIZE << dic->log_cluster_size;
790 if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
792 goto out_vunmap_cbuf;
795 ret = cops->decompress_pages(dic);
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);
801 if (provided != calculated) {
802 if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) {
803 set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT);
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);
809 set_sbi_flag(sbi, SBI_NEED_FSCK);
814 vm_unmap_ram(dic->cbuf, dic->nr_cpages);
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);
821 trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
823 f2fs_decompress_end_io(dic, ret);
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).
832 void f2fs_end_read_compressed_page(struct page *page, bool failed,
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);
839 dec_page_count(sbi, F2FS_RD_DATA);
842 WRITE_ONCE(dic->failed, true);
844 f2fs_cache_compressed_page(sbi, page,
845 dic->inode->i_ino, blkaddr);
847 if (atomic_dec_and_test(&dic->remaining_pages))
848 f2fs_decompress_cluster(dic);
851 static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
853 if (cc->cluster_idx == NULL_CLUSTER)
855 return cc->cluster_idx == cluster_idx(cc, index);
858 bool f2fs_cluster_is_empty(struct compress_ctx *cc)
860 return cc->nr_rpages == 0;
863 static bool f2fs_cluster_is_full(struct compress_ctx *cc)
865 return cc->cluster_size == cc->nr_rpages;
868 bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
870 if (f2fs_cluster_is_empty(cc))
872 return is_page_in_cluster(cc, index);
875 bool f2fs_all_cluster_page_loaded(struct compress_ctx *cc, struct pagevec *pvec,
876 int index, int nr_pages)
881 if (nr_pages - index < cc->cluster_size)
884 pgidx = pvec->pages[index]->index;
886 for (i = 1; i < cc->cluster_size; i++) {
887 if (pvec->pages[index + i]->index != pgidx + i)
894 static bool cluster_has_invalid_data(struct compress_ctx *cc)
896 loff_t i_size = i_size_read(cc->inode);
897 unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
900 for (i = 0; i < cc->cluster_size; i++) {
901 struct page *page = cc->rpages[i];
903 f2fs_bug_on(F2FS_I_SB(cc->inode), !page);
906 if (page->index >= nr_pages)
912 bool f2fs_sanity_check_cluster(struct dnode_of_data *dn)
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;
924 /* [..., COMPR_ADDR, ...] */
925 if (dn->ofs_in_node % cluster_size) {
926 reason = "[*|C|*|*]";
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);
934 /* [COMPR_ADDR, ..., COMPR_ADDR] */
935 if (blkaddr == COMPRESS_ADDR) {
936 reason = "[C|*|C|*]";
940 if (!__is_valid_data_blkaddr(blkaddr)) {
945 /* [COMPR_ADDR, NULL_ADDR or NEW_ADDR, valid_blkaddr] */
947 reason = "[C|N|N|V]";
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);
960 static int __f2fs_cluster_blocks(struct inode *inode,
961 unsigned int cluster_idx, bool compr)
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;
969 set_new_dnode(&dn, inode, NULL, NULL, 0);
970 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
977 if (f2fs_sanity_check_cluster(&dn)) {
982 if (dn.data_blkaddr == COMPRESS_ADDR) {
986 for (i = 1; i < cluster_size; i++) {
989 blkaddr = data_blkaddr(dn.inode,
990 dn.node_page, dn.ofs_in_node + i);
992 if (__is_valid_data_blkaddr(blkaddr))
995 if (blkaddr != NULL_ADDR)
1000 f2fs_bug_on(F2FS_I_SB(inode),
1001 !compr && ret != cluster_size &&
1002 !is_inode_flag_set(inode, FI_COMPRESS_RELEASED));
1005 f2fs_put_dnode(&dn);
1009 /* return # of compressed blocks in compressed cluster */
1010 static int f2fs_compressed_blocks(struct compress_ctx *cc)
1012 return __f2fs_cluster_blocks(cc->inode, cc->cluster_idx, true);
1015 /* return # of valid blocks in compressed cluster */
1016 int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
1018 return __f2fs_cluster_blocks(inode,
1019 index >> F2FS_I(inode)->i_log_cluster_size,
1023 static bool cluster_may_compress(struct compress_ctx *cc)
1025 if (!f2fs_need_compress_data(cc->inode))
1027 if (f2fs_is_atomic_file(cc->inode))
1029 if (!f2fs_cluster_is_full(cc))
1031 if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
1033 return !cluster_has_invalid_data(cc);
1036 static void set_cluster_writeback(struct compress_ctx *cc)
1040 for (i = 0; i < cc->cluster_size; i++) {
1042 set_page_writeback(cc->rpages[i]);
1046 static void set_cluster_dirty(struct compress_ctx *cc)
1050 for (i = 0; i < cc->cluster_size; i++)
1052 set_page_dirty(cc->rpages[i]);
1055 static int prepare_compress_overwrite(struct compress_ctx *cc,
1056 struct page **pagep, pgoff_t index, void **fsdata)
1058 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
1059 struct address_space *mapping = cc->inode->i_mapping;
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);
1067 ret = f2fs_is_compressed_cluster(cc->inode, start_idx);
1071 ret = f2fs_init_compress_ctx(cc);
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);
1084 if (PageUptodate(page))
1085 f2fs_put_page(page, 1);
1087 f2fs_compress_ctx_add_page(cc, page);
1090 if (!f2fs_cluster_is_empty(cc)) {
1091 struct bio *bio = NULL;
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);
1100 f2fs_submit_bio(sbi, bio, DATA);
1102 ret = f2fs_init_compress_ctx(cc);
1107 for (i = 0; i < cc->cluster_size; i++) {
1108 f2fs_bug_on(sbi, cc->rpages[i]);
1110 page = find_lock_page(mapping, start_idx + i);
1112 /* page can be truncated */
1113 goto release_and_retry;
1116 f2fs_wait_on_page_writeback(page, DATA, true, true);
1117 f2fs_compress_ctx_add_page(cc, page);
1119 if (!PageUptodate(page)) {
1121 f2fs_put_rpages(cc);
1122 f2fs_unlock_rpages(cc, i + 1);
1123 f2fs_destroy_compress_ctx(cc, true);
1129 *fsdata = cc->rpages;
1130 *pagep = cc->rpages[offset_in_cluster(cc, index)];
1131 return cc->cluster_size;
1135 f2fs_put_rpages(cc);
1136 f2fs_unlock_rpages(cc, i);
1137 f2fs_destroy_compress_ctx(cc, true);
1142 int f2fs_prepare_compress_overwrite(struct inode *inode,
1143 struct page **pagep, pgoff_t index, void **fsdata)
1145 struct compress_ctx cc = {
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,
1154 return prepare_compress_overwrite(&cc, pagep, index, fsdata);
1157 bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
1158 pgoff_t index, unsigned copied)
1161 struct compress_ctx cc = {
1163 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
1164 .cluster_size = F2FS_I(inode)->i_cluster_size,
1167 bool first_index = (index == cc.rpages[0]->index);
1170 set_cluster_dirty(&cc);
1172 f2fs_put_rpages_wbc(&cc, NULL, false, 1);
1173 f2fs_destroy_compress_ctx(&cc, false);
1178 int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
1180 void *fsdata = NULL;
1182 int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
1183 pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
1187 err = f2fs_is_compressed_cluster(inode, start_idx);
1191 /* truncate normal cluster */
1193 return f2fs_do_truncate_blocks(inode, from, lock);
1195 /* truncate compressed cluster */
1196 err = f2fs_prepare_compress_overwrite(inode, &pagep,
1197 start_idx, &fsdata);
1199 /* should not be a normal cluster */
1200 f2fs_bug_on(F2FS_I_SB(inode), err == 0);
1206 struct page **rpages = fsdata;
1207 int cluster_size = F2FS_I(inode)->i_cluster_size;
1210 for (i = cluster_size - 1; i >= 0; i--) {
1211 loff_t start = rpages[i]->index << PAGE_SHIFT;
1213 if (from <= start) {
1214 zero_user_segment(rpages[i], 0, PAGE_SIZE);
1216 zero_user_segment(rpages[i], from - start,
1222 f2fs_compress_write_end(inode, fsdata, start_idx, true);
1227 static int f2fs_write_compressed_pages(struct compress_ctx *cc,
1229 struct writeback_control *wbc,
1230 enum iostat_type io_type)
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 = {
1237 .ino = cc->inode->i_ino,
1240 .op_flags = wbc_to_write_flags(wbc),
1241 .old_blkaddr = NEW_ADDR,
1243 .encrypted_page = NULL,
1244 .compressed_page = NULL,
1248 .encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode),
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;
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);
1264 if (IS_NOQUOTA(inode)) {
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.
1270 down_read(&sbi->node_write);
1271 } else if (!f2fs_trylock_op(sbi)) {
1275 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1277 err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
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)
1287 psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
1289 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni);
1293 fio.version = ni.version;
1295 cic = f2fs_kmem_cache_alloc(cic_entry_slab, GFP_F2FS_ZERO, false, sbi);
1299 cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
1301 atomic_set(&cic->pending_pages, cc->valid_nr_cpages);
1302 cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1306 cic->nr_rpages = cc->cluster_size;
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];
1313 fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
1314 dn.ofs_in_node + i + 1);
1316 /* wait for GCed page writeback via META_MAPPING */
1317 f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
1319 if (fio.encrypted) {
1320 fio.page = cc->rpages[i + 1];
1321 err = f2fs_encrypt_one_page(&fio);
1323 goto out_destroy_crypt;
1324 cc->cpages[i] = fio.encrypted_page;
1328 set_cluster_writeback(cc);
1330 for (i = 0; i < cc->cluster_size; i++)
1331 cic->rpages[i] = cc->rpages[i];
1333 for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
1336 blkaddr = f2fs_data_blkaddr(&dn);
1337 fio.page = cc->rpages[i];
1338 fio.old_blkaddr = blkaddr;
1340 /* cluster header */
1342 if (blkaddr == COMPRESS_ADDR)
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;
1350 if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
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);
1358 goto unlock_continue;
1361 f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
1364 fio.encrypted_page = cc->cpages[i - 1];
1366 fio.compressed_page = cc->cpages[i - 1];
1368 cc->cpages[i - 1] = NULL;
1369 f2fs_outplace_write_data(&dn, &fio);
1372 inode_dec_dirty_pages(cc->inode);
1373 unlock_page(fio.page);
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);
1381 set_inode_flag(cc->inode, FI_APPEND_WRITE);
1382 if (cc->cluster_idx == 0)
1383 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1385 f2fs_put_dnode(&dn);
1386 if (IS_NOQUOTA(inode))
1387 up_read(&sbi->node_write);
1389 f2fs_unlock_op(sbi);
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);
1396 f2fs_put_rpages(cc);
1397 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1399 f2fs_destroy_compress_ctx(cc, false);
1403 page_array_free(cc->inode, cic->rpages, cc->cluster_size);
1405 for (--i; i >= 0; i--)
1406 fscrypt_finalize_bounce_page(&cc->cpages[i]);
1408 kmem_cache_free(cic_entry_slab, cic);
1410 f2fs_put_dnode(&dn);
1412 if (IS_NOQUOTA(inode))
1413 up_read(&sbi->node_write);
1415 f2fs_unlock_op(sbi);
1417 for (i = 0; i < cc->valid_nr_cpages; i++) {
1418 f2fs_compress_free_page(cc->cpages[i]);
1419 cc->cpages[i] = NULL;
1421 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1426 void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
1428 struct f2fs_sb_info *sbi = bio->bi_private;
1429 struct compress_io_ctx *cic =
1430 (struct compress_io_ctx *)page_private(page);
1433 if (unlikely(bio->bi_status))
1434 mapping_set_error(cic->inode->i_mapping, -EIO);
1436 f2fs_compress_free_page(page);
1438 dec_page_count(sbi, F2FS_WB_DATA);
1440 if (atomic_dec_return(&cic->pending_pages))
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]);
1449 page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
1450 kmem_cache_free(cic_entry_slab, cic);
1453 static int f2fs_write_raw_pages(struct compress_ctx *cc,
1455 struct writeback_control *wbc,
1456 enum iostat_type io_type)
1458 struct address_space *mapping = cc->inode->i_mapping;
1459 int _submitted, compr_blocks, ret;
1460 int i = -1, err = 0;
1462 compr_blocks = f2fs_compressed_blocks(cc);
1463 if (compr_blocks < 0) {
1468 for (i = 0; i < cc->cluster_size; i++) {
1472 if (cc->rpages[i]->mapping != mapping) {
1473 unlock_page(cc->rpages[i]);
1477 BUG_ON(!PageLocked(cc->rpages[i]));
1479 ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
1480 NULL, NULL, wbc, io_type,
1481 compr_blocks, false);
1483 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1484 unlock_page(cc->rpages[i]);
1486 } else if (ret == -EAGAIN) {
1488 * for quota file, just redirty left pages to
1489 * avoid deadlock caused by cluster update race
1490 * from foreground operation.
1492 if (IS_NOQUOTA(cc->inode)) {
1498 congestion_wait(BLK_RW_ASYNC,
1499 DEFAULT_IO_TIMEOUT);
1500 lock_page(cc->rpages[i]);
1502 if (!PageDirty(cc->rpages[i])) {
1503 unlock_page(cc->rpages[i]);
1507 clear_page_dirty_for_io(cc->rpages[i]);
1514 *submitted += _submitted;
1517 f2fs_balance_fs(F2FS_M_SB(mapping), true);
1521 for (++i; i < cc->cluster_size; i++) {
1524 redirty_page_for_writepage(wbc, cc->rpages[i]);
1525 unlock_page(cc->rpages[i]);
1530 int f2fs_write_multi_pages(struct compress_ctx *cc,
1532 struct writeback_control *wbc,
1533 enum iostat_type io_type)
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);
1544 f2fs_put_rpages_wbc(cc, wbc, true, 1);
1548 err = f2fs_write_compressed_pages(cc, submitted,
1552 f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1555 f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
1557 err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
1558 f2fs_put_rpages_wbc(cc, wbc, false, 0);
1560 f2fs_destroy_compress_ctx(cc, false);
1564 static void f2fs_free_dic(struct decompress_io_ctx *dic);
1566 struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1568 struct decompress_io_ctx *dic;
1569 pgoff_t start_idx = start_idx_of_cluster(cc);
1572 dic = f2fs_kmem_cache_alloc(dic_entry_slab, GFP_F2FS_ZERO,
1573 false, F2FS_I_SB(cc->inode));
1575 return ERR_PTR(-ENOMEM);
1577 dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1579 kmem_cache_free(dic_entry_slab, dic);
1580 return ERR_PTR(-ENOMEM);
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);
1594 for (i = 0; i < dic->cluster_size; i++)
1595 dic->rpages[i] = cc->rpages[i];
1596 dic->nr_rpages = cc->cluster_size;
1598 dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
1602 for (i = 0; i < dic->nr_cpages; i++) {
1605 page = f2fs_compress_alloc_page();
1609 f2fs_set_compressed_page(page, cc->inode,
1610 start_idx + i + 1, dic);
1611 dic->cpages[i] = page;
1618 return ERR_PTR(-ENOMEM);
1621 static void f2fs_free_dic(struct decompress_io_ctx *dic)
1626 for (i = 0; i < dic->cluster_size; i++) {
1629 if (!dic->tpages[i])
1631 f2fs_compress_free_page(dic->tpages[i]);
1633 page_array_free(dic->inode, dic->tpages, dic->cluster_size);
1637 for (i = 0; i < dic->nr_cpages; i++) {
1638 if (!dic->cpages[i])
1640 f2fs_compress_free_page(dic->cpages[i]);
1642 page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
1645 page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
1646 kmem_cache_free(dic_entry_slab, dic);
1649 static void f2fs_put_dic(struct decompress_io_ctx *dic)
1651 if (refcount_dec_and_test(&dic->refcnt))
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.
1659 static void __f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed)
1663 for (i = 0; i < dic->cluster_size; i++) {
1664 struct page *rpage = dic->rpages[i];
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);
1675 SetPageUptodate(rpage);
1683 static void f2fs_verify_cluster(struct work_struct *work)
1685 struct decompress_io_ctx *dic =
1686 container_of(work, struct decompress_io_ctx, verity_work);
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];
1693 if (rpage && !fsverity_verify_page(rpage))
1694 SetPageError(rpage);
1697 __f2fs_decompress_end_io(dic, false);
1701 * This is called when a compressed cluster has been decompressed
1702 * (or failed to be read and/or decompressed).
1704 void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed)
1706 if (!failed && dic->need_verity) {
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.
1713 INIT_WORK(&dic->verity_work, f2fs_verify_cluster);
1714 fsverity_enqueue_verify_work(&dic->verity_work);
1716 __f2fs_decompress_end_io(dic, failed);
1721 * Put a reference to a compressed page's decompress_io_ctx.
1723 * This is called when the page is no longer needed and can be freed.
1725 void f2fs_put_page_dic(struct page *page)
1727 struct decompress_io_ctx *dic =
1728 (struct decompress_io_ctx *)page_private(page);
1734 * check whether cluster blocks are contiguous, and add extent cache entry
1735 * only if cluster blocks are logically and physically contiguous.
1737 unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn)
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);
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);
1748 if (!__is_valid_data_blkaddr(blkaddr))
1750 if (first_blkaddr + i - (compressed ? 1 : 0) != blkaddr)
1754 return compressed ? i - 1 : i;
1757 const struct address_space_operations f2fs_compress_aops = {
1758 .releasepage = f2fs_release_page,
1759 .invalidatepage = f2fs_invalidate_page,
1762 struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
1764 return sbi->compress_inode->i_mapping;
1767 void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
1769 if (!sbi->compress_inode)
1771 invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
1774 void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
1775 nid_t ino, block_t blkaddr)
1780 if (!test_opt(sbi, COMPRESS_CACHE))
1783 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
1786 if (!f2fs_available_free_memory(sbi, COMPRESS_PAGE))
1789 cpage = find_get_page(COMPRESS_MAPPING(sbi), blkaddr);
1791 f2fs_put_page(cpage, 0);
1795 cpage = alloc_page(__GFP_NOWARN | __GFP_IO);
1799 ret = add_to_page_cache_lru(cpage, COMPRESS_MAPPING(sbi),
1802 f2fs_put_page(cpage, 0);
1806 set_page_private_data(cpage, ino);
1808 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
1811 memcpy(page_address(cpage), page_address(page), PAGE_SIZE);
1812 SetPageUptodate(cpage);
1814 f2fs_put_page(cpage, 1);
1817 bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
1821 bool hitted = false;
1823 if (!test_opt(sbi, COMPRESS_CACHE))
1826 cpage = f2fs_pagecache_get_page(COMPRESS_MAPPING(sbi),
1827 blkaddr, FGP_LOCK | FGP_NOWAIT, GFP_NOFS);
1829 if (PageUptodate(cpage)) {
1830 atomic_inc(&sbi->compress_page_hit);
1831 memcpy(page_address(page),
1832 page_address(cpage), PAGE_SIZE);
1835 f2fs_put_page(cpage, 1);
1841 void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino)
1843 struct address_space *mapping = sbi->compress_inode->i_mapping;
1844 struct pagevec pvec;
1846 pgoff_t end = MAX_BLKADDR(sbi);
1848 if (!mapping->nrpages)
1851 pagevec_init(&pvec);
1854 unsigned int nr_pages;
1857 nr_pages = pagevec_lookup_range(&pvec, mapping,
1862 for (i = 0; i < nr_pages; i++) {
1863 struct page *page = pvec.pages[i];
1865 if (page->index > end)
1869 if (page->mapping != mapping) {
1874 if (ino != get_page_private_data(page)) {
1879 generic_error_remove_page(mapping, page);
1882 pagevec_release(&pvec);
1884 } while (index < end);
1887 int f2fs_init_compress_inode(struct f2fs_sb_info *sbi)
1889 struct inode *inode;
1891 if (!test_opt(sbi, COMPRESS_CACHE))
1894 inode = f2fs_iget(sbi->sb, F2FS_COMPRESS_INO(sbi));
1896 return PTR_ERR(inode);
1897 sbi->compress_inode = inode;
1899 sbi->compress_percent = COMPRESS_PERCENT;
1900 sbi->compress_watermark = COMPRESS_WATERMARK;
1902 atomic_set(&sbi->compress_page_hit, 0);
1907 void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi)
1909 if (!sbi->compress_inode)
1911 iput(sbi->compress_inode);
1912 sbi->compress_inode = NULL;
1915 int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
1917 dev_t dev = sbi->sb->s_bdev->bd_dev;
1920 sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
1922 sbi->page_array_slab_size = sizeof(struct page *) <<
1923 F2FS_OPTION(sbi).compress_log_size;
1925 sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
1926 sbi->page_array_slab_size);
1927 if (!sbi->page_array_slab)
1932 void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
1934 kmem_cache_destroy(sbi->page_array_slab);
1937 static int __init f2fs_init_cic_cache(void)
1939 cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
1940 sizeof(struct compress_io_ctx));
1941 if (!cic_entry_slab)
1946 static void f2fs_destroy_cic_cache(void)
1948 kmem_cache_destroy(cic_entry_slab);
1951 static int __init f2fs_init_dic_cache(void)
1953 dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
1954 sizeof(struct decompress_io_ctx));
1955 if (!dic_entry_slab)
1960 static void f2fs_destroy_dic_cache(void)
1962 kmem_cache_destroy(dic_entry_slab);
1965 int __init f2fs_init_compress_cache(void)
1969 err = f2fs_init_cic_cache();
1972 err = f2fs_init_dic_cache();
1977 f2fs_destroy_cic_cache();
1982 void f2fs_destroy_compress_cache(void)
1984 f2fs_destroy_dic_cache();
1985 f2fs_destroy_cic_cache();