[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH 1/3] file: Auf LIOv2 umstellen



+ file: Es werden jetzt zwei Möglichkeiten unterstützt, file:/ zu
  implementieren. Wenn der file-Service gestartet wird, dann wird die
  alte Methode verwendet, die für LIOv1-Services (z.B. FAT bei der
  Floppy-Variante) benötigt wird. Für einen LIOv2-Service auf file:/
  darf der Service dagegen nicht gestartet werden, dann wird der Symlink
  im kerneleigenen file-Service gesetzt (z.B. für ext2 nach dem
  nächsten Patch).

Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
 src/kernel2/src/lostio/lostio.c       |    2 +
 src/kernel2/src/lostio/modules/file.c |  121 +++++++++++++++++++++++++++++++++
 src/modules/file/client.c             |   33 +++++++--
 src/modules/file/main.c               |    2 +-
 4 files changed, 149 insertions(+), 9 deletions(-)
 create mode 100644 src/kernel2/src/lostio/modules/file.c

diff --git a/src/kernel2/src/lostio/lostio.c b/src/kernel2/src/lostio/lostio.c
index 54e2182..a9eb2d3 100644
--- a/src/kernel2/src/lostio/lostio.c
+++ b/src/kernel2/src/lostio/lostio.c
@@ -39,6 +39,7 @@
 #include "lostio_int.h"
 
 extern void lio_init_tmp(void);
+extern void lio_init_file(void);
 
 void lio_init(void)
 {
@@ -46,4 +47,5 @@ void lio_init(void)
     lio_init_userspace();
 
     lio_init_tmp();
+    lio_init_file();
 }
diff --git a/src/kernel2/src/lostio/modules/file.c b/src/kernel2/src/lostio/modules/file.c
new file mode 100644
index 0000000..708d969
--- /dev/null
+++ b/src/kernel2/src/lostio/modules/file.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2010 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Antoine Kaufmann.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the tyndur Project
+ *     and its contributors.
+ * 4. Neither the name of the tyndur Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <string.h>
+#include <stdlib.h>
+
+#include "lostio_int.h"
+#include "kprintf.h"
+
+#define BLOCKSIZE 128
+
+static struct lio_resource* load_root(struct lio_tree* tree);
+static int file_read(struct lio_resource* res, uint64_t offset,
+    size_t bytes, void* buf);
+static int file_write(struct lio_resource* res, uint64_t offset,
+    size_t bytes, void* buf);
+static int truncate(struct lio_resource* res, uint64_t size);
+
+static struct lio_resource root_res = {
+    .size = 0,
+    .blocksize = BLOCKSIZE,
+    .resolvable = 1,
+    .retargetable = 1,
+    .opaque = NULL,
+};
+
+static char* link = NULL;
+static size_t link_len = 0;
+
+static struct lio_resource* load_root(struct lio_tree* tree)
+{
+    if (tree->source) {
+        return NULL;
+    }
+
+    lio_init_resource(&root_res);
+    return &root_res;
+}
+
+static int file_read(struct lio_resource* res, uint64_t offset,
+    size_t bytes, void* buf)
+{
+    if (link) {
+        if (bytes > link_len) {
+            memset(buf + link_len, 0, bytes - link_len);
+            bytes = link_len;
+        }
+        memcpy(buf, link + offset, bytes);
+    }
+
+    return 0;
+}
+
+static int file_write(struct lio_resource* res, uint64_t offset,
+    size_t bytes, void* buf)
+{
+    if (offset + bytes >= link_len) {
+        truncate(res, offset + bytes);
+    }
+
+    if (link) {
+        memcpy(link + offset, buf, bytes);
+    }
+
+    return 0;
+}
+
+static int truncate(struct lio_resource* res, uint64_t size)
+{
+    link_len = size;
+    link = realloc(link, size);
+    memset(link, 0, size);
+    return 0;
+}
+
+static struct lio_service service = {
+    .name = "file",
+    .lio_ops = {
+        .load_root  = load_root,
+        .read       = file_read,
+        .write      = file_write,
+        .truncate   = truncate,
+    },
+};
+
+void lio_init_file(void)
+{
+    lio_add_service(&service);
+}
diff --git a/src/modules/file/client.c b/src/modules/file/client.c
index f15e416..988df70 100644
--- a/src/modules/file/client.c
+++ b/src/modules/file/client.c
@@ -56,16 +56,33 @@ int client(int argc, char* argv[], pid_t file_service)
     if (!strcmp(argv[1], "mount") && (argc == 4)) {
         size_t len_src = strlen(argv[2]) + 1;
         size_t len_dst = strlen(argv[3]) + 1;
-        size_t len = RPC_FUNCTION_NAME_LENGTH + len_src + len_dst;
 
-        char data[len];
-        memset(data, 0, RPC_FUNCTION_NAME_LENGTH);
-        strncpy(data, "MOUNT", RPC_FUNCTION_NAME_LENGTH);
-        strncpy(data + RPC_FUNCTION_NAME_LENGTH, argv[2], len_src);
-        strncpy(data + RPC_FUNCTION_NAME_LENGTH + len_src, argv[3], len_dst);
+        if (file_service) {
+            size_t len = RPC_FUNCTION_NAME_LENGTH + len_src + len_dst;
+            char data[len];
+
+            memset(data, 0, RPC_FUNCTION_NAME_LENGTH);
+            strncpy(data, "MOUNT", RPC_FUNCTION_NAME_LENGTH);
+            strncpy(data + RPC_FUNCTION_NAME_LENGTH, argv[2], len_src);
+            strncpy(data + RPC_FUNCTION_NAME_LENGTH + len_src, argv[3],
+                len_dst);
+
+            send_message(file_service, RPC_MESSAGE, 0, len, data);
+        } else {
+            lio_resource_t res = -2;
+            lio_stream_t s = -2;
+
+            if (strcmp(argv[2], "file:/") ||
+                ((res = lio_resource("file:/", 0)) < 0) ||
+                ((s = lio_open(res, LIO_SYMLINK | LIO_WRITE | LIO_TRUNC)) < 0))
+            {
+                printf("Err: (%s) %llx %llx\n", argv[2], res, s);
+                return EXIT_FAILURE;
+            }
+            lio_write(s, len_dst - 1, argv[3]);
+            lio_close(s);
+        }
 
-        send_message(file_service, RPC_MESSAGE, 0, len, data);
-        
         return EXIT_SUCCESS;
     } else {
         client_print_usage();
diff --git a/src/modules/file/main.c b/src/modules/file/main.c
index 6ff34bc..da12bb7 100644
--- a/src/modules/file/main.c
+++ b/src/modules/file/main.c
@@ -41,7 +41,7 @@
 int main(int argc, char* argv[])
 {
     pid_t file_service = init_service_get(SERVICE_NAME);
-    if (file_service) {
+    if (argc > 1 || file_service) {
         return client(argc, argv, file_service);
     } else {
         return server();
-- 
1.7.7