From patchwork Tue Apr 24 23:37:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 10361203 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 A9E1A60209 for ; Tue, 24 Apr 2018 23:41:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9B67828DCC for ; Tue, 24 Apr 2018 23:41:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8FF0028E8F; Tue, 24 Apr 2018 23:41:06 +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,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_WEB,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 2B91528DCC for ; Tue, 24 Apr 2018 23:41:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751161AbeDXXlE (ORCPT ); Tue, 24 Apr 2018 19:41:04 -0400 Received: from mail-lf0-f66.google.com ([209.85.215.66]:34717 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750841AbeDXXk7 (ORCPT ); Tue, 24 Apr 2018 19:40:59 -0400 Received: by mail-lf0-f66.google.com with SMTP id m13-v6so8614295lfc.1 for ; Tue, 24 Apr 2018 16:40:58 -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:in-reply-to:references; bh=6ScAUsyv4Imqd7O68bAGo0GRYOE3GXmE9/r89Cfjwy8=; b=Ckl+ljSpyHxvIB8cl1lbM6CxRhqWSISARbsWkENKiBKTf1Cf4OlXNXj4NhnIuZ1TQL BI9dmnM1RccwZposRwov3FY5CPdklGbMma3LgGsA55RSjr2XkE7zFgg/tzagQJumfm6X eAeqXkkc2coElxCeGhYyQ8sC+MQP5fqk0TVxRKOVaLGKqBHjd44b6d0Xnodsz2406BH4 P77IcvIB3aFU+Z2556gGrwsmsn7N97ykVBLImna0V/Vmr6VoFYLWh9H9g/um2qD5BbnW 60/l5d+LQLrZ0H6VTasVFBuSVqAJ57+lmkIwoMvOzHlh2IUL/RrgOz5SrHuOSqH4P/+9 Es1w== 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=od88AXdq3a3fu+LoDIfwKlE+1pwoaICTSGG74yS3Li6NV5NsiReHBwLEvXnWyFJRqp Lg9h2cW8HVRCD0RBA+DrDxVpcNTEUT5aDpew4+eBi4SUYDxXzt4ENo/ttaMoJhnYnIop TGF59H+sc/QyJoMAaLorZuhHWIYKyHgBGnlMoxoz/BI03HyjzP2gacRjzZ9M3gy2McHc njOwXVDSgkxXBpSWq4MU3hH3rxsUVN/474bVflla4VBzFV27yzH/6xxHuwOuhTR5BeHy Cj5VLSy6KrahhBv9RxXKAbnzf3Bgc2O9WEZF4dArLAhjYZaYG0Sn8U5tPzErOLaV48Zk /txA== X-Gm-Message-State: ALQs6tC3tgmeUpNDo+PvBP1mxUkrL0yVoUhTLxBLjwbNtvp6evxh5U/1 HH/4rmAFnCOduZ73sP6Srhdxdg== X-Google-Smtp-Source: AB8JxZquJ+/5btalqomDHlnEn0rnyDx9GIcOtclYz8wphlkrOhjb2NJvoVWza73GA34qlFZ7vdUw/A== X-Received: by 10.46.144.71 with SMTP id n7mr1269122ljg.50.1524613257502; Tue, 24 Apr 2018 16:40:57 -0700 (PDT) Received: from localhost.localdomain ([178.121.235.109]) by smtp.gmail.com with ESMTPSA id 66-v6sm3618547lfr.52.2018.04.24.16.40.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Apr 2018 16:40:56 -0700 (PDT) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH 2/4] [RESEND] Btrfs: make should_defrag_range() understood compressed extents Date: Wed, 25 Apr 2018 02:37:15 +0300 Message-Id: <20180424233717.31283-3-nefelim4ag@gmail.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180424233717.31283-1-nefelim4ag@gmail.com> References: <20180424233717.31283-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