1 /* 2 * File access convenience functions. 3 * 4 * Copyright (C) 2021 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 <ipc/cap_alloc.h> 23 #include <ipc/mem_ipc.h> 24 25 #include <string.h> 26 27 #include "dataspace_client.h" 28 #include "file_client.h" 29 #include "flush_client.h" 30 #include "opener_client.h" 31 #include "opener_context_client.h" 32 #include "pipe_client.h" 33 #include "pipe_opener_client.h" 34 #include "mapped_file_client.h" 35 36 #include "file.h" 37 #include "notifier.h" 38 39 40 41 /* Update the extent of the file in a region using the region start and end 42 positions and the file size. */ 43 44 static void _update_extent(file_t *file) 45 { 46 /* Handle files ending after or within the region. */ 47 48 if (file->size > file->start_pos) 49 { 50 if (file->size > file->end_pos) 51 file->data_end = file->end_pos - file->start_pos; 52 else 53 file->data_end = file->size - file->start_pos; 54 } 55 56 /* Handle files ending before the region. */ 57 58 else 59 file->data_end = 0; 60 } 61 62 63 64 /* Initialise the given file structure. */ 65 66 void file_init(file_t *file) 67 { 68 file->memory = NULL; 69 file->ref = L4_INVALID_CAP; 70 file->notifier = L4_INVALID_CAP; 71 file->start_pos = 0; 72 file->end_pos = 0; 73 file->data_end = 0; 74 file->data_current = 0; 75 file->can_mmap = 1; 76 file->has_size = 1; 77 file->can_block = 0; 78 file->notifications = 0; 79 } 80 81 82 83 /* Release resources for the given file. */ 84 85 void file_close(file_t *file) 86 { 87 if (l4_is_valid_cap(file->ref)) 88 ipc_cap_free_um(file->ref); 89 90 if (l4_is_valid_cap(file->notifier)) 91 ipc_cap_free_um(file->notifier); 92 93 if (file->memory != NULL) 94 ipc_detach_dataspace(file->memory); 95 96 file_init(file); 97 } 98 99 /* Open a file using the given structure, indicating the filename and 100 filesystem server. The file_mmap function should be used to obtain access to 101 memory providing file data. This is a convenience function invoking 102 file_context and file_context_open. */ 103 104 long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) 105 { 106 file_t context; 107 long err; 108 109 err = file_context(&context, server); 110 if (err) 111 return err; 112 113 if (!file_string_set(&context, filename, 0, NULL)) 114 return -L4_ENOMEM; 115 116 err = file_context_open(file, flags, &context); 117 118 /* Close the context, although a separate mechanism could permit contexts to 119 open several files. */ 120 121 file_close(&context); 122 return err; 123 } 124 125 126 127 /* Initialise a file structure for a context obtained from the given server 128 attaching memory to communicate filename information. */ 129 130 long file_context(file_t *file, l4_cap_idx_t server) 131 { 132 if (l4_is_invalid_cap(server)) 133 return -L4_EINVAL; 134 135 client_Opener opener(server); 136 offset_t size; 137 flags_t flags; 138 long err; 139 140 file_init(file); 141 142 err = opener.context(&file->ref); 143 if (err) 144 return err; 145 146 client_Dataspace context_ds(file->ref); 147 148 err = context_ds.info(&size, &flags); 149 if (err) 150 return err; 151 152 file->start_pos = 0; 153 file->end_pos = size; 154 155 return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); 156 } 157 158 /* Open a file using the given structure and context. */ 159 160 long file_context_open(file_t *file, flags_t flags, file_t *context) 161 { 162 client_OpenerContext openercontext(context->ref); 163 file_init(file); 164 return openercontext.open(flags, &file->size, &file->ref); 165 } 166 167 168 169 /* Flush populated data and obtain an updated file size and populated data 170 details. */ 171 172 long file_flush(file_t *file) 173 { 174 client_Flush _file(file->ref); 175 long err = _file.flush(file->data_current, &file->size); 176 177 if (err) 178 return err; 179 180 _update_extent(file); 181 182 return L4_EOK; 183 } 184 185 /* Map a region of the given file to a memory region, obtaining an updated file 186 size and populated data details. Unmap any previously mapped region. */ 187 188 long file_mmap(file_t *file, offset_t position, offset_t length) 189 { 190 char *memory = file->memory; 191 client_MappedFile mapped_file(file->ref); 192 long err = mapped_file.mmap(position, length, &file->start_pos, 193 &file->end_pos, &file->size); 194 195 if (err) 196 return err; 197 198 _update_extent(file); 199 200 err = ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); 201 if (err) 202 return err; 203 204 if (memory != NULL) 205 ipc_detach_dataspace(memory); 206 207 return L4_EOK; 208 } 209 210 /* Resize a file, obtaining updated file size and populated data details. */ 211 212 long file_resize(file_t *file, offset_t size) 213 { 214 if (!file->has_size) 215 return -L4_EIO; 216 217 client_File _file(file->ref); 218 offset_t file_size = size; 219 long err = _file.resize(&file_size); 220 221 if (err) 222 return err; 223 224 file->size = file_size; 225 _update_extent(file); 226 return L4_EOK; 227 } 228 229 230 231 /* Return the amount of data in the mapped region for the given file. */ 232 233 offset_t file_populated_span(file_t *file) 234 { 235 offset_t size = file_span(file); 236 return (file->data_end < size) ? file->data_end : size; 237 } 238 239 /* Return the size of the mapped region for the given file. */ 240 241 offset_t file_span(file_t *file) 242 { 243 return file->end_pos - file->start_pos; 244 } 245 246 247 248 /* Get a pointer to any terminated string at the given offset or NULL if the 249 data from offset is not terminated. */ 250 251 char *file_string_get(file_t *file, offset_t offset) 252 { 253 offset_t limit = file_span(file) - offset; 254 255 if (strnlen(file->memory + offset, limit) < limit) 256 return file->memory + offset; 257 else 258 return NULL; 259 } 260 261 /* Copy a string to the mapped region at the given offset, returning 1 (true) 262 where all characters were copied, 0 (false) otherwise. The precise number of 263 characters copied, excluding the zero terminator is provided via the written 264 parameter if it is not specified as NULL. */ 265 266 int file_string_set(file_t *file, const char *data, offset_t offset, 267 offset_t *written) 268 { 269 offset_t i, pos, limit = file_span(file); 270 271 /* Do not attempt to copy data with an invalid offset. */ 272 273 if (offset >= limit) 274 { 275 if (written != NULL) 276 *written = 0; 277 return 0; 278 } 279 280 /* Copy the data to the given offset, stopping at the end of the region. */ 281 282 for (i = 0, pos = offset; pos < limit; i++, pos++) 283 { 284 file->memory[pos] = data[i]; 285 286 /* Terminator written, can return immediately. */ 287 288 if (!data[i]) 289 { 290 if (written != NULL) 291 *written = pos - offset; 292 return 1; 293 } 294 } 295 296 /* Terminate the incomplete string at the end of the region. */ 297 298 file->memory[limit - 1] = '\0'; 299 if (written != NULL) 300 *written = limit - 1 - offset; 301 return 0; 302 } 303 304 305 306 /* Return the number of remaining populated bytes in the region. */ 307 308 offset_t file_data_available(file_t *file) 309 { 310 return file_populated_span(file) - file->data_current; 311 } 312 313 /* Return the current data offset in the region. */ 314 315 char *file_data_current(file_t *file) 316 { 317 return file->memory + file->data_current; 318 } 319 320 /* Return the current access position in the file. */ 321 322 offset_t file_data_current_position(file_t *file) 323 { 324 return file->start_pos + file->data_current; 325 } 326 327 /* Return the position of the end of the populated bytes in the region. */ 328 329 offset_t file_data_end_position(file_t *file) 330 { 331 return file->start_pos + file->data_end; 332 } 333 334 /* Return the amount of remaining space in the region. */ 335 336 offset_t file_data_space(file_t *file) 337 { 338 return file_span(file) - file->data_current; 339 } 340 341 342 343 /* Copy data to the given buffer from the current data position, updating the 344 position. */ 345 346 void file_data_read(file_t *file, char *buf, offset_t to_transfer) 347 { 348 memcpy(buf, file_data_current(file), to_transfer); 349 350 /* Update position details. */ 351 352 file->data_current += to_transfer; 353 } 354 355 /* Copy data from the given buffer to the current data position, updating the 356 position and the extent of populated data if this was exceeded. */ 357 358 void file_data_write(file_t *file, char *buf, offset_t to_transfer) 359 { 360 memcpy(file_data_current(file), buf, to_transfer); 361 362 /* Update position details. */ 363 364 file->data_current += to_transfer; 365 366 if (file->data_current > file->data_end) 367 file->data_end = file->data_current; 368 } 369 370 371 372 /* Subscribe to notification events on a file. */ 373 374 long file_notify_subscribe(file_t *file, notify_flags_t flags) 375 { 376 FileNotifier *notifier = get_notifier(); 377 378 return notifier->subscribe(file, flags); 379 } 380 381 /* Unsubscribe from notification events on a file. */ 382 383 long file_notify_unsubscribe(file_t *file) 384 { 385 if (l4_is_invalid_cap(file->notifier)) 386 return -L4_EINVAL; 387 388 FileNotifier *notifier = get_notifier(); 389 390 return notifier->unsubscribe(file); 391 } 392 393 /* Wait for a notification event on a file. */ 394 395 long file_notify_wait_file(file_t *file) 396 { 397 FileNotifier *notifier = get_notifier(); 398 399 return notifier->wait_file(file); 400 } 401 402 /* Wait for notification events on files. */ 403 404 long file_notify_wait_files(file_t **file) 405 { 406 FileNotifier *notifier = get_notifier(); 407 408 return notifier->wait(file); 409 } 410 411 412 413 /* Open two pipe endpoints using the given pipe server. */ 414 415 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) 416 { 417 if (l4_is_invalid_cap(server)) 418 return -L4_EINVAL; 419 420 client_PipeOpener opener(server); 421 422 file_init(reader); 423 file_init(writer); 424 425 /* Pipes can usually only be accessed via region navigation. */ 426 427 reader->can_mmap = 0; 428 reader->has_size = 0; 429 writer->can_mmap = 0; 430 writer->has_size = 0; 431 432 long err = opener.pipe(size, &reader->ref, &writer->ref); 433 if (err) 434 return err; 435 436 err = pipe_next(writer) || pipe_next(reader); 437 438 if (err) 439 { 440 file_close(reader); 441 file_close(writer); 442 } 443 444 return err; 445 } 446 447 /* Access the current region for a pipe endpoint. */ 448 449 long pipe_current(file_t *pipe) 450 { 451 client_Pipe _pipe(pipe->ref); 452 long err = _pipe.current_region(&pipe->data_end, &pipe->size); 453 454 if (err) 455 return err; 456 457 pipe->end_pos = pipe->size; 458 459 /* Attach memory if necessary. */ 460 461 if (pipe->memory == NULL) 462 { 463 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 464 if (err) 465 return err; 466 } 467 468 return L4_EOK; 469 } 470 471 /* Access the next region for a pipe endpoint, updating the eventual size of 472 the current region. */ 473 474 long pipe_next(file_t *pipe) 475 { 476 client_Pipe _pipe(pipe->ref); 477 long err = _pipe.next_region(&pipe->data_end, &pipe->size); 478 char *memory = pipe->memory; 479 480 if (err) 481 return err; 482 483 pipe->end_pos = pipe->size; 484 485 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 486 if (err) 487 return err; 488 489 if (memory != NULL) 490 ipc_detach_dataspace(memory); 491 492 return L4_EOK; 493 } 494 495 /* Set the size of the written region. */ 496 497 long pipe_written(file_t *pipe, offset_t size) 498 { 499 if (size <= pipe->size) 500 { 501 pipe->data_end = size; 502 return L4_EOK; 503 } 504 else 505 return -L4_EINVAL; 506 } 507 508 // vim: tabstop=2 expandtab shiftwidth=2