Skip to contentSkip to conversation

xfs: load uncached unlinked inodes into memory on demand

RFC PATCH Superseded Darrick J. Wong 2 participants 3 messages Aug 29, 2023 – Aug 30, 2023 XFS filesystem

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 xfs: load uncached unlinked inodes into memory on demand

From: Darrick J. Wong <[email protected]>

shrikanth hegde reports that filesystems fail shortly after mount with the following failure:

	WARNING: CPU: 56 PID: 12450 at fs/xfs/xfs_inode.c:1839 xfs_iunlink_lookup+0x58/0x80 [xfs]

This of course is the WARN_ON_ONCE in xfs_iunlink_lookup:

	ip = radix_tree_lookup(&pag->pag_ici_root, agino);
	if (WARN_ON_ONCE(!ip || !ip->i_ino)) { ... }

From diagnostic data collected by the bug reporters, it would appear that we cleanly mounted a filesystem that contained unlinked inodes. Unlinked inodes are only processed as a final step of log recovery, which means that clean mounts do not process the unlinked list at all.

Prior to the introduction of the incore unlinked lists, this wasn't a problem because the unlink code would (very expensively) traverse the entire ondisk metadata iunlink chain to keep things up to date. However, the incore unlinked list code complains when it realizes that it is out of sync with the ondisk metadata and shuts down the fs, which is bad.

Ritesh proposed to solve this problem by unconditionally parsing the unlinked lists at mount time, but this imposes a mount time cost for every filesystem to catch something that should be very infrequent. Instead, let's target the places where we can encounter a next_unlinked pointer that refers to an inode that is not in cache, and load it into cache.

Note: This patch does not address the problem of iget loading an inode from the middle of the iunlink list and needing to set i_prev_unlinked correctly.

---
 fs/xfs/xfs_inode.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 fs/xfs/xfs_trace.h |   25 +++++++++++++++++++
 2 files changed, 92 insertions(+), 3 deletions(-)
fs/xfs/xfs_inode.c +67 -3
index 6ee266be45d4..3ab140ec09bb 100644
@@ -1829,12 +1829,17 @@ xfs_iunlink_lookup(
 
 	rcu_read_lock();
 	ip = radix_tree_lookup(&pag->pag_ici_root, agino);
+	if (!ip) {
+		/* Caller can handle inode not being in memory. */
+		rcu_read_unlock();
+		return NULL;
+	}
 
 	/*
-	 * Inode not in memory or in RCU freeing limbo should not happen.
-	 * Warn about this and let the caller handle the failure.
+	 * Inode in RCU freeing limbo should not happen.  Warn about this and
+	 * let the caller handle the failure.
 	 */
-	if (WARN_ON_ONCE(!ip || !ip->i_ino)) {
+	if (WARN_ON_ONCE(!ip->i_ino)) {
 		rcu_read_unlock();
 		return NULL;
 	}
@@ -1902,6 +1907,60 @@ xfs_iunlink_update_bucket(
 	return 0;
 }
 
+/*
+ * Load the inode @next_agino into the cache and set its prev_unlinked pointer
+ * to @prev_agino.  Caller must hold the AGI to synchronize with other changes
+ * to the unlinked list.
+ */
+STATIC int
+xfs_iunlink_reload_next(
+	struct xfs_trans	*tp,
+	struct xfs_buf		*agibp,
+	xfs_agino_t		prev_agino,
+	xfs_agino_t		next_agino)
+{
+	struct xfs_perag	*pag = agibp->b_pag;
+	struct xfs_mount	*mp = pag->pag_mount;
+	struct xfs_inode	*next_ip = NULL;
+	xfs_ino_t		ino;
+	int			error;
+
+	ASSERT(next_agino != NULLAGINO);
+
+#ifdef DEBUG
+	rcu_read_lock();
+	next_ip = radix_tree_lookup(&pag->pag_ici_root, next_agino);
+	ASSERT(next_ip == NULL);
+	rcu_read_unlock();
+#endif
+
+	ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, next_agino);
+	error = xfs_iget(mp, tp, ino, XFS_IGET_UNTRUSTED, 0, &next_ip);
+	if (error)
+		return error;
+
+	/* If this is not an unlinked inode, something is very wrong. */
+	if (VFS_I(next_ip)->i_nlink != 0) {
+		error = -EFSCORRUPTED;
+		goto rele;
+	}
+
+	next_ip->i_prev_unlinked = prev_agino;
+	trace_xfs_iunlink_reload_next(next_ip);
+rele:
+	/*
+	 * We're running in transaction context, so we cannot run any inode
+	 * release code.  Clear DONTCACHE on this inode to prevent the VFS from
+	 * initiating writeback and to force the irele to push this inode to
+	 * the LRU instead of dropping it immediately.
+	 */
+	spin_lock(&VFS_I(next_ip)->i_lock);
+	VFS_I(next_ip)->i_state &= ~I_DONTCACHE;
+	spin_unlock(&VFS_I(next_ip)->i_lock);
+	xfs_irele(next_ip);
+	return error;
+}
+
 static int
 xfs_iunlink_insert_inode(
 	struct xfs_trans	*tp,
@@ -1933,6 +1992,8 @@ xfs_iunlink_insert_inode(
 	 * inode.
 	 */
 	error = xfs_iunlink_update_backref(pag, agino, next_agino);
+	if (error == -ENOLINK)
+		error = xfs_iunlink_reload_next(tp, agibp, agino, next_agino);
 	if (error)
 		return error;
 
@@ -2027,6 +2088,9 @@ xfs_iunlink_remove_inode(
 	 */
 	error = xfs_iunlink_update_backref(pag, ip->i_prev_unlinked,
 			ip->i_next_unlinked);
+	if (error == -ENOLINK)
+		error = xfs_iunlink_reload_next(tp, agibp, ip->i_prev_unlinked,
+				ip->i_next_unlinked);
 	if (error)
 		return error;
 
fs/xfs/xfs_trace.h +25 -0
index 36bd42ed9ec8..f4e46bac9b91 100644
@@ -3832,6 +3832,31 @@ TRACE_EVENT(xfs_iunlink_update_dinode,
 		  __entry->new_ptr)
 );
 
+TRACE_EVENT(xfs_iunlink_reload_next,
+	TP_PROTO(struct xfs_inode *ip),
+	TP_ARGS(ip),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(xfs_agnumber_t, agno)
+		__field(xfs_agino_t, agino)
+		__field(xfs_agino_t, prev_agino)
+		__field(xfs_agino_t, next_agino)
+	),
+	TP_fast_assign(
+		__entry->dev = ip->i_mount->m_super->s_dev;
+		__entry->agno = XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino);
+		__entry->agino = XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino);
+		__entry->prev_agino = ip->i_prev_unlinked;
+		__entry->next_agino = ip->i_next_unlinked;
+	),
+	TP_printk("dev %d:%d agno 0x%x agino 0x%x prev_unlinked 0x%x next_unlinked 0x%x",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->agno,
+		  __entry->agino,
+		  __entry->prev_agino,
+		  __entry->next_agino)
+);
+
 DECLARE_EVENT_CLASS(xfs_ag_inode_class,
 	TP_PROTO(struct xfs_inode *ip),
 	TP_ARGS(ip),
#1 lore raw
Quoted 59 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

shrikanth hegde reports that filesystems fail shortly after mount with the following failure:

	WARNING: CPU: 56 PID: 12450 at fs/xfs/xfs_inode.c:1839 xfs_iunlink_lookup+0x58/0x80 [xfs]

This of course is the WARN_ON_ONCE in xfs_iunlink_lookup:

	ip = radix_tree_lookup(&pag->pag_ici_root, agino);
	if (WARN_ON_ONCE(!ip || !ip->i_ino)) { ... }

From diagnostic data collected by the bug reporters, it would appear that we cleanly mounted a filesystem that contained unlinked inodes. Unlinked inodes are only processed as a final step of log recovery, which means that clean mounts do not process the unlinked list at all.

Prior to the introduction of the incore unlinked lists, this wasn't a problem because the unlink code would (very expensively) traverse the entire ondisk metadata iunlink chain to keep things up to date. However, the incore unlinked list code complains when it realizes that it is out of sync with the ondisk metadata and shuts down the fs, which is bad.

Ritesh proposed to solve this problem by unconditionally parsing the unlinked lists at mount time, but this imposes a mount time cost for every filesystem to catch something that should be very infrequent. Instead, let's target the places where we can encounter a next_unlinked pointer that refers to an inode that is not in cache, and load it into cache.

Note: This patch does not address the problem of iget loading an inode from the middle of the iunlink list and needing to set i_prev_unlinked correctly.

---
 fs/xfs/xfs_inode.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 fs/xfs/xfs_trace.h |   25 +++++++++++++++++++
 2 files changed, 92 insertions(+), 3 deletions(-)
fs/xfs/xfs_inode.c +5 -0
index 6ee266be45d4..3ab140ec09bb 100644
@@ -1829,12 +1829,17 @@ xfs_iunlink_lookup(
 
 	rcu_read_lock();
 	ip = radix_tree_lookup(&pag->pag_ici_root, agino);
+	if (!ip) {
+		/* Caller can handle inode not being in memory. */
+		rcu_read_unlock();
+		return NULL;
+	}
 
 	/*

fs/xfs/xfs_inode.c xfs_iunlink_lookup( · line 1845

Quoted 10 lines
fs/xfs/xfs_inode.c +3 -3
-	 * Inode not in memory or in RCU freeing limbo should not happen.
-	 * Warn about this and let the caller handle the failure.
+	 * Inode in RCU freeing limbo should not happen.  Warn about this and
+	 * let the caller handle the failure.
 	 */
-	if (WARN_ON_ONCE(!ip || !ip->i_ino)) {
+	if (WARN_ON_ONCE(!ip->i_ino)) {
 		rcu_read_unlock();
 		return NULL;
 	}

I think we should still log a message about this situation, as it implies that we had an unrecovered unlinked list on the filesystem and that should "never happen" in normal conditions.

i.e. something like:

XFS(dev): Found unrecovered unlinked inodes in AG X. Runtime recovery initiated.

which uses a perag state flag to only issue the message once per AG per mount. At least this way, if we get weird stuff happening because of loading an inode in the middle of an unlinked list (the unhandled prev_agino case) we know why weird stuff might be happening...

Quoted 25 lines
fs/xfs/xfs_inode.c +21 -0
@@ -1902,6 +1907,60 @@ xfs_iunlink_update_bucket(
 	return 0;
 }
 
+/*
+ * Load the inode @next_agino into the cache and set its prev_unlinked pointer
+ * to @prev_agino.  Caller must hold the AGI to synchronize with other changes
+ * to the unlinked list.
+ */
+STATIC int
+xfs_iunlink_reload_next(
+	struct xfs_trans	*tp,
+	struct xfs_buf		*agibp,
+	xfs_agino_t		prev_agino,
+	xfs_agino_t		next_agino)
+{
+	struct xfs_perag	*pag = agibp->b_pag;
+	struct xfs_mount	*mp = pag->pag_mount;
+	struct xfs_inode	*next_ip = NULL;
+	xfs_ino_t		ino;
+	int			error;
+
+	ASSERT(next_agino != NULLAGINO);
+
+#ifdef DEBUG

fs/xfs/xfs_inode.c xfs_iunlink_update_bucket( · line 1940

Quoted 10 lines
fs/xfs/xfs_inode.c +10 -0
+	rcu_read_lock();
+	next_ip = radix_tree_lookup(&pag->pag_ici_root, next_agino);
+	ASSERT(next_ip == NULL);
+	rcu_read_unlock();
+#endif
+
+	ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, next_agino);
+	error = xfs_iget(mp, tp, ino, XFS_IGET_UNTRUSTED, 0, &next_ip);
+	if (error)
+		return error;

WHy are we using XFS_IGET_UNTRUSTED here? A comment explaining why we don't trust the agino on th eunlinked list we are about to try to recover (i.e. trust!) would be good.

fs/xfs/xfs_inode.c xfs_iunlink_update_bucket( · line 1946

Quoted 5 lines
fs/xfs/xfs_inode.c +5 -0
+	/* If this is not an unlinked inode, something is very wrong. */
+	if (VFS_I(next_ip)->i_nlink != 0) {
+		error = -EFSCORRUPTED;
+		goto rele;
+	}

*nod*

Quoted 4 lines
fs/xfs/xfs_inode.c +4 -0
+
+	next_ip->i_prev_unlinked = prev_agino;
+	trace_xfs_iunlink_reload_next(next_ip);
+rele:

fs/xfs/xfs_inode.c xfs_iunlink_update_bucket( · line 1960

Quoted 10 lines
fs/xfs/xfs_inode.c +10 -0
+	/*
+	 * We're running in transaction context, so we cannot run any inode
+	 * release code.  Clear DONTCACHE on this inode to prevent the VFS from
+	 * initiating writeback and to force the irele to push this inode to
+	 * the LRU instead of dropping it immediately.
+	 */
+	spin_lock(&VFS_I(next_ip)->i_lock);
+	VFS_I(next_ip)->i_state &= ~I_DONTCACHE;
+	spin_unlock(&VFS_I(next_ip)->i_lock);
+	xfs_irele(next_ip);

Huh. We just loaded the next_ip into memory - how is it dirty, and what writeback will happen? Also, how would I_DONTCACHE get set in the first place here?

Quoted 7 lines
fs/xfs/xfs_inode.c +3 -0
+	return error;
+}
+
 static int
 xfs_iunlink_insert_inode(
 	struct xfs_trans	*tp,

fs/xfs/xfs_inode.c xfs_iunlink_insert_inode( · line 1998

Quoted 7 lines
fs/xfs/xfs_inode.c +2 -0
@@ -1933,6 +1992,8 @@ xfs_iunlink_insert_inode(
 	 * inode.
 	 */
 	error = xfs_iunlink_update_backref(pag, agino, next_agino);
+	if (error == -ENOLINK)
+		error = xfs_iunlink_reload_next(tp, agibp, agino, next_agino);
 	if (error)
 		return error;

Where does this -ENOLINK error come from? xfs_iunlink_update_backref() returns either -EFSCORRUPTED or 0. Is the patch missing hunks or is it dependent on some other patch that does this?

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#2 lore raw
Quoted 77 lines · Dave Chinner
Quoted 59 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

shrikanth hegde reports that filesystems fail shortly after mount with the following failure:

	WARNING: CPU: 56 PID: 12450 at fs/xfs/xfs_inode.c:1839 xfs_iunlink_lookup+0x58/0x80 [xfs]

This of course is the WARN_ON_ONCE in xfs_iunlink_lookup:

	ip = radix_tree_lookup(&pag->pag_ici_root, agino);
	if (WARN_ON_ONCE(!ip || !ip->i_ino)) { ... }

From diagnostic data collected by the bug reporters, it would appear that we cleanly mounted a filesystem that contained unlinked inodes. Unlinked inodes are only processed as a final step of log recovery, which means that clean mounts do not process the unlinked list at all.

Prior to the introduction of the incore unlinked lists, this wasn't a problem because the unlink code would (very expensively) traverse the entire ondisk metadata iunlink chain to keep things up to date. However, the incore unlinked list code complains when it realizes that it is out of sync with the ondisk metadata and shuts down the fs, which is bad.

Ritesh proposed to solve this problem by unconditionally parsing the unlinked lists at mount time, but this imposes a mount time cost for every filesystem to catch something that should be very infrequent. Instead, let's target the places where we can encounter a next_unlinked pointer that refers to an inode that is not in cache, and load it into cache.

Note: This patch does not address the problem of iget loading an inode from the middle of the iunlink list and needing to set i_prev_unlinked correctly.

---
 fs/xfs/xfs_inode.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 fs/xfs/xfs_trace.h |   25 +++++++++++++++++++
 2 files changed, 92 insertions(+), 3 deletions(-)
fs/xfs/xfs_inode.c +5 -0
index 6ee266be45d4..3ab140ec09bb 100644
@@ -1829,12 +1829,17 @@ xfs_iunlink_lookup(
 
 	rcu_read_lock();
 	ip = radix_tree_lookup(&pag->pag_ici_root, agino);
+	if (!ip) {
+		/* Caller can handle inode not being in memory. */
+		rcu_read_unlock();
+		return NULL;
+	}
 
 	/*

fs/xfs/xfs_inode.c xfs_iunlink_lookup( · line 1845

Quoted 10 lines
fs/xfs/xfs_inode.c +3 -3
-	 * Inode not in memory or in RCU freeing limbo should not happen.
-	 * Warn about this and let the caller handle the failure.
+	 * Inode in RCU freeing limbo should not happen.  Warn about this and
+	 * let the caller handle the failure.
 	 */
-	if (WARN_ON_ONCE(!ip || !ip->i_ino)) {
+	if (WARN_ON_ONCE(!ip->i_ino)) {
 		rcu_read_unlock();
 		return NULL;
 	}

I think we should still log a message about this situation, as it implies that we had an unrecovered unlinked list on the filesystem and that should "never happen" in normal conditions.

i.e. something like:

Quoted 7 lines

XFS(dev): Found unrecovered unlinked inodes in AG X. Runtime recovery initiated.

which uses a perag state flag to only issue the message once per AG per mount. At least this way, if we get weird stuff happening because of loading an inode in the middle of an unlinked list (the unhandled prev_agino case) we know why weird stuff might be happening...

<nod> Ok, I'll make that explicit.

Quoted 32 lines
Quoted 32 lines
fs/xfs/xfs_inode.c +27 -0
@@ -1902,6 +1907,60 @@ xfs_iunlink_update_bucket(
 	return 0;
 }
 
+/*
+ * Load the inode @next_agino into the cache and set its prev_unlinked pointer
+ * to @prev_agino.  Caller must hold the AGI to synchronize with other changes
+ * to the unlinked list.
+ */
+STATIC int
+xfs_iunlink_reload_next(
+	struct xfs_trans	*tp,
+	struct xfs_buf		*agibp,
+	xfs_agino_t		prev_agino,
+	xfs_agino_t		next_agino)
+{
+	struct xfs_perag	*pag = agibp->b_pag;
+	struct xfs_mount	*mp = pag->pag_mount;
+	struct xfs_inode	*next_ip = NULL;
+	xfs_ino_t		ino;
+	int			error;
+
+	ASSERT(next_agino != NULLAGINO);
+
+#ifdef DEBUG
+	rcu_read_lock();
+	next_ip = radix_tree_lookup(&pag->pag_ici_root, next_agino);
+	ASSERT(next_ip == NULL);
+	rcu_read_unlock();
+#endif
+
Quoted 8 lines

fs/xfs/xfs_inode.c xfs_iunlink_update_bucket( · line 1940

Quoted 4 lines
fs/xfs/xfs_inode.c +4 -0
+	ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, next_agino);
+	error = xfs_iget(mp, tp, ino, XFS_IGET_UNTRUSTED, 0, &next_ip);
+	if (error)
+		return error;

WHy are we using XFS_IGET_UNTRUSTED here? A comment explaining why we don't trust the agino on th eunlinked list we are about to try to recover (i.e. trust!) would be good.

/*
* Use an untrusted lookup just to be cautious in case the AGI
* has been corrupted and now points at a free inode. That
* shouldn't happen, but we'd rather shut down now since we're
* already running in a weird situation.
*/

Quoted 18 lines

fs/xfs/xfs_inode.c xfs_iunlink_update_bucket( · line 1946

Quoted 5 lines
fs/xfs/xfs_inode.c +5 -0
+	/* If this is not an unlinked inode, something is very wrong. */
+	if (VFS_I(next_ip)->i_nlink != 0) {
+		error = -EFSCORRUPTED;
+		goto rele;
+	}

*nod*

Quoted 10 lines
fs/xfs/xfs_inode.c +10 -0
+
+	next_ip->i_prev_unlinked = prev_agino;
+	trace_xfs_iunlink_reload_next(next_ip);
+rele:
+	/*
+	 * We're running in transaction context, so we cannot run any inode
+	 * release code.  Clear DONTCACHE on this inode to prevent the VFS from
+	 * initiating writeback and to force the irele to push this inode to
+	 * the LRU instead of dropping it immediately.
+	 */
Quoted 8 lines

fs/xfs/xfs_inode.c xfs_iunlink_update_bucket( · line 1960

Quoted 4 lines
fs/xfs/xfs_inode.c +4 -0
+	spin_lock(&VFS_I(next_ip)->i_lock);
+	VFS_I(next_ip)->i_state &= ~I_DONTCACHE;
+	spin_unlock(&VFS_I(next_ip)->i_lock);
+	xfs_irele(next_ip);

Huh. We just loaded the next_ip into memory - how is it dirty, and what writeback will happen? Also, how would I_DONTCACHE get set in the first place here?

Ah, that's a historical accident -- originally when I thought the possibility of unrecovered unlinked inodes was vanishingly small, I wrote a whole bunch of code into online repair to deal with reloading the incore list, etc.

When I first started prototyping it, xchk_irele didn't exist yet, so any time I had to release an inode within a scrub transaction, I had to manually clear I_DONTCACHE. That got copied around everywhere in the scrub code, and then it got copied over when I started working on the runtime version. That's been lurking beyond the depths of djwong-wtf for quite a long time now, and I never got back to it until the heat started going up after 6.1.

I think here it's not necessary since (as you point out) nobody can actually dirty the inode, nor can they set DONTCACHE.

Quoted 12 lines
Quoted 12 lines
fs/xfs/xfs_inode.c +4 -0
+	return error;
+}
+
 static int
 xfs_iunlink_insert_inode(
 	struct xfs_trans	*tp,
@@ -1933,6 +1992,8 @@ xfs_iunlink_insert_inode(
 	 * inode.
 	 */
 	error = xfs_iunlink_update_backref(pag, agino, next_agino);
+	if (error == -ENOLINK)
Quoted 8 lines

fs/xfs/xfs_inode.c xfs_iunlink_insert_inode( · line 1998

Quoted 3 lines
fs/xfs/xfs_inode.c +1 -0
+		error = xfs_iunlink_reload_next(tp, agibp, agino, next_agino);
 	if (error)
 		return error;

Where does this -ENOLINK error come from? xfs_iunlink_update_backref() returns either -EFSCORRUPTED or 0. Is the patch missing hunks or is it dependent on some other patch that does this?

<sigh> I forgot to copy that when I backported this patch from my dev tree to TOT. Welllllp thanks for catching that, now I can go restart the test fleet.

/* Update the prev pointer of the next agino. */
static int
xfs_iunlink_update_backref(
struct xfs_perag *pag,
xfs_agino_t prev_agino,
xfs_agino_t next_agino)

{
	struct xfs_inode	*ip;

	/* No update necessary if we are at the end of the list. */
	if (next_agino == NULLAGINO)
		return 0;

	ip = xfs_iunlink_lookup(pag, next_agino);
	if (!ip)
		return -ENOLINK;

	ip->i_prev_unlinked = prev_agino;
	return 0;
}

--D

Quoted 6 lines

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]