All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org
Cc: samuel.thibault@ens-lyon.org, wl@xen.org,
	Juergen Gross <jgross@suse.com>
Subject: [PATCH 03/15] mini-os: make offset a common struct file member for all types
Date: Thu,  6 Jan 2022 12:57:29 +0100	[thread overview]
Message-ID: <20220106115741.3219-4-jgross@suse.com> (raw)
In-Reply-To: <20220106115741.3219-1-jgross@suse.com>

Currently 4 file types have an offset member in their private struct
file part. Make offset a common struct member shared by all file types.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 blkfront.c    |  5 ++---
 include/lib.h |  5 +----
 lib/sys.c     | 14 +++++---------
 tpm_tis.c     | 11 +++++------
 tpmfront.c    | 11 +++++------
 5 files changed, 18 insertions(+), 28 deletions(-)

diff --git a/blkfront.c b/blkfront.c
index 7c8eb74..8137106 100644
--- a/blkfront.c
+++ b/blkfront.c
@@ -563,14 +563,13 @@ int blkfront_open(struct blkfront_dev *dev)
     dev->fd = alloc_fd(FTYPE_BLK);
     printk("blk_open(%s) -> %d\n", dev->nodename, dev->fd);
     files[dev->fd].blk.dev = dev;
-    files[dev->fd].blk.offset = 0;
     return dev->fd;
 }
 
 int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int write)
 {
    struct blkfront_dev* dev = files[fd].blk.dev;
-   off_t offset = files[fd].blk.offset;
+   off_t offset = files[fd].offset;
    struct blkfront_aiocb aiocb;
    unsigned long long disksize = dev->info.sectors * dev->info.sector_size;
    unsigned int blocksize = dev->info.sector_size;
@@ -712,7 +711,7 @@ int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int write)
    }
 
    free(copybuf);
-   files[fd].blk.offset += rc;
+   files[fd].offset += rc;
    return rc;
 
 }
diff --git a/include/lib.h b/include/lib.h
index df2de9e..4d9b14b 100644
--- a/include/lib.h
+++ b/include/lib.h
@@ -185,6 +185,7 @@ struct evtchn_port_info {
 struct file {
     enum fd_type type;
     bool read;	/* maybe available for read */
+    off_t offset;
     union {
 	struct {
             /* lwIP fd */
@@ -193,7 +194,6 @@ struct file {
 	struct {
             /* FS import fd */
 	    int fd;
-	    off_t offset;
 	} file;
 	struct {
 	    struct evtchn_port_list ports;
@@ -204,7 +204,6 @@ struct file {
 	} tap;
 	struct {
 	    struct blkfront_dev *dev;
-            off_t offset;
 	} blk;
 	struct {
 	    struct kbdfront_dev *dev;
@@ -219,14 +218,12 @@ struct file {
 	struct {
 	   struct tpmfront_dev *dev;
 	   int respgot;
-	   off_t offset;
 	} tpmfront;
 #endif
 #ifdef CONFIG_TPM_TIS
 	struct {
 	   struct tpm_chip *dev;
 	   int respgot;
-	   off_t offset;
 	} tpm_tis;
 #endif
 #ifdef CONFIG_XENBUS
diff --git a/lib/sys.c b/lib/sys.c
index e8d5eb2..e1cea70 100644
--- a/lib/sys.c
+++ b/lib/sys.c
@@ -107,6 +107,7 @@ int alloc_fd(enum fd_type type)
     for (i=0; i<NOFILE; i++) {
 	if (files[i].type == FTYPE_NONE) {
 	    files[i].type = type;
+            files[i].offset = 0;
 	    pthread_mutex_unlock(&fd_lock);
 	    return i;
 	}
@@ -363,25 +364,20 @@ int write(int fd, const void *buf, size_t nbytes)
 
 off_t lseek(int fd, off_t offset, int whence)
 {
-    off_t* target = NULL;
     switch(files[fd].type) {
 #ifdef CONFIG_BLKFRONT
        case FTYPE_BLK:
-          target = &files[fd].blk.offset;
           break;
 #endif
 #ifdef CONFIG_TPMFRONT
        case FTYPE_TPMFRONT:
-          target = &files[fd].tpmfront.offset;
           break;
 #endif
 #ifdef CONFIG_TPM_TIS
        case FTYPE_TPM_TIS:
-          target = &files[fd].tpm_tis.offset;
           break;
 #endif
        case FTYPE_FILE:
-          target = &files[fd].file.offset;
           break;
        default:
           /* Not implemented for this filetype */
@@ -391,10 +387,10 @@ off_t lseek(int fd, off_t offset, int whence)
 
     switch (whence) {
        case SEEK_SET:
-          *target = offset;
+          files[fd].offset = offset;
           break;
        case SEEK_CUR:
-          *target += offset;
+          files[fd].offset += offset;
           break;
        case SEEK_END:
           {
@@ -403,14 +399,14 @@ off_t lseek(int fd, off_t offset, int whence)
              ret = fstat(fd, &st);
              if (ret)
                 return -1;
-             *target = st.st_size + offset;
+             files[fd].offset = st.st_size + offset;
              break;
           }
        default:
           errno = EINVAL;
           return -1;
     }
-    return *target;
+    return files[fd].offset;
 }
 
 int fsync(int fd) {
diff --git a/tpm_tis.c b/tpm_tis.c
index 4a51027..8a632b1 100644
--- a/tpm_tis.c
+++ b/tpm_tis.c
@@ -847,7 +847,7 @@ int tpm_tis_send(struct tpm_chip* tpm, uint8_t* buf, size_t len) {
    if(tpm->fd >= 0) {
       files[tpm->fd].read = false;
       files[tpm->fd].tpm_tis.respgot = 0;
-      files[tpm->fd].tpm_tis.offset = 0;
+      files[tpm->fd].offset = 0;
    }
 #endif
    return len;
@@ -1290,7 +1290,6 @@ int tpm_tis_open(struct tpm_chip* tpm)
    tpm->fd = alloc_fd(FTYPE_TPM_TIS);
    printk("tpm_tis_open() -> %d\n", tpm->fd);
    files[tpm->fd].tpm_tis.dev = tpm;
-   files[tpm->fd].tpm_tis.offset = 0;
    files[tpm->fd].tpm_tis.respgot = 0;
    return tpm->fd;
 }
@@ -1340,13 +1339,13 @@ int tpm_tis_posix_read(int fd, uint8_t* buf, size_t count)
 
 
    /* Handle EOF case */
-   if(files[fd].tpm_tis.offset >= tpm->data_len) {
+   if(files[fd].offset >= tpm->data_len) {
       rc = 0;
    } else {
-      rc = min(tpm->data_len - files[fd].tpm_tis.offset, count);
-      memcpy(buf, tpm->data_buffer + files[fd].tpm_tis.offset, rc);
+      rc = min(tpm->data_len - files[fd].offset, count);
+      memcpy(buf, tpm->data_buffer + files[fd].offset, rc);
    }
-   files[fd].tpm_tis.offset += rc;
+   files[fd].offset += rc;
    /* Reset the data pending flag */
    return rc;
 }
diff --git a/tpmfront.c b/tpmfront.c
index d825b49..8b2a910 100644
--- a/tpmfront.c
+++ b/tpmfront.c
@@ -440,7 +440,7 @@ int tpmfront_send(struct tpmfront_dev* dev, const uint8_t* msg, size_t length)
    if(dev->fd >= 0) {
       files[dev->fd].read = false;
       files[dev->fd].tpmfront.respgot = 0;
-      files[dev->fd].tpmfront.offset = 0;
+      files[dev->fd].offset = 0;
    }
 #endif
    wmb();
@@ -539,7 +539,6 @@ int tpmfront_open(struct tpmfront_dev* dev)
    dev->fd = alloc_fd(FTYPE_TPMFRONT);
    printk("tpmfront_open(%s) -> %d\n", dev->nodename, dev->fd);
    files[dev->fd].tpmfront.dev = dev;
-   files[dev->fd].tpmfront.offset = 0;
    files[dev->fd].tpmfront.respgot = 0;
    return dev->fd;
 }
@@ -589,14 +588,14 @@ int tpmfront_posix_read(int fd, uint8_t* buf, size_t count)
    }
 
    /* handle EOF case */
-   if(files[dev->fd].tpmfront.offset >= dev->resplen) {
+   if(files[dev->fd].offset >= dev->resplen) {
       return 0;
    }
 
    /* Compute the number of bytes and do the copy operation */
-   if((rc = min(count, dev->resplen - files[dev->fd].tpmfront.offset)) != 0) {
-      memcpy(buf, dev->respbuf + files[dev->fd].tpmfront.offset, rc);
-      files[dev->fd].tpmfront.offset += rc;
+   if((rc = min(count, dev->resplen - files[dev->fd].offset)) != 0) {
+      memcpy(buf, dev->respbuf + files[dev->fd].offset, rc);
+      files[dev->fd].offset += rc;
    }
 
    return rc;
-- 
2.26.2



  parent reply	other threads:[~2022-01-06 11:59 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-06 11:57 [PATCH 00/15] mini-os: remove struct file dependency from config Juergen Gross
2022-01-06 11:57 ` [PATCH 01/15] mini-os: split struct file definition from its usage Juergen Gross
2022-01-09  1:17   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 02/15] mini-os: makes file.read bool and move it ahead of device specific part Juergen Gross
2022-01-09  1:18   ` Samuel Thibault
2022-01-09  1:21     ` Samuel Thibault
2022-01-09  6:12       ` Juergen Gross
2022-01-09 10:20         ` Samuel Thibault
2022-01-09 10:28           ` Juergen Gross
2022-01-09 10:33             ` Samuel Thibault
2022-01-06 11:57 ` Juergen Gross [this message]
2022-01-09  1:23   ` [PATCH 03/15] mini-os: make offset a common struct file member for all types Samuel Thibault
2022-01-06 11:57 ` [PATCH 04/15] mini-os: replace multiple fd elements in struct file by common one Juergen Gross
2022-01-09  1:25   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 05/15] mini-os: introduce a common dev pointer in struct file Juergen Gross
2022-01-09  1:25   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 06/15] mini-os: eliminate blkfront union member " Juergen Gross
2022-01-09  1:26   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 07/15] mini-os: eliminate consfront " Juergen Gross
2022-01-09  1:27   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 08/15] mini-os: eliminate fbfront " Juergen Gross
2022-01-09  1:27   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 09/15] mini-os: eliminate kbdfront " Juergen Gross
2022-01-09  1:28   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 10/15] mini-os: eliminate netfront " Juergen Gross
2022-01-09  1:28   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 11/15] mini-os: move tpm respgot member of struct file to device specific data Juergen Gross
2022-01-09  1:30   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 12/15] mini-os: eliminate tpmfront union member in struct file Juergen Gross
2022-01-09  1:30   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 13/15] mini-os: eliminate tpmtis " Juergen Gross
2022-01-09  1:31   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 14/15] mini-os: eliminate xenbus " Juergen Gross
2022-01-09  1:32   ` Samuel Thibault
2022-01-06 11:57 ` [PATCH 15/15] mini-os: introduce get_file_from_fd() Juergen Gross
2022-01-09  1:33   ` Samuel Thibault
2022-01-07  2:39 ` [PATCH 00/15] mini-os: remove struct file dependency from config Samuel Thibault

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220106115741.3219-4-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=minios-devel@lists.xenproject.org \
    --cc=samuel.thibault@ens-lyon.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.