Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp292652yba; Mon, 20 May 2019 08:48:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqyD4f9qT6JBfc8WxQSaOg85IlwAXRyQq17fYjlGP2yLDMEU5D9hGkZuGcXtL3+p3uRcApm5 X-Received: by 2002:a62:5f42:: with SMTP id t63mr38575537pfb.83.1558367338123; Mon, 20 May 2019 08:48:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558367338; cv=none; d=google.com; s=arc-20160816; b=MLuIzQy+I32qKNwTpCpPgCeoYINFVuZQQjKFGAIWpT6ZrjkTy4HInGlwl0J1zmaPBj +k6ORr4HgY5n4aTaYAzPilldBRZn6v1nbkEU8oFRegCtBfZ17SiDTWATaqQHGaZIrGwr yXOnoU8l7x6XTHJphLQtX/i9V5S76KSVlY+y2ABc/2UltRLYWEys8Bs/g5mqQOydrC1M WRYoQ1XskRuJXx/H3zp/6KQicJBy9H/+Tt4uRJVhIzQU26W4Y3xg7e423i8S9s4B4rRI 53xh++gbfFNMmf9x8EUmeDxZXsWGsAMMs0CttEsFgJutTZfFYpMmu79JVORs2TFF6e/J amIQ== 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=QD+xKEduKKYxtfMHjfT27wOWToEZMspo9E7XzCirBG4=; b=qastXOx0haMWmnQ7cGTG9rhh4ZAk8Z4fZ4SImZuDbJRqixpsckKcCRfrg2vygWwSTK xPiUfvbeRbPbpNSrQWkI1O04+XD/Ilqc5+gvGRrkFMt3X3/q6fs0hVpXbuem0wYuHFJ1 e6TuMsbpYU0pg6ctpRgKM0CFgyeg29oBivqs3UnzuZWYVZkgTDzQQuVZah9jq2dkW5Ni uNyGIjQo2USc6T22fW48ihnHqCstpsCpmbG9TRcLJttDMEnjce0UnDS8l+qHGvUENOVz hA/xciFkCJdVGcZnrJWuCqrXqIKCCmzjqWbtSPjh8rZqCHzykeWrJ+5zqH3uuIA4Wl/8 oSjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=hYlXsZMs; 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 d2si17612951pgv.315.2019.05.20.08.48.41; Mon, 20 May 2019 08:48:58 -0700 (PDT) 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=hYlXsZMs; 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 S2388770AbfETMYK (ORCPT + 99 others); Mon, 20 May 2019 08:24:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:38908 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387451AbfETMYG (ORCPT ); Mon, 20 May 2019 08:24:06 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.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 DE21A20815; Mon, 20 May 2019 12:24:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1558355045; bh=A70ERJCpw2XeA0ZmvbUTTGlAu2BvYj8SBPLlPWWiQOI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hYlXsZMsmjI1lSac2KhPIUAkVm1yIqXMWh2irG6Ci1LdYN6Q1yVH0mLsjX2ebl1jQ fFn7Ja1WsQWUPEXorSvhFnt8fsrKNHSLO8R1xML5PK4d73c7uxkHDECQ7UpEoBQICA uyftb2/GLk93t5K3LI0LPofXpCgJKlab3x+wP55o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nikolay Borisov , David Sterba Subject: [PATCH 4.19 075/105] btrfs: Honour FITRIM range constraints during free space trim Date: Mon, 20 May 2019 14:14:21 +0200 Message-Id: <20190520115252.400703952@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190520115247.060821231@linuxfoundation.org> References: <20190520115247.060821231@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Nikolay Borisov commit c2d1b3aae33605a61cbab445d8ae1c708ccd2698 upstream. Up until now trimming the freespace was done irrespective of what the arguments of the FITRIM ioctl were. For example fstrim's -o/-l arguments will be entirely ignored. Fix it by correctly handling those paramter. This requires breaking if the found freespace extent is after the end of the passed range as well as completing trim after trimming fstrim_range::len bytes. Fixes: 499f377f49f0 ("btrfs: iterate over unused chunk space in FITRIM") CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Nikolay Borisov Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/extent-tree.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -10789,9 +10789,9 @@ int btrfs_error_unpin_extent_range(struc * held back allocations. */ static int btrfs_trim_free_extents(struct btrfs_device *device, - u64 minlen, u64 *trimmed) + struct fstrim_range *range, u64 *trimmed) { - u64 start = 0, len = 0; + u64 start = range->start, len = 0; int ret; *trimmed = 0; @@ -10834,8 +10834,8 @@ static int btrfs_trim_free_extents(struc if (!trans) up_read(&fs_info->commit_root_sem); - ret = find_free_dev_extent_start(trans, device, minlen, start, - &start, &len); + ret = find_free_dev_extent_start(trans, device, range->minlen, + start, &start, &len); if (trans) { up_read(&fs_info->commit_root_sem); btrfs_put_transaction(trans); @@ -10848,6 +10848,16 @@ static int btrfs_trim_free_extents(struc break; } + /* If we are out of the passed range break */ + if (start > range->start + range->len - 1) { + mutex_unlock(&fs_info->chunk_mutex); + ret = 0; + break; + } + + start = max(range->start, start); + len = min(range->len, len); + ret = btrfs_issue_discard(device->bdev, start, len, &bytes); mutex_unlock(&fs_info->chunk_mutex); @@ -10857,6 +10867,10 @@ static int btrfs_trim_free_extents(struc start += len; *trimmed += bytes; + /* We've trimmed enough */ + if (*trimmed >= range->len) + break; + if (fatal_signal_pending(current)) { ret = -ERESTARTSYS; break; @@ -10940,8 +10954,7 @@ int btrfs_trim_fs(struct btrfs_fs_info * mutex_lock(&fs_info->fs_devices->device_list_mutex); devices = &fs_info->fs_devices->devices; list_for_each_entry(device, devices, dev_list) { - ret = btrfs_trim_free_extents(device, range->minlen, - &group_trimmed); + ret = btrfs_trim_free_extents(device, range, &group_trimmed); if (ret) { dev_failed++; dev_ret = ret;