paul@0 | 1 | /* |
paul@0 | 2 | * Access several filesystems. |
paul@0 | 3 | * |
paul@21 | 4 | * Copyright (C) 2018, 2019, 2020 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/re/env.h> |
paul@0 | 23 | #include <l4/sys/err.h> |
paul@0 | 24 | |
paul@0 | 25 | #include <stdlib.h> |
paul@0 | 26 | #include <stdio.h> |
paul@0 | 27 | #include <string.h> |
paul@0 | 28 | #include <unistd.h> |
paul@0 | 29 | |
paul@16 | 30 | #include <map> |
paul@16 | 31 | |
paul@0 | 32 | #include <fsclient/fsdesc.h> |
paul@19 | 33 | #include <fsclient/fsdesc_client.h> |
paul@0 | 34 | #include "fspath.h" |
paul@0 | 35 | |
paul@19 | 36 | /* Exposure of the resource. */ |
paul@19 | 37 | |
paul@22 | 38 | #include <fsserver/resource_server.h> |
paul@19 | 39 | |
paul@19 | 40 | /* Implemented interfaces. */ |
paul@19 | 41 | |
paul@19 | 42 | #include <fsserver/user_filesystem_resource.h> |
paul@19 | 43 | |
paul@0 | 44 | |
paul@0 | 45 | |
paul@16 | 46 | /* Mountpoint-to-server mapping. */ |
paul@16 | 47 | |
paul@16 | 48 | typedef std::map<const char *, l4_cap_idx_t> MountTable; |
paul@16 | 49 | typedef std::map<const char *, l4_cap_idx_t>::iterator MountTableIterator; |
paul@16 | 50 | typedef std::map<const char *, l4_cap_idx_t>::value_type MountTableEntry; |
paul@16 | 51 | |
paul@16 | 52 | |
paul@16 | 53 | |
paul@19 | 54 | /* Virtual filesystem resource. */ |
paul@0 | 55 | |
paul@19 | 56 | class Vfs_resource : public UserFilesystemResource |
paul@0 | 57 | { |
paul@0 | 58 | private: |
paul@16 | 59 | MountTable _servers; |
paul@0 | 60 | |
paul@0 | 61 | public: |
paul@19 | 62 | explicit Vfs_resource() |
paul@19 | 63 | : UserFilesystemResource() |
paul@0 | 64 | { |
paul@0 | 65 | } |
paul@0 | 66 | |
paul@19 | 67 | |
paul@19 | 68 | |
paul@19 | 69 | /* Obtain an object as a filesystem reference. */ |
paul@19 | 70 | |
paul@19 | 71 | long getfs_object(fs_object_t *fsobj, l4_cap_idx_t *fs) |
paul@0 | 72 | { |
paul@19 | 73 | file_descriptor_t desc; |
paul@19 | 74 | long err; |
paul@19 | 75 | |
paul@19 | 76 | /* Obtain a suitable descriptor. */ |
paul@19 | 77 | |
paul@19 | 78 | err = init_desc(&desc, fsobj); |
paul@19 | 79 | |
paul@19 | 80 | if (err) |
paul@19 | 81 | return err; |
paul@19 | 82 | |
paul@19 | 83 | /* Return a reference to any directly-accessed object. */ |
paul@19 | 84 | |
paul@19 | 85 | char *path = fsdesc_get_name(fsobj); |
paul@0 | 86 | |
paul@19 | 87 | if ((path != NULL) && !strlen(path)) |
paul@19 | 88 | { |
paul@19 | 89 | *fs = desc.server; |
paul@19 | 90 | return L4_EOK; |
paul@19 | 91 | } |
paul@19 | 92 | |
paul@21 | 93 | /* Obtain a context. */ |
paul@21 | 94 | |
paul@21 | 95 | err = fs_context(&desc); |
paul@21 | 96 | |
paul@21 | 97 | if (err) |
paul@21 | 98 | return err; |
paul@21 | 99 | |
paul@19 | 100 | /* Find the path within the selected filesystem. */ |
paul@19 | 101 | |
paul@21 | 102 | strcpy(desc.cobj.buffer, fsobj->buffer); |
paul@21 | 103 | |
paul@19 | 104 | /* Propagate the message to the identified filesystem. |
paul@19 | 105 | The L4_MSGTAG_PROPAGATE flag is not supported by Fiasco.OC, so this |
paul@19 | 106 | object has to act as intermediary. */ |
paul@0 | 107 | |
paul@21 | 108 | err = fs_context_getfs(&desc); |
paul@19 | 109 | |
paul@19 | 110 | if (err) |
paul@21 | 111 | { |
paul@21 | 112 | fsdesc_deallocate_context(&desc); |
paul@19 | 113 | return err; |
paul@21 | 114 | } |
paul@19 | 115 | |
paul@21 | 116 | /* Free the context and its buffer from this task. */ |
paul@0 | 117 | |
paul@21 | 118 | fsdesc_deallocate_context(&desc); |
paul@19 | 119 | |
paul@19 | 120 | /* Export and eventually free the server reference from this task. */ |
paul@19 | 121 | |
paul@19 | 122 | *fs = discard_cap(desc.ref); |
paul@19 | 123 | |
paul@19 | 124 | return L4_EOK; |
paul@0 | 125 | } |
paul@0 | 126 | |
paul@19 | 127 | /* Mount a filesystem at a particular location. */ |
paul@19 | 128 | |
paul@19 | 129 | long mount_object(fs_object_t *fsobj, l4_cap_idx_t fs) |
paul@6 | 130 | { |
paul@19 | 131 | /* Obtain the object's path. */ |
paul@19 | 132 | |
paul@19 | 133 | char *path = fsdesc_get_name(fsobj); |
paul@19 | 134 | |
paul@19 | 135 | if (path == NULL) |
paul@19 | 136 | return -L4_EINVAL; |
paul@19 | 137 | |
paul@19 | 138 | /* Mount the supplied capability. */ |
paul@19 | 139 | |
paul@19 | 140 | mount_at_path(fs, path); |
paul@19 | 141 | |
paul@19 | 142 | return L4_EOK; |
paul@6 | 143 | } |
paul@6 | 144 | |
paul@19 | 145 | /* Open a filesystem object. */ |
paul@6 | 146 | |
paul@19 | 147 | long open_object(fs_object_t *fsobj, int flags, size_t *size, l4_cap_idx_t *file) |
paul@0 | 148 | { |
paul@11 | 149 | file_descriptor_t desc; |
paul@0 | 150 | long err; |
paul@0 | 151 | |
paul@19 | 152 | /* Obtain a suitable descriptor. */ |
paul@19 | 153 | |
paul@19 | 154 | err = init_desc(&desc, fsobj); |
paul@19 | 155 | |
paul@19 | 156 | if (err) |
paul@19 | 157 | return err; |
paul@19 | 158 | |
paul@21 | 159 | /* Obtain a context. */ |
paul@21 | 160 | |
paul@21 | 161 | err = fs_context(&desc); |
paul@21 | 162 | |
paul@21 | 163 | if (err) |
paul@21 | 164 | return err; |
paul@21 | 165 | |
paul@19 | 166 | /* Find the path within the selected filesystem. */ |
paul@1 | 167 | |
paul@21 | 168 | strcpy(desc.cobj.buffer, fsobj->buffer); |
paul@21 | 169 | |
paul@19 | 170 | /* Propagate the message to the identified filesystem. |
paul@19 | 171 | The L4_MSGTAG_PROPAGATE flag is not supported by Fiasco.OC, so this |
paul@19 | 172 | object has to act as intermediary. */ |
paul@1 | 173 | |
paul@21 | 174 | err = fs_context_open(&desc, flags); |
paul@19 | 175 | |
paul@19 | 176 | if (err) |
paul@21 | 177 | { |
paul@21 | 178 | fsdesc_deallocate_context(&desc); |
paul@19 | 179 | return err; |
paul@21 | 180 | } |
paul@19 | 181 | |
paul@21 | 182 | /* Free the context and its buffer from this task. */ |
paul@0 | 183 | |
paul@21 | 184 | fsdesc_deallocate_context(&desc); |
paul@19 | 185 | |
paul@19 | 186 | /* Export and eventually free the server reference from this task. */ |
paul@19 | 187 | |
paul@19 | 188 | *size = desc.obj.size; |
paul@19 | 189 | *file = discard_cap(desc.ref); |
paul@19 | 190 | |
paul@19 | 191 | return L4_EOK; |
paul@19 | 192 | } |
paul@19 | 193 | |
paul@19 | 194 | |
paul@19 | 195 | |
paul@19 | 196 | /* Non-exported methods. */ |
paul@19 | 197 | |
paul@19 | 198 | /* Initialise a descriptor for accessing a mounted filesystem. */ |
paul@19 | 199 | |
paul@19 | 200 | long init_desc(file_descriptor_t *desc, fs_object_t *obj) |
paul@19 | 201 | { |
paul@0 | 202 | /* Match the path to a mountpoint. */ |
paul@0 | 203 | |
paul@21 | 204 | char *path = fsdesc_get_name(obj); |
paul@18 | 205 | |
paul@18 | 206 | if (path == NULL) |
paul@19 | 207 | return -L4_EINVAL; |
paul@18 | 208 | |
paul@18 | 209 | MountTableIterator entry = find_mountpoint(path); |
paul@0 | 210 | |
paul@16 | 211 | if (entry == _servers.end()) |
paul@19 | 212 | return -L4_ENOENT; |
paul@0 | 213 | |
paul@0 | 214 | /* Rewrite the path for the selected filesystem. */ |
paul@0 | 215 | |
paul@18 | 216 | rewrite_path(path, entry->first); |
paul@0 | 217 | |
paul@19 | 218 | /* Use the filesystem as the target of any query. */ |
paul@0 | 219 | |
paul@19 | 220 | desc->server = entry->second; |
paul@0 | 221 | |
paul@19 | 222 | return L4_EOK; |
paul@0 | 223 | } |
paul@0 | 224 | |
paul@0 | 225 | void mount_at_path(l4_cap_idx_t server, const char *path) |
paul@0 | 226 | { |
paul@16 | 227 | /* NOTE: Should terminate with path separator if absent. |
paul@16 | 228 | NOTE: Should also ensure zero termination. */ |
paul@0 | 229 | |
paul@16 | 230 | _servers.insert(MountTableEntry(strdup(path), server)); |
paul@0 | 231 | } |
paul@0 | 232 | |
paul@16 | 233 | MountTableIterator find_mountpoint(const char *path) |
paul@0 | 234 | { |
paul@16 | 235 | /* Search only until "later" paths are encountered. Such paths will either |
paul@16 | 236 | incorporate components that are themselves greater than those in the path |
paul@16 | 237 | or will be longer than the path. */ |
paul@0 | 238 | |
paul@16 | 239 | MountTableIterator it, found = _servers.end(), |
paul@16 | 240 | limit = _servers.upper_bound(path); |
paul@20 | 241 | ssize_t length, longest = 0; |
paul@0 | 242 | |
paul@0 | 243 | /* Find the largest matching path. */ |
paul@0 | 244 | |
paul@16 | 245 | for (it = _servers.begin(); it != limit; it++) |
paul@0 | 246 | { |
paul@16 | 247 | length = match_path(path, it->first); |
paul@0 | 248 | |
paul@16 | 249 | if (length >= longest) |
paul@16 | 250 | found = it; |
paul@0 | 251 | } |
paul@0 | 252 | |
paul@16 | 253 | return found; |
paul@16 | 254 | } |
paul@16 | 255 | |
paul@16 | 256 | ssize_t match_path(const char *path, const char *mountpoint) |
paul@16 | 257 | { |
paul@16 | 258 | size_t length = strlen(mountpoint); |
paul@16 | 259 | int suffixed = (mountpoint[length - 1] == '/'); |
paul@16 | 260 | |
paul@16 | 261 | /* The empty mountpoint always matches; |
paul@16 | 262 | suffixed mountpoints may match as prefixes; |
paul@16 | 263 | suffixed mountpoints may match unsuffixed paths; |
paul@16 | 264 | unsuffixed mountpoints must match exactly. */ |
paul@16 | 265 | |
paul@16 | 266 | /* NOTE: Unsuffixed mountpoints might support subpaths by insisting on |
paul@16 | 267 | a path separator as the first subsequent character. */ |
paul@16 | 268 | |
paul@16 | 269 | if (!length || |
paul@16 | 270 | (suffixed && !strncmp(mountpoint, path, length)) || |
paul@16 | 271 | (suffixed && (strlen(path) == length - 1) && !strncmp(mountpoint, path, length - 1)) || |
paul@16 | 272 | !strcmp(mountpoint, path)) |
paul@16 | 273 | return length; |
paul@16 | 274 | |
paul@16 | 275 | return -1; |
paul@0 | 276 | } |
paul@0 | 277 | }; |
paul@0 | 278 | |
paul@0 | 279 | |
paul@0 | 280 | |
paul@0 | 281 | int main(int argc, char *argv[]) |
paul@0 | 282 | { |
paul@0 | 283 | FILE *fp; |
paul@0 | 284 | char buffer[256], *sep; |
paul@12 | 285 | l4_cap_idx_t fscap; |
paul@0 | 286 | |
paul@0 | 287 | if (argc < 2) |
paul@0 | 288 | { |
paul@0 | 289 | printf("Need a mount table.\n"); |
paul@0 | 290 | return 1; |
paul@0 | 291 | } |
paul@0 | 292 | |
paul@0 | 293 | /* Initialise and register a new server object. */ |
paul@0 | 294 | |
paul@19 | 295 | Vfs_resource resource; |
paul@22 | 296 | ResourceServer server(&resource); |
paul@0 | 297 | |
paul@19 | 298 | if (server.bind("export")) |
paul@0 | 299 | { |
paul@0 | 300 | printf("Could not bind thread.\n"); |
paul@0 | 301 | return 1; |
paul@0 | 302 | } |
paul@0 | 303 | |
paul@0 | 304 | /* Mount filesystems. */ |
paul@0 | 305 | |
paul@0 | 306 | fp = fopen(argv[1], "r"); |
paul@0 | 307 | if (fp == NULL) |
paul@0 | 308 | { |
paul@0 | 309 | printf("Could not open %s.\n", argv[1]); |
paul@0 | 310 | return 1; |
paul@0 | 311 | } |
paul@0 | 312 | |
paul@0 | 313 | /* Obtain capabilities for mountpoints. */ |
paul@0 | 314 | |
paul@0 | 315 | while (fgets(buffer, 256, fp) != NULL) |
paul@0 | 316 | { |
paul@0 | 317 | sep = strchr(buffer, (int) '\t'); |
paul@0 | 318 | if (sep == NULL) |
paul@0 | 319 | continue; |
paul@0 | 320 | |
paul@0 | 321 | /* Terminate newline-suffixed strings. */ |
paul@0 | 322 | |
paul@0 | 323 | if (sep[strlen(sep) - 1] == '\n') |
paul@0 | 324 | sep[strlen(sep) - 1] = '\0'; |
paul@0 | 325 | |
paul@0 | 326 | /* Obtain the named capability. */ |
paul@0 | 327 | |
paul@0 | 328 | fscap = l4re_env_get_cap(sep + 1); |
paul@0 | 329 | |
paul@0 | 330 | if (l4_is_invalid_cap(fscap)) |
paul@0 | 331 | continue; |
paul@0 | 332 | |
paul@0 | 333 | /* Terminate the path and register the mountpoint. */ |
paul@0 | 334 | |
paul@0 | 335 | *sep = '\0'; |
paul@19 | 336 | resource.mount_at_path(fscap, buffer); |
paul@0 | 337 | } |
paul@0 | 338 | |
paul@0 | 339 | fclose(fp); |
paul@0 | 340 | |
paul@5 | 341 | /* Enter the IPC server loop. */ |
paul@0 | 342 | |
paul@19 | 343 | printf("Starting vfs resource...\n"); |
paul@19 | 344 | server.start(); |
paul@0 | 345 | return 0; |
paul@0 | 346 | } |