1 /* 2 * File access convenience functions. 3 * 4 * Copyright (C) 2021, 2022, 2023, 2024 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 #include <notifier/notifier.h> 25 #include <systypes/fcntl.h> 26 #include <systypes/stat.h> 27 28 #include <stdio.h> 29 #include <string.h> 30 31 #include "dataspace_client.h" 32 #include "directory_client.h" 33 #include "file_client.h" 34 #include "filesystem_client.h" 35 #include "flush_client.h" 36 #include "opener_client.h" 37 #include "opener_context_client.h" 38 #include "pipe_client.h" 39 #include "pipe_opener_client.h" 40 #include "mapped_file_client.h" 41 42 #include "file.h" 43 44 45 46 /* Update the extent of the file in a region using the region start and end 47 positions and the file size. */ 48 49 static void _update_extent(file_t *file) 50 { 51 /* Handle files ending after or within the region. */ 52 53 if (file->size > file->start_pos) 54 { 55 if (file->size > file->end_pos) 56 file->data_end = file->end_pos - file->start_pos; 57 else 58 file->data_end = file->size - file->start_pos; 59 } 60 61 /* Handle files ending before the region. */ 62 63 else 64 file->data_end = 0; 65 } 66 67 68 69 /* Obtain a reference to a file opener for the given user. */ 70 71 long file_open_for_user(user_t user, l4_cap_idx_t server, l4_cap_idx_t *opener) 72 { 73 client_Filesystem filesystem(server); 74 75 return filesystem.open_for_user(user, opener); 76 } 77 78 79 80 /* Initialise the given file structure. */ 81 82 void file_init(file_t *file) 83 { 84 file->memory = NULL; 85 file->ref = L4_INVALID_CAP; 86 file->start_pos = 0; 87 file->end_pos = 0; 88 file->data_end = 0; 89 file->data_current = 0; 90 file->object_flags = 0; 91 file->can_block = 0; 92 file->flags = 0; 93 file->error = 0; 94 file->metadata = NULL; 95 96 /* Initialise the notifiable section of the structure. */ 97 98 file->notifiable.notifications = 0; 99 file->notifiable.pending_notifications = 0; 100 file->notifiable.base = (notifiable_base_t *) file; 101 file->notifiable.handler = NULL; 102 } 103 104 105 106 /* Release resources for the given file. Note that this function does not 107 deallocate the structure since it may be statically allocated. */ 108 109 void file_close(file_t *file) 110 { 111 if (l4_is_valid_cap(file->ref)) 112 { 113 notifier_get_task_notifier()->unsubscribe(file_notifiable(file)); 114 ipc_cap_free_um(file->ref); 115 } 116 117 if (file->memory != NULL) 118 ipc_detach_dataspace(file->memory); 119 120 file_init(file); 121 } 122 123 /* Make a directory in the filesystem. This is a convenience function invoking 124 file_context and file_context_mkdir. */ 125 126 long file_mkdir(const char *filename, mode_t mode, l4_cap_idx_t server) 127 { 128 file_t context; 129 long err; 130 131 err = file_context(&context, server); 132 if (err) 133 return err; 134 135 if (!file_string_set(&context, filename, 0, NULL)) 136 return -L4_ENOMEM; 137 138 err = file_context_mkdir(systypes_to_sys_mode(mode), &context); 139 140 /* Close the context, although a separate mechanism could permit contexts to 141 remove several files. */ 142 143 file_close(&context); 144 return err; 145 } 146 147 /* Open a file using the given structure, indicating the filename and 148 filesystem server. The file_mmap function should be used to obtain access to 149 memory providing file data. This is a convenience function invoking 150 file_context and file_context_open. */ 151 152 long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) 153 { 154 file_t context; 155 long err; 156 157 err = file_context(&context, server); 158 if (err) 159 return err; 160 161 if (!file_string_set(&context, filename, 0, NULL)) 162 return -L4_ENOMEM; 163 164 err = file_context_open(file, flags, &context); 165 166 /* Close the context, although a separate mechanism could permit contexts to 167 open several files. */ 168 169 file_close(&context); 170 return err; 171 } 172 173 /* Remove a file from the filesystem. This is a convenience function invoking 174 file_context and file_context_remove. */ 175 176 long file_remove(const char *filename, l4_cap_idx_t server) 177 { 178 file_t context; 179 long err; 180 181 err = file_context(&context, server); 182 if (err) 183 return err; 184 185 if (!file_string_set(&context, filename, 0, NULL)) 186 return -L4_ENOMEM; 187 188 err = file_context_remove(&context); 189 190 /* Close the context, although a separate mechanism could permit contexts to 191 remove several files. */ 192 193 file_close(&context); 194 return err; 195 } 196 197 /* Open a new instance of a file using the given structure. */ 198 199 long file_reopen(file_t *file, file_t *new_file, flags_t flags) 200 { 201 client_File _file(file->ref); 202 file_init(new_file); 203 new_file->flags = flags; 204 return _file.reopen(flags, &new_file->size, &new_file->ref, &new_file->object_flags); 205 } 206 207 /* Rename an object in the filesystem. This is a convenience function invoking 208 file_context and file_context_rename. */ 209 210 long file_rename(const char *source, const char *target, l4_cap_idx_t server) 211 { 212 file_t context; 213 offset_t written; 214 long err; 215 216 err = file_context(&context, server); 217 if (err) 218 return err; 219 220 if (!file_string_set(&context, source, 0, &written)) 221 return -L4_ENOMEM; 222 223 if (!file_string_set(&context, target, written + 1, NULL)) 224 return -L4_ENOMEM; 225 226 err = file_context_rename(&context); 227 228 /* Close the context, although a separate mechanism could permit contexts to 229 rename several files. */ 230 231 file_close(&context); 232 return err; 233 } 234 235 /* Obtain filesystem object statistics. This is a convenience function invoking 236 file_context and file_context_stat. */ 237 238 long file_stat(const char *filename, struct stat *st, l4_cap_idx_t server) 239 { 240 file_t context; 241 offset_t written; 242 long err; 243 244 err = file_context(&context, server); 245 if (err) 246 return err; 247 248 if (!file_string_set(&context, filename, 0, &written)) 249 return -L4_ENOMEM; 250 251 err = file_context_stat(st, &context); 252 253 /* Close the context, although a separate mechanism could permit contexts to 254 rename several files. */ 255 256 file_close(&context); 257 return err; 258 } 259 260 261 262 /* Initialise a file structure for a context obtained from the given server 263 attaching memory to communicate filename information. */ 264 265 long file_context(file_t *file, l4_cap_idx_t server) 266 { 267 if (l4_is_invalid_cap(server)) 268 return -L4_EINVAL; 269 270 client_Opener opener(server); 271 ds_stats_t stats; 272 long err; 273 274 file_init(file); 275 file->flags = O_RDWR; 276 277 err = opener.context(&file->ref); 278 if (err) 279 return err; 280 281 client_Dataspace context_ds(file->ref); 282 283 err = context_ds.info(&stats); 284 if (err) 285 return err; 286 287 file->start_pos = 0; 288 file->end_pos = stats.size; 289 290 return ipc_attach_dataspace(file->ref, stats.size, (void **) &file->memory); 291 } 292 293 /* Make a directory using the given context. */ 294 295 long file_context_mkdir(mode_t mode, file_t *context) 296 { 297 client_OpenerContext openercontext(context->ref); 298 return openercontext.mkdir(mode); 299 } 300 301 /* Open a file using the given structure and context. */ 302 303 long file_context_open(file_t *file, flags_t flags, file_t *context) 304 { 305 client_OpenerContext openercontext(context->ref); 306 file_init(file); 307 file->flags = flags; 308 return openercontext.open(flags, &file->size, &file->ref, &file->object_flags); 309 } 310 311 /* Remove a file using the given context. */ 312 313 long file_context_remove(file_t *context) 314 { 315 client_OpenerContext openercontext(context->ref); 316 return openercontext.remove(); 317 } 318 319 /* Rename a file using the given context. */ 320 321 long file_context_rename(file_t *context) 322 { 323 client_OpenerContext openercontext(context->ref); 324 return openercontext.rename(); 325 } 326 327 /* Obtain filesystem object statistics using the given context. */ 328 329 long file_context_stat(struct stat *st, file_t *context) 330 { 331 client_OpenerContext openercontext(context->ref); 332 long err = openercontext.stat(); 333 334 if (err) 335 return err; 336 337 /* Copy the stat structure manually in case of any divergence. */ 338 339 systypes_copy_from_sys_stat(st, (sys_stat_t *) context->memory); 340 341 return L4_EOK; 342 } 343 344 345 346 /* Flush populated data and obtain an updated file size. */ 347 348 long file_flush(file_t *file) 349 { 350 if (l4_is_invalid_cap(file->ref)) 351 return -L4_EINVAL; 352 353 client_Flush _file(file->ref); 354 long err = _file.flush(file->data_current, &file->size); 355 356 if (err) 357 return err; 358 359 _update_extent(file); 360 361 return L4_EOK; 362 } 363 364 /* Map a region of the given file to a memory region, obtaining an updated file 365 size and populated data details. Unmap any previously mapped region. */ 366 367 long file_mmap(file_t *file, offset_t position, offset_t length, 368 offset_t start_visible, offset_t end_visible, 369 rm_flags_t region_flags) 370 { 371 long err; 372 373 if (file->memory != NULL) 374 { 375 err = ipc_detach_dataspace(file->memory); 376 if (err) 377 return err; 378 379 file->memory = NULL; 380 } 381 382 err = file_mmap_only(file, position, length, start_visible, end_visible); 383 384 if (err) 385 return err; 386 387 err = ipc_attach_dataspace_align(file->ref, file_span(file), 388 L4RE_RM_F_SEARCH_ADDR | region_flags, 389 L4_PAGESHIFT, 390 (void **) &file->memory); 391 if (err) 392 return err; 393 394 return L4_EOK; 395 } 396 397 /* Request access to a region of the given file, obtaining an updated file size 398 and populated data details. The region is not mapped, however. */ 399 400 long file_mmap_only(file_t *file, offset_t position, offset_t length, 401 offset_t start_visible, offset_t end_visible) 402 { 403 client_MappedFile mapped_file(file->ref); 404 long err = mapped_file.mmap(position, length, start_visible, end_visible, 405 &file->start_pos, &file->end_pos, &file->size); 406 407 if (err) 408 return err; 409 410 _update_extent(file); 411 412 return L4_EOK; 413 } 414 415 /* Return opening flags compatible with the given region flags. */ 416 417 flags_t file_opening_flags(rm_flags_t rm_flags) 418 { 419 if ((rm_flags & L4RE_RM_F_RW) == L4RE_RM_F_RW) 420 return O_RDWR; 421 else if (rm_flags & L4RE_RM_F_W) 422 return O_WRONLY; 423 else 424 return O_RDONLY; 425 } 426 427 /* Return mmap flags corresponding to the file access flags. */ 428 429 rm_flags_t file_region_flags(flags_t flags) 430 { 431 rm_flags_t rm_flags; 432 433 switch (flags & 3) 434 { 435 case O_WRONLY: 436 rm_flags = L4RE_RM_F_W; 437 break; 438 439 case O_RDWR: 440 rm_flags = L4RE_RM_F_RW; 441 break; 442 443 default: 444 rm_flags = L4RE_RM_F_R; 445 break; 446 } 447 448 return rm_flags; 449 } 450 451 /* Resize a file, obtaining updated file size details. */ 452 453 long file_resize(file_t *file, offset_t size) 454 { 455 if (!(file->object_flags & OBJECT_HAS_SIZE)) 456 return -L4_EIO; 457 458 client_File _file(file->ref); 459 offset_t file_size = size; 460 long err = _file.resize(&file_size); 461 462 if (err) 463 return err; 464 465 file->size = file_size; 466 _update_extent(file); 467 return L4_EOK; 468 } 469 470 471 472 /* Return the amount of data in the mapped region for the given file. */ 473 474 offset_t file_populated_span(file_t *file) 475 { 476 offset_t size = file_span(file); 477 return (file->data_end < size) ? file->data_end : size; 478 } 479 480 /* Return the size of the mapped region for the given file. */ 481 482 offset_t file_span(file_t *file) 483 { 484 return file->end_pos - file->start_pos; 485 } 486 487 488 489 /* Get a pointer to any terminated string at the given offset or NULL if the 490 data from offset is not terminated. */ 491 492 char *file_string_get(file_t *file, offset_t offset) 493 { 494 offset_t limit = file_span(file) - offset; 495 496 if (strnlen(file->memory + offset, limit) < limit) 497 return file->memory + offset; 498 else 499 return NULL; 500 } 501 502 /* Copy a string to the mapped region at the given offset, returning 1 (true) 503 where all characters were copied, 0 (false) otherwise. The precise number of 504 characters copied, excluding the zero terminator is provided via the written 505 parameter if it is not specified as NULL. */ 506 507 int file_string_set(file_t *file, const char *data, offset_t offset, 508 offset_t *written) 509 { 510 offset_t i, pos, limit = file_span(file); 511 512 /* Do not attempt to copy data with an invalid offset. */ 513 514 if (offset >= limit) 515 { 516 if (written != NULL) 517 *written = 0; 518 return 0; 519 } 520 521 /* Copy the data to the given offset, stopping at the end of the region. */ 522 523 for (i = 0, pos = offset; pos < limit; i++, pos++) 524 { 525 file->memory[pos] = data[i]; 526 527 /* Terminator written, can return immediately. */ 528 529 if (!data[i]) 530 { 531 if (written != NULL) 532 *written = pos - offset; 533 return 1; 534 } 535 } 536 537 /* Terminate the incomplete string at the end of the region. */ 538 539 file->memory[limit - 1] = '\0'; 540 if (written != NULL) 541 *written = limit - 1 - offset; 542 return 0; 543 } 544 545 546 547 /* Return the number of remaining populated bytes in the region. */ 548 549 offset_t file_data_available(file_t *file) 550 { 551 if (file_populated_span(file) > file->data_current) 552 return file_populated_span(file) - file->data_current; 553 else 554 return 0; 555 } 556 557 /* Return the current data offset in the region. */ 558 559 char *file_data_current(file_t *file) 560 { 561 return file->memory + file->data_current; 562 } 563 564 /* Return the current access position in the file. */ 565 566 offset_t file_data_current_position(file_t *file) 567 { 568 return file->start_pos + file->data_current; 569 } 570 571 /* Return the position of the end of the populated bytes in the region. */ 572 573 offset_t file_data_end_position(file_t *file) 574 { 575 return file->start_pos + file->data_end; 576 } 577 578 /* Return the amount of remaining space in the region. */ 579 580 offset_t file_data_space(file_t *file) 581 { 582 return file_span(file) - file->data_current; 583 } 584 585 586 587 /* Copy data to the given buffer from the current data position, updating the 588 position. */ 589 590 void file_data_read(file_t *file, char *buf, offset_t to_transfer) 591 { 592 memcpy(buf, file_data_current(file), to_transfer); 593 594 /* Update position details. */ 595 596 file->data_current += to_transfer; 597 } 598 599 /* Copy data from the given buffer to the current data position, updating the 600 position and the extent of populated data if this was exceeded. */ 601 602 void file_data_write(file_t *file, char *buf, offset_t to_transfer) 603 { 604 memcpy(file_data_current(file), buf, to_transfer); 605 606 /* Update position details. */ 607 608 file->data_current += to_transfer; 609 610 if (file->data_current > file->data_end) 611 file->data_end = file->data_current; 612 } 613 614 615 616 /* Conversion to the generic notification types. */ 617 618 notifiable_t *file_notifiable(file_t *file) 619 { 620 return notify_notifiable((notifiable_base_t *) file); 621 } 622 623 /* Return the notification flags for a file. */ 624 625 notify_flags_t file_notifications(file_t *file) 626 { 627 return notify_notifications((notifiable_base_t *) file); 628 } 629 630 /* Wait for a notification event on a file. */ 631 632 long file_notify_wait_file(file_t *file, notifier_t *notifier) 633 { 634 long err = notify_wait(file_notifiable(file), notifier); 635 636 /* Unsubscribe if a closure notification has been received. */ 637 638 if (!err && (file_notifications(file) & NOTIFY_PEER_CLOSED)) 639 notify_unsubscribe(file_notifiable(file), notifier); 640 641 return err; 642 } 643 644 /* Wait for notification events on files. */ 645 646 long file_notify_wait_files(file_t **file, notifier_t *notifier) 647 { 648 notifiable_t *notifiable; 649 long err = notify_wait_many(¬ifiable, notifier); 650 651 *file = (file_t *) notifiable->base; 652 653 /* Unsubscribe if a closure notification has been received. */ 654 655 if (!err && (notifiable->notifications & NOTIFY_PEER_CLOSED)) 656 notify_unsubscribe(notifiable, notifier); 657 658 return err; 659 } 660 661 662 663 /* Open two pipe endpoints using the given pipe server. */ 664 665 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) 666 { 667 if (l4_is_invalid_cap(server)) 668 return -L4_EINVAL; 669 670 client_PipeOpener opener(server); 671 672 file_init(reader); 673 file_init(writer); 674 reader->flags = O_RDONLY; 675 writer->flags = O_WRONLY; 676 677 return opener.pipe(size, &reader->ref, &writer->ref); 678 } 679 680 /* Access the current region for a pipe endpoint. */ 681 682 long pipe_current(file_t *pipe, int sync) 683 { 684 client_Flush _pipe(pipe->ref); 685 offset_t data_current; 686 long err = _pipe.refresh(&data_current, &pipe->size, &pipe->end_pos); 687 688 if (err) 689 return err; 690 691 _update_extent(pipe); 692 693 /* Obtain the current position if synchronising position state. */ 694 695 if (sync) 696 pipe->data_current = data_current; 697 698 /* Handle any case where the current region has been exhausted. */ 699 700 if (!file_populated_span(pipe)) 701 pipe->data_current = 0; 702 703 /* Attach memory if necessary. */ 704 705 if (pipe->memory == NULL) 706 { 707 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 708 if (err) 709 return err; 710 } 711 712 return L4_EOK; 713 } 714 715 /* Access the next region for a pipe endpoint, updating the eventual size of 716 the current region if writing. */ 717 718 long pipe_next(file_t *pipe) 719 { 720 client_Pipe _pipe(pipe->ref); 721 offset_t size = pipe->data_current; 722 long err = _pipe.next_region(&size, &pipe->end_pos); 723 724 if (err) 725 return err; 726 727 pipe->size = size; 728 _update_extent(pipe); 729 730 if (pipe->memory != NULL) 731 err = ipc_detach_dataspace(pipe->memory); 732 733 if (err) 734 return err; 735 736 pipe->data_current = 0; 737 pipe->memory = NULL; 738 739 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 740 if (err) 741 return err; 742 743 return L4_EOK; 744 } 745 746 /* Set the size of the written region. */ 747 748 long pipe_written(file_t *pipe, offset_t size) 749 { 750 if (size <= pipe->size) 751 { 752 pipe->data_end = size; 753 return L4_EOK; 754 } 755 else 756 return -L4_EINVAL; 757 } 758 759 760 761 /* Obtain a directory listing stream from a directory. */ 762 763 long directory_opendir(file_t *file, file_t *reader) 764 { 765 client_Directory directory(file->ref); 766 file_init(reader); 767 reader->flags = O_RDONLY; 768 return directory.opendir(&reader->size, &reader->ref, &reader->object_flags); 769 } 770 771 // vim: tabstop=2 expandtab shiftwidth=2