yeahuh / rpms / qemu-kvm

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