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