Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3468854imu; Sun, 11 Nov 2018 15:48:59 -0800 (PST) X-Google-Smtp-Source: AJdET5fJDUcF28GSYIzAJN7cDpk7+kL2mIfQekHkPFbr73q9FmLcBd0MQDB3yHy12MXQFDltr/qW X-Received: by 2002:a63:b16:: with SMTP id 22mr15401360pgl.306.1541980139211; Sun, 11 Nov 2018 15:48:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541980139; cv=none; d=google.com; s=arc-20160816; b=mprAvUZRfK5ev90ffUNCCtWIrD3hmiWECRRbK7Wj6YCoHsT9rtcZVkeAmXbtwRtKa0 xQuKh2xUrMYYqBvcwwk9OiCpC0xmDD6f42wfUX2vJmv8zdxNUyiZYUvfJywYKKyv/JeO OSc7HPtPOg4CM7wP57REqy4Q5wNdHZKA1lPknkj2DoNtzcJfO9FCZHUhEpnhyMmqX2qf apFdQNeDw5YbG1W/0lgfi2V3n5GK0w8tAlQaWzhoiM1WWWLCUDnaIMhJSoz5wSFfdEQS sR9xlSY410icvmVWKIYRe3/J2OQT87ZQkbYzzE67ecuMSSWUEuj9Dm4gRMfL6kTBgO/l Ugbw== 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=btvHeIKzAsvkrg24BsLSS5p7rExUxHL8prmyVAuQILM=; b=h3q6GoA0DU+gTji/6KivnE04YfIIUqHoBJBcnEht0OlD0y21I4Ucas+2iYRBEH1wXa droM3CBqoDeqzhpwlgnfQWPxwRxj6E5pB03Kfm6t0pyT+QWYVQk6au/H4IjmMjmr7mkm TeHr5EqMGhdjHMVXFXNk67DtGVXxhNwJOHtUYn86Vxqg4H2dF4wQ5KQFZFcq0nB5RXt+ JhLpBepIZ41EOjoTLB+14x42ePFbuWV3h2AKkHkNAGFQhQaWIZbQLNvFfvFQicSMUJAJ t557ZLhnFQLCOq2Twt0SZQX9fVZ/V4vHkW6U7lpqNf2yXYTVZf97efmtPAlvtrOYfJQz GRyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=P5QDJjJM; 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 129-v6si17322620pfy.164.2018.11.11.15.48.44; Sun, 11 Nov 2018 15:48:59 -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=P5QDJjJM; 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 S1733112AbeKLISn (ORCPT + 99 others); Mon, 12 Nov 2018 03:18:43 -0500 Received: from mail.kernel.org ([198.145.29.99]:38848 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733055AbeKLISm (ORCPT ); Mon, 12 Nov 2018 03:18:42 -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 6B09E21707; Sun, 11 Nov 2018 22:28:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975325; bh=GDLb0FvJVwosa3sIGuSIdPc0VLyr6S6YLrkVdBIZvRk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P5QDJjJM/vozuoBMMkUhd6Qt5oCsJ6P9R/11IwK3cTVebKV5Ga2GCe+LdKiT7MhE8 /dM9/BamKfIyDGD9bFcnlJhUdpe02/FseEJRfd6J0ZSJqfzit2dob/PyJ2OV0G4+o6 3U2rSAY6UKxaaghpCbNpqqNXqiYokZDMv80D6bpo= 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.19 332/361] btrfs: Ensure btrfs_trim_fs can trim the whole filesystem Date: Sun, 11 Nov 2018 14:21:19 -0800 Message-Id: <20181111221659.986765206@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221619.915519183@linuxfoundation.org> References: <20181111221619.915519183@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.19-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 @@ -10868,21 +10868,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 @@ -491,7 +491,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)) @@ -515,11 +514,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)