From patchwork Fri Jul 14 10:30:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 9840495 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 BD83B60212 for ; Fri, 14 Jul 2017 10:30:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AEE782876E for ; Fri, 14 Jul 2017 10:30:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A35D328777; Fri, 14 Jul 2017 10:30:23 +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 2EF782876E for ; Fri, 14 Jul 2017 10:30:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753691AbdGNKaV (ORCPT ); Fri, 14 Jul 2017 06:30:21 -0400 Received: from mail-lf0-f66.google.com ([209.85.215.66]:34254 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753471AbdGNKaU (ORCPT ); Fri, 14 Jul 2017 06:30:20 -0400 Received: by mail-lf0-f66.google.com with SMTP id g21so7817913lfk.1 for ; Fri, 14 Jul 2017 03:30:19 -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=wsUImkNEhqZ+aJuoUzIk9M4/yXxXqCZIdL31OST/YQU=; b=HERvU/jFDzPfJ3zaZCuQJ1bXpoqIfimz5hmC9RWipPt+JExVKtP9/l9EiypycoIG6A hoocYFQRgPuKv0/TI4lvCoS/UVcgKd1VzEm50vPQRLFssflfig0u3tOL7dmCDOC/Gxqf OfECFhQ2V8I6q5gy/b1xuAqWS9S8dn3R8VMXPAbLJfd89BKS+yQvcs5T8JQH9p4sexGn EO3J7oLMED7V14suuPOjl3iW8AAw7RUhBjdHnBxwLxjYF+tjdzfuqF89q87UqGp3croe mymcrzvCtZePJtGGWnjiOjsm1p2B1keJ6HN2J4XjuhfNYrlSYQwipHtc5lKA9hL59Lzy yDXg== 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=wsUImkNEhqZ+aJuoUzIk9M4/yXxXqCZIdL31OST/YQU=; b=iInX76FmkzB7IVBv8zn2dslD/faqjnJceNsl+WC1ynLhW0ZQvaPVl/CbzVEmJ/bniO drVWfZWvVc060SyD1eNjRqfg2mS21TIBmxHz1Q2Vh7/PC3nadsZCjyo2/R6Hz9ynXtw0 H9Mhrv5aJrE+P3FG3WxQ4vyBpldZK0h9HZ/8XwKKAuJ6dTXoRMDN5ZXEPceuAYVb+rDD u1XXlugjxJynD4hBgjN+nCvoExRKm6YJELXN8iaJSHqfU4TP3JQmn0qQKi0Qiu5MZNR4 Y31KJRftNTiA+OR3CnBJZEruIbCTVE+JYBfAPg4yNCzihKODgLNdePRBpJNIeS6sf6vi IbOg== X-Gm-Message-State: AIVw111ynYo8YKSgQmeDYJvyhBtE0qwPA+nufhohfFRG1PGu96ZeZaDO FnGeIWvVBalq4ent X-Received: by 10.25.196.194 with SMTP id u185mr2945067lff.88.1500028218204; Fri, 14 Jul 2017 03:30:18 -0700 (PDT) Received: from titovetst-beplan.itransition.corp ([93.171.6.183]) by smtp.gmail.com with ESMTPSA id 25sm1723468lju.69.2017.07.14.03.30.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Jul 2017 03:30:17 -0700 (PDT) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH] Btrfs: add skeleton code for compression heuristic Date: Fri, 14 Jul 2017 13:30:00 +0300 Message-Id: <20170714103000.9052-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 | 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 {