Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2523793imu; Thu, 29 Nov 2018 06:23:18 -0800 (PST) X-Google-Smtp-Source: AFSGD/VrCMRRSRXoqbeBOGIwTGQ+TJNJgIeBwriWlYAGaLJbtHCONUKD5l1Uvt1D/lIm9HBXpiYb X-Received: by 2002:a63:7418:: with SMTP id p24mr1434905pgc.196.1543501398314; Thu, 29 Nov 2018 06:23:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543501398; cv=none; d=google.com; s=arc-20160816; b=OydfQCgT+J5XuknmRODRqvJ4tydoETnBBojdM2tJPZ+Nd22ZQNTxMbqStLJfxKJbof O6N7ZmFc8WXwxgw9jnP8K3f9VT3c/Anyn2ulqKS5Tk1G18WF9hiHLRwdu5eTczdy62Id CiVX2jDGZdUf0a7xew9BpbBL4owWSAo07ISWuyHXtKbwl/ogP0TH5VPdmeoJa2HY8MEk Zoyqfa74RiyjzQWsLCwWTB9Qpo/la49A/Q5zq2/ZGP7g/aWvGr/LfNWc5X2v6Cc2fUJE 2mdwNbNiBx7dEStv7ZutgBJYoYP8jQ0W+wqu1ZBsRMtPNoK+NMdQPAdA5PZWRuo7KZ24 Ly4g== 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=zKGMKzfI51CxXRAT40eIZk/gwYwHbeBznWrHN/QJNEg=; b=uzGzhHq2fH596kUI8BW5ozEGtmPuZGVE7h2NcFIMZtix0gkd0BW9eWQyuseFbleYfA jEbBKvMrrMSxyUqgnOps23cjVqLvnwkU7CRLrqnA+1zxWXVMXk/wQ8qFvZfplUtrM5ik vT6pTrWCQRrWFRDDmSwRajVorObBu1nbDBEybQBKQ+Kg7mKpXgi1Ome1IXqENDdpDbcg EESCoSiEpZ8tT/dppmfmDo1H6CGexiot73XHOtUgsz5O5DwO+oClaXDddeqp3eMp5RP+ 8UX5P7IATG7NLnNsI4Ei+lX97dsfelwdMYZug+rOkOwbbD4ra/BJKSJOii3S8v/XSwWp zTWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1clyfOg+; 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 76si2360097pfw.66.2018.11.29.06.23.03; Thu, 29 Nov 2018 06:23:18 -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=1clyfOg+; 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 S1731239AbeK3BZ0 (ORCPT + 99 others); Thu, 29 Nov 2018 20:25:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:48830 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729806AbeK3BZZ (ORCPT ); Thu, 29 Nov 2018 20:25:25 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 3551D208E7; Thu, 29 Nov 2018 14:19:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543501193; bh=VZRWVEgLHfY4MaREpi+s/vj5lVnUek8myFpCjngm9tU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1clyfOg+A/uvcIlnTyRn//o5joa8vJWUjtNo4THiQby0l3LxizLFV+JN9pI3OFjAG URmdYT8FrM77Fu7BQM8BWRtVq2Vy08mu4db5o+ntmdBT1VR7xECcHt3rRwNWzCy2s5 DXbKy5gKD0L85SAzvCNX+OsMp6V5pPyY2Ujz2ptI= 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.4 82/86] btrfs: Ensure btrfs_trim_fs can trim the whole filesystem Date: Thu, 29 Nov 2018 15:12:47 +0100 Message-Id: <20181129140116.297281513@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181129140109.832117862@linuxfoundation.org> References: <20181129140109.832117862@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.4-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 @@ -10708,17 +10708,9 @@ int btrfs_trim_fs(struct btrfs_root *roo u64 start; u64 end; u64 trimmed = 0; - u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); 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); while (cache) { if (cache->key.objectid >= (range->start + range->len)) { btrfs_put_block_group(cache); --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -378,7 +378,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)) @@ -402,11 +401,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->tree_root, &range); if (ret < 0)