From: "Dr. David Alan Gilbert (git)" <dgilbert@redhat.com>
To: qemu-devel@nongnu.org
Cc: vgoyal@redhat.com, miklos@szeredi.hu, stefanha@redhat.com,
sweil@redhat.com, swhiteho@redhat.com
Subject: [Qemu-devel] [RFC PATCH 5/7] virtio-fs: Fill in slave commands for mapping
Date: Mon, 10 Dec 2018 17:31:49 +0000 [thread overview]
Message-ID: <20181210173151.16629-6-dgilbert@redhat.com> (raw)
In-Reply-To: <20181210173151.16629-1-dgilbert@redhat.com>
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Fill in definitions for map, unmap and sync commands.
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
---
hw/virtio/vhost-user-fs.c | 129 ++++++++++++++++++++++++++++++++++++--
1 file changed, 123 insertions(+), 6 deletions(-)
diff --git a/hw/virtio/vhost-user-fs.c b/hw/virtio/vhost-user-fs.c
index da70d9cd2c..bbb15477e5 100644
--- a/hw/virtio/vhost-user-fs.c
+++ b/hw/virtio/vhost-user-fs.c
@@ -24,20 +24,137 @@
int vhost_user_fs_slave_map(struct vhost_dev *dev, VhostUserFSSlaveMsg *sm,
int fd)
{
- /* TODO */
- return -1;
+ VHostUserFS *fs = VHOST_USER_FS(dev->vdev);
+ size_t cache_size = fs->conf.cache_size;
+ void *cache_host = memory_region_get_ram_ptr(&fs->cache);
+
+ unsigned int i;
+ int res = 0;
+
+ if (fd < 0) {
+ fprintf(stderr, "%s: Bad fd for map\n", __func__);
+ return -1;
+ }
+
+ for (i = 0; i < VHOST_USER_FS_SLAVE_ENTRIES; i++) {
+ if (sm->len[i] == 0) {
+ continue;
+ }
+
+ if ((sm->c_offset[i] + sm->len[i]) < sm->len[i] ||
+ (sm->c_offset[i] + sm->len[i]) > cache_size) {
+ fprintf(stderr, "%s: Bad offset/len for map [%d] %"
+ PRIx64 "+%" PRIx64 "\n", __func__,
+ i, sm->c_offset[i], sm->len[i]);
+ res = -1;
+ break;
+ }
+
+ if (mmap(cache_host + sm->c_offset[i], sm->len[i],
+ ((sm->flags[i] & VHOST_USER_FS_FLAG_MAP_R) ? PROT_READ : 0) |
+ ((sm->flags[i] & VHOST_USER_FS_FLAG_MAP_W) ? PROT_WRITE : 0),
+ MAP_SHARED | MAP_FIXED,
+ fd, sm->fd_offset[i]) != (cache_host + sm->c_offset[i])) {
+ fprintf(stderr, "%s: map failed err %d [%d] %"
+ PRIx64 "+%" PRIx64 " from %" PRIx64 "\n", __func__,
+ errno, i, sm->c_offset[i], sm->len[i],
+ sm->fd_offset[i]);
+ res = -1;
+ break;
+ }
+ }
+
+ if (res) {
+ /* Something went wrong, unmap them all */
+ vhost_user_fs_slave_unmap(dev, sm);
+ }
+ return res;
}
int vhost_user_fs_slave_unmap(struct vhost_dev *dev, VhostUserFSSlaveMsg *sm)
{
- /* TODO */
- return -1;
+ VHostUserFS *fs = VHOST_USER_FS(dev->vdev);
+ size_t cache_size = fs->conf.cache_size;
+ void *cache_host = memory_region_get_ram_ptr(&fs->cache);
+
+ unsigned int i;
+ int res = 0;
+
+ /* Note even if one unmap fails we try the rest, since the effect
+ * is to clean up as much as possible.
+ */
+ for (i = 0; i < VHOST_USER_FS_SLAVE_ENTRIES; i++) {
+ void *ptr;
+ if (sm->len[i] == 0) {
+ continue;
+ }
+
+ if (sm->len[i] == ~(uint64_t)0) {
+ /* Special case meaning the whole arena */
+ sm->len[i] = cache_size;
+ }
+
+ if ((sm->c_offset[i] + sm->len[i]) < sm->len[i] ||
+ (sm->c_offset[i] + sm->len[i]) > cache_size) {
+ fprintf(stderr, "%s: Bad offset/len for unmap [%d] %"
+ PRIx64 "+%" PRIx64 "\n", __func__,
+ i, sm->c_offset[i], sm->len[i]);
+ res = -1;
+ continue;
+ }
+
+ ptr = mmap(cache_host + sm->c_offset[i], sm->len[i],
+ PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+ if (ptr != (cache_host + sm->c_offset[i])) {
+ fprintf(stderr, "%s: mmap failed (%s) [%d] %"
+ PRIx64 "+%" PRIx64 " from %" PRIx64 " res: %p\n",
+ __func__,
+ strerror(errno),
+ i, sm->c_offset[i], sm->len[i],
+ sm->fd_offset[i], ptr);
+ res = -1;
+ }
+ }
+
+ return res;
}
int vhost_user_fs_slave_sync(struct vhost_dev *dev, VhostUserFSSlaveMsg *sm)
{
- /* TODO */
- return -1;
+ VHostUserFS *fs = VHOST_USER_FS(dev->vdev);
+ size_t cache_size = fs->conf.cache_size;
+ void *cache_host = memory_region_get_ram_ptr(&fs->cache);
+
+ unsigned int i;
+ int res = 0;
+
+ /* Note even if one sync fails we try the rest */
+ for (i = 0; i < VHOST_USER_FS_SLAVE_ENTRIES; i++) {
+ if (sm->len[i] == 0) {
+ continue;
+ }
+
+ if ((sm->c_offset[i] + sm->len[i]) < sm->len[i] ||
+ (sm->c_offset[i] + sm->len[i]) > cache_size) {
+ fprintf(stderr, "%s: Bad offset/len for sync [%d] %"
+ PRIx64 "+%" PRIx64 "\n", __func__,
+ i, sm->c_offset[i], sm->len[i]);
+ res = -1;
+ continue;
+ }
+
+ if (msync(cache_host + sm->c_offset[i], sm->len[i],
+ MS_SYNC /* ?? */)) {
+ fprintf(stderr, "%s: msync failed (%s) [%d] %"
+ PRIx64 "+%" PRIx64 " from %" PRIx64 "\n", __func__,
+ strerror(errno),
+ i, sm->c_offset[i], sm->len[i],
+ sm->fd_offset[i]);
+ res = -1;
+ }
+ }
+
+ return res;
}
--
2.19.2
next prev parent reply other threads:[~2018-12-10 17:32 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-10 17:31 [Qemu-devel] [RFC PATCH 0/7] virtio-fs: shared file system for virtual machines3 Dr. David Alan Gilbert (git)
2018-12-10 17:31 ` [Qemu-devel] [RFC PATCH 1/7] virtio: Add shared memory capability Dr. David Alan Gilbert (git)
2018-12-10 21:03 ` Eric Blake
2018-12-11 10:24 ` Dr. David Alan Gilbert
2018-12-10 17:31 ` [Qemu-devel] [RFC PATCH 2/7] virtio: add vhost-user-fs-pci device Dr. David Alan Gilbert (git)
2018-12-10 17:31 ` [Qemu-devel] [RFC PATCH 3/7] virtio-fs: Add cache BAR Dr. David Alan Gilbert (git)
2018-12-10 21:10 ` Eric Blake
2018-12-11 10:25 ` Dr. David Alan Gilbert
2018-12-10 17:31 ` [Qemu-devel] [RFC PATCH 4/7] virtio-fs: Add vhost-user slave commands for mapping Dr. David Alan Gilbert (git)
2018-12-10 17:31 ` Dr. David Alan Gilbert (git) [this message]
2018-12-10 17:31 ` [Qemu-devel] [RFC PATCH 6/7] virtio-fs: Allow mapping of meta data version table Dr. David Alan Gilbert (git)
2018-12-10 17:31 ` [Qemu-devel] [RFC PATCH 7/7] virtio-fs: Allow mapping of journal Dr. David Alan Gilbert (git)
2018-12-10 21:12 ` Eric Blake
2018-12-11 10:34 ` Dr. David Alan Gilbert
2018-12-10 20:26 ` [Qemu-devel] [RFC PATCH 0/7] virtio-fs: shared file system for virtual machines3 no-reply
2018-12-11 12:53 ` Stefan Hajnoczi
2018-12-12 12:30 ` Daniel P. Berrangé
2018-12-12 13:52 ` Dr. David Alan Gilbert
2018-12-12 13:58 ` Daniel P. Berrangé
2018-12-12 14:49 ` Stefan Hajnoczi
2018-12-22 9:27 ` jiangyiwen
2018-12-26 19:08 ` Vivek Goyal
2019-01-08 6:08 ` jiangyiwen
2019-04-04 13:24 ` Greg Kurz
2019-04-05 8:59 ` Dr. David Alan Gilbert
2019-04-05 8:59 ` Dr. David Alan Gilbert
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=20181210173151.16629-6-dgilbert@redhat.com \
--to=dgilbert@redhat.com \
--cc=miklos@szeredi.hu \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
--cc=sweil@redhat.com \
--cc=swhiteho@redhat.com \
--cc=vgoyal@redhat.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).