From patchwork Wed Dec 4 19:08:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivek Goyal X-Patchwork-Id: 11273481 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 147126C1 for ; Wed, 4 Dec 2019 19:14:37 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DE37F2073C for ; Wed, 4 Dec 2019 19:14:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Xxe93rq1" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DE37F2073C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:46102 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ica6d-0006jY-0o for patchwork-qemu-devel@patchwork.kernel.org; Wed, 04 Dec 2019 14:14:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51172) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ica1E-0002cw-Bx for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:09:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ica18-0005TU-Ru for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:56 -0500 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:35930 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 1ica16-0005PF-UA for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575486529; 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=FeQb7b0beOVO0Lr1WGzTfmVOOghutFPqqMJQKCCjm/o=; b=Xxe93rq18hT76H/Pf3iLJ1l+WOG8rD8C5OgxCyLIZT7SeJv1dKbqrMCYbdYGQbKag4nWeN pDWNmibR4UEe7JqAQNwhBa/wyihXYdAgqftxRTlNojVYyzcdUNclsJhe579pfM7vZhazWj z2Jn0dV3yJrwIcTVBJNe1HpxnA09KiM= 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-422-Ws-x8GdTOuOUWsXmhGfT7g-1; Wed, 04 Dec 2019 14:08:48 -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 A9A8D1005516 for ; Wed, 4 Dec 2019 19:08:47 +0000 (UTC) Received: from horse.redhat.com (unknown [10.18.25.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9B935A4B60; Wed, 4 Dec 2019 19:08:42 +0000 (UTC) Received: by horse.redhat.com (Postfix, from userid 10451) id 15A8F2246AE; Wed, 4 Dec 2019 14:08:42 -0500 (EST) From: Vivek Goyal To: virtio-fs@redhat.com, qemu-devel@nongnu.org Subject: [PATCH v2 1/5] virtiofsd: Get rid of unused fields in fv_QueueInfo Date: Wed, 4 Dec 2019 14:08:32 -0500 Message-Id: <20191204190836.31324-2-vgoyal@redhat.com> In-Reply-To: <20191204190836.31324-1-vgoyal@redhat.com> References: <20191204190836.31324-1-vgoyal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: Ws-x8GdTOuOUWsXmhGfT7g-1 X-Mimecast-Spam-Score: 0 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: mszeredi@redhat.com, dgilbert@redhat.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" There are some unused fields in "struct fv_QueueInfo". Get rid of these fields. Signed-off-by: Vivek Goyal Reviewed-by: Dr. David Alan Gilbert --- contrib/virtiofsd/fuse_virtio.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/contrib/virtiofsd/fuse_virtio.c b/contrib/virtiofsd/fuse_virtio.c index 31c8542b6c..2a9cd60a01 100644 --- a/contrib/virtiofsd/fuse_virtio.c +++ b/contrib/virtiofsd/fuse_virtio.c @@ -50,12 +50,6 @@ struct fv_QueueInfo { int qidx; int kick_fd; int kill_fd; /* For killing the thread */ - - /* The element for the command currently being processed */ - VuVirtqElement *qe; - /* If any of the qe vec elements (towards vmm) are unmappable */ - unsigned int elem_bad_in; - bool reply_sent; }; /* A FUSE request */ From patchwork Wed Dec 4 19:08:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivek Goyal X-Patchwork-Id: 11273477 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EE3A56C1 for ; Wed, 4 Dec 2019 19:10:32 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C478220656 for ; Wed, 4 Dec 2019 19:10:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MlTKrvOC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C478220656 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:46082 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ica2h-000425-Go for patchwork-qemu-devel@patchwork.kernel.org; Wed, 04 Dec 2019 14:10:31 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51137) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ica18-0002ZG-Ky for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ica15-0005Pw-A1 for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:52 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:40269 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 1ica15-0005PK-0Y for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575486530; 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=wtQy2nb+w6js/FC7hyDX4RQMgk6gm8WimLPO9TMsXP4=; b=MlTKrvOCTxhg3KNF5QmKUT+bC5Yf0UEgt3N/I+kMYqhI1KxqkCYvhjHPwI9f0z/9cLC1yI YKHxn25YeLxvxRYl5N2Mv52LtUS4xKbwcoylf27I6/I+nXNnXBkSsv52q6MvBuU8E9CMie 7y06YQoB3IPEt57I/CSGADNLv6HWbfU= 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-29-bhuuTWqpMYyvsqgpjmQ9qQ-1; Wed, 04 Dec 2019 14:08:48 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BBE481023139 for ; Wed, 4 Dec 2019 19:08:47 +0000 (UTC) Received: from horse.redhat.com (unknown [10.18.25.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 99ED419C68; Wed, 4 Dec 2019 19:08:42 +0000 (UTC) Received: by horse.redhat.com (Postfix, from userid 10451) id 1C8572246B1; Wed, 4 Dec 2019 14:08:42 -0500 (EST) From: Vivek Goyal To: virtio-fs@redhat.com, qemu-devel@nongnu.org Subject: [PATCH v2 2/5] virtiofsd: Release file locks using F_UNLCK Date: Wed, 4 Dec 2019 14:08:33 -0500 Message-Id: <20191204190836.31324-3-vgoyal@redhat.com> In-Reply-To: <20191204190836.31324-1-vgoyal@redhat.com> References: <20191204190836.31324-1-vgoyal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: bhuuTWqpMYyvsqgpjmQ9qQ-1 X-Mimecast-Spam-Score: 0 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: mszeredi@redhat.com, dgilbert@redhat.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" 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 | 31 ++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/contrib/virtiofsd/passthrough_ll.c b/contrib/virtiofsd/passthrough_ll.c index bc214df0c7..6aa56882e8 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_inode *inode) } } +static void posix_locks_value_destroy(gpointer data) +{ + struct lo_inode_plock *plock = data; + + close(plock->fd); + free(plock); +} + /* Increments nlookup and caller must release refcount using * lo_inode_put(&parent). */ @@ -994,7 +1002,9 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, inode->key.ino = e->attr.st_ino; inode->key.dev = e->attr.st_dev; pthread_mutex_init(&inode->plock_mutex, NULL); - inode->posix_locks = g_hash_table_new(g_direct_hash, g_direct_equal); + inode->posix_locks = g_hash_table_new_full(g_direct_hash, + g_direct_equal, NULL, + posix_locks_value_destroy); get_shared(lo, inode); @@ -1436,9 +1446,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); @@ -1868,6 +1875,7 @@ static struct lo_inode_plock *lookup_create_plock_ctx(struct lo_data *lo, plock->fd = 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; } @@ -2046,6 +2054,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; inode = lo_inode(req, ino); if (!inode) { @@ -2058,14 +2067,16 @@ static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) plock = 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 = F_UNLCK; + flock.l_whence = SEEK_SET; + /* Unlock whole file */ + flock.l_start = flock.l_len = 0; + fcntl(plock->fd, F_OFD_SETLK, &flock); } pthread_mutex_unlock(&inode->plock_mutex); From patchwork Wed Dec 4 19:08:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivek Goyal X-Patchwork-Id: 11273483 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 689156C1 for ; Wed, 4 Dec 2019 19:14:45 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2D65B2073C for ; Wed, 4 Dec 2019 19:14:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MzbuwHRL" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2D65B2073C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:46104 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ica6m-0006x4-5T for patchwork-qemu-devel@patchwork.kernel.org; Wed, 04 Dec 2019 14:14:44 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51162) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ica1A-0002Zt-RV for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:09:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ica18-0005Tb-SR for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:56 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:33248 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 1ica16-0005PE-V9 for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575486529; 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=mAhYHUCtIsGWrIxvI8A8iRtDjmmVa3sO5ynZ4qkQ8zs=; b=MzbuwHRL58viIq1pCteogtkDmBdKnCHZXTnwpKsD7VCClKTLdAu1YuCSi3k9HXMXXXYipo 9fJEWwHf7EazatjUGL9So5ETuJ7wPJMwdU4qPpRtZfQ34mVvow9qnOk34PNnBdMYmBHg0K UWqkUWYGsZhIgdUkCP2PSE26nv+mBJA= 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-11-8Gv5jSC7NbiMAaXj4HbROw-1; Wed, 04 Dec 2019 14:08:48 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6BABD18A6EF1 for ; Wed, 4 Dec 2019 19:08:47 +0000 (UTC) Received: from horse.redhat.com (unknown [10.18.25.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9A6D5600C8; Wed, 4 Dec 2019 19:08:42 +0000 (UTC) Received: by horse.redhat.com (Postfix, from userid 10451) id 2B8B922471C; Wed, 4 Dec 2019 14:08:42 -0500 (EST) From: Vivek Goyal To: virtio-fs@redhat.com, qemu-devel@nongnu.org Subject: [PATCH v2 3/5] virtiofd: Create a notification queue Date: Wed, 4 Dec 2019 14:08:34 -0500 Message-Id: <20191204190836.31324-4-vgoyal@redhat.com> In-Reply-To: <20191204190836.31324-1-vgoyal@redhat.com> References: <20191204190836.31324-1-vgoyal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: 8Gv5jSC7NbiMAaXj4HbROw-1 X-Mimecast-Spam-Score: 0 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: mszeredi@redhat.com, dgilbert@redhat.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" 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 | 74 +++++++++++++++------- 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, 87 insertions(+), 31 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; }; struct fuse_chan { diff --git a/contrib/virtiofsd/fuse_virtio.c b/contrib/virtiofsd/fuse_virtio.c index 2a9cd60a01..b1eebcf054 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" @@ -92,23 +93,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; }; /* Callback from libvhost-user */ static uint64_t fv_get_features(VuDev *dev) { - return 1ULL << VIRTIO_F_VERSION_1; + uint64_t features; + + features = 1ull << VIRTIO_F_VERSION_1 | + 1ull << VIRTIO_FS_F_NOTIFICATION; + + return features; } /* Callback from libvhost-user */ static void fv_set_features(VuDev *dev, uint64_t features) { + struct fv_VuDev *vud = container_of(dev, struct fv_VuDev, dev); + struct fuse_session *se = vud->se; + + if ((1ull << VIRTIO_FS_F_NOTIFICATION) & features) { + vud->notify_enabled = true; + se->notify_enabled = true; + } } /* @@ -765,6 +774,9 @@ static void fv_queue_set_started(VuDev *dev, int qidx, bool started) { struct fv_VuDev *vud = container_of(dev, struct fv_VuDev, dev); struct fv_QueueInfo *ourqi; + void * (*thread_func) (void *) = fv_queue_thread; + int valid_queues = 2; /* One hiprio queue and one request queue */ + bool notification_q = false; fuse_log(FUSE_LOG_INFO, "%s: qidx=%d started=%d\n", __func__, qidx, started); @@ -776,10 +788,18 @@ 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 notification queue is enabled, then qidx 1 is notificaiton queue. + */ + if (qidx == 1) + notification_q = true; + } + + if (qidx >= 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); } @@ -803,13 +823,19 @@ static void fv_queue_set_started(VuDev *dev, int qidx, bool started) assert(vud->qi[qidx]->kick_fd == -1); } ourqi = vud->qi[qidx]; + pthread_mutex_init(&ourqi->vq_lock, NULL); + /* + * For notification queue, we don't have to start a thread yet. + */ + if (notification_q) + return; + ourqi->kick_fd = dev->vq[qidx].kick_fd; ourqi->kill_fd = eventfd(0, EFD_CLOEXEC | EFD_SEMAPHORE); assert(ourqi->kill_fd != -1); - pthread_mutex_init(&ourqi->vq_lock, NULL); - if (pthread_create(&ourqi->thread, NULL, fv_queue_thread, ourqi)) { + 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); @@ -819,17 +845,19 @@ static void fv_queue_set_started(VuDev *dev, int qidx, bool started) assert(qidx < vud->nqueues); ourqi = vud->qi[qidx]; - /* Kill the thread */ - if (eventfd_write(ourqi->kill_fd, 1)) { - fuse_log(FUSE_LOG_ERR, "Eventfd_read for queue: %m\n"); - } - ret = pthread_join(ourqi->thread, NULL); - if (ret) { - fuse_log(FUSE_LOG_ERR, "%s: Failed to join thread idx %d err %d\n", - __func__, qidx, ret); + if (!notification_q) { + /* Kill the thread */ + if (eventfd_write(ourqi->kill_fd, 1)) { + fuse_log(FUSE_LOG_ERR, "Eventfd_read for queue: %m\n"); + } + ret = pthread_join(ourqi->thread, NULL); + if (ret) { + fuse_log(FUSE_LOG_ERR, "%s: Failed to join thread idx %d err" + " %d\n", __func__, qidx, ret); + } + close(ourqi->kill_fd); } pthread_mutex_destroy(&ourqi->vq_lock); - close(ourqi->kill_fd); ourqi->kick_fd = -1; free(vud->qi[qidx]); vud->qi[qidx] = NULL; @@ -1034,7 +1062,7 @@ int virtio_session_mount(struct fuse_session *se) se->virtio_dev = calloc(sizeof(struct fv_VuDev), 1); se->virtio_dev->se = 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_watch, + vu_init(&se->virtio_dev->dev, 3, se->vu_socketfd, fv_panic, fv_set_watch, fv_remove_watch, &fv_iface); 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 *vpci_dev, Error **errp) uint64_t totalsize; if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { - vpci_dev->nvectors = dev->vdev.conf.num_request_queues + 1; + vpci_dev->nvectors = dev->vdev.conf.num_request_queues + 2; } 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" +static const int user_feature_bits[] = { + VIRTIO_FS_F_NOTIFICATION, +}; + uint64_t vhost_user_fs_slave_map(struct vhost_dev *dev, VhostUserFSSlaveMsg *sm, int fd) { @@ -378,12 +382,23 @@ static void vuf_set_status(VirtIODevice *vdev, uint8_t status) } } -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 = 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 = VHOST_USER_FS(vdev); + + if (virtio_has_feature(features, VIRTIO_FS_F_NOTIFICATION)) { + fs->notify_enabled = true; + } } static void vuf_handle_output(VirtIODevice *vdev, VirtQueue *vq) @@ -515,13 +530,20 @@ static void vuf_device_realize(DeviceState *dev, Error **errp) /* Hiprio queue */ virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output); + /* 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 = 0; i < fs->conf.num_request_queues; i++) { virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output); } - /* 1 high prio queue, plus the number configured */ - fs->vhost_dev.nvqs = 1 + fs->conf.num_request_queues; + /* 1 high prio queue, 1 notification queue plus the number configured */ + fs->vhost_dev.nvqs = 2 + fs->conf.num_request_queues; + fs->vhost_dev.vqs = g_new0(struct vhost_virtqueue, fs->vhost_dev.nvqs); ret = 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 *data) vdc->realize = vuf_device_realize; vdc->unrealize = vuf_device_unrealize; vdc->get_features = vuf_get_features; + vdc->set_features = vuf_set_features; vdc->get_config = vuf_get_config; vdc->set_status = vuf_set_status; vdc->guest_notifier_mask = vuf_guest_notifier_mask; diff --git a/include/hw/virtio/vhost-user-fs.h b/include/hw/virtio/vhost-user-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; /* 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" +/* 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]; From patchwork Wed Dec 4 19:08:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivek Goyal X-Patchwork-Id: 11273475 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DE1F3138C for ; Wed, 4 Dec 2019 19:10:31 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6FA8420656 for ; Wed, 4 Dec 2019 19:10:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="TvNRhrjP" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6FA8420656 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:46076 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ica2f-0003zR-V3 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 04 Dec 2019 14:10:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51138) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ica18-0002ZH-LK for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ica15-0005Pq-8H for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:52 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:32247 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 1ica14-0005Oj-WB for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575486529; 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=HNKuisohKuzW0O/AF+rSjIu6/T1DLhroU5kUOiE3QF0=; b=TvNRhrjPEnOoMvI5VoTf2B6MABFlrU1RzIsfgOwIUSdgOT8IJED2LbKk8TmVSSQKolvBre 2ShjSq5cqGvTb8PLT5Wlyra3VSLFyFeYtFD9B1QOZVYwzNuox6nXgz0/ThexnVkmiHusSP RqMvDE1JlyFsy0L8edtrP/bRzW6V7Zo= 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-352-XXtWF4u6MjCRQXVPHE99WA-1; Wed, 04 Dec 2019 14:08:48 -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 7D51618A6EF2 for ; Wed, 4 Dec 2019 19:08:47 +0000 (UTC) Received: from horse.redhat.com (unknown [10.18.25.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9B85810013A1; Wed, 4 Dec 2019 19:08:42 +0000 (UTC) Received: by horse.redhat.com (Postfix, from userid 10451) id 2F3A722474F; Wed, 4 Dec 2019 14:08:42 -0500 (EST) From: Vivek Goyal To: virtio-fs@redhat.com, qemu-devel@nongnu.org Subject: [PATCH v2 4/5] virtiofsd: Specify size of notification buffer using config space Date: Wed, 4 Dec 2019 14:08:35 -0500 Message-Id: <20191204190836.31324-5-vgoyal@redhat.com> In-Reply-To: <20191204190836.31324-1-vgoyal@redhat.com> References: <20191204190836.31324-1-vgoyal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-MC-Unique: XXtWF4u6MjCRQXVPHE99WA-1 X-Mimecast-Spam-Score: 0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.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: mszeredi@redhat.com, dgilbert@redhat.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" 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 | 31 ++++++++++++++++++++++ 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, 61 insertions(+) diff --git a/contrib/virtiofsd/fuse_virtio.c b/contrib/virtiofsd/fuse_virtio.c index b1eebcf054..94cf9b3791 100644 --- a/contrib/virtiofsd/fuse_virtio.c +++ b/contrib/virtiofsd/fuse_virtio.c @@ -869,6 +869,35 @@ static bool fv_queue_order(VuDev *dev, int qidx) return false; } +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 = {}; + unsigned notify_size, roundto = 64; + union fuse_notify_union { + struct fuse_notify_poll_wakeup_out wakeup_out; + struct fuse_notify_inval_inode_out inode_out; + struct fuse_notify_inval_entry_out entry_out; + struct fuse_notify_delete_out delete_out; + struct fuse_notify_store_out store_out; + struct fuse_notify_retrieve_out retrieve_out; + }; + + notify_size = sizeof(struct fuse_out_header) + + sizeof(union fuse_notify_union); + notify_size = ((notify_size + roundto)/roundto) * roundto; + + fscfg.notify_buf_size = notify_size; + memcpy(config, &fscfg, len); + fuse_log(FUSE_LOG_DEBUG, "%s:Setting notify_buf_size=%d\n", __func__, + fscfg.notify_buf_size); + return 0; +} + static const VuDevIface fv_iface = { .get_features = fv_get_features, .set_features = fv_set_features, @@ -877,6 +906,8 @@ static const VuDevIface fv_iface = { .queue_set_started = fv_queue_set_started, .queue_is_processed_in_order = fv_queue_order, + .get_protocol_features = fv_get_protocol_features, + .get_config = fv_get_config, }; /* diff --git a/hw/virtio/vhost-user-fs.c b/hw/virtio/vhost-user-fs.c index 5555fe9dbe..5a6d244b98 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; } +static int vhost_user_fs_handle_config_change(struct vhost_dev *dev) +{ + return 0; +} + +const VhostDevConfigOps fs_ops = { + .vhost_dev_config_notifier = vhost_user_fs_handle_config_change, +}; static void vuf_get_config(VirtIODevice *vdev, uint8_t *config) { VHostUserFS *fs = VHOST_USER_FS(vdev); struct virtio_fs_config fscfg = {}; + int ret; + + /* + * As of now we only get notification buffer size from device. And that's + * needed only if notification queue is enabled. + */ + if (fs->notify_enabled) { + ret = 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=%d\n", ret); + return; + } + } memcpy((char *)fscfg.tag, fs->conf.tag, MIN(strlen(fs->conf.tag) + 1, sizeof(fscfg.tag))); virtio_stl_p(vdev, &fscfg.num_request_queues, fs->conf.num_request_queues); + virtio_stl_p(vdev, &fscfg.notify_buf_size, fs->fscfg.notify_buf_size); memcpy(config, &fscfg, sizeof(fscfg)); } @@ -545,6 +569,8 @@ static void vuf_device_realize(DeviceState *dev, Error **errp) fs->vhost_dev.nvqs = 2 + fs->conf.num_request_queues; fs->vhost_dev.vqs = g_new0(struct vhost_virtqueue, fs->vhost_dev.nvqs); + + vhost_dev_set_config_notifier(&fs->vhost_dev, &fs_ops); ret = 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-user-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 +#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; /*< 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 { /* Number of request queues */ uint32_t num_request_queues; + /* Size of notification buffer */ + uint32_t notify_buf_size; } QEMU_PACKED; #define VIRTIO_FS_PCI_CACHE_BAR 2 From patchwork Wed Dec 4 19:08:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivek Goyal X-Patchwork-Id: 11273489 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 70F076C1 for ; Wed, 4 Dec 2019 19:22:17 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E711420675 for ; Wed, 4 Dec 2019 19:22:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="BoA0SXS/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E711420675 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:46192 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1icaE3-00021l-NQ for patchwork-qemu-devel@patchwork.kernel.org; Wed, 04 Dec 2019 14:22:15 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51191) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ica1G-0002dH-Lr for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:09:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ica1E-0005Y6-JK for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:09:02 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:38460 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 1ica1A-0005U3-PP for qemu-devel@nongnu.org; Wed, 04 Dec 2019 14:08:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575486535; 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=n8gBRP4c6w19EXcmTWgsrrJ8ke0Lp20aBqG4u2MjHGw=; b=BoA0SXS/OOVkijkYPCURl5RS+ZFRely4g5umRXI8JYA7f5atLuh9IHFxeYfNppL/H2pj6y oqeGySrwCdySZQA3pOfEirU4Mxh/qzZaqEBQ7fqM6K5hB45Z3bAUzABnF+HRWdy8AMKg/s hB2cGcyvrSQnaj1gbo2qTzhABbZpZI4= 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-299-_14MUJXLP-a19YejEDOd4w-1; Wed, 04 Dec 2019 14:08:53 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CE66318A6EF4 for ; Wed, 4 Dec 2019 19:08:52 +0000 (UTC) Received: from horse.redhat.com (unknown [10.18.25.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id CA9C860BF3; Wed, 4 Dec 2019 19:08:47 +0000 (UTC) Received: by horse.redhat.com (Postfix, from userid 10451) id 35CF3224750; Wed, 4 Dec 2019 14:08:42 -0500 (EST) From: Vivek Goyal To: virtio-fs@redhat.com, qemu-devel@nongnu.org Subject: [PATCH v2 5/5] virtiofsd: Implement blocking posix locks Date: Wed, 4 Dec 2019 14:08:36 -0500 Message-Id: <20191204190836.31324-6-vgoyal@redhat.com> In-Reply-To: <20191204190836.31324-1-vgoyal@redhat.com> References: <20191204190836.31324-1-vgoyal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: _14MUJXLP-a19YejEDOd4w-1 X-Mimecast-Spam-Score: 0 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: mszeredi@redhat.com, dgilbert@redhat.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" 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 | 97 ++++++++++++++++++++++++++++-- contrib/virtiofsd/passthrough_ll.c | 49 ++++++++++++--- 5 files changed, 185 insertions(+), 16 deletions(-) diff --git a/contrib/virtiofsd/fuse_kernel.h b/contrib/virtiofsd/fuse_kernel.h index 2bdc8b1c88..432eb14d14 100644 --- a/contrib/virtiofsd/fuse_kernel.h +++ b/contrib/virtiofsd/fuse_kernel.h @@ -444,6 +444,7 @@ enum fuse_notify_code { FUSE_NOTIFY_STORE = 4, FUSE_NOTIFY_RETRIEVE = 5, FUSE_NOTIFY_DELETE = 6, + FUSE_NOTIFY_LOCK = 7, FUSE_NOTIFY_CODE_MAX, }; @@ -836,6 +837,12 @@ struct fuse_notify_retrieve_in { uint64_t dummy4; }; +struct fuse_notify_lock_out { + uint64_t unique; + int32_t error; + int32_t padding; +}; + /* Device ioctls: */ #define FUSE_DEV_IOC_CLONE _IOR(229, 0, uint32_t) diff --git a/contrib/virtiofsd/fuse_lowlevel.c b/contrib/virtiofsd/fuse_lowlevel.c index d4a42d9804..3d9c289510 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 error, struct iovec *iov, { struct fuse_out_header out; - if (error <= -1000 || error > 0) { + /* error = 1 has been used to signal client to wait for notificaiton */ + if (error <= -1000 || error > 1) { fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error); error = -ERANGE; } @@ -291,6 +292,12 @@ int fuse_reply_err(fuse_req_t req, int err) return send_reply(req, -err, NULL, 0); } +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); } +int fuse_lowlevel_notify_lock(struct fuse_session *se, uint64_t unique, + int32_t error) +{ + struct fuse_notify_lock_out outarg = {0}; + struct iovec iov[2]; + + outarg.unique = unique; + outarg.error = -error; + + iov[1].iov_base = &outarg; + iov[1].iov_len = sizeof(outarg); + return send_notify_iov(se, FUSE_NOTIFY_LOCK, iov, 2); +} + int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph) { if (ph != NULL) { diff --git a/contrib/virtiofsd/fuse_lowlevel.h b/contrib/virtiofsd/fuse_lowlevel.h index e664d2d12d..4126b4f967 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); +/** + * 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 unique the unique 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 unique, + int32_t error); /* ----------------------------------------------------------- * * Utility functions * diff --git a/contrib/virtiofsd/fuse_virtio.c b/contrib/virtiofsd/fuse_virtio.c index 94cf9b3791..129dd329f6 100644 --- a/contrib/virtiofsd/fuse_virtio.c +++ b/contrib/virtiofsd/fuse_virtio.c @@ -208,6 +208,83 @@ static void copy_iov(struct iovec *src_iov, int src_count, } } +static int virtio_send_notify_msg(struct fuse_session *se, struct iovec *iov, + int count) +{ + struct fv_QueueInfo *qi; + VuDev *dev = &se->virtio_dev->dev; + VuVirtq *q; + FVRequest *req; + VuVirtqElement *elem; + unsigned int in_num; + struct fuse_out_header *out = iov[0].iov_base; + size_t in_len, tosend_len = iov_size(iov, count); + struct iovec *in_sg; + int ret = 0; + + /* Notifications have unique == 0 */ + assert (!out->unique); + + if (!se->notify_enabled) + return -EOPNOTSUPP; + + /* If notifications are enabled, queue index 1 is notification queue */ + qi = se->virtio_dev->qi[1]; + q = 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 = vu_queue_pop(dev, q, sizeof(FVRequest), NULL, NULL); + if (!req) { + /* TODO: Implement some sort of ring buffer and queue notifications + * on that and send these later when notification queue has space + * available. + */ + ret = -ENOSPC; + } + pthread_mutex_unlock(&qi->vq_lock); + pthread_rwlock_unlock(&qi->virtio_dev->vu_dispatch_rwlock); + + if (ret) + return ret; + + out->len = tosend_len; + elem = &req->elem; + in_num = elem->in_num; + in_sg = elem->in_sg; + in_len = 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 = -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 = -E2BIG; + goto out; + } + + /* First copy the header data from iov->in_sg */ + copy_iov(iov, count, in_sg, in_num, tosend_len); + + 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 @@ -216,11 +293,11 @@ static void copy_iov(struct iovec *src_iov, int src_count, int virtio_send_msg(struct fuse_session *se, struct fuse_chan *ch, struct iovec *iov, int count) { - FVRequest *req = container_of(ch, FVRequest, ch); - struct fv_QueueInfo *qi = ch->qi; + FVRequest *req; + struct fv_QueueInfo *qi; VuDev *dev = &se->virtio_dev->dev; - VuVirtq *q = vu_get_queue(dev, qi->qidx); - VuVirtqElement *elem = &req->elem; + VuVirtq *q; + VuVirtqElement *elem; int ret = 0; assert(count >= 1); @@ -231,8 +308,15 @@ int virtio_send_msg(struct fuse_session *se, struct fuse_chan *ch, size_t tosend_len = iov_size(iov, count); - /* unique == 0 is notification, which we don't support */ - assert(out->unique); + /* unique == 0 is notification */ + if (!out->unique) + return virtio_send_notify_msg(se, iov, count); + + assert(ch); + req = container_of(ch, FVRequest, ch); + elem = &req->elem; + qi = ch->qi; + q = vu_get_queue(dev, qi->qidx); assert(!req->reply_sent); /* The 'in' part of the elem is to qemu */ @@ -885,6 +969,7 @@ static int fv_get_config(VuDev *dev, uint8_t *config, uint32_t len) struct fuse_notify_delete_out delete_out; struct fuse_notify_store_out store_out; struct fuse_notify_retrieve_out retrieve_out; + struct fuse_notify_lock_out lock_out; }; notify_size = sizeof(struct fuse_out_header) + diff --git a/contrib/virtiofsd/passthrough_ll.c b/contrib/virtiofsd/passthrough_ll.c index 6aa56882e8..308fc76530 100644 --- a/contrib/virtiofsd/passthrough_ll.c +++ b/contrib/virtiofsd/passthrough_ll.c @@ -1926,7 +1926,10 @@ static void lo_setlk(fuse_req_t req, fuse_ino_t ino, struct lo_data *lo = lo_data(req); struct lo_inode *inode; struct lo_inode_plock *plock; - int ret, saverr = 0; + int ret, saverr = 0, ofd; + uint64_t unique; + struct fuse_session *se = req->se; + bool async_lock = false; fuse_log(FUSE_LOG_DEBUG, "lo_setlk(ino=%" PRIu64 ", flags=%d)" " cmd=%d pid=%d owner=0x%lx sleep=%d l_whence=%d" @@ -1934,11 +1937,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); - if (sleep) { - fuse_reply_err(req, EOPNOTSUPP); - return; - } - inode = lo_inode(req, ino); if (!inode) { fuse_reply_err(req, EBADF); @@ -1951,21 +1949,54 @@ static void lo_setlk(fuse_req_t req, fuse_ino_t ino, if (!plock) { saverr = ret; + pthread_mutex_unlock(&inode->plock_mutex); goto out; } + /* + * 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 = 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 != F_UNLCK) { + /* + * If notification queue is not enabled, can't support async + * locks. + */ + if (!se->notify_enabled) { + saverr = EOPNOTSUPP; + goto out; + } + async_lock = true; + unique = req->unique; + fuse_reply_wait(req); + } /* TODO: Is it alright to modify flock? */ lock->l_pid = 0; - ret = fcntl(plock->fd, F_OFD_SETLK, lock); + if (async_lock) + ret = fcntl(ofd, F_OFD_SETLKW, lock); + else + ret = fcntl(ofd, F_OFD_SETLK, lock); if (ret == -1) { saverr = errno; } out: - pthread_mutex_unlock(&inode->plock_mutex); lo_inode_put(lo, &inode); - fuse_reply_err(req, saverr); + if (!async_lock) + fuse_reply_err(req, saverr); + else { + fuse_lowlevel_notify_lock(se, unique, saverr); + } } static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,