[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v4 03/12] virtiofsd: Add TempFd structure
From: |
Hanna Reitz |
Subject: |
[PATCH v4 03/12] virtiofsd: Add TempFd structure |
Date: |
Thu, 16 Sep 2021 10:40:36 +0200 |
We are planning to add file handles to lo_inode objects as an
alternative to lo_inode.fd. That means that everywhere where we
currently reference lo_inode.fd, we will have to open a temporary file
descriptor that needs to be closed after use.
So instead of directly accessing lo_inode.fd, there will be a helper
function (lo_inode_fd()) that either returns lo_inode.fd, or opens a new
file descriptor with open_by_handle_at(). It encapsulates this result
in a TempFd structure to let the caller know whether the FD needs to be
closed after use (opened from the handle) or not (copied from
lo_inode.fd).
By using g_auto(TempFd) to store this result, callers will not even have
to care about closing a temporary FD after use. It will be done
automatically once the object goes out of scope.
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
---
tools/virtiofsd/passthrough_ll.c | 63 ++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
index b43afdfbd3..88318a4ba9 100644
--- a/tools/virtiofsd/passthrough_ll.c
+++ b/tools/virtiofsd/passthrough_ll.c
@@ -180,6 +180,28 @@ struct lo_data {
int user_posix_acl, posix_acl;
};
+/**
+ * Represents a file descriptor that may either be owned by this
+ * TempFd, or only referenced (i.e. the ownership belongs to some
+ * other object, and the value has just been copied into this TempFd).
+ *
+ * The purpose of this encapsulation is to be used as g_auto(TempFd)
+ * to automatically clean up owned file descriptors when this object
+ * goes out of scope.
+ *
+ * Use temp_fd_steal() to get an owned file descriptor that will not
+ * be closed when the TempFd goes out of scope.
+ */
+typedef struct {
+ int fd;
+ bool owned; /* fd owned by this object? */
+} TempFd;
+
+#define TEMP_FD_INIT ((TempFd) { .fd = -1, .owned = false })
+
+static void temp_fd_clear(TempFd *temp_fd);
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TempFd, temp_fd_clear);
+
static const struct fuse_opt lo_opts[] = {
{ "sandbox=namespace",
offsetof(struct lo_data, sandbox),
@@ -257,6 +279,47 @@ static struct lo_data *lo_data(fuse_req_t req)
return (struct lo_data *)fuse_req_userdata(req);
}
+/**
+ * Clean-up function for TempFds
+ */
+static void temp_fd_clear(TempFd *temp_fd)
+{
+ if (temp_fd->owned) {
+ close(temp_fd->fd);
+ *temp_fd = TEMP_FD_INIT;
+ }
+}
+
+/**
+ * Return an owned fd from *temp_fd that will not be closed when
+ * *temp_fd goes out of scope.
+ *
+ * (TODO: Remove __attribute__ once this is used.)
+ */
+static __attribute__((unused)) int temp_fd_steal(TempFd *temp_fd)
+{
+ if (temp_fd->owned) {
+ temp_fd->owned = false;
+ return temp_fd->fd;
+ } else {
+ return dup(temp_fd->fd);
+ }
+}
+
+/**
+ * Create a borrowing copy of an existing TempFd. Note that *to is
+ * only valid as long as *from is valid.
+ *
+ * (TODO: Remove __attribute__ once this is used.)
+ */
+static __attribute__((unused)) void temp_fd_copy(const TempFd *from, TempFd
*to)
+{
+ *to = (TempFd) {
+ .fd = from->fd,
+ .owned = false,
+ };
+}
+
/*
* Load capng's state from our saved state if the current thread
* hadn't previously been loaded.
--
2.31.1
- [PATCH v4 00/12] virtiofsd: Allow using file handles instead of O_PATH FDs, Hanna Reitz, 2021/09/16
- [PATCH v4 01/12] virtiofsd: Keep /proc/self/mountinfo open, Hanna Reitz, 2021/09/16
- [PATCH v4 02/12] virtiofsd: Limit setxattr()'s creds-dropped region, Hanna Reitz, 2021/09/16
- [PATCH v4 03/12] virtiofsd: Add TempFd structure,
Hanna Reitz <=
- [PATCH v4 04/12] virtiofsd: Use lo_inode_open() instead of openat(), Hanna Reitz, 2021/09/16
- [PATCH v4 05/12] virtiofsd: Add lo_inode_fd() helper, Hanna Reitz, 2021/09/16
- [PATCH v4 06/12] virtiofsd: Let lo_fd() return a TempFd, Hanna Reitz, 2021/09/16
- [PATCH v4 07/12] virtiofsd: Let lo_inode_open() return a TempFd, Hanna Reitz, 2021/09/16
- [PATCH v4 08/12] virtiofsd: Pass lo_data to lo_inode_{fd,open}(), Hanna Reitz, 2021/09/16
- [PATCH v4 09/12] virtiofsd: Add lo_inode.fhandle, Hanna Reitz, 2021/09/16
- [PATCH v4 10/12] virtiofsd: Add inodes_by_handle hash table, Hanna Reitz, 2021/09/16
- [PATCH v4 11/12] virtiofsd: Optionally fill lo_inode.fhandle, Hanna Reitz, 2021/09/16
- [PATCH v4 12/12] virtiofsd: Add lazy lo_do_find(), Hanna Reitz, 2021/09/16