From patchwork Tue Jul 4 17:28:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 9825447 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 743FE602F0 for ; Tue, 4 Jul 2017 17:28:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6015826E55 for ; Tue, 4 Jul 2017 17:28:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 536AD284C8; Tue, 4 Jul 2017 17:28:30 +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 E716626E55 for ; Tue, 4 Jul 2017 17:28:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751982AbdGDR21 (ORCPT ); Tue, 4 Jul 2017 13:28:27 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:34743 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751536AbdGDR20 (ORCPT ); Tue, 4 Jul 2017 13:28:26 -0400 Received: by mail-wm0-f66.google.com with SMTP id p204so27770657wmg.1 for ; Tue, 04 Jul 2017 10:28:25 -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=wetQ0rMlSOP/M6SDhsRMuNN0v3pY1mozI+zDktUirn0=; b=KQklxIPjcXJt1fNJ3GHkJ4Wn/vwLk2/PA5pqLhrxXGoR57wwbmViiIAhoNyw4pIa4+ uejPHX1N3gclCusF3v5hA4OYL+6xHnrb+xqI2679wMtUmLmckzli7K8fQXwS/qyQgcTe 2tWjn930FK+ICDRMJWQeZF2BkGyakGaIXbwEH/IMux9BYBrkaRplDlN2bVKjyhLjxnaZ vq31MgwWoImKu1z0QAahwwukyOo8xf4LCvUOMny/jH4rafwysnTpBtsOEw7b/e6e2Rz0 gOw9gTo6a1bgsu7MCklt1lfMvb26b8aa908KaVofnhp7LyyOt6jruCPc4EW8XxChhjJS GGSA== 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=wetQ0rMlSOP/M6SDhsRMuNN0v3pY1mozI+zDktUirn0=; b=EanvAr1gFBxesOq62Ji9ZNrH3Jk0mbXOeT6e6/Ti9Sa6Vf9j+W9tfnNosIbmdgU01I NeDmGh58CL+eBfLr1+zsAw3ftbQy66g2diHpIGQArrL17fGCwhFKoXmUNTm0iGH5d7yJ +9jxujn6Jbp5c8BSucIVc/+Lnhq3vnG3mbK/+qdSz8gdaoaWX4epPUIzq1r0Hl5rMP8/ gWUj1IG3KPrNwXWhI90zWGOfG98Y4EKlK7EhKXjLo8FIc9v6u6aGLW3i89AhS/kDV0AS 7L4NWXWEBo6gVqoHyDACVymh0mPqK/rJOtuqaYbQEhZBkwM85Y1RT8QE3584ZSi6IuWC u27A== X-Gm-Message-State: AIVw110tY10UWU8X4J4/1RzT8/k5vvyhZqMiNLlyX3fLdzz//apTdgB6 +usIvvwpzfvvxJg4 X-Received: by 10.28.146.208 with SMTP id u199mr21167491wmd.6.1499189304638; Tue, 04 Jul 2017 10:28:24 -0700 (PDT) Received: from titovetst-beplan.itransition.corp ([93.171.6.183]) by smtp.gmail.com with ESMTPSA id w30sm23514362wrb.49.2017.07.04.10.28.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Jul 2017 10:28:24 -0700 (PDT) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH] Btrfs: add skeleton code for compression heuristic Date: Tue, 4 Jul 2017 20:28:15 +0300 Message-Id: <20170704172815.26449-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 Signed-off-by: Timofey Titovets --- fs/btrfs/compression.c | 22 ++++++++++++++++++++++ fs/btrfs/compression.h | 2 ++ fs/btrfs/inode.c | 25 ++++++++++++++++--------- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c index a2fad39f79ba..481e56f61461 100644 --- a/fs/btrfs/compression.c +++ b/fs/btrfs/compression.c @@ -1098,3 +1098,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 680d4265d601..259ea776c9d4 100644 --- a/fs/btrfs/compression.h +++ b/fs/btrfs/compression.h @@ -93,4 +93,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 556c93060606..285e5b5eed35 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -510,15 +510,6 @@ static noinline void compress_file_range(struct inode *inode, */ if (inode_need_compress(inode)) { WARN_ON(pages); - pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); - if (!pages) { - /* just bail out to the uncompressed code */ - goto cont; - } - - if (BTRFS_I(inode)->force_compress) - compress_type = BTRFS_I(inode)->force_compress; - /* * we need to call clear_page_dirty_for_io on each * page in the range. Otherwise applications with the file @@ -530,6 +521,22 @@ static noinline void compress_file_range(struct inode *inode, */ extent_range_clear_dirty_for_io(inode, start, end); redirty = 1; + + ret = btrfs_compress_heuristic(inode, start, end); + + /* Heuristic say: dont try compress that */ + if (ret == 0) + goto cont; + + pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); + if (!pages) { + /* just bail out to the uncompressed code */ + goto cont; + } + + if (BTRFS_I(inode)->force_compress) + compress_type = BTRFS_I(inode)->force_compress; + ret = btrfs_compress_pages(compress_type, inode->i_mapping, start, pages,