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