14c8ff709SChao Yu // SPDX-License-Identifier: GPL-2.0 24c8ff709SChao Yu /* 34c8ff709SChao Yu * f2fs compress support 44c8ff709SChao Yu * 54c8ff709SChao Yu * Copyright (c) 2019 Chao Yu <chao@kernel.org> 64c8ff709SChao Yu */ 74c8ff709SChao Yu 84c8ff709SChao Yu #include <linux/fs.h> 94c8ff709SChao Yu #include <linux/f2fs_fs.h> 104c8ff709SChao Yu #include <linux/writeback.h> 114c8ff709SChao Yu #include <linux/backing-dev.h> 124c8ff709SChao Yu #include <linux/lzo.h> 134c8ff709SChao Yu #include <linux/lz4.h> 1450cfa66fSChao Yu #include <linux/zstd.h> 154c8ff709SChao Yu 164c8ff709SChao Yu #include "f2fs.h" 174c8ff709SChao Yu #include "node.h" 184c8ff709SChao Yu #include <trace/events/f2fs.h> 194c8ff709SChao Yu 20c68d6c88SChao Yu static struct kmem_cache *cic_entry_slab; 21c68d6c88SChao Yu static struct kmem_cache *dic_entry_slab; 22c68d6c88SChao Yu 2331083031SChao Yu static void *page_array_alloc(struct inode *inode, int nr) 2431083031SChao Yu { 2531083031SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2631083031SChao Yu unsigned int size = sizeof(struct page *) * nr; 2731083031SChao Yu 2831083031SChao Yu if (likely(size <= sbi->page_array_slab_size)) 2931083031SChao Yu return kmem_cache_zalloc(sbi->page_array_slab, GFP_NOFS); 3031083031SChao Yu return f2fs_kzalloc(sbi, size, GFP_NOFS); 3131083031SChao Yu } 3231083031SChao Yu 3331083031SChao Yu static void page_array_free(struct inode *inode, void *pages, int nr) 3431083031SChao Yu { 3531083031SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3631083031SChao Yu unsigned int size = sizeof(struct page *) * nr; 3731083031SChao Yu 3831083031SChao Yu if (!pages) 3931083031SChao Yu return; 4031083031SChao Yu 4131083031SChao Yu if (likely(size <= sbi->page_array_slab_size)) 4231083031SChao Yu kmem_cache_free(sbi->page_array_slab, pages); 4331083031SChao Yu else 4431083031SChao Yu kfree(pages); 4531083031SChao Yu } 4631083031SChao Yu 474c8ff709SChao Yu struct f2fs_compress_ops { 484c8ff709SChao Yu int (*init_compress_ctx)(struct compress_ctx *cc); 494c8ff709SChao Yu void (*destroy_compress_ctx)(struct compress_ctx *cc); 504c8ff709SChao Yu int (*compress_pages)(struct compress_ctx *cc); 5123b1faaaSChao Yu int (*init_decompress_ctx)(struct decompress_io_ctx *dic); 5223b1faaaSChao Yu void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic); 534c8ff709SChao Yu int (*decompress_pages)(struct decompress_io_ctx *dic); 544c8ff709SChao Yu }; 554c8ff709SChao Yu 564c8ff709SChao Yu static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index) 574c8ff709SChao Yu { 584c8ff709SChao Yu return index & (cc->cluster_size - 1); 594c8ff709SChao Yu } 604c8ff709SChao Yu 614c8ff709SChao Yu static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index) 624c8ff709SChao Yu { 634c8ff709SChao Yu return index >> cc->log_cluster_size; 644c8ff709SChao Yu } 654c8ff709SChao Yu 664c8ff709SChao Yu static pgoff_t start_idx_of_cluster(struct compress_ctx *cc) 674c8ff709SChao Yu { 684c8ff709SChao Yu return cc->cluster_idx << cc->log_cluster_size; 694c8ff709SChao Yu } 704c8ff709SChao Yu 714c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page) 724c8ff709SChao Yu { 734c8ff709SChao Yu if (!PagePrivate(page)) 744c8ff709SChao Yu return false; 754c8ff709SChao Yu if (!page_private(page)) 764c8ff709SChao Yu return false; 774c8ff709SChao Yu if (IS_ATOMIC_WRITTEN_PAGE(page) || IS_DUMMY_WRITTEN_PAGE(page)) 784c8ff709SChao Yu return false; 7929b993c7SYu Changchun 804c8ff709SChao Yu f2fs_bug_on(F2FS_M_SB(page->mapping), 814c8ff709SChao Yu *((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC); 824c8ff709SChao Yu return true; 834c8ff709SChao Yu } 844c8ff709SChao Yu 854c8ff709SChao Yu static void f2fs_set_compressed_page(struct page *page, 86887347a0SChao Yu struct inode *inode, pgoff_t index, void *data) 874c8ff709SChao Yu { 884c8ff709SChao Yu SetPagePrivate(page); 894c8ff709SChao Yu set_page_private(page, (unsigned long)data); 904c8ff709SChao Yu 914c8ff709SChao Yu /* i_crypto_info and iv index */ 924c8ff709SChao Yu page->index = index; 934c8ff709SChao Yu page->mapping = inode->i_mapping; 944c8ff709SChao Yu } 954c8ff709SChao Yu 964c8ff709SChao Yu static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock) 974c8ff709SChao Yu { 984c8ff709SChao Yu int i; 994c8ff709SChao Yu 1004c8ff709SChao Yu for (i = 0; i < len; i++) { 1014c8ff709SChao Yu if (!cc->rpages[i]) 1024c8ff709SChao Yu continue; 1034c8ff709SChao Yu if (unlock) 1044c8ff709SChao Yu unlock_page(cc->rpages[i]); 1054c8ff709SChao Yu else 1064c8ff709SChao Yu put_page(cc->rpages[i]); 1074c8ff709SChao Yu } 1084c8ff709SChao Yu } 1094c8ff709SChao Yu 1104c8ff709SChao Yu static void f2fs_put_rpages(struct compress_ctx *cc) 1114c8ff709SChao Yu { 1124c8ff709SChao Yu f2fs_drop_rpages(cc, cc->cluster_size, false); 1134c8ff709SChao Yu } 1144c8ff709SChao Yu 1154c8ff709SChao Yu static void f2fs_unlock_rpages(struct compress_ctx *cc, int len) 1164c8ff709SChao Yu { 1174c8ff709SChao Yu f2fs_drop_rpages(cc, len, true); 1184c8ff709SChao Yu } 1194c8ff709SChao Yu 1204c8ff709SChao Yu static void f2fs_put_rpages_wbc(struct compress_ctx *cc, 1214c8ff709SChao Yu struct writeback_control *wbc, bool redirty, int unlock) 1224c8ff709SChao Yu { 1234c8ff709SChao Yu unsigned int i; 1244c8ff709SChao Yu 1254c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 1264c8ff709SChao Yu if (!cc->rpages[i]) 1274c8ff709SChao Yu continue; 1284c8ff709SChao Yu if (redirty) 1294c8ff709SChao Yu redirty_page_for_writepage(wbc, cc->rpages[i]); 1304c8ff709SChao Yu f2fs_put_page(cc->rpages[i], unlock); 1314c8ff709SChao Yu } 1324c8ff709SChao Yu } 1334c8ff709SChao Yu 1344c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page) 1354c8ff709SChao Yu { 1364c8ff709SChao Yu return ((struct compress_io_ctx *)page_private(page))->rpages[0]; 1374c8ff709SChao Yu } 1384c8ff709SChao Yu 1394c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc) 1404c8ff709SChao Yu { 141adfc6943SJaegeuk Kim if (cc->rpages) 1424c8ff709SChao Yu return 0; 1434c8ff709SChao Yu 14431083031SChao Yu cc->rpages = page_array_alloc(cc->inode, cc->cluster_size); 1454c8ff709SChao Yu return cc->rpages ? 0 : -ENOMEM; 1464c8ff709SChao Yu } 1474c8ff709SChao Yu 1488bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse) 1494c8ff709SChao Yu { 15031083031SChao Yu page_array_free(cc->inode, cc->rpages, cc->cluster_size); 1514c8ff709SChao Yu cc->rpages = NULL; 1524c8ff709SChao Yu cc->nr_rpages = 0; 1534c8ff709SChao Yu cc->nr_cpages = 0; 1548bfbfb0dSChao Yu if (!reuse) 1554c8ff709SChao Yu cc->cluster_idx = NULL_CLUSTER; 1564c8ff709SChao Yu } 1574c8ff709SChao Yu 1584c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page) 1594c8ff709SChao Yu { 1604c8ff709SChao Yu unsigned int cluster_ofs; 1614c8ff709SChao Yu 1624c8ff709SChao Yu if (!f2fs_cluster_can_merge_page(cc, page->index)) 1634c8ff709SChao Yu f2fs_bug_on(F2FS_I_SB(cc->inode), 1); 1644c8ff709SChao Yu 1654c8ff709SChao Yu cluster_ofs = offset_in_cluster(cc, page->index); 1664c8ff709SChao Yu cc->rpages[cluster_ofs] = page; 1674c8ff709SChao Yu cc->nr_rpages++; 1684c8ff709SChao Yu cc->cluster_idx = cluster_idx(cc, page->index); 1694c8ff709SChao Yu } 1704c8ff709SChao Yu 1714c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO 1724c8ff709SChao Yu static int lzo_init_compress_ctx(struct compress_ctx *cc) 1734c8ff709SChao Yu { 1744c8ff709SChao Yu cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), 1754c8ff709SChao Yu LZO1X_MEM_COMPRESS, GFP_NOFS); 1764c8ff709SChao Yu if (!cc->private) 1774c8ff709SChao Yu return -ENOMEM; 1784c8ff709SChao Yu 1794c8ff709SChao Yu cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size); 1804c8ff709SChao Yu return 0; 1814c8ff709SChao Yu } 1824c8ff709SChao Yu 1834c8ff709SChao Yu static void lzo_destroy_compress_ctx(struct compress_ctx *cc) 1844c8ff709SChao Yu { 1854c8ff709SChao Yu kvfree(cc->private); 1864c8ff709SChao Yu cc->private = NULL; 1874c8ff709SChao Yu } 1884c8ff709SChao Yu 1894c8ff709SChao Yu static int lzo_compress_pages(struct compress_ctx *cc) 1904c8ff709SChao Yu { 1914c8ff709SChao Yu int ret; 1924c8ff709SChao Yu 1934c8ff709SChao Yu ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata, 1944c8ff709SChao Yu &cc->clen, cc->private); 1954c8ff709SChao Yu if (ret != LZO_E_OK) { 1964c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n", 1974c8ff709SChao Yu KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret); 1984c8ff709SChao Yu return -EIO; 1994c8ff709SChao Yu } 2004c8ff709SChao Yu return 0; 2014c8ff709SChao Yu } 2024c8ff709SChao Yu 2034c8ff709SChao Yu static int lzo_decompress_pages(struct decompress_io_ctx *dic) 2044c8ff709SChao Yu { 2054c8ff709SChao Yu int ret; 2064c8ff709SChao Yu 2074c8ff709SChao Yu ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen, 2084c8ff709SChao Yu dic->rbuf, &dic->rlen); 2094c8ff709SChao Yu if (ret != LZO_E_OK) { 2104c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n", 2114c8ff709SChao Yu KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret); 2124c8ff709SChao Yu return -EIO; 2134c8ff709SChao Yu } 2144c8ff709SChao Yu 2154c8ff709SChao Yu if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) { 2164c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, " 2174c8ff709SChao Yu "expected:%lu\n", KERN_ERR, 2184c8ff709SChao Yu F2FS_I_SB(dic->inode)->sb->s_id, 2194c8ff709SChao Yu dic->rlen, 2204c8ff709SChao Yu PAGE_SIZE << dic->log_cluster_size); 2214c8ff709SChao Yu return -EIO; 2224c8ff709SChao Yu } 2234c8ff709SChao Yu return 0; 2244c8ff709SChao Yu } 2254c8ff709SChao Yu 2264c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lzo_ops = { 2274c8ff709SChao Yu .init_compress_ctx = lzo_init_compress_ctx, 2284c8ff709SChao Yu .destroy_compress_ctx = lzo_destroy_compress_ctx, 2294c8ff709SChao Yu .compress_pages = lzo_compress_pages, 2304c8ff709SChao Yu .decompress_pages = lzo_decompress_pages, 2314c8ff709SChao Yu }; 2324c8ff709SChao Yu #endif 2334c8ff709SChao Yu 2344c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4 2354c8ff709SChao Yu static int lz4_init_compress_ctx(struct compress_ctx *cc) 2364c8ff709SChao Yu { 2373fde13f8SChao Yu unsigned int size = LZ4_MEM_COMPRESS; 2383fde13f8SChao Yu 2393fde13f8SChao Yu #ifdef CONFIG_F2FS_FS_LZ4HC 2403fde13f8SChao Yu if (F2FS_I(cc->inode)->i_compress_flag >> COMPRESS_LEVEL_OFFSET) 2413fde13f8SChao Yu size = LZ4HC_MEM_COMPRESS; 2423fde13f8SChao Yu #endif 2433fde13f8SChao Yu 2443fde13f8SChao Yu cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS); 2454c8ff709SChao Yu if (!cc->private) 2464c8ff709SChao Yu return -ENOMEM; 2474c8ff709SChao Yu 248f6644143SChao Yu /* 249f6644143SChao Yu * we do not change cc->clen to LZ4_compressBound(inputsize) to 250f6644143SChao Yu * adapt worst compress case, because lz4 compressor can handle 251f6644143SChao Yu * output budget properly. 252f6644143SChao Yu */ 253f6644143SChao Yu cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE; 2544c8ff709SChao Yu return 0; 2554c8ff709SChao Yu } 2564c8ff709SChao Yu 2574c8ff709SChao Yu static void lz4_destroy_compress_ctx(struct compress_ctx *cc) 2584c8ff709SChao Yu { 2594c8ff709SChao Yu kvfree(cc->private); 2604c8ff709SChao Yu cc->private = NULL; 2614c8ff709SChao Yu } 2624c8ff709SChao Yu 2633fde13f8SChao Yu #ifdef CONFIG_F2FS_FS_LZ4HC 2643fde13f8SChao Yu static int lz4hc_compress_pages(struct compress_ctx *cc) 2653fde13f8SChao Yu { 2663fde13f8SChao Yu unsigned char level = F2FS_I(cc->inode)->i_compress_flag >> 2673fde13f8SChao Yu COMPRESS_LEVEL_OFFSET; 2683fde13f8SChao Yu int len; 2693fde13f8SChao Yu 2703fde13f8SChao Yu if (level) 2713fde13f8SChao Yu len = LZ4_compress_HC(cc->rbuf, cc->cbuf->cdata, cc->rlen, 2723fde13f8SChao Yu cc->clen, level, cc->private); 2733fde13f8SChao Yu else 2743fde13f8SChao Yu len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen, 2753fde13f8SChao Yu cc->clen, cc->private); 2763fde13f8SChao Yu if (!len) 2773fde13f8SChao Yu return -EAGAIN; 2783fde13f8SChao Yu 2793fde13f8SChao Yu cc->clen = len; 2803fde13f8SChao Yu return 0; 2813fde13f8SChao Yu } 2823fde13f8SChao Yu #endif 2833fde13f8SChao Yu 2844c8ff709SChao Yu static int lz4_compress_pages(struct compress_ctx *cc) 2854c8ff709SChao Yu { 2864c8ff709SChao Yu int len; 2874c8ff709SChao Yu 2883fde13f8SChao Yu #ifdef CONFIG_F2FS_FS_LZ4HC 2893fde13f8SChao Yu return lz4hc_compress_pages(cc); 2903fde13f8SChao Yu #endif 2914c8ff709SChao Yu len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen, 2924c8ff709SChao Yu cc->clen, cc->private); 293f6644143SChao Yu if (!len) 294f6644143SChao Yu return -EAGAIN; 295f6644143SChao Yu 2964c8ff709SChao Yu cc->clen = len; 2974c8ff709SChao Yu return 0; 2984c8ff709SChao Yu } 2994c8ff709SChao Yu 3004c8ff709SChao Yu static int lz4_decompress_pages(struct decompress_io_ctx *dic) 3014c8ff709SChao Yu { 3024c8ff709SChao Yu int ret; 3034c8ff709SChao Yu 3044c8ff709SChao Yu ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf, 3054c8ff709SChao Yu dic->clen, dic->rlen); 3064c8ff709SChao Yu if (ret < 0) { 3074c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n", 3084c8ff709SChao Yu KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret); 3094c8ff709SChao Yu return -EIO; 3104c8ff709SChao Yu } 3114c8ff709SChao Yu 3124c8ff709SChao Yu if (ret != PAGE_SIZE << dic->log_cluster_size) { 3134c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, " 3144c8ff709SChao Yu "expected:%lu\n", KERN_ERR, 3154c8ff709SChao Yu F2FS_I_SB(dic->inode)->sb->s_id, 3164c8ff709SChao Yu dic->rlen, 3174c8ff709SChao Yu PAGE_SIZE << dic->log_cluster_size); 3184c8ff709SChao Yu return -EIO; 3194c8ff709SChao Yu } 3204c8ff709SChao Yu return 0; 3214c8ff709SChao Yu } 3224c8ff709SChao Yu 3234c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lz4_ops = { 3244c8ff709SChao Yu .init_compress_ctx = lz4_init_compress_ctx, 3254c8ff709SChao Yu .destroy_compress_ctx = lz4_destroy_compress_ctx, 3264c8ff709SChao Yu .compress_pages = lz4_compress_pages, 3274c8ff709SChao Yu .decompress_pages = lz4_decompress_pages, 3284c8ff709SChao Yu }; 3294c8ff709SChao Yu #endif 3304c8ff709SChao Yu 33150cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD 33250cfa66fSChao Yu #define F2FS_ZSTD_DEFAULT_CLEVEL 1 33350cfa66fSChao Yu 33450cfa66fSChao Yu static int zstd_init_compress_ctx(struct compress_ctx *cc) 33550cfa66fSChao Yu { 33650cfa66fSChao Yu ZSTD_parameters params; 33750cfa66fSChao Yu ZSTD_CStream *stream; 33850cfa66fSChao Yu void *workspace; 33950cfa66fSChao Yu unsigned int workspace_size; 3403fde13f8SChao Yu unsigned char level = F2FS_I(cc->inode)->i_compress_flag >> 3413fde13f8SChao Yu COMPRESS_LEVEL_OFFSET; 34250cfa66fSChao Yu 3433fde13f8SChao Yu if (!level) 3443fde13f8SChao Yu level = F2FS_ZSTD_DEFAULT_CLEVEL; 3453fde13f8SChao Yu 3463fde13f8SChao Yu params = ZSTD_getParams(level, cc->rlen, 0); 34750cfa66fSChao Yu workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams); 34850cfa66fSChao Yu 34950cfa66fSChao Yu workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode), 35050cfa66fSChao Yu workspace_size, GFP_NOFS); 35150cfa66fSChao Yu if (!workspace) 35250cfa66fSChao Yu return -ENOMEM; 35350cfa66fSChao Yu 35450cfa66fSChao Yu stream = ZSTD_initCStream(params, 0, workspace, workspace_size); 35550cfa66fSChao Yu if (!stream) { 35650cfa66fSChao Yu printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n", 35750cfa66fSChao Yu KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, 35850cfa66fSChao Yu __func__); 35950cfa66fSChao Yu kvfree(workspace); 36050cfa66fSChao Yu return -EIO; 36150cfa66fSChao Yu } 36250cfa66fSChao Yu 36350cfa66fSChao Yu cc->private = workspace; 36450cfa66fSChao Yu cc->private2 = stream; 36550cfa66fSChao Yu 36650cfa66fSChao Yu cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE; 36750cfa66fSChao Yu return 0; 36850cfa66fSChao Yu } 36950cfa66fSChao Yu 37050cfa66fSChao Yu static void zstd_destroy_compress_ctx(struct compress_ctx *cc) 37150cfa66fSChao Yu { 37250cfa66fSChao Yu kvfree(cc->private); 37350cfa66fSChao Yu cc->private = NULL; 37450cfa66fSChao Yu cc->private2 = NULL; 37550cfa66fSChao Yu } 37650cfa66fSChao Yu 37750cfa66fSChao Yu static int zstd_compress_pages(struct compress_ctx *cc) 37850cfa66fSChao Yu { 37950cfa66fSChao Yu ZSTD_CStream *stream = cc->private2; 38050cfa66fSChao Yu ZSTD_inBuffer inbuf; 38150cfa66fSChao Yu ZSTD_outBuffer outbuf; 38250cfa66fSChao Yu int src_size = cc->rlen; 38350cfa66fSChao Yu int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE; 38450cfa66fSChao Yu int ret; 38550cfa66fSChao Yu 38650cfa66fSChao Yu inbuf.pos = 0; 38750cfa66fSChao Yu inbuf.src = cc->rbuf; 38850cfa66fSChao Yu inbuf.size = src_size; 38950cfa66fSChao Yu 39050cfa66fSChao Yu outbuf.pos = 0; 39150cfa66fSChao Yu outbuf.dst = cc->cbuf->cdata; 39250cfa66fSChao Yu outbuf.size = dst_size; 39350cfa66fSChao Yu 39450cfa66fSChao Yu ret = ZSTD_compressStream(stream, &outbuf, &inbuf); 39550cfa66fSChao Yu if (ZSTD_isError(ret)) { 39650cfa66fSChao Yu printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n", 39750cfa66fSChao Yu KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, 39850cfa66fSChao Yu __func__, ZSTD_getErrorCode(ret)); 39950cfa66fSChao Yu return -EIO; 40050cfa66fSChao Yu } 40150cfa66fSChao Yu 40250cfa66fSChao Yu ret = ZSTD_endStream(stream, &outbuf); 40350cfa66fSChao Yu if (ZSTD_isError(ret)) { 40450cfa66fSChao Yu printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n", 40550cfa66fSChao Yu KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, 40650cfa66fSChao Yu __func__, ZSTD_getErrorCode(ret)); 40750cfa66fSChao Yu return -EIO; 40850cfa66fSChao Yu } 40950cfa66fSChao Yu 4101454c978SChao Yu /* 4111454c978SChao Yu * there is compressed data remained in intermediate buffer due to 4121454c978SChao Yu * no more space in cbuf.cdata 4131454c978SChao Yu */ 4141454c978SChao Yu if (ret) 4151454c978SChao Yu return -EAGAIN; 4161454c978SChao Yu 41750cfa66fSChao Yu cc->clen = outbuf.pos; 41850cfa66fSChao Yu return 0; 41950cfa66fSChao Yu } 42050cfa66fSChao Yu 42150cfa66fSChao Yu static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic) 42250cfa66fSChao Yu { 42350cfa66fSChao Yu ZSTD_DStream *stream; 42450cfa66fSChao Yu void *workspace; 42550cfa66fSChao Yu unsigned int workspace_size; 4260e2b7385SChao Yu unsigned int max_window_size = 4270e2b7385SChao Yu MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size); 42850cfa66fSChao Yu 4290e2b7385SChao Yu workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size); 43050cfa66fSChao Yu 43150cfa66fSChao Yu workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode), 43250cfa66fSChao Yu workspace_size, GFP_NOFS); 43350cfa66fSChao Yu if (!workspace) 43450cfa66fSChao Yu return -ENOMEM; 43550cfa66fSChao Yu 4360e2b7385SChao Yu stream = ZSTD_initDStream(max_window_size, workspace, workspace_size); 43750cfa66fSChao Yu if (!stream) { 43850cfa66fSChao Yu printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n", 43950cfa66fSChao Yu KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, 44050cfa66fSChao Yu __func__); 44150cfa66fSChao Yu kvfree(workspace); 44250cfa66fSChao Yu return -EIO; 44350cfa66fSChao Yu } 44450cfa66fSChao Yu 44550cfa66fSChao Yu dic->private = workspace; 44650cfa66fSChao Yu dic->private2 = stream; 44750cfa66fSChao Yu 44850cfa66fSChao Yu return 0; 44950cfa66fSChao Yu } 45050cfa66fSChao Yu 45150cfa66fSChao Yu static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic) 45250cfa66fSChao Yu { 45350cfa66fSChao Yu kvfree(dic->private); 45450cfa66fSChao Yu dic->private = NULL; 45550cfa66fSChao Yu dic->private2 = NULL; 45650cfa66fSChao Yu } 45750cfa66fSChao Yu 45850cfa66fSChao Yu static int zstd_decompress_pages(struct decompress_io_ctx *dic) 45950cfa66fSChao Yu { 46050cfa66fSChao Yu ZSTD_DStream *stream = dic->private2; 46150cfa66fSChao Yu ZSTD_inBuffer inbuf; 46250cfa66fSChao Yu ZSTD_outBuffer outbuf; 46350cfa66fSChao Yu int ret; 46450cfa66fSChao Yu 46550cfa66fSChao Yu inbuf.pos = 0; 46650cfa66fSChao Yu inbuf.src = dic->cbuf->cdata; 46750cfa66fSChao Yu inbuf.size = dic->clen; 46850cfa66fSChao Yu 46950cfa66fSChao Yu outbuf.pos = 0; 47050cfa66fSChao Yu outbuf.dst = dic->rbuf; 47150cfa66fSChao Yu outbuf.size = dic->rlen; 47250cfa66fSChao Yu 47350cfa66fSChao Yu ret = ZSTD_decompressStream(stream, &outbuf, &inbuf); 47450cfa66fSChao Yu if (ZSTD_isError(ret)) { 47550cfa66fSChao Yu printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n", 47650cfa66fSChao Yu KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, 47750cfa66fSChao Yu __func__, ZSTD_getErrorCode(ret)); 47850cfa66fSChao Yu return -EIO; 47950cfa66fSChao Yu } 48050cfa66fSChao Yu 48150cfa66fSChao Yu if (dic->rlen != outbuf.pos) { 48250cfa66fSChao Yu printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, " 48350cfa66fSChao Yu "expected:%lu\n", KERN_ERR, 48450cfa66fSChao Yu F2FS_I_SB(dic->inode)->sb->s_id, 48550cfa66fSChao Yu __func__, dic->rlen, 48650cfa66fSChao Yu PAGE_SIZE << dic->log_cluster_size); 48750cfa66fSChao Yu return -EIO; 48850cfa66fSChao Yu } 48950cfa66fSChao Yu 49050cfa66fSChao Yu return 0; 49150cfa66fSChao Yu } 49250cfa66fSChao Yu 49350cfa66fSChao Yu static const struct f2fs_compress_ops f2fs_zstd_ops = { 49450cfa66fSChao Yu .init_compress_ctx = zstd_init_compress_ctx, 49550cfa66fSChao Yu .destroy_compress_ctx = zstd_destroy_compress_ctx, 49650cfa66fSChao Yu .compress_pages = zstd_compress_pages, 49750cfa66fSChao Yu .init_decompress_ctx = zstd_init_decompress_ctx, 49850cfa66fSChao Yu .destroy_decompress_ctx = zstd_destroy_decompress_ctx, 49950cfa66fSChao Yu .decompress_pages = zstd_decompress_pages, 50050cfa66fSChao Yu }; 50150cfa66fSChao Yu #endif 50250cfa66fSChao Yu 5036d92b201SChao Yu #ifdef CONFIG_F2FS_FS_LZO 5046d92b201SChao Yu #ifdef CONFIG_F2FS_FS_LZORLE 5056d92b201SChao Yu static int lzorle_compress_pages(struct compress_ctx *cc) 5066d92b201SChao Yu { 5076d92b201SChao Yu int ret; 5086d92b201SChao Yu 5096d92b201SChao Yu ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata, 5106d92b201SChao Yu &cc->clen, cc->private); 5116d92b201SChao Yu if (ret != LZO_E_OK) { 5126d92b201SChao Yu printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d\n", 5136d92b201SChao Yu KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret); 5146d92b201SChao Yu return -EIO; 5156d92b201SChao Yu } 5166d92b201SChao Yu return 0; 5176d92b201SChao Yu } 5186d92b201SChao Yu 5196d92b201SChao Yu static const struct f2fs_compress_ops f2fs_lzorle_ops = { 5206d92b201SChao Yu .init_compress_ctx = lzo_init_compress_ctx, 5216d92b201SChao Yu .destroy_compress_ctx = lzo_destroy_compress_ctx, 5226d92b201SChao Yu .compress_pages = lzorle_compress_pages, 5236d92b201SChao Yu .decompress_pages = lzo_decompress_pages, 5246d92b201SChao Yu }; 5256d92b201SChao Yu #endif 5266d92b201SChao Yu #endif 5276d92b201SChao Yu 5284c8ff709SChao Yu static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = { 5294c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO 5304c8ff709SChao Yu &f2fs_lzo_ops, 5314c8ff709SChao Yu #else 5324c8ff709SChao Yu NULL, 5334c8ff709SChao Yu #endif 5344c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4 5354c8ff709SChao Yu &f2fs_lz4_ops, 5364c8ff709SChao Yu #else 5374c8ff709SChao Yu NULL, 5384c8ff709SChao Yu #endif 53950cfa66fSChao Yu #ifdef CONFIG_F2FS_FS_ZSTD 54050cfa66fSChao Yu &f2fs_zstd_ops, 54150cfa66fSChao Yu #else 54250cfa66fSChao Yu NULL, 54350cfa66fSChao Yu #endif 5446d92b201SChao Yu #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE) 5456d92b201SChao Yu &f2fs_lzorle_ops, 5466d92b201SChao Yu #else 5476d92b201SChao Yu NULL, 5486d92b201SChao Yu #endif 5494c8ff709SChao Yu }; 5504c8ff709SChao Yu 5514c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode) 5524c8ff709SChao Yu { 5534c8ff709SChao Yu if (!f2fs_compressed_file(inode)) 5544c8ff709SChao Yu return true; 5554c8ff709SChao Yu return f2fs_cops[F2FS_I(inode)->i_compress_algorithm]; 5564c8ff709SChao Yu } 5574c8ff709SChao Yu 55899bbe307SJaegeuk Kim static mempool_t *compress_page_pool; 5595e6bbde9SChao Yu static int num_compress_pages = 512; 5605e6bbde9SChao Yu module_param(num_compress_pages, uint, 0444); 5615e6bbde9SChao Yu MODULE_PARM_DESC(num_compress_pages, 5625e6bbde9SChao Yu "Number of intermediate compress pages to preallocate"); 5635e6bbde9SChao Yu 5645e6bbde9SChao Yu int f2fs_init_compress_mempool(void) 5655e6bbde9SChao Yu { 5665e6bbde9SChao Yu compress_page_pool = mempool_create_page_pool(num_compress_pages, 0); 5675e6bbde9SChao Yu if (!compress_page_pool) 5685e6bbde9SChao Yu return -ENOMEM; 5695e6bbde9SChao Yu 5705e6bbde9SChao Yu return 0; 5715e6bbde9SChao Yu } 5725e6bbde9SChao Yu 5735e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void) 5745e6bbde9SChao Yu { 5755e6bbde9SChao Yu mempool_destroy(compress_page_pool); 5765e6bbde9SChao Yu } 5775e6bbde9SChao Yu 5785e6bbde9SChao Yu static struct page *f2fs_compress_alloc_page(void) 5794c8ff709SChao Yu { 5804c8ff709SChao Yu struct page *page; 5814c8ff709SChao Yu 5825e6bbde9SChao Yu page = mempool_alloc(compress_page_pool, GFP_NOFS); 5834c8ff709SChao Yu lock_page(page); 5845e6bbde9SChao Yu 5854c8ff709SChao Yu return page; 5864c8ff709SChao Yu } 5874c8ff709SChao Yu 5885e6bbde9SChao Yu static void f2fs_compress_free_page(struct page *page) 5895e6bbde9SChao Yu { 5905e6bbde9SChao Yu if (!page) 5915e6bbde9SChao Yu return; 5925e6bbde9SChao Yu set_page_private(page, (unsigned long)NULL); 5935e6bbde9SChao Yu ClearPagePrivate(page); 5945e6bbde9SChao Yu page->mapping = NULL; 5955e6bbde9SChao Yu unlock_page(page); 5965e6bbde9SChao Yu mempool_free(page, compress_page_pool); 5975e6bbde9SChao Yu } 5985e6bbde9SChao Yu 5996fcaebacSDaeho Jeong #define MAX_VMAP_RETRIES 3 6006fcaebacSDaeho Jeong 6016fcaebacSDaeho Jeong static void *f2fs_vmap(struct page **pages, unsigned int count) 6026fcaebacSDaeho Jeong { 6036fcaebacSDaeho Jeong int i; 6046fcaebacSDaeho Jeong void *buf = NULL; 6056fcaebacSDaeho Jeong 6066fcaebacSDaeho Jeong for (i = 0; i < MAX_VMAP_RETRIES; i++) { 6076fcaebacSDaeho Jeong buf = vm_map_ram(pages, count, -1); 6086fcaebacSDaeho Jeong if (buf) 6096fcaebacSDaeho Jeong break; 6106fcaebacSDaeho Jeong vm_unmap_aliases(); 6116fcaebacSDaeho Jeong } 6126fcaebacSDaeho Jeong return buf; 6136fcaebacSDaeho Jeong } 6146fcaebacSDaeho Jeong 6154c8ff709SChao Yu static int f2fs_compress_pages(struct compress_ctx *cc) 6164c8ff709SChao Yu { 6174c8ff709SChao Yu struct f2fs_inode_info *fi = F2FS_I(cc->inode); 6184c8ff709SChao Yu const struct f2fs_compress_ops *cops = 6194c8ff709SChao Yu f2fs_cops[fi->i_compress_algorithm]; 62031083031SChao Yu unsigned int max_len, new_nr_cpages; 62131083031SChao Yu struct page **new_cpages; 622b28f047bSChao Yu u32 chksum = 0; 6234c8ff709SChao Yu int i, ret; 6244c8ff709SChao Yu 6254c8ff709SChao Yu trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx, 6264c8ff709SChao Yu cc->cluster_size, fi->i_compress_algorithm); 6274c8ff709SChao Yu 62823b1faaaSChao Yu if (cops->init_compress_ctx) { 6294c8ff709SChao Yu ret = cops->init_compress_ctx(cc); 6304c8ff709SChao Yu if (ret) 6314c8ff709SChao Yu goto out; 63223b1faaaSChao Yu } 6334c8ff709SChao Yu 6344c8ff709SChao Yu max_len = COMPRESS_HEADER_SIZE + cc->clen; 6354c8ff709SChao Yu cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE); 6364c8ff709SChao Yu 63731083031SChao Yu cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages); 6384c8ff709SChao Yu if (!cc->cpages) { 6394c8ff709SChao Yu ret = -ENOMEM; 6404c8ff709SChao Yu goto destroy_compress_ctx; 6414c8ff709SChao Yu } 6424c8ff709SChao Yu 6434c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 6445e6bbde9SChao Yu cc->cpages[i] = f2fs_compress_alloc_page(); 6454c8ff709SChao Yu if (!cc->cpages[i]) { 6464c8ff709SChao Yu ret = -ENOMEM; 6474c8ff709SChao Yu goto out_free_cpages; 6484c8ff709SChao Yu } 6494c8ff709SChao Yu } 6504c8ff709SChao Yu 6516fcaebacSDaeho Jeong cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size); 6524c8ff709SChao Yu if (!cc->rbuf) { 6534c8ff709SChao Yu ret = -ENOMEM; 6544c8ff709SChao Yu goto out_free_cpages; 6554c8ff709SChao Yu } 6564c8ff709SChao Yu 6576fcaebacSDaeho Jeong cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages); 6584c8ff709SChao Yu if (!cc->cbuf) { 6594c8ff709SChao Yu ret = -ENOMEM; 6604c8ff709SChao Yu goto out_vunmap_rbuf; 6614c8ff709SChao Yu } 6624c8ff709SChao Yu 6634c8ff709SChao Yu ret = cops->compress_pages(cc); 6644c8ff709SChao Yu if (ret) 6654c8ff709SChao Yu goto out_vunmap_cbuf; 6664c8ff709SChao Yu 6674c8ff709SChao Yu max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE; 6684c8ff709SChao Yu 6694c8ff709SChao Yu if (cc->clen > max_len) { 6704c8ff709SChao Yu ret = -EAGAIN; 6714c8ff709SChao Yu goto out_vunmap_cbuf; 6724c8ff709SChao Yu } 6734c8ff709SChao Yu 6744c8ff709SChao Yu cc->cbuf->clen = cpu_to_le32(cc->clen); 6754c8ff709SChao Yu 676b28f047bSChao Yu if (fi->i_compress_flag & 1 << COMPRESS_CHKSUM) 677b28f047bSChao Yu chksum = f2fs_crc32(F2FS_I_SB(cc->inode), 678b28f047bSChao Yu cc->cbuf->cdata, cc->clen); 679b28f047bSChao Yu cc->cbuf->chksum = cpu_to_le32(chksum); 680b28f047bSChao Yu 6814c8ff709SChao Yu for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++) 6824c8ff709SChao Yu cc->cbuf->reserved[i] = cpu_to_le32(0); 6834c8ff709SChao Yu 68431083031SChao Yu new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE); 68531083031SChao Yu 68631083031SChao Yu /* Now we're going to cut unnecessary tail pages */ 68731083031SChao Yu new_cpages = page_array_alloc(cc->inode, new_nr_cpages); 68831083031SChao Yu if (!new_cpages) { 68931083031SChao Yu ret = -ENOMEM; 69031083031SChao Yu goto out_vunmap_cbuf; 69131083031SChao Yu } 6927fa6d598SEric Biggers 6937fa6d598SEric Biggers /* zero out any unused part of the last page */ 6947fa6d598SEric Biggers memset(&cc->cbuf->cdata[cc->clen], 0, 69531083031SChao Yu (new_nr_cpages * PAGE_SIZE) - 69631083031SChao Yu (cc->clen + COMPRESS_HEADER_SIZE)); 6977fa6d598SEric Biggers 6986fcaebacSDaeho Jeong vm_unmap_ram(cc->cbuf, cc->nr_cpages); 6996fcaebacSDaeho Jeong vm_unmap_ram(cc->rbuf, cc->cluster_size); 7004c8ff709SChao Yu 70131083031SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 70231083031SChao Yu if (i < new_nr_cpages) { 70331083031SChao Yu new_cpages[i] = cc->cpages[i]; 70431083031SChao Yu continue; 70531083031SChao Yu } 7065e6bbde9SChao Yu f2fs_compress_free_page(cc->cpages[i]); 7074c8ff709SChao Yu cc->cpages[i] = NULL; 7084c8ff709SChao Yu } 7094c8ff709SChao Yu 71023b1faaaSChao Yu if (cops->destroy_compress_ctx) 71109ff4801SChao Yu cops->destroy_compress_ctx(cc); 71209ff4801SChao Yu 71331083031SChao Yu page_array_free(cc->inode, cc->cpages, cc->nr_cpages); 71431083031SChao Yu cc->cpages = new_cpages; 71531083031SChao Yu cc->nr_cpages = new_nr_cpages; 7164c8ff709SChao Yu 7174c8ff709SChao Yu trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx, 7184c8ff709SChao Yu cc->clen, ret); 7194c8ff709SChao Yu return 0; 7204c8ff709SChao Yu 7214c8ff709SChao Yu out_vunmap_cbuf: 7226fcaebacSDaeho Jeong vm_unmap_ram(cc->cbuf, cc->nr_cpages); 7234c8ff709SChao Yu out_vunmap_rbuf: 7246fcaebacSDaeho Jeong vm_unmap_ram(cc->rbuf, cc->cluster_size); 7254c8ff709SChao Yu out_free_cpages: 7264c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 7274c8ff709SChao Yu if (cc->cpages[i]) 7285e6bbde9SChao Yu f2fs_compress_free_page(cc->cpages[i]); 7294c8ff709SChao Yu } 73031083031SChao Yu page_array_free(cc->inode, cc->cpages, cc->nr_cpages); 7314c8ff709SChao Yu cc->cpages = NULL; 7324c8ff709SChao Yu destroy_compress_ctx: 73323b1faaaSChao Yu if (cops->destroy_compress_ctx) 7344c8ff709SChao Yu cops->destroy_compress_ctx(cc); 7354c8ff709SChao Yu out: 7364c8ff709SChao Yu trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx, 7374c8ff709SChao Yu cc->clen, ret); 7384c8ff709SChao Yu return ret; 7394c8ff709SChao Yu } 7404c8ff709SChao Yu 7417f59b277SEric Biggers static void f2fs_decompress_cluster(struct decompress_io_ctx *dic) 7424c8ff709SChao Yu { 7434c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode); 7444c8ff709SChao Yu struct f2fs_inode_info *fi = F2FS_I(dic->inode); 7454c8ff709SChao Yu const struct f2fs_compress_ops *cops = 7464c8ff709SChao Yu f2fs_cops[fi->i_compress_algorithm]; 7474c8ff709SChao Yu int ret; 748b2f57a8eSChao Yu int i; 7494c8ff709SChao Yu 7504c8ff709SChao Yu trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx, 7514c8ff709SChao Yu dic->cluster_size, fi->i_compress_algorithm); 7524c8ff709SChao Yu 7534c8ff709SChao Yu if (dic->failed) { 7544c8ff709SChao Yu ret = -EIO; 7557f59b277SEric Biggers goto out_end_io; 7564c8ff709SChao Yu } 7574c8ff709SChao Yu 75831083031SChao Yu dic->tpages = page_array_alloc(dic->inode, dic->cluster_size); 759b2f57a8eSChao Yu if (!dic->tpages) { 760b2f57a8eSChao Yu ret = -ENOMEM; 7617f59b277SEric Biggers goto out_end_io; 762b2f57a8eSChao Yu } 763b2f57a8eSChao Yu 764b2f57a8eSChao Yu for (i = 0; i < dic->cluster_size; i++) { 765b2f57a8eSChao Yu if (dic->rpages[i]) { 766b2f57a8eSChao Yu dic->tpages[i] = dic->rpages[i]; 767b2f57a8eSChao Yu continue; 768b2f57a8eSChao Yu } 769b2f57a8eSChao Yu 770b2f57a8eSChao Yu dic->tpages[i] = f2fs_compress_alloc_page(); 771b2f57a8eSChao Yu if (!dic->tpages[i]) { 772b2f57a8eSChao Yu ret = -ENOMEM; 7737f59b277SEric Biggers goto out_end_io; 774b2f57a8eSChao Yu } 775b2f57a8eSChao Yu } 776b2f57a8eSChao Yu 77723b1faaaSChao Yu if (cops->init_decompress_ctx) { 77823b1faaaSChao Yu ret = cops->init_decompress_ctx(dic); 77923b1faaaSChao Yu if (ret) 7807f59b277SEric Biggers goto out_end_io; 78123b1faaaSChao Yu } 78223b1faaaSChao Yu 7836fcaebacSDaeho Jeong dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size); 7844c8ff709SChao Yu if (!dic->rbuf) { 7854c8ff709SChao Yu ret = -ENOMEM; 7867f59b277SEric Biggers goto out_destroy_decompress_ctx; 7874c8ff709SChao Yu } 7884c8ff709SChao Yu 7896fcaebacSDaeho Jeong dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages); 7904c8ff709SChao Yu if (!dic->cbuf) { 7914c8ff709SChao Yu ret = -ENOMEM; 7924c8ff709SChao Yu goto out_vunmap_rbuf; 7934c8ff709SChao Yu } 7944c8ff709SChao Yu 7954c8ff709SChao Yu dic->clen = le32_to_cpu(dic->cbuf->clen); 7964c8ff709SChao Yu dic->rlen = PAGE_SIZE << dic->log_cluster_size; 7974c8ff709SChao Yu 7984c8ff709SChao Yu if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) { 7994c8ff709SChao Yu ret = -EFSCORRUPTED; 8004c8ff709SChao Yu goto out_vunmap_cbuf; 8014c8ff709SChao Yu } 8024c8ff709SChao Yu 8034c8ff709SChao Yu ret = cops->decompress_pages(dic); 8044c8ff709SChao Yu 80575e91c88SChao Yu if (!ret && (fi->i_compress_flag & 1 << COMPRESS_CHKSUM)) { 806b28f047bSChao Yu u32 provided = le32_to_cpu(dic->cbuf->chksum); 807b28f047bSChao Yu u32 calculated = f2fs_crc32(sbi, dic->cbuf->cdata, dic->clen); 808b28f047bSChao Yu 809b28f047bSChao Yu if (provided != calculated) { 810b28f047bSChao Yu if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) { 811b28f047bSChao Yu set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT); 812b28f047bSChao Yu printk_ratelimited( 813b28f047bSChao Yu "%sF2FS-fs (%s): checksum invalid, nid = %lu, %x vs %x", 814b28f047bSChao Yu KERN_INFO, sbi->sb->s_id, dic->inode->i_ino, 815b28f047bSChao Yu provided, calculated); 816b28f047bSChao Yu } 817b28f047bSChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 818b28f047bSChao Yu } 819b28f047bSChao Yu } 820b28f047bSChao Yu 8214c8ff709SChao Yu out_vunmap_cbuf: 8226fcaebacSDaeho Jeong vm_unmap_ram(dic->cbuf, dic->nr_cpages); 8234c8ff709SChao Yu out_vunmap_rbuf: 8246fcaebacSDaeho Jeong vm_unmap_ram(dic->rbuf, dic->cluster_size); 8257f59b277SEric Biggers out_destroy_decompress_ctx: 82623b1faaaSChao Yu if (cops->destroy_decompress_ctx) 82723b1faaaSChao Yu cops->destroy_decompress_ctx(dic); 8287f59b277SEric Biggers out_end_io: 8294c8ff709SChao Yu trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx, 8304c8ff709SChao Yu dic->clen, ret); 8317f59b277SEric Biggers f2fs_decompress_end_io(dic, ret); 8327f59b277SEric Biggers } 8337f59b277SEric Biggers 8347f59b277SEric Biggers /* 8357f59b277SEric Biggers * This is called when a page of a compressed cluster has been read from disk 8367f59b277SEric Biggers * (or failed to be read from disk). It checks whether this page was the last 8377f59b277SEric Biggers * page being waited on in the cluster, and if so, it decompresses the cluster 8387f59b277SEric Biggers * (or in the case of a failure, cleans up without actually decompressing). 8397f59b277SEric Biggers */ 8407f59b277SEric Biggers void f2fs_end_read_compressed_page(struct page *page, bool failed) 8417f59b277SEric Biggers { 8427f59b277SEric Biggers struct decompress_io_ctx *dic = 8437f59b277SEric Biggers (struct decompress_io_ctx *)page_private(page); 8447f59b277SEric Biggers struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode); 8457f59b277SEric Biggers 8467f59b277SEric Biggers dec_page_count(sbi, F2FS_RD_DATA); 8477f59b277SEric Biggers 8487f59b277SEric Biggers if (failed) 8497f59b277SEric Biggers WRITE_ONCE(dic->failed, true); 8507f59b277SEric Biggers 8517f59b277SEric Biggers if (atomic_dec_and_test(&dic->remaining_pages)) 8527f59b277SEric Biggers f2fs_decompress_cluster(dic); 8534c8ff709SChao Yu } 8544c8ff709SChao Yu 8554c8ff709SChao Yu static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index) 8564c8ff709SChao Yu { 8574c8ff709SChao Yu if (cc->cluster_idx == NULL_CLUSTER) 8584c8ff709SChao Yu return true; 8594c8ff709SChao Yu return cc->cluster_idx == cluster_idx(cc, index); 8604c8ff709SChao Yu } 8614c8ff709SChao Yu 8624c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc) 8634c8ff709SChao Yu { 8644c8ff709SChao Yu return cc->nr_rpages == 0; 8654c8ff709SChao Yu } 8664c8ff709SChao Yu 8674c8ff709SChao Yu static bool f2fs_cluster_is_full(struct compress_ctx *cc) 8684c8ff709SChao Yu { 8694c8ff709SChao Yu return cc->cluster_size == cc->nr_rpages; 8704c8ff709SChao Yu } 8714c8ff709SChao Yu 8724c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index) 8734c8ff709SChao Yu { 8744c8ff709SChao Yu if (f2fs_cluster_is_empty(cc)) 8754c8ff709SChao Yu return true; 8764c8ff709SChao Yu return is_page_in_cluster(cc, index); 8774c8ff709SChao Yu } 8784c8ff709SChao Yu 879*5db479f0SChao Yu static bool cluster_has_invalid_data(struct compress_ctx *cc) 8804c8ff709SChao Yu { 8814c8ff709SChao Yu loff_t i_size = i_size_read(cc->inode); 8824c8ff709SChao Yu unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE); 8834c8ff709SChao Yu int i; 8844c8ff709SChao Yu 8854c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 8864c8ff709SChao Yu struct page *page = cc->rpages[i]; 8874c8ff709SChao Yu 8888af85f71SChao Yu f2fs_bug_on(F2FS_I_SB(cc->inode), !page); 8894c8ff709SChao Yu 8904c8ff709SChao Yu /* beyond EOF */ 8914c8ff709SChao Yu if (page->index >= nr_pages) 8924c8ff709SChao Yu return true; 8934c8ff709SChao Yu } 894*5db479f0SChao Yu return false; 895*5db479f0SChao Yu } 8964c8ff709SChao Yu 8971a67cbe1SChao Yu static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr) 8984c8ff709SChao Yu { 8994c8ff709SChao Yu struct dnode_of_data dn; 9004c8ff709SChao Yu int ret; 9014c8ff709SChao Yu 9024c8ff709SChao Yu set_new_dnode(&dn, cc->inode, NULL, NULL, 0); 9034c8ff709SChao Yu ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc), 9044c8ff709SChao Yu LOOKUP_NODE); 9054c8ff709SChao Yu if (ret) { 9064c8ff709SChao Yu if (ret == -ENOENT) 9074c8ff709SChao Yu ret = 0; 9084c8ff709SChao Yu goto fail; 9094c8ff709SChao Yu } 9104c8ff709SChao Yu 9114c8ff709SChao Yu if (dn.data_blkaddr == COMPRESS_ADDR) { 9124c8ff709SChao Yu int i; 9134c8ff709SChao Yu 9144c8ff709SChao Yu ret = 1; 9154c8ff709SChao Yu for (i = 1; i < cc->cluster_size; i++) { 9164c8ff709SChao Yu block_t blkaddr; 9174c8ff709SChao Yu 918a2ced1ceSChao Yu blkaddr = data_blkaddr(dn.inode, 9194c8ff709SChao Yu dn.node_page, dn.ofs_in_node + i); 9201a67cbe1SChao Yu if (compr) { 9211a67cbe1SChao Yu if (__is_valid_data_blkaddr(blkaddr)) 9221a67cbe1SChao Yu ret++; 9231a67cbe1SChao Yu } else { 9244c8ff709SChao Yu if (blkaddr != NULL_ADDR) 9254c8ff709SChao Yu ret++; 9264c8ff709SChao Yu } 9274c8ff709SChao Yu } 9281a67cbe1SChao Yu } 9294c8ff709SChao Yu fail: 9304c8ff709SChao Yu f2fs_put_dnode(&dn); 9314c8ff709SChao Yu return ret; 9324c8ff709SChao Yu } 9334c8ff709SChao Yu 9341a67cbe1SChao Yu /* return # of compressed blocks in compressed cluster */ 9351a67cbe1SChao Yu static int f2fs_compressed_blocks(struct compress_ctx *cc) 9361a67cbe1SChao Yu { 9371a67cbe1SChao Yu return __f2fs_cluster_blocks(cc, true); 9381a67cbe1SChao Yu } 9391a67cbe1SChao Yu 9401a67cbe1SChao Yu /* return # of valid blocks in compressed cluster */ 941d078319dSWang Xiaojun static int f2fs_cluster_blocks(struct compress_ctx *cc) 9421a67cbe1SChao Yu { 9431a67cbe1SChao Yu return __f2fs_cluster_blocks(cc, false); 9441a67cbe1SChao Yu } 9451a67cbe1SChao Yu 9464c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index) 9474c8ff709SChao Yu { 9484c8ff709SChao Yu struct compress_ctx cc = { 9494c8ff709SChao Yu .inode = inode, 9504c8ff709SChao Yu .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 9514c8ff709SChao Yu .cluster_size = F2FS_I(inode)->i_cluster_size, 9524c8ff709SChao Yu .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size, 9534c8ff709SChao Yu }; 9544c8ff709SChao Yu 955d078319dSWang Xiaojun return f2fs_cluster_blocks(&cc); 9564c8ff709SChao Yu } 9574c8ff709SChao Yu 9584c8ff709SChao Yu static bool cluster_may_compress(struct compress_ctx *cc) 9594c8ff709SChao Yu { 960602a16d5SDaeho Jeong if (!f2fs_need_compress_data(cc->inode)) 9614c8ff709SChao Yu return false; 9624c8ff709SChao Yu if (f2fs_is_atomic_file(cc->inode)) 9634c8ff709SChao Yu return false; 9644c8ff709SChao Yu if (f2fs_is_mmap_file(cc->inode)) 9654c8ff709SChao Yu return false; 9664c8ff709SChao Yu if (!f2fs_cluster_is_full(cc)) 9674c8ff709SChao Yu return false; 968dc35d73aSChao Yu if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode)))) 969dc35d73aSChao Yu return false; 970*5db479f0SChao Yu return !cluster_has_invalid_data(cc); 9714c8ff709SChao Yu } 9724c8ff709SChao Yu 9734c8ff709SChao Yu static void set_cluster_writeback(struct compress_ctx *cc) 9744c8ff709SChao Yu { 9754c8ff709SChao Yu int i; 9764c8ff709SChao Yu 9774c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 9784c8ff709SChao Yu if (cc->rpages[i]) 9794c8ff709SChao Yu set_page_writeback(cc->rpages[i]); 9804c8ff709SChao Yu } 9814c8ff709SChao Yu } 9824c8ff709SChao Yu 9834c8ff709SChao Yu static void set_cluster_dirty(struct compress_ctx *cc) 9844c8ff709SChao Yu { 9854c8ff709SChao Yu int i; 9864c8ff709SChao Yu 9874c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) 9884c8ff709SChao Yu if (cc->rpages[i]) 9894c8ff709SChao Yu set_page_dirty(cc->rpages[i]); 9904c8ff709SChao Yu } 9914c8ff709SChao Yu 9924c8ff709SChao Yu static int prepare_compress_overwrite(struct compress_ctx *cc, 9934c8ff709SChao Yu struct page **pagep, pgoff_t index, void **fsdata) 9944c8ff709SChao Yu { 9954c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 9964c8ff709SChao Yu struct address_space *mapping = cc->inode->i_mapping; 9974c8ff709SChao Yu struct page *page; 9984c8ff709SChao Yu struct dnode_of_data dn; 9994c8ff709SChao Yu sector_t last_block_in_bio; 10004c8ff709SChao Yu unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT; 10014c8ff709SChao Yu pgoff_t start_idx = start_idx_of_cluster(cc); 10024c8ff709SChao Yu int i, ret; 10034c8ff709SChao Yu bool prealloc; 10044c8ff709SChao Yu 10054c8ff709SChao Yu retry: 1006d078319dSWang Xiaojun ret = f2fs_cluster_blocks(cc); 10074c8ff709SChao Yu if (ret <= 0) 10084c8ff709SChao Yu return ret; 10094c8ff709SChao Yu 10104c8ff709SChao Yu /* compressed case */ 10114c8ff709SChao Yu prealloc = (ret < cc->cluster_size); 10124c8ff709SChao Yu 10134c8ff709SChao Yu ret = f2fs_init_compress_ctx(cc); 10144c8ff709SChao Yu if (ret) 10154c8ff709SChao Yu return ret; 10164c8ff709SChao Yu 10174c8ff709SChao Yu /* keep page reference to avoid page reclaim */ 10184c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 10194c8ff709SChao Yu page = f2fs_pagecache_get_page(mapping, start_idx + i, 10204c8ff709SChao Yu fgp_flag, GFP_NOFS); 10214c8ff709SChao Yu if (!page) { 10224c8ff709SChao Yu ret = -ENOMEM; 10234c8ff709SChao Yu goto unlock_pages; 10244c8ff709SChao Yu } 10254c8ff709SChao Yu 10264c8ff709SChao Yu if (PageUptodate(page)) 1027a949dc5fSChao Yu f2fs_put_page(page, 1); 10284c8ff709SChao Yu else 10294c8ff709SChao Yu f2fs_compress_ctx_add_page(cc, page); 10304c8ff709SChao Yu } 10314c8ff709SChao Yu 10324c8ff709SChao Yu if (!f2fs_cluster_is_empty(cc)) { 10334c8ff709SChao Yu struct bio *bio = NULL; 10344c8ff709SChao Yu 10354c8ff709SChao Yu ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size, 10360683728aSChao Yu &last_block_in_bio, false, true); 1037a949dc5fSChao Yu f2fs_put_rpages(cc); 10388bfbfb0dSChao Yu f2fs_destroy_compress_ctx(cc, true); 10394c8ff709SChao Yu if (ret) 1040a949dc5fSChao Yu goto out; 10414c8ff709SChao Yu if (bio) 10424c8ff709SChao Yu f2fs_submit_bio(sbi, bio, DATA); 10434c8ff709SChao Yu 10444c8ff709SChao Yu ret = f2fs_init_compress_ctx(cc); 10454c8ff709SChao Yu if (ret) 1046a949dc5fSChao Yu goto out; 10474c8ff709SChao Yu } 10484c8ff709SChao Yu 10494c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 10504c8ff709SChao Yu f2fs_bug_on(sbi, cc->rpages[i]); 10514c8ff709SChao Yu 10524c8ff709SChao Yu page = find_lock_page(mapping, start_idx + i); 1053a949dc5fSChao Yu if (!page) { 1054a949dc5fSChao Yu /* page can be truncated */ 1055a949dc5fSChao Yu goto release_and_retry; 1056a949dc5fSChao Yu } 10574c8ff709SChao Yu 10584c8ff709SChao Yu f2fs_wait_on_page_writeback(page, DATA, true, true); 10594c8ff709SChao Yu f2fs_compress_ctx_add_page(cc, page); 10604c8ff709SChao Yu 10614c8ff709SChao Yu if (!PageUptodate(page)) { 1062a949dc5fSChao Yu release_and_retry: 1063a949dc5fSChao Yu f2fs_put_rpages(cc); 10644c8ff709SChao Yu f2fs_unlock_rpages(cc, i + 1); 10658bfbfb0dSChao Yu f2fs_destroy_compress_ctx(cc, true); 10664c8ff709SChao Yu goto retry; 10674c8ff709SChao Yu } 10684c8ff709SChao Yu } 10694c8ff709SChao Yu 10704c8ff709SChao Yu if (prealloc) { 10710ef81833SChao Yu f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true); 10724c8ff709SChao Yu 10734c8ff709SChao Yu set_new_dnode(&dn, cc->inode, NULL, NULL, 0); 10744c8ff709SChao Yu 10754c8ff709SChao Yu for (i = cc->cluster_size - 1; i > 0; i--) { 10764c8ff709SChao Yu ret = f2fs_get_block(&dn, start_idx + i); 10774c8ff709SChao Yu if (ret) { 10784c8ff709SChao Yu i = cc->cluster_size; 10794c8ff709SChao Yu break; 10804c8ff709SChao Yu } 10814c8ff709SChao Yu 10824c8ff709SChao Yu if (dn.data_blkaddr != NEW_ADDR) 10834c8ff709SChao Yu break; 10844c8ff709SChao Yu } 10854c8ff709SChao Yu 10860ef81833SChao Yu f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false); 10874c8ff709SChao Yu } 10884c8ff709SChao Yu 10894c8ff709SChao Yu if (likely(!ret)) { 10904c8ff709SChao Yu *fsdata = cc->rpages; 10914c8ff709SChao Yu *pagep = cc->rpages[offset_in_cluster(cc, index)]; 10924c8ff709SChao Yu return cc->cluster_size; 10934c8ff709SChao Yu } 10944c8ff709SChao Yu 10954c8ff709SChao Yu unlock_pages: 1096a949dc5fSChao Yu f2fs_put_rpages(cc); 10974c8ff709SChao Yu f2fs_unlock_rpages(cc, i); 10988bfbfb0dSChao Yu f2fs_destroy_compress_ctx(cc, true); 1099a949dc5fSChao Yu out: 11004c8ff709SChao Yu return ret; 11014c8ff709SChao Yu } 11024c8ff709SChao Yu 11034c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode, 11044c8ff709SChao Yu struct page **pagep, pgoff_t index, void **fsdata) 11054c8ff709SChao Yu { 11064c8ff709SChao Yu struct compress_ctx cc = { 11074c8ff709SChao Yu .inode = inode, 11084c8ff709SChao Yu .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 11094c8ff709SChao Yu .cluster_size = F2FS_I(inode)->i_cluster_size, 11104c8ff709SChao Yu .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size, 11114c8ff709SChao Yu .rpages = NULL, 11124c8ff709SChao Yu .nr_rpages = 0, 11134c8ff709SChao Yu }; 11144c8ff709SChao Yu 11154c8ff709SChao Yu return prepare_compress_overwrite(&cc, pagep, index, fsdata); 11164c8ff709SChao Yu } 11174c8ff709SChao Yu 11184c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata, 11194c8ff709SChao Yu pgoff_t index, unsigned copied) 11204c8ff709SChao Yu 11214c8ff709SChao Yu { 11224c8ff709SChao Yu struct compress_ctx cc = { 112331083031SChao Yu .inode = inode, 11244c8ff709SChao Yu .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 11254c8ff709SChao Yu .cluster_size = F2FS_I(inode)->i_cluster_size, 11264c8ff709SChao Yu .rpages = fsdata, 11274c8ff709SChao Yu }; 11284c8ff709SChao Yu bool first_index = (index == cc.rpages[0]->index); 11294c8ff709SChao Yu 11304c8ff709SChao Yu if (copied) 11314c8ff709SChao Yu set_cluster_dirty(&cc); 11324c8ff709SChao Yu 11334c8ff709SChao Yu f2fs_put_rpages_wbc(&cc, NULL, false, 1); 11348bfbfb0dSChao Yu f2fs_destroy_compress_ctx(&cc, false); 11354c8ff709SChao Yu 11364c8ff709SChao Yu return first_index; 11374c8ff709SChao Yu } 11384c8ff709SChao Yu 11393265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock) 11403265d3dbSChao Yu { 11413265d3dbSChao Yu void *fsdata = NULL; 11423265d3dbSChao Yu struct page *pagep; 11433265d3dbSChao Yu int log_cluster_size = F2FS_I(inode)->i_log_cluster_size; 11443265d3dbSChao Yu pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) << 11453265d3dbSChao Yu log_cluster_size; 11463265d3dbSChao Yu int err; 11473265d3dbSChao Yu 11483265d3dbSChao Yu err = f2fs_is_compressed_cluster(inode, start_idx); 11493265d3dbSChao Yu if (err < 0) 11503265d3dbSChao Yu return err; 11513265d3dbSChao Yu 11523265d3dbSChao Yu /* truncate normal cluster */ 11533265d3dbSChao Yu if (!err) 11543265d3dbSChao Yu return f2fs_do_truncate_blocks(inode, from, lock); 11553265d3dbSChao Yu 11563265d3dbSChao Yu /* truncate compressed cluster */ 11573265d3dbSChao Yu err = f2fs_prepare_compress_overwrite(inode, &pagep, 11583265d3dbSChao Yu start_idx, &fsdata); 11593265d3dbSChao Yu 11603265d3dbSChao Yu /* should not be a normal cluster */ 11613265d3dbSChao Yu f2fs_bug_on(F2FS_I_SB(inode), err == 0); 11623265d3dbSChao Yu 11633265d3dbSChao Yu if (err <= 0) 11643265d3dbSChao Yu return err; 11653265d3dbSChao Yu 11663265d3dbSChao Yu if (err > 0) { 11673265d3dbSChao Yu struct page **rpages = fsdata; 11683265d3dbSChao Yu int cluster_size = F2FS_I(inode)->i_cluster_size; 11693265d3dbSChao Yu int i; 11703265d3dbSChao Yu 11713265d3dbSChao Yu for (i = cluster_size - 1; i >= 0; i--) { 11723265d3dbSChao Yu loff_t start = rpages[i]->index << PAGE_SHIFT; 11733265d3dbSChao Yu 11743265d3dbSChao Yu if (from <= start) { 11753265d3dbSChao Yu zero_user_segment(rpages[i], 0, PAGE_SIZE); 11763265d3dbSChao Yu } else { 11773265d3dbSChao Yu zero_user_segment(rpages[i], from - start, 11783265d3dbSChao Yu PAGE_SIZE); 11793265d3dbSChao Yu break; 11803265d3dbSChao Yu } 11813265d3dbSChao Yu } 11823265d3dbSChao Yu 11833265d3dbSChao Yu f2fs_compress_write_end(inode, fsdata, start_idx, true); 11843265d3dbSChao Yu } 11853265d3dbSChao Yu return 0; 11863265d3dbSChao Yu } 11873265d3dbSChao Yu 11884c8ff709SChao Yu static int f2fs_write_compressed_pages(struct compress_ctx *cc, 11894c8ff709SChao Yu int *submitted, 11904c8ff709SChao Yu struct writeback_control *wbc, 11914c8ff709SChao Yu enum iostat_type io_type) 11924c8ff709SChao Yu { 11934c8ff709SChao Yu struct inode *inode = cc->inode; 11944c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 11954c8ff709SChao Yu struct f2fs_inode_info *fi = F2FS_I(inode); 11964c8ff709SChao Yu struct f2fs_io_info fio = { 11974c8ff709SChao Yu .sbi = sbi, 11984c8ff709SChao Yu .ino = cc->inode->i_ino, 11994c8ff709SChao Yu .type = DATA, 12004c8ff709SChao Yu .op = REQ_OP_WRITE, 12014c8ff709SChao Yu .op_flags = wbc_to_write_flags(wbc), 12024c8ff709SChao Yu .old_blkaddr = NEW_ADDR, 12034c8ff709SChao Yu .page = NULL, 12044c8ff709SChao Yu .encrypted_page = NULL, 12054c8ff709SChao Yu .compressed_page = NULL, 12064c8ff709SChao Yu .submitted = false, 12074c8ff709SChao Yu .io_type = io_type, 12084c8ff709SChao Yu .io_wbc = wbc, 120927aacd28SSatya Tangirala .encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode), 12104c8ff709SChao Yu }; 12114c8ff709SChao Yu struct dnode_of_data dn; 12124c8ff709SChao Yu struct node_info ni; 12134c8ff709SChao Yu struct compress_io_ctx *cic; 12144c8ff709SChao Yu pgoff_t start_idx = start_idx_of_cluster(cc); 12154c8ff709SChao Yu unsigned int last_index = cc->cluster_size - 1; 12164c8ff709SChao Yu loff_t psize; 12174c8ff709SChao Yu int i, err; 12184c8ff709SChao Yu 121979963d96SChao Yu if (IS_NOQUOTA(inode)) { 122079963d96SChao Yu /* 122179963d96SChao Yu * We need to wait for node_write to avoid block allocation during 122279963d96SChao Yu * checkpoint. This can only happen to quota writes which can cause 122379963d96SChao Yu * the below discard race condition. 122479963d96SChao Yu */ 122579963d96SChao Yu down_read(&sbi->node_write); 122679963d96SChao Yu } else if (!f2fs_trylock_op(sbi)) { 122731083031SChao Yu goto out_free; 122879963d96SChao Yu } 12294c8ff709SChao Yu 1230df77fbd8SChao Yu set_new_dnode(&dn, cc->inode, NULL, NULL, 0); 12314c8ff709SChao Yu 12324c8ff709SChao Yu err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); 12334c8ff709SChao Yu if (err) 12344c8ff709SChao Yu goto out_unlock_op; 12354c8ff709SChao Yu 12364c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 1237a2ced1ceSChao Yu if (data_blkaddr(dn.inode, dn.node_page, 12384c8ff709SChao Yu dn.ofs_in_node + i) == NULL_ADDR) 12394c8ff709SChao Yu goto out_put_dnode; 12404c8ff709SChao Yu } 12414c8ff709SChao Yu 12424c8ff709SChao Yu psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT; 12434c8ff709SChao Yu 12444c8ff709SChao Yu err = f2fs_get_node_info(fio.sbi, dn.nid, &ni); 12454c8ff709SChao Yu if (err) 12464c8ff709SChao Yu goto out_put_dnode; 12474c8ff709SChao Yu 12484c8ff709SChao Yu fio.version = ni.version; 12494c8ff709SChao Yu 1250c68d6c88SChao Yu cic = kmem_cache_zalloc(cic_entry_slab, GFP_NOFS); 12514c8ff709SChao Yu if (!cic) 12524c8ff709SChao Yu goto out_put_dnode; 12534c8ff709SChao Yu 12544c8ff709SChao Yu cic->magic = F2FS_COMPRESSED_PAGE_MAGIC; 12554c8ff709SChao Yu cic->inode = inode; 1256e6c3948dSChao Yu atomic_set(&cic->pending_pages, cc->nr_cpages); 125731083031SChao Yu cic->rpages = page_array_alloc(cc->inode, cc->cluster_size); 12584c8ff709SChao Yu if (!cic->rpages) 12594c8ff709SChao Yu goto out_put_cic; 12604c8ff709SChao Yu 12614c8ff709SChao Yu cic->nr_rpages = cc->cluster_size; 12624c8ff709SChao Yu 12634c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 12644c8ff709SChao Yu f2fs_set_compressed_page(cc->cpages[i], inode, 1265887347a0SChao Yu cc->rpages[i + 1]->index, cic); 12664c8ff709SChao Yu fio.compressed_page = cc->cpages[i]; 1267f567adb0SChao Yu 1268f567adb0SChao Yu fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page, 1269f567adb0SChao Yu dn.ofs_in_node + i + 1); 1270f567adb0SChao Yu 1271f567adb0SChao Yu /* wait for GCed page writeback via META_MAPPING */ 1272f567adb0SChao Yu f2fs_wait_on_block_writeback(inode, fio.old_blkaddr); 1273f567adb0SChao Yu 12744c8ff709SChao Yu if (fio.encrypted) { 12754c8ff709SChao Yu fio.page = cc->rpages[i + 1]; 12764c8ff709SChao Yu err = f2fs_encrypt_one_page(&fio); 12774c8ff709SChao Yu if (err) 12784c8ff709SChao Yu goto out_destroy_crypt; 12794c8ff709SChao Yu cc->cpages[i] = fio.encrypted_page; 12804c8ff709SChao Yu } 12814c8ff709SChao Yu } 12824c8ff709SChao Yu 12834c8ff709SChao Yu set_cluster_writeback(cc); 12844c8ff709SChao Yu 12854c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) 12864c8ff709SChao Yu cic->rpages[i] = cc->rpages[i]; 12874c8ff709SChao Yu 12884c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) { 12894c8ff709SChao Yu block_t blkaddr; 12904c8ff709SChao Yu 1291a2ced1ceSChao Yu blkaddr = f2fs_data_blkaddr(&dn); 129295978caaSChao Yu fio.page = cc->rpages[i]; 12934c8ff709SChao Yu fio.old_blkaddr = blkaddr; 12944c8ff709SChao Yu 12954c8ff709SChao Yu /* cluster header */ 12964c8ff709SChao Yu if (i == 0) { 12974c8ff709SChao Yu if (blkaddr == COMPRESS_ADDR) 12984c8ff709SChao Yu fio.compr_blocks++; 12994c8ff709SChao Yu if (__is_valid_data_blkaddr(blkaddr)) 13004c8ff709SChao Yu f2fs_invalidate_blocks(sbi, blkaddr); 13014c8ff709SChao Yu f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR); 13024c8ff709SChao Yu goto unlock_continue; 13034c8ff709SChao Yu } 13044c8ff709SChao Yu 13054c8ff709SChao Yu if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr)) 13064c8ff709SChao Yu fio.compr_blocks++; 13074c8ff709SChao Yu 13084c8ff709SChao Yu if (i > cc->nr_cpages) { 13094c8ff709SChao Yu if (__is_valid_data_blkaddr(blkaddr)) { 13104c8ff709SChao Yu f2fs_invalidate_blocks(sbi, blkaddr); 13114c8ff709SChao Yu f2fs_update_data_blkaddr(&dn, NEW_ADDR); 13124c8ff709SChao Yu } 13134c8ff709SChao Yu goto unlock_continue; 13144c8ff709SChao Yu } 13154c8ff709SChao Yu 13164c8ff709SChao Yu f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR); 13174c8ff709SChao Yu 13184c8ff709SChao Yu if (fio.encrypted) 13194c8ff709SChao Yu fio.encrypted_page = cc->cpages[i - 1]; 13204c8ff709SChao Yu else 13214c8ff709SChao Yu fio.compressed_page = cc->cpages[i - 1]; 13224c8ff709SChao Yu 13234c8ff709SChao Yu cc->cpages[i - 1] = NULL; 13244c8ff709SChao Yu f2fs_outplace_write_data(&dn, &fio); 13254c8ff709SChao Yu (*submitted)++; 13264c8ff709SChao Yu unlock_continue: 13274c8ff709SChao Yu inode_dec_dirty_pages(cc->inode); 13284c8ff709SChao Yu unlock_page(fio.page); 13294c8ff709SChao Yu } 13304c8ff709SChao Yu 13314c8ff709SChao Yu if (fio.compr_blocks) 13324c8ff709SChao Yu f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false); 13334c8ff709SChao Yu f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true); 13345ac443e2SDaeho Jeong add_compr_block_stat(inode, cc->nr_cpages); 13354c8ff709SChao Yu 13364c8ff709SChao Yu set_inode_flag(cc->inode, FI_APPEND_WRITE); 13374c8ff709SChao Yu if (cc->cluster_idx == 0) 13384c8ff709SChao Yu set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); 13394c8ff709SChao Yu 13404c8ff709SChao Yu f2fs_put_dnode(&dn); 134179963d96SChao Yu if (IS_NOQUOTA(inode)) 134279963d96SChao Yu up_read(&sbi->node_write); 134379963d96SChao Yu else 13444c8ff709SChao Yu f2fs_unlock_op(sbi); 13454c8ff709SChao Yu 1346c10c9820SChao Yu spin_lock(&fi->i_size_lock); 13474c8ff709SChao Yu if (fi->last_disk_size < psize) 13484c8ff709SChao Yu fi->last_disk_size = psize; 1349c10c9820SChao Yu spin_unlock(&fi->i_size_lock); 13504c8ff709SChao Yu 13514c8ff709SChao Yu f2fs_put_rpages(cc); 135231083031SChao Yu page_array_free(cc->inode, cc->cpages, cc->nr_cpages); 135331083031SChao Yu cc->cpages = NULL; 13548bfbfb0dSChao Yu f2fs_destroy_compress_ctx(cc, false); 13554c8ff709SChao Yu return 0; 13564c8ff709SChao Yu 13574c8ff709SChao Yu out_destroy_crypt: 135831083031SChao Yu page_array_free(cc->inode, cic->rpages, cc->cluster_size); 13594c8ff709SChao Yu 13604c8ff709SChao Yu for (--i; i >= 0; i--) 13614c8ff709SChao Yu fscrypt_finalize_bounce_page(&cc->cpages[i]); 13624c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 13634c8ff709SChao Yu if (!cc->cpages[i]) 13644c8ff709SChao Yu continue; 1365a12cc5b4SChao Yu f2fs_compress_free_page(cc->cpages[i]); 1366a12cc5b4SChao Yu cc->cpages[i] = NULL; 13674c8ff709SChao Yu } 13684c8ff709SChao Yu out_put_cic: 1369c68d6c88SChao Yu kmem_cache_free(cic_entry_slab, cic); 13704c8ff709SChao Yu out_put_dnode: 13714c8ff709SChao Yu f2fs_put_dnode(&dn); 13724c8ff709SChao Yu out_unlock_op: 137379963d96SChao Yu if (IS_NOQUOTA(inode)) 137479963d96SChao Yu up_read(&sbi->node_write); 137579963d96SChao Yu else 13764c8ff709SChao Yu f2fs_unlock_op(sbi); 137731083031SChao Yu out_free: 137831083031SChao Yu page_array_free(cc->inode, cc->cpages, cc->nr_cpages); 137931083031SChao Yu cc->cpages = NULL; 13804c8ff709SChao Yu return -EAGAIN; 13814c8ff709SChao Yu } 13824c8ff709SChao Yu 13834c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page) 13844c8ff709SChao Yu { 13854c8ff709SChao Yu struct f2fs_sb_info *sbi = bio->bi_private; 13864c8ff709SChao Yu struct compress_io_ctx *cic = 13874c8ff709SChao Yu (struct compress_io_ctx *)page_private(page); 13884c8ff709SChao Yu int i; 13894c8ff709SChao Yu 13904c8ff709SChao Yu if (unlikely(bio->bi_status)) 13914c8ff709SChao Yu mapping_set_error(cic->inode->i_mapping, -EIO); 13924c8ff709SChao Yu 13935e6bbde9SChao Yu f2fs_compress_free_page(page); 13944c8ff709SChao Yu 13954c8ff709SChao Yu dec_page_count(sbi, F2FS_WB_DATA); 13964c8ff709SChao Yu 1397e6c3948dSChao Yu if (atomic_dec_return(&cic->pending_pages)) 13984c8ff709SChao Yu return; 13994c8ff709SChao Yu 14004c8ff709SChao Yu for (i = 0; i < cic->nr_rpages; i++) { 14014c8ff709SChao Yu WARN_ON(!cic->rpages[i]); 14024c8ff709SChao Yu clear_cold_data(cic->rpages[i]); 14034c8ff709SChao Yu end_page_writeback(cic->rpages[i]); 14044c8ff709SChao Yu } 14054c8ff709SChao Yu 140631083031SChao Yu page_array_free(cic->inode, cic->rpages, cic->nr_rpages); 1407c68d6c88SChao Yu kmem_cache_free(cic_entry_slab, cic); 14084c8ff709SChao Yu } 14094c8ff709SChao Yu 14104c8ff709SChao Yu static int f2fs_write_raw_pages(struct compress_ctx *cc, 14114c8ff709SChao Yu int *submitted, 14124c8ff709SChao Yu struct writeback_control *wbc, 14134c8ff709SChao Yu enum iostat_type io_type) 14144c8ff709SChao Yu { 14154c8ff709SChao Yu struct address_space *mapping = cc->inode->i_mapping; 14164c8ff709SChao Yu int _submitted, compr_blocks, ret; 14174c8ff709SChao Yu int i = -1, err = 0; 14184c8ff709SChao Yu 14194c8ff709SChao Yu compr_blocks = f2fs_compressed_blocks(cc); 14204c8ff709SChao Yu if (compr_blocks < 0) { 14214c8ff709SChao Yu err = compr_blocks; 14224c8ff709SChao Yu goto out_err; 14234c8ff709SChao Yu } 14244c8ff709SChao Yu 14254c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 14264c8ff709SChao Yu if (!cc->rpages[i]) 14274c8ff709SChao Yu continue; 14284c8ff709SChao Yu retry_write: 14294c8ff709SChao Yu if (cc->rpages[i]->mapping != mapping) { 14304c8ff709SChao Yu unlock_page(cc->rpages[i]); 14314c8ff709SChao Yu continue; 14324c8ff709SChao Yu } 14334c8ff709SChao Yu 14344c8ff709SChao Yu BUG_ON(!PageLocked(cc->rpages[i])); 14354c8ff709SChao Yu 14364c8ff709SChao Yu ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted, 14374c8ff709SChao Yu NULL, NULL, wbc, io_type, 14383afae09fSChao Yu compr_blocks, false); 14394c8ff709SChao Yu if (ret) { 14404c8ff709SChao Yu if (ret == AOP_WRITEPAGE_ACTIVATE) { 14414c8ff709SChao Yu unlock_page(cc->rpages[i]); 14424c8ff709SChao Yu ret = 0; 14434c8ff709SChao Yu } else if (ret == -EAGAIN) { 1444466357dcSChao Yu /* 1445466357dcSChao Yu * for quota file, just redirty left pages to 1446466357dcSChao Yu * avoid deadlock caused by cluster update race 1447466357dcSChao Yu * from foreground operation. 1448466357dcSChao Yu */ 1449466357dcSChao Yu if (IS_NOQUOTA(cc->inode)) { 1450466357dcSChao Yu err = 0; 1451466357dcSChao Yu goto out_err; 1452466357dcSChao Yu } 14534c8ff709SChao Yu ret = 0; 14544c8ff709SChao Yu cond_resched(); 14555df7731fSChao Yu congestion_wait(BLK_RW_ASYNC, 14565df7731fSChao Yu DEFAULT_IO_TIMEOUT); 14574c8ff709SChao Yu lock_page(cc->rpages[i]); 1458eb1353cfSChao Yu 1459eb1353cfSChao Yu if (!PageDirty(cc->rpages[i])) { 1460eb1353cfSChao Yu unlock_page(cc->rpages[i]); 1461eb1353cfSChao Yu continue; 1462eb1353cfSChao Yu } 1463eb1353cfSChao Yu 14644c8ff709SChao Yu clear_page_dirty_for_io(cc->rpages[i]); 14654c8ff709SChao Yu goto retry_write; 14664c8ff709SChao Yu } 14674c8ff709SChao Yu err = ret; 1468466357dcSChao Yu goto out_err; 14694c8ff709SChao Yu } 14704c8ff709SChao Yu 14714c8ff709SChao Yu *submitted += _submitted; 14724c8ff709SChao Yu } 14733afae09fSChao Yu 14743afae09fSChao Yu f2fs_balance_fs(F2FS_M_SB(mapping), true); 14753afae09fSChao Yu 14764c8ff709SChao Yu return 0; 14774c8ff709SChao Yu out_err: 14784c8ff709SChao Yu for (++i; i < cc->cluster_size; i++) { 14794c8ff709SChao Yu if (!cc->rpages[i]) 14804c8ff709SChao Yu continue; 14814c8ff709SChao Yu redirty_page_for_writepage(wbc, cc->rpages[i]); 14824c8ff709SChao Yu unlock_page(cc->rpages[i]); 14834c8ff709SChao Yu } 14844c8ff709SChao Yu return err; 14854c8ff709SChao Yu } 14864c8ff709SChao Yu 14874c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc, 14884c8ff709SChao Yu int *submitted, 14894c8ff709SChao Yu struct writeback_control *wbc, 14904c8ff709SChao Yu enum iostat_type io_type) 14914c8ff709SChao Yu { 14924c8ff709SChao Yu int err; 14934c8ff709SChao Yu 14944c8ff709SChao Yu *submitted = 0; 14954c8ff709SChao Yu if (cluster_may_compress(cc)) { 14964c8ff709SChao Yu err = f2fs_compress_pages(cc); 14974c8ff709SChao Yu if (err == -EAGAIN) { 14984c8ff709SChao Yu goto write; 14994c8ff709SChao Yu } else if (err) { 15004c8ff709SChao Yu f2fs_put_rpages_wbc(cc, wbc, true, 1); 15014c8ff709SChao Yu goto destroy_out; 15024c8ff709SChao Yu } 15034c8ff709SChao Yu 15044c8ff709SChao Yu err = f2fs_write_compressed_pages(cc, submitted, 15054c8ff709SChao Yu wbc, io_type); 15064c8ff709SChao Yu if (!err) 15074c8ff709SChao Yu return 0; 15084c8ff709SChao Yu f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN); 15094c8ff709SChao Yu } 15104c8ff709SChao Yu write: 15114c8ff709SChao Yu f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted); 15124c8ff709SChao Yu 15134c8ff709SChao Yu err = f2fs_write_raw_pages(cc, submitted, wbc, io_type); 15144c8ff709SChao Yu f2fs_put_rpages_wbc(cc, wbc, false, 0); 15154c8ff709SChao Yu destroy_out: 15168bfbfb0dSChao Yu f2fs_destroy_compress_ctx(cc, false); 15174c8ff709SChao Yu return err; 15184c8ff709SChao Yu } 15194c8ff709SChao Yu 15207f59b277SEric Biggers static void f2fs_free_dic(struct decompress_io_ctx *dic); 15217f59b277SEric Biggers 15224c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc) 15234c8ff709SChao Yu { 15244c8ff709SChao Yu struct decompress_io_ctx *dic; 15254c8ff709SChao Yu pgoff_t start_idx = start_idx_of_cluster(cc); 15264c8ff709SChao Yu int i; 15274c8ff709SChao Yu 1528c68d6c88SChao Yu dic = kmem_cache_zalloc(dic_entry_slab, GFP_NOFS); 15294c8ff709SChao Yu if (!dic) 15304c8ff709SChao Yu return ERR_PTR(-ENOMEM); 15314c8ff709SChao Yu 153231083031SChao Yu dic->rpages = page_array_alloc(cc->inode, cc->cluster_size); 15334c8ff709SChao Yu if (!dic->rpages) { 1534c68d6c88SChao Yu kmem_cache_free(dic_entry_slab, dic); 15354c8ff709SChao Yu return ERR_PTR(-ENOMEM); 15364c8ff709SChao Yu } 15374c8ff709SChao Yu 15384c8ff709SChao Yu dic->magic = F2FS_COMPRESSED_PAGE_MAGIC; 15394c8ff709SChao Yu dic->inode = cc->inode; 15407f59b277SEric Biggers atomic_set(&dic->remaining_pages, cc->nr_cpages); 15414c8ff709SChao Yu dic->cluster_idx = cc->cluster_idx; 15424c8ff709SChao Yu dic->cluster_size = cc->cluster_size; 15434c8ff709SChao Yu dic->log_cluster_size = cc->log_cluster_size; 15444c8ff709SChao Yu dic->nr_cpages = cc->nr_cpages; 15457f59b277SEric Biggers refcount_set(&dic->refcnt, 1); 15464c8ff709SChao Yu dic->failed = false; 15477f59b277SEric Biggers dic->need_verity = f2fs_need_verity(cc->inode, start_idx); 15484c8ff709SChao Yu 15494c8ff709SChao Yu for (i = 0; i < dic->cluster_size; i++) 15504c8ff709SChao Yu dic->rpages[i] = cc->rpages[i]; 15514c8ff709SChao Yu dic->nr_rpages = cc->cluster_size; 15524c8ff709SChao Yu 155331083031SChao Yu dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages); 15544c8ff709SChao Yu if (!dic->cpages) 15554c8ff709SChao Yu goto out_free; 15564c8ff709SChao Yu 15574c8ff709SChao Yu for (i = 0; i < dic->nr_cpages; i++) { 15584c8ff709SChao Yu struct page *page; 15594c8ff709SChao Yu 15605e6bbde9SChao Yu page = f2fs_compress_alloc_page(); 15614c8ff709SChao Yu if (!page) 15624c8ff709SChao Yu goto out_free; 15634c8ff709SChao Yu 15644c8ff709SChao Yu f2fs_set_compressed_page(page, cc->inode, 1565887347a0SChao Yu start_idx + i + 1, dic); 15664c8ff709SChao Yu dic->cpages[i] = page; 15674c8ff709SChao Yu } 15684c8ff709SChao Yu 15694c8ff709SChao Yu return dic; 15704c8ff709SChao Yu 15714c8ff709SChao Yu out_free: 15724c8ff709SChao Yu f2fs_free_dic(dic); 15734c8ff709SChao Yu return ERR_PTR(-ENOMEM); 15744c8ff709SChao Yu } 15754c8ff709SChao Yu 15767f59b277SEric Biggers static void f2fs_free_dic(struct decompress_io_ctx *dic) 15774c8ff709SChao Yu { 15784c8ff709SChao Yu int i; 15794c8ff709SChao Yu 15804c8ff709SChao Yu if (dic->tpages) { 15814c8ff709SChao Yu for (i = 0; i < dic->cluster_size; i++) { 15824c8ff709SChao Yu if (dic->rpages[i]) 15834c8ff709SChao Yu continue; 15848908e753SChao Yu if (!dic->tpages[i]) 15858908e753SChao Yu continue; 15865e6bbde9SChao Yu f2fs_compress_free_page(dic->tpages[i]); 15874c8ff709SChao Yu } 158831083031SChao Yu page_array_free(dic->inode, dic->tpages, dic->cluster_size); 15894c8ff709SChao Yu } 15904c8ff709SChao Yu 15914c8ff709SChao Yu if (dic->cpages) { 15924c8ff709SChao Yu for (i = 0; i < dic->nr_cpages; i++) { 15934c8ff709SChao Yu if (!dic->cpages[i]) 15944c8ff709SChao Yu continue; 15955e6bbde9SChao Yu f2fs_compress_free_page(dic->cpages[i]); 15964c8ff709SChao Yu } 159731083031SChao Yu page_array_free(dic->inode, dic->cpages, dic->nr_cpages); 15984c8ff709SChao Yu } 15994c8ff709SChao Yu 160031083031SChao Yu page_array_free(dic->inode, dic->rpages, dic->nr_rpages); 1601c68d6c88SChao Yu kmem_cache_free(dic_entry_slab, dic); 16024c8ff709SChao Yu } 16034c8ff709SChao Yu 16047f59b277SEric Biggers static void f2fs_put_dic(struct decompress_io_ctx *dic) 16057f59b277SEric Biggers { 16067f59b277SEric Biggers if (refcount_dec_and_test(&dic->refcnt)) 16077f59b277SEric Biggers f2fs_free_dic(dic); 16087f59b277SEric Biggers } 16097f59b277SEric Biggers 16107f59b277SEric Biggers /* 16117f59b277SEric Biggers * Update and unlock the cluster's pagecache pages, and release the reference to 16127f59b277SEric Biggers * the decompress_io_ctx that was being held for I/O completion. 16137f59b277SEric Biggers */ 16147f59b277SEric Biggers static void __f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed) 16154c8ff709SChao Yu { 16164c8ff709SChao Yu int i; 16174c8ff709SChao Yu 16187f59b277SEric Biggers for (i = 0; i < dic->cluster_size; i++) { 16197f59b277SEric Biggers struct page *rpage = dic->rpages[i]; 16204c8ff709SChao Yu 16214c8ff709SChao Yu if (!rpage) 16224c8ff709SChao Yu continue; 16234c8ff709SChao Yu 16247f59b277SEric Biggers /* PG_error was set if verity failed. */ 16257f59b277SEric Biggers if (failed || PageError(rpage)) { 16264c8ff709SChao Yu ClearPageUptodate(rpage); 16277f59b277SEric Biggers /* will re-read again later */ 16284c8ff709SChao Yu ClearPageError(rpage); 16297f59b277SEric Biggers } else { 16307f59b277SEric Biggers SetPageUptodate(rpage); 16317f59b277SEric Biggers } 16324c8ff709SChao Yu unlock_page(rpage); 16334c8ff709SChao Yu } 16347f59b277SEric Biggers 16357f59b277SEric Biggers f2fs_put_dic(dic); 16367f59b277SEric Biggers } 16377f59b277SEric Biggers 16387f59b277SEric Biggers static void f2fs_verify_cluster(struct work_struct *work) 16397f59b277SEric Biggers { 16407f59b277SEric Biggers struct decompress_io_ctx *dic = 16417f59b277SEric Biggers container_of(work, struct decompress_io_ctx, verity_work); 16427f59b277SEric Biggers int i; 16437f59b277SEric Biggers 16447f59b277SEric Biggers /* Verify the cluster's decompressed pages with fs-verity. */ 16457f59b277SEric Biggers for (i = 0; i < dic->cluster_size; i++) { 16467f59b277SEric Biggers struct page *rpage = dic->rpages[i]; 16477f59b277SEric Biggers 16487f59b277SEric Biggers if (rpage && !fsverity_verify_page(rpage)) 16497f59b277SEric Biggers SetPageError(rpage); 16507f59b277SEric Biggers } 16517f59b277SEric Biggers 16527f59b277SEric Biggers __f2fs_decompress_end_io(dic, false); 16537f59b277SEric Biggers } 16547f59b277SEric Biggers 16557f59b277SEric Biggers /* 16567f59b277SEric Biggers * This is called when a compressed cluster has been decompressed 16577f59b277SEric Biggers * (or failed to be read and/or decompressed). 16587f59b277SEric Biggers */ 16597f59b277SEric Biggers void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed) 16607f59b277SEric Biggers { 16617f59b277SEric Biggers if (!failed && dic->need_verity) { 16627f59b277SEric Biggers /* 16637f59b277SEric Biggers * Note that to avoid deadlocks, the verity work can't be done 16647f59b277SEric Biggers * on the decompression workqueue. This is because verifying 16657f59b277SEric Biggers * the data pages can involve reading metadata pages from the 16667f59b277SEric Biggers * file, and these metadata pages may be compressed. 16677f59b277SEric Biggers */ 16687f59b277SEric Biggers INIT_WORK(&dic->verity_work, f2fs_verify_cluster); 16697f59b277SEric Biggers fsverity_enqueue_verify_work(&dic->verity_work); 16707f59b277SEric Biggers } else { 16717f59b277SEric Biggers __f2fs_decompress_end_io(dic, failed); 16727f59b277SEric Biggers } 16737f59b277SEric Biggers } 16747f59b277SEric Biggers 16757f59b277SEric Biggers /* 16767f59b277SEric Biggers * Put a reference to a compressed page's decompress_io_ctx. 16777f59b277SEric Biggers * 16787f59b277SEric Biggers * This is called when the page is no longer needed and can be freed. 16797f59b277SEric Biggers */ 16807f59b277SEric Biggers void f2fs_put_page_dic(struct page *page) 16817f59b277SEric Biggers { 16827f59b277SEric Biggers struct decompress_io_ctx *dic = 16837f59b277SEric Biggers (struct decompress_io_ctx *)page_private(page); 16847f59b277SEric Biggers 16857f59b277SEric Biggers f2fs_put_dic(dic); 16864c8ff709SChao Yu } 168731083031SChao Yu 168831083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) 168931083031SChao Yu { 169031083031SChao Yu dev_t dev = sbi->sb->s_bdev->bd_dev; 169131083031SChao Yu char slab_name[32]; 169231083031SChao Yu 169331083031SChao Yu sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev)); 169431083031SChao Yu 169531083031SChao Yu sbi->page_array_slab_size = sizeof(struct page *) << 169631083031SChao Yu F2FS_OPTION(sbi).compress_log_size; 169731083031SChao Yu 169831083031SChao Yu sbi->page_array_slab = f2fs_kmem_cache_create(slab_name, 169931083031SChao Yu sbi->page_array_slab_size); 170031083031SChao Yu if (!sbi->page_array_slab) 170131083031SChao Yu return -ENOMEM; 170231083031SChao Yu return 0; 170331083031SChao Yu } 170431083031SChao Yu 170531083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) 170631083031SChao Yu { 170731083031SChao Yu kmem_cache_destroy(sbi->page_array_slab); 170831083031SChao Yu } 1709c68d6c88SChao Yu 1710c68d6c88SChao Yu static int __init f2fs_init_cic_cache(void) 1711c68d6c88SChao Yu { 1712c68d6c88SChao Yu cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry", 1713c68d6c88SChao Yu sizeof(struct compress_io_ctx)); 1714c68d6c88SChao Yu if (!cic_entry_slab) 1715c68d6c88SChao Yu return -ENOMEM; 1716c68d6c88SChao Yu return 0; 1717c68d6c88SChao Yu } 1718c68d6c88SChao Yu 1719c68d6c88SChao Yu static void f2fs_destroy_cic_cache(void) 1720c68d6c88SChao Yu { 1721c68d6c88SChao Yu kmem_cache_destroy(cic_entry_slab); 1722c68d6c88SChao Yu } 1723c68d6c88SChao Yu 1724c68d6c88SChao Yu static int __init f2fs_init_dic_cache(void) 1725c68d6c88SChao Yu { 1726c68d6c88SChao Yu dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry", 1727c68d6c88SChao Yu sizeof(struct decompress_io_ctx)); 1728c68d6c88SChao Yu if (!dic_entry_slab) 1729c68d6c88SChao Yu return -ENOMEM; 1730c68d6c88SChao Yu return 0; 1731c68d6c88SChao Yu } 1732c68d6c88SChao Yu 1733c68d6c88SChao Yu static void f2fs_destroy_dic_cache(void) 1734c68d6c88SChao Yu { 1735c68d6c88SChao Yu kmem_cache_destroy(dic_entry_slab); 1736c68d6c88SChao Yu } 1737c68d6c88SChao Yu 1738c68d6c88SChao Yu int __init f2fs_init_compress_cache(void) 1739c68d6c88SChao Yu { 1740c68d6c88SChao Yu int err; 1741c68d6c88SChao Yu 1742c68d6c88SChao Yu err = f2fs_init_cic_cache(); 1743c68d6c88SChao Yu if (err) 1744c68d6c88SChao Yu goto out; 1745c68d6c88SChao Yu err = f2fs_init_dic_cache(); 1746c68d6c88SChao Yu if (err) 1747c68d6c88SChao Yu goto free_cic; 1748c68d6c88SChao Yu return 0; 1749c68d6c88SChao Yu free_cic: 1750c68d6c88SChao Yu f2fs_destroy_cic_cache(); 1751c68d6c88SChao Yu out: 1752c68d6c88SChao Yu return -ENOMEM; 1753c68d6c88SChao Yu } 1754c68d6c88SChao Yu 1755c68d6c88SChao Yu void f2fs_destroy_compress_cache(void) 1756c68d6c88SChao Yu { 1757c68d6c88SChao Yu f2fs_destroy_dic_cache(); 1758c68d6c88SChao Yu f2fs_destroy_cic_cache(); 1759c68d6c88SChao Yu } 1760