# HG changeset patch # User Paul Boddie # Date 1549557014 -3600 # Node ID a9a64a18003f7e604ce734292feea636bde6f956 A virtual filesystem server. diff -r 000000000000 -r a9a64a18003f Control --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Control Thu Feb 07 17:30:14 2019 +0100 @@ -0,0 +1,3 @@ +requires: libc libipc libfsclient libfsserver +provides: vfsserver +maintainer: paul@boddie.org.uk diff -r 000000000000 -r a9a64a18003f Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Thu Feb 07 17:30:14 2019 +0100 @@ -0,0 +1,4 @@ +PKGDIR ?= . +L4DIR ?= $(PKGDIR)/../.. + +include $(L4DIR)/mk/subdir.mk diff -r 000000000000 -r a9a64a18003f server/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/Makefile Thu Feb 07 17:30:14 2019 +0100 @@ -0,0 +1,4 @@ +PKGDIR ?= .. +L4DIR ?= $(PKGDIR)/../.. + +include $(L4DIR)/mk/subdir.mk diff -r 000000000000 -r a9a64a18003f server/src/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/Makefile Thu Feb 07 17:30:14 2019 +0100 @@ -0,0 +1,11 @@ +PKGDIR ?= ../.. +L4DIR ?= $(PKGDIR)/../.. + +TARGET = vfsserver +MODE = shared + +SRC_CC = main.cc fspath.cc + +REQUIRES_LIBS = l4re_c l4re_c-util libipc libfsclient libfsserver + +include $(L4DIR)/mk/prog.mk diff -r 000000000000 -r a9a64a18003f server/src/fspath.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/fspath.cc Thu Feb 07 17:30:14 2019 +0100 @@ -0,0 +1,42 @@ +/* + * Filesystem path utilities. + * + * Copyright (C) 2018 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include + +#include "fspath.h" + +void rewrite_path(char *path, const char *prefix) +{ + size_t pathlen = strlen(path), + prefixlen = strlen(prefix), + newpathlen = pathlen - prefixlen; + const char *newpath = path + prefixlen; + + /* Copy the new path (minus the prefix) to the base address. */ + + memmove(path, newpath, strlen(newpath)); + + /* Overwrite remaining traces of the old path. Since the new path has been + relocated over the prefix, the remaining traces are as long as the + prefix. */ + + memset(path + newpathlen, 0, prefixlen); +} diff -r 000000000000 -r a9a64a18003f server/src/fspath.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/fspath.h Thu Feb 07 17:30:14 2019 +0100 @@ -0,0 +1,24 @@ +/* + * Filesystem path utilities. + * + * Copyright (C) 2018 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#pragma once + +void rewrite_path(char *path, const char *prefix); diff -r 000000000000 -r a9a64a18003f server/src/main.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/main.cc Thu Feb 07 17:30:14 2019 +0100 @@ -0,0 +1,297 @@ +/* + * Access several filesystems. + * + * Copyright (C) 2018, 2019 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include "fspath.h" + + + +/* Virtual filesystem server. */ + +class Vfs_server : public Server +{ +private: + /* NOTE: Improvised structure! */ + + l4_cap_idx_t _servers[10]; + const char *_mountpoints[10]; + int _mounted = 0; + +public: + explicit Vfs_server() + { + } + + void dispatch(ipc_message_t *msg) + { + switch (l4_msgtag_label(msg->tag)) + { + case Fs_op_mount: + mount(msg); + break; + + case Fs_op_open: + open(Fs_op_open, msg); + break; + + case Fs_op_getfs: + open(Fs_op_getfs, msg); + break; + + default: + error(msg, -L4_EBADPROTO); + break; + } + } + + void open(int op, ipc_message_t *msg) + { + l4_cap_idx_t server; + l4re_ds_t ds; + size_t size; + char *buffer; + long err; + + /* Obtain the dataspace. */ + + err = import_dataspace(msg, 0, &ds, (l4_addr_t *) &buffer); + if (err) + { + error(msg, -L4_EIO); + return; + } + + /* Match the path to a mountpoint. */ + + int selected = find_mountpoint(buffer); + + if (selected < 0) + { + error(msg, -L4_ENOENT); + return; + } + + /* Rewrite the path for the selected filesystem. */ + + rewrite_path(buffer, _mountpoints[selected]); + + /* Find the path within the selected filesystem. */ + + /* Propagate the message to the identified filesystem. + NOTE: The L4_MSGTAG_PROPAGATE flag is not supported by Fiasco.OC, so this + NOTE: object has to act as intermediary. */ + + err = fs_ipc_open(_servers[selected], op, ds, &server, &size); + if (err) + { + error(msg, -L4_EIO); + return; + } + + /* Free the capability and buffer from this task. */ + + discard_dataspace(ds, (l4_addr_t) buffer); + + /* Return the file size. */ + + ipc_message_add_word(msg, size); + + /* Export and eventually free the server reference from this task. */ + + ipc_message_propagate_item(msg, server); + } + + void mount(ipc_message_t *msg) + { + l4_cap_idx_t server; + l4re_ds_t ds; + char *buffer; + long err; + + /* Obtain the filesystem capability. */ + + err = import_capability(msg, 0, &server); + if (err) + { + error(msg, -L4_EIO); + return; + } + + /* Obtain the dataspace. */ + + err = import_dataspace(msg, 1, &ds, (l4_addr_t *) &buffer); + if (err) + { + error(msg, -L4_EIO); + return; + } + + /* Obtain the object's path. */ + + char *path = buffer; + + /* Mount the supplied capability. */ + + mount_at_path(server, path); + + /* Free the capability and buffer from this task. */ + + discard_dataspace(ds, (l4_addr_t) buffer); + } + + /* Non-exported methods. */ + + void mount_at_path(l4_cap_idx_t server, const char *path) + { + /* NOTE: Should return error, terminate with path separator if absent. */ + + if (_mounted >= 10) + return; + + _servers[_mounted] = server; + _mountpoints[_mounted] = strdup(path); + + _mounted++; + } + + int find_mountpoint(const char *path) + { + int i, matching; + size_t length, longest; + + if (!_mounted) + return -1; + + matching = -1; + longest = 0; + + /* Find the largest matching path. */ + + for (i = 0; i < _mounted; i++) + { + length = strlen(_mountpoints[i]); + + /* The empty mountpoint always matches; + suffixed mountpoints may match as prefixes; + suffixed mountpoints may match unsuffixed paths; + unsuffixed mountpoints must match exactly. */ + + /* NOTE: Unsuffixed mountpoints might support subpaths by insisting on + a path separator as the first subsequent character. */ + + if (!length || + ((_mountpoints[i][length - 1] == '/') && !strncmp(_mountpoints[i], path, length)) || + ((_mountpoints[i][length - 1] == '/') && (strlen(path) == length - 1) && !strncmp(_mountpoints[i], path, length - 1)) || + !strcmp(_mountpoints[i], path)) + { + if (length >= longest) + matching = i; + } + } + + return matching; + } +}; + + + +int main(int argc, char *argv[]) +{ + FILE *fp; + char buffer[256], *sep; + l4_cap_idx_t fscap, server; + + if (argc < 2) + { + printf("Need a mount table.\n"); + return 1; + } + + /* Initialise and register a new server object. */ + + Vfs_server server_obj; + + if (bind_to_capability("export", l4_umword_t(&server_obj), &server)) + { + printf("Could not bind thread.\n"); + return 1; + } + + /* Mount filesystems. */ + + fp = fopen(argv[1], "r"); + if (fp == NULL) + { + printf("Could not open %s.\n", argv[1]); + return 1; + } + + /* Obtain capabilities for mountpoints. */ + + while (fgets(buffer, 256, fp) != NULL) + { + sep = strchr(buffer, (int) '\t'); + if (sep == NULL) + continue; + + /* Terminate newline-suffixed strings. */ + + if (sep[strlen(sep) - 1] == '\n') + sep[strlen(sep) - 1] = '\0'; + + /* Obtain the named capability. */ + + fscap = l4re_env_get_cap(sep + 1); + + if (l4_is_invalid_cap(fscap)) + continue; + + /* Terminate the path and register the mountpoint. */ + + *sep = '\0'; + server_obj.mount_at_path(fscap, buffer); + } + + fclose(fp); + + /* Enter the IPC server loop expecting at most two items. */ + + server_loop(2); + return 0; +}