From nobody Thu Apr 25 12:35:40 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; dkim=pass; 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=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1641913965; cv=none; d=zohomail.com; s=zohoarc; b=lAs4NonO3epnxn+je5AF2LZG3dffcqg6G2V0oe0k0XugK7Gevm9eTx7L3pQFphzD8j/jSHfjhW3TfzA1z0vr0gPsuLrC0umEDcg0awLAxkY5Dx3LcU2rPWBbQDUJQI2RbeYQ3XydURpRZv6M8jliqfapymrv4Y5KmtooAfIzQTw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1641913965; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=THtUtk0c0qdaxelPl/C+8VH9LB692eb/zBc/uFOuwwQ=; b=iDNjJv2hS54XNpQ44NVDGC4msCKVBxaGDeB80GUyZKNR7ET+SmM1BMwFh2j3QUBvREbdztgQ6GS4Rux5MRcYybK+ZuCAdDQWIC8DWxY60Yrq/fcAk1UJKOc3eX7vEVdl3WRx8IThAxOQar0rvkAIZ0XmYxm7aESo/Qjv9kmxQTw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1641913965663783.4778758876887; Tue, 11 Jan 2022 07:12:45 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.255880.438818 (Exim 4.92) (envelope-from ) id 1n7Ip7-0004yz-44; Tue, 11 Jan 2022 15:12:33 +0000 Received: by outflank-mailman (output) from mailman id 255880.438818; Tue, 11 Jan 2022 15:12:33 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1n7Ip6-0004rz-JQ; Tue, 11 Jan 2022 15:12:32 +0000 Received: by outflank-mailman (input) for mailman id 255880; Tue, 11 Jan 2022 15:12:28 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1n7Ip1-0002Fw-Dw for xen-devel@lists.xenproject.org; Tue, 11 Jan 2022 15:12:27 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id ddce9212-72f0-11ec-81c1-a30af7de8005; Tue, 11 Jan 2022 16:12:18 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 10C69212CA; Tue, 11 Jan 2022 15:12:18 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id DECFF13DDD; Tue, 11 Jan 2022 15:12:17 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 0FpANVGe3WG8AwAAMHmgww (envelope-from ); Tue, 11 Jan 2022 15:12:17 +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: ddce9212-72f0-11ec-81c1-a30af7de8005 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1641913938; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=THtUtk0c0qdaxelPl/C+8VH9LB692eb/zBc/uFOuwwQ=; b=vKJEN28JIArVqpWVZTeqYRy4g4OU9gH+pS+WPLnZmfXn4JtfTQnaXR4xV1gx1rOmKlrH7j mQBXhesIVlqrukqW5u5OqsQpmcAF9tRbuQ6CqiRH7FtEKMPRc6L0+MF+1ED1KtsJ1BFNpB /0SYyHP/lOWL8f4N6KqIurpds9GOLqg= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, wl@xen.org, Juergen Gross Subject: [PATCH v2 06/12] mini-os: use alloc_file_type() and get_file_from_fd() in blkfront Date: Tue, 11 Jan 2022 16:12:09 +0100 Message-Id: <20220111151215.22955-7-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20220111151215.22955-1-jgross@suse.com> References: <20220111151215.22955-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1641913967283100003 Content-Type: text/plain; charset="utf-8" Allocate the file type dynamically via alloc_file_type(). Instead of directly accessing the files[] array use get_file_from_fd(). Signed-off-by: Juergen Gross Reviewed-by: Samuel Thibault --- blkfront.c | 92 ++++++++++++++++++++++++++++++++++------------ include/blkfront.h | 5 --- include/lib.h | 3 +- lib/sys.c | 24 ------------ 4 files changed, 69 insertions(+), 55 deletions(-) diff --git a/blkfront.c b/blkfront.c index e3f42be..b7ea1d7 100644 --- a/blkfront.c +++ b/blkfront.c @@ -59,10 +59,10 @@ void blkfront_handler(evtchn_port_t port, struct pt_reg= s *regs, void *data) { #ifdef HAVE_LIBC struct blkfront_dev *dev =3D data; - int fd =3D dev->fd; + struct file *file =3D get_file_from_fd(dev->fd); =20 - if (fd !=3D -1) - files[fd].read =3D true; + if ( file ) + file->read =3D true; #endif wake_up(&blkfront_queue); } @@ -483,9 +483,13 @@ int blkfront_aio_poll(struct blkfront_dev *dev) =20 moretodo: #ifdef HAVE_LIBC - if (dev->fd !=3D -1) { - files[dev->fd].read =3D false; - mb(); /* Make sure to let the handler set read to 1 before we star= t looking at the ring */ + { + struct file *file =3D get_file_from_fd(dev->fd); + + if ( file ) { + file->read =3D false; + mb(); /* Make sure to let the handler set read to 1 before we = start looking at the ring */ + } } #endif =20 @@ -554,22 +558,11 @@ moretodo: } =20 #ifdef HAVE_LIBC -int blkfront_open(struct blkfront_dev *dev) -{ - /* Silently prevent multiple opens */ - if(dev->fd !=3D -1) { - return dev->fd; - } - dev->fd =3D alloc_fd(FTYPE_BLK); - printk("blk_open(%s) -> %d\n", dev->nodename, dev->fd); - files[dev->fd].dev =3D dev; - return dev->fd; -} - -int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int write) +static int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int wri= te) { - struct blkfront_dev* dev =3D files[fd].dev; - off_t offset =3D files[fd].offset; + struct file *file =3D get_file_from_fd(fd); + struct blkfront_dev* dev =3D file->dev; + off_t offset =3D file->offset; struct blkfront_aiocb aiocb; unsigned long long disksize =3D dev->info.sectors * dev->info.sector_si= ze; unsigned int blocksize =3D dev->info.sector_size; @@ -711,14 +704,25 @@ int blkfront_posix_rwop(int fd, uint8_t* buf, size_t = count, int write) } =20 free(copybuf); - files[fd].offset +=3D rc; + file->offset +=3D rc; return rc; =20 } =20 -int blkfront_posix_fstat(int fd, struct stat* buf) +static int blkfront_posix_read(int fd, void *buf, size_t nbytes) +{ + return blkfront_posix_rwop(fd, buf, nbytes, 0); +} + +static int blkfront_posix_write(int fd, const void *buf, size_t nbytes) { - struct blkfront_dev* dev =3D files[fd].dev; + return blkfront_posix_rwop(fd, (void *)buf, nbytes, 1); +} + +static int blkfront_posix_fstat(int fd, struct stat *buf) +{ + struct file *file =3D get_file_from_fd(fd); + struct blkfront_dev* dev =3D file->dev; =20 buf->st_mode =3D dev->info.mode; buf->st_uid =3D 0; @@ -728,4 +732,44 @@ int blkfront_posix_fstat(int fd, struct stat* buf) =20 return 0; } + +static int blkfront_close_fd(int fd) +{ + struct file *file =3D get_file_from_fd(fd); + + shutdown_blkfront(file->dev); + + return 0; +} + +static struct file_ops blk_ops =3D { + .name =3D "blk", + .read =3D blkfront_posix_read, + .write =3D blkfront_posix_write, + .lseek =3D lseek_default, + .close =3D blkfront_close_fd, + .fstat =3D blkfront_posix_fstat, + .select_rd =3D select_read_flag, +}; + +int blkfront_open(struct blkfront_dev *dev) +{ + struct file *file; + static unsigned int ftype_blk; + + /* Silently prevent multiple opens */ + if(dev->fd !=3D -1) { + return dev->fd; + } + + if ( !ftype_blk ) + ftype_blk =3D alloc_file_type(&blk_ops); + + dev->fd =3D alloc_fd(ftype_blk); + printk("blk_open(%s) -> %d\n", dev->nodename, dev->fd); + file =3D get_file_from_fd(dev->fd); + file->dev =3D dev; + + return dev->fd; +} #endif diff --git a/include/blkfront.h b/include/blkfront.h index 3528af9..7f84a0a 100644 --- a/include/blkfront.h +++ b/include/blkfront.h @@ -28,17 +28,12 @@ struct blkfront_info }; struct blkfront_dev *init_blkfront(char *nodename, struct blkfront_info *i= nfo); #ifdef HAVE_LIBC -#include /* POSIX IO functions: * use blkfront_open() to get a file descriptor to the block device * Don't use the other blkfront posix functions here directly, instead use * read(), write(), lseek() and fstat() on the file descriptor */ int blkfront_open(struct blkfront_dev *dev); -int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int write); -#define blkfront_posix_write(fd, buf, count) blkfront_posix_rwop(fd, (uint= 8_t*)buf, count, 1) -#define blkfront_posix_read(fd, buf, count) blkfront_posix_rwop(fd, (uint8= _t*)buf, count, 0) -int blkfront_posix_fstat(int fd, struct stat* buf); #endif void blkfront_aio(struct blkfront_aiocb *aiocbp, int write); #define blkfront_aio_read(aiocbp) blkfront_aio(aiocbp, 0) diff --git a/include/lib.h b/include/lib.h index f6478de..05f5083 100644 --- a/include/lib.h +++ b/include/lib.h @@ -164,8 +164,7 @@ void sanity_check(void); #define FTYPE_FB 6 #define FTYPE_KBD 7 #define FTYPE_TAP 8 -#define FTYPE_BLK 9 -#define FTYPE_N 10 +#define FTYPE_N 9 #define FTYPE_SPARE 16 =20 typedef int file_read_func(int fd, void *buf, size_t nbytes); diff --git a/lib/sys.c b/lib/sys.c index ff6be52..f84fedd 100644 --- a/lib/sys.c +++ b/lib/sys.c @@ -337,11 +337,6 @@ int read(int fd, void *buf, size_t nbytes) } return ret * sizeof(union xenfb_in_event); } -#endif -#ifdef CONFIG_BLKFRONT - case FTYPE_BLK: { - return blkfront_posix_read(fd, buf, nbytes); - } #endif default: break; @@ -379,10 +374,6 @@ int write(int fd, const void *buf, size_t nbytes) case FTYPE_TAP: netfront_xmit(files[fd].dev, (void*) buf, nbytes); return nbytes; -#endif -#ifdef CONFIG_BLKFRONT - case FTYPE_BLK: - return blkfront_posix_write(fd, buf, nbytes); #endif default: break; @@ -432,10 +423,6 @@ off_t lseek(int fd, off_t offset, int whence) return ops->lseek(fd, offset, whence); =20 switch(files[fd].type) { -#ifdef CONFIG_BLKFRONT - case FTYPE_BLK: - break; -#endif case FTYPE_FILE: break; default: @@ -477,11 +464,6 @@ int close(int fd) shutdown_netfront(files[fd].dev); break; #endif -#ifdef CONFIG_BLKFRONT - case FTYPE_BLK: - shutdown_blkfront(files[fd].dev); - break; -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD: shutdown_kbdfront(files[fd].dev); @@ -554,10 +536,6 @@ int fstat(int fd, struct stat *buf) buf->st_ctime =3D time(NULL); return 0; } -#ifdef CONFIG_BLKFRONT - case FTYPE_BLK: - return blkfront_posix_fstat(fd, buf); -#endif default: break; } @@ -663,7 +641,6 @@ static const char *file_types[] =3D { [FTYPE_CONSOLE] =3D "console", [FTYPE_SOCKET] =3D "socket", [FTYPE_TAP] =3D "net", - [FTYPE_BLK] =3D "blk", [FTYPE_KBD] =3D "kbd", [FTYPE_FB] =3D "fb", }; @@ -841,7 +818,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_se= t *writefds, fd_set *exce FD_CLR(i, exceptfds); break; case FTYPE_TAP: - case FTYPE_BLK: case FTYPE_KBD: case FTYPE_FB: if (FD_ISSET(i, readfds)) { --=20 2.26.2