From nobody Mon Sep 16 19:22:07 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=quarantine dis=quarantine) header.from=suse.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1707411534231911.4912333837063; Thu, 8 Feb 2024 08:58:54 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.678374.1055670 (Exim 4.92) (envelope-from ) id 1rY7jT-0004vY-Kt; Thu, 08 Feb 2024 16:58:39 +0000 Received: by outflank-mailman (output) from mailman id 678374.1055670; Thu, 08 Feb 2024 16:58:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rY7jT-0004vP-Gc; Thu, 08 Feb 2024 16:58:39 +0000 Received: by outflank-mailman (input) for mailman id 678374; Thu, 08 Feb 2024 16:58:38 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rY7hY-0005bD-Ip for xen-devel@lists.xenproject.org; Thu, 08 Feb 2024 16:56:40 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 072fc20f-c6a3-11ee-8a4a-1f161083a0e0; Thu, 08 Feb 2024 17:56:40 +0100 (CET) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id A7D4021F60; Thu, 8 Feb 2024 16:56:39 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 7A0D31326D; Thu, 8 Feb 2024 16:56:39 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id udg8HMcHxWX7EwAAD6G6ig (envelope-from ); Thu, 08 Feb 2024 16:56:39 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 072fc20f-c6a3-11ee-8a4a-1f161083a0e0 From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Anthony PERARD , Jason Andryuk Subject: [PATCH v5 09/22] tools/9pfsd: add 9pfs open request support Date: Thu, 8 Feb 2024 17:55:33 +0100 Message-Id: <20240208165546.5715-10-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20240208165546.5715-1-jgross@suse.com> References: <20240208165546.5715-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Authentication-Results: smtp-out1.suse.de; none X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Rspamd-Queue-Id: A7D4021F60 X-Spam-Level: X-Spam-Score: -4.00 X-Spam-Flag: NO X-ZM-MESSAGEID: 1707411535631100001 Content-Type: text/plain; charset="utf-8" Add the open request of the 9pfs protocol. Signed-off-by: Juergen Gross Acked-by: Anthony PERARD Reviewed-by: Jason Andryuk --- V2: - don't allow to open symbolic link V3: - use openat() (Jason Andryuk) - use common error handling in p9_open() --- tools/9pfsd/io.c | 137 ++++++++++++++++++++++++++++++++++++++++ tools/9pfsd/xen-9pfsd.h | 4 ++ 2 files changed, 141 insertions(+) diff --git a/tools/9pfsd/io.c b/tools/9pfsd/io.c index 5b827706b7..2e2275e8ab 100644 --- a/tools/9pfsd/io.c +++ b/tools/9pfsd/io.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include /* For cpu barriers. */ #include =20 @@ -29,6 +31,15 @@ #define P9_CMD_ATTACH 104 #define P9_CMD_ERROR 107 #define P9_CMD_WALK 110 +#define P9_CMD_OPEN 112 + +/* P9 protocol open flags. */ +#define P9_OREAD 0 /* read */ +#define P9_OWRITE 1 /* write */ +#define P9_ORDWR 2 /* read and write */ +#define P9_OMODEMASK 0x03 +#define P9_OTRUNC 0x10 /* or'ed in, truncate file first */ +#define P9_OREMOVE 0x40 /* or'ed in, remove file after clunk */ =20 #define P9_MIN_MSIZE 2048 #define P9_VERSION "9P2000.u" @@ -829,6 +840,128 @@ static void p9_walk(struct ring *ring, struct p9_head= er *hdr) free(names); } =20 +static int open_flags_from_mode(uint8_t mode) +{ + int flags; + + switch ( mode & P9_OMODEMASK ) + { + case P9_OREAD: + flags =3D O_RDONLY; + break; + + case P9_OWRITE: + flags =3D O_WRONLY; + break; + + case P9_ORDWR: + flags =3D O_RDWR; + break; + + default: + errno =3D EINVAL; + return -1; + } + + if ( mode & P9_OTRUNC ) + flags |=3D O_TRUNC; + + return flags; +} + +static unsigned int get_iounit(struct ring *ring, struct stat *st) +{ + return (ring->max_size - st->st_blksize) & ~(st->st_blksize - 1); +} + +static void p9_open(struct ring *ring, struct p9_header *hdr) +{ + device *device =3D ring->device; + uint32_t fid; + uint8_t mode; + struct p9_fid *fidp; + struct stat st; + struct p9_qid qid; + uint32_t iounit; + int flags; + int ret; + + ret =3D fill_data(ring, "Ub", &fid, &mode); + if ( ret !=3D 2 ) + { + p9_error(ring, hdr->tag, EINVAL); + return; + } + if ( mode & ~(P9_OMODEMASK | P9_OTRUNC | P9_OREMOVE) ) + { + p9_error(ring, hdr->tag, EINVAL); + return; + } + + fidp =3D get_fid_ref(device, fid); + if ( !fidp ) + { + p9_error(ring, hdr->tag, ENOENT); + return; + } + if ( fidp->opened ) + { + errno =3D EINVAL; + goto err; + } + + if ( fstatat(device->root_fd, fidp->path, &st, 0) < 0 ) + { + errno =3D ENOENT; + goto err; + } + + if ( S_ISLNK(st.st_mode) ) + { + errno =3D EMLINK; + goto err; + } + + fidp->isdir =3D S_ISDIR(st.st_mode); + fidp->mode =3D mode; + if ( fidp->isdir ) + { + if ( mode !=3D P9_OREAD ) + { + errno =3D EINVAL; + goto err; + } + fidp->fd =3D openat(device->root_fd, fidp->path, O_RDONLY); + if ( fidp->fd < 0 ) + goto err; + fidp->data =3D fdopendir(fidp->fd); + if ( !fidp->data ) + goto err; + } + else + { + flags =3D open_flags_from_mode(mode); + if ( flags < 0 ) + goto err; + + fidp->fd =3D openat(device->root_fd, fidp->path, flags); + if ( fidp->fd < 0 ) + goto err; + } + + fill_qid(device, fidp->path, &qid, &st); + iounit =3D get_iounit(ring, &st); + fidp->opened =3D true; + + fill_buffer(ring, hdr->cmd + 1, hdr->tag, "QU", &qid, &iounit); + + return; + + err: + free_fid(device, fidp); + p9_error(ring, hdr->tag, errno); +} + void *io_thread(void *arg) { struct ring *ring =3D arg; @@ -896,6 +1029,10 @@ void *io_thread(void *arg) p9_walk(ring, &hdr); break; =20 + case P9_CMD_OPEN: + p9_open(ring, &hdr); + break; + default: syslog(LOG_DEBUG, "%u.%u sent unhandled command %u\n", ring->device->domid, ring->device->devid, hdr.cmd); diff --git a/tools/9pfsd/xen-9pfsd.h b/tools/9pfsd/xen-9pfsd.h index f01fffb0bb..757be2da4b 100644 --- a/tools/9pfsd/xen-9pfsd.h +++ b/tools/9pfsd/xen-9pfsd.h @@ -25,7 +25,11 @@ struct p9_fid { XEN_TAILQ_ENTRY(struct p9_fid) list; unsigned int fid; unsigned int ref; + int fd; + uint8_t mode; bool opened; + bool isdir; + void *data; /* File type specific. */ char path[]; }; =20 --=20 2.35.3