From patchwork Fri Jul 14 12:00:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 9840719 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1F259602BD for ; Fri, 14 Jul 2017 12:01:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 10E5027D4D for ; Fri, 14 Jul 2017 12:01:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 05A622837E; Fri, 14 Jul 2017 12:01:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9DC2727D4D for ; Fri, 14 Jul 2017 12:00:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753683AbdGNMA5 (ORCPT ); Fri, 14 Jul 2017 08:00:57 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:35063 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753472AbdGNMA4 (ORCPT ); Fri, 14 Jul 2017 08:00:56 -0400 Received: by mail-wm0-f68.google.com with SMTP id u23so10465671wma.2 for ; Fri, 14 Jul 2017 05:00:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=a0jv5K0En4nQzYm+EeJdiqWupVgcLKnslC6Hgjzt15Q=; b=pKpFQtSqN4ZOpgjE4Bazul5vcFYYbHT0+V3mGXDsl0GNOdEbvzKZGfdxmgncd2B4nZ LZU2ghPT5ZB3c+5d/W1O1s6NM+cRfvvmCvUdJtaF8T1oHiLpORgtsgW2RR7HC06JYkpt f0KbOtdLA2sR5njbkr9vTt1SkTa9ihvsM5G7+seNIk3emA42Xv7SAoprr06otNMujA8i pxNAqivbR/6R5vPXWo/MzcAoSZx22u5sfHsDJLTvKQHjTj6BbWnWJo9nPkiDIBSmLhSD 9puG2xNs0hrOqFp1sJMgZHqo1Z0WRm9x9RLtjLvkmjvpskDGUu1it4VoUs30LWrThU+r kUZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=a0jv5K0En4nQzYm+EeJdiqWupVgcLKnslC6Hgjzt15Q=; b=nhaC4XdogbUQuJb5W3LShVw5sVGhNubqlV6+1x/rCuvEFR/9jGihw0XOsHjx9mEtTX 99kwtyt9XI47jh+S4FtwhYkPVhOp+SODA433MqzDtnI/QQpTcf5b9foSI1mfOOH8FmE/ JFmUkxtwII0rEeNiXLhQpUrGKp64xONKGXxDJUkOHgv068xnQVKGqK7HvKMYn7EYafuO o75wsvejc1eLCSahyfFMq+jR0Nscqgh+sY2ZIq7gUtl/82ZMydBP9QXN0TMC+HrinFFM CNiVg4L/yI9IJGylzFTfNzh7NTYaLj1wx+DACsT7epzvo5NaTdUt8Gf2c4WoPws0pmS/ HvCQ== X-Gm-Message-State: AIVw112FUQAazJOY3r4eMUGehtxNwR67S7cqslFl2gg50RqWo4QJKZtW wec+mOl8GanKoRz0 X-Received: by 10.28.137.146 with SMTP id l140mr2565844wmd.101.1500033654933; Fri, 14 Jul 2017 05:00:54 -0700 (PDT) Received: from titovetst-beplan.itransition.corp ([93.171.6.183]) by smtp.gmail.com with ESMTPSA id l186sm3148294wma.10.2017.07.14.05.00.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Jul 2017 05:00:53 -0700 (PDT) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH v2] Btrfs: add skeleton code for compression heuristic Date: Fri, 14 Jul 2017 15:00:46 +0300 Message-Id: <20170714120046.3451-1-nefelim4ag@gmail.com> X-Mailer: git-send-email 2.13.2 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP For now that code just return true Later more complex heuristic code will be added Changes v1 -> v2: - Heuristic call moved into inode_need_compress() function Signed-off-by: Timofey Titovets --- fs/btrfs/compression.c | 22 ++++++++++++++++++++++ fs/btrfs/compression.h | 2 ++ fs/btrfs/inode.c | 18 ++++++++++++------ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c index 2c0b7b57fcd5..d0cf2024def7 100644 --- a/fs/btrfs/compression.c +++ b/fs/btrfs/compression.c @@ -1046,3 +1046,25 @@ int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start, return 1; } + +/* + * Heuristic skeleton + * For now just would be a naive and very optimistic 'return true'. + */ +int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end) +{ + u64 index = start >> PAGE_SHIFT; + u64 end_index = end >> PAGE_SHIFT; + struct page *page; + int ret = 1; + + while (index <= end_index) { + page = find_get_page(inode->i_mapping, index); + kmap(page); + kunmap(page); + put_page(page); + index++; + } + + return ret; +} diff --git a/fs/btrfs/compression.h b/fs/btrfs/compression.h index 87f6d3332163..8508ba6b9aef 100644 --- a/fs/btrfs/compression.h +++ b/fs/btrfs/compression.h @@ -129,4 +129,6 @@ struct btrfs_compress_op { extern const struct btrfs_compress_op btrfs_zlib_compress; extern const struct btrfs_compress_op btrfs_lzo_compress; +int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end); + #endif diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 06dea7c89bbd..27922b29fa4e 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -392,20 +392,26 @@ static noinline int add_async_extent(struct async_cow *cow, return 0; } -static inline int inode_need_compress(struct inode *inode) +static inline int inode_need_compress(struct inode *inode, u64 start, u64 end) { + int ret = 0; struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); /* force compress */ if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) - return 1; + goto try_compress; /* bad compression ratios */ if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) - return 0; + goto skip_compress; if (btrfs_test_opt(fs_info, COMPRESS) || BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || BTRFS_I(inode)->force_compress) - return 1; + goto try_compress; + goto skip_compress; +try_compress: + ret = btrfs_compress_heuristic(inode, start, end); + return ret; +skip_compress: return 0; } @@ -503,7 +509,7 @@ static noinline void compress_file_range(struct inode *inode, * inode has not been flagged as nocompress. This flag can * change at any time if we discover bad compression ratios. */ - if (inode_need_compress(inode)) { + if (inode_need_compress(inode, start, end)) { WARN_ON(pages); pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); if (!pages) { @@ -1576,7 +1582,7 @@ static int run_delalloc_range(void *private_data, struct page *locked_page, } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { ret = run_delalloc_nocow(inode, locked_page, start, end, page_started, 0, nr_written); - } else if (!inode_need_compress(inode)) { + } else if (!inode_need_compress(inode, start, end)) { ret = cow_file_range(inode, locked_page, start, end, end, page_started, nr_written, 1, NULL); } else {