[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v2 06/12] mini-os: use alloc_file_type() and get_file_from_fd() in blkfront



Juergen Gross, le mar. 11 janv. 2022 16:12:09 +0100, a ecrit:
> 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 <jgross@xxxxxxxx>

modulo the int fd / file * thing,

Reviewed-by: Samuel Thibault <samuel.thibault@xxxxxxxxxxxx>

> ---
>  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_regs 
> *regs, void *data)
>  {
>  #ifdef HAVE_LIBC
>      struct blkfront_dev *dev = data;
> -    int fd = dev->fd;
> +    struct file *file = get_file_from_fd(dev->fd);
>  
> -    if (fd != -1)
> -        files[fd].read = true;
> +    if ( file )
> +        file->read = true;
>  #endif
>      wake_up(&blkfront_queue);
>  }
> @@ -483,9 +483,13 @@ int blkfront_aio_poll(struct blkfront_dev *dev)
>  
>  moretodo:
>  #ifdef HAVE_LIBC
> -    if (dev->fd != -1) {
> -        files[dev->fd].read = false;
> -        mb(); /* Make sure to let the handler set read to 1 before we start 
> looking at the ring */
> +    {
> +        struct file *file = get_file_from_fd(dev->fd);
> +
> +        if ( file ) {
> +            file->read = false;
> +            mb(); /* Make sure to let the handler set read to 1 before we 
> start looking at the ring */
> +        }
>      }
>  #endif
>  
> @@ -554,22 +558,11 @@ moretodo:
>  }
>  
>  #ifdef HAVE_LIBC
> -int blkfront_open(struct blkfront_dev *dev)
> -{
> -    /* Silently prevent multiple opens */
> -    if(dev->fd != -1) {
> -       return dev->fd;
> -    }
> -    dev->fd = alloc_fd(FTYPE_BLK);
> -    printk("blk_open(%s) -> %d\n", dev->nodename, dev->fd);
> -    files[dev->fd].dev = 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 write)
>  {
> -   struct blkfront_dev* dev = files[fd].dev;
> -   off_t offset = files[fd].offset;
> +   struct file *file = get_file_from_fd(fd);
> +   struct blkfront_dev* dev = file->dev;
> +   off_t offset = file->offset;
>     struct blkfront_aiocb aiocb;
>     unsigned long long disksize = dev->info.sectors * dev->info.sector_size;
>     unsigned int blocksize = dev->info.sector_size;
> @@ -711,14 +704,25 @@ int blkfront_posix_rwop(int fd, uint8_t* buf, size_t 
> count, int write)
>     }
>  
>     free(copybuf);
> -   files[fd].offset += rc;
> +   file->offset += rc;
>     return rc;
>  
>  }
>  
> -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 = 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 = get_file_from_fd(fd);
> +   struct blkfront_dev* dev = file->dev;
>  
>     buf->st_mode = dev->info.mode;
>     buf->st_uid = 0;
> @@ -728,4 +732,44 @@ int blkfront_posix_fstat(int fd, struct stat* buf)
>  
>     return 0;
>  }
> +
> +static int blkfront_close_fd(int fd)
> +{
> +    struct file *file = get_file_from_fd(fd);
> +
> +    shutdown_blkfront(file->dev);
> +
> +    return 0;
> +}
> +
> +static struct file_ops blk_ops = {
> +    .name = "blk",
> +    .read = blkfront_posix_read,
> +    .write = blkfront_posix_write,
> +    .lseek = lseek_default,
> +    .close = blkfront_close_fd,
> +    .fstat = blkfront_posix_fstat,
> +    .select_rd = 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 != -1) {
> +       return dev->fd;
> +    }
> +
> +    if ( !ftype_blk )
> +        ftype_blk = alloc_file_type(&blk_ops);
> +
> +    dev->fd = alloc_fd(ftype_blk);
> +    printk("blk_open(%s) -> %d\n", dev->nodename, dev->fd);
> +    file = get_file_from_fd(dev->fd);
> +    file->dev = 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 
> *info);
>  #ifdef HAVE_LIBC
> -#include <sys/stat.h>
>  /* 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, 
> (uint8_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
>  
>  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);
>  
>      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 = 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[] = {
>      [FTYPE_CONSOLE] = "console",
>      [FTYPE_SOCKET]  = "socket",
>      [FTYPE_TAP]     = "net",
> -    [FTYPE_BLK]     = "blk",
>      [FTYPE_KBD]     = "kbd",
>      [FTYPE_FB]      = "fb",
>  };
> @@ -841,7 +818,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set 
> *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)) {
> -- 
> 2.26.2
> 

-- 
Samuel
gawk; talk; nice; date; wine; grep; touch; unzip; strip;  \
touch; gasp; finger; gasp; lyx; gasp; latex; mount; fsck; \
more; yes; gasp; umount; make clean; make mrproper; sleep



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.