From patchwork Thu Dec 14 13:35: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: 10112109 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 36CD060352 for ; Thu, 14 Dec 2017 13:36:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2785C2871B for ; Thu, 14 Dec 2017 13:36:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1C54C288CD; Thu, 14 Dec 2017 13:36:09 +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 ACBB5288B6 for ; Thu, 14 Dec 2017 13:36:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752456AbdLNNgG (ORCPT ); Thu, 14 Dec 2017 08:36:06 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:33449 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752329AbdLNNgG (ORCPT ); Thu, 14 Dec 2017 08:36:06 -0500 Received: by mail-lf0-f66.google.com with SMTP id g80so2229217lfg.0 for ; Thu, 14 Dec 2017 05:36:05 -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; bh=3uR49gmDwvSnPQA57t5oKpFpvll1v62XNTNwIP1/3lE=; b=q2+DsPUOPFQEIBk26mM+DpZg7MxC+o4LsKYFecvDOAAfZqRDNCln/zOvSs23/jHZWd zzrGERnCh3LXDwMorBBbTLmKkU8mBg94nMQPKUx1Nxb/qgaEsbL9Zmsbthozgni4if2a nY5kQmsi6oA3BVR5NYM7FE5oSfthDML/GbPU56DF74kS0iYDl7kw0knnzMfZDI5Py+NI EyQhPN0aN4EKeIv1UhDMVUVDDsSBRV3CZvAqnOTD8cEvXUmaTlOMsW2DR12rbG+YEq9Y B7tutGqF+YHwjUdwllunHu8qayD/ibTZQQdJT+TdpXj/6ToWo2DETR4N+gNZu41+jWTb amsw== 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=3uR49gmDwvSnPQA57t5oKpFpvll1v62XNTNwIP1/3lE=; b=J0tL1WbxhVa5NvXaTDyROYGwzAgZ+Cdzj0NeuLiUSM7LqVxmtOMEUX8FJZEMgrz2wV Su4ll8FgkNSzkPWIgseFYX6bHhlkHEwzwcvp2et2ctKnhCAlc8NZ/bk60gVIz90lVEaq lckjbZs70tzGRXH5SUhpz30S54m66qhprwykIVN8tkZUQCGCDz6YgH8RpfmtPLaDVU0c QzZ75ZOzOT449OGtala8hcrhnJSeHh3BIpzvvzjxNDzf5HLHHW8gLhRLNILpuKbxB6mP lRkNOs1X5kcdCxAkU0ssJlxXiIUU5LxRXV0Oh+32HbdDJOjpgIvPRQf0KesuqqZe5sMZ Sw8g== X-Gm-Message-State: AKGB3mK5kJDz8ZUK/gU/b1fUn5STbdb1JXWhUyRk8d/D8YsLdOJy7eVm UFpiqhx5MbYAttyctgO3RVp5aw== X-Google-Smtp-Source: ACJfBovW5j3rLM5C2PgD8MAqvKE9wis5I26PGaMRqp7UHGq1Xmbuaw5x9dwNU0agj4EFnVAv2zXT4Q== X-Received: by 10.25.28.9 with SMTP id c9mr3447669lfc.40.1513258564033; Thu, 14 Dec 2017 05:36:04 -0800 (PST) Received: from titovetst-beplan.itransition.corp ([93.171.6.182]) by smtp.gmail.com with ESMTPSA id j1sm831922lfe.66.2017.12.14.05.36.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Dec 2017 05:36:03 -0800 (PST) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH] Btrfs: make should_defrag_range() understood compressed extents Date: Thu, 14 Dec 2017 16:35:46 +0300 Message-Id: <20171214133546.28609-1-nefelim4ag@gmail.com> X-Mailer: git-send-email 2.15.1 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 target extent size default is 256KiB Btrfs progs by default, use 32MiB. Both bigger then max (not fragmented) compressed extent size 128KiB. That lead to rewrite all compressed data on disk. Fix that and also make should_defrag_range() understood if requested target compression are same as current extent compression type. 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 be5bd81b3669..12d4fa5d6dec 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