From nobody Mon Sep 16 19:14:12 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 1707411481179509.0741309730321; Thu, 8 Feb 2024 08:58:01 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.678371.1055635 (Exim 4.92) (envelope-from ) id 1rY7if-0002wI-P8; Thu, 08 Feb 2024 16:57:49 +0000 Received: by outflank-mailman (output) from mailman id 678371.1055635; Thu, 08 Feb 2024 16:57:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rY7if-0002vX-LE; Thu, 08 Feb 2024 16:57:49 +0000 Received: by outflank-mailman (input) for mailman id 678371; Thu, 08 Feb 2024 16:57:48 +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 1rY7hk-0005bD-8A for xen-devel@lists.xenproject.org; Thu, 08 Feb 2024 16:56:52 +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 0de5890d-c6a3-11ee-8a4a-1f161083a0e0; Thu, 08 Feb 2024 17:56:51 +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 E6A5821F60; Thu, 8 Feb 2024 16:56:50 +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 AB4C41326D; Thu, 8 Feb 2024 16:56:50 +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 XcdVKNIHxWUPFAAAD6G6ig (envelope-from ); Thu, 08 Feb 2024 16:56:50 +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: 0de5890d-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 11/22] tools/9pfsd: add 9pfs create request support Date: Thu, 8 Feb 2024 17:55:35 +0100 Message-Id: <20240208165546.5715-12-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 X-Spam-Level: Authentication-Results: smtp-out1.suse.de; none X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Spam-Score: -4.00 X-Rspamd-Queue-Id: E6A5821F60 X-Spam-Flag: NO X-ZM-MESSAGEID: 1707411483367100001 Content-Type: text/plain; charset="utf-8" Add the create request of the 9pfs protocol. Signed-off-by: Juergen Gross Acked-by: Anthony PERARD Reviewed-by: Jason Andryuk --- V2: - set permissions correctly (Jason Andryuk) V3: - use opendirat() etc. (Jason Andryuk) - rework error handling a little bit --- tools/9pfsd/io.c | 151 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/tools/9pfsd/io.c b/tools/9pfsd/io.c index 74efd21c10..583eea6059 100644 --- a/tools/9pfsd/io.c +++ b/tools/9pfsd/io.c @@ -32,6 +32,7 @@ #define P9_CMD_ERROR 107 #define P9_CMD_WALK 110 #define P9_CMD_OPEN 112 +#define P9_CMD_CREATE 114 #define P9_CMD_CLUNK 120 =20 /* P9 protocol open flags. */ @@ -42,6 +43,12 @@ #define P9_OTRUNC 0x10 /* or'ed in, truncate file first */ #define P9_OREMOVE 0x40 /* or'ed in, remove file after clunk */ =20 +/* P9 protocol create permission masks. */ +#define P9_CREATE_PERM_DIR 0x80000000 +#define P9_CREATE_PERM_NOTSUPP 0x03b00000 /* link, symlink, ... */ +#define P9_CREATE_PERM_DIR_MASK 0777 +#define P9_CREATE_PERM_FILE_MASK 0666 + #define P9_MIN_MSIZE 2048 #define P9_VERSION "9P2000.u" #define P9_WALK_MAXELEM 16 @@ -963,6 +970,146 @@ static void p9_open(struct ring *ring, struct p9_head= er *hdr) p9_error(ring, hdr->tag, errno); } =20 +static void p9_create(struct ring *ring, struct p9_header *hdr) +{ + device *device =3D ring->device; + uint32_t fid; + unsigned int name_off; + uint32_t perm; + uint8_t mode; + unsigned int ext_off; + struct p9_fid *fidp; + struct p9_fid *new_fidp; + char *path; + struct stat st; + struct p9_qid qid; + uint32_t iounit; + int flags; + int ret; + + ret =3D fill_data(ring, "USUbS", &fid, &name_off, &perm, &mode, &ext_o= ff); + if ( ret !=3D 5 ) + { + p9_error(ring, hdr->tag, EINVAL); + return; + } + + if ( !name_ok(ring->str + name_off) ) + { + p9_error(ring, hdr->tag, ENOENT); + return; + } + + if ( perm & P9_CREATE_PERM_NOTSUPP ) + { + p9_error(ring, hdr->tag, EINVAL); + return; + } + + fidp =3D get_fid_ref(device, fid); + if ( !fidp || fidp->opened ) + { + free_fid(device, fidp); + p9_error(ring, hdr->tag, EINVAL); + return; + } + if ( fstatat(device->root_fd, fidp->path, &st, 0) < 0 ) + { + free_fid(device, fidp); + p9_error(ring, hdr->tag, errno); + return; + } + + path =3D malloc(strlen(fidp->path) + strlen(ring->str + name_off) + 2); + if ( !path ) + { + free_fid(device, fidp); + p9_error(ring, hdr->tag, ENOMEM); + return; + } + sprintf(path, "%s/%s", fidp->path, ring->str + name_off); + new_fidp =3D alloc_fid_mem(device, fid, path); + free(path); + if ( !new_fidp ) + { + free_fid(device, fidp); + p9_error(ring, hdr->tag, ENOMEM); + return; + } + + pthread_mutex_lock(&device->fid_mutex); + + new_fidp->ref =3D fidp->ref; + + if ( perm & P9_CREATE_PERM_DIR ) + { + perm &=3D P9_CREATE_PERM_DIR_MASK & st.st_mode; + if ( mode !=3D P9_OREAD ) + { + errno =3D EINVAL; + goto err; + } + if ( mkdirat(device->root_fd, new_fidp->path, perm) < 0 ) + goto err; + + XEN_TAILQ_REMOVE(&device->fids, fidp, list); + XEN_TAILQ_INSERT_HEAD(&device->fids, new_fidp, list); + free(fidp); + fidp =3D new_fidp; + new_fidp =3D NULL; + + 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 ) + { + errno =3D EINVAL; + goto err; + } + perm &=3D P9_CREATE_PERM_FILE_MASK & st.st_mode; + + XEN_TAILQ_REMOVE(&device->fids, fidp, list); + XEN_TAILQ_INSERT_HEAD(&device->fids, new_fidp, list); + free(fidp); + fidp =3D new_fidp; + new_fidp =3D NULL; + + fidp->fd =3D openat(device->root_fd, fidp->path, flags | O_CREAT |= O_EXCL, + perm); + if ( fidp->fd < 0 ) + goto err; + } + + if ( fstatat(device->root_fd, fidp->path, &st, 0) < 0 ) + goto err; + + fill_qid(device, fidp->path, &qid, &st); + iounit =3D get_iounit(ring, &st); + fidp->opened =3D true; + fidp->mode =3D mode; + + pthread_mutex_unlock(&device->fid_mutex); + + fill_buffer(ring, hdr->cmd + 1, hdr->tag, "QU", &qid, &iounit); + + return; + + err: + p9_error(ring, hdr->tag, errno); + + pthread_mutex_unlock(&device->fid_mutex); + + free(new_fidp); + free_fid(device, fidp); +} + static void p9_clunk(struct ring *ring, struct p9_header *hdr) { device *device =3D ring->device; @@ -1072,6 +1219,10 @@ void *io_thread(void *arg) p9_open(ring, &hdr); break; =20 + case P9_CMD_CREATE: + p9_create(ring, &hdr); + break; + case P9_CMD_CLUNK: p9_clunk(ring, &hdr); break; --=20 2.35.3