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