45e84a0
From ed6857bf98e6c8b8080be208ffe15bb678591466 Mon Sep 17 00:00:00 2001
45e84a0
From: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
45e84a0
Date: Sun, 4 Dec 2011 22:35:28 +0530
45e84a0
Subject: [PATCH 07/25] hw/9pfs: Use the correct file descriptor in Fsdriver
45e84a0
 Callback
45e84a0
45e84a0
Fsdriver callback that operate on file descriptor need to
45e84a0
differentiate between directory fd and file fd.
45e84a0
45e84a0
Based on the original patch from Sassan Panahinejad <sassan@sassan.me.uk>
45e84a0
45e84a0
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
45e84a0
---
45e84a0
 fsdev/file-op-9p.h         |    4 ++--
45e84a0
 hw/9pfs/cofile.c           |    4 ++--
45e84a0
 hw/9pfs/virtio-9p-handle.c |   28 ++++++++++++++++++++++------
45e84a0
 hw/9pfs/virtio-9p-local.c  |   36 ++++++++++++++++++++++++++----------
45e84a0
 hw/9pfs/virtio-9p-synth.c  |    5 +++--
45e84a0
 5 files changed, 55 insertions(+), 22 deletions(-)
45e84a0
45e84a0
diff --git a/fsdev/file-op-9p.h b/fsdev/file-op-9p.h
45e84a0
index 1928da2..a85ecd3 100644
45e84a0
--- a/fsdev/file-op-9p.h
45e84a0
+++ b/fsdev/file-op-9p.h
45e84a0
@@ -112,10 +112,10 @@ typedef struct FileOperations
45e84a0
     ssize_t (*pwritev)(FsContext *, V9fsFidOpenState *,
45e84a0
                        const struct iovec *, int, off_t);
45e84a0
     int (*mkdir)(FsContext *, V9fsPath *, const char *, FsCred *);
45e84a0
-    int (*fstat)(FsContext *, V9fsFidOpenState *, struct stat *);
45e84a0
+    int (*fstat)(FsContext *, int, V9fsFidOpenState *, struct stat *);
45e84a0
     int (*rename)(FsContext *, const char *, const char *);
45e84a0
     int (*truncate)(FsContext *, V9fsPath *, off_t);
45e84a0
-    int (*fsync)(FsContext *, V9fsFidOpenState *, int);
45e84a0
+    int (*fsync)(FsContext *, int, V9fsFidOpenState *, int);
45e84a0
     int (*statfs)(FsContext *s, V9fsPath *path, struct statfs *stbuf);
45e84a0
     ssize_t (*lgetxattr)(FsContext *, V9fsPath *,
45e84a0
                          const char *, void *, size_t);
45e84a0
diff --git a/hw/9pfs/cofile.c b/hw/9pfs/cofile.c
45e84a0
index 586b038..b15838c 100644
45e84a0
--- a/hw/9pfs/cofile.c
45e84a0
+++ b/hw/9pfs/cofile.c
45e84a0
@@ -71,7 +71,7 @@ int v9fs_co_fstat(V9fsPDU *pdu, V9fsFidState *fidp, struct stat *stbuf)
45e84a0
     }
45e84a0
     v9fs_co_run_in_worker(
45e84a0
         {
45e84a0
-            err = s->ops->fstat(&s->ctx, &fidp->fs, stbuf);
45e84a0
+            err = s->ops->fstat(&s->ctx, fidp->fid_type, &fidp->fs, stbuf);
45e84a0
             if (err < 0) {
45e84a0
                 err = -errno;
45e84a0
             }
45e84a0
@@ -192,7 +192,7 @@ int v9fs_co_fsync(V9fsPDU *pdu, V9fsFidState *fidp, int datasync)
45e84a0
     }
45e84a0
     v9fs_co_run_in_worker(
45e84a0
         {
45e84a0
-            err = s->ops->fsync(&s->ctx, &fidp->fs, datasync);
45e84a0
+            err = s->ops->fsync(&s->ctx, fidp->fid_type, &fidp->fs, datasync);
45e84a0
             if (err < 0) {
45e84a0
                 err = -errno;
45e84a0
             }
45e84a0
diff --git a/hw/9pfs/virtio-9p-handle.c b/hw/9pfs/virtio-9p-handle.c
45e84a0
index a62f690..f97d898 100644
45e84a0
--- a/hw/9pfs/virtio-9p-handle.c
45e84a0
+++ b/hw/9pfs/virtio-9p-handle.c
45e84a0
@@ -255,10 +255,17 @@ static int handle_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
45e84a0
     return ret;
45e84a0
 }
45e84a0
45e84a0
-static int handle_fstat(FsContext *fs_ctx, V9fsFidOpenState *fs,
45e84a0
-                        struct stat *stbuf)
45e84a0
+static int handle_fstat(FsContext *fs_ctx, int fid_type,
45e84a0
+                        V9fsFidOpenState *fs, struct stat *stbuf)
45e84a0
 {
45e84a0
-    return fstat(fs->fd, stbuf);
45e84a0
+    int fd;
45e84a0
+
45e84a0
+    if (fid_type == P9_FID_DIR) {
45e84a0
+        fd = dirfd(fs->dir);
45e84a0
+    } else {
45e84a0
+        fd = fs->fd;
45e84a0
+    }
45e84a0
+    return fstat(fd, stbuf);
45e84a0
 }
45e84a0
45e84a0
 static int handle_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
45e84a0
@@ -395,12 +402,21 @@ static int handle_remove(FsContext *ctx, const char *path)
45e84a0
     return -1;
45e84a0
 }
45e84a0
45e84a0
-static int handle_fsync(FsContext *ctx, V9fsFidOpenState *fs, int datasync)
45e84a0
+static int handle_fsync(FsContext *ctx, int fid_type,
45e84a0
+                        V9fsFidOpenState *fs, int datasync)
45e84a0
 {
45e84a0
+    int fd;
45e84a0
+
45e84a0
+    if (fid_type == P9_FID_DIR) {
45e84a0
+        fd = dirfd(fs->dir);
45e84a0
+    } else {
45e84a0
+        fd = fs->fd;
45e84a0
+    }
45e84a0
+
45e84a0
     if (datasync) {
45e84a0
-        return qemu_fdatasync(fs->fd);
45e84a0
+        return qemu_fdatasync(fd);
45e84a0
     } else {
45e84a0
-        return fsync(fs->fd);
45e84a0
+        return fsync(fd);
45e84a0
     }
45e84a0
 }
45e84a0
45e84a0
diff --git a/hw/9pfs/virtio-9p-local.c b/hw/9pfs/virtio-9p-local.c
45e84a0
index 99ef0cd..371a94d 100644
45e84a0
--- a/hw/9pfs/virtio-9p-local.c
45e84a0
+++ b/hw/9pfs/virtio-9p-local.c
45e84a0
@@ -366,11 +366,18 @@ out:
45e84a0
     return err;
45e84a0
 }
45e84a0
45e84a0
-static int local_fstat(FsContext *fs_ctx,
45e84a0
+static int local_fstat(FsContext *fs_ctx, int fid_type,
45e84a0
                        V9fsFidOpenState *fs, struct stat *stbuf)
45e84a0
 {
45e84a0
-    int err;
45e84a0
-    err = fstat(fs->fd, stbuf);
45e84a0
+    int err, fd;
45e84a0
+
45e84a0
+    if (fid_type == P9_FID_DIR) {
45e84a0
+        fd = dirfd(fs->dir);
45e84a0
+    } else {
45e84a0
+        fd = fs->fd;
45e84a0
+    }
45e84a0
+
45e84a0
+    err = fstat(fd, stbuf);
45e84a0
     if (err) {
45e84a0
         return err;
45e84a0
     }
45e84a0
@@ -381,19 +388,19 @@ static int local_fstat(FsContext *fs_ctx,
45e84a0
         mode_t tmp_mode;
45e84a0
         dev_t tmp_dev;
45e84a0
45e84a0
-        if (fgetxattr(fs->fd, "user.virtfs.uid",
45e84a0
+        if (fgetxattr(fd, "user.virtfs.uid",
45e84a0
                       &tmp_uid, sizeof(uid_t)) > 0) {
45e84a0
             stbuf->st_uid = tmp_uid;
45e84a0
         }
45e84a0
-        if (fgetxattr(fs->fd, "user.virtfs.gid",
45e84a0
+        if (fgetxattr(fd, "user.virtfs.gid",
45e84a0
                       &tmp_gid, sizeof(gid_t)) > 0) {
45e84a0
             stbuf->st_gid = tmp_gid;
45e84a0
         }
45e84a0
-        if (fgetxattr(fs->fd, "user.virtfs.mode",
45e84a0
+        if (fgetxattr(fd, "user.virtfs.mode",
45e84a0
                       &tmp_mode, sizeof(mode_t)) > 0) {
45e84a0
             stbuf->st_mode = tmp_mode;
45e84a0
         }
45e84a0
-        if (fgetxattr(fs->fd, "user.virtfs.rdev",
45e84a0
+        if (fgetxattr(fd, "user.virtfs.rdev",
45e84a0
                       &tmp_dev, sizeof(dev_t)) > 0) {
45e84a0
                 stbuf->st_rdev = tmp_dev;
45e84a0
         }
45e84a0
@@ -592,12 +599,21 @@ static int local_remove(FsContext *ctx, const char *path)
45e84a0
     return remove(rpath(ctx, path, buffer));
45e84a0
 }
45e84a0
45e84a0
-static int local_fsync(FsContext *ctx, V9fsFidOpenState *fs, int datasync)
45e84a0
+static int local_fsync(FsContext *ctx, int fid_type,
45e84a0
+                       V9fsFidOpenState *fs, int datasync)
45e84a0
 {
45e84a0
+    int fd;
45e84a0
+
45e84a0
+    if (fid_type == P9_FID_DIR) {
45e84a0
+        fd = dirfd(fs->dir);
45e84a0
+    } else {
45e84a0
+        fd = fs->fd;
45e84a0
+    }
45e84a0
+
45e84a0
     if (datasync) {
45e84a0
-        return qemu_fdatasync(fs->fd);
45e84a0
+        return qemu_fdatasync(fd);
45e84a0
     } else {
45e84a0
-        return fsync(fs->fd);
45e84a0
+        return fsync(fd);
45e84a0
     }
45e84a0
 }
45e84a0
45e84a0
diff --git a/hw/9pfs/virtio-9p-synth.c b/hw/9pfs/virtio-9p-synth.c
45e84a0
index f573616..92e0b09 100644
45e84a0
--- a/hw/9pfs/virtio-9p-synth.c
45e84a0
+++ b/hw/9pfs/virtio-9p-synth.c
45e84a0
@@ -166,7 +166,7 @@ static int v9fs_synth_lstat(FsContext *fs_ctx,
45e84a0
     return 0;
45e84a0
 }
45e84a0
45e84a0
-static int v9fs_synth_fstat(FsContext *fs_ctx,
45e84a0
+static int v9fs_synth_fstat(FsContext *fs_ctx, int fid_type,
45e84a0
                             V9fsFidOpenState *fs, struct stat *stbuf)
45e84a0
 {
45e84a0
     V9fsSynthOpenState *synth_open = fs->private;
45e84a0
@@ -414,7 +414,8 @@ static int v9fs_synth_remove(FsContext *ctx, const char *path)
45e84a0
     return -1;
45e84a0
 }
45e84a0
45e84a0
-static int v9fs_synth_fsync(FsContext *ctx, V9fsFidOpenState *fs, int datasync)
45e84a0
+static int v9fs_synth_fsync(FsContext *ctx, int fid_type,
45e84a0
+                            V9fsFidOpenState *fs, int datasync)
45e84a0
 {
45e84a0
     errno = ENOSYS;
45e84a0
     return 0;
45e84a0
-- 
45e84a0
1.7.7.5
45e84a0