Skip to contentSkip to conversation

ovl: add ioctls to retrieve layer file descriptors

PATCH In review Giuseppe Scrivano 4 participants 20 messages Jul 8 – Jul 10

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

PATCH 1/1 ovl: add ioctls to retrieve layer file descriptors

Add two ioctls to overlay filesystem to allow userspace to retrieve information about the overlay layers:

OVL_IOC_OPEN_LAYER: return an O_PATH fd to the root of a layer.
arg == 0 returns the upper layer (-ENOENT if no upper is configured), arg >= 1 returns lower layers (-ENOENT if index is out of range).
OVL_IOC_GET_LAYERS_INFO: copy a struct ovl_layers_info to userspace
with numlower, numlowerdata, and has_upper.

The ioctls work on any overlayfs file or directory and require CAP_SYS_ADMIN in the mounter's user namespace.

The UAPI constants and struct are defined in include/uapi/linux/overlay.h.

---
 MAINTAINERS                  |  1 +
 fs/overlayfs/file.c          |  2 ++
 fs/overlayfs/overlayfs.h     |  4 +++
 fs/overlayfs/ovl_entry.h     |  2 ++
 fs/overlayfs/params.c        | 10 ++++++
 fs/overlayfs/params.h        |  1 +
 fs/overlayfs/readdir.c       |  2 ++
 fs/overlayfs/super.c         | 70 ++++++++++++++++++++++++++++++++++++
 include/uapi/linux/overlay.h | 30 ++++++++++++++++
 9 files changed, 122 insertions(+)
 create mode 100644 include/uapi/linux/overlay.h
MAINTAINERS +1 -0
index 25453040dffb..b64c696686e4 100644
@@ -20368,6 +20368,7 @@ S:	Supported
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/overlayfs/vfs.git
 F:	Documentation/filesystems/overlayfs.rst
 F:	fs/overlayfs/
+F:	include/uapi/linux/overlay.h
 
 P54 WIRELESS DRIVER
 M:	Christian Lamparter <[email protected]>
fs/overlayfs/file.c +2 -0
index 27cc07738f33..fc9c448c5959 100644
@@ -649,4 +649,6 @@ const struct file_operations ovl_file_operations = {
 	.copy_file_range	= ovl_copy_file_range,
 	.remap_file_range	= ovl_remap_file_range,
 	.setlease		= generic_setlease,
+	.unlocked_ioctl		= ovl_ioctl,
+	.compat_ioctl		= compat_ptr_ioctl,
 };
fs/overlayfs/overlayfs.h +4 -0
index b75df37f70ac..10f7ef8cb78c 100644
@@ -8,6 +8,7 @@
 #include <linux/uuid.h>
 #include <linux/fs.h>
 #include <linux/fsverity.h>
+#include <uapi/linux/overlay.h>
 #include <linux/namei.h>
 #include <linux/posix_acl.h>
 #include <linux/posix_acl_xattr.h>
@@ -908,6 +909,9 @@ void ovl_tempname(char name[OVL_TEMPNAME_SIZE]);
 struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
 			       struct ovl_cattr *attr);
 
+/* super.c */
+long ovl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
+
 /* file.c */
 extern const struct file_operations ovl_file_operations;
 int ovl_real_fileattr_get(const struct path *realpath, struct file_kattr *fa);
fs/overlayfs/ovl_entry.h +2 -0
index 80cad4ea96a3..b8f4bca89a27 100644
@@ -35,6 +35,8 @@ struct ovl_layer {
 	struct vfsmount *mnt;
 	/* Trap in ovl inode cache */
 	struct inode *trap;
+	/* Keeps the original fsmount file alive for OVL_IOC_OPEN_LAYER */
+	struct file *origin;
 	struct ovl_sb *fs;
 	/* Index of this layer in fs root (upper idx == 0) */
 	int idx;
fs/overlayfs/params.c +10 -0
index c93fcaa45d4a..92d1a56178f2 100644
@@ -482,6 +482,11 @@ static int ovl_parse_layer(struct fs_context *fc, struct fs_parameter *param,
 			return PTR_ERR(layer_name);
 
 		err = ovl_do_parse_layer(fc, layer_name, &layer_path, layer);
+		if (!err && !is_upper_layer(layer)) {
+			struct ovl_fs_context *ctx = fc->fs_private;
+
+			ctx->lower[ctx->nr - 1].origin = get_file(param->file);
+		}
 		break;
 	}
 	default:
@@ -504,6 +509,9 @@ static void ovl_reset_lowerdirs(struct ovl_fs_context *ctx)
 		path_put(&l->path);
 		kfree(l->name);
 		l->name = NULL;
+		if (l->origin)
+			fput(l->origin);
+		l->origin = NULL;
 	}
 	ctx->nr = 0;
 	ctx->nr_data = 0;
@@ -856,6 +864,8 @@ void ovl_free_fs(struct ovl_fs *ofs)
 	mounts = (struct vfsmount **) ofs->config.lowerdirs;
 	for (i = 0; i < ofs->numlayer; i++) {
 		iput(ofs->layers[i].trap);
+		if (ofs->layers[i].origin)
+			fput(ofs->layers[i].origin);
 		kfree(ofs->config.lowerdirs[i]);
 		mounts[i] = ofs->layers[i].mnt;
 	}
fs/overlayfs/params.h +1 -0
index ffd53cdd8482..1d8fe8fbaca2 100644
@@ -22,6 +22,7 @@ struct ovl_opt_set {
 struct ovl_fs_context_layer {
 	char *name;
 	struct path path;
+	struct file *origin;
 };
 
 struct ovl_fs_context {
fs/overlayfs/readdir.c +2 -0
index e7fe29cb6028..7bab71c8bcc2 100644
@@ -1069,6 +1069,8 @@ const struct file_operations ovl_dir_operations = {
 	.iterate_shared	= shared_ovl_iterate,
 	.llseek		= ovl_dir_llseek,
 	.fsync		= ovl_dir_fsync,
+	.unlocked_ioctl	= ovl_ioctl,
+	.compat_ioctl	= compat_ptr_ioctl,
 	.release	= ovl_dir_release,
 	.setlease	= generic_setlease,
 };
fs/overlayfs/super.c +70 -0

82 lines not on this page · raw message

include/uapi/linux/overlay.h new file +30 -0
new file mode 100644
index 000000000000..c92ccecd9e21
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_LINUX_OVERLAY_H
+#define _UAPI_LINUX_OVERLAY_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+/**
+ * struct ovl_layers_info - overlay layer configuration summary
+ * @numlower:     number of lower (metadata) layers
+ * @numlowerdata: number of data-only lower layers
+ * @has_upper:    1 if an upper layer is configured, 0 otherwise
+ */
+struct ovl_layers_info {
+	__u32 numlower;
+	__u32 numlowerdata;
+	__u32 has_upper;
+};
+
+/*
+ * Return an O_PATH fd to the root of the specified overlay layer.
+ * arg == 0: upper layer (returns -ENOENT if no upper is configured)
+ * arg >= 1: lower layers (returns -ENOENT if index is out of range)
+ */
+#define OVL_IOC_OPEN_LAYER	_IO('O', 1)
+
+/* Retrieve overlay layer configuration into struct ovl_layers_info. */
+#define OVL_IOC_GET_LAYERS_INFO	_IOR('O', 2, struct ovl_layers_info)
+
+#endif /* _UAPI_LINUX_OVERLAY_H */
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 9 lines · Giuseppe Scrivano

Add two ioctls to overlay filesystem to allow userspace to retrieve information about the overlay layers:

OVL_IOC_OPEN_LAYER: return an O_PATH fd to the root of a layer.
arg == 0 returns the upper layer (-ENOENT if no upper is configured), arg >= 1 returns lower layers (-ENOENT if index is out of range).

We could do this with a plain open() call. Something like the magic
symlinks we have under /proc/PID/fd/. Question is where could these
live...

Quoted 2 lines

OVL_IOC_GET_LAYERS_INFO: copy a struct ovl_layers_info to userspace
with numlower, numlowerdata, and has_upper.

Isn't this info obtainable via statmount(2) already? If not, it should be there, instead of a specialized ioctl.

fs/overlayfs/ovl_entry.h struct ovl_layer { · line 39

Quoted 8 lines
fs/overlayfs/ovl_entry.h +2 -0
@@ -35,6 +35,8 @@ struct ovl_layer {
        struct vfsmount *mnt;
        /* Trap in ovl inode cache */
        struct inode *trap;
+       /* Keeps the original fsmount file alive for OVL_IOC_OPEN_LAYER */
+       struct file *origin;

Don't need to keep the file open: the only info missing is the original vfsmount, everything else is already there to reconstruct the file.

Thanks,
Miklos

#2 lore raw
Quoted 11 lines · Miklos Szeredi
Quoted 9 lines · Giuseppe Scrivano

Add two ioctls to overlay filesystem to allow userspace to retrieve information about the overlay layers:

OVL_IOC_OPEN_LAYER: return an O_PATH fd to the root of a layer.
arg == 0 returns the upper layer (-ENOENT if no upper is configured), arg >= 1 returns lower layers (-ENOENT if index is out of range).

Quoted 3 lines

We could do this with a plain open() call. Something like the magic
symlinks we have under /proc/PID/fd/. Question is where could these
live...

is there any existing user of such a mechanism? I don't see any mount specific info under /proc or /sys.

Quoted 6 lines
Quoted 3 lines

OVL_IOC_GET_LAYERS_INFO: copy a struct ovl_layers_info to userspace
with numlower, numlowerdata, and has_upper.

Isn't this info obtainable via statmount(2) already? If not, it should be there, instead of a specialized ioctl.

no that is not exposed by statmount and I don't see any way to export file system specific data through it. Do you've anything in mind?

Quoted 4 lines
Quoted 4 lines
fs/overlayfs/ovl_entry.h +0 -0
@@ -35,6 +35,8 @@ struct ovl_layer {
        struct vfsmount *mnt;
Quoted 8 lines

fs/overlayfs/ovl_entry.h struct ovl_layer { · line 39

Quoted 4 lines
fs/overlayfs/ovl_entry.h +2 -0
        /* Trap in ovl inode cache */
        struct inode *trap;
+       /* Keeps the original fsmount file alive for OVL_IOC_OPEN_LAYER */
+       struct file *origin;

Don't need to keep the file open: the only info missing is the original vfsmount, everything else is already there to reconstruct the file.

I didn't manage to get that to work. As soon as the userspace process closes the mount fd that was passed to fsconfig, the anonymous mount namespace is destroyed and dissolve_on_fput sets mnt->mnt_ns to NULL.

So whenever I try to use this mount again from userspace, it is not usable because the mount namespace is empty, causing check_mnt() to fail.

Do you have any suggestions on how to solve this problem?

Regards,
Giuseppe

maintainer
#3 lore raw
Quoted 48 lines · Giuseppe Scrivano
Quoted 11 lines · Miklos Szeredi
Quoted 9 lines · Giuseppe Scrivano

Add two ioctls to overlay filesystem to allow userspace to retrieve information about the overlay layers:

OVL_IOC_OPEN_LAYER: return an O_PATH fd to the root of a layer.
arg == 0 returns the upper layer (-ENOENT if no upper is configured), arg >= 1 returns lower layers (-ENOENT if index is out of range).

Quoted 3 lines

We could do this with a plain open() call. Something like the magic
symlinks we have under /proc/PID/fd/. Question is where could these
live...

is there any existing user of such a mechanism? I don't see any mount specific info under /proc or /sys.

Quoted 6 lines
Quoted 3 lines

OVL_IOC_GET_LAYERS_INFO: copy a struct ovl_layers_info to userspace
with numlower, numlowerdata, and has_upper.

Isn't this info obtainable via statmount(2) already? If not, it should be there, instead of a specialized ioctl.

no that is not exposed by statmount and I don't see any way to export file system specific data through it. Do you've anything in mind?

Quoted 4 lines
Quoted 4 lines
fs/overlayfs/ovl_entry.h +0 -0
@@ -35,6 +35,8 @@ struct ovl_layer {
        struct vfsmount *mnt;
Quoted 8 lines

fs/overlayfs/ovl_entry.h struct ovl_layer { · line 39

Quoted 4 lines
fs/overlayfs/ovl_entry.h +2 -0
        /* Trap in ovl inode cache */
        struct inode *trap;
+       /* Keeps the original fsmount file alive for OVL_IOC_OPEN_LAYER */
+       struct file *origin;

Don't need to keep the file open: the only info missing is the original vfsmount, everything else is already there to reconstruct the file.

I didn't manage to get that to work. As soon as the userspace process closes the mount fd that was passed to fsconfig, the anonymous mount namespace is destroyed and dissolve_on_fput sets mnt->mnt_ns to NULL.

Quoted 5 lines

So whenever I try to use this mount again from userspace, it is not usable because the mount namespace is empty, causing check_mnt() to fail.

Do you have any suggestions on how to solve this problem?

The suggestion was to store origin->f_path->mnt instead of storing origin file, because you only end up using the origin vfsmount.

Thanks,
Amir.

#4 lore raw
Quoted 49 lines · Amir Goldstein
Quoted 48 lines · Giuseppe Scrivano
Quoted 11 lines · Miklos Szeredi
Quoted 9 lines · Giuseppe Scrivano

Add two ioctls to overlay filesystem to allow userspace to retrieve information about the overlay layers:

OVL_IOC_OPEN_LAYER: return an O_PATH fd to the root of a layer.
arg == 0 returns the upper layer (-ENOENT if no upper is configured), arg >= 1 returns lower layers (-ENOENT if index is out of range).

Quoted 3 lines

We could do this with a plain open() call. Something like the magic
symlinks we have under /proc/PID/fd/. Question is where could these
live...

is there any existing user of such a mechanism? I don't see any mount specific info under /proc or /sys.

Quoted 6 lines
Quoted 3 lines

OVL_IOC_GET_LAYERS_INFO: copy a struct ovl_layers_info to userspace
with numlower, numlowerdata, and has_upper.

Isn't this info obtainable via statmount(2) already? If not, it should be there, instead of a specialized ioctl.

no that is not exposed by statmount and I don't see any way to export file system specific data through it. Do you've anything in mind?

Quoted 4 lines
Quoted 4 lines
fs/overlayfs/ovl_entry.h +0 -0
@@ -35,6 +35,8 @@ struct ovl_layer {
        struct vfsmount *mnt;
Quoted 8 lines

fs/overlayfs/ovl_entry.h struct ovl_layer { · line 39

Quoted 4 lines
fs/overlayfs/ovl_entry.h +2 -0
        /* Trap in ovl inode cache */
        struct inode *trap;
+       /* Keeps the original fsmount file alive for OVL_IOC_OPEN_LAYER */
+       struct file *origin;

Don't need to keep the file open: the only info missing is the original vfsmount, everything else is already there to reconstruct the file.

I didn't manage to get that to work. As soon as the userspace process closes the mount fd that was passed to fsconfig, the anonymous mount namespace is destroyed and dissolve_on_fput sets mnt->mnt_ns to NULL.

Quoted 8 lines
Quoted 5 lines

So whenever I try to use this mount again from userspace, it is not usable because the mount namespace is empty, causing check_mnt() to fail.

Do you have any suggestions on how to solve this problem?

The suggestion was to store origin->f_path->mnt instead of storing origin file, because you only end up using the origin vfsmount.

the reason I am keeping the file and not just the vfsmount is that the file is what keeps the mount namespace alive (preventing dissolve_on_fput from fire).

Should we export open_detached_copy from fs/namespace.c? I've not tested it, but it might work. Are there other ways to solve it that I am not seeing?

Thanks,
Giuseppe

maintainer
#5 lore raw
Quoted 61 lines · Giuseppe Scrivano
Quoted 49 lines · Amir Goldstein
Quoted 48 lines · Giuseppe Scrivano
Quoted 11 lines · Miklos Szeredi
Quoted 9 lines · Giuseppe Scrivano

Add two ioctls to overlay filesystem to allow userspace to retrieve information about the overlay layers:

OVL_IOC_OPEN_LAYER: return an O_PATH fd to the root of a layer.
arg == 0 returns the upper layer (-ENOENT if no upper is configured), arg >= 1 returns lower layers (-ENOENT if index is out of range).

Quoted 3 lines

We could do this with a plain open() call. Something like the magic
symlinks we have under /proc/PID/fd/. Question is where could these
live...

is there any existing user of such a mechanism? I don't see any mount specific info under /proc or /sys.

Quoted 6 lines
Quoted 3 lines

OVL_IOC_GET_LAYERS_INFO: copy a struct ovl_layers_info to userspace
with numlower, numlowerdata, and has_upper.

Isn't this info obtainable via statmount(2) already? If not, it should be there, instead of a specialized ioctl.

no that is not exposed by statmount and I don't see any way to export file system specific data through it. Do you've anything in mind?

Quoted 4 lines
Quoted 4 lines
fs/overlayfs/ovl_entry.h +0 -0
@@ -35,6 +35,8 @@ struct ovl_layer {
        struct vfsmount *mnt;
Quoted 8 lines

fs/overlayfs/ovl_entry.h struct ovl_layer { · line 39

Quoted 4 lines
fs/overlayfs/ovl_entry.h +2 -0
        /* Trap in ovl inode cache */
        struct inode *trap;
+       /* Keeps the original fsmount file alive for OVL_IOC_OPEN_LAYER */
+       struct file *origin;

Don't need to keep the file open: the only info missing is the original vfsmount, everything else is already there to reconstruct the file.

I didn't manage to get that to work. As soon as the userspace process closes the mount fd that was passed to fsconfig, the anonymous mount namespace is destroyed and dissolve_on_fput sets mnt->mnt_ns to NULL.

Quoted 8 lines
Quoted 5 lines

So whenever I try to use this mount again from userspace, it is not usable because the mount namespace is empty, causing check_mnt() to fail.

Do you have any suggestions on how to solve this problem?

The suggestion was to store origin->f_path->mnt instead of storing origin file, because you only end up using the origin vfsmount.

Quoted 7 lines

the reason I am keeping the file and not just the vfsmount is that the file is what keeps the mount namespace alive (preventing dissolve_on_fput from fire).

Should we export open_detached_copy from fs/namespace.c? I've not tested it, but it might work. Are there other ways to solve it that I am not seeing?

Using an anon namespace sounds good to me, that means the original vfsmount isn't needed at all.

Not sure if it's okay for the case where the original ns is not anon, but we can save the vfsmount in that case if it turns out to be a problem.

Thanks,
Miklos

#6 lore raw
Quoted 73 lines · Miklos Szeredi
Quoted 61 lines · Giuseppe Scrivano
Quoted 49 lines · Amir Goldstein
Quoted 48 lines · Giuseppe Scrivano
Quoted 11 lines · Miklos Szeredi
Quoted 9 lines · Giuseppe Scrivano

Add two ioctls to overlay filesystem to allow userspace to retrieve information about the overlay layers:

OVL_IOC_OPEN_LAYER: return an O_PATH fd to the root of a layer.
arg == 0 returns the upper layer (-ENOENT if no upper is configured), arg >= 1 returns lower layers (-ENOENT if index is out of range).

Quoted 3 lines

We could do this with a plain open() call. Something like the magic
symlinks we have under /proc/PID/fd/. Question is where could these
live...

is there any existing user of such a mechanism? I don't see any mount specific info under /proc or /sys.

Quoted 6 lines
Quoted 3 lines

OVL_IOC_GET_LAYERS_INFO: copy a struct ovl_layers_info to userspace
with numlower, numlowerdata, and has_upper.

Isn't this info obtainable via statmount(2) already? If not, it should be there, instead of a specialized ioctl.

no that is not exposed by statmount and I don't see any way to export file system specific data through it. Do you've anything in mind?

Quoted 4 lines
Quoted 4 lines
fs/overlayfs/ovl_entry.h +0 -0
@@ -35,6 +35,8 @@ struct ovl_layer {
        struct vfsmount *mnt;
Quoted 8 lines

fs/overlayfs/ovl_entry.h struct ovl_layer { · line 39

Quoted 4 lines
fs/overlayfs/ovl_entry.h +2 -0
        /* Trap in ovl inode cache */
        struct inode *trap;
+       /* Keeps the original fsmount file alive for OVL_IOC_OPEN_LAYER */
+       struct file *origin;

Don't need to keep the file open: the only info missing is the original vfsmount, everything else is already there to reconstruct the file.

I didn't manage to get that to work. As soon as the userspace process closes the mount fd that was passed to fsconfig, the anonymous mount namespace is destroyed and dissolve_on_fput sets mnt->mnt_ns to NULL.

Quoted 8 lines
Quoted 5 lines

So whenever I try to use this mount again from userspace, it is not usable because the mount namespace is empty, causing check_mnt() to fail.

Do you have any suggestions on how to solve this problem?

The suggestion was to store origin->f_path->mnt instead of storing origin file, because you only end up using the origin vfsmount.

Quoted 7 lines

the reason I am keeping the file and not just the vfsmount is that the file is what keeps the mount namespace alive (preventing dissolve_on_fput from fire).

Should we export open_detached_copy from fs/namespace.c? I've not tested it, but it might work. Are there other ways to solve it that I am not seeing?

Using an anon namespace sounds good to me, that means the original vfsmount isn't needed at all.

Quoted 6 lines

Not sure if it's okay for the case where the original ns is not anon, but we can save the vfsmount in that case if it turns out to be a problem.

Thanks,
Miklos

for this to work, I need something like:

fs/namespace.c +34 -0
index 3d5cd5bf3b05..138d15ab37ef 100644
@@ -3091,6 +3091,52 @@ static struct file *open_detached_copy(struct path *path, unsigned int flags)
 	return file;
 }
 
+struct file *open_detached_copy_internal(struct path *path)
+{
+	struct mnt_namespace *ns, *mnt_ns = current->nsproxy->mnt_ns;
+	struct mount *mnt;
+	struct file *file;
+
+	ns = alloc_mnt_ns(mnt_ns->user_ns, true);
+	if (IS_ERR(ns))
+		return ERR_CAST(ns);
+
+	guard(namespace_excl)();
+
+	mnt = clone_mnt(real_mount(path->mnt), path->dentry,
+			CL_COPY_MNT_NS_FILE);
+	if (IS_ERR(mnt)) {
+		free_mnt_ns(ns);
+		return ERR_CAST(mnt);
+	}
+
+	mnt_add_to_ns(ns, mnt);
+	ns->nr_mounts++;
+	ns->root = mnt;
+
+	mntput(path->mnt);
+	path->mnt = mntget(&mnt->mnt);
+	file = dentry_open(path, O_PATH, current_cred());
+	if (IS_ERR(file))
+		dissolve_on_fput(path->mnt);
+	else
+		file->f_mode |= FMODE_NEED_UNMOUNT;
+	return file;
+}
+EXPORT_SYMBOL_GPL(open_detached_copy_internal);
+
 enum mount_copy_flags_t {
 	MOUNT_COPY_RECURSIVE    = (1 << 0),
 	MOUNT_COPY_NEW		= (1 << 1),

is this acceptable?

Regards,
Giuseppe

maintainer
#7 lore raw
In reply to Giuseppe Scrivano
Quoted 2 lines · Giuseppe Scrivano

is there any existing user of such a mechanism? I don't see any mount specific info under /proc or /sys.

Not yet, but I think it would be good to have one. It's definitely a bigger bite than adding an ioctl(), though.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

OVL_IOC_GET_LAYERS_INFO: copy a struct ovl_layers_info to userspace
with numlower, numlowerdata, and has_upper.

Isn't this info obtainable via statmount(2) already? If not, it should be there, instead of a specialized ioctl.

no that is not exposed by statmount

I mean, you should be able to get that info by counting the lowerdir+, datadir+ and upperdir options, no?

Thanks,
Miklos

#8 lore raw
Quoted 11 lines · Miklos Szeredi
Quoted 2 lines · Giuseppe Scrivano

is there any existing user of such a mechanism? I don't see any mount specific info under /proc or /sys.

Not yet, but I think it would be good to have one. It's definitely a bigger bite than adding an ioctl(), though.

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

OVL_IOC_GET_LAYERS_INFO: copy a struct ovl_layers_info to userspace
with numlower, numlowerdata, and has_upper.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Isn't this info obtainable via statmount(2) already? If not, it should be there, instead of a specialized ioctl.

no that is not exposed by statmount

I mean, you should be able to get that info by counting the lowerdir+, datadir+ and upperdir options, no?

Amir suggested to add that functionality when I've asked for some feedback before sending the patch here. I am fine to drop it if this is the consensus although I see its utility from user space.

Regards,
Giuseppe

maintainer
#9 lore raw
Quoted 3 lines · Giuseppe Scrivano

Amir suggested to add that functionality when I've asked for some feedback before sending the patch here. I am fine to drop it if this is the consensus although I see its utility from user space.

How about a completely different interface:

int get_fd_opt(const char *name, unsigned int index, unsigned int flags);

Enumerating layers would be as easy as passing an index stating from zero and stopping when -ERANGE is received.

It would work for all filesystems that use files as options. No more fs specific ioctls.

Thanks,
Miklos

#10 lore raw
Quoted 8 lines · Miklos Szeredi
Quoted 3 lines · Giuseppe Scrivano

Amir suggested to add that functionality when I've asked for some feedback before sending the patch here. I am fine to drop it if this is the consensus although I see its utility from user space.

How about a completely different interface:

Quoted 7 lines

int get_fd_opt(const char *name, unsigned int index, unsigned int flags);

Enumerating layers would be as easy as passing an index stating from zero and stopping when -ERANGE is received.

It would work for all filesystems that use files as options. No more fs specific ioctls.

Is a new syscall really justified for such a narrow use case?

On the other hand, it would cover both ioctl's I am working on right now.

Regards,
Giuseppe

maintainer
#11 lore raw
Quoted 8 lines · Giuseppe Scrivano
Quoted 5 lines · Miklos Szeredi
Quoted 3 lines · Giuseppe Scrivano

Amir suggested to add that functionality when I've asked for some feedback before sending the patch here. I am fine to drop it if this is the consensus although I see its utility from user space.

I was thinking that getting the number of layers or info would be a good idea to complement getting a layer fd.

I agree that the same information is probably available via statmount by parsing the upperdir/lowerdir/datadir mount options.

Quoted 5 lines
Quoted 5 lines

How about a completely different interface:

int get_fd_opt(const char *name, unsigned int index, unsigned int flags);

Quoted 8 lines
Quoted 5 lines

Enumerating layers would be as easy as passing an index stating from zero and stopping when -ERANGE is received.

It would work for all filesystems that use files as options. No more fs specific ioctls.

Is a new syscall really justified for such a narrow use case?

I feel the same way.

Giuseppe,

Could you add some high level context in this thread on why you need this functionality.
I think it's this composefs-rs work. right?
https://github.com/giuseppe/composefs-rs/commits/reuse-mounts-and-prevent-gc-overlay/

I must say this seems a bit upside down to me.

If you want to keep a pool of mounted erofs images, you could do that in userspace -
create a service that indexes mounted erofs images by unique mount point paths. Then you can introspect the overlayfs mount options referring to those mount points.

Going through the kernel to get an fd and reuse that fd for a new overlayfs mount
sounds like a strange way of accomplishing this.

If the overlayfs mounter is unprivileged, it would have to go through systemd-mountfsd
to request a mount of erofs trusted image, right?

Can't the same service provide the "is_image_mounted" query which provides the mount path?

I am not against introspection of overlayfs, but I'd like to understand the use cases before finalizing the uapi.

Thanks,
Amir.

#12 lore raw
Quoted 39 lines · Amir Goldstein
Quoted 8 lines · Giuseppe Scrivano
Quoted 5 lines · Miklos Szeredi
Quoted 3 lines · Giuseppe Scrivano

Amir suggested to add that functionality when I've asked for some feedback before sending the patch here. I am fine to drop it if this is the consensus although I see its utility from user space.

I was thinking that getting the number of layers or info would be a good idea to complement getting a layer fd.

I agree that the same information is probably available via statmount by parsing the upperdir/lowerdir/datadir mount options.

Quoted 5 lines
Quoted 5 lines

How about a completely different interface:

int get_fd_opt(const char *name, unsigned int index, unsigned int flags);

Quoted 8 lines
Quoted 5 lines

Enumerating layers would be as easy as passing an index stating from zero and stopping when -ERANGE is received.

It would work for all filesystems that use files as options. No more fs specific ioctls.

Is a new syscall really justified for such a narrow use case?

I feel the same way.

Giuseppe,

Could you add some high level context in this thread on why you need this functionality.
I think it's this composefs-rs work. right?
https://github.com/giuseppe/composefs-rs/commits/reuse-mounts-and-prevent-gc-overlay/

Quoted 7 lines

I must say this seems a bit upside down to me.

If you want to keep a pool of mounted erofs images, you could do that in userspace -
create a service that indexes mounted erofs images by unique mount point paths. Then you can introspect the overlayfs mount options referring to those mount points.

A first issue is that the mount options won't have this information anymore, as we use /proc/self/fd/$i paths as lower dirs so we are sure the fd points exactly to the file we have measured its fs-verity digest before using it.

I know this can be achieved with a system daemon, but do we really need one if this information is already known to the kernel?

Combined with listmount/statmount for discovery and fs-verity for validation, the entire mechanism is stateless from userspace.

More in general we need a way to introspect overlay mounts to know where they are pointing to since paths can be hidden using /proc/*/fd symlinks, or files get replaced.

Another similar request:
https://github.com/systemd/systemd/issues/35017#issuecomment-2457333218

Quoted 7 lines

Going through the kernel to get an fd and reuse that fd for a new overlayfs mount
sounds like a strange way of accomplishing this.

If the overlayfs mounter is unprivileged, it would have to go through systemd-mountfsd
to request a mount of erofs trusted image, right?

off-topic but for now we are considering FUSE to deal with mounting EROFS as it would serve only the metadata anyway in a composefs setup.

Regards,
Giuseppe

Quoted 8 lines

Can't the same service provide the "is_image_mounted" query which provides the mount path?

I am not against introspection of overlayfs, but I'd like to understand the use cases before finalizing the uapi.

Thanks,
Amir.

#13 lore raw
In reply to Amir Goldstein
Quoted 3 lines · Amir Goldstein

If you want to keep a pool of mounted erofs images, you could do that in userspace -
create a service that indexes mounted erofs images by unique mount point paths.

You're right that it’d be possible to do this in user space.

However, overlayfs is used by many things (it’s a powerful “Swiss army knife”!) and this ability to reliably introspect its components is I think generally useful.

For example: https://github.com/systemd/systemd/issues/35017#issuecomment-2457333218

Various tools want to know the backing filesystem(s) and/or block devices thereof, and if we don’t have something like this in the general case every overlayfs user would need to agree on a scheme to store this data out of band - and manage its lifecycle as mounts change and deal with where that data is stored with respect to mount namespaces etc.

Even if we had this API, would it be better to have a user space cache server for the original use case here? Maybe. I could personally go either way.

But the introspection I think really would be generally useful and there are code bases (in systemd and in composefs at least) that would start using it (with fallback for old kernels) for at least that use case right away.

maintainer
#14 lore raw
In reply to Giuseppe Scrivano
Quoted 13 lines · Giuseppe Scrivano
Quoted 10 lines · Miklos Szeredi
Quoted 3 lines · Giuseppe Scrivano

Amir suggested to add that functionality when I've asked for some feedback before sending the patch here. I am fine to drop it if this is the consensus although I see its utility from user space.

How about a completely different interface:

int get_fd_opt(const char *name, unsigned int index, unsigned int flags);

Quoted 7 lines
Quoted 5 lines

Enumerating layers would be as easy as passing an index stating from zero and stopping when -ERANGE is received.

It would work for all filesystems that use files as options. No more fs specific ioctls.

Is a new syscall really justified for such a narrow use case?

That's the reason I advocate pseudo fs based solutions. Let's see, we had a proposal to use openat(), something. like:

openat(base_fd, "mount/options/lowerdir+/0", O_ALT | O_PATH);

Meaning that O_ALT switches to an alternative/meta namespace that is based on the given fd and in that meta namespace the tree under mount/ represents the attributes of the mount that base_fd is on.

See this post from Linus as well:

https://lore.kernel.org/all/CAHk-=wjzLmMRf=QG-n+1HnxWCx4KTQn9+OhVvUSJ=ZCQd6Y1WA@mail.gmail.com/

The statmount api took a different route, but for getting an O_PATH file this would be a very natural interface without added syscalls.

Thanks,
Miklos

#15 lore raw
Quoted 31 lines · Miklos Szeredi
Quoted 13 lines · Giuseppe Scrivano
Quoted 10 lines · Miklos Szeredi
Quoted 3 lines · Giuseppe Scrivano

Amir suggested to add that functionality when I've asked for some feedback before sending the patch here. I am fine to drop it if this is the consensus although I see its utility from user space.

How about a completely different interface:

int get_fd_opt(const char *name, unsigned int index, unsigned int flags);

Quoted 7 lines
Quoted 5 lines

Enumerating layers would be as easy as passing an index stating from zero and stopping when -ERANGE is received.

It would work for all filesystems that use files as options. No more fs specific ioctls.

Is a new syscall really justified for such a narrow use case?

That's the reason I advocate pseudo fs based solutions. Let's see, we had a proposal to use openat(), something. like:

openat(base_fd, "mount/options/lowerdir+/0", O_ALT | O_PATH);

Meaning that O_ALT switches to an alternative/meta namespace that is based on the given fd and in that meta namespace the tree under mount/ represents the attributes of the mount that base_fd is on.

Quoted 6 lines

See this post from Linus as well:

https://lore.kernel.org/all/CAHk-=wjzLmMRf=QG-n+1HnxWCx4KTQn9+OhVvUSJ=ZCQd6Y1WA@mail.gmail.com/

The statmount api took a different route, but for getting an O_PATH file this would be a very natural interface without added syscalls.

I don't argue against such an API, I only argue that I am probably not the best person to drive such a significant change :-)

Would you be OK with the ioctl as a solution that works today, and duplicate it when an alternative API materializes?

Thanks,
Giuseppe

maintainer
#16 lore raw
Quoted 2 lines · Giuseppe Scrivano

Would you be OK with the ioctl as a solution that works today, and duplicate it when an alternative API materializes?

Let me have a go at the pseudo fs interface first. I can probably do a quick 'n dirty prototype within a couple of days and see what reaction it gets...

Thanks,
Miklos

maintainer
#17 lore raw
In reply to Miklos Szeredi
Quoted 21 lines · Miklos Szeredi
Quoted 19 lines · Giuseppe Scrivano
Quoted 15 lines · Miklos Szeredi
Quoted 3 lines · Giuseppe Scrivano

Amir suggested to add that functionality when I've asked for some feedback before sending the patch here. I am fine to drop it if this is the consensus although I see its utility from user space.

How about a completely different interface:

int get_fd_opt(const char *name, unsigned int index, unsigned int flags);

Enumerating layers would be as easy as passing an index stating from zero and stopping when -ERANGE is received.

It would work for all filesystems that use files as options. No more fs specific ioctls.

Quoted 7 lines
Quoted 1 line

Is a new syscall really justified for such a narrow use case?

That's the reason I advocate pseudo fs based solutions. Let's see, we had a proposal to use openat(), something. like:

openat(base_fd, "mount/options/lowerdir+/0", O_ALT | O_PATH);

Ack for this, but please don't use mount/options/... it's a bit ugly because most mount options are not opan-able.

Please stick to something logical like "fs/layers/N" corresponding to ovl_fs::layers, where layer 0 is reserved for upper.
We could also support opening by aliases fs/layers/upper->0 etc, but no rush IMO.

What I am contemplating is whether we should implement introspection of the ovl_entry stack for merge dirs in addition or instead introspection of fs/layers.

IIRC, the root dir stack does NOT hold references to lowerdata dirs, so support for "fs/layers/N" is still needed.

For directories we could implement "real/0..N" with aliases real/upper->0 real/origin->1.

For a regular file we could have constant aliases real/upper->0 real/lower->1 real/lowerdata->2 and/or dynamic aliases real/data->{0,1,2}, real/metadata->{0,1}, real/origin->{0,1}.

Those could be used to implement "revert to origin". Specifically, I think Daan asked for it during the last LSFMM. Although I don't think we currently store the original lowerdata in ovl_entry when lookup is done post data copy up.

Thanks,
Amir.

maintainer
#18 lore raw
Quoted 2 lines · Amir Goldstein

Ack for this, but please don't use mount/options/... it's a bit ugly because most mount options are not opan-able.

True, but I was thinking of doing this as a reverse fsconfig() thing.

Quoted 4 lines

Please stick to something logical like "fs/layers/N" corresponding to ovl_fs::layers, where layer 0 is reserved for upper.
We could also support opening by aliases fs/layers/upper->0 etc, but no rush IMO.

Layer numbering has been internal to overlayfs to date. I'd prefer to keep the layer enumeration that is external, which is the mount option interface. Works for EROFS as well.

Quoted 3 lines

What I am contemplating is whether we should implement introspection of the ovl_entry stack for merge dirs in addition or instead introspection of fs/layers.

Interesting.

Quoted 2 lines

IIRC, the root dir stack does NOT hold references to lowerdata dirs, so support for "fs/layers/N" is still needed.

So let's just stick with the per-superblock layering for now, which is reflected in the mount options.

Quoted 4 lines

For directories we could implement "real/0..N" with aliases real/upper->0 real/origin->1.

Quoted 8 lines

For a regular file we could have constant aliases real/upper->0 real/lower->1 real/lowerdata->2 and/or dynamic aliases real/data->{0,1,2}, real/metadata->{0,1}, real/origin->{0,1}.

Those could be used to implement "revert to origin". Specifically, I think Daan asked for it during the last LSFMM. Although I don't think we currently store the original lowerdata in ovl_entry when lookup is done post data copy up.

I'm trying to focus on the "return an O_PATH descriptor for stacked layers" for now. It avoids having to deal with opening files or directories within the O_ALT namespace, since magic symlink following is used.

Thanks,
Miklos

maintainer
#19 lore raw
In reply to Giuseppe Scrivano
Quoted 36 lines · Giuseppe Scrivano

Add two ioctls to overlay filesystem to allow userspace to retrieve information about the overlay layers:

OVL_IOC_OPEN_LAYER: return an O_PATH fd to the root of a layer.
arg == 0 returns the upper layer (-ENOENT if no upper is configured), arg >= 1 returns lower layers (-ENOENT if index is out of range).
OVL_IOC_GET_LAYERS_INFO: copy a struct ovl_layers_info to userspace
with numlower, numlowerdata, and has_upper.

The ioctls work on any overlayfs file or directory and require CAP_SYS_ADMIN in the mounter's user namespace.

The UAPI constants and struct are defined in include/uapi/linux/overlay.h.

---
 MAINTAINERS                  |  1 +
 fs/overlayfs/file.c          |  2 ++
 fs/overlayfs/overlayfs.h     |  4 +++
 fs/overlayfs/ovl_entry.h     |  2 ++
 fs/overlayfs/params.c        | 10 ++++++
 fs/overlayfs/params.h        |  1 +
 fs/overlayfs/readdir.c       |  2 ++
 fs/overlayfs/super.c         | 70 ++++++++++++++++++++++++++++++++++++
 include/uapi/linux/overlay.h | 30 ++++++++++++++++
 9 files changed, 122 insertions(+)
 create mode 100644 include/uapi/linux/overlay.h
MAINTAINERS +0 -0
index 25453040dffb..b64c696686e4 100644

MAINTAINERS S: Supported · line 20371

Quoted 4 lines
MAINTAINERS +1 -0
@@ -20368,6 +20368,7 @@ S:      Supported
 T:     git git://git.kernel.org/pub/scm/linux/kernel/git/overlayfs/vfs.git
 F:     Documentation/filesystems/overlayfs.rst
 F:     fs/overlayfs/
+F:     include/uapi/linux/overlay.h

overlayfs.h please

Quoted 41 lines
MAINTAINERS +0 -0
 P54 WIRELESS DRIVER
 M:     Christian Lamparter <[email protected]>
fs/overlayfs/file.c +2 -0
index 27cc07738f33..fc9c448c5959 100644
@@ -649,4 +649,6 @@ const struct file_operations ovl_file_operations = {
        .copy_file_range        = ovl_copy_file_range,
        .remap_file_range       = ovl_remap_file_range,
        .setlease               = generic_setlease,
+       .unlocked_ioctl         = ovl_ioctl,
+       .compat_ioctl           = compat_ptr_ioctl,
 };
fs/overlayfs/overlayfs.h +4 -0
index b75df37f70ac..10f7ef8cb78c 100644
@@ -8,6 +8,7 @@
 #include <linux/uuid.h>
 #include <linux/fs.h>
 #include <linux/fsverity.h>
+#include <uapi/linux/overlay.h>
 #include <linux/namei.h>
 #include <linux/posix_acl.h>
 #include <linux/posix_acl_xattr.h>
@@ -908,6 +909,9 @@ void ovl_tempname(char name[OVL_TEMPNAME_SIZE]);
 struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
                               struct ovl_cattr *attr);
 
+/* super.c */
+long ovl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
+
 /* file.c */
 extern const struct file_operations ovl_file_operations;
 int ovl_real_fileattr_get(const struct path *realpath, struct file_kattr *fa);
fs/overlayfs/ovl_entry.h +0 -0
index 80cad4ea96a3..b8f4bca89a27 100644

fs/overlayfs/ovl_entry.h struct ovl_layer { · line 39

Quoted 5 lines
fs/overlayfs/ovl_entry.h +2 -0
@@ -35,6 +35,8 @@ struct ovl_layer {
        struct vfsmount *mnt;
        /* Trap in ovl inode cache */
        struct inode *trap;
+       /* Keeps the original fsmount file alive for OVL_IOC_OPEN_LAYER */
+       struct file *origin;

as Miklos wrote, keep the file is an overkill

include/uapi/linux/overlay.h

Quoted 156 lines
fs/overlayfs/ovl_entry.h +0 -0
        struct ovl_sb *fs;
        /* Index of this layer in fs root (upper idx == 0) */
        int idx;
fs/overlayfs/params.c +10 -0
index c93fcaa45d4a..92d1a56178f2 100644
@@ -482,6 +482,11 @@ static int ovl_parse_layer(struct fs_context *fc, struct fs_parameter *param,
                        return PTR_ERR(layer_name);
 
                err = ovl_do_parse_layer(fc, layer_name, &layer_path, layer);
+               if (!err && !is_upper_layer(layer)) {
+                       struct ovl_fs_context *ctx = fc->fs_private;
+
+                       ctx->lower[ctx->nr - 1].origin = get_file(param->file);
+               }
                break;
        }
        default:
@@ -504,6 +509,9 @@ static void ovl_reset_lowerdirs(struct ovl_fs_context *ctx)
                path_put(&l->path);
                kfree(l->name);
                l->name = NULL;
+               if (l->origin)
+                       fput(l->origin);
+               l->origin = NULL;
        }
        ctx->nr = 0;
        ctx->nr_data = 0;
@@ -856,6 +864,8 @@ void ovl_free_fs(struct ovl_fs *ofs)
        mounts = (struct vfsmount **) ofs->config.lowerdirs;
        for (i = 0; i < ofs->numlayer; i++) {
                iput(ofs->layers[i].trap);
+               if (ofs->layers[i].origin)
+                       fput(ofs->layers[i].origin);
                kfree(ofs->config.lowerdirs[i]);
                mounts[i] = ofs->layers[i].mnt;
        }
fs/overlayfs/params.h +1 -0
index ffd53cdd8482..1d8fe8fbaca2 100644
@@ -22,6 +22,7 @@ struct ovl_opt_set {
 struct ovl_fs_context_layer {
        char *name;
        struct path path;
+       struct file *origin;
 };
 
 struct ovl_fs_context {
fs/overlayfs/readdir.c +2 -0
index e7fe29cb6028..7bab71c8bcc2 100644
@@ -1069,6 +1069,8 @@ const struct file_operations ovl_dir_operations = {
        .iterate_shared = shared_ovl_iterate,
        .llseek         = ovl_dir_llseek,
        .fsync          = ovl_dir_fsync,
+       .unlocked_ioctl = ovl_ioctl,
+       .compat_ioctl   = compat_ptr_ioctl,
        .release        = ovl_dir_release,
        .setlease       = generic_setlease,
 };
fs/overlayfs/super.c +70 -0
index 60f0b7ceef0a..d143002e74b2 100644
@@ -1107,6 +1107,8 @@ static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
                 */
                mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
 
+               layers[ofs->numlayer].origin = l->origin;
+               l->origin = NULL;
                layers[ofs->numlayer].trap = trap;
                layers[ofs->numlayer].mnt = mnt;
                layers[ofs->numlayer].idx = ofs->numlayer;
@@ -1568,6 +1570,74 @@ int ovl_fill_super(struct super_block *sb, struct fs_context *fc)
        return err;
 }
 
+static long ovl_ioctl_open_layer(struct file *filp, unsigned long arg)
+{
+       struct super_block *sb = file_inode(filp)->i_sb;
+       struct ovl_fs *ofs = OVL_FS(sb);
+       struct path root;
+       struct file *f;
+       int fd;
+
+       if (arg >= ofs->numlayer)
+               return -ENOENT;
+       if (arg == 0 && !ovl_upper_mnt(ofs))
+               return -ENOENT;
+       if (!ofs->layers[arg].origin)
+               return -EOPNOTSUPP;
+
+       root.mnt = mntget(ofs->layers[arg].origin->f_path.mnt);
+       root.dentry = dget(root.mnt->mnt_root);
+
+       fd = get_unused_fd_flags(O_CLOEXEC);
+       if (fd < 0) {
+               path_put(&root);
+               return fd;
+       }
+
+       f = dentry_open(&root, O_PATH | O_NOFOLLOW, current_cred());
+       path_put(&root);
+       if (IS_ERR(f)) {
+               put_unused_fd(fd);
+               return PTR_ERR(f);
+       }
+
+       fd_install(fd, f);
+       return fd;
+}
+
+static long ovl_ioctl_get_layers_info(struct file *filp, unsigned long arg)
+{
+       struct super_block *sb = file_inode(filp)->i_sb;
+       struct ovl_fs *ofs = OVL_FS(sb);
+       struct ovl_layers_info info = {
+               .numlower = ofs->numlayer - 1,
+               .numlowerdata = ofs->numdatalayer,
+               .has_upper = !!ovl_upper_mnt(ofs),
+       };
+
+       if (copy_to_user((void __user *)arg, &info, sizeof(info)))
+               return -EFAULT;
+
+       return 0;
+}
+
+long ovl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+       struct ovl_fs *ofs = OVL_FS(file_inode(filp)->i_sb);
+
+       if (!ns_capable(ofs->creator_cred->user_ns, CAP_SYS_ADMIN))
+               return -EPERM;
+
+       switch (cmd) {
+       case OVL_IOC_OPEN_LAYER:
+               return ovl_ioctl_open_layer(filp, arg);
+       case OVL_IOC_GET_LAYERS_INFO:
+               return ovl_ioctl_get_layers_info(filp, arg);
+       default:
+               return -ENOTTY;
+       }
+}
+
 struct file_system_type ovl_fs_type = {
        .owner                  = THIS_MODULE,
        .name                   = "overlay",
include/uapi/linux/overlay.h new file +0 -0
new file mode 100644
index 000000000000..c92ccecd9e21

overlayfs.h please

Quoted 9 lines
include/uapi/linux/overlay.h new file +8 -0
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_LINUX_OVERLAY_H
+#define _UAPI_LINUX_OVERLAY_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+/**

include/uapi/linux/overlay.h line 18

Quoted 10 lines
include/uapi/linux/overlay.h new file +10 -0
+ * struct ovl_layers_info - overlay layer configuration summary
+ * @numlower:     number of lower (metadata) layers
+ * @numlowerdata: number of data-only lower layers
+ * @has_upper:    1 if an upper layer is configured, 0 otherwise
+ */
+struct ovl_layers_info {
+       __u32 numlower;
+       __u32 numlowerdata;
+       __u32 has_upper;
+};

Whether this stays as ioctl or statmount blob

please use:

__u32 flags;
__u32 pad;

and use a flag for has_upper so we can extend this data struct in the future.

Thanks,
Amir.