From: "M. Mohan Kumar" <mo...@in.ibm.com>

Add interfaces to create filesystem objects like directory,
device nodes, symbolic links, links for proxy filesytem driver

Signed-off-by: M. Mohan Kumar <mo...@in.ibm.com>
---
 fsdev/virtfs-proxy-helper.c |  120 ++++++++++++++++++++++++++--
 hw/9pfs/virtio-9p-proxy.c   |  185 +++++++++++++++++++++++++++++++++++++++----
 hw/9pfs/virtio-9p-proxy.h   |    8 ++-
 3 files changed, 287 insertions(+), 26 deletions(-)

diff --git a/fsdev/virtfs-proxy-helper.c b/fsdev/virtfs-proxy-helper.c
index 867fdcc..246be77 100644
--- a/fsdev/virtfs-proxy-helper.c
+++ b/fsdev/virtfs-proxy-helper.c
@@ -216,6 +216,28 @@ static void send_fd(int sockfd, int fd)
     }
 }
 
+static int send_status(int sockfd, struct iovec *iovec, int status)
+{
+    int retval, msg_size;;
+    ProxyHeader header;
+
+    if (status < 0) {
+        header.type = T_ERROR;
+    } else {
+        header.type = T_SUCCESS;
+    }
+    header.size = sizeof(status);
+
+    /* marshal the return status */
+    msg_size = proxy_marshal(iovec, 1, 0, "ddd", header.type, header.size,
+                    status);
+    retval = socket_write(sockfd, iovec->iov_base, msg_size);
+    if (retval != msg_size) {
+        return -EIO;
+    }
+    return 0;
+}
+
 /*
  * from man 7 capabilities, section
  * Effect of User ID Changes on Capabilities:
@@ -235,6 +257,59 @@ static int setfsugid(int uid, int gid)
 }
 
 /*
+ * create a other filesystem objects and send 0 on success
+ * return -errno on error
+ */
+static int do_create_others(int type, struct iovec *iovec)
+{
+    dev_t rdev;
+    int retval = 0;
+    V9fsString oldpath, path;
+    int mode, uid, gid, cur_uid, cur_gid;
+    int offset = HDR_SZ;
+
+    cur_uid = geteuid();
+    cur_gid = getegid();
+
+    offset += proxy_unmarshal(iovec, 1, offset, "dd", &uid, &gid);
+    if (setfsugid(uid, gid) < 0) {
+        return -EPERM;
+    }
+    switch (type) {
+    case T_MKNOD:
+        if (proxy_unmarshal(iovec, 1, offset, "sdq", &path, &mode, &rdev) < 0) 
{
+            retval = -EOVERFLOW;
+            goto out;
+        }
+        retval = mknod(path.data, mode, rdev);
+        break;
+    case T_MKDIR:
+        if (proxy_unmarshal(iovec, 1, offset, "sd", &path, &mode) < 0) {
+            retval = -EOVERFLOW;
+            goto out;
+        }
+        retval = mkdir(path.data, mode);
+        break;
+    case T_SYMLINK:
+        if (proxy_unmarshal(iovec, 1, offset, "ss", &oldpath, &path) < 0) {
+            retval = -EOVERFLOW;
+            goto out;
+        }
+        retval = symlink(oldpath.data, path.data);
+        v9fs_string_free(&oldpath);
+        break;
+    }
+
+    if (retval < 0) {
+        retval = -errno;
+    }
+    v9fs_string_free(&path);
+out:
+    setfsugid(cur_uid, cur_gid);
+    return retval;
+}
+
+/*
  * create a file and send fd on success
  * return -errno on error
  */
@@ -294,38 +369,65 @@ static void usage(char *prog)
 static int process_requests(int sock)
 {
     int type, retval = 0;
-    struct iovec iovec;
+    V9fsString oldpath, path;
+    struct iovec in_iovec, out_iovec;
+
+    in_iovec.iov_base = g_malloc(BUFF_SZ);
+    in_iovec.iov_len = BUFF_SZ;
+    out_iovec.iov_base = g_malloc(BUFF_SZ);
+    out_iovec.iov_len = BUFF_SZ;
 
-    iovec.iov_base = g_malloc(BUFF_SZ);
-    iovec.iov_len = BUFF_SZ;
     while (1) {
-        type = read_request(sock, &iovec);
+        type = read_request(sock, &in_iovec);
         switch (type) {
         case T_OPEN:
-            retval = do_open(&iovec);
+            retval = do_open(&in_iovec);
             break;
         case T_CREATE:
-            retval = do_create(&iovec);
+            retval = do_create(&in_iovec);
+            break;
+        case T_MKNOD:
+        case T_MKDIR:
+        case T_SYMLINK:
+            retval = do_create_others(type, &in_iovec);
+            break;
+        case T_LINK:
+            if (proxy_unmarshal(&in_iovec, 1, HDR_SZ, "ss",
+                                    &oldpath, &path) < 0) {
+                retval = -EOVERFLOW;
+                break;
+            }
+            retval = link(oldpath.data, path.data);
+            if (retval < 0) {
+                retval = -errno;
+            }
+            v9fs_string_free(&oldpath);
+            v9fs_string_free(&path);
             break;
         default:
             goto error;
             break;
         }
-
         /* Send response */
         switch (type) {
         case T_OPEN:
         case T_CREATE:
             send_fd(sock, retval);
             break;
+        case T_MKNOD:
+        case T_MKDIR:
+        case T_SYMLINK:
+        case T_LINK:
+            send_status(sock, &out_iovec, retval);
+            break;
         default:
             goto error;
             break;
         }
     }
-    (void)socket_write;
 error:
-    g_free(iovec.iov_base);
+    g_free(in_iovec.iov_base);
+    g_free(out_iovec.iov_base);
     return -1;
 }
 
diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c
index 3472337..f116773 100644
--- a/hw/9pfs/virtio-9p-proxy.c
+++ b/hw/9pfs/virtio-9p-proxy.c
@@ -19,7 +19,8 @@
 typedef struct V9fsProxy {
     int sockfd;
     QemuMutex mutex;
-    struct iovec iovec;
+    struct iovec in_iovec;
+    struct iovec out_iovec;
 } V9fsProxy;
 
 /*
@@ -79,6 +80,38 @@ static int v9fs_receivefd(int sockfd, int *sock_error)
     return -ENFILE; /* Ancillary data sent but not received */
 }
 
+static ssize_t socket_read(int sockfd, void *buff, size_t size)
+{
+    ssize_t retval;
+    do {
+        retval = read(sockfd, buff, size);
+    } while (retval < 0 && errno == EINTR);
+    return retval;
+}
+
+
+static int v9fs_receive_status(V9fsProxy *proxy, int *sock_error,
+                struct iovec *reply)
+{
+    ProxyHeader header;
+    int retval;
+
+    *sock_error = 0;
+    retval = socket_read(proxy->sockfd, reply->iov_base, HDR_SZ);
+    if (retval != HDR_SZ) {
+        *sock_error = 1;
+        return -EIO;
+    }
+    proxy_unmarshal(reply, 1, 0, "dd", &header.type, &header.size);
+    retval = socket_read(proxy->sockfd, reply->iov_base + HDR_SZ, header.size);
+    if (retval != header.size) {
+        *sock_error = 1;
+        return -EIO;
+    }
+    proxy_unmarshal(reply, 1, HDR_SZ, "d", &retval);
+    return retval;
+}
+
 /*
  * Proxy->header and proxy->request written to socket by QEMU process.
  * This request read by proxy helper process
@@ -90,17 +123,18 @@ static int v9fs_request(V9fsProxy *proxy, int type,
     int retval;
     ProxyHeader header;
     va_list ap;
-    V9fsString *path;
+    V9fsString *path, *oldpath;
     int sock_error, flags, mode, uid, gid;
-    struct iovec *iovec = NULL;
+    struct iovec *iovec = NULL, *reply = NULL;
+    dev_t rdev;
 
     qemu_mutex_lock(&proxy->mutex);
 
     if (proxy->sockfd == -1) {
         goto error;
     }
-    iovec = &proxy->iovec;
-
+    iovec = &proxy->out_iovec;
+    reply = &proxy->in_iovec;
     va_start(ap, fmt);
     switch (type) {
     case T_OPEN:
@@ -129,6 +163,61 @@ static int v9fs_request(V9fsProxy *proxy, int type,
         proxy_marshal(iovec, 1, 0, "dd", header.type, header.size);
         header.size += HDR_SZ;
         break;
+    case T_MKNOD:
+        path = va_arg(ap, V9fsString *);
+        mode = va_arg(ap, int);
+        rdev = va_arg(ap, long int);
+        uid = va_arg(ap, int);
+        gid = va_arg(ap, int);
+        header.size = proxy_marshal(iovec, 1, HDR_SZ, "ddsdq",
+                                   uid, gid, path, mode, rdev);
+        if (header.size < 0) {
+            goto out_overflow;
+        }
+        header.type = T_MKNOD;
+        proxy_marshal(iovec, 1, 0, "dd", header.type, header.size);
+        header.size += HDR_SZ;
+        break;
+    case T_MKDIR:
+        path = va_arg(ap, V9fsString *);
+        mode = va_arg(ap, int);
+        uid = va_arg(ap, int);
+        gid = va_arg(ap, int);
+        header.size = proxy_marshal(iovec, 1, HDR_SZ, "ddsd",
+                                   uid, gid, path, mode);
+        if (header.size < 0) {
+            goto out_overflow;
+        }
+        header.type = T_MKDIR;
+        proxy_marshal(iovec, 1, 0, "dd", header.type, header.size);
+        header.size += HDR_SZ;
+        break;
+    case T_SYMLINK:
+        oldpath = va_arg(ap, V9fsString *);
+        path = va_arg(ap, V9fsString *);
+        uid = va_arg(ap, int);
+        gid = va_arg(ap, int);
+        header.size = proxy_marshal(iovec, 1, HDR_SZ, "ddss",
+                                   uid, gid, oldpath, path);
+        if (header.size < 0) {
+            goto out_overflow;
+       }
+        header.type = T_SYMLINK;
+        proxy_marshal(iovec, 1, 0, "dd", header.type, header.size);
+        header.size += HDR_SZ;
+        break;
+    case T_LINK:
+        oldpath = va_arg(ap, V9fsString *);
+        path = va_arg(ap, V9fsString *);
+        header.size = proxy_marshal(iovec, 1, HDR_SZ, "ss",
+                                   oldpath, path);
+        if (header.size < 0) {
+            goto out_overflow;
+       }
+        header.type = T_LINK;
+        proxy_marshal(iovec, 1, 0, "dd", header.type, header.size);
+        header.size += HDR_SZ;
+        break;
     default:
         error_report("Invalid type %d\n", type);
         va_end(ap);
@@ -154,6 +243,15 @@ static int v9fs_request(V9fsProxy *proxy, int type,
             goto close_error;
         }
         break;
+    case T_MKNOD:
+    case T_MKDIR:
+    case T_SYMLINK:
+    case T_LINK:
+        retval = v9fs_receive_status(proxy, &sock_error, reply);
+        if (sock_error) {
+            goto close_error;
+        }
+        break;
     }
     qemu_mutex_unlock(&proxy->mutex);
     return retval;
@@ -301,15 +399,40 @@ static int proxy_chmod(FsContext *fs_ctx, V9fsPath 
*fs_path, FsCred *credp)
 static int proxy_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
                        const char *name, FsCred *credp)
 {
-    errno = EOPNOTSUPP;
-    return -1;
+    V9fsString fullname;
+    int retval;
+    v9fs_string_init(&fullname);
+    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+
+    retval = v9fs_request(fs_ctx->private, T_MKNOD, NULL, "sdqdd",
+                          &fullname, credp->fc_mode, credp->fc_rdev,
+                          credp->fc_uid, credp->fc_gid);
+    v9fs_string_free(&fullname);
+    if (retval < 0) {
+        errno = -retval;
+        retval = -1;
+    }
+    return retval;
 }
 
 static int proxy_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
                        const char *name, FsCred *credp)
 {
-    errno = EOPNOTSUPP;
-    return -1;
+    V9fsString fullname;
+    int retval;
+
+    v9fs_string_init(&fullname);
+    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+
+    retval = v9fs_request(fs_ctx->private, T_MKDIR, NULL, "sddd", &fullname,
+                          credp->fc_mode, credp->fc_uid, credp->fc_gid);
+    v9fs_string_free(&fullname);
+    if (retval < 0) {
+        errno = -retval;
+        retval = -1;
+    }
+    v9fs_string_free(&fullname);
+    return retval;
 }
 
 static int proxy_fstat(FsContext *fs_ctx,
@@ -339,19 +462,46 @@ static int proxy_open2(FsContext *fs_ctx, V9fsPath 
*dir_path, const char *name,
     return fs->fd;
 }
 
-
 static int proxy_symlink(FsContext *fs_ctx, const char *oldpath,
                          V9fsPath *dir_path, const char *name, FsCred *credp)
 {
-    errno = EOPNOTSUPP;
-    return -1;
+    V9fsString fullname, target;
+    int retval;
+
+    v9fs_string_init(&fullname);
+    v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
+    v9fs_string_init(&target);
+    v9fs_string_sprintf(&target, "%s", oldpath);
+
+    retval = v9fs_request(fs_ctx->private, T_SYMLINK, NULL, "ssdd",
+                          &target, &fullname, credp->fc_uid, credp->fc_gid);
+    v9fs_string_free(&fullname);
+    v9fs_string_free(&target);
+    if (retval < 0) {
+        errno = -retval;
+        retval = -1;
+    }
+    return retval;
 }
 
 static int proxy_link(FsContext *ctx, V9fsPath *oldpath,
                       V9fsPath *dirpath, const char *name)
 {
-    errno = EOPNOTSUPP;
-    return -1;
+    int retval;
+    V9fsString newpath;
+
+    v9fs_string_init(&newpath);
+    v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
+
+    retval = v9fs_request(ctx->private, T_LINK, NULL, "ss",
+                          oldpath, &newpath);
+    v9fs_string_free(&newpath);
+    if (retval < 0) {
+        errno = -retval;
+        retval = -1;
+    }
+    v9fs_string_free(&newpath);
+    return retval;
 }
 
 static int proxy_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
@@ -500,8 +650,11 @@ static int proxy_init(FsContext *ctx)
     }
     g_free(ctx->fs_root);
 
-    proxy->iovec.iov_base = g_malloc(BUFF_SZ);
-    proxy->iovec.iov_len = BUFF_SZ;
+    proxy->in_iovec.iov_base = g_malloc(BUFF_SZ);
+    proxy->in_iovec.iov_len = BUFF_SZ;
+    proxy->out_iovec.iov_base = g_malloc(BUFF_SZ);
+    proxy->out_iovec.iov_len = BUFF_SZ;
+
     ctx->private = proxy;
     proxy->sockfd = sock_id;
     qemu_mutex_init(&proxy->mutex);
diff --git a/hw/9pfs/virtio-9p-proxy.h b/hw/9pfs/virtio-9p-proxy.h
index 27f2507..9a43f64 100644
--- a/hw/9pfs/virtio-9p-proxy.h
+++ b/hw/9pfs/virtio-9p-proxy.h
@@ -22,8 +22,14 @@ typedef struct {
 #define HDR_SZ (sizeof(ProxyHeader))
 
 enum {
-    T_OPEN = 1,
+    T_SUCCESS = 0,
+    T_ERROR,
+    T_OPEN,
     T_CREATE,
+    T_MKNOD,
+    T_MKDIR,
+    T_SYMLINK,
+    T_LINK,
 };
 
 #endif
-- 
1.7.6


Reply via email to