($INBOX_DIR/description missing)
 help / color / Atom feed
From: Amir Goldstein <amir73il@gmail.com>
To: Miklos Szeredi <miklos@szeredi.hu>
Cc: linux-unionfs@vger.kernel.org
Subject: [PATCH] ovl: fix WARN_ON nlink drop to zero
Date: Mon, 23 Mar 2020 21:08:50 +0200
Message-ID: <20200323190850.3091-1-amir73il@gmail.com> (raw)

Changes to underlying layers should not cause WARN_ON(), but this repro

 mkdir w l u mnt
 sudo mount -t overlay -o workdir=w,lowerdir=l,upperdir=u overlay mnt
 touch mnt/h
 ln u/h u/k
 rm -rf mnt/k
 rm -rf mnt/h

 ------------[ cut here ]------------
 WARNING: CPU: 1 PID: 116244 at fs/inode.c:302 drop_nlink+0x28/0x40

After upper hardlinks were added while overlay is mounted, unlinking all
overlay hardlinks drops overlay nlink to zero before all upper inodes
are unlinked.

Detect too low i_nlink before unlink/rename and set the overlay nlink
to the upper inode nlink (minus one for an index entry).

Reported-by: Phasip <phasip@gmail.com>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
 fs/overlayfs/util.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)


This fix passed the reported reproducers (with index=off),
overlay/034 with (index=on) and overlay/034 with s/LOWER/UPPER:


As well as the rest of overlay/quick group.

I will post the overlay/034 fork as a separate test later.


diff --git a/fs/overlayfs/util.c b/fs/overlayfs/util.c
index 36b60788ee47..e894a14857c7 100644
--- a/fs/overlayfs/util.c
+++ b/fs/overlayfs/util.c
@@ -734,7 +734,9 @@ static void ovl_cleanup_index(struct dentry *dentry)
 int ovl_nlink_start(struct dentry *dentry)
 	struct inode *inode = d_inode(dentry);
+	struct inode *iupper = ovl_inode_upper(inode);
 	const struct cred *old_cred;
+	int index_nlink;
 	int err;
 	if (WARN_ON(!inode))
@@ -764,7 +766,26 @@ int ovl_nlink_start(struct dentry *dentry)
 	if (err)
 		return err;
-	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
+	if (d_is_dir(dentry))
+		goto out;
+	/* index adds +1 to upper nlink */
+	index_nlink = !!ovl_test_flag(OVL_INDEX, inode);
+	if (iupper && (iupper->i_nlink - index_nlink) > inode->i_nlink) {
+		pr_warn_ratelimited("inode nlink too low (%pd2, ino=%lu, nlink=%u, upper_nlink=%u)\n",
+				    dentry, inode->i_ino, inode->i_nlink,
+				    iupper->i_nlink - index_nlink);
+		/*
+		 * Upper hardlinks were added while overlay is mounted.
+		 * Unlinking all overlay hardlinks would drop overlay nlink to
+		 * zero before all upper inodes are unlinked.  As a safety
+		 * measure, when that situation is detected, set the overlay
+		 * nlink to the upper inode nlink minus one for the index entry.
+		 */
+		set_nlink(d_inode(dentry), iupper->i_nlink - index_nlink);
+	}
+	if (!index_nlink)
 		goto out;
 	old_cred = ovl_override_creds(dentry->d_sb);

             reply index

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-23 19:08 Amir Goldstein [this message]
2020-03-24  9:48 ` Miklos Szeredi
2020-03-24 10:20   ` Amir Goldstein
2020-03-25 10:33     ` Miklos Szeredi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200323190850.3091-1-amir73il@gmail.com \
    --to=amir73il@gmail.com \
    --cc=linux-unionfs@vger.kernel.org \
    --cc=miklos@szeredi.hu \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

($INBOX_DIR/description missing)

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-unionfs/0 linux-unionfs/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-unionfs linux-unionfs/ https://lore.kernel.org/linux-unionfs \
	public-inbox-index linux-unionfs

Example config snippet for mirrors

Newsgroup available over NNTP:

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git