From patchwork Fri Oct 25 10:01:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 11211959 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 1D97A112B for ; Fri, 25 Oct 2019 10:39:47 +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 E75602070B for ; Fri, 25 Oct 2019 10:39:46 +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="HNCoImOW" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E75602070B 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]:58414 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNx0T-0007vO-R0 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 25 Oct 2019 06:39:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33068) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNwRC-0002DO-5f for qemu-devel@nongnu.org; Fri, 25 Oct 2019 06:03:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNwQz-0006J0-SV for qemu-devel@nongnu.org; Fri, 25 Oct 2019 06:03:16 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:46796 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 1iNwQz-0006A2-M8 for qemu-devel@nongnu.org; Fri, 25 Oct 2019 06:03:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571997738; 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=LwhXgl2ciXXzy/J77uxFbsgMEqtSSgjQHtAsTomujWc=; b=HNCoImOW7ThhzlzYt0NE/S+6M0KU444n1kqcccb0U1gE4OKjiDEBc27eq5WbD3+C2D+BpH yuPuYVis/5h01MPhJBr2HgZptJUxerS0nFlHahb+k/7xCN50SpzERYXY8uE4GXlg3kniV6 m+r2uCFMLSz8BnIlQJwUvtw6D16CtZg= 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-362-eJvXOwXeMqC3epOlF6APpQ-1; Fri, 25 Oct 2019 06:02:14 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0F9B147B for ; Fri, 25 Oct 2019 10:02:14 +0000 (UTC) Received: from localhost (unknown [10.36.118.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 683E65C1D4; Fri, 25 Oct 2019 10:02:04 +0000 (UTC) From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [RFC 1/3] WIP virtiofsd: import Linux header file Date: Fri, 25 Oct 2019 12:01:50 +0200 Message-Id: <20191025100152.6638-2-stefanha@redhat.com> In-Reply-To: <20191025100152.6638-1-stefanha@redhat.com> References: <20191025100152.6638-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: eJvXOwXeMqC3epOlF6APpQ-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: Laurent Vivier , Thomas Huth , "Michael S. Tsirkin" , Cornelia Huck , "Dr. David Alan Gilbert" , virtio-fs@redhat.com, Stefan Hajnoczi , Paolo Bonzini , vgoyal@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" tests/vhost-user-fs-test.c needs fuse.h. The private copy that virtiofsd has can be replaced with a properly imported file using update-linux-headers.sh. TODO rerun update-linux-headers.sh with upstream kernel tree! Signed-off-by: Stefan Hajnoczi --- contrib/virtiofsd/fuse_lowlevel.h | 2 +- .../fuse_kernel.h => include/standard-headers/linux/fuse.h | 0 contrib/virtiofsd/fuse_loop_mt.c | 2 +- contrib/virtiofsd/fuse_lowlevel.c | 2 +- contrib/virtiofsd/fuse_virtio.c | 2 +- scripts/update-linux-headers.sh | 3 ++- 6 files changed, 6 insertions(+), 5 deletions(-) rename contrib/virtiofsd/fuse_kernel.h => include/standard-headers/linux/fuse.h (100%) diff --git a/contrib/virtiofsd/fuse_lowlevel.h b/contrib/virtiofsd/fuse_lowlevel.h index 79fb30a1c2..a8c92ff7e0 100644 --- a/contrib/virtiofsd/fuse_lowlevel.h +++ b/contrib/virtiofsd/fuse_lowlevel.h @@ -23,7 +23,7 @@ #endif #include "fuse_common.h" -#include "fuse_kernel.h" +#include "standard-headers/linux/fuse.h" #include #include diff --git a/contrib/virtiofsd/fuse_kernel.h b/include/standard-headers/linux/fuse.h similarity index 100% rename from contrib/virtiofsd/fuse_kernel.h rename to include/standard-headers/linux/fuse.h diff --git a/contrib/virtiofsd/fuse_loop_mt.c b/contrib/virtiofsd/fuse_loop_mt.c index 2000a8902a..af7b501fac 100644 --- a/contrib/virtiofsd/fuse_loop_mt.c +++ b/contrib/virtiofsd/fuse_loop_mt.c @@ -11,7 +11,7 @@ #include "fuse_i.h" #include "fuse_lowlevel.h" #include "fuse_misc.h" -#include "fuse_kernel.h" +#include "standard-headers/linux/fuse.h" #include "fuse_virtio.h" #include diff --git a/contrib/virtiofsd/fuse_lowlevel.c b/contrib/virtiofsd/fuse_lowlevel.c index 78ccfe3a27..c1a901cb4d 100644 --- a/contrib/virtiofsd/fuse_lowlevel.c +++ b/contrib/virtiofsd/fuse_lowlevel.c @@ -10,7 +10,7 @@ */ #include "fuse_i.h" -#include "fuse_kernel.h" +#include "standard-headers/linux/fuse.h" #include "fuse_opt.h" #include "fuse_misc.h" #include "fuse_virtio.h" diff --git a/contrib/virtiofsd/fuse_virtio.c b/contrib/virtiofsd/fuse_virtio.c index 533ef24bb7..7a0d0b2603 100644 --- a/contrib/virtiofsd/fuse_virtio.c +++ b/contrib/virtiofsd/fuse_virtio.c @@ -15,7 +15,7 @@ #include "qapi/error.h" #include "fuse_i.h" -#include "fuse_kernel.h" +#include "standard-headers/linux/fuse.h" #include "fuse_misc.h" #include "fuse_opt.h" #include "fuse_virtio.h" diff --git a/scripts/update-linux-headers.sh b/scripts/update-linux-headers.sh index f76d77363b..1a627ccd73 100755 --- a/scripts/update-linux-headers.sh +++ b/scripts/update-linux-headers.sh @@ -184,7 +184,8 @@ EOF rm -rf "$output/include/standard-headers/linux" mkdir -p "$output/include/standard-headers/linux" -for i in "$tmpdir"/include/linux/*virtio*.h \ +for i in "$tmpdir/include/linux/fuse.h" \ + "$tmpdir"/include/linux/*virtio*.h \ "$tmpdir/include/linux/qemu_fw_cfg.h" \ "$tmpdir/include/linux/input.h" \ "$tmpdir/include/linux/input-event-codes.h" \ From patchwork Fri Oct 25 10:01:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 11211949 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 C1E431390 for ; Fri, 25 Oct 2019 10:32:46 +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 9668720578 for ; Fri, 25 Oct 2019 10:32:46 +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="fgXyIXPF" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9668720578 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]:58364 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNwth-00071m-E4 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 25 Oct 2019 06:32:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33066) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNwRC-0002DN-5v for qemu-devel@nongnu.org; Fri, 25 Oct 2019 06:03:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNwQz-0006J8-Sy for qemu-devel@nongnu.org; Fri, 25 Oct 2019 06:03:16 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:48472 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 1iNwQz-0006Bv-NA for qemu-devel@nongnu.org; Fri, 25 Oct 2019 06:03:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571997748; 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=PE1JdYLcfkPlEa4T0j9k7C93O795//rjI/j503JnUjE=; b=fgXyIXPFCm/LhYEtqdGK467LbSdv8O9k3BYezdYD3zkU4jDTNNiunxwjEdbvAMpXZfXHMc dOYktY+thefwUH6AisnLQPAKEv7rnJ1eILR0wiV/YzXruTy6etFFDqTnjzepAUF0Ud4i1j FQ9hlAsfZysP1+Ii7MZBS7oYI0ErebU= 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-215-sA4AcyYlPQ61-uBbvhJUmg-1; Fri, 25 Oct 2019 06:02:26 -0400 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 1E95C80183D for ; Fri, 25 Oct 2019 10:02:25 +0000 (UTC) Received: from localhost (unknown [10.36.118.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 751FE600D1; Fri, 25 Oct 2019 10:02:16 +0000 (UTC) From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [RFC 2/3] qgraph: add an "after" test callback function Date: Fri, 25 Oct 2019 12:01:51 +0200 Message-Id: <20191025100152.6638-3-stefanha@redhat.com> In-Reply-To: <20191025100152.6638-1-stefanha@redhat.com> References: <20191025100152.6638-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: sA4AcyYlPQ61-uBbvhJUmg-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: Laurent Vivier , Thomas Huth , "Michael S. Tsirkin" , Cornelia Huck , "Dr. David Alan Gilbert" , virtio-fs@redhat.com, Stefan Hajnoczi , Paolo Bonzini , vgoyal@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" Add a callback that runs after a test completes. This will be used for cleanup. Signed-off-by: Stefan Hajnoczi --- tests/libqos/qgraph.h | 2 ++ tests/libqos/qgraph_internal.h | 1 + tests/libqos/qgraph.c | 1 + tests/qos-test.c | 6 ++++++ 4 files changed, 10 insertions(+) diff --git a/tests/libqos/qgraph.h b/tests/libqos/qgraph.h index 3a25dda4b2..5e73a00e4a 100644 --- a/tests/libqos/qgraph.h +++ b/tests/libqos/qgraph.h @@ -47,6 +47,7 @@ typedef void (*QOSStartFunct) (QOSGraphObject *object); /* Test options functions */ typedef void *(*QOSBeforeTest) (GString *cmd_line, void *arg); +typedef void (*QOSAfterTest) (void *arg); /** * SECTION: qgraph.h @@ -341,6 +342,7 @@ struct QOSGraphTestOptions { * additional parameters to the command line * and modify the argument to the test function. */ + QOSAfterTest after; /* executed after the test */ bool subprocess; /* run the test in a subprocess */ }; diff --git a/tests/libqos/qgraph_internal.h b/tests/libqos/qgraph_internal.h index f4734c8681..5e1131f06c 100644 --- a/tests/libqos/qgraph_internal.h +++ b/tests/libqos/qgraph_internal.h @@ -68,6 +68,7 @@ struct QOSGraphNode { QOSTestFunc function; void *arg; QOSBeforeTest before; + QOSAfterTest after; bool subprocess; } test; } u; diff --git a/tests/libqos/qgraph.c b/tests/libqos/qgraph.c index 7a7ae2a19e..f3e792a509 100644 --- a/tests/libqos/qgraph.c +++ b/tests/libqos/qgraph.c @@ -603,6 +603,7 @@ void qos_add_test(const char *name, const char *interface, assert(!opts->edge.size_arg); node->u.test.before = opts->before; + node->u.test.after = opts->after; node->u.test.subprocess = opts->subprocess; node->available = true; add_edge(interface, test_name, QEDGE_CONSUMED_BY, &opts->edge); diff --git a/tests/qos-test.c b/tests/qos-test.c index fd70d73ea5..fa77f661c6 100644 --- a/tests/qos-test.c +++ b/tests/qos-test.c @@ -273,6 +273,7 @@ void *qos_allocate_objects(QTestState *qts, QGuestAllocator **p_alloc) * 3) call all nodes constructor and get_driver/get_device depending on edge, * start the hardware (*_device_enable functions) * 4) start test + * 5) @after test function as defined in the given QOSGraphTestOptions */ static void run_one_test(const void *arg) { @@ -296,6 +297,11 @@ static void run_one_test(const void *arg) obj = qos_allocate_objects(global_qtest, &alloc); test_node->u.test.function(obj, test_arg, alloc); + + /* After test */ + if (test_node->u.test.after) { + test_node->u.test.after(test_arg); + } } static void subprocess_run_one_test(const void *arg) From patchwork Fri Oct 25 10:01:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 11211943 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 A44A7139A for ; Fri, 25 Oct 2019 10:22:39 +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 677BC20578 for ; Fri, 25 Oct 2019 10:22:39 +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="FtYmabqw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 677BC20578 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]:58296 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNwju-0003b8-C2 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 25 Oct 2019 06:22:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33060) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNwRC-0002DL-4s for qemu-devel@nongnu.org; Fri, 25 Oct 2019 06:03:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNwQz-0006JO-U0 for qemu-devel@nongnu.org; Fri, 25 Oct 2019 06:03:14 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:53725 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 1iNwQz-0006DQ-LQ for qemu-devel@nongnu.org; Fri, 25 Oct 2019 06:03:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571997758; 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=eEiJLaIDLLC4oXF/+Pb+mvROnN/JAAxO/m2hAhGux1s=; b=FtYmabqwfKlgEQiIQmuHMW1mhadgojGEK7HLQppHv8VDH8aGCYfPdF75pr+5+bPcb15pai 7wNCYqc044w2OEDD2hfkv+rox/6PQs7JDGfGvDkOxDuOvldkKvDOIrn4x33Cjsr4mCYjkg UTc8VH5N8xd69113rueJKHSuUSpdvCA= 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-34-p9oj9GvgPw6UY4RT7pZUvQ-1; Fri, 25 Oct 2019 06:02:36 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 871C3107AD31 for ; Fri, 25 Oct 2019 10:02:35 +0000 (UTC) Received: from localhost (unknown [10.36.118.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 036B65C1B5; Fri, 25 Oct 2019 10:02:26 +0000 (UTC) From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [RFC 3/3] tests/vhost-user-fs-test: add vhost-user-fs test case Date: Fri, 25 Oct 2019 12:01:52 +0200 Message-Id: <20191025100152.6638-4-stefanha@redhat.com> In-Reply-To: <20191025100152.6638-1-stefanha@redhat.com> References: <20191025100152.6638-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: p9oj9GvgPw6UY4RT7pZUvQ-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: Laurent Vivier , Thomas Huth , "Michael S. Tsirkin" , Cornelia Huck , "Dr. David Alan Gilbert" , virtio-fs@redhat.com, Stefan Hajnoczi , Paolo Bonzini , vgoyal@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" Add a test case for the vhost-user-fs device. There are two limitations: 1. This test only runs when invoked as root. The virtiofsd vhost-user device backend currently requires root in order to maintain accurate file system ownership information (uid/gid). 2. Cross-endian is not supported because virtiofsd currently only supports same-endian configurations. This test uses FUSE_INIT, FUSE_LOOKUP, FUSE_OPEN, FUSE_CREATE, FUSE_READ, FUSE_WRITE, FUSE_RELEASE, and FUSE_FORGET messages to perform basic sanity testing. This test can be expanded on in the future to perform low-level virtio-fs testing, including invalid FUSE messages that are hard to generate from a real guest. Signed-off-by: Stefan Hajnoczi --- tests/Makefile.include | 8 +- tests/libqos/virtio-fs.h | 46 +++ tests/libqos/virtio-fs.c | 104 ++++++ tests/vhost-user-fs-test.c | 660 +++++++++++++++++++++++++++++++++++++ 4 files changed, 816 insertions(+), 2 deletions(-) create mode 100644 tests/libqos/virtio-fs.h create mode 100644 tests/libqos/virtio-fs.c create mode 100644 tests/vhost-user-fs-test.c diff --git a/tests/Makefile.include b/tests/Makefile.include index fde8a0c5ef..0472565d96 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -718,6 +718,7 @@ qos-test-obj-y += tests/libqos/sdhci.o qos-test-obj-y += tests/libqos/tpci200.o qos-test-obj-y += tests/libqos/virtio.o qos-test-obj-$(CONFIG_VIRTFS) += tests/libqos/virtio-9p.o +qos-test-obj-$(CONFIG_VHOST_USER_FS) += tests/libqos/virtio-fs.o qos-test-obj-y += tests/libqos/virtio-balloon.o qos-test-obj-y += tests/libqos/virtio-blk.o qos-test-obj-y += tests/libqos/virtio-mmio.o @@ -759,6 +760,7 @@ qos-test-obj-y += tests/spapr-phb-test.o qos-test-obj-y += tests/tmp105-test.o qos-test-obj-y += tests/usb-hcd-ohci-test.o $(libqos-usb-obj-y) qos-test-obj-$(CONFIG_VHOST_NET_USER) += tests/vhost-user-test.o $(chardev-obj-y) $(test-io-obj-y) +qos-test-obj-$(CONFIG_VHOST_USER_FS) += tests/vhost-user-fs-test.o qos-test-obj-y += tests/virtio-test.o qos-test-obj-$(CONFIG_VIRTFS) += tests/virtio-9p-test.o qos-test-obj-y += tests/virtio-blk-test.o @@ -907,7 +909,8 @@ endef $(patsubst %, check-qtest-%, $(QTEST_TARGETS)): check-qtest-%: %-softmmu/all $(check-qtest-y) $(call do_test_human,$(check-qtest-$*-y) $(check-qtest-generic-y), \ QTEST_QEMU_BINARY=$*-softmmu/qemu-system-$* \ - QTEST_QEMU_IMG=qemu-img$(EXESUF)) + QTEST_QEMU_IMG=qemu-img$(EXESUF) \ + QTEST_VIRTIOFSD=virtiofsd$(EXESUF)) check-unit: $(check-unit-y) $(call do_test_human, $^) @@ -920,7 +923,8 @@ check-speed: $(check-speed-y) $(patsubst %, check-report-qtest-%.tap, $(QTEST_TARGETS)): check-report-qtest-%.tap: %-softmmu/all $(check-qtest-y) $(call do_test_tap, $(check-qtest-$*-y) $(check-qtest-generic-y), \ QTEST_QEMU_BINARY=$*-softmmu/qemu-system-$* \ - QTEST_QEMU_IMG=qemu-img$(EXESUF)) + QTEST_QEMU_IMG=qemu-img$(EXESUF) \ + QTEST_VIRTIOFSD=virtiofsd$(EXESUF)) check-report-unit.tap: $(check-unit-y) $(call do_test_tap,$^) diff --git a/tests/libqos/virtio-fs.h b/tests/libqos/virtio-fs.h new file mode 100644 index 0000000000..40289ba283 --- /dev/null +++ b/tests/libqos/virtio-fs.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifer: GPL-2.0-or-later */ +/* + * libqos virtio-fs device driver + * + * Copyright (C) 2019 Red Hat, Inc. + */ + +#ifndef TESTS_LIBQOS_VIRTIO_FS_H +#define TESTS_LIBQOS_VIRTIO_FS_H + +#include "libqos/virtio-pci.h" + +#define VIRTIO_FS_TAG "myfs" + +typedef struct { + QVirtioDevice *vdev; + QGuestAllocator *alloc; + QVirtQueue *hiprio_vq; + QVirtQueue *request_vq; + uint64_t unique_counter; +} QVirtioFS; + +typedef struct { + QVirtioPCIDevice pci_vdev; + QVirtioFS vfs; +} QVirtioFSPCI; + +typedef struct { + QOSGraphObject obj; + QVirtioFS vfs; +} QVirtioFSDevice; + +static inline uint64_t virtio_fs_get_unique(QVirtioFS *vfs) +{ + /* + * Interrupt requests share the unique ID of the request, except the + * least-significant bit. + * + * Note that unique ID 0 is invalid so we increment right away. + */ + vfs->unique_counter += 2; + + return vfs->unique_counter; +} + +#endif /* TESTS_LIBQOS_VIRTIO_FS_H */ diff --git a/tests/libqos/virtio-fs.c b/tests/libqos/virtio-fs.c new file mode 100644 index 0000000000..47f22d50b9 --- /dev/null +++ b/tests/libqos/virtio-fs.c @@ -0,0 +1,104 @@ +/* SPDX-License-Identifer: GPL-2.0-or-later */ +/* + * libqos virtio-fs device driver + * + * Copyright (C) 2019 Red Hat, Inc. + */ + +#include "qemu/osdep.h" +#include "standard-headers/linux/virtio_fs.h" +#include "libqos/virtio-fs.h" + +static void virtio_fs_cleanup(QVirtioFS *vfs) +{ + QVirtioDevice *vdev = vfs->vdev; + + qvirtqueue_cleanup(vdev->bus, vfs->hiprio_vq, vfs->alloc); + qvirtqueue_cleanup(vdev->bus, vfs->request_vq, vfs->alloc); + vfs->hiprio_vq = NULL; + vfs->request_vq = NULL; +} + +static void virtio_fs_setup(QVirtioFS *vfs) +{ + QVirtioDevice *vdev = vfs->vdev; + uint64_t features; + + features = qvirtio_get_features(vdev); + features &= ~(QVIRTIO_F_BAD_FEATURE | + (1ull << VIRTIO_RING_F_EVENT_IDX)); + qvirtio_set_features(vdev, features); + + vfs->hiprio_vq = qvirtqueue_setup(vdev, vfs->alloc, 0); + vfs->request_vq = qvirtqueue_setup(vdev, vfs->alloc, 1); + + qvirtio_set_driver_ok(vdev); +} + +static void vhost_user_fs_pci_destructor(QOSGraphObject *obj) +{ + QVirtioFSPCI *vfs_pci = (QVirtioFSPCI *)obj; + QVirtioFS *vfs = &vfs_pci->vfs; + + virtio_fs_cleanup(vfs); + qvirtio_pci_destructor(&vfs_pci->pci_vdev.obj); +} + +static void vhost_user_fs_pci_start_hw(QOSGraphObject *obj) +{ + QVirtioFSPCI *vfs_pci = (QVirtioFSPCI *)obj; + QVirtioFS *vfs = &vfs_pci->vfs; + + qvirtio_pci_start_hw(&vfs_pci->pci_vdev.obj); + virtio_fs_setup(vfs); +} + +static void *vhost_user_fs_pci_get_driver(void *object, const char *interface) +{ + QVirtioFSPCI *vfs_pci = object; + + if (g_strcmp0(interface, "virtio-fs") == 0) { + return &vfs_pci->vfs; + } + + fprintf(stderr, "%s not present in virtio-fs\n", interface); + g_assert_not_reached(); +} + +static void *vhost_user_fs_pci_create(void *pci_bus, QGuestAllocator *alloc, void *addr) +{ + QVirtioFSPCI *vfs_pci = g_new0(QVirtioFSPCI, 1); + QVirtioFS *vfs = &vfs_pci->vfs; + QOSGraphObject *obj = &vfs_pci->pci_vdev.obj; + + virtio_pci_init(&vfs_pci->pci_vdev, pci_bus, addr); + vfs->vdev = &vfs_pci->pci_vdev.vdev; + vfs->alloc = alloc; + + g_assert_cmphex(vfs->vdev->device_type, ==, VIRTIO_ID_FS); + + obj->destructor = vhost_user_fs_pci_destructor; + obj->start_hw = vhost_user_fs_pci_start_hw; + obj->get_driver = vhost_user_fs_pci_get_driver; + + return obj; +} + +static void virtio_fs_register_nodes(void) +{ + QOSGraphEdgeOptions opts = { + .extra_device_opts = "chardev=char-virtio-fs,addr=04.0,tag=" VIRTIO_FS_TAG, + .before_cmd_line = "-m 512M -object memory-backend-file,id=mem," + "size=512M,mem-path=/dev/shm,share=on -numa node,memdev=mem", + }; + QPCIAddress addr = { + .devfn = QPCI_DEVFN(4, 0), + }; + + add_qpci_address(&opts, &addr); + qos_node_create_driver("vhost-user-fs-pci", vhost_user_fs_pci_create); + qos_node_consumes("vhost-user-fs-pci", "pci-bus", &opts); + qos_node_produces("vhost-user-fs-pci", "virtio-fs"); +} + +libqos_init(virtio_fs_register_nodes); diff --git a/tests/vhost-user-fs-test.c b/tests/vhost-user-fs-test.c new file mode 100644 index 0000000000..76394adee6 --- /dev/null +++ b/tests/vhost-user-fs-test.c @@ -0,0 +1,660 @@ +/* SPDX-License-Identifer: GPL-2.0-or-later */ +/* + * vhost-user-fs device test + * + * Copyright (C) 2019 Red Hat, Inc. + */ + +#include "qemu/osdep.h" +#include "qemu/bswap.h" +#include "qemu/iov.h" +#include "standard-headers/linux/virtio_fs.h" +#include "standard-headers/linux/fuse.h" +#include "libqos/virtio-fs.h" +#include "libqtest-single.h" + +#define TIMEOUT_US (30 * 1000 * 1000) + +#ifdef HOST_WORDS_BIGENDIAN +static const bool host_is_big_endian = true; +#else +static const bool host_is_big_endian; /* false */ +#endif + +/* + * This macro skips tests when run in a cross-endian configuration. + * virtiofsd does not byte-swap FUSE messages and therefore does not support + * cross-endian. + */ +#define SKIP_TEST_IF_CROSS_ENDIAN() { \ + if (host_is_big_endian != qtest_big_endian(global_qtest)) { \ + g_test_skip("cross-endian is not supported by virtiofsd yet"); \ + return; \ + } \ +} + +static char *socket_path; +static char *shared_dir; + +static bool remove_dir_and_children(const char *path) +{ + GDir *dir; + const gchar *name; + + dir = g_dir_open(path, 0, NULL); + if (!dir) { + return false; + } + + while ((name = g_dir_read_name(dir)) != NULL) { + g_autofree gchar *child = g_strdup_printf("%s/%s", path, name); + + g_test_message("unlinking %s", child); + + if (unlink(child) == -1 && errno == EISDIR) { + remove_dir_and_children(child); + } + } + + g_dir_close(dir); + + g_test_message("rmdir %s", path); + return rmdir(path) == 0; +} + +static void after_test(void *arg G_GNUC_UNUSED) +{ + unlink(socket_path); + + remove_dir_and_children(shared_dir); + + /* + * Both QEMU and virtiofsd need to be restarted after each test and the + * shared directory will be recreated. This ensures isolation between test + * runs. + */ + qos_invalidate_command_line(); +} + +/* Called on SIGABRT */ +static void abrt_handler(void *arg G_GNUC_UNUSED) +{ + after_test(NULL); +} + +static int create_socket(const char *path) +{ + union { + struct sockaddr sa; + struct sockaddr_un un; + } sa; + int fd; + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd < 0) { + g_test_message("socket failed (errno=%d)", errno); + abort(); + } + + unlink(path); /* in case it already exists */ + + sa.un.sun_family = AF_UNIX; + snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), "%s", path); + + if (bind(fd, &sa.sa, sizeof(sa.un)) < 0) { + g_test_message("bind failed (errno=%d)", errno); + abort(); + } + + if (listen(fd, 1) < 0) { + g_test_message("listen failed (errno=%d)", errno); + abort(); + } + + return fd; +} + +static const char *qtest_virtiofsd(void) +{ + const char *virtiofsd_binary; + + virtiofsd_binary = getenv("QTEST_VIRTIOFSD"); + if (!virtiofsd_binary) { + fprintf(stderr, "Environment variable QTEST_VIRTIOFSD required\n"); + exit(1); + } + + return virtiofsd_binary; +} + +/* Launch virtiofsd before each test with an empty shared directory */ +static void *before_test(GString *cmd_line G_GNUC_UNUSED, void *arg) +{ + g_autofree char *command = NULL; + char *virtiofsd_path; + int fd; + pid_t pid; + + fd = create_socket(socket_path); + + if (mkdir(shared_dir, 0777) < 0) { + g_message("mkdir failed (errno=%d)", errno); + abort(); + } + + virtiofsd_path = realpath(qtest_virtiofsd(), NULL); + g_assert_nonnull(virtiofsd_path); + + command = g_strdup_printf("exec %s --fd=%d -o source=%s", + virtiofsd_path, + fd, + shared_dir); + free(virtiofsd_path); + g_test_message("starting virtiofsd: %s", command); + + /* virtiofsd terminates when QEMU closes the vhost-user socket connection, + * so there is no need to kill it explicitly later on. + */ + pid = fork(); + g_assert_cmpint(pid, >=, 0); + if (pid == 0) { + execlp("/bin/sh", "sh", "-c", command, NULL); + exit(1); + } + + close(fd); + + return arg; +} + +/* + * Send scatter-gather lists on the request virtqueue and return the number of + * bytes filled by the device. + * + * Note that in/out have opposite meanings in FUSE and VIRTIO. This function + * uses VIRTIO terminology (out - to device, in - from device). + */ +static uint32_t do_request(QVirtioFS *vfs, QTestState *qts, + struct iovec *sg_out, unsigned out_num, + struct iovec *sg_in, unsigned in_num) +{ + QVirtioDevice *dev = vfs->vdev; + QVirtQueue *vq = vfs->request_vq; + size_t out_bytes = iov_size(sg_out, out_num); + size_t in_bytes = iov_size(sg_in, in_num); + uint64_t out_addr; + uint64_t in_addr; + uint64_t addr; + uint32_t head = 0; + uint32_t nfilled; + unsigned i; + + g_assert_cmpint(out_num, >, 0); + g_assert_cmpint(in_num, >, 0); + + /* Add out buffers */ + addr = out_addr = guest_alloc(vfs->alloc, out_bytes); + for (i = 0; i < out_num; i++) { + size_t len = sg_out[i].iov_len; + uint32_t desc_idx; + bool first = i == 0; + + qtest_memwrite(qts, addr, sg_out[i].iov_base, len); + desc_idx = qvirtqueue_add(qts, vq, addr, len, false, true); + + if (first) { + head = desc_idx; + } + + addr += len; + } + + /* Add in buffers */ + addr = in_addr = guest_alloc(vfs->alloc, in_bytes); + for (i = 0; i < in_num; i++) { + size_t len = sg_in[i].iov_len; + bool next = i != in_num - 1; + + qvirtqueue_add(qts, vq, addr, len, true, next); + + addr += len; + } + + /* Process the request */ + qvirtqueue_kick(qts, dev, vq, head); + qvirtio_wait_used_elem(qts, dev, vq, head, &nfilled, TIMEOUT_US); + + /* Copy in buffers back */ + addr = in_addr; + for (i = 0; i < in_num; i++) { + size_t len = sg_in[i].iov_len; + + qtest_memread(qts, addr, sg_in[i].iov_base, len); + addr += len; + } + + guest_free(vfs->alloc, in_addr); + guest_free(vfs->alloc, out_addr); + + return nfilled; +} + +/* Byte-swap values if host endianness differs from guest */ +static uint32_t guest32(uint32_t val) +{ + if (qtest_big_endian(global_qtest) != host_is_big_endian) { + return bswap32(val); + } + return val; +} + +static uint64_t guest64(uint64_t val) +{ + if (qtest_big_endian(global_qtest) != host_is_big_endian) { + return bswap64(val); + } + return val; +} + +/* Make a FUSE_INIT request */ +static void fuse_init(QVirtioFS *vfs) +{ + struct fuse_in_header in_hdr = { + .opcode = guest32(FUSE_INIT), + .unique = guest64(virtio_fs_get_unique(vfs)), + }; + struct fuse_init_in in = { + .major = guest32(FUSE_KERNEL_VERSION), + .minor = guest32(FUSE_KERNEL_MINOR_VERSION), + }; + struct iovec sg_in[] = { + { .iov_base = &in_hdr, .iov_len = sizeof(in_hdr) }, + { .iov_base = &in, .iov_len = sizeof(in) }, + }; + struct fuse_out_header out_hdr; + struct fuse_init_out out; + struct iovec sg_out[] = { + { .iov_base = &out_hdr, .iov_len = sizeof(out_hdr) }, + { .iov_base = &out, .iov_len = sizeof(out) }, + }; + + in_hdr.len = guest32(iov_size(sg_in, G_N_ELEMENTS(sg_in))); + + do_request(vfs, global_qtest, sg_in, G_N_ELEMENTS(sg_in), + sg_out, G_N_ELEMENTS(sg_out)); + + g_assert_cmpint(guest32(out_hdr.error), ==, 0); + g_assert_cmpint(guest32(out.major), ==, FUSE_KERNEL_VERSION); +} + +/* Look up a directory entry by name using FUSE_LOOKUP */ +static int32_t fuse_lookup(QVirtioFS *vfs, uint64_t parent, const char *name, + struct fuse_entry_out *entry) +{ + struct fuse_in_header in_hdr = { + .opcode = guest32(FUSE_LOOKUP), + .unique = guest64(virtio_fs_get_unique(vfs)), + .nodeid = guest64(parent), + }; + struct iovec sg_in[] = { + { .iov_base = &in_hdr, .iov_len = sizeof(in_hdr) }, + { .iov_base = (void *)name, .iov_len = strlen(name) + 1 }, + }; + struct fuse_out_header out_hdr; + struct iovec sg_out[] = { + { .iov_base = &out_hdr, .iov_len = sizeof(out_hdr) }, + { .iov_base = entry, .iov_len = sizeof(*entry) }, + }; + + in_hdr.len = guest32(iov_size(sg_in, G_N_ELEMENTS(sg_in))); + + do_request(vfs, global_qtest, sg_in, G_N_ELEMENTS(sg_in), + sg_out, G_N_ELEMENTS(sg_out)); + + return guest32(out_hdr.error); +} + +/* Open a file by nodeid using FUSE_OPEN */ +static int32_t fuse_open(QVirtioFS *vfs, uint64_t nodeid, uint32_t flags, + uint64_t *fh) +{ + struct fuse_in_header in_hdr = { + .opcode = guest32(FUSE_OPEN), + .unique = guest64(virtio_fs_get_unique(vfs)), + .nodeid = guest64(nodeid), + }; + struct fuse_open_in in = { + .flags = guest32(flags), + }; + struct iovec sg_in[] = { + { .iov_base = &in_hdr, .iov_len = sizeof(in_hdr) }, + { .iov_base = &in, .iov_len = sizeof(in) }, + }; + struct fuse_out_header out_hdr; + struct fuse_open_out out; + struct iovec sg_out[] = { + { .iov_base = &out_hdr, .iov_len = sizeof(out_hdr) }, + { .iov_base = &out, .iov_len = sizeof(out) }, + }; + int32_t error; + + in_hdr.len = guest32(iov_size(sg_in, G_N_ELEMENTS(sg_in))); + + do_request(vfs, global_qtest, sg_in, G_N_ELEMENTS(sg_in), + sg_out, G_N_ELEMENTS(sg_out)); + + error = guest32(out_hdr.error); + if (!error) { + *fh = guest64(out.fh); + } else { + *fh = 0; + } + return error; +} + +/* Create a file using FUSE_CREATE */ +static int32_t fuse_create(QVirtioFS *vfs, uint64_t parent, const char *name, + uint32_t mode, uint32_t flags, + uint64_t *nodeid, uint64_t *fh) +{ + struct fuse_in_header in_hdr = { + .opcode = guest32(FUSE_CREATE), + .unique = guest64(virtio_fs_get_unique(vfs)), + .nodeid = guest64(parent), + }; + struct fuse_create_in in = { + .flags = guest32(flags), + .mode = guest32(mode), + .umask = guest32(0002), + }; + struct iovec sg_in[] = { + { .iov_base = &in_hdr, .iov_len = sizeof(in_hdr) }, + { .iov_base = &in, .iov_len = sizeof(in) }, + { .iov_base = (void *)name, .iov_len = strlen(name) + 1 }, + }; + struct fuse_out_header out_hdr; + struct fuse_entry_out entry; + struct fuse_open_out out; + struct iovec sg_out[] = { + { .iov_base = &out_hdr, .iov_len = sizeof(out_hdr) }, + { .iov_base = &entry, .iov_len = sizeof(entry) }, + { .iov_base = &out, .iov_len = sizeof(out) }, + }; + int32_t error; + + in_hdr.len = guest32(iov_size(sg_in, G_N_ELEMENTS(sg_in))); + + do_request(vfs, global_qtest, sg_in, G_N_ELEMENTS(sg_in), + sg_out, G_N_ELEMENTS(sg_out)); + + error = guest32(out_hdr.error); + if (!error) { + *nodeid = guest64(entry.nodeid); + *fh = guest64(out.fh); + } else { + *nodeid = 0; + *fh = 0; + } + return error; +} + +/* Read bytes from a file using FILE_READ */ +static ssize_t fuse_read(QVirtioFS *vfs, uint64_t fh, uint64_t offset, + void *buf, size_t len) +{ + struct fuse_in_header in_hdr = { + .opcode = guest32(FUSE_READ), + .unique = guest64(virtio_fs_get_unique(vfs)), + }; + struct fuse_read_in in = { + .fh = guest64(fh), + .offset = guest64(offset), + .size = guest32(len), + }; + struct iovec sg_in[] = { + { .iov_base = &in_hdr, .iov_len = sizeof(in_hdr) }, + { .iov_base = &in, .iov_len = sizeof(in) }, + }; + struct fuse_out_header out_hdr; + struct iovec sg_out[] = { + { .iov_base = &out_hdr, .iov_len = sizeof(out_hdr) }, + { .iov_base = buf, .iov_len = len }, + }; + uint32_t nread; + + in_hdr.len = guest32(iov_size(sg_in, G_N_ELEMENTS(sg_in))); + + nread = do_request(vfs, global_qtest, sg_in, G_N_ELEMENTS(sg_in), + sg_out, G_N_ELEMENTS(sg_out)); + g_assert_cmpint(guest32(out_hdr.error), ==, 0); + + return nread - sizeof(out_hdr); +} + +/* Write bytes to a file using FILE_WRITE */ +static ssize_t fuse_write(QVirtioFS *vfs, uint64_t fh, uint64_t offset, + const void *buf, size_t len) +{ + struct fuse_in_header in_hdr = { + .opcode = guest32(FUSE_WRITE), + .unique = guest64(virtio_fs_get_unique(vfs)), + }; + struct fuse_write_in in = { + .fh = guest64(fh), + .offset = guest64(offset), + .size = guest32(len), + }; + struct iovec sg_in[] = { + { .iov_base = &in_hdr, .iov_len = sizeof(in_hdr) }, + { .iov_base = &in, .iov_len = sizeof(in) }, + { .iov_base = (void *)buf, .iov_len = len }, + }; + struct fuse_out_header out_hdr; + struct fuse_write_out out; + struct iovec sg_out[] = { + { .iov_base = &out_hdr, .iov_len = sizeof(out_hdr) }, + { .iov_base = &out, .iov_len = sizeof(out) }, + }; + + in_hdr.len = guest32(iov_size(sg_in, G_N_ELEMENTS(sg_in))); + + do_request(vfs, global_qtest, sg_in, G_N_ELEMENTS(sg_in), + sg_out, G_N_ELEMENTS(sg_out)); + g_assert_cmpint(guest32(out_hdr.error), ==, 0); + + return guest32(out.size); +} + +/* Close a file handle using FUSE_RELEASE */ +static void fuse_release(QVirtioFS *vfs, uint64_t fh) +{ + struct fuse_in_header in_hdr = { + .opcode = guest32(FUSE_RELEASE), + .unique = guest64(virtio_fs_get_unique(vfs)), + }; + struct fuse_release_in in = { + .fh = guest64(fh), + }; + struct iovec sg_in[] = { + { .iov_base = &in_hdr, .iov_len = sizeof(in_hdr) }, + { .iov_base = &in, .iov_len = sizeof(in) }, + }; + struct fuse_out_header out_hdr; + struct iovec sg_out[] = { + { .iov_base = &out_hdr, .iov_len = sizeof(out_hdr) }, + }; + + in_hdr.len = guest32(iov_size(sg_in, G_N_ELEMENTS(sg_in))); + + do_request(vfs, global_qtest, sg_in, G_N_ELEMENTS(sg_in), + sg_out, G_N_ELEMENTS(sg_out)); + + g_assert_cmpint(guest32(out_hdr.error), ==, 0); +} + +/* Drop an inode reference using FUSE_FORGET */ +static void fuse_forget(QVirtioFS *vfs, uint64_t nodeid) +{ + struct fuse_in_header in_hdr = { + .opcode = guest32(FUSE_FORGET), + .unique = guest64(virtio_fs_get_unique(vfs)), + .nodeid = guest64(nodeid), + }; + struct fuse_forget_in in = { + .nlookup = guest64(1), + }; + struct iovec sg_in[] = { + { .iov_base = &in_hdr, .iov_len = sizeof(in_hdr) }, + { .iov_base = &in, .iov_len = sizeof(in) }, + }; + struct fuse_out_header out_hdr; + struct iovec sg_out[] = { + { .iov_base = &out_hdr, .iov_len = sizeof(out_hdr) }, + }; + + in_hdr.len = guest32(iov_size(sg_in, G_N_ELEMENTS(sg_in))); + + do_request(vfs, global_qtest, sg_in, G_N_ELEMENTS(sg_in), + sg_out, G_N_ELEMENTS(sg_out)); + + g_assert_cmpint(guest32(out_hdr.error), ==, 0); +} + +/* Check contents of VIRTIO Configuration Space */ +static void test_config(void *parent, void *arg, QGuestAllocator *alloc) +{ + QVirtioFS *vfs = parent; + size_t i; + uint32_t num_request_queues; + char tag[37]; + + SKIP_TEST_IF_CROSS_ENDIAN(); + + for (i = 0; i < sizeof(tag) - 1; i++) { + tag[i] = qvirtio_config_readw(vfs->vdev, i); + } + tag[36] = '\0'; + + g_assert_cmpstr(tag, ==, VIRTIO_FS_TAG); + + num_request_queues = qvirtio_config_readl(vfs->vdev, + offsetof(struct virtio_fs_config, num_request_queues)); + + g_assert_cmpint(num_request_queues, ==, 1); +} + +/* Create file on host and check its contents and metadata in guest */ +static void test_file_from_host(void *parent, void *arg, QGuestAllocator *alloc) +{ + g_autofree gchar *filename = g_strdup_printf("%s/%s", shared_dir, "foo"); + const char *str = "This is a test\n"; + char buf[strlen(str)]; + QVirtioFS *vfs = parent; + struct fuse_entry_out entry; + int32_t error; + uint64_t nodeid; + uint64_t fh; + ssize_t nread; + gboolean ok; + + SKIP_TEST_IF_CROSS_ENDIAN(); + + /* Create the test file in the shared directory */ + ok = g_file_set_contents(filename, str, strlen(str), NULL); + g_assert(ok); + + fuse_init(vfs); + + error = fuse_lookup(vfs, FUSE_ROOT_ID, "foo", &entry); + g_assert_cmpint(error, ==, 0); + g_assert_cmpint(guest64(entry.attr.size), ==, strlen(str)); + nodeid = guest64(entry.nodeid); + + error = fuse_open(vfs, nodeid, O_RDONLY, &fh); + g_assert_cmpint(error, ==, 0); + + nread = fuse_read(vfs, fh, 0, buf, sizeof(buf)); + g_assert_cmpint(nread, ==, sizeof(buf)); + g_assert_cmpint(memcmp(buf, str, sizeof(buf)), ==, 0); + + fuse_release(vfs, fh); + fuse_forget(vfs, nodeid); +} + +/* Create file from host and check its contents and metadata on host */ +static void test_file_from_guest(void *parent, void *arg, + QGuestAllocator *alloc) +{ + g_autofree gchar *filename = g_strdup_printf("%s/%s", shared_dir, "foo"); + const char *str = "This is a test\n"; + gchar *contents = NULL; + gsize length = 0; + QVirtioFS *vfs = parent; + int32_t error; + uint64_t nodeid; + uint64_t fh; + ssize_t nwritten; + gboolean ok; + + SKIP_TEST_IF_CROSS_ENDIAN(); + + fuse_init(vfs); + + error = fuse_create(vfs, FUSE_ROOT_ID, "foo", 0644, O_CREAT | O_WRONLY, + &nodeid, &fh); + g_assert_cmpint(error, ==, 0); + + nwritten = fuse_write(vfs, fh, 0, str, strlen(str)); + g_assert_cmpint(nwritten, ==, strlen(str)); + + fuse_release(vfs, fh); + fuse_forget(vfs, nodeid); + + /* Check the file on the host */ + ok = g_file_get_contents(filename, &contents, &length, NULL); + g_assert(ok); + g_assert_cmpint(length, ==, strlen(str)); + g_assert_cmpint(memcmp(contents, str, strlen(str)), ==, 0); + g_free(contents); +} + +static void register_vhost_user_fs_test(void) +{ + g_autofree gchar *cmd_line = + g_strdup_printf("-chardev socket,id=char-virtio-fs,path=%s", + socket_path); + QOSGraphTestOptions opts = { + .edge.before_cmd_line = cmd_line, + .before = before_test, + .after = after_test, + }; + + if (geteuid() != 0) { + g_test_message("Skipping vhost-user-fs tests because root is " + "required for virtiofsd"); + return; + } + + qtest_add_abrt_handler(abrt_handler, NULL); + + qos_add_test("config", "virtio-fs", test_config, &opts); + qos_add_test("file-from-host", "virtio-fs", test_file_from_host, &opts); + qos_add_test("file-from-guest", "virtio-fs", test_file_from_guest, &opts); +} + +libqos_init(register_vhost_user_fs_test); + +static void __attribute__((constructor)) init_paths(void) +{ + socket_path = g_strdup_printf("/tmp/qtest-%d-vhost-fs.sock", getpid()); + shared_dir = g_strdup_printf("/tmp/qtest-%d-virtio-fs-dir", getpid()); +} + +static void __attribute__((destructor)) destroy_paths(void) +{ + g_free(shared_dir); + shared_dir = NULL; + + g_free(socket_path); + socket_path = NULL; +}