Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3427370imu; Sun, 11 Nov 2018 14:51:49 -0800 (PST) X-Google-Smtp-Source: AJdET5c8zG5kgSrglPP//k5BJQAjXm2Ay5EOY0vCnQVrrzjluwO1R3J7pyrVtgVujbBPN7F/Qk1y X-Received: by 2002:a63:dd15:: with SMTP id t21mr1741539pgg.347.1541976709523; Sun, 11 Nov 2018 14:51:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541976709; cv=none; d=google.com; s=arc-20160816; b=P0/eQzDOVYcSo9KlpkXtBoMXxt/NvQxA45E9ac94D6FlihE6Oinyz7R1/TdYYalUkB ZDJjx3hph3FbNLfZnj271/gKmiiEXSJjU5CwDpBfKbpNPWaTKAIWX+wmEfF1SLBIXYfs d6Oerxt/wEhJHnZ54ejFKk2AkGZ2FyCMQ9ee5zdoYqvqVb0GgzaDkvRkfuxcRopF/92A 5pSgYYpvl8nyNwb5TOf0PhFj+EY7Eo7/JL8G0BUvlM6yGMEWp+qrphwa9uE2H2Xi9/yr RM3w/a03vt8qMr3M93jZBy+Ma67vNgo2CU6esj3GaYetuYFJQDIg/p0SyJ4fdlksDJKc Qs7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/Rt0Xytu1AMV52y3ctfgBZKw0qr2dD1iOp3XkZh+jbA=; b=0R9m2phc8a46C2/RwckOb1wgqPYSAdq2QBSKpBgETG7W+T14lIW3JNrD7z6ocVFb8a w1of1pPfx5E6ldtAv0u4k+X1c6u4ji3UI+6fFJVThvkSl3PICiPJXuhmda155N4D8rZe XfVRzAFZX8Ezukg29Q1vANvTH/OsIVVw2oV5z16LgDlkE/O9+O1HwWDg0KzaY0FgLmdJ StvYmZg4B0T9su1+hXlAy88WeB5qdwTnDH7Hb6jtWtEpOSR2f7xtcG+TK3UC7m9fZZPT D0ioF95yI5bgrjUWhnjxwF9Zyjmf2e4233xQoqLaGxWUCZOn64WHS0cfeHGbLl8baO6I zFXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=NOFom1Bw; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i20-v6si13001986pgk.562.2018.11.11.14.51.34; Sun, 11 Nov 2018 14:51:49 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=NOFom1Bw; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404313AbeKLIlQ (ORCPT + 99 others); Mon, 12 Nov 2018 03:41:16 -0500 Received: from mail.kernel.org ([198.145.29.99]:54620 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404153AbeKLIXH (ORCPT ); Mon, 12 Nov 2018 03:23:07 -0500 Received: from localhost (unknown [206.108.79.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3C7C4223DD; Sun, 11 Nov 2018 22:33:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975590; bh=rALukCeLIVZw1uw0Znpe+Yh1D9hnFLzK0iw83v0xUPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NOFom1Bw6Cdl6wPso6nG4hIoiPLea9UxsyUjMVsal79NNyWCEBvAtZ6HjYyZZe/9A DLjwIyRM3YZp4X151Zj/h+TmkteQwNLppp/4cwK2jLN6FH4mqxke8eCPuVMZVCW0G2 eLD5Sy2d8vQjgiqzruptHG5F8LyM3zuqSc1+T9b8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chris Murphy , Qu Wenruo , Nikolay Borisov , David Sterba Subject: [PATCH 4.14 201/222] btrfs: Ensure btrfs_trim_fs can trim the whole filesystem Date: Sun, 11 Nov 2018 14:24:58 -0800 Message-Id: <20181111221704.710887970@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221647.665769131@linuxfoundation.org> References: <20181111221647.665769131@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Qu Wenruo commit 6ba9fc8e628becf0e3ec94083450d089b0dec5f5 upstream. [BUG] fstrim on some btrfs only trims the unallocated space, not trimming any space in existing block groups. [CAUSE] Before fstrim_range passed to btrfs_trim_fs(), it gets truncated to range [0, super->total_bytes). So later btrfs_trim_fs() will only be able to trim block groups in range [0, super->total_bytes). While for btrfs, any bytenr aligned to sectorsize is valid, since btrfs uses its logical address space, there is nothing limiting the location where we put block groups. For filesystem with frequent balance, it's quite easy to relocate all block groups and bytenr of block groups will start beyond super->total_bytes. In that case, btrfs will not trim existing block groups. [FIX] Just remove the truncation in btrfs_ioctl_fitrim(), so btrfs_trim_fs() can get the unmodified range, which is normally set to [0, U64_MAX]. Reported-by: Chris Murphy Fixes: f4c697e6406d ("btrfs: return EINVAL if start > total_bytes in fitrim ioctl") CC: # v4.4+ Signed-off-by: Qu Wenruo Reviewed-by: Nikolay Borisov Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/extent-tree.c | 10 +--------- fs/btrfs/ioctl.c | 11 +++++++---- 2 files changed, 8 insertions(+), 13 deletions(-) --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -11055,21 +11055,13 @@ int btrfs_trim_fs(struct btrfs_fs_info * u64 start; u64 end; u64 trimmed = 0; - u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); u64 bg_failed = 0; u64 dev_failed = 0; int bg_ret = 0; int dev_ret = 0; int ret = 0; - /* - * try to trim all FS space, our block group may start from non-zero. - */ - if (range->len == total_bytes) - cache = btrfs_lookup_first_block_group(fs_info, range->start); - else - cache = btrfs_lookup_block_group(fs_info, range->start); - + cache = btrfs_lookup_first_block_group(fs_info, range->start); for (; cache; cache = next_block_group(fs_info, cache)) { if (cache->key.objectid >= (range->start + range->len)) { btrfs_put_block_group(cache); --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -352,7 +352,6 @@ static noinline int btrfs_ioctl_fitrim(s struct fstrim_range range; u64 minlen = ULLONG_MAX; u64 num_devices = 0; - u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); int ret; if (!capable(CAP_SYS_ADMIN)) @@ -376,11 +375,15 @@ static noinline int btrfs_ioctl_fitrim(s return -EOPNOTSUPP; if (copy_from_user(&range, arg, sizeof(range))) return -EFAULT; - if (range.start > total_bytes || - range.len < fs_info->sb->s_blocksize) + + /* + * NOTE: Don't truncate the range using super->total_bytes. Bytenr of + * block group is in the logical address space, which can be any + * sectorsize aligned bytenr in the range [0, U64_MAX]. + */ + if (range.len < fs_info->sb->s_blocksize) return -EINVAL; - range.len = min(range.len, total_bytes - range.start); range.minlen = max(range.minlen, minlen); ret = btrfs_trim_fs(fs_info, &range); if (ret < 0)