From patchwork Tue Sep 18 10:09:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 10604035 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E7042913 for ; Tue, 18 Sep 2018 10:09:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D699B29E1D for ; Tue, 18 Sep 2018 10:09:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CA01529E23; Tue, 18 Sep 2018 10:09:25 +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=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI 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 2434C29E1D for ; Tue, 18 Sep 2018 10:09:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728624AbeIRPlQ (ORCPT ); Tue, 18 Sep 2018 11:41:16 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:33605 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727161AbeIRPlP (ORCPT ); Tue, 18 Sep 2018 11:41:15 -0400 Received: by mail-wr1-f65.google.com with SMTP id v90-v6so1450025wrc.0 for ; Tue, 18 Sep 2018 03:09:22 -0700 (PDT) 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:mime-version :content-transfer-encoding; bh=KNcLZ/7htZF18lJhLe8yWzvH5lqmR55d2Bkkrt8AmIA=; b=QLvW4PzyzVcoyijp5C3jzEkkWsyZTYlQyCsvZIJPbmV5L4GbLkoYmsfN/frLNHFqGv rj/fUDHgRSn4/wJ/cHrKKa40PBb5hgOR7XjlLSybjp8RBB3CbVOhnDrIbdQz9wP5sz3F ovEIOoQcq+Z1eJZjxh0Ir7kah0RT8VBK0wtSDdMjyyQwyrhSm+iy74dlZ3NjyqW3aY9U 2DLCwwJsz2Kt2gzyRwTIc72XZQn4z/1HkKf483qs8DNavKfW2phxc9cKblSL+2C5ndRg ARDaDH9/zTkx1UmMwKNF9FfUDoZBVxNNl3IorB25KCCD0RaQ6W6AKBvHa9QCHZZM43Zv TVDA== X-Gm-Message-State: APzg51BplimRo9YVSuKG2S2dvLpYMnov2DwGpseZ8KNOAr4zRZfVzdqi jBek/XQt/gGOQ+r1yS3J1qWdvB78 X-Google-Smtp-Source: ANB0VdZ3aBNBQgqK9itzfIRAMLE7fvxo8aJxsvG9YRIitFHH4oJ9Fbmh2WJ2tN/YeJSJPbVA+QydRg== X-Received: by 2002:adf:ee86:: with SMTP id b6-v6mr22167704wro.242.1537265361529; Tue, 18 Sep 2018 03:09:21 -0700 (PDT) Received: from TitovetsT.synesis.local ([86.57.155.118]) by smtp.gmail.com with ESMTPSA id 144-v6sm1588629wma.19.2018.09.18.03.09.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Sep 2018 03:09:20 -0700 (PDT) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH RESEND] Btrfs: make should_defrag_range() understood compressed extents Date: Tue, 18 Sep 2018 13:09:13 +0300 Message-Id: <20180918100913.31285-1-timofey.titovets@synesis.ru> X-Mailer: git-send-email 2.19.0 MIME-Version: 1.0 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 From: Timofey Titovets 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 a990a9045139..0a5ea1ccc89d 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -1142,7 +1142,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; @@ -1177,8 +1177,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. @@ -1477,7 +1498,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