[PATCH] ovl: nfs_export: Parse index inode's nlink when 'd_inode(lowerdentry)->i_nlink == 1'

From: Zhihao Cheng
Date: Fri Aug 06 2021 - 05:43:14 EST


For overlayfs mounted by "nfs_export=on", all files/directories will be
indexed regardless of whether 'nlink > 1' satisfies. Following process
may cause breaking hard links on copy up:

mkdir low upper work merge
touch low/file
mount -t overlay over
-oupperdir=upper,lowerdir=low,workdir=work,nfs_export=on merge
chmod +x merge/file
# Now nlink of upper/file's correspoding index file equals to 2,
and we have xattr "trusted.overlay.nlink=U-1" on upper/file
echo 3 > /proc/sys/vm/drop_caches
# merge/file's nlink changes to 2

For nfs_export enabled overlayfs, don't ignore parsing index inode's nlink
when 'd_inode(lowerdentry)->i_nlink == 1'.

Fixes: f168f1098dd9038 ("ovl: add support for "nfs_export" configuration")
Signed-off-by: Zhihao Cheng <chengzhihao1@xxxxxxxxxx>
---
fs/overlayfs/inode.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c
index 5e828a1c98a8..75a61243371c 100644
--- a/fs/overlayfs/inode.c
+++ b/fs/overlayfs/inode.c
@@ -832,7 +832,8 @@ unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
char buf[13];
int err;

- if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
+ if (!lowerdentry || !upperdentry ||
+ (!ofs->config.nfs_export && d_inode(lowerdentry)->i_nlink == 1))
return fallback;

err = ovl_do_getxattr(ofs, upperdentry, OVL_XATTR_NLINK,
--
2.31.1