When the filesystem gets an error outside of a transaction commit, e.g.
when a log tree sync aborts the transaction, __btrfs_handle_fs_error()
forces it read-only and leaves a running scrub or device replace alone.
It used to cancel them, but commit
1acd6831d9877 ("Btrfs: avoid risk of
a deadlock in btrfs_handle_error") removed the cancel, because waiting
for the scrub to stop from the error handler could deadlock. Only an
abort in the commit path cancels the scrub, from cleanup_transaction().
Nothing else stops them. Scrub checks only for cancel requests and
freezing between extents, and at each block group
btrfs_inc_block_group_ro() takes its sb_rdonly() path, which needs no
transaction. So the scrub or the replace goes on reading the rest of
the device, block group by block group, until it reaches the end or the
filesystem is unmounted. A replace cannot record its progress on the
read-only filesystem, and at the end btrfs_dev_replace_finishing() fails
to start a transaction with -EROFS and leaves the replace started, to be
resumed at the next writable mount from the last committed cursor. No
data is lost, but all that I/O is wasted, and for a replace it is spent
on a source device that may be the failing device being replaced.
Make the scrub stop with -EROFS at the next extent once BTRFS_FS_ERROR()
is set, next to the check for cancel requests, and do not start another
block group in scrub_enumerate_chunks(). This also keeps the next block
group from being marked read-only for nothing; on a filesystem whose
error is set before it is forced read-only, btrfs_inc_block_group_ro()
would fail to join a transaction and log "failed setting block group
ro". Nothing waits for the scrub, so the deadlock the cancel had does
not come back. The scrub ioctl returns -EROFS with the progress up to
where it stopped. A replace keeps cursor_left at the start of the block
group it was copying, as with any other error ("btrfs: dev-replace: do
not skip the rest of a chunk when resuming a replace"), stays started,
and resumes from there at the next writable mount.
Update the comment in __btrfs_handle_fs_error(), which says that the
replace goes on until it completes.
Fixes: 1acd6831d9877 ("Btrfs: avoid risk of a deadlock in btrfs_handle_error")
Assisted-by: Claude:claude-opus-5-5
btrfs_set_sb_rdonly(sb);
btrfs_info(fs_info, "forced readonly");
/*
- * Note that a running device replace operation is not canceled here
- * although there is no way to update the progress. It would add the
- * risk of a deadlock, therefore the canceling is omitted. The only
- * penalty is that some I/O remains active until the procedure
- * completes. The next time when the filesystem is mounted writable
- * again, the device replace operation continues.
+ * A running scrub or device replace is not canceled here: waiting for
+ * it would add the risk of a deadlock. It stops on its own with
+ * -EROFS at the next extent or block group it looks at, where scrub
+ * checks BTRFS_FS_ERROR() next to its cancel requests. A device
+ * replace stays in the started state, and continues from the start
+ * of the block group it was copying the next time the filesystem is
+ * mounted writable.
*/
}
* indicate the reason:
* - -ECANCELED - Being explicitly canceled through ioctl.
* - -EINTR - Being interrupted by signal or fs/process freezing.
+ * - -EROFS - The filesystem has an error and was forced read-only.
*/
static int should_cancel_scrub(const struct scrub_ctx *sctx)
{
atomic_read(&sctx->cancel_req))
return -ECANCELED;
+ /*
+ * The filesystem has an error and was forced read-only: a replace can
+ * neither record its progress nor finish, and a scrub must not write
+ * repairs. Stop instead of reading the rest of the device.
+ */
+ if (BTRFS_FS_ERROR(fs_info))
+ return -EROFS;
+
/*
* The user (e.g. fsfreeze command) or power management (PM)
* suspend/hibernate can freeze the fs. And PM suspend/hibernate will
while (1) {
u64 dev_extent_len;
+ /*
+ * Do not start another block group on a filesystem with an
+ * error, see the per-extent check above.
+ */
+ if (BTRFS_FS_ERROR(fs_info)) {
+ ret = -EROFS;
+ break;
+ }
+
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
break;