From patchwork Tue Dec 19 10:02:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 10122903 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 2CD626019C for ; Tue, 19 Dec 2017 10:03:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1DB112905F for ; Tue, 19 Dec 2017 10:03:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 12B86290FB; Tue, 19 Dec 2017 10:03:08 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, 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 AF0852905F for ; Tue, 19 Dec 2017 10:03:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1761707AbdLSKDF (ORCPT ); Tue, 19 Dec 2017 05:03:05 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:44912 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1761668AbdLSKC7 (ORCPT ); Tue, 19 Dec 2017 05:02:59 -0500 Received: by mail-wm0-f68.google.com with SMTP id t8so2373042wmc.3 for ; Tue, 19 Dec 2017 02:02:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6ScAUsyv4Imqd7O68bAGo0GRYOE3GXmE9/r89Cfjwy8=; b=IDusLFtNtB9ej48RI4/Gl3gNSZJFjQkENRTROYTrS/uqwLlzLmBJjeTjWx49yVQIbt n8rengTCGEgT2r81EHkciOVYkfN8VTdmpsl7w3tPWDT5LAdm5mUc97ALO4nhth9RpnC0 NL9L1cXZETGZm0mA1X2ZZr+72p1B2Df8ccNH4e2VIySzQTMk438qF48as26lStTPOnGc wGDaXKaxUegiaXXHosNMSznhyrHhIV8R10BiID3GENQL8uilL52++GHge6qkybWFYUev f/PSb3lsTBhSyHTOJqRzFfbhawS8TgptusHUIjh5j9Onj2hwbsF30vD/9bgjBTQcTT/g 4LXw== 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:in-reply-to :references; bh=6ScAUsyv4Imqd7O68bAGo0GRYOE3GXmE9/r89Cfjwy8=; b=uOsaUtSXGkELmOu4CZEQ/ncEqdVhycjd60/8MRDG9pxeCdoe6BngTsuepCRgxFFzCJ dgQL5sk4ds2Z8Uf2rLn2zl6DqcvzpfuxuElE1DkfRCIjzxqFzNo0ubbDReC83PbWSMTQ wIwxwyovkljQDo17YhBg6xTbaYgPp+mYnM7HQq4ttOo3bJp7DfFPi+4mYdLHWyVEOEfs Himb1cJbQP38ZRuh3m91YWuV8oayhxDfmTFD1i5iBu852VApAiKI+V48L+ZwsesKSRy+ Lh1lo2E8HuWHBnqdFUMvmC1RS/z6Ls8HXOiWcodGnVE2dlcXtSBBnBNt8DDEmSmlWDpm HPCw== X-Gm-Message-State: AKGB3mJ56uYyxitzGE7hZ3UYZ4mP3iu96m3mc+hPE5gVmhNCE5t1RXeu DZsJHwtObifoWrfpTT91rPaRdQ== X-Google-Smtp-Source: ACJfBotrHi5VOZzNXYpQgyjBLo6cF/F3qUxKIJGT5pPkUbMt7QHRL2BsJbfNynRQ8ltmGZLsmsWJ4w== X-Received: by 10.28.125.85 with SMTP id y82mr2792422wmc.25.1513677777940; Tue, 19 Dec 2017 02:02:57 -0800 (PST) Received: from titovetst-beplan.itransition.corp ([93.171.6.182]) by smtp.gmail.com with ESMTPSA id s70sm1404372wma.37.2017.12.19.02.02.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Dec 2017 02:02:57 -0800 (PST) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH 2/4] Btrfs: make should_defrag_range() understood compressed extents Date: Tue, 19 Dec 2017 13:02:45 +0300 Message-Id: <20171219100247.13880-3-nefelim4ag@gmail.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171219100247.13880-1-nefelim4ag@gmail.com> References: <20171219100247.13880-1-nefelim4ag@gmail.com> 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 Both, defrag ioctl and autodefrag - call btrfs_defrag_file() for file defragmentation. Kernel default target extent size - 256KiB. Btrfs progs default - 32MiB. Both bigger then maximum size of compressed extent - 128KiB. That lead to rewrite all compressed data on disk. Fix that by check compression extents with different logic. As addition, make should_defrag_range() understood compressed extent type, if requested target compression are same as current extent compression type. Just don't recompress/rewrite extents. To avoid useless recompression of compressed extents. Signed-off-by: Timofey Titovets --- fs/btrfs/ioctl.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 45a47d0891fc..b29ea1f0f621 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -1008,7 +1008,7 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, u64 *last_len, u64 *skip, u64 *defrag_end, - int compress) + int compress, int compress_type) { struct extent_map *em; int ret = 1; @@ -1043,8 +1043,29 @@ static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, * real extent, don't bother defragging it */ if (!compress && (*last_len == 0 || *last_len >= thresh) && - (em->len >= thresh || (!next_mergeable && !prev_mergeable))) + (em->len >= thresh || (!next_mergeable && !prev_mergeable))) { ret = 0; + goto out; + } + + + /* + * Try not recompress compressed extents + * thresh >= BTRFS_MAX_UNCOMPRESSED will lead to + * recompress all compressed extents + */ + if (em->compress_type != 0 && thresh >= BTRFS_MAX_UNCOMPRESSED) { + if (!compress) { + if (em->len == BTRFS_MAX_UNCOMPRESSED) + ret = 0; + } else { + if (em->compress_type != compress_type) + goto out; + if (em->len == BTRFS_MAX_UNCOMPRESSED) + ret = 0; + } + } + out: /* * last_len ends up being a counter of how many bytes we've defragged. @@ -1342,7 +1363,8 @@ int btrfs_defrag_file(struct inode *inode, struct file *file, if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, extent_thresh, &last_len, &skip, - &defrag_end, do_compress)){ + &defrag_end, do_compress, + compress_type)){ unsigned long next; /* * the should_defrag function tells us how much to skip