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, 55*887347a0SChao Yu struct inode *inode, pgoff_t index, void *data) 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 } 644c8ff709SChao Yu 654c8ff709SChao Yu static void f2fs_put_compressed_page(struct page *page) 664c8ff709SChao Yu { 674c8ff709SChao Yu set_page_private(page, (unsigned long)NULL); 684c8ff709SChao Yu ClearPagePrivate(page); 694c8ff709SChao Yu page->mapping = NULL; 704c8ff709SChao Yu unlock_page(page); 714c8ff709SChao Yu put_page(page); 724c8ff709SChao Yu } 734c8ff709SChao Yu 744c8ff709SChao Yu static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock) 754c8ff709SChao Yu { 764c8ff709SChao Yu int i; 774c8ff709SChao Yu 784c8ff709SChao Yu for (i = 0; i < len; i++) { 794c8ff709SChao Yu if (!cc->rpages[i]) 804c8ff709SChao Yu continue; 814c8ff709SChao Yu if (unlock) 824c8ff709SChao Yu unlock_page(cc->rpages[i]); 834c8ff709SChao Yu else 844c8ff709SChao Yu put_page(cc->rpages[i]); 854c8ff709SChao Yu } 864c8ff709SChao Yu } 874c8ff709SChao Yu 884c8ff709SChao Yu static void f2fs_put_rpages(struct compress_ctx *cc) 894c8ff709SChao Yu { 904c8ff709SChao Yu f2fs_drop_rpages(cc, cc->cluster_size, false); 914c8ff709SChao Yu } 924c8ff709SChao Yu 934c8ff709SChao Yu static void f2fs_unlock_rpages(struct compress_ctx *cc, int len) 944c8ff709SChao Yu { 954c8ff709SChao Yu f2fs_drop_rpages(cc, len, true); 964c8ff709SChao Yu } 974c8ff709SChao Yu 984c8ff709SChao Yu static void f2fs_put_rpages_mapping(struct compress_ctx *cc, 994c8ff709SChao Yu struct address_space *mapping, 1004c8ff709SChao Yu pgoff_t start, int len) 1014c8ff709SChao Yu { 1024c8ff709SChao Yu int i; 1034c8ff709SChao Yu 1044c8ff709SChao Yu for (i = 0; i < len; i++) { 1054c8ff709SChao Yu struct page *page = find_get_page(mapping, start + i); 1064c8ff709SChao Yu 1074c8ff709SChao Yu put_page(page); 1084c8ff709SChao Yu put_page(page); 1094c8ff709SChao Yu } 1104c8ff709SChao Yu } 1114c8ff709SChao Yu 1124c8ff709SChao Yu static void f2fs_put_rpages_wbc(struct compress_ctx *cc, 1134c8ff709SChao Yu struct writeback_control *wbc, bool redirty, int unlock) 1144c8ff709SChao Yu { 1154c8ff709SChao Yu unsigned int i; 1164c8ff709SChao Yu 1174c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 1184c8ff709SChao Yu if (!cc->rpages[i]) 1194c8ff709SChao Yu continue; 1204c8ff709SChao Yu if (redirty) 1214c8ff709SChao Yu redirty_page_for_writepage(wbc, cc->rpages[i]); 1224c8ff709SChao Yu f2fs_put_page(cc->rpages[i], unlock); 1234c8ff709SChao Yu } 1244c8ff709SChao Yu } 1254c8ff709SChao Yu 1264c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page) 1274c8ff709SChao Yu { 1284c8ff709SChao Yu return ((struct compress_io_ctx *)page_private(page))->rpages[0]; 1294c8ff709SChao Yu } 1304c8ff709SChao Yu 1314c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc) 1324c8ff709SChao Yu { 1334c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 1344c8ff709SChao Yu 1354c8ff709SChao Yu if (cc->nr_rpages) 1364c8ff709SChao Yu return 0; 1374c8ff709SChao Yu 1384c8ff709SChao Yu cc->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) << 1394c8ff709SChao Yu cc->log_cluster_size, GFP_NOFS); 1404c8ff709SChao Yu return cc->rpages ? 0 : -ENOMEM; 1414c8ff709SChao Yu } 1424c8ff709SChao Yu 1434c8ff709SChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc) 1444c8ff709SChao Yu { 1454c8ff709SChao Yu kfree(cc->rpages); 1464c8ff709SChao Yu cc->rpages = NULL; 1474c8ff709SChao Yu cc->nr_rpages = 0; 1484c8ff709SChao Yu cc->nr_cpages = 0; 1494c8ff709SChao Yu cc->cluster_idx = NULL_CLUSTER; 1504c8ff709SChao Yu } 1514c8ff709SChao Yu 1524c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page) 1534c8ff709SChao Yu { 1544c8ff709SChao Yu unsigned int cluster_ofs; 1554c8ff709SChao Yu 1564c8ff709SChao Yu if (!f2fs_cluster_can_merge_page(cc, page->index)) 1574c8ff709SChao Yu f2fs_bug_on(F2FS_I_SB(cc->inode), 1); 1584c8ff709SChao Yu 1594c8ff709SChao Yu cluster_ofs = offset_in_cluster(cc, page->index); 1604c8ff709SChao Yu cc->rpages[cluster_ofs] = page; 1614c8ff709SChao Yu cc->nr_rpages++; 1624c8ff709SChao Yu cc->cluster_idx = cluster_idx(cc, page->index); 1634c8ff709SChao Yu } 1644c8ff709SChao Yu 1654c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO 1664c8ff709SChao Yu static int lzo_init_compress_ctx(struct compress_ctx *cc) 1674c8ff709SChao Yu { 1684c8ff709SChao Yu cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), 1694c8ff709SChao Yu LZO1X_MEM_COMPRESS, GFP_NOFS); 1704c8ff709SChao Yu if (!cc->private) 1714c8ff709SChao Yu return -ENOMEM; 1724c8ff709SChao Yu 1734c8ff709SChao Yu cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size); 1744c8ff709SChao Yu return 0; 1754c8ff709SChao Yu } 1764c8ff709SChao Yu 1774c8ff709SChao Yu static void lzo_destroy_compress_ctx(struct compress_ctx *cc) 1784c8ff709SChao Yu { 1794c8ff709SChao Yu kvfree(cc->private); 1804c8ff709SChao Yu cc->private = NULL; 1814c8ff709SChao Yu } 1824c8ff709SChao Yu 1834c8ff709SChao Yu static int lzo_compress_pages(struct compress_ctx *cc) 1844c8ff709SChao Yu { 1854c8ff709SChao Yu int ret; 1864c8ff709SChao Yu 1874c8ff709SChao Yu ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata, 1884c8ff709SChao Yu &cc->clen, cc->private); 1894c8ff709SChao Yu if (ret != LZO_E_OK) { 1904c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n", 1914c8ff709SChao Yu KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret); 1924c8ff709SChao Yu return -EIO; 1934c8ff709SChao Yu } 1944c8ff709SChao Yu return 0; 1954c8ff709SChao Yu } 1964c8ff709SChao Yu 1974c8ff709SChao Yu static int lzo_decompress_pages(struct decompress_io_ctx *dic) 1984c8ff709SChao Yu { 1994c8ff709SChao Yu int ret; 2004c8ff709SChao Yu 2014c8ff709SChao Yu ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen, 2024c8ff709SChao Yu dic->rbuf, &dic->rlen); 2034c8ff709SChao Yu if (ret != LZO_E_OK) { 2044c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n", 2054c8ff709SChao Yu KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret); 2064c8ff709SChao Yu return -EIO; 2074c8ff709SChao Yu } 2084c8ff709SChao Yu 2094c8ff709SChao Yu if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) { 2104c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, " 2114c8ff709SChao Yu "expected:%lu\n", KERN_ERR, 2124c8ff709SChao Yu F2FS_I_SB(dic->inode)->sb->s_id, 2134c8ff709SChao Yu dic->rlen, 2144c8ff709SChao Yu PAGE_SIZE << dic->log_cluster_size); 2154c8ff709SChao Yu return -EIO; 2164c8ff709SChao Yu } 2174c8ff709SChao Yu return 0; 2184c8ff709SChao Yu } 2194c8ff709SChao Yu 2204c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lzo_ops = { 2214c8ff709SChao Yu .init_compress_ctx = lzo_init_compress_ctx, 2224c8ff709SChao Yu .destroy_compress_ctx = lzo_destroy_compress_ctx, 2234c8ff709SChao Yu .compress_pages = lzo_compress_pages, 2244c8ff709SChao Yu .decompress_pages = lzo_decompress_pages, 2254c8ff709SChao Yu }; 2264c8ff709SChao Yu #endif 2274c8ff709SChao Yu 2284c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4 2294c8ff709SChao Yu static int lz4_init_compress_ctx(struct compress_ctx *cc) 2304c8ff709SChao Yu { 2314c8ff709SChao Yu cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), 2324c8ff709SChao Yu LZ4_MEM_COMPRESS, GFP_NOFS); 2334c8ff709SChao Yu if (!cc->private) 2344c8ff709SChao Yu return -ENOMEM; 2354c8ff709SChao Yu 2364c8ff709SChao Yu cc->clen = LZ4_compressBound(PAGE_SIZE << cc->log_cluster_size); 2374c8ff709SChao Yu return 0; 2384c8ff709SChao Yu } 2394c8ff709SChao Yu 2404c8ff709SChao Yu static void lz4_destroy_compress_ctx(struct compress_ctx *cc) 2414c8ff709SChao Yu { 2424c8ff709SChao Yu kvfree(cc->private); 2434c8ff709SChao Yu cc->private = NULL; 2444c8ff709SChao Yu } 2454c8ff709SChao Yu 2464c8ff709SChao Yu static int lz4_compress_pages(struct compress_ctx *cc) 2474c8ff709SChao Yu { 2484c8ff709SChao Yu int len; 2494c8ff709SChao Yu 2504c8ff709SChao Yu len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen, 2514c8ff709SChao Yu cc->clen, cc->private); 2524c8ff709SChao Yu if (!len) { 2534c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lz4 compress failed\n", 2544c8ff709SChao Yu KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id); 2554c8ff709SChao Yu return -EIO; 2564c8ff709SChao Yu } 2574c8ff709SChao Yu cc->clen = len; 2584c8ff709SChao Yu return 0; 2594c8ff709SChao Yu } 2604c8ff709SChao Yu 2614c8ff709SChao Yu static int lz4_decompress_pages(struct decompress_io_ctx *dic) 2624c8ff709SChao Yu { 2634c8ff709SChao Yu int ret; 2644c8ff709SChao Yu 2654c8ff709SChao Yu ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf, 2664c8ff709SChao Yu dic->clen, dic->rlen); 2674c8ff709SChao Yu if (ret < 0) { 2684c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n", 2694c8ff709SChao Yu KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret); 2704c8ff709SChao Yu return -EIO; 2714c8ff709SChao Yu } 2724c8ff709SChao Yu 2734c8ff709SChao Yu if (ret != PAGE_SIZE << dic->log_cluster_size) { 2744c8ff709SChao Yu printk_ratelimited("%sF2FS-fs (%s): lz4 invalid rlen:%zu, " 2754c8ff709SChao Yu "expected:%lu\n", KERN_ERR, 2764c8ff709SChao Yu F2FS_I_SB(dic->inode)->sb->s_id, 2774c8ff709SChao Yu dic->rlen, 2784c8ff709SChao Yu PAGE_SIZE << dic->log_cluster_size); 2794c8ff709SChao Yu return -EIO; 2804c8ff709SChao Yu } 2814c8ff709SChao Yu return 0; 2824c8ff709SChao Yu } 2834c8ff709SChao Yu 2844c8ff709SChao Yu static const struct f2fs_compress_ops f2fs_lz4_ops = { 2854c8ff709SChao Yu .init_compress_ctx = lz4_init_compress_ctx, 2864c8ff709SChao Yu .destroy_compress_ctx = lz4_destroy_compress_ctx, 2874c8ff709SChao Yu .compress_pages = lz4_compress_pages, 2884c8ff709SChao Yu .decompress_pages = lz4_decompress_pages, 2894c8ff709SChao Yu }; 2904c8ff709SChao Yu #endif 2914c8ff709SChao Yu 2924c8ff709SChao Yu static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = { 2934c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZO 2944c8ff709SChao Yu &f2fs_lzo_ops, 2954c8ff709SChao Yu #else 2964c8ff709SChao Yu NULL, 2974c8ff709SChao Yu #endif 2984c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_LZ4 2994c8ff709SChao Yu &f2fs_lz4_ops, 3004c8ff709SChao Yu #else 3014c8ff709SChao Yu NULL, 3024c8ff709SChao Yu #endif 3034c8ff709SChao Yu }; 3044c8ff709SChao Yu 3054c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode) 3064c8ff709SChao Yu { 3074c8ff709SChao Yu if (!f2fs_compressed_file(inode)) 3084c8ff709SChao Yu return true; 3094c8ff709SChao Yu return f2fs_cops[F2FS_I(inode)->i_compress_algorithm]; 3104c8ff709SChao Yu } 3114c8ff709SChao Yu 3124c8ff709SChao Yu static struct page *f2fs_grab_page(void) 3134c8ff709SChao Yu { 3144c8ff709SChao Yu struct page *page; 3154c8ff709SChao Yu 3164c8ff709SChao Yu page = alloc_page(GFP_NOFS); 3174c8ff709SChao Yu if (!page) 3184c8ff709SChao Yu return NULL; 3194c8ff709SChao Yu lock_page(page); 3204c8ff709SChao Yu return page; 3214c8ff709SChao Yu } 3224c8ff709SChao Yu 3234c8ff709SChao Yu static int f2fs_compress_pages(struct compress_ctx *cc) 3244c8ff709SChao Yu { 3254c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 3264c8ff709SChao Yu struct f2fs_inode_info *fi = F2FS_I(cc->inode); 3274c8ff709SChao Yu const struct f2fs_compress_ops *cops = 3284c8ff709SChao Yu f2fs_cops[fi->i_compress_algorithm]; 3294c8ff709SChao Yu unsigned int max_len, nr_cpages; 3304c8ff709SChao Yu int i, ret; 3314c8ff709SChao Yu 3324c8ff709SChao Yu trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx, 3334c8ff709SChao Yu cc->cluster_size, fi->i_compress_algorithm); 3344c8ff709SChao Yu 3354c8ff709SChao Yu ret = cops->init_compress_ctx(cc); 3364c8ff709SChao Yu if (ret) 3374c8ff709SChao Yu goto out; 3384c8ff709SChao Yu 3394c8ff709SChao Yu max_len = COMPRESS_HEADER_SIZE + cc->clen; 3404c8ff709SChao Yu cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE); 3414c8ff709SChao Yu 3424c8ff709SChao Yu cc->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) * 3434c8ff709SChao Yu cc->nr_cpages, GFP_NOFS); 3444c8ff709SChao Yu if (!cc->cpages) { 3454c8ff709SChao Yu ret = -ENOMEM; 3464c8ff709SChao Yu goto destroy_compress_ctx; 3474c8ff709SChao Yu } 3484c8ff709SChao Yu 3494c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 3504c8ff709SChao Yu cc->cpages[i] = f2fs_grab_page(); 3514c8ff709SChao Yu if (!cc->cpages[i]) { 3524c8ff709SChao Yu ret = -ENOMEM; 3534c8ff709SChao Yu goto out_free_cpages; 3544c8ff709SChao Yu } 3554c8ff709SChao Yu } 3564c8ff709SChao Yu 3574c8ff709SChao Yu cc->rbuf = vmap(cc->rpages, cc->cluster_size, VM_MAP, PAGE_KERNEL_RO); 3584c8ff709SChao Yu if (!cc->rbuf) { 3594c8ff709SChao Yu ret = -ENOMEM; 3604c8ff709SChao Yu goto out_free_cpages; 3614c8ff709SChao Yu } 3624c8ff709SChao Yu 3634c8ff709SChao Yu cc->cbuf = vmap(cc->cpages, cc->nr_cpages, VM_MAP, PAGE_KERNEL); 3644c8ff709SChao Yu if (!cc->cbuf) { 3654c8ff709SChao Yu ret = -ENOMEM; 3664c8ff709SChao Yu goto out_vunmap_rbuf; 3674c8ff709SChao Yu } 3684c8ff709SChao Yu 3694c8ff709SChao Yu ret = cops->compress_pages(cc); 3704c8ff709SChao Yu if (ret) 3714c8ff709SChao Yu goto out_vunmap_cbuf; 3724c8ff709SChao Yu 3734c8ff709SChao Yu max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE; 3744c8ff709SChao Yu 3754c8ff709SChao Yu if (cc->clen > max_len) { 3764c8ff709SChao Yu ret = -EAGAIN; 3774c8ff709SChao Yu goto out_vunmap_cbuf; 3784c8ff709SChao Yu } 3794c8ff709SChao Yu 3804c8ff709SChao Yu cc->cbuf->clen = cpu_to_le32(cc->clen); 3814c8ff709SChao Yu 3824c8ff709SChao Yu for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++) 3834c8ff709SChao Yu cc->cbuf->reserved[i] = cpu_to_le32(0); 3844c8ff709SChao Yu 3857fa6d598SEric Biggers nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE); 3867fa6d598SEric Biggers 3877fa6d598SEric Biggers /* zero out any unused part of the last page */ 3887fa6d598SEric Biggers memset(&cc->cbuf->cdata[cc->clen], 0, 3897fa6d598SEric Biggers (nr_cpages * PAGE_SIZE) - (cc->clen + COMPRESS_HEADER_SIZE)); 3907fa6d598SEric Biggers 3914c8ff709SChao Yu vunmap(cc->cbuf); 3924c8ff709SChao Yu vunmap(cc->rbuf); 3934c8ff709SChao Yu 3944c8ff709SChao Yu for (i = nr_cpages; i < cc->nr_cpages; i++) { 3954c8ff709SChao Yu f2fs_put_compressed_page(cc->cpages[i]); 3964c8ff709SChao Yu cc->cpages[i] = NULL; 3974c8ff709SChao Yu } 3984c8ff709SChao Yu 3994c8ff709SChao Yu cc->nr_cpages = nr_cpages; 4004c8ff709SChao Yu 4014c8ff709SChao Yu trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx, 4024c8ff709SChao Yu cc->clen, ret); 4034c8ff709SChao Yu return 0; 4044c8ff709SChao Yu 4054c8ff709SChao Yu out_vunmap_cbuf: 4064c8ff709SChao Yu vunmap(cc->cbuf); 4074c8ff709SChao Yu out_vunmap_rbuf: 4084c8ff709SChao Yu vunmap(cc->rbuf); 4094c8ff709SChao Yu out_free_cpages: 4104c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 4114c8ff709SChao Yu if (cc->cpages[i]) 4124c8ff709SChao Yu f2fs_put_compressed_page(cc->cpages[i]); 4134c8ff709SChao Yu } 4144c8ff709SChao Yu kfree(cc->cpages); 4154c8ff709SChao Yu cc->cpages = NULL; 4164c8ff709SChao Yu destroy_compress_ctx: 4174c8ff709SChao Yu cops->destroy_compress_ctx(cc); 4184c8ff709SChao Yu out: 4194c8ff709SChao Yu trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx, 4204c8ff709SChao Yu cc->clen, ret); 4214c8ff709SChao Yu return ret; 4224c8ff709SChao Yu } 4234c8ff709SChao Yu 4244c8ff709SChao Yu void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity) 4254c8ff709SChao Yu { 4264c8ff709SChao Yu struct decompress_io_ctx *dic = 4274c8ff709SChao Yu (struct decompress_io_ctx *)page_private(page); 4284c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode); 4294c8ff709SChao Yu struct f2fs_inode_info *fi= F2FS_I(dic->inode); 4304c8ff709SChao Yu const struct f2fs_compress_ops *cops = 4314c8ff709SChao Yu f2fs_cops[fi->i_compress_algorithm]; 4324c8ff709SChao Yu int ret; 4334c8ff709SChao Yu 4344c8ff709SChao Yu dec_page_count(sbi, F2FS_RD_DATA); 4354c8ff709SChao Yu 4364c8ff709SChao Yu if (bio->bi_status || PageError(page)) 4374c8ff709SChao Yu dic->failed = true; 4384c8ff709SChao Yu 4394c8ff709SChao Yu if (refcount_dec_not_one(&dic->ref)) 4404c8ff709SChao Yu return; 4414c8ff709SChao Yu 4424c8ff709SChao Yu trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx, 4434c8ff709SChao Yu dic->cluster_size, fi->i_compress_algorithm); 4444c8ff709SChao Yu 4454c8ff709SChao Yu /* submit partial compressed pages */ 4464c8ff709SChao Yu if (dic->failed) { 4474c8ff709SChao Yu ret = -EIO; 4484c8ff709SChao Yu goto out_free_dic; 4494c8ff709SChao Yu } 4504c8ff709SChao Yu 4514c8ff709SChao Yu dic->rbuf = vmap(dic->tpages, dic->cluster_size, VM_MAP, PAGE_KERNEL); 4524c8ff709SChao Yu if (!dic->rbuf) { 4534c8ff709SChao Yu ret = -ENOMEM; 4544c8ff709SChao Yu goto out_free_dic; 4554c8ff709SChao Yu } 4564c8ff709SChao Yu 4574c8ff709SChao Yu dic->cbuf = vmap(dic->cpages, dic->nr_cpages, VM_MAP, PAGE_KERNEL_RO); 4584c8ff709SChao Yu if (!dic->cbuf) { 4594c8ff709SChao Yu ret = -ENOMEM; 4604c8ff709SChao Yu goto out_vunmap_rbuf; 4614c8ff709SChao Yu } 4624c8ff709SChao Yu 4634c8ff709SChao Yu dic->clen = le32_to_cpu(dic->cbuf->clen); 4644c8ff709SChao Yu dic->rlen = PAGE_SIZE << dic->log_cluster_size; 4654c8ff709SChao Yu 4664c8ff709SChao Yu if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) { 4674c8ff709SChao Yu ret = -EFSCORRUPTED; 4684c8ff709SChao Yu goto out_vunmap_cbuf; 4694c8ff709SChao Yu } 4704c8ff709SChao Yu 4714c8ff709SChao Yu ret = cops->decompress_pages(dic); 4724c8ff709SChao Yu 4734c8ff709SChao Yu out_vunmap_cbuf: 4744c8ff709SChao Yu vunmap(dic->cbuf); 4754c8ff709SChao Yu out_vunmap_rbuf: 4764c8ff709SChao Yu vunmap(dic->rbuf); 4774c8ff709SChao Yu out_free_dic: 47879bbefb1SChao Yu if (verity) 479*887347a0SChao Yu refcount_set(&dic->ref, dic->nr_cpages); 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 5381a67cbe1SChao 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); 5611a67cbe1SChao Yu if (compr) { 5621a67cbe1SChao Yu if (__is_valid_data_blkaddr(blkaddr)) 5631a67cbe1SChao Yu ret++; 5641a67cbe1SChao Yu } else { 5654c8ff709SChao Yu if (blkaddr != NULL_ADDR) 5664c8ff709SChao Yu ret++; 5674c8ff709SChao Yu } 5684c8ff709SChao Yu } 5691a67cbe1SChao Yu } 5704c8ff709SChao Yu fail: 5714c8ff709SChao Yu f2fs_put_dnode(&dn); 5724c8ff709SChao Yu return ret; 5734c8ff709SChao Yu } 5744c8ff709SChao Yu 5751a67cbe1SChao Yu /* return # of compressed blocks in compressed cluster */ 5761a67cbe1SChao Yu static int f2fs_compressed_blocks(struct compress_ctx *cc) 5771a67cbe1SChao Yu { 5781a67cbe1SChao Yu return __f2fs_cluster_blocks(cc, true); 5791a67cbe1SChao Yu } 5801a67cbe1SChao Yu 5811a67cbe1SChao Yu /* return # of valid blocks in compressed cluster */ 5821a67cbe1SChao Yu static int f2fs_cluster_blocks(struct compress_ctx *cc, bool compr) 5831a67cbe1SChao Yu { 5841a67cbe1SChao Yu return __f2fs_cluster_blocks(cc, false); 5851a67cbe1SChao Yu } 5861a67cbe1SChao 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 5961a67cbe1SChao 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: 6451a67cbe1SChao 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; 835*887347a0SChao Yu refcount_set(&cic->ref, cc->nr_cpages); 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, 845*887347a0SChao Yu cc->rpages[i + 1]->index, cic); 8464c8ff709SChao Yu fio.compressed_page = cc->cpages[i]; 8474c8ff709SChao Yu if (fio.encrypted) { 8484c8ff709SChao Yu fio.page = cc->rpages[i + 1]; 8494c8ff709SChao Yu err = f2fs_encrypt_one_page(&fio); 8504c8ff709SChao Yu if (err) 8514c8ff709SChao Yu goto out_destroy_crypt; 8524c8ff709SChao Yu cc->cpages[i] = fio.encrypted_page; 8534c8ff709SChao Yu } 8544c8ff709SChao Yu } 8554c8ff709SChao Yu 8564c8ff709SChao Yu set_cluster_writeback(cc); 8574c8ff709SChao Yu 8584c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) 8594c8ff709SChao Yu cic->rpages[i] = cc->rpages[i]; 8604c8ff709SChao Yu 8614c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) { 8624c8ff709SChao Yu block_t blkaddr; 8634c8ff709SChao Yu 864a2ced1ceSChao Yu blkaddr = f2fs_data_blkaddr(&dn); 86595978caaSChao Yu fio.page = cc->rpages[i]; 8664c8ff709SChao Yu fio.old_blkaddr = blkaddr; 8674c8ff709SChao Yu 8684c8ff709SChao Yu /* cluster header */ 8694c8ff709SChao Yu if (i == 0) { 8704c8ff709SChao Yu if (blkaddr == COMPRESS_ADDR) 8714c8ff709SChao Yu fio.compr_blocks++; 8724c8ff709SChao Yu if (__is_valid_data_blkaddr(blkaddr)) 8734c8ff709SChao Yu f2fs_invalidate_blocks(sbi, blkaddr); 8744c8ff709SChao Yu f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR); 8754c8ff709SChao Yu goto unlock_continue; 8764c8ff709SChao Yu } 8774c8ff709SChao Yu 8784c8ff709SChao Yu if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr)) 8794c8ff709SChao Yu fio.compr_blocks++; 8804c8ff709SChao Yu 8814c8ff709SChao Yu if (i > cc->nr_cpages) { 8824c8ff709SChao Yu if (__is_valid_data_blkaddr(blkaddr)) { 8834c8ff709SChao Yu f2fs_invalidate_blocks(sbi, blkaddr); 8844c8ff709SChao Yu f2fs_update_data_blkaddr(&dn, NEW_ADDR); 8854c8ff709SChao Yu } 8864c8ff709SChao Yu goto unlock_continue; 8874c8ff709SChao Yu } 8884c8ff709SChao Yu 8894c8ff709SChao Yu f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR); 8904c8ff709SChao Yu 8914c8ff709SChao Yu if (fio.encrypted) 8924c8ff709SChao Yu fio.encrypted_page = cc->cpages[i - 1]; 8934c8ff709SChao Yu else 8944c8ff709SChao Yu fio.compressed_page = cc->cpages[i - 1]; 8954c8ff709SChao Yu 8964c8ff709SChao Yu cc->cpages[i - 1] = NULL; 8974c8ff709SChao Yu f2fs_outplace_write_data(&dn, &fio); 8984c8ff709SChao Yu (*submitted)++; 8994c8ff709SChao Yu unlock_continue: 9004c8ff709SChao Yu inode_dec_dirty_pages(cc->inode); 9014c8ff709SChao Yu unlock_page(fio.page); 9024c8ff709SChao Yu } 9034c8ff709SChao Yu 9044c8ff709SChao Yu if (fio.compr_blocks) 9054c8ff709SChao Yu f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false); 9064c8ff709SChao Yu f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true); 9074c8ff709SChao Yu 9084c8ff709SChao Yu set_inode_flag(cc->inode, FI_APPEND_WRITE); 9094c8ff709SChao Yu if (cc->cluster_idx == 0) 9104c8ff709SChao Yu set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); 9114c8ff709SChao Yu 9124c8ff709SChao Yu f2fs_put_dnode(&dn); 9134c8ff709SChao Yu f2fs_unlock_op(sbi); 9144c8ff709SChao Yu 915c10c9820SChao Yu spin_lock(&fi->i_size_lock); 9164c8ff709SChao Yu if (fi->last_disk_size < psize) 9174c8ff709SChao Yu fi->last_disk_size = psize; 918c10c9820SChao Yu spin_unlock(&fi->i_size_lock); 9194c8ff709SChao Yu 9204c8ff709SChao Yu f2fs_put_rpages(cc); 9214c8ff709SChao Yu f2fs_destroy_compress_ctx(cc); 9224c8ff709SChao Yu return 0; 9234c8ff709SChao Yu 9244c8ff709SChao Yu out_destroy_crypt: 9254c8ff709SChao Yu kfree(cic->rpages); 9264c8ff709SChao Yu 9274c8ff709SChao Yu for (--i; i >= 0; i--) 9284c8ff709SChao Yu fscrypt_finalize_bounce_page(&cc->cpages[i]); 9294c8ff709SChao Yu for (i = 0; i < cc->nr_cpages; i++) { 9304c8ff709SChao Yu if (!cc->cpages[i]) 9314c8ff709SChao Yu continue; 9324c8ff709SChao Yu f2fs_put_page(cc->cpages[i], 1); 9334c8ff709SChao Yu } 9344c8ff709SChao Yu out_put_cic: 9354c8ff709SChao Yu kfree(cic); 9364c8ff709SChao Yu out_put_dnode: 9374c8ff709SChao Yu f2fs_put_dnode(&dn); 9384c8ff709SChao Yu out_unlock_op: 9394c8ff709SChao Yu f2fs_unlock_op(sbi); 9404c8ff709SChao Yu return -EAGAIN; 9414c8ff709SChao Yu } 9424c8ff709SChao Yu 9434c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page) 9444c8ff709SChao Yu { 9454c8ff709SChao Yu struct f2fs_sb_info *sbi = bio->bi_private; 9464c8ff709SChao Yu struct compress_io_ctx *cic = 9474c8ff709SChao Yu (struct compress_io_ctx *)page_private(page); 9484c8ff709SChao Yu int i; 9494c8ff709SChao Yu 9504c8ff709SChao Yu if (unlikely(bio->bi_status)) 9514c8ff709SChao Yu mapping_set_error(cic->inode->i_mapping, -EIO); 9524c8ff709SChao Yu 9534c8ff709SChao Yu f2fs_put_compressed_page(page); 9544c8ff709SChao Yu 9554c8ff709SChao Yu dec_page_count(sbi, F2FS_WB_DATA); 9564c8ff709SChao Yu 9574c8ff709SChao Yu if (refcount_dec_not_one(&cic->ref)) 9584c8ff709SChao Yu return; 9594c8ff709SChao Yu 9604c8ff709SChao Yu for (i = 0; i < cic->nr_rpages; i++) { 9614c8ff709SChao Yu WARN_ON(!cic->rpages[i]); 9624c8ff709SChao Yu clear_cold_data(cic->rpages[i]); 9634c8ff709SChao Yu end_page_writeback(cic->rpages[i]); 9644c8ff709SChao Yu } 9654c8ff709SChao Yu 9664c8ff709SChao Yu kfree(cic->rpages); 9674c8ff709SChao Yu kfree(cic); 9684c8ff709SChao Yu } 9694c8ff709SChao Yu 9704c8ff709SChao Yu static int f2fs_write_raw_pages(struct compress_ctx *cc, 9714c8ff709SChao Yu int *submitted, 9724c8ff709SChao Yu struct writeback_control *wbc, 9734c8ff709SChao Yu enum iostat_type io_type) 9744c8ff709SChao Yu { 9754c8ff709SChao Yu struct address_space *mapping = cc->inode->i_mapping; 9764c8ff709SChao Yu int _submitted, compr_blocks, ret; 9774c8ff709SChao Yu int i = -1, err = 0; 9784c8ff709SChao Yu 9794c8ff709SChao Yu compr_blocks = f2fs_compressed_blocks(cc); 9804c8ff709SChao Yu if (compr_blocks < 0) { 9814c8ff709SChao Yu err = compr_blocks; 9824c8ff709SChao Yu goto out_err; 9834c8ff709SChao Yu } 9844c8ff709SChao Yu 9854c8ff709SChao Yu for (i = 0; i < cc->cluster_size; i++) { 9864c8ff709SChao Yu if (!cc->rpages[i]) 9874c8ff709SChao Yu continue; 9884c8ff709SChao Yu retry_write: 9894c8ff709SChao Yu if (cc->rpages[i]->mapping != mapping) { 9904c8ff709SChao Yu unlock_page(cc->rpages[i]); 9914c8ff709SChao Yu continue; 9924c8ff709SChao Yu } 9934c8ff709SChao Yu 9944c8ff709SChao Yu BUG_ON(!PageLocked(cc->rpages[i])); 9954c8ff709SChao Yu 9964c8ff709SChao Yu ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted, 9974c8ff709SChao Yu NULL, NULL, wbc, io_type, 9984c8ff709SChao Yu compr_blocks); 9994c8ff709SChao Yu if (ret) { 10004c8ff709SChao Yu if (ret == AOP_WRITEPAGE_ACTIVATE) { 10014c8ff709SChao Yu unlock_page(cc->rpages[i]); 10024c8ff709SChao Yu ret = 0; 10034c8ff709SChao Yu } else if (ret == -EAGAIN) { 1004466357dcSChao Yu /* 1005466357dcSChao Yu * for quota file, just redirty left pages to 1006466357dcSChao Yu * avoid deadlock caused by cluster update race 1007466357dcSChao Yu * from foreground operation. 1008466357dcSChao Yu */ 1009466357dcSChao Yu if (IS_NOQUOTA(cc->inode)) { 1010466357dcSChao Yu err = 0; 1011466357dcSChao Yu goto out_err; 1012466357dcSChao Yu } 10134c8ff709SChao Yu ret = 0; 10144c8ff709SChao Yu cond_resched(); 10155df7731fSChao Yu congestion_wait(BLK_RW_ASYNC, 10165df7731fSChao Yu DEFAULT_IO_TIMEOUT); 10174c8ff709SChao Yu lock_page(cc->rpages[i]); 10184c8ff709SChao Yu clear_page_dirty_for_io(cc->rpages[i]); 10194c8ff709SChao Yu goto retry_write; 10204c8ff709SChao Yu } 10214c8ff709SChao Yu err = ret; 1022466357dcSChao Yu goto out_err; 10234c8ff709SChao Yu } 10244c8ff709SChao Yu 10254c8ff709SChao Yu *submitted += _submitted; 10264c8ff709SChao Yu } 10274c8ff709SChao Yu return 0; 10284c8ff709SChao Yu out_err: 10294c8ff709SChao Yu for (++i; i < cc->cluster_size; i++) { 10304c8ff709SChao Yu if (!cc->rpages[i]) 10314c8ff709SChao Yu continue; 10324c8ff709SChao Yu redirty_page_for_writepage(wbc, cc->rpages[i]); 10334c8ff709SChao Yu unlock_page(cc->rpages[i]); 10344c8ff709SChao Yu } 10354c8ff709SChao Yu return err; 10364c8ff709SChao Yu } 10374c8ff709SChao Yu 10384c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc, 10394c8ff709SChao Yu int *submitted, 10404c8ff709SChao Yu struct writeback_control *wbc, 10414c8ff709SChao Yu enum iostat_type io_type) 10424c8ff709SChao Yu { 10434c8ff709SChao Yu struct f2fs_inode_info *fi = F2FS_I(cc->inode); 10444c8ff709SChao Yu const struct f2fs_compress_ops *cops = 10454c8ff709SChao Yu f2fs_cops[fi->i_compress_algorithm]; 10464c8ff709SChao Yu int err; 10474c8ff709SChao Yu 10484c8ff709SChao Yu *submitted = 0; 10494c8ff709SChao Yu if (cluster_may_compress(cc)) { 10504c8ff709SChao Yu err = f2fs_compress_pages(cc); 10514c8ff709SChao Yu if (err == -EAGAIN) { 10524c8ff709SChao Yu goto write; 10534c8ff709SChao Yu } else if (err) { 10544c8ff709SChao Yu f2fs_put_rpages_wbc(cc, wbc, true, 1); 10554c8ff709SChao Yu goto destroy_out; 10564c8ff709SChao Yu } 10574c8ff709SChao Yu 10584c8ff709SChao Yu err = f2fs_write_compressed_pages(cc, submitted, 10594c8ff709SChao Yu wbc, io_type); 10604c8ff709SChao Yu cops->destroy_compress_ctx(cc); 10614c8ff709SChao Yu if (!err) 10624c8ff709SChao Yu return 0; 10634c8ff709SChao Yu f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN); 10644c8ff709SChao Yu } 10654c8ff709SChao Yu write: 10664c8ff709SChao Yu f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted); 10674c8ff709SChao Yu 10684c8ff709SChao Yu err = f2fs_write_raw_pages(cc, submitted, wbc, io_type); 10694c8ff709SChao Yu f2fs_put_rpages_wbc(cc, wbc, false, 0); 10704c8ff709SChao Yu destroy_out: 10714c8ff709SChao Yu f2fs_destroy_compress_ctx(cc); 10724c8ff709SChao Yu return err; 10734c8ff709SChao Yu } 10744c8ff709SChao Yu 10754c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc) 10764c8ff709SChao Yu { 10774c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode); 10784c8ff709SChao Yu struct decompress_io_ctx *dic; 10794c8ff709SChao Yu pgoff_t start_idx = start_idx_of_cluster(cc); 10804c8ff709SChao Yu int i; 10814c8ff709SChao Yu 10824c8ff709SChao Yu dic = f2fs_kzalloc(sbi, sizeof(struct decompress_io_ctx), GFP_NOFS); 10834c8ff709SChao Yu if (!dic) 10844c8ff709SChao Yu return ERR_PTR(-ENOMEM); 10854c8ff709SChao Yu 10864c8ff709SChao Yu dic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) << 10874c8ff709SChao Yu cc->log_cluster_size, GFP_NOFS); 10884c8ff709SChao Yu if (!dic->rpages) { 10894c8ff709SChao Yu kfree(dic); 10904c8ff709SChao Yu return ERR_PTR(-ENOMEM); 10914c8ff709SChao Yu } 10924c8ff709SChao Yu 10934c8ff709SChao Yu dic->magic = F2FS_COMPRESSED_PAGE_MAGIC; 10944c8ff709SChao Yu dic->inode = cc->inode; 1095*887347a0SChao Yu refcount_set(&dic->ref, cc->nr_cpages); 10964c8ff709SChao Yu dic->cluster_idx = cc->cluster_idx; 10974c8ff709SChao Yu dic->cluster_size = cc->cluster_size; 10984c8ff709SChao Yu dic->log_cluster_size = cc->log_cluster_size; 10994c8ff709SChao Yu dic->nr_cpages = cc->nr_cpages; 11004c8ff709SChao Yu dic->failed = false; 11014c8ff709SChao Yu 11024c8ff709SChao Yu for (i = 0; i < dic->cluster_size; i++) 11034c8ff709SChao Yu dic->rpages[i] = cc->rpages[i]; 11044c8ff709SChao Yu dic->nr_rpages = cc->cluster_size; 11054c8ff709SChao Yu 11064c8ff709SChao Yu dic->cpages = f2fs_kzalloc(sbi, sizeof(struct page *) * 11074c8ff709SChao Yu dic->nr_cpages, GFP_NOFS); 11084c8ff709SChao Yu if (!dic->cpages) 11094c8ff709SChao Yu goto out_free; 11104c8ff709SChao Yu 11114c8ff709SChao Yu for (i = 0; i < dic->nr_cpages; i++) { 11124c8ff709SChao Yu struct page *page; 11134c8ff709SChao Yu 11144c8ff709SChao Yu page = f2fs_grab_page(); 11154c8ff709SChao Yu if (!page) 11164c8ff709SChao Yu goto out_free; 11174c8ff709SChao Yu 11184c8ff709SChao Yu f2fs_set_compressed_page(page, cc->inode, 1119*887347a0SChao Yu start_idx + i + 1, dic); 11204c8ff709SChao Yu dic->cpages[i] = page; 11214c8ff709SChao Yu } 11224c8ff709SChao Yu 11234c8ff709SChao Yu dic->tpages = f2fs_kzalloc(sbi, sizeof(struct page *) * 11244c8ff709SChao Yu dic->cluster_size, GFP_NOFS); 11254c8ff709SChao Yu if (!dic->tpages) 11264c8ff709SChao Yu goto out_free; 11274c8ff709SChao Yu 11284c8ff709SChao Yu for (i = 0; i < dic->cluster_size; i++) { 11294c8ff709SChao Yu if (cc->rpages[i]) 11304c8ff709SChao Yu continue; 11314c8ff709SChao Yu 11324c8ff709SChao Yu dic->tpages[i] = f2fs_grab_page(); 11334c8ff709SChao Yu if (!dic->tpages[i]) 11344c8ff709SChao Yu goto out_free; 11354c8ff709SChao Yu } 11364c8ff709SChao Yu 11374c8ff709SChao Yu for (i = 0; i < dic->cluster_size; i++) { 11384c8ff709SChao Yu if (dic->tpages[i]) 11394c8ff709SChao Yu continue; 11404c8ff709SChao Yu dic->tpages[i] = cc->rpages[i]; 11414c8ff709SChao Yu } 11424c8ff709SChao Yu 11434c8ff709SChao Yu return dic; 11444c8ff709SChao Yu 11454c8ff709SChao Yu out_free: 11464c8ff709SChao Yu f2fs_free_dic(dic); 11474c8ff709SChao Yu return ERR_PTR(-ENOMEM); 11484c8ff709SChao Yu } 11494c8ff709SChao Yu 11504c8ff709SChao Yu void f2fs_free_dic(struct decompress_io_ctx *dic) 11514c8ff709SChao Yu { 11524c8ff709SChao Yu int i; 11534c8ff709SChao Yu 11544c8ff709SChao Yu if (dic->tpages) { 11554c8ff709SChao Yu for (i = 0; i < dic->cluster_size; i++) { 11564c8ff709SChao Yu if (dic->rpages[i]) 11574c8ff709SChao Yu continue; 115861fbae2bSChao Yu unlock_page(dic->tpages[i]); 115961fbae2bSChao Yu put_page(dic->tpages[i]); 11604c8ff709SChao Yu } 11614c8ff709SChao Yu kfree(dic->tpages); 11624c8ff709SChao Yu } 11634c8ff709SChao Yu 11644c8ff709SChao Yu if (dic->cpages) { 11654c8ff709SChao Yu for (i = 0; i < dic->nr_cpages; i++) { 11664c8ff709SChao Yu if (!dic->cpages[i]) 11674c8ff709SChao Yu continue; 11684c8ff709SChao Yu f2fs_put_compressed_page(dic->cpages[i]); 11694c8ff709SChao Yu } 11704c8ff709SChao Yu kfree(dic->cpages); 11714c8ff709SChao Yu } 11724c8ff709SChao Yu 11734c8ff709SChao Yu kfree(dic->rpages); 11744c8ff709SChao Yu kfree(dic); 11754c8ff709SChao Yu } 11764c8ff709SChao Yu 11774c8ff709SChao Yu void f2fs_decompress_end_io(struct page **rpages, 11784c8ff709SChao Yu unsigned int cluster_size, bool err, bool verity) 11794c8ff709SChao Yu { 11804c8ff709SChao Yu int i; 11814c8ff709SChao Yu 11824c8ff709SChao Yu for (i = 0; i < cluster_size; i++) { 11834c8ff709SChao Yu struct page *rpage = rpages[i]; 11844c8ff709SChao Yu 11854c8ff709SChao Yu if (!rpage) 11864c8ff709SChao Yu continue; 11874c8ff709SChao Yu 118823c51bedSChao Yu if (err || PageError(rpage)) 118923c51bedSChao Yu goto clear_uptodate; 119023c51bedSChao Yu 119123c51bedSChao Yu if (!verity || fsverity_verify_page(rpage)) { 119223c51bedSChao Yu SetPageUptodate(rpage); 119323c51bedSChao Yu goto unlock; 119423c51bedSChao Yu } 119523c51bedSChao Yu clear_uptodate: 11964c8ff709SChao Yu ClearPageUptodate(rpage); 11974c8ff709SChao Yu ClearPageError(rpage); 119823c51bedSChao Yu unlock: 11994c8ff709SChao Yu unlock_page(rpage); 12004c8ff709SChao Yu } 12014c8ff709SChao Yu } 1202