Skip to contentSkip to conversation

pNFS: reclaim uncommitted layout writes after a server restart

PATCH New Christoph Engelbert 1 participant 5 messages Dec 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover pNFS: reclaim uncommitted layout writes after a server restart

A block or SCSI layout client writes file data straight to the storage device, into extents the metadata server allocated as unwritten. The server learns that the extents now hold data only from LAYOUTCOMMIT. If the server restarts between the writes and the commit, the client never tells it: the data is on the device, but every other client reads those blocks as zeros. The writer reads its own page cache and sees nothing wrong. Its close() returned 0 until Benjamin Coddington's commit below. Now it fails with EIO, but the data is still lost.

RFC 8881, section 12.7.4 gives the recovery: a LAYOUTCOMMIT with loca_reclaim set, sent during the server's grace period. nfsd accepts one. The Linux client never sends it:

- encode_layoutcommit() hardcodes loca_reclaim to false.
- nfs4_layoutcommit_done() ignores NFS4ERR_GRACE, so a commit refused during grace is never sent again.
- pnfs_layout_handle_reboot() returns and invalidates the layouts, and the layout driver's record of the uncommitted extents goes with them.

We hit this on a block-layout export served from XFS. Writes made under a layout in the minutes before a metadata server restart read back as zeros on a second client (about 25 MiB per file, 12,909 blocks). fio on the writer reported success.

Patch 1 encodes the flag and sends a reclaiming commit on a privileged slot. Patch 2 retries a commit refused with NFS4ERR_GRACE. Patch 3 adds a prepare_layoutcommit_reclaim layout driver operation and sends the reclaim from pnfs_layout_handle_reboot(), before each layout is returned or invalidated. A refused reclaim is recorded as a writeback error, so fsync() and close() report it. Patch 4 implements the operation for the block and SCSI layouts. It includes extents whose ordinary commit was still in flight at the restart.

The series is based on Trond's testing branch, because it builds on Benjamin Coddington's "pnfs/blocklayout: report data lost to a failed LAYOUTCOMMIT" there. With that commit, a commit refused with NFS4ERR_GRACE fails fsync() with EIO instead of returning 0; patch 2 turns it into a retry. Patch 4 keeps a failed reclaim from recording that error, since recovery sends the reclaim again.

Tested on arm64 under QEMU, on the testing branch and on v7.3-rc7, with two VMs: nfsd exporting XFS with pnfs on an nvmet NVMe/TCP namespace (SCSI layouts), and a client that writes 64 MiB with O_DIRECT, keeps the file open without fsync(), and closes it after nfsd restarts:

- Client connected through the restart: without the series the file is lost (size 0, all zeros). With it, the LAYOUTCOMMIT with loca_reclaim set is accepted before RECLAIM_COMPLETE, and the file is intact in every run, on both bases.
- Client cut off until grace is over: the data cannot be saved. On the testing branch, fsync() or close() fails with EIO, with or without the series. On v7.3-rc7 both return 0, with or without the series. That report comes from Benjamin's commit, not from this series, so a backport of the series should carry it too.

The reproducer builds both kernels and a Debian root file system in Docker and runs the VMs with QEMU (KVM, HVF, or TCG):

https://gist.github.com/noctarius/bd6055d0c0bb2c9e7d630076ddc59806

Not tested: the block layout (blkmapd), an ordinary LAYOUTCOMMIT in flight at the restart, and a server that refuses a reclaim sent during grace (patch 3's EIO path; a client that misses grace never sends one).

The series was written with the help of an LLM-based coding assistant. It was used to trace the client's recovery and LAYOUTCOMMIT paths against RFC 8881, to draft the changes and changelogs, and to run checkpatch. I reviewed the changes and the analysis behind them.

---
Christoph Engelbert (4):
NFSv4.1: encode loca_reclaim in LAYOUTCOMMIT
NFSv4.1: retry a LAYOUTCOMMIT refused with NFS4ERR_GRACE
pNFS: reclaim uncommitted layout writes after a server restart pnfs/blocklayout: reclaim uncommitted extents after a server restart

 fs/nfs/blocklayout/blocklayout.c |   8 +++
 fs/nfs/blocklayout/blocklayout.h |   8 +++
 fs/nfs/blocklayout/extent_tree.c | 118 +++++++++++++++++++++++++++--------
 fs/nfs/nfs4proc.c                |  11 +++-
 fs/nfs/nfs4xdr.c                 |   2 +-
 fs/nfs/pnfs.c                    | 130 ++++++++++++++++++++++++++++++++++++++-
 fs/nfs/pnfs.h                    |   8 +++
 include/linux/nfs_xdr.h          |   1 +
 8 files changed, 254 insertions(+), 32 deletions(-)

---
base-commit: 3e19991734326332c736e24c9b47b2cffb8f9e42
change-id: 20261201-pnfs-layoutcommit-reclaim-nfs-testing-be4ad2026feb

Best regards,

Signature · 1 line
Christoph Engelbert <[email protected]>
#1 lore raw

PATCH 1/4 NFSv4.1: encode loca_reclaim in LAYOUTCOMMIT

LAYOUTCOMMIT carries a reclaim flag for committing, during a server's grace period, a layout that was granted before the server restarted (RFC 8881, section 18.42.3). The client has always encoded it as false.

Add a reclaim field to the arguments, encode it, and send a reclaiming LAYOUTCOMMIT on a privileged slot, since it is sent by the state manager while recovery is in progress. Its completion leaves the status alone instead of ignoring or retrying errors, so that the state manager can tell a refused reclaim from one that has to be sent again.

Nothing sets the field yet.

---
 fs/nfs/nfs4proc.c       | 10 +++++++++-
 fs/nfs/nfs4xdr.c        |  2 +-
 include/linux/nfs_xdr.h |  1 +
 3 files changed, 11 insertions(+), 2 deletions(-)
fs/nfs/nfs4proc.c +9 -1
index 13f4e2570b73..bea74c922655 100644
@@ -10078,6 +10078,13 @@ nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
 	if (!nfs41_sequence_done(task, &data->res.seq_res))
 		return;
 
+	/*
+	 * A reclaim is sent by the state manager during recovery, which
+	 * decides what its outcome means for the data it covers.
+	 */
+	if (data->args.reclaim)
+		return;
+
 	switch (task->tk_status) { /* Just ignore these failures */
 	case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
 	case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
@@ -10180,7 +10187,8 @@ nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
 		task_setup_data.workqueue = nfsiod_workqueue;
 	}
 	nfs4_init_sequence(NFS_SERVER(data->args.inode)->nfs_client,
-			   &data->args.seq_args, &data->res.seq_res, 1, 0);
+			   &data->args.seq_args, &data->res.seq_res, 1,
+			   data->args.reclaim);
 	task = rpc_run_task(&task_setup_data);
 	if (IS_ERR(task))
 		return PTR_ERR(task);
fs/nfs/nfs4xdr.c +1 -1
index c0971af20154..d4fa5c5e8207 100644
@@ -2097,7 +2097,7 @@ encode_layoutcommit(struct xdr_stream *xdr,
 	/* Only whole file layouts */
 	p = xdr_encode_hyper(p, 0); /* offset */
 	p = xdr_encode_hyper(p, args->lastbytewritten + 1);	/* length */
-	*p = cpu_to_be32(0); /* reclaim */
+	*p = cpu_to_be32(args->reclaim);
 	encode_nfs4_stateid(xdr, &args->stateid);
 	if (args->lastbytewritten != U64_MAX) {
 		p = reserve_space(xdr, 20);
include/linux/nfs_xdr.h +1 -0
index c0e29b4dfa62..0b98ca4d6ef6 100644
@@ -315,6 +315,7 @@ struct nfs4_layoutcommit_args {
 	struct page *layoutupdate_page;
 	struct page **layoutupdate_pages;
 	__be32 *start_p;
+	bool reclaim;
 };
 
 struct nfs4_layoutcommit_res {
Signature · 1 line
Git-157)
#2 lore raw

PATCH 2/4 NFSv4.1: retry a LAYOUTCOMMIT refused with NFS4ERR_GRACE

In reply to Christoph Engelbert

A server in its grace period refuses a LAYOUTCOMMIT that does not set loca_reclaim with NFS4ERR_GRACE. nfs4_layoutcommit_done() ignores that error, so the commit is never sent again. Commit f770d34f7112 ("pnfs/blocklayout: report data lost to a failed LAYOUTCOMMIT") made the block layout put the extents back and record -EIO, so fsync() and close() now fail for data that a commit after the grace period would have recorded. Before that commit, they returned 0 while the server still had the blocks as unwritten.

NFS4ERR_GRACE asks the client to try again later, so take the generic error path, which delays and retries the commit as it does for NFS4ERR_DELAY. A commit of a layout granted before the restart is sent as a reclaim by the state manager before the grace period ends.

Fixes e59d27e05a64 ("NFSv4.1: Fix layoutcommit error handling")
---
 fs/nfs/nfs4proc.c | 1 -
 1 file changed, 1 deletion(-)
fs/nfs/nfs4proc.c +0 -1
index bea74c922655..ea7bf329771e 100644
@@ -10089,7 +10089,6 @@ nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
 	case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
 	case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
 	case -NFS4ERR_BADLAYOUT:     /* no layout */
-	case -NFS4ERR_GRACE:	    /* loca_recalim always false */
 		task->tk_status = 0;
 		break;
 	case -NFS4ERR_OLD_STATEID: {
Signature · 1 line
Git-157)
#3 lore raw

PATCH 3/4 pNFS: reclaim uncommitted layout writes after a server restart

In reply to Christoph Engelbert

A client that wrote to the storage devices through a layout, and had not committed the layout when the metadata server restarted, has data on the devices that the server does not know about. On a block or SCSI layout it sits in blocks the server allocated as unwritten, which every other client reads as zeros. The writer finds out only if a LAYOUTCOMMIT it sends after the restart fails.
RFC 8881, section 12.7.4 gives a LAYOUTCOMMIT with loca_reclaim set, sent during the server's grace period, as the only way to recover it.

The client never sends one. On a restart, pnfs_layout_handle_reboot() returns and invalidates the layouts, and the layout driver's record of what was written under them goes with them.

Add a prepare_layoutcommit_reclaim operation for layout drivers that can say which writes the server has not acknowledged, and send a reclaiming LAYOUTCOMMIT from pnfs_layout_handle_reboot(), which runs in the state manager after the opens are reclaimed and before RECLAIM_COMPLETE, for every layout before it is returned or invalidated. The reclaim does not take NFS_INO_LAYOUTCOMMITTING: a commit sent before the restart may still hold it, waiting for the recovery this runs in.

An error that leaves the server unasked keeps the layout and retries recovery. A server that refuses the reclaim has lost the data, which is recorded as a writeback error on the mapping, so that the next fsync() or close() reports it instead of returning 0.

Fixes 863a3c6c686d ("NFSv4.1: layoutcommit")
---
 fs/nfs/pnfs.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 fs/nfs/pnfs.h |   8 ++++
 2 files changed, 135 insertions(+), 3 deletions(-)
fs/nfs/pnfs.c +127 -3
index d2ff78acbed9..f0906f27f99c 100644
@@ -919,6 +919,110 @@ pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
 	return 0;
 }
 
+/*
+ * Errors after which a reclaiming LAYOUTCOMMIT is sent again on the next
+ * pass of recovery, rather than taken as the server's refusal.
+ */
+static bool pnfs_layoutcommit_reclaim_again(int status)
+{
+	switch (status) {
+	case -NFS4ERR_DELAY:
+	case -NFS4ERR_BADSESSION:
+	case -NFS4ERR_DEADSESSION:
+	case -NFS4ERR_BADSLOT:
+	case -NFS4ERR_SEQ_MISORDERED:
+	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
+	case -NFS4ERR_STALE_CLIENTID:
+	case -ENOMEM:
+	case -EAGAIN:
+	case -ETIMEDOUT:
+		return true;
+	}
+	return false;
+}
+
+/*
+ * Send one reclaiming LAYOUTCOMMIT. Returns 1 when the layout driver could
+ * fit only part of the writes into it and another has to follow, 0 when
+ * none are left, and a negative error otherwise.
+ */
+static int pnfs_layoutcommit_reclaim_one(struct inode *inode)
+{
+	struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
+	struct nfs4_layoutcommit_data *data;
+	struct pnfs_layout_hdr *lo;
+	int status, more = 0;
+
+	data = kzalloc_obj(*data, GFP_NOFS);
+	if (!data)
+		return -ENOMEM;
+
+	spin_lock(&inode->i_lock);
+	lo = NFS_I(inode)->layout;
+	if (!lo || !pnfs_layout_is_valid(lo)) {
+		spin_unlock(&inode->i_lock);
+		kfree(data);
+		return 0;
+	}
+	nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
+	data->cred = get_cred(lo->plh_lc_cred);
+	spin_unlock(&inode->i_lock);
+
+	INIT_LIST_HEAD(&data->lseg_list);
+	data->args.inode = inode;
+	data->args.reclaim = true;
+	data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
+	nfs_fattr_init(&data->fattr);
+	data->res.fattr = &data->fattr;
+	data->res.server = NFS_SERVER(inode);
+
+	status = ld->prepare_layoutcommit_reclaim(&data->args);
+	if (status == -ENOSPC) {
+		more = 1;
+	} else if (status) {
+		put_cred(data->cred);
+		kfree(data);
+		return status == -ENODATA ? 0 : status;
+	}
+
+	status = nfs4_proc_layoutcommit(data, true);
+	return status ? status : more;
+}
+
+/*
+ * Commit the writes made under a layout granted before the server
+ * restarted. The data is on the storage devices, but only a LAYOUTCOMMIT
+ * with loca_reclaim set, sent during the server's grace period, makes the
+ * server record where (RFC 8881, section 12.7.4), and once the layout is
+ * destroyed the layout driver no longer knows either.
+ *
+ * Returns -EAGAIN when recovery should try again. A server that refuses
+ * the reclaim has lost the data, which is recorded as a writeback error on
+ * the mapping for fsync() and close() to report.
+ */
+static int pnfs_layoutcommit_reclaim(struct inode *inode)
+{
+	struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
+	int status;
+
+	if (!ld || !ld->prepare_layoutcommit_reclaim)
+		return 0;
+
+	do {
+		status = pnfs_layoutcommit_reclaim_one(inode);
+	} while (status == 1);
+
+	if (!status)
+		return 0;
+	if (pnfs_layoutcommit_reclaim_again(status))
+		return -EAGAIN;
+	mapping_set_error(inode->i_mapping, -EIO);
+	pr_warn_ratelimited("NFS: %s refused to reclaim writes to inode %llu after a restart: %d\n",
+			    NFS_SERVER(inode)->nfs_client->cl_hostname,
+			    inode->i_ino, status);
+	return 0;
+}
+
 static int
 pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
 				   enum pnfs_layout_destroy_mode mode)
@@ -935,6 +1039,21 @@ pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
 			lo->plh_inode->i_ino);
 		inode = lo->plh_inode;
 
+		if (mode == PNFS_LAYOUT_REBOOT &&
+		    pnfs_layoutcommit_reclaim(inode) == -EAGAIN) {
+			/*
+			 * Keep the layout for the next pass: once it is
+			 * invalid, its writes can no longer be reclaimed.
+			 */
+			spin_lock(&inode->i_lock);
+			list_del_init(&lo->plh_bulk_destroy);
+			spin_unlock(&inode->i_lock);
+			pnfs_put_layout_hdr(lo);
+			nfs_iput_and_deactive(inode);
+			ret = -EAGAIN;
+			continue;
+		}
+
 		pnfs_layoutcommit_inode(inode, false);
 
 		spin_lock(&inode->i_lock);
@@ -1054,6 +1173,10 @@ static int pnfs_layout_bulk_list_reboot(struct list_head *list)
 
 	list_for_each_entry(lo, list, plh_bulk_destroy) {
 		server = NFS_SERVER(lo->plh_inode);
+		/* Returning the layout ends the record of what it wrote. */
+		ret = pnfs_layoutcommit_reclaim(lo->plh_inode);
+		if (ret)
+			goto err;
 		ret = pnfs_layout_return_on_reboot(lo);
 		switch (ret) {
 		case 0:
@@ -1081,8 +1204,7 @@ int pnfs_layout_handle_reboot(struct nfs_client *clp)
 	pnfs_layout_build_recover_list_byclient(clp, &list);
 	if (!list_empty(&list))
 		ret = pnfs_layout_bulk_list_reboot(&list);
-	ret2 = pnfs_layout_do_destroy_byclid(clp, &list,
-					     PNFS_LAYOUT_INVALIDATE);
+	ret2 = pnfs_layout_do_destroy_byclid(clp, &list, PNFS_LAYOUT_REBOOT);
 	if (!ret)
 		ret = ret2;
 	return (ret == 0) ?  0 : -EAGAIN;
@@ -3741,7 +3863,9 @@ void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
 
 	if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
 		nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
-	pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
+	/* A reclaim takes neither segments nor NFS_INO_LAYOUTCOMMITTING. */
+	if (!data->args.reclaim)
+		pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
 }
 
 /*
fs/nfs/pnfs.h +8 -0
index bbe56b682dc0..b4d212e990cd 100644
@@ -127,6 +127,7 @@ enum pnfs_layout_destroy_mode {
 	PNFS_LAYOUT_INVALIDATE = 0,
 	PNFS_LAYOUT_BULK_RETURN,
 	PNFS_LAYOUT_FILE_BULK_RETURN,
+	PNFS_LAYOUT_REBOOT,
 };
 
 struct nfs4_deviceid_node;
@@ -198,6 +199,13 @@ struct pnfs_layoutdriver_type {
 
 	void (*cleanup_layoutcommit) (struct nfs4_layoutcommit_data *data);
 	int (*prepare_layoutcommit) (struct nfs4_layoutcommit_args *args);
+	/*
+	 * As prepare_layoutcommit, for a LAYOUTCOMMIT with loca_reclaim set
+	 * after a server restart: encode every write made under the layout
+	 * that the server has not acknowledged, including those of a commit
+	 * still in flight. Returns -ENODATA when there is none.
+	 */
+	int (*prepare_layoutcommit_reclaim) (struct nfs4_layoutcommit_args *args);
 	int (*prepare_layoutstats) (struct nfs42_layoutstat_args *args);
 
 	void (*cancel_io)(struct pnfs_layout_segment *lseg,
Signature · 1 line
Git-157)
#4 lore raw

PATCH 4/4 pnfs/blocklayout: reclaim uncommitted extents after a server restart

In reply to Christoph Engelbert

Implement prepare_layoutcommit_reclaim for the block and SCSI layouts. It encodes the extents written and not yet committed, as an ordinary commit does, and also those still tagged EXTENT_COMMITTING: a commit sent before the restart has not been acknowledged by the restarted server either, and may be waiting for the recovery the reclaim runs in. A successful reclaim marks both kinds committed, so that commit finds nothing left to mark when it completes.

A failed reclaim puts its extents back without recording a writeback error. The state manager sends the reclaim again after an error that leaves the server unasked, and records the error itself when the server refuses it. Reporting every failure, as ext_tree_mark_committed() does for an ordinary commit, would fail fsync() and close() for data that the next pass of recovery commits.

The new size is the end of every write the server has not acknowledged. bl_lwb cannot give it, since a commit takes it when it is sent, so keep the end of those writes in bl_lwb_pending as well, and forget it once a commit succeeds with nothing left uncommitted.

---
 fs/nfs/blocklayout/blocklayout.c |   8 +++
 fs/nfs/blocklayout/blocklayout.h |   8 +++
 fs/nfs/blocklayout/extent_tree.c | 118 ++++++++++++++++++++++++++++++---------
 3 files changed, 108 insertions(+), 26 deletions(-)
fs/nfs/blocklayout/blocklayout.c +8 -0
index 86039e04d4fc..f90e2ac950d9 100644
@@ -779,6 +779,12 @@ bl_prepare_layoutcommit(struct nfs4_layoutcommit_args *arg)
 	return ext_tree_prepare_commit(arg);
 }
 
+static int
+bl_prepare_layoutcommit_reclaim(struct nfs4_layoutcommit_args *arg)
+{
+	return ext_tree_prepare_commit_reclaim(arg);
+}
+
 static void
 bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
 {
@@ -978,6 +984,7 @@ static struct pnfs_layoutdriver_type blocklayout_type = {
 	.free_lseg			= bl_free_lseg,
 	.return_range			= bl_return_range,
 	.prepare_layoutcommit		= bl_prepare_layoutcommit,
+	.prepare_layoutcommit_reclaim	= bl_prepare_layoutcommit_reclaim,
 	.cleanup_layoutcommit		= bl_cleanup_layoutcommit,
 	.set_layoutdriver		= bl_set_layoutdriver,
 	.alloc_deviceid_node		= bl_alloc_deviceid_node,
@@ -1044,6 +1051,7 @@ static struct pnfs_layoutdriver_type scsilayout_type = {
 	.free_lseg			= bl_free_lseg,
 	.return_range			= bl_return_range,
 	.prepare_layoutcommit		= bl_prepare_layoutcommit,
+	.prepare_layoutcommit_reclaim	= bl_prepare_layoutcommit_reclaim,
 	.cleanup_layoutcommit		= bl_cleanup_layoutcommit,
 	.set_layoutdriver		= bl_set_layoutdriver,
 	.alloc_deviceid_node		= bl_alloc_deviceid_node,
fs/nfs/blocklayout/blocklayout.h +8 -0
index 8a0b4e0d7cd3..fd82b4f700aa 100644
@@ -147,6 +147,13 @@ struct pnfs_block_layout {
 	spinlock_t		bl_ext_lock;   /* Protects list manipulation */
 	bool			bl_scsi_layout;
 	u64			bl_lwb;
+	/*
+	 * The end of the writes the server has not acknowledged yet: bl_lwb
+	 * is handed to a LAYOUTCOMMIT when it is sent, this only once one
+	 * succeeds with nothing left uncommitted. A reclaim after a server
+	 * restart sends it as the new size.
+	 */
+	u64			bl_lwb_pending;
 };
 
 static inline struct pnfs_block_layout *
@@ -188,6 +195,7 @@ int ext_tree_mark_written(struct pnfs_block_layout *bl, sector_t start,
 bool ext_tree_lookup(struct pnfs_block_layout *bl, sector_t isect,
 		struct pnfs_block_extent *ret, bool rw);
 int ext_tree_prepare_commit(struct nfs4_layoutcommit_args *arg);
+int ext_tree_prepare_commit_reclaim(struct nfs4_layoutcommit_args *arg);
 void ext_tree_mark_committed(struct nfs4_layoutcommit_args *arg, int status);
 
 /* rpc_pipefs.c */
fs/nfs/blocklayout/extent_tree.c +92 -26

215 lines not on this page · raw message

Signature · 1 line
Git-157)