[PATCH] fs: clean up is_mounted() check with extra helper

From: Andrii Nakryiko
Date: Wed Oct 14 2020 - 21:38:25 EST


Add is_real_ns() helper validating that mount namespace is a valid one.
Use that from is_mounted() and clean up prepare_path() that open-coded similar
check.

Suggested-by: Alexander Viro <viro@xxxxxxxxxxxxxxxxxx>
Signed-off-by: Andrii Nakryiko <andrii@xxxxxxxxxx>
---
fs/d_path.c | 3 +--
fs/mount.h | 9 +++++++--
2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/fs/d_path.c b/fs/d_path.c
index a69e2cd36e6e..300bf675f097 100644
--- a/fs/d_path.c
+++ b/fs/d_path.c
@@ -119,8 +119,7 @@ static int prepend_path(const struct path *path,
continue;
}
mnt_ns = READ_ONCE(mnt->mnt_ns);
- /* open-coded is_mounted() to use local mnt_ns */
- if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
+ if (is_real_ns(mnt_ns) && !is_anon_ns(mnt_ns))
error = 1; // absolute root
else
error = 2; // detached or not attached yet
diff --git a/fs/mount.h b/fs/mount.h
index c7abb7b394d8..0b0eb18ba80a 100644
--- a/fs/mount.h
+++ b/fs/mount.h
@@ -92,10 +92,15 @@ static inline int mnt_has_parent(struct mount *mnt)
return mnt != mnt->mnt_parent;
}

-static inline int is_mounted(struct vfsmount *mnt)
+static inline bool is_real_ns(const struct mnt_namespace *mnt_ns)
{
/* neither detached nor internal? */
- return !IS_ERR_OR_NULL(real_mount(mnt)->mnt_ns);
+ return mnt_ns && mnt_ns != MNT_NS_INTERNAL;
+}
+
+static inline int is_mounted(struct vfsmount *mnt)
+{
+ return is_real_ns(real_mount(mnt)->mnt_ns);
}

extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *);
--
2.24.1