cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
902636
From 8e46d0862c4c204f92c08ce2ae961921f270efb5 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:02:03 +0100
902636
Subject: [PATCH 092/116] virtiofsd: Support remote posix locks
902636
MIME-Version: 1.0
902636
Content-Type: text/plain; charset=UTF-8
902636
Content-Transfer-Encoding: 8bit
902636
902636
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Message-id: <20200127190227.40942-89-dgilbert@redhat.com>
902636
Patchwork-id: 93537
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 088/112] virtiofsd: Support remote posix locks
902636
Bugzilla: 1694164
902636
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
902636
902636
From: Vivek Goyal <vgoyal@redhat.com>
902636
902636
Doing posix locks with-in guest kernel are not sufficient if a file/dir
902636
is being shared by multiple guests. So we need the notion of daemon doing
902636
the locks which are visible to rest of the guests.
902636
902636
Given posix locks are per process, one can not call posix lock API on host,
902636
otherwise bunch of basic posix locks properties are broken. For example,
902636
If two processes (A and B) in guest open the file and take locks on different
902636
sections of file, if one of the processes closes the fd, it will close
902636
fd on virtiofsd and all posix locks on file will go away. This means if
902636
process A closes the fd, then locks of process B will go away too.
902636
902636
Similar other problems exist too.
902636
902636
This patch set tries to emulate posix locks while using open file
902636
description locks provided on Linux.
902636
902636
Daemon provides two options (-o posix_lock, -o no_posix_lock) to enable
902636
or disable posix locking in daemon. By default it is enabled.
902636
902636
There are few issues though.
902636
902636
- GETLK() returns pid of process holding lock. As we are emulating locks
902636
  using OFD, and these locks are not per process and don't return pid
902636
  of process, so GETLK() in guest does not reuturn process pid.
902636
902636
- As of now only F_SETLK is supported and not F_SETLKW. We can't block
902636
  the thread in virtiofsd for arbitrary long duration as there is only
902636
  one thread serving the queue. That means unlock request will not make
902636
  it to daemon and F_SETLKW will block infinitely and bring virtio-fs
902636
  to a halt. This is a solvable problem though and will require significant
902636
  changes in virtiofsd and kernel. Left as a TODO item for now.
902636
902636
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
902636
Reviewed-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit 0e81414c54161296212f6bc8a1c70526c4a9755a)
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/helper.c         |   3 +
902636
 tools/virtiofsd/passthrough_ll.c | 189 +++++++++++++++++++++++++++++++++++++++
902636
 2 files changed, 192 insertions(+)
902636
902636
diff --git a/tools/virtiofsd/helper.c b/tools/virtiofsd/helper.c
902636
index 5672024..33749bf 100644
902636
--- a/tools/virtiofsd/helper.c
902636
+++ b/tools/virtiofsd/helper.c
902636
@@ -156,6 +156,9 @@ void fuse_cmdline_help(void)
902636
            "                               allowed (default: 10)\n"
902636
            "    -o norace                  disable racy fallback\n"
902636
            "                               default: false\n"
902636
+           "    -o posix_lock|no_posix_lock\n"
902636
+           "                               enable/disable remote posix lock\n"
902636
+           "                               default: posix_lock\n"
902636
            "    -o readdirplus|no_readdirplus\n"
902636
            "                               enable/disable readirplus\n"
902636
            "                               default: readdirplus except with "
902636
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
902636
index 05b5f89..9414935 100644
902636
--- a/tools/virtiofsd/passthrough_ll.c
902636
+++ b/tools/virtiofsd/passthrough_ll.c
902636
@@ -67,6 +67,12 @@
902636
 #include "passthrough_helpers.h"
902636
 #include "seccomp.h"
902636
 
902636
+/* Keep track of inode posix locks for each owner. */
902636
+struct lo_inode_plock {
902636
+    uint64_t lock_owner;
902636
+    int fd; /* fd for OFD locks */
902636
+};
902636
+
902636
 struct lo_map_elem {
902636
     union {
902636
         struct lo_inode *inode;
902636
@@ -95,6 +101,8 @@ struct lo_inode {
902636
     struct lo_key key;
902636
     uint64_t refcount; /* protected by lo->mutex */
902636
     fuse_ino_t fuse_ino;
902636
+    pthread_mutex_t plock_mutex;
902636
+    GHashTable *posix_locks; /* protected by lo_inode->plock_mutex */
902636
 };
902636
 
902636
 struct lo_cred {
902636
@@ -114,6 +122,7 @@ struct lo_data {
902636
     int norace;
902636
     int writeback;
902636
     int flock;
902636
+    int posix_lock;
902636
     int xattr;
902636
     char *source;
902636
     double timeout;
902636
@@ -137,6 +146,8 @@ static const struct fuse_opt lo_opts[] = {
902636
     { "source=%s", offsetof(struct lo_data, source), 0 },
902636
     { "flock", offsetof(struct lo_data, flock), 1 },
902636
     { "no_flock", offsetof(struct lo_data, flock), 0 },
902636
+    { "posix_lock", offsetof(struct lo_data, posix_lock), 1 },
902636
+    { "no_posix_lock", offsetof(struct lo_data, posix_lock), 0 },
902636
     { "xattr", offsetof(struct lo_data, xattr), 1 },
902636
     { "no_xattr", offsetof(struct lo_data, xattr), 0 },
902636
     { "timeout=%lf", offsetof(struct lo_data, timeout), 0 },
902636
@@ -485,6 +496,17 @@ static void lo_init(void *userdata, struct fuse_conn_info *conn)
902636
         fuse_log(FUSE_LOG_DEBUG, "lo_init: activating flock locks\n");
902636
         conn->want |= FUSE_CAP_FLOCK_LOCKS;
902636
     }
902636
+
902636
+    if (conn->capable & FUSE_CAP_POSIX_LOCKS) {
902636
+        if (lo->posix_lock) {
902636
+            fuse_log(FUSE_LOG_DEBUG, "lo_init: activating posix locks\n");
902636
+            conn->want |= FUSE_CAP_POSIX_LOCKS;
902636
+        } else {
902636
+            fuse_log(FUSE_LOG_DEBUG, "lo_init: disabling posix locks\n");
902636
+            conn->want &= ~FUSE_CAP_POSIX_LOCKS;
902636
+        }
902636
+    }
902636
+
902636
     if ((lo->cache == CACHE_NONE && !lo->readdirplus_set) ||
902636
         lo->readdirplus_clear) {
902636
         fuse_log(FUSE_LOG_DEBUG, "lo_init: disabling readdirplus\n");
902636
@@ -772,6 +794,19 @@ static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st)
902636
     return p;
902636
 }
902636
 
902636
+/* value_destroy_func for posix_locks GHashTable */
902636
+static void posix_locks_value_destroy(gpointer data)
902636
+{
902636
+    struct lo_inode_plock *plock = data;
902636
+
902636
+    /*
902636
+     * We had used open() for locks and had only one fd. So
902636
+     * closing this fd should release all OFD locks.
902636
+     */
902636
+    close(plock->fd);
902636
+    free(plock);
902636
+}
902636
+
902636
 static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
902636
                         struct fuse_entry_param *e)
902636
 {
902636
@@ -825,6 +860,9 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
902636
         newfd = -1;
902636
         inode->key.ino = e->attr.st_ino;
902636
         inode->key.dev = e->attr.st_dev;
902636
+        pthread_mutex_init(&inode->plock_mutex, NULL);
902636
+        inode->posix_locks = g_hash_table_new_full(
902636
+            g_direct_hash, g_direct_equal, NULL, posix_locks_value_destroy);
902636
 
902636
         pthread_mutex_lock(&lo->mutex);
902636
         inode->fuse_ino = lo_add_inode_mapping(req, inode);
902636
@@ -1160,6 +1198,11 @@ static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
902636
     if (!inode->refcount) {
902636
         lo_map_remove(&lo->ino_map, inode->fuse_ino);
902636
         g_hash_table_remove(lo->inodes, &inode->key);
902636
+        if (g_hash_table_size(inode->posix_locks)) {
902636
+            fuse_log(FUSE_LOG_WARNING, "Hash table is not empty\n");
902636
+        }
902636
+        g_hash_table_destroy(inode->posix_locks);
902636
+        pthread_mutex_destroy(&inode->plock_mutex);
902636
         pthread_mutex_unlock(&lo->mutex);
902636
         close(inode->fd);
902636
         free(inode);
902636
@@ -1516,6 +1559,136 @@ out:
902636
     }
902636
 }
902636
 
902636
+/* Should be called with inode->plock_mutex held */
902636
+static struct lo_inode_plock *lookup_create_plock_ctx(struct lo_data *lo,
902636
+                                                      struct lo_inode *inode,
902636
+                                                      uint64_t lock_owner,
902636
+                                                      pid_t pid, int *err)
902636
+{
902636
+    struct lo_inode_plock *plock;
902636
+    char procname[64];
902636
+    int fd;
902636
+
902636
+    plock =
902636
+        g_hash_table_lookup(inode->posix_locks, GUINT_TO_POINTER(lock_owner));
902636
+
902636
+    if (plock) {
902636
+        return plock;
902636
+    }
902636
+
902636
+    plock = malloc(sizeof(struct lo_inode_plock));
902636
+    if (!plock) {
902636
+        *err = ENOMEM;
902636
+        return NULL;
902636
+    }
902636
+
902636
+    /* Open another instance of file which can be used for ofd locks. */
902636
+    sprintf(procname, "%i", inode->fd);
902636
+
902636
+    /* TODO: What if file is not writable? */
902636
+    fd = openat(lo->proc_self_fd, procname, O_RDWR);
902636
+    if (fd == -1) {
902636
+        *err = errno;
902636
+        free(plock);
902636
+        return NULL;
902636
+    }
902636
+
902636
+    plock->lock_owner = lock_owner;
902636
+    plock->fd = fd;
902636
+    g_hash_table_insert(inode->posix_locks, GUINT_TO_POINTER(plock->lock_owner),
902636
+                        plock);
902636
+    return plock;
902636
+}
902636
+
902636
+static void lo_getlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
902636
+                     struct flock *lock)
902636
+{
902636
+    struct lo_data *lo = lo_data(req);
902636
+    struct lo_inode *inode;
902636
+    struct lo_inode_plock *plock;
902636
+    int ret, saverr = 0;
902636
+
902636
+    fuse_log(FUSE_LOG_DEBUG,
902636
+             "lo_getlk(ino=%" PRIu64 ", flags=%d)"
902636
+             " owner=0x%lx, l_type=%d l_start=0x%lx"
902636
+             " l_len=0x%lx\n",
902636
+             ino, fi->flags, fi->lock_owner, lock->l_type, lock->l_start,
902636
+             lock->l_len);
902636
+
902636
+    inode = lo_inode(req, ino);
902636
+    if (!inode) {
902636
+        fuse_reply_err(req, EBADF);
902636
+        return;
902636
+    }
902636
+
902636
+    pthread_mutex_lock(&inode->plock_mutex);
902636
+    plock =
902636
+        lookup_create_plock_ctx(lo, inode, fi->lock_owner, lock->l_pid, &ret;;
902636
+    if (!plock) {
902636
+        pthread_mutex_unlock(&inode->plock_mutex);
902636
+        fuse_reply_err(req, ret);
902636
+        return;
902636
+    }
902636
+
902636
+    ret = fcntl(plock->fd, F_OFD_GETLK, lock);
902636
+    if (ret == -1) {
902636
+        saverr = errno;
902636
+    }
902636
+    pthread_mutex_unlock(&inode->plock_mutex);
902636
+
902636
+    if (saverr) {
902636
+        fuse_reply_err(req, saverr);
902636
+    } else {
902636
+        fuse_reply_lock(req, lock);
902636
+    }
902636
+}
902636
+
902636
+static void lo_setlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
902636
+                     struct flock *lock, int sleep)
902636
+{
902636
+    struct lo_data *lo = lo_data(req);
902636
+    struct lo_inode *inode;
902636
+    struct lo_inode_plock *plock;
902636
+    int ret, saverr = 0;
902636
+
902636
+    fuse_log(FUSE_LOG_DEBUG,
902636
+             "lo_setlk(ino=%" PRIu64 ", flags=%d)"
902636
+             " cmd=%d pid=%d owner=0x%lx sleep=%d l_whence=%d"
902636
+             " l_start=0x%lx l_len=0x%lx\n",
902636
+             ino, fi->flags, lock->l_type, lock->l_pid, fi->lock_owner, sleep,
902636
+             lock->l_whence, lock->l_start, lock->l_len);
902636
+
902636
+    if (sleep) {
902636
+        fuse_reply_err(req, EOPNOTSUPP);
902636
+        return;
902636
+    }
902636
+
902636
+    inode = lo_inode(req, ino);
902636
+    if (!inode) {
902636
+        fuse_reply_err(req, EBADF);
902636
+        return;
902636
+    }
902636
+
902636
+    pthread_mutex_lock(&inode->plock_mutex);
902636
+    plock =
902636
+        lookup_create_plock_ctx(lo, inode, fi->lock_owner, lock->l_pid, &ret;;
902636
+
902636
+    if (!plock) {
902636
+        pthread_mutex_unlock(&inode->plock_mutex);
902636
+        fuse_reply_err(req, ret);
902636
+        return;
902636
+    }
902636
+
902636
+    /* TODO: Is it alright to modify flock? */
902636
+    lock->l_pid = 0;
902636
+    ret = fcntl(plock->fd, F_OFD_SETLK, lock);
902636
+    if (ret == -1) {
902636
+        saverr = errno;
902636
+    }
902636
+    pthread_mutex_unlock(&inode->plock_mutex);
902636
+    fuse_reply_err(req, saverr);
902636
+}
902636
+
902636
 static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
902636
                         struct fuse_file_info *fi)
902636
 {
902636
@@ -1617,6 +1790,19 @@ static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
902636
 {
902636
     int res;
902636
     (void)ino;
902636
+    struct lo_inode *inode;
902636
+
902636
+    inode = lo_inode(req, ino);
902636
+    if (!inode) {
902636
+        fuse_reply_err(req, EBADF);
902636
+        return;
902636
+    }
902636
+
902636
+    /* An fd is going away. Cleanup associated posix locks */
902636
+    pthread_mutex_lock(&inode->plock_mutex);
902636
+    g_hash_table_remove(inode->posix_locks, GUINT_TO_POINTER(fi->lock_owner));
902636
+    pthread_mutex_unlock(&inode->plock_mutex);
902636
+
902636
     res = close(dup(lo_fi_fd(req, fi)));
902636
     fuse_reply_err(req, res == -1 ? errno : 0);
902636
 }
902636
@@ -2080,6 +2266,8 @@ static struct fuse_lowlevel_ops lo_oper = {
902636
     .releasedir = lo_releasedir,
902636
     .fsyncdir = lo_fsyncdir,
902636
     .create = lo_create,
902636
+    .getlk = lo_getlk,
902636
+    .setlk = lo_setlk,
902636
     .open = lo_open,
902636
     .release = lo_release,
902636
     .flush = lo_flush,
902636
@@ -2434,6 +2622,7 @@ int main(int argc, char *argv[])
902636
     struct lo_data lo = {
902636
         .debug = 0,
902636
         .writeback = 0,
902636
+        .posix_lock = 1,
902636
         .proc_self_fd = -1,
902636
     };
902636
     struct lo_map_elem *root_elem;
902636
-- 
902636
1.8.3.1
902636