Merge tag 'mfd-next-5.7' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd
[linux-2.6-block.git] / fs / f2fs / compress.c
index 748933813cd1fa25d5307278d404d447af9b7f2b..df7b2d15eacdee0e7ddafa18cffcf51f28cf0948 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/backing-dev.h>
 #include <linux/lzo.h>
 #include <linux/lz4.h>
+#include <linux/zstd.h>
 
 #include "f2fs.h"
 #include "node.h"
@@ -20,6 +21,8 @@ struct f2fs_compress_ops {
        int (*init_compress_ctx)(struct compress_ctx *cc);
        void (*destroy_compress_ctx)(struct compress_ctx *cc);
        int (*compress_pages)(struct compress_ctx *cc);
+       int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
+       void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
        int (*decompress_pages)(struct decompress_io_ctx *dic);
 };
 
@@ -52,7 +55,7 @@ bool f2fs_is_compressed_page(struct page *page)
 }
 
 static void f2fs_set_compressed_page(struct page *page,
-               struct inode *inode, pgoff_t index, void *data, refcount_t *r)
+               struct inode *inode, pgoff_t index, void *data)
 {
        SetPagePrivate(page);
        set_page_private(page, (unsigned long)data);
@@ -60,8 +63,6 @@ static void f2fs_set_compressed_page(struct page *page,
        /* i_crypto_info and iv index */
        page->index = index;
        page->mapping = inode->i_mapping;
-       if (r)
-               refcount_inc(r);
 }
 
 static void f2fs_put_compressed_page(struct page *page)
@@ -291,6 +292,165 @@ static const struct f2fs_compress_ops f2fs_lz4_ops = {
 };
 #endif
 
+#ifdef CONFIG_F2FS_FS_ZSTD
+#define F2FS_ZSTD_DEFAULT_CLEVEL       1
+
+static int zstd_init_compress_ctx(struct compress_ctx *cc)
+{
+       ZSTD_parameters params;
+       ZSTD_CStream *stream;
+       void *workspace;
+       unsigned int workspace_size;
+
+       params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen, 0);
+       workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
+
+       workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
+                                       workspace_size, GFP_NOFS);
+       if (!workspace)
+               return -ENOMEM;
+
+       stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
+       if (!stream) {
+               printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n",
+                               KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
+                               __func__);
+               kvfree(workspace);
+               return -EIO;
+       }
+
+       cc->private = workspace;
+       cc->private2 = stream;
+
+       cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
+       return 0;
+}
+
+static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
+{
+       kvfree(cc->private);
+       cc->private = NULL;
+       cc->private2 = NULL;
+}
+
+static int zstd_compress_pages(struct compress_ctx *cc)
+{
+       ZSTD_CStream *stream = cc->private2;
+       ZSTD_inBuffer inbuf;
+       ZSTD_outBuffer outbuf;
+       int src_size = cc->rlen;
+       int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
+       int ret;
+
+       inbuf.pos = 0;
+       inbuf.src = cc->rbuf;
+       inbuf.size = src_size;
+
+       outbuf.pos = 0;
+       outbuf.dst = cc->cbuf->cdata;
+       outbuf.size = dst_size;
+
+       ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
+       if (ZSTD_isError(ret)) {
+               printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
+                               KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
+                               __func__, ZSTD_getErrorCode(ret));
+               return -EIO;
+       }
+
+       ret = ZSTD_endStream(stream, &outbuf);
+       if (ZSTD_isError(ret)) {
+               printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n",
+                               KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
+                               __func__, ZSTD_getErrorCode(ret));
+               return -EIO;
+       }
+
+       cc->clen = outbuf.pos;
+       return 0;
+}
+
+static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
+{
+       ZSTD_DStream *stream;
+       void *workspace;
+       unsigned int workspace_size;
+
+       workspace_size = ZSTD_DStreamWorkspaceBound(MAX_COMPRESS_WINDOW_SIZE);
+
+       workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
+                                       workspace_size, GFP_NOFS);
+       if (!workspace)
+               return -ENOMEM;
+
+       stream = ZSTD_initDStream(MAX_COMPRESS_WINDOW_SIZE,
+                                       workspace, workspace_size);
+       if (!stream) {
+               printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n",
+                               KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
+                               __func__);
+               kvfree(workspace);
+               return -EIO;
+       }
+
+       dic->private = workspace;
+       dic->private2 = stream;
+
+       return 0;
+}
+
+static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
+{
+       kvfree(dic->private);
+       dic->private = NULL;
+       dic->private2 = NULL;
+}
+
+static int zstd_decompress_pages(struct decompress_io_ctx *dic)
+{
+       ZSTD_DStream *stream = dic->private2;
+       ZSTD_inBuffer inbuf;
+       ZSTD_outBuffer outbuf;
+       int ret;
+
+       inbuf.pos = 0;
+       inbuf.src = dic->cbuf->cdata;
+       inbuf.size = dic->clen;
+
+       outbuf.pos = 0;
+       outbuf.dst = dic->rbuf;
+       outbuf.size = dic->rlen;
+
+       ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
+       if (ZSTD_isError(ret)) {
+               printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
+                               KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
+                               __func__, ZSTD_getErrorCode(ret));
+               return -EIO;
+       }
+
+       if (dic->rlen != outbuf.pos) {
+               printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
+                               "expected:%lu\n", KERN_ERR,
+                               F2FS_I_SB(dic->inode)->sb->s_id,
+                               __func__, dic->rlen,
+                               PAGE_SIZE << dic->log_cluster_size);
+               return -EIO;
+       }
+
+       return 0;
+}
+
+static const struct f2fs_compress_ops f2fs_zstd_ops = {
+       .init_compress_ctx      = zstd_init_compress_ctx,
+       .destroy_compress_ctx   = zstd_destroy_compress_ctx,
+       .compress_pages         = zstd_compress_pages,
+       .init_decompress_ctx    = zstd_init_decompress_ctx,
+       .destroy_decompress_ctx = zstd_destroy_decompress_ctx,
+       .decompress_pages       = zstd_decompress_pages,
+};
+#endif
+
 static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
 #ifdef CONFIG_F2FS_FS_LZO
        &f2fs_lzo_ops,
@@ -302,6 +462,11 @@ static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
 #else
        NULL,
 #endif
+#ifdef CONFIG_F2FS_FS_ZSTD
+       &f2fs_zstd_ops,
+#else
+       NULL,
+#endif
 };
 
 bool f2fs_is_compress_backend_ready(struct inode *inode)
@@ -334,9 +499,11 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
        trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
                                cc->cluster_size, fi->i_compress_algorithm);
 
-       ret = cops->init_compress_ctx(cc);
-       if (ret)
-               goto out;
+       if (cops->init_compress_ctx) {
+               ret = cops->init_compress_ctx(cc);
+               if (ret)
+                       goto out;
+       }
 
        max_len = COMPRESS_HEADER_SIZE + cc->clen;
        cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
@@ -398,6 +565,9 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
                cc->cpages[i] = NULL;
        }
 
+       if (cops->destroy_compress_ctx)
+               cops->destroy_compress_ctx(cc);
+
        cc->nr_cpages = nr_cpages;
 
        trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
@@ -416,7 +586,8 @@ out_free_cpages:
        kfree(cc->cpages);
        cc->cpages = NULL;
 destroy_compress_ctx:
-       cops->destroy_compress_ctx(cc);
+       if (cops->destroy_compress_ctx)
+               cops->destroy_compress_ctx(cc);
 out:
        trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
                                                        cc->clen, ret);
@@ -450,10 +621,16 @@ void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity)
                goto out_free_dic;
        }
 
+       if (cops->init_decompress_ctx) {
+               ret = cops->init_decompress_ctx(dic);
+               if (ret)
+                       goto out_free_dic;
+       }
+
        dic->rbuf = vmap(dic->tpages, dic->cluster_size, VM_MAP, PAGE_KERNEL);
        if (!dic->rbuf) {
                ret = -ENOMEM;
-               goto out_free_dic;
+               goto destroy_decompress_ctx;
        }
 
        dic->cbuf = vmap(dic->cpages, dic->nr_cpages, VM_MAP, PAGE_KERNEL_RO);
@@ -476,7 +653,12 @@ out_vunmap_cbuf:
        vunmap(dic->cbuf);
 out_vunmap_rbuf:
        vunmap(dic->rbuf);
+destroy_decompress_ctx:
+       if (cops->destroy_decompress_ctx)
+               cops->destroy_decompress_ctx(dic);
 out_free_dic:
+       if (verity)
+               refcount_set(&dic->ref, dic->nr_cpages);
        if (!verity)
                f2fs_decompress_end_io(dic->rpages, dic->cluster_size,
                                                                ret, false);
@@ -535,8 +717,7 @@ static bool __cluster_may_compress(struct compress_ctx *cc)
        return true;
 }
 
-/* return # of compressed block addresses */
-static int f2fs_compressed_blocks(struct compress_ctx *cc)
+static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
 {
        struct dnode_of_data dn;
        int ret;
@@ -559,8 +740,13 @@ static int f2fs_compressed_blocks(struct compress_ctx *cc)
 
                        blkaddr = data_blkaddr(dn.inode,
                                        dn.node_page, dn.ofs_in_node + i);
-                       if (blkaddr != NULL_ADDR)
-                               ret++;
+                       if (compr) {
+                               if (__is_valid_data_blkaddr(blkaddr))
+                                       ret++;
+                       } else {
+                               if (blkaddr != NULL_ADDR)
+                                       ret++;
+                       }
                }
        }
 fail:
@@ -568,6 +754,18 @@ fail:
        return ret;
 }
 
+/* return # of compressed blocks in compressed cluster */
+static int f2fs_compressed_blocks(struct compress_ctx *cc)
+{
+       return __f2fs_cluster_blocks(cc, true);
+}
+
+/* return # of valid blocks in compressed cluster */
+static int f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
+{
+       return __f2fs_cluster_blocks(cc, false);
+}
+
 int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
 {
        struct compress_ctx cc = {
@@ -577,7 +775,7 @@ int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
                .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
        };
 
-       return f2fs_compressed_blocks(&cc);
+       return f2fs_cluster_blocks(&cc, false);
 }
 
 static bool cluster_may_compress(struct compress_ctx *cc)
@@ -626,7 +824,7 @@ static int prepare_compress_overwrite(struct compress_ctx *cc,
        bool prealloc;
 
 retry:
-       ret = f2fs_compressed_blocks(cc);
+       ret = f2fs_cluster_blocks(cc, false);
        if (ret <= 0)
                return ret;
 
@@ -656,7 +854,7 @@ retry:
                struct bio *bio = NULL;
 
                ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
-                                               &last_block_in_bio, false);
+                                       &last_block_in_bio, false, true);
                f2fs_destroy_compress_ctx(cc);
                if (ret)
                        goto release_pages;
@@ -816,7 +1014,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
 
        cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
        cic->inode = inode;
-       refcount_set(&cic->ref, 1);
+       refcount_set(&cic->ref, cc->nr_cpages);
        cic->rpages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
                        cc->log_cluster_size, GFP_NOFS);
        if (!cic->rpages)
@@ -826,8 +1024,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
 
        for (i = 0; i < cc->nr_cpages; i++) {
                f2fs_set_compressed_page(cc->cpages[i], inode,
-                                       cc->rpages[i + 1]->index,
-                                       cic, i ? &cic->ref : NULL);
+                                       cc->rpages[i + 1]->index, cic);
                fio.compressed_page = cc->cpages[i];
                if (fio.encrypted) {
                        fio.page = cc->rpages[i + 1];
@@ -986,6 +1183,15 @@ retry_write:
                                unlock_page(cc->rpages[i]);
                                ret = 0;
                        } else if (ret == -EAGAIN) {
+                               /*
+                                * for quota file, just redirty left pages to
+                                * avoid deadlock caused by cluster update race
+                                * from foreground operation.
+                                */
+                               if (IS_NOQUOTA(cc->inode)) {
+                                       err = 0;
+                                       goto out_err;
+                               }
                                ret = 0;
                                cond_resched();
                                congestion_wait(BLK_RW_ASYNC,
@@ -995,16 +1201,12 @@ retry_write:
                                goto retry_write;
                        }
                        err = ret;
-                       goto out_fail;
+                       goto out_err;
                }
 
                *submitted += _submitted;
        }
        return 0;
-
-out_fail:
-       /* TODO: revoke partially updated block addresses */
-       BUG_ON(compr_blocks);
 out_err:
        for (++i; i < cc->cluster_size; i++) {
                if (!cc->rpages[i])
@@ -1072,7 +1274,7 @@ struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
 
        dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
        dic->inode = cc->inode;
-       refcount_set(&dic->ref, 1);
+       refcount_set(&dic->ref, cc->nr_cpages);
        dic->cluster_idx = cc->cluster_idx;
        dic->cluster_size = cc->cluster_size;
        dic->log_cluster_size = cc->log_cluster_size;
@@ -1096,8 +1298,7 @@ struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
                        goto out_free;
 
                f2fs_set_compressed_page(page, cc->inode,
-                                       start_idx + i + 1,
-                                       dic, i ? &dic->ref : NULL);
+                                       start_idx + i + 1, dic);
                dic->cpages[i] = page;
        }
 
@@ -1107,20 +1308,16 @@ struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
                goto out_free;
 
        for (i = 0; i < dic->cluster_size; i++) {
-               if (cc->rpages[i])
+               if (cc->rpages[i]) {
+                       dic->tpages[i] = cc->rpages[i];
                        continue;
+               }
 
                dic->tpages[i] = f2fs_grab_page();
                if (!dic->tpages[i])
                        goto out_free;
        }
 
-       for (i = 0; i < dic->cluster_size; i++) {
-               if (dic->tpages[i])
-                       continue;
-               dic->tpages[i] = cc->rpages[i];
-       }
-
        return dic;
 
 out_free:
@@ -1136,6 +1333,8 @@ void f2fs_free_dic(struct decompress_io_ctx *dic)
                for (i = 0; i < dic->cluster_size; i++) {
                        if (dic->rpages[i])
                                continue;
+                       if (!dic->tpages[i])
+                               continue;
                        unlock_page(dic->tpages[i]);
                        put_page(dic->tpages[i]);
                }
@@ -1166,15 +1365,17 @@ void f2fs_decompress_end_io(struct page **rpages,
                if (!rpage)
                        continue;
 
-               if (err || PageError(rpage)) {
-                       ClearPageUptodate(rpage);
-                       ClearPageError(rpage);
-               } else {
-                       if (!verity || fsverity_verify_page(rpage))
-                               SetPageUptodate(rpage);
-                       else
-                               SetPageError(rpage);
+               if (err || PageError(rpage))
+                       goto clear_uptodate;
+
+               if (!verity || fsverity_verify_page(rpage)) {
+                       SetPageUptodate(rpage);
+                       goto unlock;
                }
+clear_uptodate:
+               ClearPageUptodate(rpage);
+               ClearPageError(rpage);
+unlock:
                unlock_page(rpage);
        }
 }