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> 144c8ff709SChao Yu 154c8ff709SChao Yu #include "f2fs.h" 164c8ff709SChao Yu #include "node.h" 174c8ff709SChao Yu #include <trace/events/f2fs.h> 184c8ff709SChao Yu 194c8ff709SChao Yu struct f2fs_compress_ops { 204c8ff709SChao Yu int (*init_compress_ctx)(struct compress_ctx *cc); 214c8ff709SChao Yu void (*destroy_compress_ctx)(struct compress_ctx *cc); 224c8ff709SChao Yu int (*compress_pages)(struct compress_ctx *cc); 234c8ff709SChao Yu int (*decompress_pages)(struct decompress_io_ctx *dic); 244c8ff709SChao Yu }; 254c8ff709SChao Yu 264c8ff709SChao Yu static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index) 274c8ff709SChao Yu { 284c8ff709SChao Yu return index & (cc->cluster_size - 1); 294c8ff709SChao Yu } 304c8ff709SChao Yu 314c8ff709SChao Yu static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index) 324c8ff709SChao Yu { 334c8ff709SChao Yu return index >> cc->log_cluster_size; 344c8ff709SChao Yu } 354c8ff709SChao Yu 364c8ff709SChao Yu static pgoff_t start_idx_of_cluster(struct compress_ctx *cc) 374c8ff709SChao Yu { 384c8ff709SChao Yu return cc->cluster_idx << cc->log_cluster_size; 394c8ff709SChao Yu } 404c8ff709SChao Yu 414c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page) 424c8ff709SChao Yu { 434c8ff709SChao Yu if (!PagePrivate(page)) 444c8ff709SChao Yu return false; 454c8ff709SChao Yu if (!page_private(page)) 464c8ff709SChao Yu return false; 474c8ff709SChao Yu if (IS_ATOMIC_WRITTEN_PAGE(page) || IS_DUMMY_WRITTEN_PAGE(page)) 484c8ff709SChao Yu return false; 494c8ff709SChao Yu f2fs_bug_on(F2FS_M_SB(page->mapping), 504c8ff709SChao Yu *((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC); 514c8ff709SChao Yu return true; 524c8ff709SChao Yu } 534c8ff709SChao Yu 544c8ff709SChao Yu static void f2fs_set_compressed_page(struct page *page, 554c8ff709SChao Yu struct inode *inode, pgoff_t index, void *data, refcount_t *r) 564c8ff709SChao Yu { 574c8ff709SChao Yu SetPagePrivate(page); 584c8ff709SChao Yu set_page_private(page, (unsigned long)data); 594c8ff709SChao Yu 604c8ff709SChao Yu /* i_crypto_info and iv index */ 614c8ff709SChao Yu page->index = index; 624c8ff709SChao Yu page->mapping = inode->i_mapping; 634c8ff709SChao Yu if (r) 644c8ff709SChao Yu refcount_inc(r); 654c8ff709SChao Yu } 664c8ff709SChao Yu 674c8ff709SChao Yu static void f2fs_put_compressed_page(struct page *page) 684c8ff709SChao Yu { 694c8ff709SChao Yu set_page_private(page, (unsigned long)NULL); 704c8ff709SChao Yu ClearPagePrivate(page); 714c8ff709SChao Yu page->mapping = NULL; 724c8ff709SChao Yu unlock_page(page); 734c8ff709SChao Yu put_page(page); 744c8ff709SChao Yu } 754c8ff709SChao Yu 764c8ff709SChao Yu static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock) 774c8ff709SChao Yu { 784c8ff709SChao Yu int i; 794c8ff709SChao Yu 804c8ff709SChao Yu for (i = 0; i < len; i++) { 814c8ff709SChao Yu if (!cc->rpages[i]) 824c8ff709SChao Yu continue; 834c8ff709SChao Yu if (unlock) 844c8ff709SChao Yu unlock_page(cc->rpages[i]); 854c8ff709SChao Yu else 864c8ff709SChao Yu put_page(cc->rpages[i]); 874c8ff709SChao Yu } 884c8ff709SChao Yu } 894c8ff709SChao Yu 904c8ff709SChao Yu static void f2fs_put_rpages(struct compress_ctx *cc) 914c8ff709SChao Yu { 924c8ff709SChao Yu f2fs_drop_rpages(cc, cc->cluster_size, false); 934c8ff709SChao Yu } 944c8ff709SChao Yu 954c8ff709SChao Yu static void f2fs_unlock_rpages(struct compress_ctx *cc, int len) 964c8ff709SChao Yu { 974c8ff709SChao Yu f2fs_drop_rpages(cc, len, true); 984c8ff709SChao Yu } 994c8ff709SChao Yu 1004c8ff709SChao Yu static void f2fs_put_rpages_mapping(struct compress_ctx *cc, 1014c8ff709SChao Yu struct address_space *mapping, 1024c8ff709SChao Yu pgoff_t start, int len) 1034c8ff709SChao Yu { 1044c8ff709SChao Yu int i; 1054c8ff709SChao Yu 1064c8ff709SChao Yu for (i = 0; i < len; i++) { 1074c8ff709SChao Yu struct page *page = find_get_page(mapping, start + i); 1084c8ff709SChao Yu 1094c8ff709SChao Yu put_page(page); 1104c8ff709SChao Yu put_page(page); 1114c8ff709SChao Yu } 1124c8ff709SChao Yu } 1134c8ff709SChao Yu 1144c8ff709SChao Yu static void f2fs_put_rpages_wbc(struct compress_ctx *cc, 1154c8ff709SChao Yu struct writeback_control *wbc, bool redirty, int unlock) 1164c8ff709SChao Yu { 1174c8ff709SChao Yu unsigned int i; 1184c8ff709SChao Yu 1194c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 1204c8ff709SChao Yu if (!cc->rpages[i]) 1214c8ff709SChao Yu continue; 1224c8ff709SChao Yu if (redirty) 1234c8ff709SChao Yu redirty_page_for_writepage(wbc, cc->rpages[i]); 1244c8ff709SChao Yu f2fs_put_page(cc->rpages[i], unlock); 1254c8ff709SChao Yu } 1264c8ff709SChao Yu } 1274c8ff709SChao Yu 1284c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page) 1294c8ff709SChao Yu { 1304c8ff709SChao Yu return ((struct compress_io_ctx *)page_private(page))->rpages[0]; 1314c8ff709SChao Yu } 1324c8ff709SChao Yu 1334c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc) 1344c8ff709SChao Yu { 1354c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 1364c8ff709SChao Yu 1374c8ff709SChao Yu if (cc->nr_rpages) 1384c8ff709SChao Yu return 0; 1394c8ff709SChao Yu 1404c8ff709SChao Yu cc->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) << 1414c8ff709SChao Yu cc->log_cluster_size, GFP_NOFS); 1424c8ff709SChao Yu return cc->rpages ? 0 : -ENOMEM; 1434c8ff709SChao Yu } 1444c8ff709SChao Yu 1454c8ff709SChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc) 1464c8ff709SChao Yu { 1474c8ff709SChao Yu kfree(cc->rpages); 1484c8ff709SChao Yu cc->rpages = NULL; 1494c8ff709SChao Yu cc->nr_rpages = 0; 1504c8ff709SChao Yu cc->nr_cpages = 0; 1514c8ff709SChao Yu cc->cluster_idx = NULL_CLUSTER; 1524c8ff709SChao Yu } 1534c8ff709SChao Yu 1544c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page) 1554c8ff709SChao Yu { 1564c8ff709SChao Yu unsigned int cluster_ofs; 1574c8ff709SChao Yu 1584c8ff709SChao Yu if (!f2fs_cluster_can_merge_page(cc, page->index)) 1594c8ff709SChao Yu f2fs_bug_on(F2FS_I_SB(cc->inode), 1); 1604c8ff709SChao Yu 1614c8ff709SChao Yu cluster_ofs = offset_in_cluster(cc, page->index); 1624c8ff709SChao Yu cc->rpages[cluster_ofs] = page; 1634c8ff709SChao Yu cc->nr_rpages++; 1644c8ff709SChao Yu cc->cluster_idx = cluster_idx(cc, page->index); 1654c8ff709SChao Yu } 1664c8ff709SChao Yu 1674c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO 1684c8ff709SChao Yu static int lzo_init_compress_ctx(struct compress_ctx *cc) 1694c8ff709SChao Yu { 1704c8ff709SChao Yu cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), 1714c8ff709SChao Yu LZO1X_MEM_COMPRESS, GFP_NOFS); 1724c8ff709SChao Yu if (!cc->private) 1734c8ff709SChao Yu return -ENOMEM; 1744c8ff709SChao Yu 1754c8ff709SChao Yu cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size); 1764c8ff709SChao Yu return 0; 1774c8ff709SChao Yu } 1784c8ff709SChao Yu 1794c8ff709SChao Yu static void lzo_destroy_compress_ctx(struct compress_ctx *cc) 1804c8ff709SChao Yu { 1814c8ff709SChao Yu kvfree(cc->private); 1824c8ff709SChao Yu cc->private = NULL; 1834c8ff709SChao Yu } 1844c8ff709SChao Yu 1854c8ff709SChao Yu static int lzo_compress_pages(struct compress_ctx *cc) 1864c8ff709SChao Yu { 1874c8ff709SChao Yu int ret; 1884c8ff709SChao Yu 1894c8ff709SChao Yu ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata, 1904c8ff709SChao Yu &cc->clen, cc->private); 1914c8ff709SChao Yu if (ret != LZO_E_OK) { 1924c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n", 1934c8ff709SChao Yu KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret); 1944c8ff709SChao Yu return -EIO; 1954c8ff709SChao Yu } 1964c8ff709SChao Yu return 0; 1974c8ff709SChao Yu } 1984c8ff709SChao Yu 1994c8ff709SChao Yu static int lzo_decompress_pages(struct decompress_io_ctx *dic) 2004c8ff709SChao Yu { 2014c8ff709SChao Yu int ret; 2024c8ff709SChao Yu 2034c8ff709SChao Yu ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen, 2044c8ff709SChao Yu dic->rbuf, &dic->rlen); 2054c8ff709SChao Yu if (ret != LZO_E_OK) { 2064c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n", 2074c8ff709SChao Yu KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret); 2084c8ff709SChao Yu return -EIO; 2094c8ff709SChao Yu } 2104c8ff709SChao Yu 2114c8ff709SChao Yu if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) { 2124c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, " 2134c8ff709SChao Yu "expected:%lu\n", KERN_ERR, 2144c8ff709SChao Yu F2FS_I_SB(dic->inode)->sb->s_id, 2154c8ff709SChao Yu dic->rlen, 2164c8ff709SChao Yu PAGE_SIZE << dic->log_cluster_size); 2174c8ff709SChao Yu return -EIO; 2184c8ff709SChao Yu } 2194c8ff709SChao Yu return 0; 2204c8ff709SChao Yu } 2214c8ff709SChao Yu 2224c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lzo_ops = { 2234c8ff709SChao Yu .init_compress_ctx = lzo_init_compress_ctx, 2244c8ff709SChao Yu .destroy_compress_ctx = lzo_destroy_compress_ctx, 2254c8ff709SChao Yu .compress_pages = lzo_compress_pages, 2264c8ff709SChao Yu .decompress_pages = lzo_decompress_pages, 2274c8ff709SChao Yu }; 2284c8ff709SChao Yu #endif 2294c8ff709SChao Yu 2304c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4 2314c8ff709SChao Yu static int lz4_init_compress_ctx(struct compress_ctx *cc) 2324c8ff709SChao Yu { 2334c8ff709SChao Yu cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), 2344c8ff709SChao Yu LZ4_MEM_COMPRESS, GFP_NOFS); 2354c8ff709SChao Yu if (!cc->private) 2364c8ff709SChao Yu return -ENOMEM; 2374c8ff709SChao Yu 2384c8ff709SChao Yu cc->clen = LZ4_compressBound(PAGE_SIZE << cc->log_cluster_size); 2394c8ff709SChao Yu return 0; 2404c8ff709SChao Yu } 2414c8ff709SChao Yu 2424c8ff709SChao Yu static void lz4_destroy_compress_ctx(struct compress_ctx *cc) 2434c8ff709SChao Yu { 2444c8ff709SChao Yu kvfree(cc->private); 2454c8ff709SChao Yu cc->private = NULL; 2464c8ff709SChao Yu } 2474c8ff709SChao Yu 2484c8ff709SChao Yu static int lz4_compress_pages(struct compress_ctx *cc) 2494c8ff709SChao Yu { 2504c8ff709SChao Yu int len; 2514c8ff709SChao Yu 2524c8ff709SChao Yu len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen, 2534c8ff709SChao Yu cc->clen, cc->private); 2544c8ff709SChao Yu if (!len) { 2554c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lz4 compress failed\n", 2564c8ff709SChao Yu KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id); 2574c8ff709SChao Yu return -EIO; 2584c8ff709SChao Yu } 2594c8ff709SChao Yu cc->clen = len; 2604c8ff709SChao Yu return 0; 2614c8ff709SChao Yu } 2624c8ff709SChao Yu 2634c8ff709SChao Yu static int lz4_decompress_pages(struct decompress_io_ctx *dic) 2644c8ff709SChao Yu { 2654c8ff709SChao Yu int ret; 2664c8ff709SChao Yu 2674c8ff709SChao Yu ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf, 2684c8ff709SChao Yu dic->clen, dic->rlen); 2694c8ff709SChao Yu if (ret < 0) { 2704c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n", 2714c8ff709SChao Yu KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret); 2724c8ff709SChao Yu return -EIO; 2734c8ff709SChao Yu } 2744c8ff709SChao Yu 2754c8ff709SChao Yu if (ret != PAGE_SIZE << dic->log_cluster_size) { 2764c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, " 2774c8ff709SChao Yu "expected:%lu\n", KERN_ERR, 2784c8ff709SChao Yu F2FS_I_SB(dic->inode)->sb->s_id, 2794c8ff709SChao Yu dic->rlen, 2804c8ff709SChao Yu PAGE_SIZE << dic->log_cluster_size); 2814c8ff709SChao Yu return -EIO; 2824c8ff709SChao Yu } 2834c8ff709SChao Yu return 0; 2844c8ff709SChao Yu } 2854c8ff709SChao Yu 2864c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lz4_ops = { 2874c8ff709SChao Yu .init_compress_ctx = lz4_init_compress_ctx, 2884c8ff709SChao Yu .destroy_compress_ctx = lz4_destroy_compress_ctx, 2894c8ff709SChao Yu .compress_pages = lz4_compress_pages, 2904c8ff709SChao Yu .decompress_pages = lz4_decompress_pages, 2914c8ff709SChao Yu }; 2924c8ff709SChao Yu #endif 2934c8ff709SChao Yu 2944c8ff709SChao Yu static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = { 2954c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO 2964c8ff709SChao Yu &f2fs_lzo_ops, 2974c8ff709SChao Yu #else 2984c8ff709SChao Yu NULL, 2994c8ff709SChao Yu #endif 3004c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4 3014c8ff709SChao Yu &f2fs_lz4_ops, 3024c8ff709SChao Yu #else 3034c8ff709SChao Yu NULL, 3044c8ff709SChao Yu #endif 3054c8ff709SChao Yu }; 3064c8ff709SChao Yu 3074c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode) 3084c8ff709SChao Yu { 3094c8ff709SChao Yu if (!f2fs_compressed_file(inode)) 3104c8ff709SChao Yu return true; 3114c8ff709SChao Yu return f2fs_cops[F2FS_I(inode)->i_compress_algorithm]; 3124c8ff709SChao Yu } 3134c8ff709SChao Yu 3144c8ff709SChao Yu static struct page *f2fs_grab_page(void) 3154c8ff709SChao Yu { 3164c8ff709SChao Yu struct page *page; 3174c8ff709SChao Yu 3184c8ff709SChao Yu page = alloc_page(GFP_NOFS); 3194c8ff709SChao Yu if (!page) 3204c8ff709SChao Yu return NULL; 3214c8ff709SChao Yu lock_page(page); 3224c8ff709SChao Yu return page; 3234c8ff709SChao Yu } 3244c8ff709SChao Yu 3254c8ff709SChao Yu static int f2fs_compress_pages(struct compress_ctx *cc) 3264c8ff709SChao Yu { 3274c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 3284c8ff709SChao Yu struct f2fs_inode_info *fi = F2FS_I(cc->inode); 3294c8ff709SChao Yu const struct f2fs_compress_ops *cops = 3304c8ff709SChao Yu f2fs_cops[fi->i_compress_algorithm]; 3314c8ff709SChao Yu unsigned int max_len, nr_cpages; 3324c8ff709SChao Yu int i, ret; 3334c8ff709SChao Yu 3344c8ff709SChao Yu trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx, 3354c8ff709SChao Yu cc->cluster_size, fi->i_compress_algorithm); 3364c8ff709SChao Yu 3374c8ff709SChao Yu ret = cops->init_compress_ctx(cc); 3384c8ff709SChao Yu if (ret) 3394c8ff709SChao Yu goto out; 3404c8ff709SChao Yu 3414c8ff709SChao Yu max_len = COMPRESS_HEADER_SIZE + cc->clen; 3424c8ff709SChao Yu cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE); 3434c8ff709SChao Yu 3444c8ff709SChao Yu cc->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) * 3454c8ff709SChao Yu cc->nr_cpages, GFP_NOFS); 3464c8ff709SChao Yu if (!cc->cpages) { 3474c8ff709SChao Yu ret = -ENOMEM; 3484c8ff709SChao Yu goto destroy_compress_ctx; 3494c8ff709SChao Yu } 3504c8ff709SChao Yu 3514c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 3524c8ff709SChao Yu cc->cpages[i] = f2fs_grab_page(); 3534c8ff709SChao Yu if (!cc->cpages[i]) { 3544c8ff709SChao Yu ret = -ENOMEM; 3554c8ff709SChao Yu goto out_free_cpages; 3564c8ff709SChao Yu } 3574c8ff709SChao Yu } 3584c8ff709SChao Yu 3594c8ff709SChao Yu cc->rbuf = vmap(cc->rpages, cc->cluster_size, VM_MAP, PAGE_KERNEL_RO); 3604c8ff709SChao Yu if (!cc->rbuf) { 3614c8ff709SChao Yu ret = -ENOMEM; 3624c8ff709SChao Yu goto out_free_cpages; 3634c8ff709SChao Yu } 3644c8ff709SChao Yu 3654c8ff709SChao Yu cc->cbuf = vmap(cc->cpages, cc->nr_cpages, VM_MAP, PAGE_KERNEL); 3664c8ff709SChao Yu if (!cc->cbuf) { 3674c8ff709SChao Yu ret = -ENOMEM; 3684c8ff709SChao Yu goto out_vunmap_rbuf; 3694c8ff709SChao Yu } 3704c8ff709SChao Yu 3714c8ff709SChao Yu ret = cops->compress_pages(cc); 3724c8ff709SChao Yu if (ret) 3734c8ff709SChao Yu goto out_vunmap_cbuf; 3744c8ff709SChao Yu 3754c8ff709SChao Yu max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE; 3764c8ff709SChao Yu 3774c8ff709SChao Yu if (cc->clen > max_len) { 3784c8ff709SChao Yu ret = -EAGAIN; 3794c8ff709SChao Yu goto out_vunmap_cbuf; 3804c8ff709SChao Yu } 3814c8ff709SChao Yu 3824c8ff709SChao Yu cc->cbuf->clen = cpu_to_le32(cc->clen); 3834c8ff709SChao Yu 3844c8ff709SChao Yu for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++) 3854c8ff709SChao Yu cc->cbuf->reserved[i] = cpu_to_le32(0); 3864c8ff709SChao Yu 3877fa6d598SEric Biggers nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE); 3887fa6d598SEric Biggers 3897fa6d598SEric Biggers /* zero out any unused part of the last page */ 3907fa6d598SEric Biggers memset(&cc->cbuf->cdata[cc->clen], 0, 3917fa6d598SEric Biggers (nr_cpages * PAGE_SIZE) - (cc->clen + COMPRESS_HEADER_SIZE)); 3927fa6d598SEric Biggers 3934c8ff709SChao Yu vunmap(cc->cbuf); 3944c8ff709SChao Yu vunmap(cc->rbuf); 3954c8ff709SChao Yu 3964c8ff709SChao Yu for (i = nr_cpages; i < cc->nr_cpages; i++) { 3974c8ff709SChao Yu f2fs_put_compressed_page(cc->cpages[i]); 3984c8ff709SChao Yu cc->cpages[i] = NULL; 3994c8ff709SChao Yu } 4004c8ff709SChao Yu 4014c8ff709SChao Yu cc->nr_cpages = nr_cpages; 4024c8ff709SChao Yu 4034c8ff709SChao Yu trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx, 4044c8ff709SChao Yu cc->clen, ret); 4054c8ff709SChao Yu return 0; 4064c8ff709SChao Yu 4074c8ff709SChao Yu out_vunmap_cbuf: 4084c8ff709SChao Yu vunmap(cc->cbuf); 4094c8ff709SChao Yu out_vunmap_rbuf: 4104c8ff709SChao Yu vunmap(cc->rbuf); 4114c8ff709SChao Yu out_free_cpages: 4124c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 4134c8ff709SChao Yu if (cc->cpages[i]) 4144c8ff709SChao Yu f2fs_put_compressed_page(cc->cpages[i]); 4154c8ff709SChao Yu } 4164c8ff709SChao Yu kfree(cc->cpages); 4174c8ff709SChao Yu cc->cpages = NULL; 4184c8ff709SChao Yu destroy_compress_ctx: 4194c8ff709SChao Yu cops->destroy_compress_ctx(cc); 4204c8ff709SChao Yu out: 4214c8ff709SChao Yu trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx, 4224c8ff709SChao Yu cc->clen, ret); 4234c8ff709SChao Yu return ret; 4244c8ff709SChao Yu } 4254c8ff709SChao Yu 4264c8ff709SChao Yu void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity) 4274c8ff709SChao Yu { 4284c8ff709SChao Yu struct decompress_io_ctx *dic = 4294c8ff709SChao Yu (struct decompress_io_ctx *)page_private(page); 4304c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode); 4314c8ff709SChao Yu struct f2fs_inode_info *fi= F2FS_I(dic->inode); 4324c8ff709SChao Yu const struct f2fs_compress_ops *cops = 4334c8ff709SChao Yu f2fs_cops[fi->i_compress_algorithm]; 4344c8ff709SChao Yu int ret; 4354c8ff709SChao Yu 4364c8ff709SChao Yu dec_page_count(sbi, F2FS_RD_DATA); 4374c8ff709SChao Yu 4384c8ff709SChao Yu if (bio->bi_status || PageError(page)) 4394c8ff709SChao Yu dic->failed = true; 4404c8ff709SChao Yu 4414c8ff709SChao Yu if (refcount_dec_not_one(&dic->ref)) 4424c8ff709SChao Yu return; 4434c8ff709SChao Yu 4444c8ff709SChao Yu trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx, 4454c8ff709SChao Yu dic->cluster_size, fi->i_compress_algorithm); 4464c8ff709SChao Yu 4474c8ff709SChao Yu /* submit partial compressed pages */ 4484c8ff709SChao Yu if (dic->failed) { 4494c8ff709SChao Yu ret = -EIO; 4504c8ff709SChao Yu goto out_free_dic; 4514c8ff709SChao Yu } 4524c8ff709SChao Yu 4534c8ff709SChao Yu dic->rbuf = vmap(dic->tpages, dic->cluster_size, VM_MAP, PAGE_KERNEL); 4544c8ff709SChao Yu if (!dic->rbuf) { 4554c8ff709SChao Yu ret = -ENOMEM; 4564c8ff709SChao Yu goto out_free_dic; 4574c8ff709SChao Yu } 4584c8ff709SChao Yu 4594c8ff709SChao Yu dic->cbuf = vmap(dic->cpages, dic->nr_cpages, VM_MAP, PAGE_KERNEL_RO); 4604c8ff709SChao Yu if (!dic->cbuf) { 4614c8ff709SChao Yu ret = -ENOMEM; 4624c8ff709SChao Yu goto out_vunmap_rbuf; 4634c8ff709SChao Yu } 4644c8ff709SChao Yu 4654c8ff709SChao Yu dic->clen = le32_to_cpu(dic->cbuf->clen); 4664c8ff709SChao Yu dic->rlen = PAGE_SIZE << dic->log_cluster_size; 4674c8ff709SChao Yu 4684c8ff709SChao Yu if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) { 4694c8ff709SChao Yu ret = -EFSCORRUPTED; 4704c8ff709SChao Yu goto out_vunmap_cbuf; 4714c8ff709SChao Yu } 4724c8ff709SChao Yu 4734c8ff709SChao Yu ret = cops->decompress_pages(dic); 4744c8ff709SChao Yu 4754c8ff709SChao Yu out_vunmap_cbuf: 4764c8ff709SChao Yu vunmap(dic->cbuf); 4774c8ff709SChao Yu out_vunmap_rbuf: 4784c8ff709SChao Yu vunmap(dic->rbuf); 4794c8ff709SChao Yu out_free_dic: 4804c8ff709SChao Yu if (!verity) 4814c8ff709SChao Yu f2fs_decompress_end_io(dic->rpages, dic->cluster_size, 4824c8ff709SChao Yu ret, false); 4834c8ff709SChao Yu 4844c8ff709SChao Yu trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx, 4854c8ff709SChao Yu dic->clen, ret); 4864c8ff709SChao Yu if (!verity) 4874c8ff709SChao Yu f2fs_free_dic(dic); 4884c8ff709SChao Yu } 4894c8ff709SChao Yu 4904c8ff709SChao Yu static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index) 4914c8ff709SChao Yu { 4924c8ff709SChao Yu if (cc->cluster_idx == NULL_CLUSTER) 4934c8ff709SChao Yu return true; 4944c8ff709SChao Yu return cc->cluster_idx == cluster_idx(cc, index); 4954c8ff709SChao Yu } 4964c8ff709SChao Yu 4974c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc) 4984c8ff709SChao Yu { 4994c8ff709SChao Yu return cc->nr_rpages == 0; 5004c8ff709SChao Yu } 5014c8ff709SChao Yu 5024c8ff709SChao Yu static bool f2fs_cluster_is_full(struct compress_ctx *cc) 5034c8ff709SChao Yu { 5044c8ff709SChao Yu return cc->cluster_size == cc->nr_rpages; 5054c8ff709SChao Yu } 5064c8ff709SChao Yu 5074c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index) 5084c8ff709SChao Yu { 5094c8ff709SChao Yu if (f2fs_cluster_is_empty(cc)) 5104c8ff709SChao Yu return true; 5114c8ff709SChao Yu return is_page_in_cluster(cc, index); 5124c8ff709SChao Yu } 5134c8ff709SChao Yu 5144c8ff709SChao Yu static bool __cluster_may_compress(struct compress_ctx *cc) 5154c8ff709SChao Yu { 5164c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 5174c8ff709SChao Yu loff_t i_size = i_size_read(cc->inode); 5184c8ff709SChao Yu unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE); 5194c8ff709SChao Yu int i; 5204c8ff709SChao Yu 5214c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 5224c8ff709SChao Yu struct page *page = cc->rpages[i]; 5234c8ff709SChao Yu 5244c8ff709SChao Yu f2fs_bug_on(sbi, !page); 5254c8ff709SChao Yu 5264c8ff709SChao Yu if (unlikely(f2fs_cp_error(sbi))) 5274c8ff709SChao Yu return false; 5284c8ff709SChao Yu if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 5294c8ff709SChao Yu return false; 5304c8ff709SChao Yu 5314c8ff709SChao Yu /* beyond EOF */ 5324c8ff709SChao Yu if (page->index >= nr_pages) 5334c8ff709SChao Yu return false; 5344c8ff709SChao Yu } 5354c8ff709SChao Yu return true; 5364c8ff709SChao Yu } 5374c8ff709SChao Yu 538*1a67cbe1SChao Yu static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr) 5394c8ff709SChao Yu { 5404c8ff709SChao Yu struct dnode_of_data dn; 5414c8ff709SChao Yu int ret; 5424c8ff709SChao Yu 5434c8ff709SChao Yu set_new_dnode(&dn, cc->inode, NULL, NULL, 0); 5444c8ff709SChao Yu ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc), 5454c8ff709SChao Yu LOOKUP_NODE); 5464c8ff709SChao Yu if (ret) { 5474c8ff709SChao Yu if (ret == -ENOENT) 5484c8ff709SChao Yu ret = 0; 5494c8ff709SChao Yu goto fail; 5504c8ff709SChao Yu } 5514c8ff709SChao Yu 5524c8ff709SChao Yu if (dn.data_blkaddr == COMPRESS_ADDR) { 5534c8ff709SChao Yu int i; 5544c8ff709SChao Yu 5554c8ff709SChao Yu ret = 1; 5564c8ff709SChao Yu for (i = 1; i < cc->cluster_size; i++) { 5574c8ff709SChao Yu block_t blkaddr; 5584c8ff709SChao Yu 559a2ced1ceSChao Yu blkaddr = data_blkaddr(dn.inode, 5604c8ff709SChao Yu dn.node_page, dn.ofs_in_node + i); 561*1a67cbe1SChao Yu if (compr) { 562*1a67cbe1SChao Yu if (__is_valid_data_blkaddr(blkaddr)) 563*1a67cbe1SChao Yu ret++; 564*1a67cbe1SChao Yu } else { 5654c8ff709SChao Yu if (blkaddr != NULL_ADDR) 5664c8ff709SChao Yu ret++; 5674c8ff709SChao Yu } 5684c8ff709SChao Yu } 569*1a67cbe1SChao Yu } 5704c8ff709SChao Yu fail: 5714c8ff709SChao Yu f2fs_put_dnode(&dn); 5724c8ff709SChao Yu return ret; 5734c8ff709SChao Yu } 5744c8ff709SChao Yu 575*1a67cbe1SChao Yu /* return # of compressed blocks in compressed cluster */ 576*1a67cbe1SChao Yu static int f2fs_compressed_blocks(struct compress_ctx *cc) 577*1a67cbe1SChao Yu { 578*1a67cbe1SChao Yu return __f2fs_cluster_blocks(cc, true); 579*1a67cbe1SChao Yu } 580*1a67cbe1SChao Yu 581*1a67cbe1SChao Yu /* return # of valid blocks in compressed cluster */ 582*1a67cbe1SChao Yu static int f2fs_cluster_blocks(struct compress_ctx *cc, bool compr) 583*1a67cbe1SChao Yu { 584*1a67cbe1SChao Yu return __f2fs_cluster_blocks(cc, false); 585*1a67cbe1SChao Yu } 586*1a67cbe1SChao Yu 5874c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index) 5884c8ff709SChao Yu { 5894c8ff709SChao Yu struct compress_ctx cc = { 5904c8ff709SChao Yu .inode = inode, 5914c8ff709SChao Yu .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 5924c8ff709SChao Yu .cluster_size = F2FS_I(inode)->i_cluster_size, 5934c8ff709SChao Yu .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size, 5944c8ff709SChao Yu }; 5954c8ff709SChao Yu 596*1a67cbe1SChao Yu return f2fs_cluster_blocks(&cc, false); 5974c8ff709SChao Yu } 5984c8ff709SChao Yu 5994c8ff709SChao Yu static bool cluster_may_compress(struct compress_ctx *cc) 6004c8ff709SChao Yu { 6014c8ff709SChao Yu if (!f2fs_compressed_file(cc->inode)) 6024c8ff709SChao Yu return false; 6034c8ff709SChao Yu if (f2fs_is_atomic_file(cc->inode)) 6044c8ff709SChao Yu return false; 6054c8ff709SChao Yu if (f2fs_is_mmap_file(cc->inode)) 6064c8ff709SChao Yu return false; 6074c8ff709SChao Yu if (!f2fs_cluster_is_full(cc)) 6084c8ff709SChao Yu return false; 6094c8ff709SChao Yu return __cluster_may_compress(cc); 6104c8ff709SChao Yu } 6114c8ff709SChao Yu 6124c8ff709SChao Yu static void set_cluster_writeback(struct compress_ctx *cc) 6134c8ff709SChao Yu { 6144c8ff709SChao Yu int i; 6154c8ff709SChao Yu 6164c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 6174c8ff709SChao Yu if (cc->rpages[i]) 6184c8ff709SChao Yu set_page_writeback(cc->rpages[i]); 6194c8ff709SChao Yu } 6204c8ff709SChao Yu } 6214c8ff709SChao Yu 6224c8ff709SChao Yu static void set_cluster_dirty(struct compress_ctx *cc) 6234c8ff709SChao Yu { 6244c8ff709SChao Yu int i; 6254c8ff709SChao Yu 6264c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) 6274c8ff709SChao Yu if (cc->rpages[i]) 6284c8ff709SChao Yu set_page_dirty(cc->rpages[i]); 6294c8ff709SChao Yu } 6304c8ff709SChao Yu 6314c8ff709SChao Yu static int prepare_compress_overwrite(struct compress_ctx *cc, 6324c8ff709SChao Yu struct page **pagep, pgoff_t index, void **fsdata) 6334c8ff709SChao Yu { 6344c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 6354c8ff709SChao Yu struct address_space *mapping = cc->inode->i_mapping; 6364c8ff709SChao Yu struct page *page; 6374c8ff709SChao Yu struct dnode_of_data dn; 6384c8ff709SChao Yu sector_t last_block_in_bio; 6394c8ff709SChao Yu unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT; 6404c8ff709SChao Yu pgoff_t start_idx = start_idx_of_cluster(cc); 6414c8ff709SChao Yu int i, ret; 6424c8ff709SChao Yu bool prealloc; 6434c8ff709SChao Yu 6444c8ff709SChao Yu retry: 645*1a67cbe1SChao Yu ret = f2fs_cluster_blocks(cc, false); 6464c8ff709SChao Yu if (ret <= 0) 6474c8ff709SChao Yu return ret; 6484c8ff709SChao Yu 6494c8ff709SChao Yu /* compressed case */ 6504c8ff709SChao Yu prealloc = (ret < cc->cluster_size); 6514c8ff709SChao Yu 6524c8ff709SChao Yu ret = f2fs_init_compress_ctx(cc); 6534c8ff709SChao Yu if (ret) 6544c8ff709SChao Yu return ret; 6554c8ff709SChao Yu 6564c8ff709SChao Yu /* keep page reference to avoid page reclaim */ 6574c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 6584c8ff709SChao Yu page = f2fs_pagecache_get_page(mapping, start_idx + i, 6594c8ff709SChao Yu fgp_flag, GFP_NOFS); 6604c8ff709SChao Yu if (!page) { 6614c8ff709SChao Yu ret = -ENOMEM; 6624c8ff709SChao Yu goto unlock_pages; 6634c8ff709SChao Yu } 6644c8ff709SChao Yu 6654c8ff709SChao Yu if (PageUptodate(page)) 6664c8ff709SChao Yu unlock_page(page); 6674c8ff709SChao Yu else 6684c8ff709SChao Yu f2fs_compress_ctx_add_page(cc, page); 6694c8ff709SChao Yu } 6704c8ff709SChao Yu 6714c8ff709SChao Yu if (!f2fs_cluster_is_empty(cc)) { 6724c8ff709SChao Yu struct bio *bio = NULL; 6734c8ff709SChao Yu 6744c8ff709SChao Yu ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size, 6750683728aSChao Yu &last_block_in_bio, false, true); 6764c8ff709SChao Yu f2fs_destroy_compress_ctx(cc); 6774c8ff709SChao Yu if (ret) 6784c8ff709SChao Yu goto release_pages; 6794c8ff709SChao Yu if (bio) 6804c8ff709SChao Yu f2fs_submit_bio(sbi, bio, DATA); 6814c8ff709SChao Yu 6824c8ff709SChao Yu ret = f2fs_init_compress_ctx(cc); 6834c8ff709SChao Yu if (ret) 6844c8ff709SChao Yu goto release_pages; 6854c8ff709SChao Yu } 6864c8ff709SChao Yu 6874c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 6884c8ff709SChao Yu f2fs_bug_on(sbi, cc->rpages[i]); 6894c8ff709SChao Yu 6904c8ff709SChao Yu page = find_lock_page(mapping, start_idx + i); 6914c8ff709SChao Yu f2fs_bug_on(sbi, !page); 6924c8ff709SChao Yu 6934c8ff709SChao Yu f2fs_wait_on_page_writeback(page, DATA, true, true); 6944c8ff709SChao Yu 6954c8ff709SChao Yu f2fs_compress_ctx_add_page(cc, page); 6964c8ff709SChao Yu f2fs_put_page(page, 0); 6974c8ff709SChao Yu 6984c8ff709SChao Yu if (!PageUptodate(page)) { 6994c8ff709SChao Yu f2fs_unlock_rpages(cc, i + 1); 7004c8ff709SChao Yu f2fs_put_rpages_mapping(cc, mapping, start_idx, 7014c8ff709SChao Yu cc->cluster_size); 7024c8ff709SChao Yu f2fs_destroy_compress_ctx(cc); 7034c8ff709SChao Yu goto retry; 7044c8ff709SChao Yu } 7054c8ff709SChao Yu } 7064c8ff709SChao Yu 7074c8ff709SChao Yu if (prealloc) { 7084c8ff709SChao Yu __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true); 7094c8ff709SChao Yu 7104c8ff709SChao Yu set_new_dnode(&dn, cc->inode, NULL, NULL, 0); 7114c8ff709SChao Yu 7124c8ff709SChao Yu for (i = cc->cluster_size - 1; i > 0; i--) { 7134c8ff709SChao Yu ret = f2fs_get_block(&dn, start_idx + i); 7144c8ff709SChao Yu if (ret) { 7154c8ff709SChao Yu i = cc->cluster_size; 7164c8ff709SChao Yu break; 7174c8ff709SChao Yu } 7184c8ff709SChao Yu 7194c8ff709SChao Yu if (dn.data_blkaddr != NEW_ADDR) 7204c8ff709SChao Yu break; 7214c8ff709SChao Yu } 7224c8ff709SChao Yu 7234c8ff709SChao Yu __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false); 7244c8ff709SChao Yu } 7254c8ff709SChao Yu 7264c8ff709SChao Yu if (likely(!ret)) { 7274c8ff709SChao Yu *fsdata = cc->rpages; 7284c8ff709SChao Yu *pagep = cc->rpages[offset_in_cluster(cc, index)]; 7294c8ff709SChao Yu return cc->cluster_size; 7304c8ff709SChao Yu } 7314c8ff709SChao Yu 7324c8ff709SChao Yu unlock_pages: 7334c8ff709SChao Yu f2fs_unlock_rpages(cc, i); 7344c8ff709SChao Yu release_pages: 7354c8ff709SChao Yu f2fs_put_rpages_mapping(cc, mapping, start_idx, i); 7364c8ff709SChao Yu f2fs_destroy_compress_ctx(cc); 7374c8ff709SChao Yu return ret; 7384c8ff709SChao Yu } 7394c8ff709SChao Yu 7404c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode, 7414c8ff709SChao Yu struct page **pagep, pgoff_t index, void **fsdata) 7424c8ff709SChao Yu { 7434c8ff709SChao Yu struct compress_ctx cc = { 7444c8ff709SChao Yu .inode = inode, 7454c8ff709SChao Yu .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 7464c8ff709SChao Yu .cluster_size = F2FS_I(inode)->i_cluster_size, 7474c8ff709SChao Yu .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size, 7484c8ff709SChao Yu .rpages = NULL, 7494c8ff709SChao Yu .nr_rpages = 0, 7504c8ff709SChao Yu }; 7514c8ff709SChao Yu 7524c8ff709SChao Yu return prepare_compress_overwrite(&cc, pagep, index, fsdata); 7534c8ff709SChao Yu } 7544c8ff709SChao Yu 7554c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata, 7564c8ff709SChao Yu pgoff_t index, unsigned copied) 7574c8ff709SChao Yu 7584c8ff709SChao Yu { 7594c8ff709SChao Yu struct compress_ctx cc = { 7604c8ff709SChao Yu .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, 7614c8ff709SChao Yu .cluster_size = F2FS_I(inode)->i_cluster_size, 7624c8ff709SChao Yu .rpages = fsdata, 7634c8ff709SChao Yu }; 7644c8ff709SChao Yu bool first_index = (index == cc.rpages[0]->index); 7654c8ff709SChao Yu 7664c8ff709SChao Yu if (copied) 7674c8ff709SChao Yu set_cluster_dirty(&cc); 7684c8ff709SChao Yu 7694c8ff709SChao Yu f2fs_put_rpages_wbc(&cc, NULL, false, 1); 7704c8ff709SChao Yu f2fs_destroy_compress_ctx(&cc); 7714c8ff709SChao Yu 7724c8ff709SChao Yu return first_index; 7734c8ff709SChao Yu } 7744c8ff709SChao Yu 7754c8ff709SChao Yu static int f2fs_write_compressed_pages(struct compress_ctx *cc, 7764c8ff709SChao Yu int *submitted, 7774c8ff709SChao Yu struct writeback_control *wbc, 7784c8ff709SChao Yu enum iostat_type io_type) 7794c8ff709SChao Yu { 7804c8ff709SChao Yu struct inode *inode = cc->inode; 7814c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 7824c8ff709SChao Yu struct f2fs_inode_info *fi = F2FS_I(inode); 7834c8ff709SChao Yu struct f2fs_io_info fio = { 7844c8ff709SChao Yu .sbi = sbi, 7854c8ff709SChao Yu .ino = cc->inode->i_ino, 7864c8ff709SChao Yu .type = DATA, 7874c8ff709SChao Yu .op = REQ_OP_WRITE, 7884c8ff709SChao Yu .op_flags = wbc_to_write_flags(wbc), 7894c8ff709SChao Yu .old_blkaddr = NEW_ADDR, 7904c8ff709SChao Yu .page = NULL, 7914c8ff709SChao Yu .encrypted_page = NULL, 7924c8ff709SChao Yu .compressed_page = NULL, 7934c8ff709SChao Yu .submitted = false, 7944c8ff709SChao Yu .io_type = io_type, 7954c8ff709SChao Yu .io_wbc = wbc, 7964c8ff709SChao Yu .encrypted = f2fs_encrypted_file(cc->inode), 7974c8ff709SChao Yu }; 7984c8ff709SChao Yu struct dnode_of_data dn; 7994c8ff709SChao Yu struct node_info ni; 8004c8ff709SChao Yu struct compress_io_ctx *cic; 8014c8ff709SChao Yu pgoff_t start_idx = start_idx_of_cluster(cc); 8024c8ff709SChao Yu unsigned int last_index = cc->cluster_size - 1; 8034c8ff709SChao Yu loff_t psize; 8044c8ff709SChao Yu int i, err; 8054c8ff709SChao Yu 806df77fbd8SChao Yu if (!f2fs_trylock_op(sbi)) 807df77fbd8SChao Yu return -EAGAIN; 8084c8ff709SChao Yu 809df77fbd8SChao Yu set_new_dnode(&dn, cc->inode, NULL, NULL, 0); 8104c8ff709SChao Yu 8114c8ff709SChao Yu err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); 8124c8ff709SChao Yu if (err) 8134c8ff709SChao Yu goto out_unlock_op; 8144c8ff709SChao Yu 8154c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 816a2ced1ceSChao Yu if (data_blkaddr(dn.inode, dn.node_page, 8174c8ff709SChao Yu dn.ofs_in_node + i) == NULL_ADDR) 8184c8ff709SChao Yu goto out_put_dnode; 8194c8ff709SChao Yu } 8204c8ff709SChao Yu 8214c8ff709SChao Yu psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT; 8224c8ff709SChao Yu 8234c8ff709SChao Yu err = f2fs_get_node_info(fio.sbi, dn.nid, &ni); 8244c8ff709SChao Yu if (err) 8254c8ff709SChao Yu goto out_put_dnode; 8264c8ff709SChao Yu 8274c8ff709SChao Yu fio.version = ni.version; 8284c8ff709SChao Yu 8294c8ff709SChao Yu cic = f2fs_kzalloc(sbi, sizeof(struct compress_io_ctx), GFP_NOFS); 8304c8ff709SChao Yu if (!cic) 8314c8ff709SChao Yu goto out_put_dnode; 8324c8ff709SChao Yu 8334c8ff709SChao Yu cic->magic = F2FS_COMPRESSED_PAGE_MAGIC; 8344c8ff709SChao Yu cic->inode = inode; 8354c8ff709SChao Yu refcount_set(&cic->ref, 1); 8364c8ff709SChao Yu cic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) << 8374c8ff709SChao Yu cc->log_cluster_size, GFP_NOFS); 8384c8ff709SChao Yu if (!cic->rpages) 8394c8ff709SChao Yu goto out_put_cic; 8404c8ff709SChao Yu 8414c8ff709SChao Yu cic->nr_rpages = cc->cluster_size; 8424c8ff709SChao Yu 8434c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 8444c8ff709SChao Yu f2fs_set_compressed_page(cc->cpages[i], inode, 8454c8ff709SChao Yu cc->rpages[i + 1]->index, 8464c8ff709SChao Yu cic, i ? &cic->ref : NULL); 8474c8ff709SChao Yu fio.compressed_page = cc->cpages[i]; 8484c8ff709SChao Yu if (fio.encrypted) { 8494c8ff709SChao Yu fio.page = cc->rpages[i + 1]; 8504c8ff709SChao Yu err = f2fs_encrypt_one_page(&fio); 8514c8ff709SChao Yu if (err) 8524c8ff709SChao Yu goto out_destroy_crypt; 8534c8ff709SChao Yu cc->cpages[i] = fio.encrypted_page; 8544c8ff709SChao Yu } 8554c8ff709SChao Yu } 8564c8ff709SChao Yu 8574c8ff709SChao Yu set_cluster_writeback(cc); 8584c8ff709SChao Yu 8594c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) 8604c8ff709SChao Yu cic->rpages[i] = cc->rpages[i]; 8614c8ff709SChao Yu 8624c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) { 8634c8ff709SChao Yu block_t blkaddr; 8644c8ff709SChao Yu 865a2ced1ceSChao Yu blkaddr = f2fs_data_blkaddr(&dn); 86695978caaSChao Yu fio.page = cc->rpages[i]; 8674c8ff709SChao Yu fio.old_blkaddr = blkaddr; 8684c8ff709SChao Yu 8694c8ff709SChao Yu /* cluster header */ 8704c8ff709SChao Yu if (i == 0) { 8714c8ff709SChao Yu if (blkaddr == COMPRESS_ADDR) 8724c8ff709SChao Yu fio.compr_blocks++; 8734c8ff709SChao Yu if (__is_valid_data_blkaddr(blkaddr)) 8744c8ff709SChao Yu f2fs_invalidate_blocks(sbi, blkaddr); 8754c8ff709SChao Yu f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR); 8764c8ff709SChao Yu goto unlock_continue; 8774c8ff709SChao Yu } 8784c8ff709SChao Yu 8794c8ff709SChao Yu if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr)) 8804c8ff709SChao Yu fio.compr_blocks++; 8814c8ff709SChao Yu 8824c8ff709SChao Yu if (i > cc->nr_cpages) { 8834c8ff709SChao Yu if (__is_valid_data_blkaddr(blkaddr)) { 8844c8ff709SChao Yu f2fs_invalidate_blocks(sbi, blkaddr); 8854c8ff709SChao Yu f2fs_update_data_blkaddr(&dn, NEW_ADDR); 8864c8ff709SChao Yu } 8874c8ff709SChao Yu goto unlock_continue; 8884c8ff709SChao Yu } 8894c8ff709SChao Yu 8904c8ff709SChao Yu f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR); 8914c8ff709SChao Yu 8924c8ff709SChao Yu if (fio.encrypted) 8934c8ff709SChao Yu fio.encrypted_page = cc->cpages[i - 1]; 8944c8ff709SChao Yu else 8954c8ff709SChao Yu fio.compressed_page = cc->cpages[i - 1]; 8964c8ff709SChao Yu 8974c8ff709SChao Yu cc->cpages[i - 1] = NULL; 8984c8ff709SChao Yu f2fs_outplace_write_data(&dn, &fio); 8994c8ff709SChao Yu (*submitted)++; 9004c8ff709SChao Yu unlock_continue: 9014c8ff709SChao Yu inode_dec_dirty_pages(cc->inode); 9024c8ff709SChao Yu unlock_page(fio.page); 9034c8ff709SChao Yu } 9044c8ff709SChao Yu 9054c8ff709SChao Yu if (fio.compr_blocks) 9064c8ff709SChao Yu f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false); 9074c8ff709SChao Yu f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true); 9084c8ff709SChao Yu 9094c8ff709SChao Yu set_inode_flag(cc->inode, FI_APPEND_WRITE); 9104c8ff709SChao Yu if (cc->cluster_idx == 0) 9114c8ff709SChao Yu set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); 9124c8ff709SChao Yu 9134c8ff709SChao Yu f2fs_put_dnode(&dn); 9144c8ff709SChao Yu f2fs_unlock_op(sbi); 9154c8ff709SChao Yu 916c10c9820SChao Yu spin_lock(&fi->i_size_lock); 9174c8ff709SChao Yu if (fi->last_disk_size < psize) 9184c8ff709SChao Yu fi->last_disk_size = psize; 919c10c9820SChao Yu spin_unlock(&fi->i_size_lock); 9204c8ff709SChao Yu 9214c8ff709SChao Yu f2fs_put_rpages(cc); 9224c8ff709SChao Yu f2fs_destroy_compress_ctx(cc); 9234c8ff709SChao Yu return 0; 9244c8ff709SChao Yu 9254c8ff709SChao Yu out_destroy_crypt: 9264c8ff709SChao Yu kfree(cic->rpages); 9274c8ff709SChao Yu 9284c8ff709SChao Yu for (--i; i >= 0; i--) 9294c8ff709SChao Yu fscrypt_finalize_bounce_page(&cc->cpages[i]); 9304c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 9314c8ff709SChao Yu if (!cc->cpages[i]) 9324c8ff709SChao Yu continue; 9334c8ff709SChao Yu f2fs_put_page(cc->cpages[i], 1); 9344c8ff709SChao Yu } 9354c8ff709SChao Yu out_put_cic: 9364c8ff709SChao Yu kfree(cic); 9374c8ff709SChao Yu out_put_dnode: 9384c8ff709SChao Yu f2fs_put_dnode(&dn); 9394c8ff709SChao Yu out_unlock_op: 9404c8ff709SChao Yu f2fs_unlock_op(sbi); 9414c8ff709SChao Yu return -EAGAIN; 9424c8ff709SChao Yu } 9434c8ff709SChao Yu 9444c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page) 9454c8ff709SChao Yu { 9464c8ff709SChao Yu struct f2fs_sb_info *sbi = bio->bi_private; 9474c8ff709SChao Yu struct compress_io_ctx *cic = 9484c8ff709SChao Yu (struct compress_io_ctx *)page_private(page); 9494c8ff709SChao Yu int i; 9504c8ff709SChao Yu 9514c8ff709SChao Yu if (unlikely(bio->bi_status)) 9524c8ff709SChao Yu mapping_set_error(cic->inode->i_mapping, -EIO); 9534c8ff709SChao Yu 9544c8ff709SChao Yu f2fs_put_compressed_page(page); 9554c8ff709SChao Yu 9564c8ff709SChao Yu dec_page_count(sbi, F2FS_WB_DATA); 9574c8ff709SChao Yu 9584c8ff709SChao Yu if (refcount_dec_not_one(&cic->ref)) 9594c8ff709SChao Yu return; 9604c8ff709SChao Yu 9614c8ff709SChao Yu for (i = 0; i < cic->nr_rpages; i++) { 9624c8ff709SChao Yu WARN_ON(!cic->rpages[i]); 9634c8ff709SChao Yu clear_cold_data(cic->rpages[i]); 9644c8ff709SChao Yu end_page_writeback(cic->rpages[i]); 9654c8ff709SChao Yu } 9664c8ff709SChao Yu 9674c8ff709SChao Yu kfree(cic->rpages); 9684c8ff709SChao Yu kfree(cic); 9694c8ff709SChao Yu } 9704c8ff709SChao Yu 9714c8ff709SChao Yu static int f2fs_write_raw_pages(struct compress_ctx *cc, 9724c8ff709SChao Yu int *submitted, 9734c8ff709SChao Yu struct writeback_control *wbc, 9744c8ff709SChao Yu enum iostat_type io_type) 9754c8ff709SChao Yu { 9764c8ff709SChao Yu struct address_space *mapping = cc->inode->i_mapping; 9774c8ff709SChao Yu int _submitted, compr_blocks, ret; 9784c8ff709SChao Yu int i = -1, err = 0; 9794c8ff709SChao Yu 9804c8ff709SChao Yu compr_blocks = f2fs_compressed_blocks(cc); 9814c8ff709SChao Yu if (compr_blocks < 0) { 9824c8ff709SChao Yu err = compr_blocks; 9834c8ff709SChao Yu goto out_err; 9844c8ff709SChao Yu } 9854c8ff709SChao Yu 9864c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 9874c8ff709SChao Yu if (!cc->rpages[i]) 9884c8ff709SChao Yu continue; 9894c8ff709SChao Yu retry_write: 9904c8ff709SChao Yu if (cc->rpages[i]->mapping != mapping) { 9914c8ff709SChao Yu unlock_page(cc->rpages[i]); 9924c8ff709SChao Yu continue; 9934c8ff709SChao Yu } 9944c8ff709SChao Yu 9954c8ff709SChao Yu BUG_ON(!PageLocked(cc->rpages[i])); 9964c8ff709SChao Yu 9974c8ff709SChao Yu ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted, 9984c8ff709SChao Yu NULL, NULL, wbc, io_type, 9994c8ff709SChao Yu compr_blocks); 10004c8ff709SChao Yu if (ret) { 10014c8ff709SChao Yu if (ret == AOP_WRITEPAGE_ACTIVATE) { 10024c8ff709SChao Yu unlock_page(cc->rpages[i]); 10034c8ff709SChao Yu ret = 0; 10044c8ff709SChao Yu } else if (ret == -EAGAIN) { 10054c8ff709SChao Yu ret = 0; 10064c8ff709SChao Yu cond_resched(); 10075df7731fSChao Yu congestion_wait(BLK_RW_ASYNC, 10085df7731fSChao Yu DEFAULT_IO_TIMEOUT); 10094c8ff709SChao Yu lock_page(cc->rpages[i]); 10104c8ff709SChao Yu clear_page_dirty_for_io(cc->rpages[i]); 10114c8ff709SChao Yu goto retry_write; 10124c8ff709SChao Yu } 10134c8ff709SChao Yu err = ret; 10144c8ff709SChao Yu goto out_fail; 10154c8ff709SChao Yu } 10164c8ff709SChao Yu 10174c8ff709SChao Yu *submitted += _submitted; 10184c8ff709SChao Yu } 10194c8ff709SChao Yu return 0; 10204c8ff709SChao Yu 10214c8ff709SChao Yu out_fail: 10224c8ff709SChao Yu /* TODO: revoke partially updated block addresses */ 10234c8ff709SChao Yu BUG_ON(compr_blocks); 10244c8ff709SChao Yu out_err: 10254c8ff709SChao Yu for (++i; i < cc->cluster_size; i++) { 10264c8ff709SChao Yu if (!cc->rpages[i]) 10274c8ff709SChao Yu continue; 10284c8ff709SChao Yu redirty_page_for_writepage(wbc, cc->rpages[i]); 10294c8ff709SChao Yu unlock_page(cc->rpages[i]); 10304c8ff709SChao Yu } 10314c8ff709SChao Yu return err; 10324c8ff709SChao Yu } 10334c8ff709SChao Yu 10344c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc, 10354c8ff709SChao Yu int *submitted, 10364c8ff709SChao Yu struct writeback_control *wbc, 10374c8ff709SChao Yu enum iostat_type io_type) 10384c8ff709SChao Yu { 10394c8ff709SChao Yu struct f2fs_inode_info *fi = F2FS_I(cc->inode); 10404c8ff709SChao Yu const struct f2fs_compress_ops *cops = 10414c8ff709SChao Yu f2fs_cops[fi->i_compress_algorithm]; 10424c8ff709SChao Yu int err; 10434c8ff709SChao Yu 10444c8ff709SChao Yu *submitted = 0; 10454c8ff709SChao Yu if (cluster_may_compress(cc)) { 10464c8ff709SChao Yu err = f2fs_compress_pages(cc); 10474c8ff709SChao Yu if (err == -EAGAIN) { 10484c8ff709SChao Yu goto write; 10494c8ff709SChao Yu } else if (err) { 10504c8ff709SChao Yu f2fs_put_rpages_wbc(cc, wbc, true, 1); 10514c8ff709SChao Yu goto destroy_out; 10524c8ff709SChao Yu } 10534c8ff709SChao Yu 10544c8ff709SChao Yu err = f2fs_write_compressed_pages(cc, submitted, 10554c8ff709SChao Yu wbc, io_type); 10564c8ff709SChao Yu cops->destroy_compress_ctx(cc); 10574c8ff709SChao Yu if (!err) 10584c8ff709SChao Yu return 0; 10594c8ff709SChao Yu f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN); 10604c8ff709SChao Yu } 10614c8ff709SChao Yu write: 10624c8ff709SChao Yu f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted); 10634c8ff709SChao Yu 10644c8ff709SChao Yu err = f2fs_write_raw_pages(cc, submitted, wbc, io_type); 10654c8ff709SChao Yu f2fs_put_rpages_wbc(cc, wbc, false, 0); 10664c8ff709SChao Yu destroy_out: 10674c8ff709SChao Yu f2fs_destroy_compress_ctx(cc); 10684c8ff709SChao Yu return err; 10694c8ff709SChao Yu } 10704c8ff709SChao Yu 10714c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc) 10724c8ff709SChao Yu { 10734c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 10744c8ff709SChao Yu struct decompress_io_ctx *dic; 10754c8ff709SChao Yu pgoff_t start_idx = start_idx_of_cluster(cc); 10764c8ff709SChao Yu int i; 10774c8ff709SChao Yu 10784c8ff709SChao Yu dic = f2fs_kzalloc(sbi, sizeof(struct decompress_io_ctx), GFP_NOFS); 10794c8ff709SChao Yu if (!dic) 10804c8ff709SChao Yu return ERR_PTR(-ENOMEM); 10814c8ff709SChao Yu 10824c8ff709SChao Yu dic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) << 10834c8ff709SChao Yu cc->log_cluster_size, GFP_NOFS); 10844c8ff709SChao Yu if (!dic->rpages) { 10854c8ff709SChao Yu kfree(dic); 10864c8ff709SChao Yu return ERR_PTR(-ENOMEM); 10874c8ff709SChao Yu } 10884c8ff709SChao Yu 10894c8ff709SChao Yu dic->magic = F2FS_COMPRESSED_PAGE_MAGIC; 10904c8ff709SChao Yu dic->inode = cc->inode; 10914c8ff709SChao Yu refcount_set(&dic->ref, 1); 10924c8ff709SChao Yu dic->cluster_idx = cc->cluster_idx; 10934c8ff709SChao Yu dic->cluster_size = cc->cluster_size; 10944c8ff709SChao Yu dic->log_cluster_size = cc->log_cluster_size; 10954c8ff709SChao Yu dic->nr_cpages = cc->nr_cpages; 10964c8ff709SChao Yu dic->failed = false; 10974c8ff709SChao Yu 10984c8ff709SChao Yu for (i = 0; i < dic->cluster_size; i++) 10994c8ff709SChao Yu dic->rpages[i] = cc->rpages[i]; 11004c8ff709SChao Yu dic->nr_rpages = cc->cluster_size; 11014c8ff709SChao Yu 11024c8ff709SChao Yu dic->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) * 11034c8ff709SChao Yu dic->nr_cpages, GFP_NOFS); 11044c8ff709SChao Yu if (!dic->cpages) 11054c8ff709SChao Yu goto out_free; 11064c8ff709SChao Yu 11074c8ff709SChao Yu for (i = 0; i < dic->nr_cpages; i++) { 11084c8ff709SChao Yu struct page *page; 11094c8ff709SChao Yu 11104c8ff709SChao Yu page = f2fs_grab_page(); 11114c8ff709SChao Yu if (!page) 11124c8ff709SChao Yu goto out_free; 11134c8ff709SChao Yu 11144c8ff709SChao Yu f2fs_set_compressed_page(page, cc->inode, 11154c8ff709SChao Yu start_idx + i + 1, 11164c8ff709SChao Yu dic, i ? &dic->ref : NULL); 11174c8ff709SChao Yu dic->cpages[i] = page; 11184c8ff709SChao Yu } 11194c8ff709SChao Yu 11204c8ff709SChao Yu dic->tpages = f2fs_kzalloc(sbi, sizeof(struct page *) * 11214c8ff709SChao Yu dic->cluster_size, GFP_NOFS); 11224c8ff709SChao Yu if (!dic->tpages) 11234c8ff709SChao Yu goto out_free; 11244c8ff709SChao Yu 11254c8ff709SChao Yu for (i = 0; i < dic->cluster_size; i++) { 11264c8ff709SChao Yu if (cc->rpages[i]) 11274c8ff709SChao Yu continue; 11284c8ff709SChao Yu 11294c8ff709SChao Yu dic->tpages[i] = f2fs_grab_page(); 11304c8ff709SChao Yu if (!dic->tpages[i]) 11314c8ff709SChao Yu goto out_free; 11324c8ff709SChao Yu } 11334c8ff709SChao Yu 11344c8ff709SChao Yu for (i = 0; i < dic->cluster_size; i++) { 11354c8ff709SChao Yu if (dic->tpages[i]) 11364c8ff709SChao Yu continue; 11374c8ff709SChao Yu dic->tpages[i] = cc->rpages[i]; 11384c8ff709SChao Yu } 11394c8ff709SChao Yu 11404c8ff709SChao Yu return dic; 11414c8ff709SChao Yu 11424c8ff709SChao Yu out_free: 11434c8ff709SChao Yu f2fs_free_dic(dic); 11444c8ff709SChao Yu return ERR_PTR(-ENOMEM); 11454c8ff709SChao Yu } 11464c8ff709SChao Yu 11474c8ff709SChao Yu void f2fs_free_dic(struct decompress_io_ctx *dic) 11484c8ff709SChao Yu { 11494c8ff709SChao Yu int i; 11504c8ff709SChao Yu 11514c8ff709SChao Yu if (dic->tpages) { 11524c8ff709SChao Yu for (i = 0; i < dic->cluster_size; i++) { 11534c8ff709SChao Yu if (dic->rpages[i]) 11544c8ff709SChao Yu continue; 115561fbae2bSChao Yu unlock_page(dic->tpages[i]); 115661fbae2bSChao Yu put_page(dic->tpages[i]); 11574c8ff709SChao Yu } 11584c8ff709SChao Yu kfree(dic->tpages); 11594c8ff709SChao Yu } 11604c8ff709SChao Yu 11614c8ff709SChao Yu if (dic->cpages) { 11624c8ff709SChao Yu for (i = 0; i < dic->nr_cpages; i++) { 11634c8ff709SChao Yu if (!dic->cpages[i]) 11644c8ff709SChao Yu continue; 11654c8ff709SChao Yu f2fs_put_compressed_page(dic->cpages[i]); 11664c8ff709SChao Yu } 11674c8ff709SChao Yu kfree(dic->cpages); 11684c8ff709SChao Yu } 11694c8ff709SChao Yu 11704c8ff709SChao Yu kfree(dic->rpages); 11714c8ff709SChao Yu kfree(dic); 11724c8ff709SChao Yu } 11734c8ff709SChao Yu 11744c8ff709SChao Yu void f2fs_decompress_end_io(struct page **rpages, 11754c8ff709SChao Yu unsigned int cluster_size, bool err, bool verity) 11764c8ff709SChao Yu { 11774c8ff709SChao Yu int i; 11784c8ff709SChao Yu 11794c8ff709SChao Yu for (i = 0; i < cluster_size; i++) { 11804c8ff709SChao Yu struct page *rpage = rpages[i]; 11814c8ff709SChao Yu 11824c8ff709SChao Yu if (!rpage) 11834c8ff709SChao Yu continue; 11844c8ff709SChao Yu 11854c8ff709SChao Yu if (err || PageError(rpage)) { 11864c8ff709SChao Yu ClearPageUptodate(rpage); 11874c8ff709SChao Yu ClearPageError(rpage); 11884c8ff709SChao Yu } else { 11894c8ff709SChao Yu if (!verity || fsverity_verify_page(rpage)) 11904c8ff709SChao Yu SetPageUptodate(rpage); 11914c8ff709SChao Yu else 11924c8ff709SChao Yu SetPageError(rpage); 11934c8ff709SChao Yu } 11944c8ff709SChao Yu unlock_page(rpage); 11954c8ff709SChao Yu } 11964c8ff709SChao Yu } 1197