diff mbox

[V2,05/12] hw/9pfs: Create other filesystem objects

Message ID 1321358265-10924-6-git-send-email-mohan@in.ibm.com
State New
Headers show

Commit Message

Mohan Kumar M Nov. 15, 2011, 11:57 a.m. UTC
Add interfaces to create filesystem objects like directory,
device nodes, symbolic links, links for proxy filesytem driver

Signed-off-by: M. Mohan Kumar <mohan@in.ibm.com>
---
 fsdev/virtfs-proxy-helper.c |  105 ++++++++++++++++++++++++--
 hw/9pfs/virtio-9p-proxy.c   |  173 +++++++++++++++++++++++++++++++++++++++----
 hw/9pfs/virtio-9p-proxy.h   |    8 ++-
 3 files changed, 261 insertions(+), 25 deletions(-)
diff mbox

Patch

diff --git a/fsdev/virtfs-proxy-helper.c b/fsdev/virtfs-proxy-helper.c
index 68d27f1..377e91a 100644
--- a/fsdev/virtfs-proxy-helper.c
+++ b/fsdev/virtfs-proxy-helper.c
@@ -210,6 +210,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:
@@ -229,6 +251,49 @@  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:
+        proxy_unmarshal(iovec, 1, offset, "sdq", &path, &mode, &rdev);
+        retval = mknod(path.data, mode, rdev);
+        break;
+    case T_MKDIR:
+        proxy_unmarshal(iovec, 1, offset, "sd", &path, &mode);
+        retval = mkdir(path.data, mode);
+        break;
+    case T_SYMLINK:
+        proxy_unmarshal(iovec, 1, offset, "ss", &oldpath, &path);
+        retval = symlink(oldpath.data, path.data);
+        v9fs_string_free(&oldpath);
+        break;
+    }
+
+    if (retval < 0) {
+        retval = -errno;
+    }
+    v9fs_string_free(&path);
+    setfsugid(cur_uid, cur_gid);
+    return retval;
+}
+
+/*
  * create a file and send fd on success
  * return -errno on error
  */
@@ -281,18 +346,36 @@  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:
+            proxy_unmarshal(&in_iovec, 1, HDR_SZ, "ss", &oldpath, &path);
+            retval = link(oldpath.data, path.data);
+            if (retval < 0) {
+                retval = -errno;
+            }
+            v9fs_string_free(&oldpath);
+            v9fs_string_free(&path);
             break;
         default:
             goto error;
@@ -305,14 +388,20 @@  static int process_requests(int sock)
         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 8cc55d6..683d762 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:
@@ -124,6 +158,49 @@  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);
+        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);
+        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);
+        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);
+        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);
@@ -149,6 +226,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;
@@ -294,15 +380,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,
@@ -332,19 +443,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)
@@ -493,8 +631,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 f30e367..32610e0 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