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/sys/err.h> |
paul@0 | 27 | #include <l4/sys/types.h> |
paul@0 | 28 | |
paul@0 | 29 | #include <stdlib.h> |
paul@0 | 30 | #include <stdio.h> |
paul@0 | 31 | #include <string.h> |
paul@0 | 32 | #include <unistd.h> |
paul@0 | 33 | |
paul@16 | 34 | #include <map> |
paul@16 | 35 | |
paul@0 | 36 | #include <fsclient/fsdesc.h> |
paul@0 | 37 | #include <fsclient/fs_ipc.h> |
paul@14 | 38 | #include <fsclient/ops.h> |
paul@0 | 39 | #include <fsserver/server.h> |
paul@0 | 40 | #include "fspath.h" |
paul@0 | 41 | |
paul@0 | 42 | |
paul@0 | 43 | |
paul@16 | 44 | /* Mountpoint-to-server mapping. */ |
paul@16 | 45 | |
paul@16 | 46 | typedef std::map<const char *, l4_cap_idx_t> MountTable; |
paul@16 | 47 | typedef std::map<const char *, l4_cap_idx_t>::iterator MountTableIterator; |
paul@16 | 48 | typedef std::map<const char *, l4_cap_idx_t>::value_type MountTableEntry; |
paul@16 | 49 | |
paul@16 | 50 | |
paul@16 | 51 | |
paul@0 | 52 | /* Virtual filesystem server. */ |
paul@0 | 53 | |
paul@5 | 54 | class Vfs_server : public OpeningServer |
paul@0 | 55 | { |
paul@0 | 56 | private: |
paul@16 | 57 | MountTable _servers; |
paul@0 | 58 | |
paul@0 | 59 | public: |
paul@0 | 60 | explicit Vfs_server() |
paul@5 | 61 | : OpeningServer() |
paul@0 | 62 | { |
paul@0 | 63 | } |
paul@0 | 64 | |
paul@0 | 65 | void dispatch(ipc_message_t *msg) |
paul@0 | 66 | { |
paul@0 | 67 | switch (l4_msgtag_label(msg->tag)) |
paul@0 | 68 | { |
paul@0 | 69 | case Fs_op_mount: |
paul@0 | 70 | mount(msg); |
paul@0 | 71 | break; |
paul@0 | 72 | |
paul@0 | 73 | case Fs_op_open: |
paul@6 | 74 | open(msg); |
paul@0 | 75 | break; |
paul@0 | 76 | |
paul@0 | 77 | case Fs_op_getfs: |
paul@6 | 78 | getfs(msg); |
paul@0 | 79 | break; |
paul@0 | 80 | |
paul@0 | 81 | default: |
paul@10 | 82 | ipc_message_send_error(msg, -L4_EBADPROTO); |
paul@0 | 83 | break; |
paul@0 | 84 | } |
paul@0 | 85 | } |
paul@0 | 86 | |
paul@6 | 87 | void getfs(ipc_message_t *msg) |
paul@6 | 88 | { |
paul@6 | 89 | _open(Fs_op_getfs, msg); |
paul@6 | 90 | } |
paul@6 | 91 | |
paul@6 | 92 | void open(ipc_message_t *msg) |
paul@6 | 93 | { |
paul@6 | 94 | _open(Fs_op_open, msg); |
paul@6 | 95 | } |
paul@6 | 96 | |
paul@6 | 97 | void _open(int op, ipc_message_t *msg) |
paul@0 | 98 | { |
paul@11 | 99 | file_descriptor_t desc; |
paul@1 | 100 | int flags; |
paul@0 | 101 | long err; |
paul@0 | 102 | |
paul@1 | 103 | /* Obtain the flags. */ |
paul@1 | 104 | |
paul@1 | 105 | flags = ipc_message_get_word(msg, 0); |
paul@1 | 106 | |
paul@5 | 107 | /* Obtain the dataspace and IRQ object. */ |
paul@0 | 108 | |
paul@15 | 109 | err = fsdesc_import_object(msg, 0, &desc.obj); |
paul@0 | 110 | if (err) |
paul@0 | 111 | { |
paul@10 | 112 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 113 | return; |
paul@0 | 114 | } |
paul@0 | 115 | |
paul@0 | 116 | /* Match the path to a mountpoint. */ |
paul@0 | 117 | |
paul@16 | 118 | MountTableIterator entry = find_mountpoint(desc.obj.buffer); |
paul@0 | 119 | |
paul@16 | 120 | if (entry == _servers.end()) |
paul@0 | 121 | { |
paul@10 | 122 | ipc_message_send_error(msg, -L4_ENOENT); |
paul@0 | 123 | return; |
paul@0 | 124 | } |
paul@0 | 125 | |
paul@0 | 126 | /* Rewrite the path for the selected filesystem. */ |
paul@0 | 127 | |
paul@16 | 128 | rewrite_path(desc.obj.buffer, entry->first); |
paul@0 | 129 | |
paul@0 | 130 | /* Find the path within the selected filesystem. */ |
paul@0 | 131 | |
paul@0 | 132 | /* Propagate the message to the identified filesystem. |
paul@0 | 133 | NOTE: The L4_MSGTAG_PROPAGATE flag is not supported by Fiasco.OC, so this |
paul@0 | 134 | NOTE: object has to act as intermediary. */ |
paul@0 | 135 | |
paul@16 | 136 | desc.server = entry->second; |
paul@11 | 137 | |
paul@11 | 138 | err = fs_ipc_open(&desc, flags, op); |
paul@0 | 139 | if (err) |
paul@0 | 140 | { |
paul@10 | 141 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 142 | return; |
paul@0 | 143 | } |
paul@0 | 144 | |
paul@15 | 145 | /* Free the capabilities and buffer from this task. */ |
paul@0 | 146 | |
paul@15 | 147 | fsdesc_deallocate_object(&desc.obj); |
paul@0 | 148 | |
paul@0 | 149 | /* Return the file size. */ |
paul@0 | 150 | |
paul@11 | 151 | ipc_message_add_word(msg, desc.obj.size); |
paul@0 | 152 | |
paul@0 | 153 | /* Export and eventually free the server reference from this task. */ |
paul@0 | 154 | |
paul@11 | 155 | ipc_message_propagate_item(msg, desc.ref); |
paul@0 | 156 | } |
paul@0 | 157 | |
paul@0 | 158 | void mount(ipc_message_t *msg) |
paul@0 | 159 | { |
paul@0 | 160 | l4_cap_idx_t server; |
paul@0 | 161 | l4re_ds_t ds; |
paul@0 | 162 | char *buffer; |
paul@0 | 163 | long err; |
paul@0 | 164 | |
paul@0 | 165 | /* Obtain the filesystem capability. */ |
paul@0 | 166 | |
paul@7 | 167 | err = ipc_message_import_capability(msg, 0, &server); |
paul@0 | 168 | if (err) |
paul@0 | 169 | { |
paul@10 | 170 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 171 | return; |
paul@0 | 172 | } |
paul@0 | 173 | |
paul@0 | 174 | /* Obtain the dataspace. */ |
paul@0 | 175 | |
paul@7 | 176 | err = ipc_message_import_dataspace(msg, 1, &ds, (l4_addr_t *) &buffer); |
paul@0 | 177 | if (err) |
paul@0 | 178 | { |
paul@10 | 179 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 180 | return; |
paul@0 | 181 | } |
paul@0 | 182 | |
paul@0 | 183 | /* Obtain the object's path. */ |
paul@0 | 184 | |
paul@0 | 185 | char *path = buffer; |
paul@0 | 186 | |
paul@0 | 187 | /* Mount the supplied capability. */ |
paul@0 | 188 | |
paul@0 | 189 | mount_at_path(server, path); |
paul@0 | 190 | |
paul@0 | 191 | /* Free the capability and buffer from this task. */ |
paul@0 | 192 | |
paul@9 | 193 | ipc_message_discard_dataspace(msg, ds, (l4_addr_t) buffer); |
paul@0 | 194 | } |
paul@0 | 195 | |
paul@0 | 196 | /* Non-exported methods. */ |
paul@0 | 197 | |
paul@0 | 198 | void mount_at_path(l4_cap_idx_t server, const char *path) |
paul@0 | 199 | { |
paul@16 | 200 | /* NOTE: Should terminate with path separator if absent. |
paul@16 | 201 | NOTE: Should also ensure zero termination. */ |
paul@0 | 202 | |
paul@16 | 203 | _servers.insert(MountTableEntry(strdup(path), server)); |
paul@0 | 204 | } |
paul@0 | 205 | |
paul@16 | 206 | MountTableIterator find_mountpoint(const char *path) |
paul@0 | 207 | { |
paul@16 | 208 | /* Search only until "later" paths are encountered. Such paths will either |
paul@16 | 209 | incorporate components that are themselves greater than those in the path |
paul@16 | 210 | or will be longer than the path. */ |
paul@0 | 211 | |
paul@16 | 212 | MountTableIterator it, found = _servers.end(), |
paul@16 | 213 | limit = _servers.upper_bound(path); |
paul@16 | 214 | size_t length, longest = 0; |
paul@0 | 215 | |
paul@0 | 216 | /* Find the largest matching path. */ |
paul@0 | 217 | |
paul@16 | 218 | for (it = _servers.begin(); it != limit; it++) |
paul@0 | 219 | { |
paul@16 | 220 | length = match_path(path, it->first); |
paul@0 | 221 | |
paul@16 | 222 | if (length >= longest) |
paul@16 | 223 | found = it; |
paul@0 | 224 | } |
paul@0 | 225 | |
paul@16 | 226 | return found; |
paul@16 | 227 | } |
paul@16 | 228 | |
paul@16 | 229 | ssize_t match_path(const char *path, const char *mountpoint) |
paul@16 | 230 | { |
paul@16 | 231 | size_t length = strlen(mountpoint); |
paul@16 | 232 | int suffixed = (mountpoint[length - 1] == '/'); |
paul@16 | 233 | |
paul@16 | 234 | /* The empty mountpoint always matches; |
paul@16 | 235 | suffixed mountpoints may match as prefixes; |
paul@16 | 236 | suffixed mountpoints may match unsuffixed paths; |
paul@16 | 237 | unsuffixed mountpoints must match exactly. */ |
paul@16 | 238 | |
paul@16 | 239 | /* NOTE: Unsuffixed mountpoints might support subpaths by insisting on |
paul@16 | 240 | a path separator as the first subsequent character. */ |
paul@16 | 241 | |
paul@16 | 242 | if (!length || |
paul@16 | 243 | (suffixed && !strncmp(mountpoint, path, length)) || |
paul@16 | 244 | (suffixed && (strlen(path) == length - 1) && !strncmp(mountpoint, path, length - 1)) || |
paul@16 | 245 | !strcmp(mountpoint, path)) |
paul@16 | 246 | return length; |
paul@16 | 247 | |
paul@16 | 248 | return -1; |
paul@0 | 249 | } |
paul@0 | 250 | }; |
paul@0 | 251 | |
paul@0 | 252 | |
paul@0 | 253 | |
paul@0 | 254 | int main(int argc, char *argv[]) |
paul@0 | 255 | { |
paul@0 | 256 | FILE *fp; |
paul@0 | 257 | char buffer[256], *sep; |
paul@12 | 258 | l4_cap_idx_t fscap; |
paul@0 | 259 | |
paul@0 | 260 | if (argc < 2) |
paul@0 | 261 | { |
paul@0 | 262 | printf("Need a mount table.\n"); |
paul@0 | 263 | return 1; |
paul@0 | 264 | } |
paul@0 | 265 | |
paul@0 | 266 | /* Initialise and register a new server object. */ |
paul@0 | 267 | |
paul@0 | 268 | Vfs_server server_obj; |
paul@0 | 269 | |
paul@13 | 270 | if (server_obj.bind("export")) |
paul@0 | 271 | { |
paul@0 | 272 | printf("Could not bind thread.\n"); |
paul@0 | 273 | return 1; |
paul@0 | 274 | } |
paul@0 | 275 | |
paul@0 | 276 | /* Mount filesystems. */ |
paul@0 | 277 | |
paul@0 | 278 | fp = fopen(argv[1], "r"); |
paul@0 | 279 | if (fp == NULL) |
paul@0 | 280 | { |
paul@0 | 281 | printf("Could not open %s.\n", argv[1]); |
paul@0 | 282 | return 1; |
paul@0 | 283 | } |
paul@0 | 284 | |
paul@0 | 285 | /* Obtain capabilities for mountpoints. */ |
paul@0 | 286 | |
paul@0 | 287 | while (fgets(buffer, 256, fp) != NULL) |
paul@0 | 288 | { |
paul@0 | 289 | sep = strchr(buffer, (int) '\t'); |
paul@0 | 290 | if (sep == NULL) |
paul@0 | 291 | continue; |
paul@0 | 292 | |
paul@0 | 293 | /* Terminate newline-suffixed strings. */ |
paul@0 | 294 | |
paul@0 | 295 | if (sep[strlen(sep) - 1] == '\n') |
paul@0 | 296 | sep[strlen(sep) - 1] = '\0'; |
paul@0 | 297 | |
paul@0 | 298 | /* Obtain the named capability. */ |
paul@0 | 299 | |
paul@0 | 300 | fscap = l4re_env_get_cap(sep + 1); |
paul@0 | 301 | |
paul@0 | 302 | if (l4_is_invalid_cap(fscap)) |
paul@0 | 303 | continue; |
paul@0 | 304 | |
paul@0 | 305 | /* Terminate the path and register the mountpoint. */ |
paul@0 | 306 | |
paul@0 | 307 | *sep = '\0'; |
paul@0 | 308 | server_obj.mount_at_path(fscap, buffer); |
paul@0 | 309 | } |
paul@0 | 310 | |
paul@0 | 311 | fclose(fp); |
paul@0 | 312 | |
paul@5 | 313 | /* Enter the IPC server loop. */ |
paul@0 | 314 | |
paul@5 | 315 | server_obj.loop(); |
paul@0 | 316 | return 0; |
paul@0 | 317 | } |