From: Vivek Goyal <vgoyal@redhat.com>
To: Hanna Reitz <hreitz@redhat.com>
Cc: virtio-fs@redhat.com, qemu-devel@nongnu.org
Subject: Re: [Virtio-fs] [PATCH v4 01/12] virtiofsd: Keep /proc/self/mountinfo open
Date: Wed, 20 Oct 2021 14:25:16 -0400 [thread overview]
Message-ID: <YXBfDHfZlnaqE7rf@redhat.com> (raw)
In-Reply-To: <fdc893e8-2858-9930-5b41-2d489e066c34@redhat.com>
On Wed, Oct 20, 2021 at 11:04:31AM +0200, Hanna Reitz wrote:
> On 18.10.21 19:07, Vivek Goyal wrote:
> > On Thu, Sep 16, 2021 at 10:40:34AM +0200, Hanna Reitz wrote:
> > > File handles are specific to mounts, and so name_to_handle_at() returns
> > > the respective mount ID. However, open_by_handle_at() is not content
> > > with an ID, it wants a file descriptor for some inode on the mount,
> > > which we have to open.
> > >
> > > We want to use /proc/self/mountinfo to find the mounts' root directories
> > > so we can open them and pass the respective FDs to open_by_handle_at().
> > > (We need to use the root directory, because we want the inode belonging
> > > to every mount FD be deletable. Before the root directory can be
> > > deleted, all entries within must have been closed, and so when it is
> > > deleted, there should not be any file handles left that need its FD as
> > > their mount FD. Thus, we can then close that FD and the inode can be
> > > deleted.[1])
> > >
> > > That is why we need to open /proc/self/mountinfo so that we can use it
> > > to translate mount IDs into root directory paths. We have to open it
> > > after setup_mounts() was called, because if we try to open it before, it
> > > will appear as an empty file after setup_mounts().
> > >
> > > [1] Note that in practice, you still cannot delete the mount root
> > > directory. It is a mount point on the host, after all, and mount points
> > > cannot be deleted. But by using the mount point as the mount FD, we
> > > will at least not hog any actually deletable inodes.
> > >
> > > Signed-off-by: Hanna Reitz <hreitz@redhat.com>
> > > ---
> > > tools/virtiofsd/passthrough_ll.c | 40 ++++++++++++++++++++++++++++++++
> > > 1 file changed, 40 insertions(+)
> > >
> > > diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
> > > index 38b2af8599..6511a6acb4 100644
> > > --- a/tools/virtiofsd/passthrough_ll.c
> > > +++ b/tools/virtiofsd/passthrough_ll.c
> > > @@ -172,6 +172,8 @@ struct lo_data {
> > > /* An O_PATH file descriptor to /proc/self/fd/ */
> > > int proc_self_fd;
> > > + /* A read-only FILE pointer for /proc/self/mountinfo */
> > > + FILE *mountinfo_fp;
> > > int user_killpriv_v2, killpriv_v2;
> > > /* If set, virtiofsd is responsible for setting umask during creation */
> > > bool change_umask;
> > > @@ -3718,6 +3720,19 @@ static void setup_chroot(struct lo_data *lo)
> > > static void setup_sandbox(struct lo_data *lo, struct fuse_session *se,
> > > bool enable_syslog)
> > > {
> > > + int proc_self, mountinfo_fd;
> > > + int saverr;
> > > +
> > > + /*
> > > + * Open /proc/self before we pivot to the new root so we can still
> > > + * open /proc/self/mountinfo afterwards
> > > + */
> > > + proc_self = open("/proc/self", O_PATH);
> > > + if (proc_self < 0) {
> > > + fuse_log(FUSE_LOG_WARNING, "Failed to open /proc/self: %m; "
> > > + "will not be able to use file handles\n");
> > > + }
> > > +
> > Hi Hanna,
> >
> > Should we open /proc/self and /proc/self/mountinfo only if user wants
> > to file handle. We have already parsed options by now so we know.
>
> I didn’t think it would matter given that it wouldn’t have an adverse
> effect. If we can’t open them (and I can’t imagine a case where we’d be
> unable to open them), the only result is a warning.
>
> > Also, if user asked for file handles, and we can't open /proc/self or
> > /proc/self/mountinfo successfully, I would think we should error out
> > and not continue (instead of just log it and continue).
>
> Well, that would break the assumption I had above. Not that that’s really
> relevant, I just want to mention it.
>
> File handles are a best effort in any case. If they don’t work, we always
> fall back. So I don’t know whether we must error out.
>
> OTOH if we know they can never work, then perhaps it would be more sensible
> to error out.
Yes. If they can't work because filesystem does not have capability or
we don't have CAP_DAC_READ_SEARCH or any other necessary component is
not there then we should fail.
>
> FWIW I’ve ported the relevant v1..v4 changes to virtiofsd-rs, and there it
> errors out. The error is unconditional, though, so even if you don’t
> request file handles, it’ll try to open mountinfo and exit on error. I
> found that reasonable because I can’t imagine a case where opening
> /proc/self/fd would work, but /proc/self/mountinfo wouldn’t – and working
> around that would be a bit cumbersome (it would mean wrapping
> PassthroughFs.mount_fds in an Option<> and .unwrap()-ing it on every use,
> with a comment why that’s fine). Honestly, I’d prefer to wait until we get a
> bug report about a failure to open /proc/self/mountinfo.
>
> > That seems to be general theme. If user asked for a feature and if
> > we can't enable it, we error out and let user retry without that
> > particular feature.
> >
> > > if (lo->sandbox == SANDBOX_NAMESPACE) {
> > > setup_namespaces(lo, se);
> > > setup_mounts(lo->source);
> > > @@ -3725,6 +3740,31 @@ static void setup_sandbox(struct lo_data *lo, struct fuse_session *se,
> > > setup_chroot(lo);
> > > }
> > > + /*
> > > + * Opening /proc/self/mountinfo before the umount2() call in
> > > + * setup_mounts() leads to the file appearing empty. That is why
> > > + * we defer opening it until here.
> > > + */
> > > + lo->mountinfo_fp = NULL;
> > > + if (proc_self >= 0) {
> > > + mountinfo_fd = openat(proc_self, "mountinfo", O_RDONLY);
> > > + if (mountinfo_fd < 0) {
> > > + saverr = errno;
> > > + } else if (mountinfo_fd >= 0) {
> > > + lo->mountinfo_fp = fdopen(mountinfo_fd, "r");
> > > + if (!lo->mountinfo_fp) {
> > > + saverr = errno;
> > > + close(mountinfo_fd);
> > > + }
> > > + }
> > > + if (!lo->mountinfo_fp) {
> > > + fuse_log(FUSE_LOG_WARNING, "Failed to open /proc/self/mountinfo: "
> > > + "%s; will not be able to use file handles\n",
> > > + strerror(saverr));
> > > + }
> > > + close(proc_self);
> > > + }
> > > +
> > Above code couple probably be moved in a helper function. Makes it
> > easier to read setup_sandbox(). Same here, open mountinfo only if
> > user wants file handle support and error out if file handle support
> > can't be enabled.
>
> Perhaps, but frankly I don’t see a need to keep setup_sandbox() readable.
> AFAIU, we are planning to deprecate C virtiofsd anyway, so while it pains me
> to say something like this, we don’t need to keep it maintainable.
>
> Now that I’ve opened an MR to bring the v1..v4 changes to virtiofsd-rs
> (https://gitlab.com/virtio-fs/virtiofsd-rs/-/merge_requests/41), I also
> don’t really see a justification for putting further development effort into
> bringing file handles to C virtiofsd. Of course I’m still grateful for your
> review, and I’ll try to adapt it to virtiofsd-rs, but right now I don’t plan
> on sending a v5.
Fair enough. If file handle stuff is important for C version, I am hoping
somebody else can pick this work. Anyway I think its almost 90% ready, we
are just dealing with last 10% of issues.
Vivek
WARNING: multiple messages have this Message-ID (diff)
From: Vivek Goyal <vgoyal@redhat.com>
To: Hanna Reitz <hreitz@redhat.com>
Cc: virtio-fs@redhat.com, qemu-devel@nongnu.org,
Stefan Hajnoczi <stefanha@redhat.com>,
"Dr . David Alan Gilbert" <dgilbert@redhat.com>
Subject: Re: [PATCH v4 01/12] virtiofsd: Keep /proc/self/mountinfo open
Date: Wed, 20 Oct 2021 14:25:16 -0400 [thread overview]
Message-ID: <YXBfDHfZlnaqE7rf@redhat.com> (raw)
In-Reply-To: <fdc893e8-2858-9930-5b41-2d489e066c34@redhat.com>
On Wed, Oct 20, 2021 at 11:04:31AM +0200, Hanna Reitz wrote:
> On 18.10.21 19:07, Vivek Goyal wrote:
> > On Thu, Sep 16, 2021 at 10:40:34AM +0200, Hanna Reitz wrote:
> > > File handles are specific to mounts, and so name_to_handle_at() returns
> > > the respective mount ID. However, open_by_handle_at() is not content
> > > with an ID, it wants a file descriptor for some inode on the mount,
> > > which we have to open.
> > >
> > > We want to use /proc/self/mountinfo to find the mounts' root directories
> > > so we can open them and pass the respective FDs to open_by_handle_at().
> > > (We need to use the root directory, because we want the inode belonging
> > > to every mount FD be deletable. Before the root directory can be
> > > deleted, all entries within must have been closed, and so when it is
> > > deleted, there should not be any file handles left that need its FD as
> > > their mount FD. Thus, we can then close that FD and the inode can be
> > > deleted.[1])
> > >
> > > That is why we need to open /proc/self/mountinfo so that we can use it
> > > to translate mount IDs into root directory paths. We have to open it
> > > after setup_mounts() was called, because if we try to open it before, it
> > > will appear as an empty file after setup_mounts().
> > >
> > > [1] Note that in practice, you still cannot delete the mount root
> > > directory. It is a mount point on the host, after all, and mount points
> > > cannot be deleted. But by using the mount point as the mount FD, we
> > > will at least not hog any actually deletable inodes.
> > >
> > > Signed-off-by: Hanna Reitz <hreitz@redhat.com>
> > > ---
> > > tools/virtiofsd/passthrough_ll.c | 40 ++++++++++++++++++++++++++++++++
> > > 1 file changed, 40 insertions(+)
> > >
> > > diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
> > > index 38b2af8599..6511a6acb4 100644
> > > --- a/tools/virtiofsd/passthrough_ll.c
> > > +++ b/tools/virtiofsd/passthrough_ll.c
> > > @@ -172,6 +172,8 @@ struct lo_data {
> > > /* An O_PATH file descriptor to /proc/self/fd/ */
> > > int proc_self_fd;
> > > + /* A read-only FILE pointer for /proc/self/mountinfo */
> > > + FILE *mountinfo_fp;
> > > int user_killpriv_v2, killpriv_v2;
> > > /* If set, virtiofsd is responsible for setting umask during creation */
> > > bool change_umask;
> > > @@ -3718,6 +3720,19 @@ static void setup_chroot(struct lo_data *lo)
> > > static void setup_sandbox(struct lo_data *lo, struct fuse_session *se,
> > > bool enable_syslog)
> > > {
> > > + int proc_self, mountinfo_fd;
> > > + int saverr;
> > > +
> > > + /*
> > > + * Open /proc/self before we pivot to the new root so we can still
> > > + * open /proc/self/mountinfo afterwards
> > > + */
> > > + proc_self = open("/proc/self", O_PATH);
> > > + if (proc_self < 0) {
> > > + fuse_log(FUSE_LOG_WARNING, "Failed to open /proc/self: %m; "
> > > + "will not be able to use file handles\n");
> > > + }
> > > +
> > Hi Hanna,
> >
> > Should we open /proc/self and /proc/self/mountinfo only if user wants
> > to file handle. We have already parsed options by now so we know.
>
> I didn’t think it would matter given that it wouldn’t have an adverse
> effect. If we can’t open them (and I can’t imagine a case where we’d be
> unable to open them), the only result is a warning.
>
> > Also, if user asked for file handles, and we can't open /proc/self or
> > /proc/self/mountinfo successfully, I would think we should error out
> > and not continue (instead of just log it and continue).
>
> Well, that would break the assumption I had above. Not that that’s really
> relevant, I just want to mention it.
>
> File handles are a best effort in any case. If they don’t work, we always
> fall back. So I don’t know whether we must error out.
>
> OTOH if we know they can never work, then perhaps it would be more sensible
> to error out.
Yes. If they can't work because filesystem does not have capability or
we don't have CAP_DAC_READ_SEARCH or any other necessary component is
not there then we should fail.
>
> FWIW I’ve ported the relevant v1..v4 changes to virtiofsd-rs, and there it
> errors out. The error is unconditional, though, so even if you don’t
> request file handles, it’ll try to open mountinfo and exit on error. I
> found that reasonable because I can’t imagine a case where opening
> /proc/self/fd would work, but /proc/self/mountinfo wouldn’t – and working
> around that would be a bit cumbersome (it would mean wrapping
> PassthroughFs.mount_fds in an Option<> and .unwrap()-ing it on every use,
> with a comment why that’s fine). Honestly, I’d prefer to wait until we get a
> bug report about a failure to open /proc/self/mountinfo.
>
> > That seems to be general theme. If user asked for a feature and if
> > we can't enable it, we error out and let user retry without that
> > particular feature.
> >
> > > if (lo->sandbox == SANDBOX_NAMESPACE) {
> > > setup_namespaces(lo, se);
> > > setup_mounts(lo->source);
> > > @@ -3725,6 +3740,31 @@ static void setup_sandbox(struct lo_data *lo, struct fuse_session *se,
> > > setup_chroot(lo);
> > > }
> > > + /*
> > > + * Opening /proc/self/mountinfo before the umount2() call in
> > > + * setup_mounts() leads to the file appearing empty. That is why
> > > + * we defer opening it until here.
> > > + */
> > > + lo->mountinfo_fp = NULL;
> > > + if (proc_self >= 0) {
> > > + mountinfo_fd = openat(proc_self, "mountinfo", O_RDONLY);
> > > + if (mountinfo_fd < 0) {
> > > + saverr = errno;
> > > + } else if (mountinfo_fd >= 0) {
> > > + lo->mountinfo_fp = fdopen(mountinfo_fd, "r");
> > > + if (!lo->mountinfo_fp) {
> > > + saverr = errno;
> > > + close(mountinfo_fd);
> > > + }
> > > + }
> > > + if (!lo->mountinfo_fp) {
> > > + fuse_log(FUSE_LOG_WARNING, "Failed to open /proc/self/mountinfo: "
> > > + "%s; will not be able to use file handles\n",
> > > + strerror(saverr));
> > > + }
> > > + close(proc_self);
> > > + }
> > > +
> > Above code couple probably be moved in a helper function. Makes it
> > easier to read setup_sandbox(). Same here, open mountinfo only if
> > user wants file handle support and error out if file handle support
> > can't be enabled.
>
> Perhaps, but frankly I don’t see a need to keep setup_sandbox() readable.
> AFAIU, we are planning to deprecate C virtiofsd anyway, so while it pains me
> to say something like this, we don’t need to keep it maintainable.
>
> Now that I’ve opened an MR to bring the v1..v4 changes to virtiofsd-rs
> (https://gitlab.com/virtio-fs/virtiofsd-rs/-/merge_requests/41), I also
> don’t really see a justification for putting further development effort into
> bringing file handles to C virtiofsd. Of course I’m still grateful for your
> review, and I’ll try to adapt it to virtiofsd-rs, but right now I don’t plan
> on sending a v5.
Fair enough. If file handle stuff is important for C version, I am hoping
somebody else can pick this work. Anyway I think its almost 90% ready, we
are just dealing with last 10% of issues.
Vivek
next prev parent reply other threads:[~2021-10-20 18:25 UTC|newest]
Thread overview: 60+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-16 8:40 [Virtio-fs] [PATCH v4 00/12] virtiofsd: Allow using file handles instead of O_PATH FDs Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 01/12] virtiofsd: Keep /proc/self/mountinfo open Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-10-18 17:07 ` [Virtio-fs] " Vivek Goyal
2021-10-18 17:07 ` Vivek Goyal
2021-10-20 9:04 ` [Virtio-fs] " Hanna Reitz
2021-10-20 9:04 ` Hanna Reitz
2021-10-20 18:25 ` Vivek Goyal [this message]
2021-10-20 18:25 ` Vivek Goyal
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 02/12] virtiofsd: Limit setxattr()'s creds-dropped region Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-10-18 17:20 ` [Virtio-fs] " Vivek Goyal
2021-10-18 17:20 ` Vivek Goyal
2021-10-20 9:11 ` [Virtio-fs] " Hanna Reitz
2021-10-20 9:11 ` Hanna Reitz
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 03/12] virtiofsd: Add TempFd structure Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 04/12] virtiofsd: Use lo_inode_open() instead of openat() Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 05/12] virtiofsd: Add lo_inode_fd() helper Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 06/12] virtiofsd: Let lo_fd() return a TempFd Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 07/12] virtiofsd: Let lo_inode_open() " Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-10-18 19:18 ` [Virtio-fs] " Vivek Goyal
2021-10-18 19:18 ` Vivek Goyal
2021-10-20 9:15 ` [Virtio-fs] " Hanna Reitz
2021-10-20 9:15 ` Hanna Reitz
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 08/12] virtiofsd: Pass lo_data to lo_inode_{fd, open}() Hanna Reitz
2021-09-16 8:40 ` [PATCH v4 08/12] virtiofsd: Pass lo_data to lo_inode_{fd,open}() Hanna Reitz
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 09/12] virtiofsd: Add lo_inode.fhandle Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 10/12] virtiofsd: Add inodes_by_handle hash table Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-10-19 20:02 ` [Virtio-fs] " Vivek Goyal
2021-10-19 20:02 ` Vivek Goyal
2021-10-20 10:02 ` [Virtio-fs] " Hanna Reitz
2021-10-20 10:02 ` Hanna Reitz
2021-10-20 12:29 ` [Virtio-fs] " Vivek Goyal
2021-10-20 12:29 ` Vivek Goyal
2021-10-20 14:10 ` [Virtio-fs] " Hanna Reitz
2021-10-20 14:10 ` Hanna Reitz
2021-10-20 18:06 ` [Virtio-fs] " Vivek Goyal
2021-10-20 18:06 ` Vivek Goyal
2021-10-20 12:53 ` [Virtio-fs] " Vivek Goyal
2021-10-20 12:53 ` Vivek Goyal
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 11/12] virtiofsd: Optionally fill lo_inode.fhandle Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-10-19 18:57 ` [Virtio-fs] " Vivek Goyal
2021-10-19 18:57 ` Vivek Goyal
2021-10-20 10:00 ` [Virtio-fs] " Hanna Reitz
2021-10-20 10:00 ` Hanna Reitz
2021-10-20 18:53 ` [Virtio-fs] " Vivek Goyal
2021-10-20 18:53 ` Vivek Goyal
2021-09-16 8:40 ` [Virtio-fs] [PATCH v4 12/12] virtiofsd: Add lazy lo_do_find() Hanna Reitz
2021-09-16 8:40 ` Hanna Reitz
2021-10-18 18:08 ` [Virtio-fs] [PATCH v4 00/12] virtiofsd: Allow using file handles instead of O_PATH FDs Vivek Goyal
2021-10-18 18:08 ` Vivek Goyal
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:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YXBfDHfZlnaqE7rf@redhat.com \
--to=vgoyal@redhat.com \
--cc=hreitz@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=virtio-fs@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.