paul@90 | 1 | /* |
paul@90 | 2 | * Filesystem client functions. |
paul@90 | 3 | * |
paul@90 | 4 | * Copyright (C) 2018, 2019, 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@90 | 5 | * |
paul@90 | 6 | * This program is free software; you can redistribute it and/or |
paul@90 | 7 | * modify it under the terms of the GNU General Public License as |
paul@90 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@90 | 9 | * the License, or (at your option) any later version. |
paul@90 | 10 | * |
paul@90 | 11 | * This program is distributed in the hope that it will be useful, |
paul@90 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@90 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@90 | 14 | * GNU General Public License for more details. |
paul@90 | 15 | * |
paul@90 | 16 | * You should have received a copy of the GNU General Public License |
paul@90 | 17 | * along with this program; if not, write to the Free Software |
paul@90 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@90 | 19 | * Boston, MA 02110-1301, USA |
paul@90 | 20 | */ |
paul@90 | 21 | |
paul@90 | 22 | #include <l4/re/env.h> |
paul@123 | 23 | #include <ipc/irq.h> |
paul@90 | 24 | |
paul@90 | 25 | #include <stdio.h> |
paul@90 | 26 | #include <stdlib.h> |
paul@90 | 27 | |
paul@90 | 28 | #include "client.h" |
paul@90 | 29 | |
paul@90 | 30 | |
paul@90 | 31 | |
paul@90 | 32 | /* Default size of pipe regions. */ |
paul@90 | 33 | |
paul@90 | 34 | const offset_t DEFAULT_PIPE_SIZE = 4096; |
paul@90 | 35 | |
paul@90 | 36 | |
paul@90 | 37 | |
paul@117 | 38 | /* Access the given position and synchronise state with the file object. */ |
paul@117 | 39 | |
paul@117 | 40 | static long _access(file_t *file, offset_t position) |
paul@117 | 41 | { |
paul@117 | 42 | long err; |
paul@117 | 43 | |
paul@117 | 44 | if (file->can_mmap) |
paul@117 | 45 | { |
paul@117 | 46 | /* Where the position is outside the current region, re-map. */ |
paul@117 | 47 | |
paul@117 | 48 | if ((position < file->start_pos) || (position >= file->end_pos)) |
paul@117 | 49 | { |
paul@117 | 50 | if (file_mmap(file, position, file_span(file))) |
paul@117 | 51 | return -L4_EIO; |
paul@117 | 52 | } |
paul@117 | 53 | |
paul@117 | 54 | /* Otherwise, flush any written data in the current region and update the |
paul@117 | 55 | file size details. */ |
paul@117 | 56 | |
paul@117 | 57 | else |
paul@117 | 58 | { |
paul@117 | 59 | err = client_flush(file); |
paul@117 | 60 | if (err) |
paul@117 | 61 | return err; |
paul@117 | 62 | } |
paul@117 | 63 | } |
paul@117 | 64 | else |
paul@117 | 65 | { |
paul@117 | 66 | /* Strict conditions for region navigation in pipes. */ |
paul@117 | 67 | |
paul@117 | 68 | if ((position < file->start_pos) || (position > file->end_pos)) |
paul@117 | 69 | { |
paul@117 | 70 | return -L4_EIO; |
paul@117 | 71 | } |
paul@117 | 72 | |
paul@117 | 73 | /* The next region is only available at the end of the mapped memory. */ |
paul@117 | 74 | |
paul@117 | 75 | else if (position == file->end_pos) |
paul@117 | 76 | { |
paul@117 | 77 | err = client_next_region(file); |
paul@117 | 78 | if (err) |
paul@117 | 79 | return err; |
paul@117 | 80 | |
paul@117 | 81 | file->data_current = 0; |
paul@117 | 82 | return L4_EOK; |
paul@117 | 83 | } |
paul@117 | 84 | |
paul@117 | 85 | /* Within the current pipe region, synchronise with the pipe object. */ |
paul@117 | 86 | |
paul@117 | 87 | else |
paul@117 | 88 | { |
paul@117 | 89 | err = client_current_region(file); |
paul@117 | 90 | if (err) |
paul@117 | 91 | return err; |
paul@117 | 92 | } |
paul@117 | 93 | } |
paul@117 | 94 | |
paul@117 | 95 | /* Update the current data offset. */ |
paul@117 | 96 | |
paul@117 | 97 | file->data_current = position - file->start_pos; |
paul@117 | 98 | |
paul@117 | 99 | return L4_EOK; |
paul@117 | 100 | } |
paul@117 | 101 | |
paul@117 | 102 | |
paul@117 | 103 | |
paul@117 | 104 | /* Return whether an access could occur, blocking if necessary. */ |
paul@117 | 105 | |
paul@117 | 106 | static int _access_blocking(file_t *file, offset_t position) |
paul@117 | 107 | { |
paul@117 | 108 | long err; |
paul@117 | 109 | |
paul@117 | 110 | while ((err = _access(file, position))) |
paul@117 | 111 | { |
paul@117 | 112 | /* Exit if blocking is not configured or suitable. */ |
paul@117 | 113 | |
paul@117 | 114 | if ((err != -L4_EBUSY) || !file->can_block) |
paul@117 | 115 | return 0; |
paul@117 | 116 | |
paul@117 | 117 | /* Handle an inability to access by blocking, exiting if waiting failed. */ |
paul@117 | 118 | |
paul@123 | 119 | if (client_wait_file(file)) |
paul@117 | 120 | return 0; |
paul@117 | 121 | } |
paul@117 | 122 | |
paul@117 | 123 | return 1; |
paul@117 | 124 | } |
paul@117 | 125 | |
paul@117 | 126 | |
paul@117 | 127 | |
paul@117 | 128 | /* Ensure that memory is mapped for accessing the given file, using the |
paul@117 | 129 | indicated count as a region size hint. */ |
paul@117 | 130 | |
paul@117 | 131 | static void *_map_memory(file_t *file, offset_t count) |
paul@117 | 132 | { |
paul@117 | 133 | if (file->memory == NULL) |
paul@117 | 134 | { |
paul@117 | 135 | if (file->can_mmap) |
paul@117 | 136 | return client_mmap(file, client_tell(file), count); |
paul@117 | 137 | else if (pipe_current(file)) |
paul@117 | 138 | return NULL; |
paul@117 | 139 | } |
paul@117 | 140 | |
paul@117 | 141 | return file->memory; |
paul@117 | 142 | } |
paul@117 | 143 | |
paul@117 | 144 | |
paul@117 | 145 | |
paul@90 | 146 | /* Close a filesystem object. */ |
paul@90 | 147 | |
paul@90 | 148 | void client_close(file_t *file) |
paul@90 | 149 | { |
paul@90 | 150 | if (file == NULL) |
paul@90 | 151 | return; |
paul@90 | 152 | |
paul@90 | 153 | file_close(file); |
paul@90 | 154 | free(file); |
paul@90 | 155 | } |
paul@90 | 156 | |
paul@90 | 157 | |
paul@90 | 158 | |
paul@90 | 159 | /* Open a filesystem object. */ |
paul@90 | 160 | |
paul@90 | 161 | file_t *client_open(const char *name, flags_t flags) |
paul@90 | 162 | { |
paul@135 | 163 | return client_open_using(name, flags, "server"); |
paul@103 | 164 | } |
paul@103 | 165 | |
paul@103 | 166 | /* Open a filesystem object via a named capability. */ |
paul@103 | 167 | |
paul@135 | 168 | file_t *client_open_using(const char *name, flags_t flags, const char *cap) |
paul@103 | 169 | { |
paul@90 | 170 | file_t *file = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 171 | |
paul@90 | 172 | if (file == NULL) |
paul@90 | 173 | return NULL; |
paul@90 | 174 | |
paul@103 | 175 | l4_cap_idx_t server = l4re_env_get_cap(cap); |
paul@90 | 176 | |
paul@90 | 177 | if (file_open(file, name, flags, server)) |
paul@90 | 178 | { |
paul@90 | 179 | free(file); |
paul@90 | 180 | return NULL; |
paul@90 | 181 | } |
paul@90 | 182 | |
paul@90 | 183 | return file; |
paul@90 | 184 | } |
paul@90 | 185 | |
paul@90 | 186 | |
paul@90 | 187 | |
paul@90 | 188 | /* Open a pipe object. */ |
paul@90 | 189 | |
paul@90 | 190 | long client_pipe(file_t **reader, file_t **writer) |
paul@90 | 191 | { |
paul@135 | 192 | return client_pipe_using(reader, writer, "pipes"); |
paul@135 | 193 | } |
paul@135 | 194 | |
paul@135 | 195 | long client_pipe_using(file_t **reader, file_t **writer, const char *cap) |
paul@135 | 196 | { |
paul@90 | 197 | *reader = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 198 | |
paul@90 | 199 | if (*reader == NULL) |
paul@90 | 200 | return -L4_ENOMEM; |
paul@90 | 201 | |
paul@90 | 202 | *writer = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 203 | |
paul@90 | 204 | if (*writer == NULL) |
paul@90 | 205 | { |
paul@90 | 206 | free(*reader); |
paul@90 | 207 | return -L4_ENOMEM; |
paul@90 | 208 | } |
paul@90 | 209 | |
paul@135 | 210 | l4_cap_idx_t server = l4re_env_get_cap(cap); |
paul@90 | 211 | |
paul@90 | 212 | long err = pipe_open(DEFAULT_PIPE_SIZE, *reader, *writer, server); |
paul@90 | 213 | |
paul@90 | 214 | if (err) |
paul@90 | 215 | { |
paul@90 | 216 | free(*reader); |
paul@90 | 217 | free(*writer); |
paul@90 | 218 | } |
paul@90 | 219 | |
paul@90 | 220 | return err; |
paul@90 | 221 | } |
paul@90 | 222 | |
paul@90 | 223 | |
paul@90 | 224 | |
paul@90 | 225 | /* Flush data explicitly to the filesystem object. */ |
paul@90 | 226 | |
paul@90 | 227 | long client_flush(file_t *file) |
paul@90 | 228 | { |
paul@90 | 229 | if (file == NULL) |
paul@90 | 230 | return -L4_EINVAL; |
paul@90 | 231 | |
paul@90 | 232 | /* Flush and retain most buffer settings. */ |
paul@90 | 233 | |
paul@90 | 234 | return file_flush(file); |
paul@90 | 235 | } |
paul@90 | 236 | |
paul@90 | 237 | |
paul@90 | 238 | |
paul@90 | 239 | /* Map a memory region to a file. */ |
paul@90 | 240 | |
paul@90 | 241 | void *client_mmap(file_t *file, offset_t position, offset_t length) |
paul@90 | 242 | { |
paul@90 | 243 | if ((file == NULL) || (file_mmap(file, position, length))) |
paul@90 | 244 | return NULL; |
paul@90 | 245 | |
paul@90 | 246 | return file->memory; |
paul@90 | 247 | } |
paul@90 | 248 | |
paul@90 | 249 | |
paul@90 | 250 | |
paul@90 | 251 | /* Obtain the current region of a pipe. */ |
paul@90 | 252 | |
paul@116 | 253 | long client_current_region(file_t *file) |
paul@90 | 254 | { |
paul@116 | 255 | if (file == NULL) |
paul@116 | 256 | return -L4_EINVAL; |
paul@90 | 257 | |
paul@116 | 258 | return pipe_current(file); |
paul@90 | 259 | } |
paul@90 | 260 | |
paul@90 | 261 | |
paul@90 | 262 | |
paul@90 | 263 | /* Obtain the next region of a pipe. */ |
paul@90 | 264 | |
paul@116 | 265 | long client_next_region(file_t *file) |
paul@90 | 266 | { |
paul@116 | 267 | if (file == NULL) |
paul@116 | 268 | return -L4_EINVAL; |
paul@90 | 269 | |
paul@116 | 270 | return pipe_next(file); |
paul@90 | 271 | } |
paul@90 | 272 | |
paul@90 | 273 | |
paul@90 | 274 | |
paul@90 | 275 | /* Read from the filesystem object into the buffer provided. */ |
paul@90 | 276 | |
paul@90 | 277 | offset_t client_read(file_t *file, void *buf, offset_t count) |
paul@90 | 278 | { |
paul@90 | 279 | if (file == NULL) |
paul@90 | 280 | return 0; |
paul@90 | 281 | |
paul@100 | 282 | /* Map memory if none has been mapped so far. */ |
paul@100 | 283 | |
paul@108 | 284 | if (_map_memory(file, count) == NULL) |
paul@100 | 285 | return 0; |
paul@100 | 286 | |
paul@90 | 287 | /* Amount available in the descriptor buffer already. */ |
paul@90 | 288 | |
paul@90 | 289 | offset_t available = file_data_available(file); |
paul@90 | 290 | offset_t to_transfer, total = 0; |
paul@90 | 291 | |
paul@90 | 292 | while (count > 0) |
paul@90 | 293 | { |
paul@90 | 294 | /* If there is no data, try and obtain more data. */ |
paul@90 | 295 | |
paul@90 | 296 | if (!available) |
paul@90 | 297 | { |
paul@90 | 298 | /* Flush any unwritten data, preparing to read from the file position at |
paul@90 | 299 | the end of the data, and returning if no new data is available. */ |
paul@90 | 300 | |
paul@117 | 301 | if (!_access_blocking(file, file_data_end_position(file))) |
paul@90 | 302 | break; |
paul@90 | 303 | |
paul@90 | 304 | available = file_data_available(file); |
paul@90 | 305 | |
paul@90 | 306 | if (!available) |
paul@90 | 307 | break; |
paul@90 | 308 | } |
paul@90 | 309 | |
paul@90 | 310 | /* Transfer data into the supplied buffer. */ |
paul@90 | 311 | |
paul@90 | 312 | to_transfer = available <= count ? available : count; |
paul@90 | 313 | |
paul@90 | 314 | file_data_read(file, (char *) buf, to_transfer); |
paul@90 | 315 | |
paul@90 | 316 | /* Update counters. */ |
paul@90 | 317 | |
paul@90 | 318 | available -= to_transfer; |
paul@90 | 319 | |
paul@90 | 320 | count -= to_transfer; |
paul@90 | 321 | total += to_transfer; |
paul@90 | 322 | |
paul@90 | 323 | buf = ((char *) buf + to_transfer); |
paul@90 | 324 | } |
paul@90 | 325 | |
paul@90 | 326 | return total; |
paul@90 | 327 | } |
paul@90 | 328 | |
paul@90 | 329 | |
paul@90 | 330 | |
paul@90 | 331 | /* Ensure that the buffer can provide the needed data. */ |
paul@90 | 332 | |
paul@90 | 333 | offset_t client_seek(file_t *file, offset_t offset, int whence) |
paul@90 | 334 | { |
paul@90 | 335 | if (file == NULL) |
paul@90 | 336 | return 0; |
paul@90 | 337 | |
paul@90 | 338 | offset_t position, current = file_data_current_position(file), change; |
paul@90 | 339 | |
paul@90 | 340 | switch (whence) |
paul@90 | 341 | { |
paul@90 | 342 | case SEEK_SET: |
paul@90 | 343 | position = offset; |
paul@90 | 344 | break; |
paul@90 | 345 | |
paul@90 | 346 | case SEEK_CUR: |
paul@90 | 347 | position = current + offset; |
paul@90 | 348 | break; |
paul@90 | 349 | |
paul@90 | 350 | case SEEK_END: |
paul@90 | 351 | position = file->size + offset; |
paul@90 | 352 | break; |
paul@90 | 353 | |
paul@90 | 354 | default: |
paul@90 | 355 | /* NOTE: Set errno to EINVAL. */ |
paul@117 | 356 | return current; |
paul@90 | 357 | } |
paul@90 | 358 | |
paul@90 | 359 | /* Retain the current position if unchanged. */ |
paul@90 | 360 | |
paul@90 | 361 | if (position == current) |
paul@90 | 362 | return position; |
paul@90 | 363 | |
paul@90 | 364 | /* Move forward in the file. */ |
paul@90 | 365 | |
paul@90 | 366 | if (position > current) |
paul@90 | 367 | { |
paul@90 | 368 | change = position - current; |
paul@90 | 369 | |
paul@90 | 370 | /* Move towards the end of available data. |
paul@90 | 371 | Request new data if not enough is available. */ |
paul@90 | 372 | |
paul@90 | 373 | if (change <= file_data_available(file)) |
paul@90 | 374 | { |
paul@90 | 375 | file->data_current += change; |
paul@90 | 376 | return position; |
paul@90 | 377 | } |
paul@90 | 378 | } |
paul@90 | 379 | |
paul@90 | 380 | /* Move backward in the file. */ |
paul@90 | 381 | |
paul@90 | 382 | else |
paul@90 | 383 | { |
paul@90 | 384 | change = current - position; |
paul@90 | 385 | |
paul@90 | 386 | /* Move towards the start of available data. |
paul@90 | 387 | Request new data if moving beyond the start of the data. */ |
paul@90 | 388 | |
paul@90 | 389 | if (change <= file->data_current) |
paul@90 | 390 | { |
paul@90 | 391 | file->data_current -= change; |
paul@90 | 392 | return position; |
paul@90 | 393 | } |
paul@90 | 394 | } |
paul@90 | 395 | |
paul@90 | 396 | /* Handle unwritten data and reset the buffer for reading. */ |
paul@90 | 397 | |
paul@117 | 398 | if (_access(file, position)) |
paul@117 | 399 | return current; |
paul@117 | 400 | |
paul@90 | 401 | return position; |
paul@90 | 402 | } |
paul@90 | 403 | |
paul@90 | 404 | |
paul@90 | 405 | |
paul@117 | 406 | /* Set or unset blocking access for a file. */ |
paul@117 | 407 | |
paul@122 | 408 | long client_set_blocking(file_t *file, notify_flags_t flags) |
paul@117 | 409 | { |
paul@117 | 410 | long err; |
paul@117 | 411 | |
paul@122 | 412 | if (file->can_block == flags) |
paul@117 | 413 | return L4_EOK; |
paul@117 | 414 | |
paul@117 | 415 | // NOTE: Set appropriate flags. |
paul@117 | 416 | |
paul@122 | 417 | if (flags) |
paul@122 | 418 | err = client_subscribe(file, flags); |
paul@117 | 419 | else |
paul@117 | 420 | err = client_unsubscribe(file); |
paul@117 | 421 | |
paul@117 | 422 | if (err) |
paul@117 | 423 | return err; |
paul@117 | 424 | |
paul@122 | 425 | file->can_block = flags; |
paul@117 | 426 | return L4_EOK; |
paul@117 | 427 | } |
paul@117 | 428 | |
paul@117 | 429 | |
paul@117 | 430 | |
paul@117 | 431 | /* Subscribe from events concerning a file. */ |
paul@117 | 432 | |
paul@121 | 433 | long client_subscribe(file_t *file, notify_flags_t flags) |
paul@117 | 434 | { |
paul@117 | 435 | if (file == NULL) |
paul@117 | 436 | return -L4_EINVAL; |
paul@117 | 437 | |
paul@117 | 438 | return file_notify_subscribe(file, flags); |
paul@117 | 439 | } |
paul@117 | 440 | |
paul@117 | 441 | |
paul@117 | 442 | |
paul@117 | 443 | /* Return the current position in the file. */ |
paul@117 | 444 | |
paul@90 | 445 | long client_tell(file_t *file) |
paul@90 | 446 | { |
paul@90 | 447 | if (file == NULL) |
paul@90 | 448 | return -L4_EINVAL; |
paul@90 | 449 | |
paul@90 | 450 | return file_data_current_position(file); |
paul@90 | 451 | } |
paul@90 | 452 | |
paul@90 | 453 | |
paul@90 | 454 | |
paul@117 | 455 | /* Unsubscribe from events concerning a file. */ |
paul@117 | 456 | |
paul@117 | 457 | long client_unsubscribe(file_t *file) |
paul@117 | 458 | { |
paul@117 | 459 | if (file == NULL) |
paul@117 | 460 | return -L4_EINVAL; |
paul@117 | 461 | |
paul@117 | 462 | return file_notify_unsubscribe(file); |
paul@117 | 463 | } |
paul@117 | 464 | |
paul@117 | 465 | |
paul@117 | 466 | |
paul@123 | 467 | /* Wait for events involving a specific file. */ |
paul@123 | 468 | |
paul@123 | 469 | long client_wait_file(file_t *file) |
paul@123 | 470 | { |
paul@123 | 471 | if (file == NULL) |
paul@123 | 472 | return -L4_EINVAL; |
paul@123 | 473 | |
paul@123 | 474 | return file_notify_wait_file(file); |
paul@123 | 475 | } |
paul@123 | 476 | |
paul@123 | 477 | /* Wait for events concerning files, referencing a file object if an event is |
paul@123 | 478 | delivered. */ |
paul@123 | 479 | |
paul@123 | 480 | long client_wait_files(file_t **file) |
paul@123 | 481 | { |
paul@123 | 482 | return file_notify_wait_files(file); |
paul@117 | 483 | } |
paul@117 | 484 | |
paul@117 | 485 | |
paul@117 | 486 | |
paul@90 | 487 | /* Write to the filesystem object from the buffer provided. */ |
paul@90 | 488 | |
paul@90 | 489 | offset_t client_write(file_t *file, const void *buf, offset_t count) |
paul@90 | 490 | { |
paul@90 | 491 | if (file == NULL) |
paul@90 | 492 | return 0; |
paul@90 | 493 | |
paul@100 | 494 | /* Map memory if none has been mapped so far. */ |
paul@100 | 495 | |
paul@108 | 496 | if (_map_memory(file, count) == NULL) |
paul@100 | 497 | return 0; |
paul@100 | 498 | |
paul@90 | 499 | /* Attempt to ensure that the file can accept the amount of data to be |
paul@90 | 500 | written. This may not resize to the needed amount if a file has a fixed |
paul@90 | 501 | size, but data will still be written to any available space. */ |
paul@90 | 502 | |
paul@90 | 503 | offset_t needed_size = file_data_current_position(file) + count; |
paul@90 | 504 | |
paul@108 | 505 | if (file->has_size) |
paul@90 | 506 | { |
paul@108 | 507 | if (file->size < needed_size) |
paul@108 | 508 | { |
paul@108 | 509 | file_resize(file, needed_size); |
paul@90 | 510 | |
paul@108 | 511 | if (file->size < needed_size) |
paul@108 | 512 | count = file->size - file_data_current_position(file); |
paul@108 | 513 | } |
paul@90 | 514 | } |
paul@90 | 515 | |
paul@90 | 516 | /* Space remaining in the descriptor buffer. */ |
paul@90 | 517 | |
paul@90 | 518 | offset_t space = file_data_space(file); |
paul@90 | 519 | offset_t to_transfer, total = 0; |
paul@90 | 520 | |
paul@90 | 521 | while (count > 0) |
paul@90 | 522 | { |
paul@90 | 523 | /* If no space is available, try and send data, reset the buffer. */ |
paul@90 | 524 | |
paul@90 | 525 | if (!space) |
paul@90 | 526 | { |
paul@90 | 527 | /* Flush any unwritten data and continue writing from the current data |
paul@90 | 528 | position. */ |
paul@90 | 529 | |
paul@117 | 530 | if (!_access_blocking(file, file_data_current_position(file))) |
paul@90 | 531 | break; |
paul@90 | 532 | |
paul@90 | 533 | space = file_data_space(file); |
paul@90 | 534 | } |
paul@90 | 535 | |
paul@90 | 536 | /* Transfer data into the supplied buffer. */ |
paul@90 | 537 | |
paul@90 | 538 | to_transfer = space <= count ? space : count; |
paul@90 | 539 | |
paul@90 | 540 | file_data_write(file, (char *) buf, to_transfer); |
paul@90 | 541 | |
paul@90 | 542 | /* Update counters. */ |
paul@90 | 543 | |
paul@90 | 544 | space -= to_transfer; |
paul@90 | 545 | |
paul@90 | 546 | count -= to_transfer; |
paul@90 | 547 | total += to_transfer; |
paul@90 | 548 | |
paul@90 | 549 | buf = ((char *) buf + to_transfer); |
paul@90 | 550 | } |
paul@90 | 551 | |
paul@90 | 552 | return total; |
paul@90 | 553 | } |
paul@90 | 554 | |
paul@90 | 555 | // vim: tabstop=2 expandtab shiftwidth=2 |