From nobody Sun May 5 13:10:43 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1573851788; cv=none; d=zoho.com; s=zohoarc; b=ASNrnNTBXlnt5zOsfIueEHC8yGIrnOG3ehdKhE1cfLBhCktz05Fyo9lhXIHufuB8Fr2bhGhxhwMZ894VbFxT0aN0WHEqa3QqQ1zYkTJxubq8HkcLJ+sJ1UssM4nJz4Uj9QORkN3DYhCtcfxKeeAfkYEaZXKGI2lok+N9RtO/kjQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573851788; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=wZN4EnkBRSkAouXoTGaCavSrSXfYxYJcSqmAS3s7B90=; b=LgB39G1BQUSoSdG4EAKo7YqtNy8ofhsfsefeGqIHd/B3BSelkHr9MkAxYJ6y+MCtVOioZ22EkwLSBEmXok0r16ofcf39IZ7mtqOLobxXrenubHxfqgoONnwKS08QsN5CmDCc1oCCGXa1foplGa0swE7qys1ZYuqTLgzNWyJrCKc= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573851788365243.39919173626504; Fri, 15 Nov 2019 13:03:08 -0800 (PST) Received: from localhost ([::1]:45024 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVikC-0006HW-3d for importer@patchew.org; Fri, 15 Nov 2019 16:03:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46582) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVieU-0001Kj-FF for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVieS-00046j-68 for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:09 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:58031 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iVieR-00045E-Pq for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:08 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-138-rXrq4BX8MQGU2Y1ZLp6rGw-1; Fri, 15 Nov 2019 15:55:58 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 13212107ACC5; Fri, 15 Nov 2019 20:55:58 +0000 (UTC) Received: from horse.redhat.com (unknown [10.18.25.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5E7E65ED4E; Fri, 15 Nov 2019 20:55:51 +0000 (UTC) Received: by horse.redhat.com (Postfix, from userid 10451) id CAAA9224775; Fri, 15 Nov 2019 15:55:50 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573851427; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wZN4EnkBRSkAouXoTGaCavSrSXfYxYJcSqmAS3s7B90=; b=Lt2OlSfuo2UzXldNt7sdPOUAedUodjE42r5rAarKyjkRz+4M1OBD3AUqHqIas20J0Cjlms yuYOaw7/Z63kvLzH7dIW3mRu87XbnAQuZnGPFh7VmOxMxBCSyfNU9OrWtSteTFYz9+T7xR 6rjeyx/ynHOwW5KruZ+mYAPZM3MP2ho= From: Vivek Goyal To: virtio-fs@redhat.com, qemu-devel@nongnu.org Subject: [PATCH 1/4] virtiofsd: Release file locks using F_UNLCK Date: Fri, 15 Nov 2019 15:55:40 -0500 Message-Id: <20191115205543.1816-2-vgoyal@redhat.com> In-Reply-To: <20191115205543.1816-1-vgoyal@redhat.com> References: <20191115205543.1816-1-vgoyal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: rXrq4BX8MQGU2Y1ZLp6rGw-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: miklos@szeredi.hu, stefanha@redhat.com, vgoyal@redhat.com, dgilbert@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" We are emulating posix locks for guest using open file description locks in virtiofsd. When any of the fd is closed in guest, we find associated OFD lock fd (if there is one) and close it to release all the locks. Assumption here is that there is no other thread using lo_inode_plock structure or plock->fd, hence it is safe to do so. But now we are about to introduce blocking variant of locks (SETLKW), and that means we might be waiting to a lock to be available and using plock->fd. And that means there are still users of plock structure. So release locks using fcntl(SETLK, F_UNLCK) instead and plock will be freed later. Signed-off-by: Vivek Goyal --- contrib/virtiofsd/passthrough_ll.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/contrib/virtiofsd/passthrough_ll.c b/contrib/virtiofsd/passthr= ough_ll.c index bc214df0c7..028e7da273 100644 --- a/contrib/virtiofsd/passthrough_ll.c +++ b/contrib/virtiofsd/passthrough_ll.c @@ -936,6 +936,14 @@ static void put_shared(struct lo_data *lo, struct lo_i= node *inode) } } =20 +static void release_plock(gpointer data) +{ + struct lo_inode_plock *plock =3D data; + + close(plock->fd); + free(plock); +} + /* Increments nlookup and caller must release refcount using * lo_inode_put(&parent). */ @@ -994,7 +1002,8 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t par= ent, const char *name, inode->key.ino =3D e->attr.st_ino; inode->key.dev =3D e->attr.st_dev; pthread_mutex_init(&inode->plock_mutex, NULL); - inode->posix_locks =3D g_hash_table_new(g_direct_hash, g_direct_equal); + inode->posix_locks =3D g_hash_table_new_full(g_direct_hash, + g_direct_equal, NULL, release_plock); =20 get_shared(lo, inode); =20 @@ -1436,9 +1445,6 @@ static void unref_inode(struct lo_data *lo, struct lo= _inode *inode, uint64_t n) if (!inode->nlookup) { lo_map_remove(&lo->ino_map, inode->fuse_ino); g_hash_table_remove(lo->inodes, &inode->key); - if (g_hash_table_size(inode->posix_locks)) { - fuse_log(FUSE_LOG_WARNING, "Hash table is not empty\n"); - } g_hash_table_destroy(inode->posix_locks); pthread_mutex_destroy(&inode->plock_mutex); =20 @@ -1868,6 +1874,7 @@ static struct lo_inode_plock *lookup_create_plock_ctx= (struct lo_data *lo, plock->fd =3D fd; g_hash_table_insert(inode->posix_locks, GUINT_TO_POINTER(plock->lock_owner), plock); + fuse_log(FUSE_LOG_DEBUG, "lookup_create_plock_ctx(): Inserted element in = posix_locks hash table with value pointer %p\n", plock); return plock; } =20 @@ -2046,6 +2053,7 @@ static void lo_flush(fuse_req_t req, fuse_ino_t ino, = struct fuse_file_info *fi) (void) ino; struct lo_inode *inode; struct lo_inode_plock *plock; + struct flock flock; =20 inode =3D lo_inode(req, ino); if (!inode) { @@ -2058,14 +2066,16 @@ static void lo_flush(fuse_req_t req, fuse_ino_t ino= , struct fuse_file_info *fi) plock =3D g_hash_table_lookup(inode->posix_locks, GUINT_TO_POINTER(fi->lock_owner)); if (plock) { - g_hash_table_remove(inode->posix_locks, - GUINT_TO_POINTER(fi->lock_owner)); /* - * We had used open() for locks and had only one fd. So - * closing this fd should release all OFD locks. + * An fd is being closed. For posix locks, this means + * drop all the associated locks. */ - close(plock->fd); - free(plock); + memset(&flock, 0, sizeof(struct flock)); + flock.l_type =3D F_UNLCK; + flock.l_whence =3D SEEK_SET; + /* Unlock whole file */ + flock.l_start =3D flock.l_len =3D 0; + fcntl(plock->fd, F_SETLK, &flock); } pthread_mutex_unlock(&inode->plock_mutex); =20 --=20 2.20.1 From nobody Sun May 5 13:10:43 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1573851788; cv=none; d=zoho.com; s=zohoarc; b=kMIROskulU9U35A9SEWjiWUfYZYKBFrNj8Mda7xzlnOhxbTOReV7jBdcbXQpOVee/ouT+bsXaR0ZrCiB4oim+/5w9r38eRYcFpCywfTSkoG8uZf+FqmgfHncMRV/pQW/3i3A3fWj1HQ5FNGj07Ex5E/2oTZbcCJ9dvJNOQVaFWI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573851788; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=8iW3nEIhTVvWbmaaRkvzhAfT538DAFhkr63/3zL9luE=; b=oeT9MlaUWCDYXCLfS1cA5+i7dAMdGA7k+JPxKvnS5Dndctx53z44TcNkZLuLM1FpaiGBcb+sP2z2imERINkMs2A5lEKQ77W+n4vnPLjj1KxJDg/BauGmSh5RlJpGk85JXIc7YOXSoH5H0eLE9k/mUNaraCHoGDEUvZx7up8XIZk= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573851788400435.7596548494407; Fri, 15 Nov 2019 13:03:08 -0800 (PST) Received: from localhost ([::1]:45026 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVikD-0006I1-60 for importer@patchew.org; Fri, 15 Nov 2019 16:03:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46589) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVieU-0001L7-FP for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVieS-00046T-08 for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:09 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:56907 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iVieR-000445-MI for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:07 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-228-_lQGP3IAMlu-FHza_tJHWw-1; Fri, 15 Nov 2019 15:55:58 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6BBAB1005510; Fri, 15 Nov 2019 20:55:57 +0000 (UTC) Received: from horse.redhat.com (unknown [10.18.25.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5E9226918E; Fri, 15 Nov 2019 20:55:51 +0000 (UTC) Received: by horse.redhat.com (Postfix, from userid 10451) id CF51E224776; Fri, 15 Nov 2019 15:55:50 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573851427; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8iW3nEIhTVvWbmaaRkvzhAfT538DAFhkr63/3zL9luE=; b=iQg7FrImMWYkp6jqEJMLWIMh9Ba883ZbOQCSjrxR9YkbqpDGYzZusUCkV7hOW4r0E83RGx 8NjVqwgF8/BU5KVMo8FnPCs2KmFzm6skTkq9+TBNYjBj5fCG63P6VbneIbKtyvsk+TOHWl HzGh5GskdRTvmO52QKqHkTt89RLSY5M= From: Vivek Goyal To: virtio-fs@redhat.com, qemu-devel@nongnu.org Subject: [PATCH 2/4] virtiofd: Create a notification queue Date: Fri, 15 Nov 2019 15:55:41 -0500 Message-Id: <20191115205543.1816-3-vgoyal@redhat.com> In-Reply-To: <20191115205543.1816-1-vgoyal@redhat.com> References: <20191115205543.1816-1-vgoyal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: _lQGP3IAMlu-FHza_tJHWw-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: miklos@szeredi.hu, stefanha@redhat.com, vgoyal@redhat.com, dgilbert@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Add a notification queue which will be used to send async notifications for file lock availability. Signed-off-by: Vivek Goyal --- contrib/virtiofsd/fuse_i.h | 1 + contrib/virtiofsd/fuse_virtio.c | 108 ++++++++++++++++++--- hw/virtio/vhost-user-fs-pci.c | 2 +- hw/virtio/vhost-user-fs.c | 37 +++++-- include/hw/virtio/vhost-user-fs.h | 1 + include/standard-headers/linux/virtio_fs.h | 3 + 6 files changed, 130 insertions(+), 22 deletions(-) diff --git a/contrib/virtiofsd/fuse_i.h b/contrib/virtiofsd/fuse_i.h index 966b1a3baa..4eeae0bfeb 100644 --- a/contrib/virtiofsd/fuse_i.h +++ b/contrib/virtiofsd/fuse_i.h @@ -74,6 +74,7 @@ struct fuse_session { char *vu_socket_lock; struct fv_VuDev *virtio_dev; int thread_pool_size; + bool notify_enabled; }; =20 struct fuse_chan { diff --git a/contrib/virtiofsd/fuse_virtio.c b/contrib/virtiofsd/fuse_virti= o.c index 31c8542b6c..411114c9b3 100644 --- a/contrib/virtiofsd/fuse_virtio.c +++ b/contrib/virtiofsd/fuse_virtio.c @@ -14,6 +14,7 @@ #include "qemu/osdep.h" #include "qemu/iov.h" #include "qapi/error.h" +#include "standard-headers/linux/virtio_fs.h" #include "fuse_i.h" #include "fuse_kernel.h" #include "fuse_misc.h" @@ -98,23 +99,31 @@ struct fv_VuDev { */ size_t nqueues; struct fv_QueueInfo **qi; -}; - -/* From spec */ -struct virtio_fs_config { - char tag[36]; - uint32_t num_queues; + /* True if notification queue is being used */ + bool notify_enabled; }; =20 /* Callback from libvhost-user */ static uint64_t fv_get_features(VuDev *dev) { - return 1ULL << VIRTIO_F_VERSION_1; + uint64_t features; + + features =3D 1ull << VIRTIO_F_VERSION_1 | + 1ull << VIRTIO_FS_F_NOTIFICATION; + + return features; } =20 /* Callback from libvhost-user */ static void fv_set_features(VuDev *dev, uint64_t features) { + struct fv_VuDev *vud =3D container_of(dev, struct fv_VuDev, dev); + struct fuse_session *se =3D vud->se; + + if ((1 << VIRTIO_FS_F_NOTIFICATION) & features) { + vud->notify_enabled =3D true; + se->notify_enabled =3D true; + } } =20 /* @@ -662,6 +671,65 @@ static void fv_queue_worker(gpointer data, gpointer us= er_data) free(req); } =20 +static void *fv_queue_notify_thread(void *opaque) +{ + struct fv_QueueInfo *qi =3D opaque; + + fuse_log(FUSE_LOG_INFO, "%s: Start for queue %d kick_fd %d\n", __func_= _, + qi->qidx, qi->kick_fd); + + while (1) { + struct pollfd pf[2]; + + pf[0].fd =3D qi->kick_fd; + pf[0].events =3D POLLIN; + pf[0].revents =3D 0; + pf[1].fd =3D qi->kill_fd; + pf[1].events =3D POLLIN; + pf[1].revents =3D 0; + + fuse_log(FUSE_LOG_DEBUG, "%s: Waiting for Queue %d event\n", __fun= c__, + qi->qidx); + int poll_res =3D ppoll(pf, 2, NULL, NULL); + + if (poll_res =3D=3D -1) { + if (errno =3D=3D EINTR) { + fuse_log(FUSE_LOG_INFO, "%s: ppoll interrupted, going arou= nd\n", + __func__); + continue; + } + fuse_log(FUSE_LOG_ERR, "fv_queue_thread ppoll: %m\n"); + break; + } + assert(poll_res >=3D 1); + if (pf[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { + fuse_log(FUSE_LOG_ERR, "%s: Unexpected poll revents %x Queue %= d\n", + __func__, pf[0].revents, qi->qidx); + break; + } + if (pf[1].revents & (POLLERR | POLLHUP | POLLNVAL)) { + fuse_log(FUSE_LOG_ERR, "%s: Unexpected poll revents %x Queue %= d" + "killfd\n", __func__, pf[1].revents, qi->qidx); + break; + } + if (pf[1].revents) { + fuse_log(FUSE_LOG_INFO, "%s: kill event on queue %d - quitting= \n", + __func__, qi->qidx); + break; + } + assert(pf[0].revents & POLLIN); + fuse_log(FUSE_LOG_DEBUG, "%s: Got queue event on Queue %d\n", __fu= nc__, + qi->qidx); + + eventfd_t evalue; + if (eventfd_read(qi->kick_fd, &evalue)) { + fuse_log(FUSE_LOG_ERR, "Eventfd_read for queue: %m\n"); + break; + } + } + return NULL; +} + /* Thread function for individual queues, created when a queue is 'started= ' */ static void *fv_queue_thread(void *opaque) { @@ -771,6 +839,8 @@ static void fv_queue_set_started(VuDev *dev, int qidx, = bool started) { struct fv_VuDev *vud =3D container_of(dev, struct fv_VuDev, dev); struct fv_QueueInfo *ourqi; + void * (*thread_func) (void *) =3D fv_queue_thread; + int valid_queues =3D 2; /* One hiprio queue and one request queue */ =20 fuse_log(FUSE_LOG_INFO, "%s: qidx=3D%d started=3D%d\n", __func__, qidx, started); @@ -782,10 +852,12 @@ static void fv_queue_set_started(VuDev *dev, int qidx= , bool started) * well-behaved client in mind and may not protect against all types of * races yet. */ - if (qidx > 1) { - fuse_log(FUSE_LOG_ERR, - "%s: multiple request queues not yet implemented, please = only " - "configure 1 request queue\n", + if (vud->notify_enabled) + valid_queues++; + + if (qidx >=3D valid_queues) { + fuse_log(FUSE_LOG_ERR, "%s: multiple request queues not yet" + "implemented, please only configure 1 request queue\n", __func__); exit(EXIT_FAILURE); } @@ -813,9 +885,17 @@ static void fv_queue_set_started(VuDev *dev, int qidx,= bool started) =20 ourqi->kill_fd =3D eventfd(0, EFD_CLOEXEC | EFD_SEMAPHORE); assert(ourqi->kill_fd !=3D -1); - pthread_mutex_init(&ourqi->vq_lock, NULL); + /* + * First queue (idx =3D 0) is hiprio queue. Second queue is + * notification queue (if enabled). And rest are request + * queues. + */ + if (vud->notify_enabled && qidx =3D=3D 1) { + thread_func =3D fv_queue_notify_thread; + } =20 - if (pthread_create(&ourqi->thread, NULL, fv_queue_thread, ourqi)) { + pthread_mutex_init(&ourqi->vq_lock, NULL); + if (pthread_create(&ourqi->thread, NULL, thread_func, ourqi)) { fuse_log(FUSE_LOG_ERR, "%s: Failed to create thread for queue = %d\n", __func__, qidx); assert(0); @@ -1040,7 +1120,7 @@ int virtio_session_mount(struct fuse_session *se) se->virtio_dev =3D calloc(sizeof(struct fv_VuDev), 1); se->virtio_dev->se =3D se; pthread_rwlock_init(&se->virtio_dev->vu_dispatch_rwlock, NULL); - vu_init(&se->virtio_dev->dev, 2, se->vu_socketfd, fv_panic, fv_set_wat= ch, + vu_init(&se->virtio_dev->dev, 3, se->vu_socketfd, fv_panic, fv_set_wat= ch, fv_remove_watch, &fv_iface); =20 return 0; diff --git a/hw/virtio/vhost-user-fs-pci.c b/hw/virtio/vhost-user-fs-pci.c index 0f3c3c8711..95f9fe5c5c 100644 --- a/hw/virtio/vhost-user-fs-pci.c +++ b/hw/virtio/vhost-user-fs-pci.c @@ -44,7 +44,7 @@ static void vhost_user_fs_pci_realize(VirtIOPCIProxy *vpc= i_dev, Error **errp) uint64_t totalsize; =20 if (vpci_dev->nvectors =3D=3D DEV_NVECTORS_UNSPECIFIED) { - vpci_dev->nvectors =3D dev->vdev.conf.num_request_queues + 1; + vpci_dev->nvectors =3D dev->vdev.conf.num_request_queues + 2; } =20 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); diff --git a/hw/virtio/vhost-user-fs.c b/hw/virtio/vhost-user-fs.c index 455e97beea..5555fe9dbe 100644 --- a/hw/virtio/vhost-user-fs.c +++ b/hw/virtio/vhost-user-fs.c @@ -24,6 +24,10 @@ #include "exec/address-spaces.h" #include "trace.h" =20 +static const int user_feature_bits[] =3D { + VIRTIO_FS_F_NOTIFICATION, +}; + uint64_t vhost_user_fs_slave_map(struct vhost_dev *dev, VhostUserFSSlaveMs= g *sm, int fd) { @@ -378,12 +382,23 @@ static void vuf_set_status(VirtIODevice *vdev, uint8_= t status) } } =20 -static uint64_t vuf_get_features(VirtIODevice *vdev, - uint64_t requested_features, - Error **errp) +static uint64_t vuf_get_features(VirtIODevice *vdev, uint64_t features, + Error **errp) { - /* No feature bits used yet */ - return requested_features; + VHostUserFS *fs =3D VHOST_USER_FS(vdev); + + virtio_add_feature(&features, VIRTIO_FS_F_NOTIFICATION); + + return vhost_get_features(&fs->vhost_dev, user_feature_bits, features); +} + +static void vuf_set_features(VirtIODevice *vdev, uint64_t features) +{ + VHostUserFS *fs =3D VHOST_USER_FS(vdev); + + if (virtio_has_feature(features, VIRTIO_FS_F_NOTIFICATION)) { + fs->notify_enabled =3D true; + } } =20 static void vuf_handle_output(VirtIODevice *vdev, VirtQueue *vq) @@ -515,13 +530,20 @@ static void vuf_device_realize(DeviceState *dev, Erro= r **errp) /* Hiprio queue */ virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output); =20 + /* Notification queue. Feature negotiation happens later. So at this + * point of time we don't know if driver will use notification queue + * or not. + */ + virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output); + /* Request queues */ for (i =3D 0; i < fs->conf.num_request_queues; i++) { virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output); } =20 - /* 1 high prio queue, plus the number configured */ - fs->vhost_dev.nvqs =3D 1 + fs->conf.num_request_queues; + /* 1 high prio queue, 1 notification queue plus the number configured = */ + fs->vhost_dev.nvqs =3D 2 + fs->conf.num_request_queues; + fs->vhost_dev.vqs =3D g_new0(struct vhost_virtqueue, fs->vhost_dev.nvq= s); ret =3D vhost_dev_init(&fs->vhost_dev, &fs->vhost_user, VHOST_BACKEND_TYPE_USER, 0); @@ -584,6 +606,7 @@ static void vuf_class_init(ObjectClass *klass, void *da= ta) vdc->realize =3D vuf_device_realize; vdc->unrealize =3D vuf_device_unrealize; vdc->get_features =3D vuf_get_features; + vdc->set_features =3D vuf_set_features; vdc->get_config =3D vuf_get_config; vdc->set_status =3D vuf_set_status; vdc->guest_notifier_mask =3D vuf_guest_notifier_mask; diff --git a/include/hw/virtio/vhost-user-fs.h b/include/hw/virtio/vhost-us= er-fs.h index 4e7be1f312..bd47e0da98 100644 --- a/include/hw/virtio/vhost-user-fs.h +++ b/include/hw/virtio/vhost-user-fs.h @@ -64,6 +64,7 @@ typedef struct { /* Metadata version table */ size_t mdvt_size; MemoryRegion mdvt; + bool notify_enabled; } VHostUserFS; =20 /* Callbacks from the vhost-user code for slave commands */ diff --git a/include/standard-headers/linux/virtio_fs.h b/include/standard-= headers/linux/virtio_fs.h index 310210b7b6..9ee95f584f 100644 --- a/include/standard-headers/linux/virtio_fs.h +++ b/include/standard-headers/linux/virtio_fs.h @@ -8,6 +8,9 @@ #include "standard-headers/linux/virtio_config.h" #include "standard-headers/linux/virtio_types.h" =20 +/* Feature bits */ +#define VIRTIO_FS_F_NOTIFICATION 0 /* Notification queue supported */ + struct virtio_fs_config { /* Filesystem name (UTF-8, not NUL-terminated, padded with NULs) */ uint8_t tag[36]; --=20 2.20.1 From nobody Sun May 5 13:10:43 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1573851545; cv=none; d=zoho.com; s=zohoarc; b=ChpOfEWiMEbD5ZgLWiCsfTFSXlI0Lisvh8ugKvkkkuzE2fKJYCZWu0Q4y4hK7/Mn4dmuQBaxRutt8Fa9/OtWXiCshBDNp6YRFq0I6N+eWIy9uRlibSgP4o7d5BB2W0km1C71QvbMIsUjAbz/0tB74kLS/LIXJj8HUqy5nVkjtJ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573851545; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mrGnB1iv8Csi0czBGKotd+GZIBwERQbtlUN8heaoB48=; b=M3TtqfGWIBI+ARo/NNF6MSEgqQmiI/gJ+jSROp2rJyKZiB/E67irkETMIWOtFsKf+WeO/jevEUs6CCVGQnomlCLLDe+82wVTq7p/i5GutwxAy/823odP7nAS8TEy4P5Ng3qFMA30B4VdekQh9+oKWDzqWM+dRivkXQhj0gmBpOU= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 157385154508941.34580999523337; Fri, 15 Nov 2019 12:59:05 -0800 (PST) Received: from localhost ([::1]:44978 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVigF-00034Q-TI for importer@patchew.org; Fri, 15 Nov 2019 15:58:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46585) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVieU-0001Km-FO for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVieS-00046d-34 for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:09 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:58178 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iVieR-00045r-VW for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:08 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-228-8oBQMcV6NAaiOs4x7NUieg-1; Fri, 15 Nov 2019 15:55:58 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7FE92107ACC4; Fri, 15 Nov 2019 20:55:57 +0000 (UTC) Received: from horse.redhat.com (unknown [10.18.25.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5E1DC5ED35; Fri, 15 Nov 2019 20:55:51 +0000 (UTC) Received: by horse.redhat.com (Postfix, from userid 10451) id D5E6C224777; Fri, 15 Nov 2019 15:55:50 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573851427; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mrGnB1iv8Csi0czBGKotd+GZIBwERQbtlUN8heaoB48=; b=XaBMW2gqTjkUGM/1i+ui/TDsz94t9b2H1QpVIyK8kyrGT4BhHOpG/uXNLCn4ANLgzRqum1 TjKXAIJ1ElWdrT4ERRe7Vj4VXOlZmyb00uFOxcMydUMIEwAzwlMbUnqrZXImPi1wZjXAzD CUAffCm9eDh/h0E+02eQvs30pcCubMo= From: Vivek Goyal To: virtio-fs@redhat.com, qemu-devel@nongnu.org Subject: [PATCH 3/4] virtiofsd: Specify size of notification buffer using config space Date: Fri, 15 Nov 2019 15:55:42 -0500 Message-Id: <20191115205543.1816-4-vgoyal@redhat.com> In-Reply-To: <20191115205543.1816-1-vgoyal@redhat.com> References: <20191115205543.1816-1-vgoyal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: 8oBQMcV6NAaiOs4x7NUieg-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: miklos@szeredi.hu, stefanha@redhat.com, vgoyal@redhat.com, dgilbert@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Daemon specifies size of notification buffer needed and that should be done using config space. Only ->notify_buf_size value of config space comes from daemon. Rest of it is filled by qemu device emulation code. Signed-off-by: Vivek Goyal --- contrib/virtiofsd/fuse_virtio.c | 26 +++++++++++++++++++++- hw/virtio/vhost-user-fs.c | 26 ++++++++++++++++++++++ include/hw/virtio/vhost-user-fs.h | 2 ++ include/standard-headers/linux/virtio_fs.h | 2 ++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/contrib/virtiofsd/fuse_virtio.c b/contrib/virtiofsd/fuse_virti= o.c index 411114c9b3..982b6ad0bd 100644 --- a/contrib/virtiofsd/fuse_virtio.c +++ b/contrib/virtiofsd/fuse_virtio.c @@ -109,7 +109,8 @@ static uint64_t fv_get_features(VuDev *dev) uint64_t features; =20 features =3D 1ull << VIRTIO_F_VERSION_1 | - 1ull << VIRTIO_FS_F_NOTIFICATION; + 1ull << VIRTIO_FS_F_NOTIFICATION | + 1ull << VHOST_USER_F_PROTOCOL_FEATURES; =20 return features; } @@ -927,6 +928,27 @@ static bool fv_queue_order(VuDev *dev, int qidx) return false; } =20 +static uint64_t fv_get_protocol_features(VuDev *dev) +{ + return 1ull << VHOST_USER_PROTOCOL_F_CONFIG; +} + +static int fv_get_config(VuDev *dev, uint8_t *config, uint32_t len) +{ + struct virtio_fs_config fscfg =3D {}; + + fuse_log(FUSE_LOG_DEBUG, "%s:Setting notify_buf_size=3D%d\n", __func__, + sizeof(struct fuse_notify_lock_out)); + /* + * As of now only notification related to lock is supported. As more + * notification types are supported, bump up the size accordingly. + */ + fscfg.notify_buf_size =3D sizeof(struct fuse_notify_lock_out); + + memcpy(config, &fscfg, len); + return 0; +} + static const VuDevIface fv_iface =3D { .get_features =3D fv_get_features, .set_features =3D fv_set_features, @@ -935,6 +957,8 @@ static const VuDevIface fv_iface =3D { .queue_set_started =3D fv_queue_set_started, =20 .queue_is_processed_in_order =3D fv_queue_order, + .get_protocol_features =3D fv_get_protocol_features, + .get_config =3D fv_get_config, }; =20 /* diff --git a/hw/virtio/vhost-user-fs.c b/hw/virtio/vhost-user-fs.c index 5555fe9dbe..8dd9b1496f 100644 --- a/hw/virtio/vhost-user-fs.c +++ b/hw/virtio/vhost-user-fs.c @@ -277,16 +277,40 @@ uint64_t vhost_user_fs_slave_io(struct vhost_dev *dev= , VhostUserFSSlaveMsg *sm, return (uint64_t)done; } =20 +static int vhost_user_fs_handle_config_change(struct vhost_dev *dev) +{ + return 0; +} + +const VhostDevConfigOps fs_ops =3D { + .vhost_dev_config_notifier =3D vhost_user_fs_handle_config_change, +}; =20 static void vuf_get_config(VirtIODevice *vdev, uint8_t *config) { VHostUserFS *fs =3D VHOST_USER_FS(vdev); struct virtio_fs_config fscfg =3D {}; + int ret; + + /* + * As of now we only get notification buffer size from device. And tha= t's + * needed only if notification queue is enabled. + */ + if (fs->notify_enabled) { + ret =3D vhost_dev_get_config(&fs->vhost_dev, (uint8_t *)&fs->fscfg, + sizeof(struct virtio_fs_config)); + if (ret < 0) { + error_report("vhost-user-fs: get device config space failed." + " ret=3D%d\n", ret); + return; + } + } =20 memcpy((char *)fscfg.tag, fs->conf.tag, MIN(strlen(fs->conf.tag) + 1, sizeof(fscfg.tag))); =20 virtio_stl_p(vdev, &fscfg.num_request_queues, fs->conf.num_request_que= ues); + virtio_stl_p(vdev, &fscfg.notify_buf_size, fs->fscfg.notify_buf_size); =20 memcpy(config, &fscfg, sizeof(fscfg)); } @@ -545,6 +569,8 @@ static void vuf_device_realize(DeviceState *dev, Error = **errp) fs->vhost_dev.nvqs =3D 2 + fs->conf.num_request_queues; =20 fs->vhost_dev.vqs =3D g_new0(struct vhost_virtqueue, fs->vhost_dev.nvq= s); + + vhost_dev_set_config_notifier(&fs->vhost_dev, &fs_ops); ret =3D vhost_dev_init(&fs->vhost_dev, &fs->vhost_user, VHOST_BACKEND_TYPE_USER, 0); if (ret < 0) { diff --git a/include/hw/virtio/vhost-user-fs.h b/include/hw/virtio/vhost-us= er-fs.h index bd47e0da98..f667cc4b5a 100644 --- a/include/hw/virtio/vhost-user-fs.h +++ b/include/hw/virtio/vhost-user-fs.h @@ -14,6 +14,7 @@ #ifndef _QEMU_VHOST_USER_FS_H #define _QEMU_VHOST_USER_FS_H =20 +#include "standard-headers/linux/virtio_fs.h" #include "hw/virtio/virtio.h" #include "hw/virtio/vhost.h" #include "hw/virtio/vhost-user.h" @@ -58,6 +59,7 @@ typedef struct { struct vhost_virtqueue *vhost_vqs; struct vhost_dev vhost_dev; VhostUserState vhost_user; + struct virtio_fs_config fscfg; =20 /*< public >*/ MemoryRegion cache; diff --git a/include/standard-headers/linux/virtio_fs.h b/include/standard-= headers/linux/virtio_fs.h index 9ee95f584f..719216a262 100644 --- a/include/standard-headers/linux/virtio_fs.h +++ b/include/standard-headers/linux/virtio_fs.h @@ -17,6 +17,8 @@ struct virtio_fs_config { =20 /* Number of request queues */ uint32_t num_request_queues; + /* Size of notification buffer */ + uint32_t notify_buf_size; } QEMU_PACKED; =20 #define VIRTIO_FS_PCI_CACHE_BAR 2 --=20 2.20.1 From nobody Sun May 5 13:10:43 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1573851544; cv=none; d=zoho.com; s=zohoarc; b=Zrqxk5uVqoih+GHRKYJZI3OIJbwCUEUzcZoEmhO7DboiEq/IFD2xTDZ8lpEIKR8kru4nV2kBg3Co/a/1GKixVcHJ9jX0LpC65qzPUugFQ7TSdGu+t+/xU+XiHam5XU4sj6nj+R0E//vkhPJsZNI7ZuIj0W1Z9W5ilSMliR2PuRs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573851544; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FpCwPrwFNpEfj3R1MxNuQltxgfFE7nKTvRQ0qQrBjoE=; b=gBQnhTHQNsyKl8PUj3IPzvH5Z3eItVwEQijSfwvqe/NdB1GbOkTspRSjIsaPedOKSX4FGB8oXrIKJxSlKsIy3aqJ/cHlRIUeLHUWhuMG/BkEV02Z33QCeXMrt6kbc7KXxCoXhwd0Xy8C6h/JDyrp+MZEFHxfmlD1GDtXQGiJIrg= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15738515449051019.5995655182136; Fri, 15 Nov 2019 12:59:04 -0800 (PST) Received: from localhost ([::1]:44982 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVigI-00036S-PA for importer@patchew.org; Fri, 15 Nov 2019 15:59:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46588) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVieU-0001L3-JD for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVieR-00046H-Tz for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:09 -0500 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:55966 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iVieR-00044t-PY for qemu-devel@nongnu.org; Fri, 15 Nov 2019 15:57:07 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-355--619w4k7MBStYWIBTvG-BA-1; Fri, 15 Nov 2019 15:55:58 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6D48D1005511; Fri, 15 Nov 2019 20:55:57 +0000 (UTC) Received: from horse.redhat.com (unknown [10.18.25.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5F23310375C0; Fri, 15 Nov 2019 20:55:51 +0000 (UTC) Received: by horse.redhat.com (Postfix, from userid 10451) id DB2A7224778; Fri, 15 Nov 2019 15:55:50 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573851427; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FpCwPrwFNpEfj3R1MxNuQltxgfFE7nKTvRQ0qQrBjoE=; b=JscRJPUi1KrwoI2lawOEEfYnaYMd2QlECCQ/1/i8l9I97BCMHwjUJ4h51fCmHrMneuTMwg IuLxZxfMvRRNdBglIKVjOyKzHvDlj7ZKre860I2dclDyi45h9FXmtv62kpfKiJgmj2DCQo SOrYXLxdckSr7Z/HErPGVBjrHiwLWmk= From: Vivek Goyal To: virtio-fs@redhat.com, qemu-devel@nongnu.org Subject: [PATCH 4/4] virtiofsd: Implement blocking posix locks Date: Fri, 15 Nov 2019 15:55:43 -0500 Message-Id: <20191115205543.1816-5-vgoyal@redhat.com> In-Reply-To: <20191115205543.1816-1-vgoyal@redhat.com> References: <20191115205543.1816-1-vgoyal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-MC-Unique: -619w4k7MBStYWIBTvG-BA-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: miklos@szeredi.hu, stefanha@redhat.com, vgoyal@redhat.com, dgilbert@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" As of now we don't support fcntl(F_SETLKW) and if we see one, we return -EOPNOTSUPP. Change that by accepting these requests and returning a reply immediately asking caller to wait. Once lock is available, send a notification to the waiter indicating lock is available. Signed-off-by: Vivek Goyal --- contrib/virtiofsd/fuse_kernel.h | 7 +++ contrib/virtiofsd/fuse_lowlevel.c | 23 +++++++- contrib/virtiofsd/fuse_lowlevel.h | 25 ++++++++ contrib/virtiofsd/fuse_virtio.c | 94 ++++++++++++++++++++++++++++-- contrib/virtiofsd/passthrough_ll.c | 49 +++++++++++++--- 5 files changed, 182 insertions(+), 16 deletions(-) diff --git a/contrib/virtiofsd/fuse_kernel.h b/contrib/virtiofsd/fuse_kerne= l.h index 2bdc8b1c88..d4d65c5414 100644 --- a/contrib/virtiofsd/fuse_kernel.h +++ b/contrib/virtiofsd/fuse_kernel.h @@ -444,6 +444,7 @@ enum fuse_notify_code { FUSE_NOTIFY_STORE =3D 4, FUSE_NOTIFY_RETRIEVE =3D 5, FUSE_NOTIFY_DELETE =3D 6, + FUSE_NOTIFY_LOCK =3D 7, FUSE_NOTIFY_CODE_MAX, }; =20 @@ -836,6 +837,12 @@ struct fuse_notify_retrieve_in { uint64_t dummy4; }; =20 +struct fuse_notify_lock_out { + uint64_t id; + int32_t error; + int32_t padding; +}; + /* Device ioctls: */ #define FUSE_DEV_IOC_CLONE _IOR(229, 0, uint32_t) =20 diff --git a/contrib/virtiofsd/fuse_lowlevel.c b/contrib/virtiofsd/fuse_low= level.c index d4a42d9804..f706e440bf 100644 --- a/contrib/virtiofsd/fuse_lowlevel.c +++ b/contrib/virtiofsd/fuse_lowlevel.c @@ -183,7 +183,8 @@ int fuse_send_reply_iov_nofree(fuse_req_t req, int erro= r, struct iovec *iov, { struct fuse_out_header out; =20 - if (error <=3D -1000 || error > 0) { + /* error =3D 1 has been used to signal client to wait for notificaiton */ + if (error <=3D -1000 || error > 1) { fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error); error =3D -ERANGE; } @@ -291,6 +292,12 @@ int fuse_reply_err(fuse_req_t req, int err) return send_reply(req, -err, NULL, 0); } =20 +int fuse_reply_wait(fuse_req_t req) +{ + /* TODO: This is a hack. Fix it */ + return send_reply(req, 1, NULL, 0); +} + void fuse_reply_none(fuse_req_t req) { fuse_free_req(req); @@ -2207,6 +2214,20 @@ static int send_notify_iov(struct fuse_session *se, = int notify_code, return fuse_send_msg(se, NULL, iov, count); } =20 +int fuse_lowlevel_notify_lock(struct fuse_session *se, uint64_t req_id, + int32_t error) +{ + struct fuse_notify_lock_out outarg; + struct iovec iov[2]; + + outarg.id =3D req_id; + outarg.error =3D -error; + + iov[1].iov_base =3D &outarg; + iov[1].iov_len =3D sizeof(outarg); + return send_notify_iov(se, FUSE_NOTIFY_LOCK, iov, 2); +} + int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph) { if (ph !=3D NULL) { diff --git a/contrib/virtiofsd/fuse_lowlevel.h b/contrib/virtiofsd/fuse_low= level.h index e664d2d12d..f0a94683b5 100644 --- a/contrib/virtiofsd/fuse_lowlevel.h +++ b/contrib/virtiofsd/fuse_lowlevel.h @@ -1251,6 +1251,22 @@ struct fuse_lowlevel_ops { */ int fuse_reply_err(fuse_req_t req, int err); =20 +/** + * Ask caller to wait for lock. + * + * Possible requests: + * setlkw + * + * If caller sends a blocking lock request (setlkw), then reply to caller + * that wait for lock to be available. Once lock is available caller will + * receive a notification with request's unique id. Notification will + * carry info whether lock was successfully obtained or not. + * + * @param req request handle + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_wait(fuse_req_t req); + /** * Don't send reply * @@ -1704,6 +1720,15 @@ int fuse_lowlevel_notify_delete(struct fuse_session = *se, int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino, off_t offset, struct fuse_bufvec *bufv, enum fuse_buf_copy_flags flags); +/** + * Notify event related to previous lock request + * + * @param se the session object + * @param req_id the id of the request which requested setlkw + * @param error zero for success, -errno for the failure + */ +int fuse_lowlevel_notify_lock(struct fuse_session *se, uint64_t req_id, + int32_t error); =20 /* ----------------------------------------------------------- * * Utility functions * diff --git a/contrib/virtiofsd/fuse_virtio.c b/contrib/virtiofsd/fuse_virti= o.c index 982b6ad0bd..98d27e7642 100644 --- a/contrib/virtiofsd/fuse_virtio.c +++ b/contrib/virtiofsd/fuse_virtio.c @@ -215,6 +215,81 @@ static void copy_iov(struct iovec *src_iov, int src_co= unt, } } =20 +static int virtio_send_notify_msg(struct fuse_session *se, struct iovec *i= ov, + int count) +{ + struct fv_QueueInfo *qi; + VuDev *dev =3D &se->virtio_dev->dev; + VuVirtq *q; + FVRequest *req; + VuVirtqElement *elem; + unsigned int in_num, bad_in_num =3D 0, bad_out_num =3D 0; + struct fuse_out_header *out =3D iov[0].iov_base; + size_t in_len, tosend_len =3D iov_size(iov, count); + struct iovec *in_sg; + int ret =3D 0; + + /* Notifications have unique =3D=3D 0 */ + assert (!out->unique); + + if (!se->notify_enabled) + return -EOPNOTSUPP; + + /* If notifications are enabled, queue index 1 is notification queue */ + qi =3D se->virtio_dev->qi[1]; + q =3D vu_get_queue(dev, qi->qidx); + + pthread_rwlock_rdlock(&qi->virtio_dev->vu_dispatch_rwlock); + pthread_mutex_lock(&qi->vq_lock); + /* Pop an element from queue */ + req =3D vu_queue_pop(dev, q, sizeof(FVRequest), &bad_in_num, &bad_out_= num); + if (!req) { + /* TODO: Implement some sort of ring buffer and queue notifications + * on that and send these later when notification queue has space + * available. + */ + return -ENOSPC; + } + pthread_mutex_unlock(&qi->vq_lock); + pthread_rwlock_unlock(&qi->virtio_dev->vu_dispatch_rwlock); + + out->len =3D tosend_len; + elem =3D &req->elem; + in_num =3D elem->in_num; + in_sg =3D elem->in_sg; + in_len =3D iov_size(in_sg, in_num); + fuse_log(FUSE_LOG_DEBUG, "%s: elem %d: with %d in desc of length %zd\n= ", + __func__, elem->index, in_num, in_len); + + if (in_len < sizeof(struct fuse_out_header)) { + fuse_log(FUSE_LOG_ERR, "%s: elem %d too short for out_header\n", + __func__, elem->index); + ret =3D -E2BIG; + goto out; + } + + if (in_len < tosend_len) { + fuse_log(FUSE_LOG_ERR, "%s: elem %d too small for data len" + " %zd\n", __func__, elem->index, tosend_len); + ret =3D -E2BIG; + goto out; + } + + /* First copy the header data from iov->in_sg */ + copy_iov(iov, count, in_sg, in_num, tosend_len); + + /* TODO: Add bad_innum handling */ + pthread_rwlock_rdlock(&qi->virtio_dev->vu_dispatch_rwlock); + pthread_mutex_lock(&qi->vq_lock); + vu_queue_push(dev, q, elem, tosend_len); + vu_queue_notify(dev, q); + pthread_mutex_unlock(&qi->vq_lock); + pthread_rwlock_unlock(&qi->virtio_dev->vu_dispatch_rwlock); +out: + free(req); + return ret; +} + /* * Called back by ll whenever it wants to send a reply/message back * The 1st element of the iov starts with the fuse_out_header @@ -223,11 +298,11 @@ static void copy_iov(struct iovec *src_iov, int src_c= ount, int virtio_send_msg(struct fuse_session *se, struct fuse_chan *ch, struct iovec *iov, int count) { - FVRequest *req =3D container_of(ch, FVRequest, ch); - struct fv_QueueInfo *qi =3D ch->qi; + FVRequest *req; + struct fv_QueueInfo *qi; VuDev *dev =3D &se->virtio_dev->dev; - VuVirtq *q =3D vu_get_queue(dev, qi->qidx); - VuVirtqElement *elem =3D &req->elem; + VuVirtq *q; + VuVirtqElement *elem; int ret =3D 0; =20 assert(count >=3D 1); @@ -238,8 +313,15 @@ int virtio_send_msg(struct fuse_session *se, struct fu= se_chan *ch, =20 size_t tosend_len =3D iov_size(iov, count); =20 - /* unique =3D=3D 0 is notification, which we don't support */ - assert(out->unique); + /* unique =3D=3D 0 is notification */ + if (!out->unique) + return virtio_send_notify_msg(se, iov, count); + + assert(ch); + req =3D container_of(ch, FVRequest, ch); + elem =3D &req->elem; + qi =3D ch->qi; + q =3D vu_get_queue(dev, qi->qidx); assert(!req->reply_sent); =20 /* The 'in' part of the elem is to qemu */ diff --git a/contrib/virtiofsd/passthrough_ll.c b/contrib/virtiofsd/passthr= ough_ll.c index 028e7da273..ed52953565 100644 --- a/contrib/virtiofsd/passthrough_ll.c +++ b/contrib/virtiofsd/passthrough_ll.c @@ -1925,7 +1925,10 @@ static void lo_setlk(fuse_req_t req, fuse_ino_t ino, struct lo_data *lo =3D lo_data(req); struct lo_inode *inode; struct lo_inode_plock *plock; - int ret, saverr =3D 0; + int ret, saverr =3D 0, ofd; + uint64_t unique; + struct fuse_session *se =3D req->se; + bool async_lock =3D false; =20 fuse_log(FUSE_LOG_DEBUG, "lo_setlk(ino=3D%" PRIu64 ", flags=3D%d)" " cmd=3D%d pid=3D%d owner=3D0x%lx sleep=3D%d l_whence=3D%d" @@ -1933,11 +1936,6 @@ static void lo_setlk(fuse_req_t req, fuse_ino_t ino, lock->l_type, lock->l_pid, fi->lock_owner, sleep, lock->l_whence, lock->l_start, lock->l_len); =20 - if (sleep) { - fuse_reply_err(req, EOPNOTSUPP); - return; - } - inode =3D lo_inode(req, ino); if (!inode) { fuse_reply_err(req, EBADF); @@ -1950,21 +1948,54 @@ static void lo_setlk(fuse_req_t req, fuse_ino_t ino, =20 if (!plock) { saverr =3D ret; + pthread_mutex_unlock(&inode->plock_mutex); goto out; } =20 + /* + * plock is now released when inode is going away. We already have + * a reference on inode, so it is guaranteed that plock->fd is + * still around even after dropping inode->plock_mutex lock + */ + ofd =3D plock->fd; + pthread_mutex_unlock(&inode->plock_mutex); + + /* + * If this lock request can block, request caller to wait for + * notification. Do not access req after this. Once lock is + * available, send a notification instead. + */ + if (sleep && lock->l_type !=3D F_UNLCK) { + /* + * If notification queue is not enabled, can't support async + * locks. + */ + if (!se->notify_enabled) { + saverr =3D EOPNOTSUPP; + goto out; + } + async_lock =3D true; + unique =3D req->unique; + fuse_reply_wait(req); + } /* TODO: Is it alright to modify flock? */ lock->l_pid =3D 0; - ret =3D fcntl(plock->fd, F_OFD_SETLK, lock); + if (async_lock) + ret =3D fcntl(ofd, F_OFD_SETLKW, lock); + else + ret =3D fcntl(ofd, F_OFD_SETLK, lock); if (ret =3D=3D -1) { saverr =3D errno; } =20 out: - pthread_mutex_unlock(&inode->plock_mutex); lo_inode_put(lo, &inode); =20 - fuse_reply_err(req, saverr); + if (!async_lock) + fuse_reply_err(req, saverr); + else { + fuse_lowlevel_notify_lock(se, unique, saverr); + } } =20 static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, --=20 2.20.1