From patchwork Mon Sep 16 10:16:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Stefani X-Patchwork-Id: 13805237 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CFC82146D7F; Mon, 16 Sep 2024 10:16:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726481789; cv=none; b=RSwAmeXg3TQOXUn4CrQfXk91GWfbkPa6vYxKVejGTv6yFnRzUd7HyeQjgUzFomib2fTSiB1KnAVfa208rft/6jr+PmFyQdoAkZy/DOODIeAAboo4mpdEXyhDZMRj+69FOHDdz4OzPFo1aDEea3MCPzkqK1jk2HVenBPqKR41Nyc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726481789; c=relaxed/simple; bh=+OQdfPxxcjTD3+sUfVA/9T2w8bAdEbP+yqDYsBHnQzg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sRBq0OiUNGC93irLZbBwsURhMcn2ewwxmQVFHG0TGh15GkUj/Bz98277QGeZ9rt9qhSOGUbHmsCu5pq/uVlOgZVvCNbmtNBmUbcUf70icgiqafB1Eq62GEUywC03ebz/7Fy4i0djJle89GnaqhsWfbSaR4yq5U1qknKvEH24V5A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MhMdrmsr; arc=none smtp.client-ip=209.85.221.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MhMdrmsr" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-375e5c12042so2299236f8f.3; Mon, 16 Sep 2024 03:16:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726481786; x=1727086586; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yjiEQutbL4t9l0my21gE0YieX3qenAsUUpmVjhWTT/Q=; b=MhMdrmsrfnHpHfSqFIQzYywCCX+dDRE51WclsaBI3T55A2TThKuTD+WKlmbK2MbV1x YCx+5uIZYQEErvESNEI/Kuxh09JdIBaemMcU3EFhNnZP2vgvqh1YYY8vnUgr6XEqfdxa tunp/oCmQmaY0Ynjmq3Ank2r52NdOf98abVrb4AyVqhA6bN5NzttwGnbu7guDPgmy80j AYw5oxD5H98kAhOMSZNeSywQDGW4ka15kJhSeLzhmTshFklTkn8AylEeaczY1CJzBYNb 1kfmXVfJtV7nTCk68Jauy5SUdC4rSRM3Ro4e5xeTo5TEU6Gll/7QrIsqRSY9fU0yzZvp 4ntA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726481786; x=1727086586; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yjiEQutbL4t9l0my21gE0YieX3qenAsUUpmVjhWTT/Q=; b=SJDTKtfbPHJqBFlHdQrFExCA0jJ9fLKqKiBYj+4UQ+AbYc6ad6/ho8HJKFE9E+iOUl JjowKgygAB5iKr+DZmYr6sco3KRdthGpuEebMXs8MNVCuOeREYhyYrTKaziFNRRlVdAr dGZnyc/aTGHIlXRSMJH3Abam/Do0vWXueNW19k24IQq5UNHDgtCO6wPPYYanNzPH1U1l h99xWSvE01iH1YJev+WaO72kstzhN77FL0iPFafxW5fQaL4EtRHqIK2xb/Z+IjifmhHT 4lQrdQPiQ2Phc15Ijjopx2DHp3/shIfTBefGxDDGQ+SJFWIDhzTWW7YUm7bMQOQKppm+ fwxg== X-Forwarded-Encrypted: i=1; AJvYcCWJJ6K3HXGXMGX1RwQMBsiVBZo1cr/ioKkYhl0le0Of0MG/sm3/tiIdv0ITrnBXPJcewCLdb+J1tt4quA==@vger.kernel.org, AJvYcCWybFcQkmeLqEUXoUVb4NGYZG8+f/GGrcAHUUMZzF5qkc1yLPTkPuFIg8IjAX2WuQeXXhdCMyVgiiRVoreH@vger.kernel.org X-Gm-Message-State: AOJu0Yx8ux1AEWmPp1uP4C7btP9dERreD2m2c+PBNaiq47x668+Ic1DC Wpg0g8euq7Vdg9tdDnfZhaybuM2wXH89NOFk4bzlSefA1enyDslx X-Google-Smtp-Source: AGHT+IGgFcyea0E2B0p7cwubLhqDr3i7lH/J8TsgQqGcCIhGCf+OChT6svaZ0M5/uCYtOr8VOhPaIw== X-Received: by 2002:a05:6000:400d:b0:377:683f:617c with SMTP id ffacd0b85a97d-378c2d07364mr11691299f8f.23.1726481785737; Mon, 16 Sep 2024 03:16:25 -0700 (PDT) Received: from fedora-thinkpad.lan (net-109-116-17-225.cust.vodafonedsl.it. [109.116.17.225]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-378e780015dsm6814201f8f.69.2024.09.16.03.16.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 03:16:25 -0700 (PDT) From: Luca Stefani To: Cc: Luca Stefani , Qu Wenruo , Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/3] btrfs: Always update fstrim_range on failure Date: Mon, 16 Sep 2024 12:16:07 +0200 Message-ID: <20240916101615.116164-2-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916101615.116164-1-luca.stefani.ge1@gmail.com> References: <20240916101615.116164-1-luca.stefani.ge1@gmail.com> Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Even in case of failure we could've discarded some data and userspace should be made aware of it, so copy fstrim_range to userspace regardless. Also make sure to update the trimmed bytes amount even if btrfs_trim_free_extents fails. Reviewed-by: Qu Wenruo Signed-off-by: Luca Stefani --- fs/btrfs/extent-tree.c | 4 ++-- fs/btrfs/ioctl.c | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index feec49e6f9c8..a5966324607d 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -6551,13 +6551,13 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) continue; ret = btrfs_trim_free_extents(device, &group_trimmed); + + trimmed += group_trimmed; if (ret) { dev_failed++; dev_ret = ret; break; } - - trimmed += group_trimmed; } mutex_unlock(&fs_devices->device_list_mutex); diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index e0a664b8a46a..94d8f29b04c5 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -543,13 +543,11 @@ static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info, range.minlen = max(range.minlen, minlen); ret = btrfs_trim_fs(fs_info, &range); - if (ret < 0) - return ret; if (copy_to_user(arg, &range, sizeof(range))) return -EFAULT; - return 0; + return ret; } int __pure btrfs_is_empty_uuid(const u8 *uuid) From patchwork Mon Sep 16 10:16:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Stefani X-Patchwork-Id: 13805238 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0519514D2B9; Mon, 16 Sep 2024 10:16:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726481790; cv=none; b=a49ccJvRXt9kU/YlinqETONcyJlTaG9BYs8W1cjEbYLnzLq8J650lP6/fFWEtxa5vK2V8Gq5biYCSbrX1wkq8Es33KiGnJx5kDbDDXGSLAZLltlfvTqbKSjQfastGHAsnBY0d1NaqEvUIQgH7RhpTGVBtfXjS0vP6T8LmN6HHw8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726481790; c=relaxed/simple; bh=1MSuuVdW6jpVBdVadZ9EKMcMMKnIRPuWO1ABjXKOiyI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SVDSxQWkC4mXlZGwbhDIPIZYb5t38A4ZTxIQDYQnpXiRg3tYhT84PHmebtBLPGfA8enj+wBOvyhYsVkyjVXcpYiXpWqS/HANlEN+kGENO78pfhSpByKdtigO+5cxKvTaniRsYBeaYcLuLqpKxY+SxzJHr/SG2KVKxiCZjE93onw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DVtWhQpI; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DVtWhQpI" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-42cb58d810eso32883435e9.0; Mon, 16 Sep 2024 03:16:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726481787; x=1727086587; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LR1zaljeSJTP/u3Pvb00FWZ1kLoJ3Nj8Q86hbGUAQ0k=; b=DVtWhQpIQ/cKzoe9bbh3ELwTqrRYHi24ZcRAue6cRtxBsZNvgemLXOMMPKJDJo7iMz ZPnnakyZcy0zFK4mQqZy9pxGTkcQgK+Qypz1KrrMSaQOIFKZZ2xri3F1krzEAyzg8kZM W+IzIbvLDGn0IdL+83B+v4OV494VRdaylDscfAlwV/BKlaWGVAj4Q02VxRVDkA5UTtrQ OkvCM2PHAzY5KaQRhHjryyQpQd9CQoNei1s+fdtGKyL0/52zeA3EiRxSpA9svLEK5V39 P+1eTW6LBZF5rYvv5cGWNNVTc1Jyb4w1+9AvTDWlTsHk7yfSrlRdP5P3Tv/tI6C9/phC RdaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726481787; x=1727086587; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LR1zaljeSJTP/u3Pvb00FWZ1kLoJ3Nj8Q86hbGUAQ0k=; b=wT/spGKRKL98Vd4f3hCmuscf57ODBccJ6nrJyHRrz15/4O5x+Vpa2TrPTTKZ8vR9aM dg+s0gQlMKmCwVoq1btMPgy0IULQY9TLpqd9ggEGH8nITIuOIctD+leS4RQgWpGnKGLL gcf9Vkip99dOXjhVYoSekOGb7/LOQ1OEiY+OyXnMtuI55JRJg7YjPncYKcB7N0jeeBbk ggf0lg3LoLvLjUClq9v2MESL2F5gNR3fEcvk2gGVOw0MGje8RycGU8zli6QZYFTLuVrm OncNyUJ4oHmm7fKFZqBuSEWl17BfKD8D3FMCqDMxtENT29KnykLE1TcrPH1H4wd+5Fd2 W5sQ== X-Forwarded-Encrypted: i=1; AJvYcCVXqa8+ZsO5DLGNlrqc9TyB49FXepZjMBAvRFiIpuzZjiKf85/HOZwX1gfgVRhoVnP0ICXJjv3CwmTNHw==@vger.kernel.org, AJvYcCXBbKHA1B8onfipM+9ax7gfEdvS24lPyFyRx2BpFt7MMPFWQ900FVsQ6mYrHNQTrTrOKegfvfLp43BHh4mW@vger.kernel.org X-Gm-Message-State: AOJu0YwRpLYqeUuFpAtPDnPOe01ivZk12hyBC56peLccGN/z0qDe9Ij0 SBE5USMBjRYWbJG/T23/VPUoq5z4EdO/Gqd+MHogYf4lKuQ/uEIM X-Google-Smtp-Source: AGHT+IFXSUdImfdKL09QrcIA9ID3+Sw5Eyz6wR1bDQu6bL19FR2/UXvzOM2Y+jseSzgfQ+k1nsE0ew== X-Received: by 2002:adf:f651:0:b0:374:c25a:f580 with SMTP id ffacd0b85a97d-378c27a925emr8234768f8f.14.1726481787220; Mon, 16 Sep 2024 03:16:27 -0700 (PDT) Received: from fedora-thinkpad.lan (net-109-116-17-225.cust.vodafonedsl.it. [109.116.17.225]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-378e780015dsm6814201f8f.69.2024.09.16.03.16.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 03:16:26 -0700 (PDT) From: Luca Stefani To: Cc: Luca Stefani , Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/3] btrfs: Split remaining space to discard in chunks Date: Mon, 16 Sep 2024 12:16:08 +0200 Message-ID: <20240916101615.116164-3-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916101615.116164-1-luca.stefani.ge1@gmail.com> References: <20240916101615.116164-1-luca.stefani.ge1@gmail.com> Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Per Qu Wenruo in case we have a very large disk, e.g. 8TiB device, mostly empty although we will do the split according to our super block locations, the last super block ends at 256G, we can submit a huge discard for the range [256G, 8T), causing a super large delay. We now split the space left to discard based on BTRFS_MAX_DATA_CHUNK_SIZE in preparation of introduction of cancellation signals handling. Link: https://bugzilla.kernel.org/show_bug.cgi?id=219180 Link: https://bugzilla.suse.com/show_bug.cgi?id=1229737 Signed-off-by: Luca Stefani --- fs/btrfs/extent-tree.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index a5966324607d..cbe66d0acff8 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -1239,7 +1239,7 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, u64 *discarded_bytes) { int j, ret = 0; - u64 bytes_left, end; + u64 bytes_left, bytes_to_discard, end; u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT); /* Adjust the range to be aligned to 512B sectors if necessary. */ @@ -1300,13 +1300,27 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, bytes_left = end - start; } - if (bytes_left) { + while (bytes_left) { + if (bytes_left > BTRFS_MAX_DATA_CHUNK_SIZE) + bytes_to_discard = BTRFS_MAX_DATA_CHUNK_SIZE; + else + bytes_to_discard = bytes_left; + ret = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT, - bytes_left >> SECTOR_SHIFT, + bytes_to_discard >> SECTOR_SHIFT, GFP_NOFS); - if (!ret) - *discarded_bytes += bytes_left; + + if (ret) { + if (ret != -EOPNOTSUPP) + break; + continue; + } + + start += bytes_to_discard; + bytes_left -= bytes_to_discard; + *discarded_bytes += bytes_to_discard; } + return ret; } From patchwork Mon Sep 16 10:16:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Stefani X-Patchwork-Id: 13805239 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24A03152E0C; Mon, 16 Sep 2024 10:16:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726481791; cv=none; b=D2giG68cdvtEbtiPKW5oMGM/9j60IDHzRryizTE2WQEcqf/UF9ryX0Dy0+72LHXpbcsp6JzxAABG3eNU7dyOg5AJx/0FggTPeKbcG55Hlol1VOvkCmQJb+qxWrunbsimgoFHrt1jpT1jQRV36TF8L86OnNCzvma842r5PYLvIfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726481791; c=relaxed/simple; bh=Qli+h/uXJqZx6B+dTAdfsruBtooNooToeUGYDRfRd80=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NWsFlWvDdiNnxDYJA1fNi30Istr0WVjRPBFeh7A23XS2mH800h8u0+B/qDNEAbacsQNLRxtGFExzpeHFnuVRO9mcCejGRyUU6jbDdQSL6osHHJRwFsA2jTtElU6O0EmTWwJuXg0uR231FvZSxVP3AuwHPczobP6SnO+soqRjbas= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mWsvn21f; arc=none smtp.client-ip=209.85.221.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mWsvn21f" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-3787e067230so3049913f8f.1; Mon, 16 Sep 2024 03:16:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726481788; x=1727086588; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+96dCE/RH3/NNKWNE9Y9s9N6w3czRGVtUhIxMgqQU3M=; b=mWsvn21fJP7M5O7r/i9o05M7omERfFqi6FW6bQF5PQB3NdRARuZLmcemySVq4Rh2IT xpz/jZ9501l8oY3L9OKjIrkLh3rDeeTEjTW62P5cUO4wAlSZWBmuChDMhQbqoTqfN4pP vShL8C968CMdf5mooST6JFu6w/FMEY/fwNzyD6dDh2n8aIJGum+TdwoXAc4xfmUlGS8O m+TgIN/iol5KbzbRNY6iGL61vgPy81kjx/K4XtjMIteT+C0wQYrsa/5AD9CkRA4JCb5l IDdxbbDG/uQ2bCMVoz1UCP0qR2T4LQeTJIi3z6DQquFeNfdFobym2sfValQKXffc6Ou3 4fHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726481788; x=1727086588; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+96dCE/RH3/NNKWNE9Y9s9N6w3czRGVtUhIxMgqQU3M=; b=n9HmVjHx/YcNcg6UPXzfG/SMavWoepXknRYQfsmkqwDI7nyq7N+5Lrvw85HXq5CkWe gOt3+lmAEBAczgD2t6HKM2RsrhCDiooh5gBO7KYv6v/jP7OV/3Py/A8kA+7Vpf5IRs3+ i276eLpOshFc1/sfgxWMastIsY9QduxtDeZ15r8Rgl6LIkVTrjB2yYmWtZQ5HZM9AbBL 0soyAEP60hkIYBYfIHTH6J6TKvezHaspqUWv1w/lVZgxzn12dRq2GCCC7Mx6+qaKeYig m7Y04g3+ZLgQuW8gixUOB7ymme8Oq1/EHavj9G+8E8xC7uGTeRLXjyLn++z3paz7dejt qOqA== X-Forwarded-Encrypted: i=1; AJvYcCU6R9+KnybpeY9+1Nu/+s8jFspZRJ+1O8HNO+q9OAav+pppGNHcBpgWTBtrW1Y4rqLTiv9anqrzQogr0MwT@vger.kernel.org, AJvYcCWHzJmqJZqTdRiMALpEL1AOsMV942FTQG9uzIYmsxawSaLXiiYqdA9BlXBR4RWi9dtirEaBiR6TgOAwAg==@vger.kernel.org X-Gm-Message-State: AOJu0YwA1wkaka0mwikusiXvvBrEvy1QXOk86QbzjXWlh3A6//tv8BRO ECpN3DYeJZYoaEdLoNtp/K6uVmVgx9x4JozI1MhvmX1lJWz0CYmD/GiJiZb2 X-Google-Smtp-Source: AGHT+IFl0F8VDK8TI+2SWw31p5UlBQYeCPPUv9AJyKtmv/4CMqWc25Im1+rPat3RplPVB4itghh46w== X-Received: by 2002:adf:f801:0:b0:374:c1de:5525 with SMTP id ffacd0b85a97d-378c2cfecefmr9223537f8f.6.1726481788461; Mon, 16 Sep 2024 03:16:28 -0700 (PDT) Received: from fedora-thinkpad.lan (net-109-116-17-225.cust.vodafonedsl.it. [109.116.17.225]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-378e780015dsm6814201f8f.69.2024.09.16.03.16.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 03:16:28 -0700 (PDT) From: Luca Stefani To: Cc: Luca Stefani , Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/3] btrfs: Don't block system suspend during fstrim Date: Mon, 16 Sep 2024 12:16:09 +0200 Message-ID: <20240916101615.116164-4-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916101615.116164-1-luca.stefani.ge1@gmail.com> References: <20240916101615.116164-1-luca.stefani.ge1@gmail.com> Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Sometimes the system isn't able to suspend because the task responsible for trimming the device isn't able to finish in time, especially since we have a free extent discarding phase, which can trim a lot of unallocated space, and there is no limits on the trim size (unlike the block group part). Since discard isn't a critical call it can be interrupted at any time, in such cases we stop the trim, report the amount of discarded bytes and return failure. Link: https://bugzilla.kernel.org/show_bug.cgi?id=219180 Link: https://bugzilla.suse.com/show_bug.cgi?id=1229737 Signed-off-by: Luca Stefani --- fs/btrfs/extent-tree.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index cbe66d0acff8..ab2e5d366a3a 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "ctree.h" #include "extent-tree.h" #include "transaction.h" @@ -1235,6 +1236,11 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans, return ret; } +static bool btrfs_trim_interrupted(void) +{ + return fatal_signal_pending(current) || freezing(current); +} + static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, u64 *discarded_bytes) { @@ -1319,6 +1325,11 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, start += bytes_to_discard; bytes_left -= bytes_to_discard; *discarded_bytes += bytes_to_discard; + + if (btrfs_trim_interrupted()) { + ret = -ERESTARTSYS; + break; + } } return ret; @@ -6473,7 +6484,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) start += len; *trimmed += bytes; - if (fatal_signal_pending(current)) { + if (btrfs_trim_interrupted()) { ret = -ERESTARTSYS; break; } @@ -6522,6 +6533,9 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) cache = btrfs_lookup_first_block_group(fs_info, range->start); for (; cache; cache = btrfs_next_block_group(cache)) { + if (btrfs_trim_interrupted()) + break; + if (cache->start >= range_end) { btrfs_put_block_group(cache); break; @@ -6561,6 +6575,9 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) mutex_lock(&fs_devices->device_list_mutex); list_for_each_entry(device, &fs_devices->devices, dev_list) { + if (btrfs_trim_interrupted()) + break; + if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) continue;