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