paul@0 | 1 | /* |
paul@0 | 2 | * Access several filesystems. |
paul@0 | 3 | * |
paul@0 | 4 | * Copyright (C) 2018, 2019 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software; you can redistribute it and/or |
paul@0 | 7 | * modify it under the terms of the GNU General Public License as |
paul@0 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 9 | * the License, or (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program; if not, write to the Free Software |
paul@0 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/util/util.h> |
paul@0 | 23 | |
paul@0 | 24 | #include <l4/re/c/dataspace.h> |
paul@0 | 25 | #include <l4/re/env.h> |
paul@0 | 26 | #include <l4/re/c/util/cap_alloc.h> |
paul@0 | 27 | #include <l4/sys/err.h> |
paul@0 | 28 | #include <l4/sys/kernel_object.h> |
paul@0 | 29 | #include <l4/sys/types.h> |
paul@0 | 30 | |
paul@0 | 31 | #include <stdlib.h> |
paul@0 | 32 | #include <stdio.h> |
paul@0 | 33 | #include <string.h> |
paul@0 | 34 | #include <unistd.h> |
paul@0 | 35 | |
paul@0 | 36 | #include <fsclient/fs_client.h> |
paul@0 | 37 | #include <fsclient/fsdesc.h> |
paul@0 | 38 | #include <fsclient/fs_ipc.h> |
paul@0 | 39 | #include <fsserver/server.h> |
paul@0 | 40 | #include "fspath.h" |
paul@0 | 41 | |
paul@0 | 42 | |
paul@0 | 43 | |
paul@0 | 44 | /* Virtual filesystem server. */ |
paul@0 | 45 | |
paul@5 | 46 | class Vfs_server : public OpeningServer |
paul@0 | 47 | { |
paul@0 | 48 | private: |
paul@0 | 49 | /* NOTE: Improvised structure! */ |
paul@0 | 50 | |
paul@0 | 51 | l4_cap_idx_t _servers[10]; |
paul@0 | 52 | const char *_mountpoints[10]; |
paul@0 | 53 | int _mounted = 0; |
paul@0 | 54 | |
paul@0 | 55 | public: |
paul@0 | 56 | explicit Vfs_server() |
paul@5 | 57 | : OpeningServer() |
paul@0 | 58 | { |
paul@0 | 59 | } |
paul@0 | 60 | |
paul@0 | 61 | void dispatch(ipc_message_t *msg) |
paul@0 | 62 | { |
paul@0 | 63 | switch (l4_msgtag_label(msg->tag)) |
paul@0 | 64 | { |
paul@0 | 65 | case Fs_op_mount: |
paul@0 | 66 | mount(msg); |
paul@0 | 67 | break; |
paul@0 | 68 | |
paul@0 | 69 | case Fs_op_open: |
paul@6 | 70 | open(msg); |
paul@0 | 71 | break; |
paul@0 | 72 | |
paul@0 | 73 | case Fs_op_getfs: |
paul@6 | 74 | getfs(msg); |
paul@0 | 75 | break; |
paul@0 | 76 | |
paul@0 | 77 | default: |
paul@10 | 78 | ipc_message_send_error(msg, -L4_EBADPROTO); |
paul@0 | 79 | break; |
paul@0 | 80 | } |
paul@0 | 81 | } |
paul@0 | 82 | |
paul@6 | 83 | void getfs(ipc_message_t *msg) |
paul@6 | 84 | { |
paul@6 | 85 | _open(Fs_op_getfs, msg); |
paul@6 | 86 | } |
paul@6 | 87 | |
paul@6 | 88 | void open(ipc_message_t *msg) |
paul@6 | 89 | { |
paul@6 | 90 | _open(Fs_op_open, msg); |
paul@6 | 91 | } |
paul@6 | 92 | |
paul@6 | 93 | void _open(int op, ipc_message_t *msg) |
paul@0 | 94 | { |
paul@11 | 95 | file_descriptor_t desc; |
paul@1 | 96 | int flags; |
paul@0 | 97 | long err; |
paul@0 | 98 | |
paul@1 | 99 | /* Obtain the flags. */ |
paul@1 | 100 | |
paul@1 | 101 | flags = ipc_message_get_word(msg, 0); |
paul@1 | 102 | |
paul@5 | 103 | /* Obtain the dataspace and IRQ object. */ |
paul@0 | 104 | |
paul@11 | 105 | err = fsdesc_import_dataspace(msg, 0, &desc.obj) || fsdesc_import_irq(msg, 1, &desc.obj); |
paul@0 | 106 | if (err) |
paul@0 | 107 | { |
paul@10 | 108 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 109 | return; |
paul@0 | 110 | } |
paul@0 | 111 | |
paul@0 | 112 | /* Match the path to a mountpoint. */ |
paul@0 | 113 | |
paul@11 | 114 | int selected = find_mountpoint(desc.obj.buffer); |
paul@0 | 115 | |
paul@0 | 116 | if (selected < 0) |
paul@0 | 117 | { |
paul@10 | 118 | ipc_message_send_error(msg, -L4_ENOENT); |
paul@0 | 119 | return; |
paul@0 | 120 | } |
paul@0 | 121 | |
paul@0 | 122 | /* Rewrite the path for the selected filesystem. */ |
paul@0 | 123 | |
paul@11 | 124 | rewrite_path(desc.obj.buffer, _mountpoints[selected]); |
paul@0 | 125 | |
paul@0 | 126 | /* Find the path within the selected filesystem. */ |
paul@0 | 127 | |
paul@0 | 128 | /* Propagate the message to the identified filesystem. |
paul@0 | 129 | NOTE: The L4_MSGTAG_PROPAGATE flag is not supported by Fiasco.OC, so this |
paul@0 | 130 | NOTE: object has to act as intermediary. */ |
paul@0 | 131 | |
paul@11 | 132 | desc.server = _servers[selected]; |
paul@11 | 133 | |
paul@11 | 134 | err = fs_ipc_open(&desc, flags, op); |
paul@0 | 135 | if (err) |
paul@0 | 136 | { |
paul@10 | 137 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 138 | return; |
paul@0 | 139 | } |
paul@0 | 140 | |
paul@0 | 141 | /* Free the capability and buffer from this task. */ |
paul@0 | 142 | |
paul@11 | 143 | ipc_message_discard_dataspace(msg, desc.obj.ds, (l4_addr_t) desc.obj.buffer); |
paul@0 | 144 | |
paul@0 | 145 | /* Return the file size. */ |
paul@0 | 146 | |
paul@11 | 147 | ipc_message_add_word(msg, desc.obj.size); |
paul@0 | 148 | |
paul@0 | 149 | /* Export and eventually free the server reference from this task. */ |
paul@0 | 150 | |
paul@11 | 151 | ipc_message_propagate_item(msg, desc.ref); |
paul@0 | 152 | } |
paul@0 | 153 | |
paul@0 | 154 | void mount(ipc_message_t *msg) |
paul@0 | 155 | { |
paul@0 | 156 | l4_cap_idx_t server; |
paul@0 | 157 | l4re_ds_t ds; |
paul@0 | 158 | char *buffer; |
paul@0 | 159 | long err; |
paul@0 | 160 | |
paul@0 | 161 | /* Obtain the filesystem capability. */ |
paul@0 | 162 | |
paul@7 | 163 | err = ipc_message_import_capability(msg, 0, &server); |
paul@0 | 164 | if (err) |
paul@0 | 165 | { |
paul@10 | 166 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 167 | return; |
paul@0 | 168 | } |
paul@0 | 169 | |
paul@0 | 170 | /* Obtain the dataspace. */ |
paul@0 | 171 | |
paul@7 | 172 | err = ipc_message_import_dataspace(msg, 1, &ds, (l4_addr_t *) &buffer); |
paul@0 | 173 | if (err) |
paul@0 | 174 | { |
paul@10 | 175 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 176 | return; |
paul@0 | 177 | } |
paul@0 | 178 | |
paul@0 | 179 | /* Obtain the object's path. */ |
paul@0 | 180 | |
paul@0 | 181 | char *path = buffer; |
paul@0 | 182 | |
paul@0 | 183 | /* Mount the supplied capability. */ |
paul@0 | 184 | |
paul@0 | 185 | mount_at_path(server, path); |
paul@0 | 186 | |
paul@0 | 187 | /* Free the capability and buffer from this task. */ |
paul@0 | 188 | |
paul@9 | 189 | ipc_message_discard_dataspace(msg, ds, (l4_addr_t) buffer); |
paul@0 | 190 | } |
paul@0 | 191 | |
paul@0 | 192 | /* Non-exported methods. */ |
paul@0 | 193 | |
paul@0 | 194 | void mount_at_path(l4_cap_idx_t server, const char *path) |
paul@0 | 195 | { |
paul@0 | 196 | /* NOTE: Should return error, terminate with path separator if absent. */ |
paul@0 | 197 | |
paul@0 | 198 | if (_mounted >= 10) |
paul@0 | 199 | return; |
paul@0 | 200 | |
paul@0 | 201 | _servers[_mounted] = server; |
paul@0 | 202 | _mountpoints[_mounted] = strdup(path); |
paul@0 | 203 | |
paul@0 | 204 | _mounted++; |
paul@0 | 205 | } |
paul@0 | 206 | |
paul@0 | 207 | int find_mountpoint(const char *path) |
paul@0 | 208 | { |
paul@0 | 209 | int i, matching; |
paul@0 | 210 | size_t length, longest; |
paul@0 | 211 | |
paul@0 | 212 | if (!_mounted) |
paul@0 | 213 | return -1; |
paul@0 | 214 | |
paul@0 | 215 | matching = -1; |
paul@0 | 216 | longest = 0; |
paul@0 | 217 | |
paul@0 | 218 | /* Find the largest matching path. */ |
paul@0 | 219 | |
paul@0 | 220 | for (i = 0; i < _mounted; i++) |
paul@0 | 221 | { |
paul@0 | 222 | length = strlen(_mountpoints[i]); |
paul@0 | 223 | |
paul@0 | 224 | /* The empty mountpoint always matches; |
paul@0 | 225 | suffixed mountpoints may match as prefixes; |
paul@0 | 226 | suffixed mountpoints may match unsuffixed paths; |
paul@0 | 227 | unsuffixed mountpoints must match exactly. */ |
paul@0 | 228 | |
paul@0 | 229 | /* NOTE: Unsuffixed mountpoints might support subpaths by insisting on |
paul@0 | 230 | a path separator as the first subsequent character. */ |
paul@0 | 231 | |
paul@0 | 232 | if (!length || |
paul@0 | 233 | ((_mountpoints[i][length - 1] == '/') && !strncmp(_mountpoints[i], path, length)) || |
paul@0 | 234 | ((_mountpoints[i][length - 1] == '/') && (strlen(path) == length - 1) && !strncmp(_mountpoints[i], path, length - 1)) || |
paul@0 | 235 | !strcmp(_mountpoints[i], path)) |
paul@0 | 236 | { |
paul@0 | 237 | if (length >= longest) |
paul@0 | 238 | matching = i; |
paul@0 | 239 | } |
paul@0 | 240 | } |
paul@0 | 241 | |
paul@0 | 242 | return matching; |
paul@0 | 243 | } |
paul@0 | 244 | }; |
paul@0 | 245 | |
paul@0 | 246 | |
paul@0 | 247 | |
paul@0 | 248 | int main(int argc, char *argv[]) |
paul@0 | 249 | { |
paul@0 | 250 | FILE *fp; |
paul@0 | 251 | char buffer[256], *sep; |
paul@12 | 252 | l4_cap_idx_t fscap; |
paul@0 | 253 | |
paul@0 | 254 | if (argc < 2) |
paul@0 | 255 | { |
paul@0 | 256 | printf("Need a mount table.\n"); |
paul@0 | 257 | return 1; |
paul@0 | 258 | } |
paul@0 | 259 | |
paul@0 | 260 | /* Initialise and register a new server object. */ |
paul@0 | 261 | |
paul@0 | 262 | Vfs_server server_obj; |
paul@0 | 263 | |
paul@12 | 264 | if (server_bind("export", &server_obj)) |
paul@0 | 265 | { |
paul@0 | 266 | printf("Could not bind thread.\n"); |
paul@0 | 267 | return 1; |
paul@0 | 268 | } |
paul@0 | 269 | |
paul@0 | 270 | /* Mount filesystems. */ |
paul@0 | 271 | |
paul@0 | 272 | fp = fopen(argv[1], "r"); |
paul@0 | 273 | if (fp == NULL) |
paul@0 | 274 | { |
paul@0 | 275 | printf("Could not open %s.\n", argv[1]); |
paul@0 | 276 | return 1; |
paul@0 | 277 | } |
paul@0 | 278 | |
paul@0 | 279 | /* Obtain capabilities for mountpoints. */ |
paul@0 | 280 | |
paul@0 | 281 | while (fgets(buffer, 256, fp) != NULL) |
paul@0 | 282 | { |
paul@0 | 283 | sep = strchr(buffer, (int) '\t'); |
paul@0 | 284 | if (sep == NULL) |
paul@0 | 285 | continue; |
paul@0 | 286 | |
paul@0 | 287 | /* Terminate newline-suffixed strings. */ |
paul@0 | 288 | |
paul@0 | 289 | if (sep[strlen(sep) - 1] == '\n') |
paul@0 | 290 | sep[strlen(sep) - 1] = '\0'; |
paul@0 | 291 | |
paul@0 | 292 | /* Obtain the named capability. */ |
paul@0 | 293 | |
paul@0 | 294 | fscap = l4re_env_get_cap(sep + 1); |
paul@0 | 295 | |
paul@0 | 296 | if (l4_is_invalid_cap(fscap)) |
paul@0 | 297 | continue; |
paul@0 | 298 | |
paul@0 | 299 | /* Terminate the path and register the mountpoint. */ |
paul@0 | 300 | |
paul@0 | 301 | *sep = '\0'; |
paul@0 | 302 | server_obj.mount_at_path(fscap, buffer); |
paul@0 | 303 | } |
paul@0 | 304 | |
paul@0 | 305 | fclose(fp); |
paul@0 | 306 | |
paul@5 | 307 | /* Enter the IPC server loop. */ |
paul@0 | 308 | |
paul@5 | 309 | server_obj.loop(); |
paul@0 | 310 | return 0; |
paul@0 | 311 | } |