1 /* 2 * Filesystem client functions. 3 * 4 * Copyright (C) 2018, 2019, 2020, 2021, 2022 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/re/env.h> 23 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include <systypes/fcntl.h> 29 30 #include "client.h" 31 32 33 34 /* Default size of pipe regions. */ 35 36 const offset_t DEFAULT_PIPE_SIZE = 4096; 37 38 /* Size of the core member region of a directory entry structure. */ 39 40 const offset_t DIRENT_CORE_SIZE = (sizeof(struct dirent) - sizeof(((struct dirent *) 0)->d_name)); 41 42 43 44 /* Merging of region flags from protection and access flags. */ 45 46 static l4re_rm_flags_t _combine_region_flags(l4re_rm_flags_t region_flags, 47 flags_t flags) 48 { 49 return region_flags & (file_region_flags(flags) | L4RE_RM_F_X); 50 } 51 52 /* Conversion of protection and access flags to region flags. */ 53 54 l4re_rm_flags_t client_region_flags(prot_t prot, flags_t flags) 55 { 56 l4re_rm_flags_t rm_flags = 0; 57 58 if (prot & PROT_READ) 59 rm_flags |= L4RE_RM_F_R; 60 if (prot & PROT_WRITE) 61 rm_flags |= L4RE_RM_F_W; 62 if (prot & PROT_EXEC) 63 rm_flags |= L4RE_RM_F_X; 64 65 return _combine_region_flags(rm_flags, flags); 66 } 67 68 69 70 /* Access the given position and synchronise state with the file object. Pipe 71 objects may return busy conditions indicating that the desired access cannot 72 yet be fulfilled. */ 73 74 static long _access(file_t *file, offset_t position) 75 { 76 long err; 77 78 if (file->object_flags & OBJECT_SUPPORTS_MMAP) 79 { 80 /* Where the position is outside the current region, re-map. */ 81 82 if ((position < file->start_pos) || (position >= file->end_pos)) 83 { 84 if (file_mmap(file, position, file_span(file), 85 file_region_flags(file->flags))) 86 return -L4_EIO; 87 } 88 89 /* Otherwise, flush any written data in the current region and update the 90 file size details. */ 91 92 else 93 { 94 err = client_flush(file); 95 if (err) 96 return err; 97 } 98 99 /* Update the current data offset. */ 100 101 file->data_current = position - file->start_pos; 102 103 return L4_EOK; 104 } 105 else 106 { 107 /* Handle the initial condition with no current region. */ 108 109 if (file->memory == NULL) 110 { 111 err = client_current_region(file); 112 if (err) 113 return err; 114 } 115 116 /* Strict conditions for region navigation in pipes. */ 117 118 if ((position < file->start_pos) || (position > file->end_pos)) 119 { 120 return -L4_EIO; 121 } 122 123 /* The next region is only available at the end of the mapped memory. */ 124 125 else if (position == file->end_pos) 126 { 127 err = client_next_region(file); 128 if (err) 129 return err; 130 131 file->data_current = 0; 132 return L4_EOK; 133 } 134 135 /* Within the current pipe region, synchronise with the pipe object. */ 136 137 else 138 return client_current_region(file); 139 } 140 } 141 142 143 144 /* Return whether an operation on file should block for more content or more 145 space. A file must be configured for blocking, not be closed, and must either 146 be lacking content (if reading) or space (if writing). */ 147 148 static int _operation_blocking(file_t *file, int reading) 149 { 150 return (file->can_block && !(file->notifications & NOTIFY_PEER_CLOSED) && ( 151 (reading && !file_data_available(file)) || 152 (!reading && !file_data_space(file)))); 153 } 154 155 156 157 /* Return whether an access could occur, blocking if necessary. */ 158 159 static int _access_blocking(file_t *file, offset_t position, int reading) 160 { 161 long err; 162 163 /* Attempt to access the position, handling an error condition or a blocking 164 condition. */ 165 166 while ((err = _access(file, position)) || _operation_blocking(file, reading)) 167 { 168 position = file->data_current; 169 170 /* Exit if blocking is not configured or suitable. */ 171 172 if ((err && (err != -L4_EBUSY)) || !file->can_block) 173 return 0; 174 175 /* Handle an inability to access by blocking, exiting if waiting failed. */ 176 177 if (client_wait_file(file, client_notifier_task())) 178 return 0; 179 } 180 181 return 1; 182 } 183 184 185 186 /* Ensure that memory is mapped for accessing the given file, using the 187 indicated count as a region size hint. */ 188 189 static void *_map_memory(file_t *file, offset_t count) 190 { 191 if (file->memory == NULL) 192 { 193 if (file->object_flags & OBJECT_SUPPORTS_MMAP) 194 return client_mmap(file, client_tell(file), count, 195 file_region_flags(file->flags)); 196 else if (pipe_current(file)) 197 return NULL; 198 } 199 200 return file->memory; 201 } 202 203 204 205 /* Open a file opening object. */ 206 207 l4_cap_idx_t client_open_for_user(user_t user) 208 { 209 l4_cap_idx_t server = l4re_env_get_cap("server"); 210 211 return client_open_for_user_using(user, server); 212 } 213 214 /* Open a file opening object via a named capability. */ 215 216 l4_cap_idx_t client_open_for_user_using(user_t user, l4_cap_idx_t server) 217 { 218 if (l4_is_invalid_cap(server)) 219 return L4_INVALID_CAP; 220 221 l4_cap_idx_t opener; 222 long err = file_open_for_user(user, server, &opener); 223 224 if (err) 225 return L4_INVALID_CAP; 226 227 return opener; 228 } 229 230 231 232 /* Close a filesystem object. */ 233 234 void client_close(file_t *file) 235 { 236 if (file == NULL) 237 return; 238 239 file_flush(file); 240 file_close(file); 241 free(file); 242 } 243 244 245 246 /* Open a filesystem object. */ 247 248 file_t *client_open(const char *name, flags_t flags) 249 { 250 l4_cap_idx_t server = l4re_env_get_cap("server"); 251 252 return client_open_using(name, flags, server); 253 } 254 255 /* Open a filesystem object via a named capability. */ 256 257 file_t *client_open_using(const char *name, flags_t flags, l4_cap_idx_t server) 258 { 259 if (l4_is_invalid_cap(server)) 260 return NULL; 261 262 file_t *file = (file_t *) malloc(sizeof(file_t)); 263 264 if (file == NULL) 265 return NULL; 266 267 if (file_open(file, name, flags, server)) 268 { 269 free(file); 270 return NULL; 271 } 272 273 return file; 274 } 275 276 277 278 /* Open a directory listing stream via the given named directory. */ 279 280 file_t *client_opendir(const char *name) 281 { 282 l4_cap_idx_t server = l4re_env_get_cap("server"); 283 284 return client_opendir_using(name, server); 285 } 286 287 /* Open a directory listing stream via the given named directory and a named 288 capability. */ 289 290 file_t *client_opendir_using(const char *name, l4_cap_idx_t server) 291 { 292 file_t *file = client_open_using(name, O_DIRECTORY, server); 293 294 if (file == NULL) 295 return NULL; 296 297 file_t *reader = client_opendir_at(file); 298 299 if (reader == NULL) 300 return NULL; 301 302 /* Release the directory and return the reader. */ 303 304 client_close(file); 305 return reader; 306 } 307 308 309 310 /* Open a directory listing stream via the given directory. */ 311 312 file_t *client_opendir_at(file_t *file) 313 { 314 if (file == NULL) 315 return NULL; 316 317 file_t *reader = (file_t *) malloc(sizeof(file_t)); 318 319 if (reader == NULL) 320 return NULL; 321 322 long err = directory_opendir(file, reader); 323 324 if (err) 325 return NULL; 326 327 /* Set blocking read mode to be able to conveniently read directory entries 328 from the stream. */ 329 330 if (client_set_blocking(reader, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED)) 331 { 332 client_close(reader); 333 return NULL; 334 } 335 336 return reader; 337 } 338 339 340 341 /* Open a pipe object. */ 342 343 long client_pipe(file_t **reader, file_t **writer, flags_t flags) 344 { 345 l4_cap_idx_t server = l4re_env_get_cap("pipes"); 346 347 return client_pipe_using(reader, writer, flags, server); 348 } 349 350 long client_pipe_using(file_t **reader, file_t **writer, flags_t flags, l4_cap_idx_t server) 351 { 352 if (l4_is_invalid_cap(server)) 353 return -L4_EINVAL; 354 355 *reader = (file_t *) malloc(sizeof(file_t)); 356 357 if (*reader == NULL) 358 return -L4_ENOMEM; 359 360 *writer = (file_t *) malloc(sizeof(file_t)); 361 362 if (*writer == NULL) 363 { 364 free(*reader); 365 return -L4_ENOMEM; 366 } 367 368 long err = pipe_open(DEFAULT_PIPE_SIZE, *reader, *writer, server); 369 370 /* Set blocking if successful and non-blocking is not indicated. */ 371 372 if (!err && !(flags & O_NONBLOCK)) 373 { 374 err = client_set_blocking(*reader, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); 375 if (!err) 376 err = client_set_blocking(*writer, NOTIFY_SPACE_AVAILABLE | NOTIFY_PEER_CLOSED); 377 } 378 379 if (err) 380 { 381 free(*reader); 382 free(*writer); 383 } 384 385 return err; 386 } 387 388 389 390 /* Make a directory in the filesystem. */ 391 392 long client_mkdir(const char *path, mode_t mode) 393 { 394 l4_cap_idx_t server = l4re_env_get_cap("server"); 395 396 return client_mkdir_using(path, mode, server); 397 } 398 399 /* Make a directory in the filesystem via a named capability. */ 400 401 long client_mkdir_using(const char *path, mode_t mode, l4_cap_idx_t server) 402 { 403 return file_mkdir(path, mode, server); 404 } 405 406 407 408 /* Remove a file from the filesystem. */ 409 410 long client_remove(const char *path) 411 { 412 l4_cap_idx_t server = l4re_env_get_cap("server"); 413 414 return client_remove_using(path, server); 415 } 416 417 /* Remove a file from the filesystem via a named capability. */ 418 419 long client_remove_using(const char *path, l4_cap_idx_t server) 420 { 421 return file_remove(path, server); 422 } 423 424 425 426 /* Rename a file in the filesystem. */ 427 428 long client_rename(const char *source, const char *target) 429 { 430 l4_cap_idx_t server = l4re_env_get_cap("server"); 431 432 return client_rename_using(source, target, server); 433 } 434 435 /* Rename a file in the filesystem via a named capability. */ 436 437 long client_rename_using(const char *source, const char *target, l4_cap_idx_t server) 438 { 439 return file_rename(source, target, server); 440 } 441 442 443 444 /* Obtain filesystem object statistics. */ 445 446 long client_stat(const char *path, struct stat *st) 447 { 448 l4_cap_idx_t server = l4re_env_get_cap("server"); 449 450 return client_stat_using(path, st, server); 451 } 452 453 /* Obtain object statistics from the filesystem via a named capability. */ 454 455 long client_stat_using(const char *path, struct stat *st, l4_cap_idx_t server) 456 { 457 return file_stat(path, st, server); 458 } 459 460 461 462 /* Obtain the current region of a pipe. */ 463 464 long client_current_region(file_t *file) 465 { 466 if (file == NULL) 467 return -L4_EINVAL; 468 469 return pipe_current(file); 470 } 471 472 473 474 /* Flush data explicitly to the filesystem object. */ 475 476 long client_flush(file_t *file) 477 { 478 if (file == NULL) 479 return -L4_EINVAL; 480 481 /* Flush and retain most buffer settings. */ 482 483 return file_flush(file); 484 } 485 486 487 488 /* Map a memory region to a file. */ 489 490 void *client_mmap(file_t *file, offset_t position, offset_t length, 491 l4re_rm_flags_t region_flags) 492 { 493 if ((file == NULL) || file_mmap(file, position, length, region_flags)) 494 return NULL; 495 496 return file->memory; 497 } 498 499 500 501 /* Obtain the next region of a pipe. */ 502 503 long client_next_region(file_t *file) 504 { 505 if (file == NULL) 506 return -L4_EINVAL; 507 508 return pipe_next(file); 509 } 510 511 512 513 /* Close a notifier object. */ 514 515 void client_notifier_close(file_notifier_t *notifier) 516 { 517 file_notify_close(notifier); 518 } 519 520 /* Obtain a local notifier object. */ 521 522 file_notifier_t *client_notifier_local() 523 { 524 return file_notify_local(); 525 } 526 527 /* Obtain a task-wide notifier object. */ 528 529 file_notifier_t *client_notifier_task() 530 { 531 return file_notify_task(); 532 } 533 534 535 536 /* Read a directory entry. This must be freed by the caller after use. */ 537 538 struct dirent *client_readdir(file_t *file) 539 { 540 char buffer[DIRENT_CORE_SIZE]; 541 offset_t nread = client_read(file, buffer, DIRENT_CORE_SIZE); 542 543 /* Stop if no new structure can be successfully read. */ 544 545 if (nread != DIRENT_CORE_SIZE) 546 return NULL; 547 548 struct dirent *dirent = (struct dirent *) buffer; 549 offset_t remaining = dirent->d_reclen - DIRENT_CORE_SIZE; 550 551 /* Allocate a buffer for the complete structure. */ 552 553 char *entry = (char *) calloc(DIRENT_CORE_SIZE + remaining, sizeof(char)); 554 555 if (entry == NULL) 556 return NULL; 557 558 /* Copy the start of the entry into a new buffer. */ 559 560 memcpy(entry, buffer, DIRENT_CORE_SIZE); 561 562 /* Append to the entry buffer. */ 563 564 char *current = entry + DIRENT_CORE_SIZE; 565 566 nread = client_read(file, current, remaining); 567 568 /* Stop if no complete structure can be successfully read. */ 569 570 if (nread != remaining) 571 { 572 free(entry); 573 return NULL; 574 } 575 576 return (struct dirent *) entry; 577 } 578 579 580 581 /* Read from the filesystem object into the buffer provided. */ 582 583 offset_t client_read(file_t *file, void *buf, offset_t count) 584 { 585 if (file == NULL) 586 return 0; 587 588 /* Map memory if none has been mapped so far. */ 589 590 if (_map_memory(file, count) == NULL) 591 return 0; 592 593 /* Amount available in the descriptor buffer already. */ 594 595 offset_t available = file_data_available(file); 596 offset_t to_transfer, total = 0; 597 598 while (count > 0) 599 { 600 /* If there is no data, try and obtain more data. */ 601 602 if (!available) 603 { 604 /* Flush any unwritten data, preparing to read from the file position at 605 the end of the data, and returning if no new data is available. */ 606 607 if (!_access_blocking(file, file_data_end_position(file), 1)) 608 break; 609 610 available = file_data_available(file); 611 612 if (!available) 613 break; 614 } 615 616 /* Transfer data into the supplied buffer. */ 617 618 to_transfer = available <= count ? available : count; 619 620 file_data_read(file, (char *) buf, to_transfer); 621 622 /* Update counters. */ 623 624 available -= to_transfer; 625 626 count -= to_transfer; 627 total += to_transfer; 628 629 buf = ((char *) buf + to_transfer); 630 } 631 632 return total; 633 } 634 635 636 637 /* Ensure that the buffer can provide the needed data. */ 638 639 offset_t client_seek(file_t *file, offset_t offset, int whence) 640 { 641 if (file == NULL) 642 return 0; 643 644 offset_t position, current = file_data_current_position(file), change; 645 646 switch (whence) 647 { 648 case SEEK_SET: 649 position = offset; 650 break; 651 652 case SEEK_CUR: 653 position = current + offset; 654 break; 655 656 case SEEK_END: 657 position = file->size + offset; 658 break; 659 660 default: 661 /* NOTE: Set errno to EINVAL. */ 662 return current; 663 } 664 665 /* Retain the current position if unchanged. */ 666 667 if (position == current) 668 return position; 669 670 /* Move forward in the file. */ 671 672 if (position > current) 673 { 674 change = position - current; 675 676 /* Move towards the end of available data. 677 Request new data if not enough is available. */ 678 679 if (change <= file_data_available(file)) 680 { 681 file->data_current += change; 682 return position; 683 } 684 } 685 686 /* Move backward in the file. */ 687 688 else 689 { 690 change = current - position; 691 692 /* Move towards the start of available data. 693 Request new data if moving beyond the start of the data. */ 694 695 if (change <= file->data_current) 696 { 697 file->data_current -= change; 698 return position; 699 } 700 } 701 702 /* Handle unwritten data and reset the buffer for reading. */ 703 704 if (_access(file, position)) 705 return current; 706 707 return position; 708 } 709 710 711 712 /* Set or unset blocking access for a file. */ 713 714 long client_set_blocking(file_t *file, notify_flags_t flags) 715 { 716 long err; 717 718 if (file->can_block == flags) 719 return L4_EOK; 720 721 /* Since blocking access is used with specific file notifications, the 722 per-task notifier is used. */ 723 724 file_notifier_t *notifier = client_notifier_task(); 725 726 if (flags) 727 err = client_subscribe(file, flags, notifier); 728 else 729 err = client_unsubscribe(file, notifier); 730 731 if (err) 732 return err; 733 734 file->can_block = flags; 735 return L4_EOK; 736 } 737 738 739 740 /* Subscribe from events concerning a file. */ 741 742 long client_subscribe(file_t *file, notify_flags_t flags, file_notifier_t *notifier) 743 { 744 if (file == NULL) 745 return -L4_EINVAL; 746 747 return file_notify_subscribe(file, flags, notifier); 748 } 749 750 751 752 /* Return the current position in the file. */ 753 754 long client_tell(file_t *file) 755 { 756 if (file == NULL) 757 return -L4_EINVAL; 758 759 return file_data_current_position(file); 760 } 761 762 763 764 /* Unsubscribe from events concerning a file. */ 765 766 long client_unsubscribe(file_t *file, file_notifier_t *notifier) 767 { 768 if (file == NULL) 769 return -L4_EINVAL; 770 771 return file_notify_unsubscribe(file, notifier); 772 } 773 774 775 776 /* Wait for events involving a specific file. */ 777 778 long client_wait_file(file_t *file, file_notifier_t *notifier) 779 { 780 if (file == NULL) 781 return -L4_EINVAL; 782 783 return file_notify_wait_file(file, notifier); 784 } 785 786 /* Wait for events concerning files, referencing a file object if an event is 787 delivered. */ 788 789 long client_wait_files(file_t **file, file_notifier_t *notifier) 790 { 791 return file_notify_wait_files(file, notifier); 792 } 793 794 795 796 /* Write to the filesystem object from the buffer provided. */ 797 798 offset_t client_write(file_t *file, const void *buf, offset_t count) 799 { 800 if (file == NULL) 801 return 0; 802 803 /* Map memory if none has been mapped so far. */ 804 805 if (_map_memory(file, count) == NULL) 806 return 0; 807 808 /* Attempt to ensure that the file can accept the amount of data to be 809 written. This may not resize to the needed amount if a file has a fixed 810 size, but data will still be written to any available space. */ 811 812 offset_t needed_size = file_data_current_position(file) + count; 813 814 if (file->object_flags & OBJECT_HAS_SIZE) 815 { 816 if (file->size < needed_size) 817 { 818 file_resize(file, needed_size); 819 820 if (file->size < needed_size) 821 count = file->size - file_data_current_position(file); 822 } 823 } 824 825 /* Space remaining in the descriptor buffer. */ 826 827 offset_t space = file_data_space(file); 828 offset_t to_transfer, total = 0; 829 830 while (count > 0) 831 { 832 /* If no space is available, try and send data, reset the buffer. */ 833 834 if (!space) 835 { 836 /* Flush any unwritten data and continue writing from the current data 837 position. */ 838 839 if (!_access_blocking(file, file_data_current_position(file), 0)) 840 break; 841 842 space = file_data_space(file); 843 } 844 845 /* Transfer data into the supplied buffer. */ 846 847 to_transfer = space <= count ? space : count; 848 849 file_data_write(file, (char *) buf, to_transfer); 850 851 /* Update counters. */ 852 853 space -= to_transfer; 854 855 count -= to_transfer; 856 total += to_transfer; 857 858 buf = ((char *) buf + to_transfer); 859 } 860 861 return total; 862 } 863 864 // vim: tabstop=2 expandtab shiftwidth=2