From patchwork Tue Sep 3 07:16:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Stefani X-Patchwork-Id: 13788154 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 7A78A1D6C48; Tue, 3 Sep 2024 07:16:40 +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=1725347802; cv=none; b=kIpV/kc99M7dIeRRSK7JWRwb/eBqtEalDY76fLYBN6EGXvl4ESZFcj+rpA2Aak5nB9+UJxMjwLc3alUQ3WQskqBKNjBAep9slpmhb4sT9SI32nUDhWvPJiVyeHJitIiKPoGTrh6FigP3vuu6epRLTnAJvq2m/FCd4uxxOgb2OUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725347802; c=relaxed/simple; bh=+OQdfPxxcjTD3+sUfVA/9T2w8bAdEbP+yqDYsBHnQzg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HhJV3/j0PAG8IJ0IJqGUGSHXyn1VuZ6aZduC91v0caHPoCDh49bDkBHt94/kPzF/azupAik5NKHpNOBGVxrsCDOpOd+iyQQf77Ait5rJWubiuHK+KACONIfZ/xEb6w6DLaJD1HUmom6Gx1Jhxl9MJUrtu/VOmJYz1U8YFeODuvA= 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=KYnTtFnh; 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="KYnTtFnh" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-374bb1a3addso1914365f8f.1; Tue, 03 Sep 2024 00:16:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725347799; x=1725952599; 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=KYnTtFnh2Edx9U9/T99U5E6ZWHNdwMz45yC3cVAkGQNS5bbXmIsCfizrqZPpRkVEZe TD1DQ63zNUYMQalCXcOWeA3ipyhqyilw8Ct/9N63vLPywV68WQ1hb1VPfLwggRLQoaXL z5P4CYc13jzSOELrw4730Gb0O3haDTpfeKHhobATbFHM8r4NhsdsjWzFFw1yM0ZfrE30 f6ajkNJCz25dd5eGNfDbu+sCnqOBSJ6EBJzgo6YPKTG/n+xhem5HN8IkmBW1j2VbmZgk TEtwCVKTWUrOe4RaVJLCURJ3YrOAQX/yIlIfBGLri3pHeqcoc+eQkxIKyhCUAy76p2Az /4xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725347799; x=1725952599; 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=cGPy9r3aCm7wb1fV5QmlvJT4IK7AkeJdyab7iG/3AhaXafWU/V4yVEgFxOXAQIfQxo H//g+halB70kSBbx9ZINqZFfwaQEvuhpmef1P4e5tHA/SvVbBOFYsToAOIwCJ3Ot079W sQ6RO3fLArJ1wUCWf3Z7sIv9AWdiCs9hd+809+NIvmbUgLKPlRC/NO1qal7pt15Yv9Vq NbUvn4lXZ1SVRgezivmNIsfss28mMfoF9O+xJwMPiROH+6JN2eBAe4Wzz7CDz8rgWLUK CidhI1xNY7RrwR3nqjA7g6olDJvDzjs3Hy/YqM0M5N8em48vXoRqESby9bgLYINPMzy2 KeDg== X-Forwarded-Encrypted: i=1; AJvYcCWcHJK6eZkz159aHzJVFqTo7yX/1Mzvs4mw0hPRt5AxJ9y86qWjKHgMyzrKTWJheeu3NPooVC3YrlBRmQ==@vger.kernel.org, AJvYcCX2TAEmoj/qN5ToIeXEhveRmrjZU1CUhyl24lMqzUHxguCauAL4yKn76sCyKPYv7Kqa5vz/yhHOtKCaWgMq@vger.kernel.org X-Gm-Message-State: AOJu0YzTW6tSBuNonfB15LyDYDwol/OXcaNqtapjnmR174dlCPUI94ba 7hUQtWnlToVKxQWqIs+xg1HHiiGahVz/WJLrT3qKf6AvNVoIPJfP X-Google-Smtp-Source: AGHT+IE3qfgEJWSXt4JsHiQ6wXgNrsQ/B6JmvLau6Ed8BoyGcY6uxABDNzyVFgsHpouelO+9MZhtfg== X-Received: by 2002:a5d:47c6:0:b0:374:c287:929b with SMTP id ffacd0b85a97d-374c28793aamr7035251f8f.4.1725347798392; Tue, 03 Sep 2024 00:16:38 -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 5b1f17b1804b1-42bb85ffbadsm154349815e9.12.2024.09.03.00.16.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2024 00:16:37 -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 v3 1/3] btrfs: Always update fstrim_range on failure Date: Tue, 3 Sep 2024 09:16:10 +0200 Message-ID: <20240903071625.957275-2-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240903071625.957275-1-luca.stefani.ge1@gmail.com> References: <20240903071625.957275-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 Tue Sep 3 07:16:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Stefani X-Patchwork-Id: 13788155 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 CF3C91D6C78; Tue, 3 Sep 2024 07:16:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725347803; cv=none; b=aPTwP0qNVTN82c8hll50qgBQHa6qFDVY/ZcGtZvM4eWs5LeP2WuabQfUGGSu40FLtUaQ8T8P5ooU8faUZzsA1BTz35uiqIy8HD5syyJNF66Y/PzlDyQLOCNSdrDv/kY6cCb1NoJVttSrH0enuXhenZ18zBAmCEF1IaUQR9xwhf4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725347803; c=relaxed/simple; bh=ThNtPFXL57A51d99qaXTeauCFubif2+4fiwjdImBiJM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=twGpJFnUWoZyFThNlkLRfR3O2uuiF8iBAJwGlfv2dMquvXQpwJCmVlDht/bD+UTdgAi+XUE+BntTRFpJIb/07rcXzdZUFBqFBqX+4/thKECSd7t4PrQbNBcjNK0diyrwYhDHng+dkuXUrAc5LpjU4d724Uf4xl7vi46cqKEp6CQ= 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=M06K5cS2; arc=none smtp.client-ip=209.85.128.47 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="M06K5cS2" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-42bbf138477so28551895e9.2; Tue, 03 Sep 2024 00:16:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725347800; x=1725952600; 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=ryqZJRWkyu5qx2NdJr7139fCsE/EdmFwuSbUl8OhEdM=; b=M06K5cS2Pi5CnKgu3QJ+hx26Ry+TLKCacSGUg7Qy7BWaLDY8fMii/NbPXmUI9Isdb9 92CgM332beOntqelnYfsinqV9YJiXHM0WteXL4XBJQ+za8Sw5W4+pQ7mltWNbP/9BqOh qRyZHesLmDPAlLN9O05ZyCmOwD8khgf2C0SVxlq001shMwm017cfRyM/Bhmgs5Y4qTE1 OfsqUOFrmeR/6u/ezui4DkVjvyYxiE9xjWf8mxh9ZqC2ydM9gVtDJbLlkhLJwhvaDNKd MW3tJI4A+6PGc3afhswR0L+cZQUV8QkcywfdClkDSTXA6swBG8FdW/TrF1tgc+paJj/G m+wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725347800; x=1725952600; 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=ryqZJRWkyu5qx2NdJr7139fCsE/EdmFwuSbUl8OhEdM=; b=FQyT+55YBv2wAbeLgLZUJ3Nm1zR64bcDMMJkbIMAr6SXs+fQHTXGun87qkeKRbqhBG +GWp8f0moXQkcbxnUY1NRV6v/Uixx9zTcHbOXyQiz8e8i7c+TDDxG1T9DTfMIt6bkL1l M0TzAUGIBGEnAweJnpecOr4ZlFXv75SS1OeVgMbsvQPyfUi/iB+T7c9cB6ExReR6KZDy KjJLasLuSKs9nv7/Pyz25yeVpOd/RHHSycfwtEND59cgoCslQ6hiYzgwBMiTzOeJXvB3 E9sZ+UkGDLOMAAAm3BNiWcn63GMJEfms90hmtaQSaUhpY+5FajZE3bqXAN4I+HYK745i 85Pw== X-Forwarded-Encrypted: i=1; AJvYcCUFuDiAq0YP721RI2dXloCGwut0ihhJvATO1Z6FBqdh4IWMq4nd/H7qnNq+ueNjxEkKX43anWjD6hHFUw==@vger.kernel.org, AJvYcCXQujuKVvWEMd6gSZMBzFlngOnM5zhUELbyOVBNjM9USnaWDd6xV7KFhHH+EvDg4NGhsnbERRropvqFSRdt@vger.kernel.org X-Gm-Message-State: AOJu0YzNezAoTVDtex941XmhoxOD4s5zDECywLwP65rcCMKaqRhUjZHw 35yVhxp6uJ3JBWFT3KQUXPWDzo97m5SJdNjyyrByYVdgSEEfFTZL X-Google-Smtp-Source: AGHT+IHclp0SP6+Nzye3WHRy+v0L2cVP+Ta1KlUQWveQ37LmGxy/YIEIQll5uQnwvAankg4Dmg/vAg== X-Received: by 2002:a05:600c:3b08:b0:42b:a9b4:3f59 with SMTP id 5b1f17b1804b1-42c880efb93mr16434265e9.14.1725347799827; Tue, 03 Sep 2024 00:16:39 -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 5b1f17b1804b1-42bb85ffbadsm154349815e9.12.2024.09.03.00.16.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2024 00:16:39 -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 v3 2/3] btrfs: Split remaining space to discard in chunks Date: Tue, 3 Sep 2024 09:16:11 +0200 Message-ID: <20240903071625.957275-3-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240903071625.957275-1-luca.stefani.ge1@gmail.com> References: <20240903071625.957275-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 the block discard limit 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..9c1ddf13659e 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -1301,12 +1301,26 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, } if (bytes_left) { - ret = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT, - bytes_left >> SECTOR_SHIFT, - GFP_NOFS); - if (!ret) - *discarded_bytes += bytes_left; + u64 bytes_to_discard; + struct bio *bio = NULL; + sector_t sector = start >> SECTOR_SHIFT; + sector_t nr_sects = bytes_left >> SECTOR_SHIFT; + + while ((bio = blk_alloc_discard_bio(bdev, §or, &nr_sects, + GFP_NOFS))) { + ret = submit_bio_wait(bio); + bio_put(bio); + + if (!ret) + bytes_to_discard = bio->bi_iter.bi_size; + else if (ret != -EOPNOTSUPP) + return ret; + + start += bytes_to_discard; + bytes_left -= bytes_to_discard; + } } + return ret; } From patchwork Tue Sep 3 07:16:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Stefani X-Patchwork-Id: 13788156 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 3B104200137; Tue, 3 Sep 2024 07:16:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725347804; cv=none; b=OzjdjQMvttl9En63tWqhXB/iP3qi9nCi0GQKn9MedX8auX+kjZit+1/08lq8zfzA9mCf86P8ug1pxGdvYRwtDtHOb5stJ4aoBhSpzrtGeN78sI4cwZlcY+8M2JxIIFTjMl7jzihPawN/X6CjAF11SntqthQ6O/nHT+mzekVQRjw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725347804; c=relaxed/simple; bh=t0LBXBgOaO2ECpAMsZRr7vHDdgrqd95DMqPwzO4EzlY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nYQccp5ryEKmLCIoWJiXD9h1H9ukzvuERIcDkV/JfWwhrf6PtQR/jkTmKbqHSQY6iov+UK0PaX7tgnPKrmNwdDqHhMmQ1klIG/Cqyxi8jERQrifWLL+L29l5qBpTJ5q5UYtK6xv+a2IhG3/Rl605t3H++CRalSJCiMFYNSAqNqE= 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=AMxxslRM; arc=none smtp.client-ip=209.85.128.42 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="AMxxslRM" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-42c79deb7c4so21653015e9.3; Tue, 03 Sep 2024 00:16:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725347801; x=1725952601; 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=NA7sQJMESOQdgSHhoH+FFgeDcGkcGI0Haw9TeaBz/rY=; b=AMxxslRMXcCGc9+lK9T9zkZYDsks1YIa/dilPy2kwXUzpReJjeLH08BZz51wYXO75X EzRItZ04sPjLWZDtd9UnZvMNVqKd+fDAPcr6Vg2dktOVw+QYjPxGufZ6TmcHVEBTHz1K jK2qSEmSofU+6VxygGpVRtphPoAOu+AczazNBWD1i4fwAs439/+a0chQjmfOemraybsi zaYwP7PGYNFonRIOJgE6sirjYHi1nhINq4mfERR3b/+E3/YHrafurE0+Pqg4yfx6xdjs a0bww0JJS8gnEgpoz0HbE+UUcfKlCySI0+PovGp4nVyMj+OrYxod6284qNcA4qV3h22H 0OZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725347801; x=1725952601; 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=NA7sQJMESOQdgSHhoH+FFgeDcGkcGI0Haw9TeaBz/rY=; b=FGaz5/gGJEyzt4PEmZISvP1XsuUbI7XcQK92Sm5ubjJ6o8ORFYdH1e8DUkxe/KJ8rw pOD0FBM/ZS46BySMcIBItEbN/FAAeK/ZOrG6asgLw0zgjItVfZe05eOBF85Jg2iU8/IB jJACn3/xl5rUaAzGH0OUDomtKMATc+0uRjjgg9m63mVwK7SU0s75ndNV4oBxaDH2oD3F dTfx8UexfCYNaaJVnJHOsQNtA4Sy+iGY0ZuBze9aCI6bPsQ9/JmNK/Ew9SIZhhs/tH7O awAJ1ncBpeVkwkSqKnyqOOg06gt+i9ykB2mZgrEGZBloRviDTrqYI6rqirEe+7CHxvUH V/8g== X-Forwarded-Encrypted: i=1; AJvYcCUK6QxmGMBKc5FBt2VrcFC175soOVzkOF5RWffROa8uILagCgZD/6IIL1mLYGlij+OtjXBzNBzg8/eRmwII@vger.kernel.org, AJvYcCXirVFv0V41tscxOVAkVYMFB6ZgsZKwHP3ksc6ydqIPHbr5SlpW8OPOFkb3JcUFciedic0dAYnXRypZgA==@vger.kernel.org X-Gm-Message-State: AOJu0Yxadq38YyPjzhmd4+KM5g3uHSkLAzOmprjSSweYKX8d+l9cjZ/O v/u3Epxc6gYFbZvfYRZB+dG5t1R277riGx7Ov3Rx0LqFbLtdliVTxCyLvGTz X-Google-Smtp-Source: AGHT+IGKQt9P5x6UXcDe6UeQqLXTdZl34817rOtn8lmmuEpEHcm1kidHxUNKPUvSywQqhF+0BaDqgg== X-Received: by 2002:a05:600c:56cc:b0:42c:87dc:85e7 with SMTP id 5b1f17b1804b1-42c87dc8765mr20936545e9.18.1725347801451; Tue, 03 Sep 2024 00:16:41 -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 5b1f17b1804b1-42bb85ffbadsm154349815e9.12.2024.09.03.00.16.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2024 00:16:41 -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 v3 3/3] btrfs: Don't block system suspend during fstrim Date: Tue, 3 Sep 2024 09:16:12 +0200 Message-ID: <20240903071625.957275-4-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240903071625.957275-1-luca.stefani.ge1@gmail.com> References: <20240903071625.957275-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 9c1ddf13659e..ee4daaa56b95 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) { @@ -1318,6 +1324,11 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, start += bytes_to_discard; bytes_left -= bytes_to_discard; + + if (btrfs_trim_interrupted()) { + ret = -ERESTARTSYS; + break; + } } } @@ -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;