All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bin Meng <bin.meng@windriver.com>
To: Christian Schoenebeck <qemu_oss@crudebyte.com>,
	Greg Kurz <groug@kaod.org>,
	qemu-devel@nongnu.org
Cc: Guohuai Shi <guohuai.shi@windriver.com>
Subject: [PATCH v5 08/16] hw/9pfs: Add a helper qemu_stat_blksize()
Date: Mon, 20 Feb 2023 18:08:07 +0800	[thread overview]
Message-ID: <20230220100815.1624266-9-bin.meng@windriver.com> (raw)
In-Reply-To: <20230220100815.1624266-1-bin.meng@windriver.com>

As Windows host does not have stat->st_blksize field, we use the one
we calculated in init_win32_root_directory().

Add a helper qemu_stat_blksize() and use it to avoid direct access to
stat->st_blksize.

Co-developed-by: Guohuai Shi <guohuai.shi@windriver.com>
Signed-off-by: Bin Meng <bin.meng@windriver.com>
---

 hw/9pfs/9p-util.h       | 13 +++++++++++++
 hw/9pfs/9p-util-win32.c |  7 +++++++
 hw/9pfs/9p.c            | 13 ++++++++++++-
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/hw/9pfs/9p-util.h b/hw/9pfs/9p-util.h
index 1fb54d0b97..ea8c116059 100644
--- a/hw/9pfs/9p-util.h
+++ b/hw/9pfs/9p-util.h
@@ -156,6 +156,7 @@ void seekdir_win32(DIR *pDir, long pos);
 long telldir_win32(DIR *pDir);
 off_t qemu_dirent_off_win32(struct V9fsState *s, union V9fsFidOpenState *fs);
 uint64_t qemu_stat_rdev_win32(struct FsContext *fs_ctx);
+uint64_t qemu_stat_blksize_win32(struct FsContext *fs_ctx);
 #endif
 
 static inline void close_preserve_errno(int fd)
@@ -285,6 +286,18 @@ static inline uint64_t qemu_stat_rdev(const struct stat *stbuf,
 #endif
 }
 
+static inline uint64_t qemu_stat_blksize(const struct stat *stbuf,
+                                         struct FsContext *fs_ctx)
+{
+#if defined(CONFIG_LINUX) || defined(CONFIG_DARWIN)
+    return stbuf->st_blksize;
+#elif defined(CONFIG_WIN32)
+    return qemu_stat_blksize_win32(fs_ctx);
+#else
+#error Missing qemu_stat_blksize() implementation for this host system
+#endif
+}
+
 /*
  * As long as mknodat is not available on macOS, this workaround
  * using pthread_fchdir_np is needed. qemu_mknodat is defined in
diff --git a/hw/9pfs/9p-util-win32.c b/hw/9pfs/9p-util-win32.c
index 61bb572261..ce7c5f7847 100644
--- a/hw/9pfs/9p-util-win32.c
+++ b/hw/9pfs/9p-util-win32.c
@@ -1443,3 +1443,10 @@ uint64_t qemu_stat_rdev_win32(struct FsContext *fs_ctx)
 
     return rdev;
 }
+
+uint64_t qemu_stat_blksize_win32(struct FsContext *fs_ctx)
+{
+    LocalData *data = fs_ctx->private;
+
+    return data ? (uint64_t)data->block_size : 0;
+}
diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c
index ead727a12b..8858d7574c 100644
--- a/hw/9pfs/9p.c
+++ b/hw/9pfs/9p.c
@@ -1333,12 +1333,14 @@ static int32_t blksize_to_iounit(const V9fsPDU *pdu, int32_t blksize)
 
 static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat *stbuf)
 {
-    return blksize_to_iounit(pdu, stbuf->st_blksize);
+    return blksize_to_iounit(pdu, qemu_stat_blksize(stbuf, &pdu->s->ctx));
 }
 
 static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
                                 V9fsStatDotl *v9lstat)
 {
+    dev_t rdev = qemu_stat_rdev(stbuf, &pdu->s->ctx);
+
     memset(v9lstat, 0, sizeof(*v9lstat));
 
     v9lstat->st_mode = stbuf->st_mode;
@@ -1348,7 +1350,16 @@ static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
     v9lstat->st_rdev = host_dev_to_dotl_dev(rdev);
     v9lstat->st_size = stbuf->st_size;
     v9lstat->st_blksize = stat_to_iounit(pdu, stbuf);
+#if defined(CONFIG_LINUX) || defined(CONFIG_DARWIN)
     v9lstat->st_blocks = stbuf->st_blocks;
+#elif defined(CONFIG_WIN32)
+    if (v9lstat->st_blksize == 0) {
+        v9lstat->st_blocks = 0;
+    } else {
+        v9lstat->st_blocks = ROUND_UP(v9lstat->st_size / v9lstat->st_blksize,
+                                      v9lstat->st_blksize);
+    }
+#endif
     v9lstat->st_atime_sec = stbuf->st_atime;
     v9lstat->st_mtime_sec = stbuf->st_mtime;
     v9lstat->st_ctime_sec = stbuf->st_ctime;
-- 
2.25.1



  parent reply	other threads:[~2023-02-20 10:09 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-20 10:07 [PATCH v5 00/16] hw/9pfs: Add 9pfs support for Windows Bin Meng
2023-02-20 10:08 ` [PATCH v5 01/16] hw/9pfs: Add missing definitions " Bin Meng
2023-02-20 10:08 ` [PATCH v5 02/16] hw/9pfs: Implement Windows specific utilities functions for 9pfs Bin Meng
2023-02-20 10:08 ` [PATCH v5 03/16] hw/9pfs: Replace the direct call to xxxdir() APIs with a wrapper Bin Meng
2023-03-06  9:31   ` Philippe Mathieu-Daudé
2023-03-06  9:35     ` Bin Meng
2023-02-20 10:08 ` [PATCH v5 04/16] hw/9pfs: Implement Windows specific xxxdir() APIs Bin Meng
2023-03-14 16:05   ` Christian Schoenebeck
2023-03-15 19:05     ` Shi, Guohuai
2023-03-16 11:05       ` Christian Schoenebeck
2023-03-16 17:28         ` Shi, Guohuai
2023-03-17  4:36           ` Shi, Guohuai
2023-03-17 12:16             ` Christian Schoenebeck
2023-02-20 10:08 ` [PATCH v5 05/16] hw/9pfs: Update the local fs driver to support Windows Bin Meng
2023-02-20 10:08 ` [PATCH v5 06/16] hw/9pfs: Support getting current directory offset for Windows Bin Meng
2023-02-20 10:08 ` [PATCH v5 07/16] hw/9pfs: Update helper qemu_stat_rdev() Bin Meng
2023-02-20 10:08 ` Bin Meng [this message]
2023-02-20 10:08 ` [PATCH v5 09/16] hw/9pfs: Disable unsupported flags and features for Windows Bin Meng
2023-02-20 10:08 ` [PATCH v5 10/16] hw/9pfs: Update v9fs_set_fd_limit() " Bin Meng
2023-02-20 10:08 ` [PATCH v5 11/16] hw/9pfs: Add Linux error number definition Bin Meng
2023-02-20 10:08 ` [PATCH v5 12/16] hw/9pfs: Translate Windows errno to Linux value Bin Meng
2023-02-20 10:08 ` [PATCH v5 13/16] fsdev: Disable proxy fs driver on Windows Bin Meng
2023-03-06  9:28   ` Philippe Mathieu-Daudé
2023-02-20 10:08 ` [PATCH v5 14/16] hw/9pfs: Update synth fs driver for Windows Bin Meng
2023-02-20 10:08 ` [PATCH v5 15/16] tests/qtest: virtio-9p-test: Adapt the case for win32 Bin Meng
2023-02-20 10:08 ` [PATCH v5 16/16] meson.build: Turn on virtfs for Windows Bin Meng
2023-03-13 12:53   ` Christian Schoenebeck
2023-03-06  6:04 ` [PATCH v5 00/16] hw/9pfs: Add 9pfs support " Bin Meng
2023-03-06 14:15 ` Christian Schoenebeck
2023-03-06 14:30   ` Philippe Mathieu-Daudé
2023-03-06 14:56   ` Bin Meng
2023-03-07 12:44     ` Christian Schoenebeck

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=20230220100815.1624266-9-bin.meng@windriver.com \
    --to=bin.meng@windriver.com \
    --cc=groug@kaod.org \
    --cc=guohuai.shi@windriver.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu_oss@crudebyte.com \
    /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.