Skip to contentSkip to conversation

ceph: fix -EAGAIN from buffered writes racing with snapshots

PATCH In review Venky Shankar 2 participants 3 messages Oct 7 – Oct 9 Ceph client

lore
Share & tools
lore
#0 lore raw

cover ceph: fix -EAGAIN from buffered writes racing with snapshots

Buffered writes on a CephFS kernel mount can fail with EAGAIN ("Resource temporarily unavailable") when they rewrite a page that is still dirty in an older snap context which cannot be flushed yet. We have been hitting this in CephFS QA (teuthology) runs that take snapshots while the workload is writing:

- kernel untar/build workunit, failing in the final link (ceph-client testing kernel, 7.3.0-rc1-g7f77d684f595)
https://pulpito.ceph.com/vshankar-2026-10-07_07:40:54-fs:workload-wip-77519-trial1-distro-default-trial/613906/

  2026-10-07T08:18:00.721 INFO:tasks.workunit.client.0.trial024.stderr:ld: final link failed: Resource temporarily unavailable

- dbench workunit (stock distro kernel)
https://pulpito.ceph.com/vshankar-2026-10-02_21:48:19-fs-wip-vshankar-testing-20261002.175142-distro-default-trial/602723/

  2026-10-02T22:35:02.642 INFO:tasks.workunit.client.0.trial029.stdout:[483386] write failed on handle 10895 (Resource temporarily unavailable)

Tracker: https://tracker.ceph.com/issues/57656

When ceph_netfs_check_write_begin() finds the folio dirty in a snap context that is neither the head nor the oldest one with dirty data, it unlocks and puts the folio, kicks writeback, waits for the older snap contexts to be flushed and then returns -EAGAIN so that the write is retried.

Since commit fac47b43c760 ("netfs: do not unlock and put the folio twice"), netfs_write_begin() retries only when ->check_write_begin() returns 0 with *foliop cleared; -EAGAIN gets no special treatment. That commit converted ceph to clear *foliop but left the -EAGAIN return in place, so since v5.19 the error goes all the way up to write(2) as soon as the wait succeeds. The patch returns the wait result as is.

Reproducer (single kernel client, snapshots enabled), in a directory on the mount:

f=file

  w() { dd if=/dev/urandom of=$f bs=4096 count=1 seek=$1 \
           conv=notrunc status=none; }
  w 0; mkdir .snap/a   # block 0 dirty in snap a
  w 1; mkdir .snap/b   # block 1 dirty in snap b
  w 1                  # unpatched: EAGAIN

The last write finds block 1 dirty in snap b while snap a still has a dirty page, so it waits for a to be flushed (~10ms here) and then fails. The dirty pages are not written back on snapshot creation, so the default 30s dirty expiry leaves plenty of time to hit this.

Results:
- unpatched: EAGAIN on the first iteration
- patched: 50 out of 50 iterations pass

With ceph dynamic debug enabled, the unpatched run shows ceph_find_incompatible() reporting the folio as "not current or oldest", writeback completing snap a's cap snap, and the writer dropping its cap refs right after waking up instead of retrying.

Venky Shankar (1):
ceph: don't fail write_begin with -EAGAIN after flushing old snapc

 fs/ceph/addr.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 1/1 ceph: don't fail write_begin with -EAGAIN after flushing old snapc

When ceph_netfs_check_write_begin() finds the folio dirty in an older snap context, it drops the folio, waits for that snap context to be flushed and then returns -EAGAIN so that the write is retried.

However, since commit fac47b43c760 ("netfs: do not unlock and put the folio twice"), netfs_write_begin() retries only when ->check_write_begin() returns 0 with *foliop cleared -- every error, -EAGAIN included, is returned as is. The -EAGAIN thus propagates up to write(2), which fails with "Resource temporarily unavailable" once the dirty data has been flushed, rather than being retried.

This is easily hit by buffered writers rewriting dirty pages while snapshots are being taken, e.g. a kernel build linking vmlinux into a directory under a snapshot schedule:

ld: final link failed: Resource temporarily unavailable

Return the wait_event_killable() result as is so that netfs re-gets the folio and retries on success.

Fixes fac47b43c760 ("netfs: do not unlock and put the folio twice")
---
 fs/ceph/addr.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/ceph/addr.c +6 -1
index b4ea5ed..dd2bd8e 100644
@@ -1982,7 +1982,12 @@ static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned
 		r = wait_event_killable(ci->i_cap_wq,
 					context_is_writeable_or_written(inode, snapc));
 		ceph_put_snap_context(snapc);
-		return r == 0 ? -EAGAIN : r;
+		/*
+		 * *foliop is cleared, so a return of 0 makes
+		 * netfs_write_begin() grab the folio again and call us again
+		 * to recheck it. Any error (fatal signal) fails the write.
+		 */
+		return r;
 	}
 	return 0;
 }
Signature · 1 line
2.50.1 (Apple Git-155)