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