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@90 | 23 | |
paul@90 | 24 | #include <stdio.h> |
paul@90 | 25 | #include <stdlib.h> |
paul@90 | 26 | |
paul@90 | 27 | #include "client.h" |
paul@90 | 28 | |
paul@90 | 29 | |
paul@90 | 30 | |
paul@90 | 31 | /* Default size of pipe regions. */ |
paul@90 | 32 | |
paul@90 | 33 | const offset_t DEFAULT_PIPE_SIZE = 4096; |
paul@90 | 34 | |
paul@90 | 35 | |
paul@90 | 36 | |
paul@90 | 37 | /* Close a filesystem object. */ |
paul@90 | 38 | |
paul@90 | 39 | void client_close(file_t *file) |
paul@90 | 40 | { |
paul@90 | 41 | if (file == NULL) |
paul@90 | 42 | return; |
paul@90 | 43 | |
paul@90 | 44 | file_close(file); |
paul@90 | 45 | free(file); |
paul@90 | 46 | } |
paul@90 | 47 | |
paul@90 | 48 | |
paul@90 | 49 | |
paul@90 | 50 | /* Open a filesystem object. */ |
paul@90 | 51 | |
paul@90 | 52 | file_t *client_open(const char *name, flags_t flags) |
paul@90 | 53 | { |
paul@103 | 54 | return client_open_device("server", name, flags); |
paul@103 | 55 | } |
paul@103 | 56 | |
paul@103 | 57 | /* Open a filesystem object via a named capability. */ |
paul@103 | 58 | |
paul@103 | 59 | file_t *client_open_device(const char *cap, const char *name, flags_t flags) |
paul@103 | 60 | { |
paul@90 | 61 | file_t *file = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 62 | |
paul@90 | 63 | if (file == NULL) |
paul@90 | 64 | return NULL; |
paul@90 | 65 | |
paul@103 | 66 | l4_cap_idx_t server = l4re_env_get_cap(cap); |
paul@90 | 67 | |
paul@90 | 68 | if (file_open(file, name, flags, server)) |
paul@90 | 69 | { |
paul@90 | 70 | free(file); |
paul@90 | 71 | return NULL; |
paul@90 | 72 | } |
paul@90 | 73 | |
paul@90 | 74 | return file; |
paul@90 | 75 | } |
paul@90 | 76 | |
paul@90 | 77 | |
paul@90 | 78 | |
paul@90 | 79 | /* Open a pipe object. */ |
paul@90 | 80 | |
paul@90 | 81 | long client_pipe(file_t **reader, file_t **writer) |
paul@90 | 82 | { |
paul@90 | 83 | *reader = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 84 | |
paul@90 | 85 | if (*reader == NULL) |
paul@90 | 86 | return -L4_ENOMEM; |
paul@90 | 87 | |
paul@90 | 88 | *writer = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 89 | |
paul@90 | 90 | if (*writer == NULL) |
paul@90 | 91 | { |
paul@90 | 92 | free(*reader); |
paul@90 | 93 | return -L4_ENOMEM; |
paul@90 | 94 | } |
paul@90 | 95 | |
paul@90 | 96 | l4_cap_idx_t server = l4re_env_get_cap("pipes"); |
paul@90 | 97 | |
paul@90 | 98 | long err = pipe_open(DEFAULT_PIPE_SIZE, *reader, *writer, server); |
paul@90 | 99 | |
paul@90 | 100 | if (err) |
paul@90 | 101 | { |
paul@90 | 102 | free(*reader); |
paul@90 | 103 | free(*writer); |
paul@90 | 104 | } |
paul@90 | 105 | |
paul@90 | 106 | return err; |
paul@90 | 107 | } |
paul@90 | 108 | |
paul@90 | 109 | |
paul@90 | 110 | |
paul@113 | 111 | /* Access the given position and synchronise state with the file object. */ |
paul@90 | 112 | |
paul@113 | 113 | static long _access(file_t *file, offset_t position) |
paul@90 | 114 | { |
paul@90 | 115 | long err; |
paul@90 | 116 | |
paul@113 | 117 | if (file->can_mmap) |
paul@113 | 118 | { |
paul@113 | 119 | /* Where the position is outside the current region, re-map. */ |
paul@90 | 120 | |
paul@113 | 121 | if ((position < file->start_pos) || (position >= file->end_pos)) |
paul@90 | 122 | { |
paul@90 | 123 | if (file_mmap(file, position, file_span(file))) |
paul@90 | 124 | return -L4_EIO; |
paul@90 | 125 | } |
paul@90 | 126 | |
paul@113 | 127 | /* Otherwise, flush any written data in the current region and update the |
paul@113 | 128 | file size details. */ |
paul@90 | 129 | |
paul@108 | 130 | else |
paul@108 | 131 | { |
paul@108 | 132 | err = client_flush(file); |
paul@108 | 133 | if (err) |
paul@108 | 134 | return err; |
paul@108 | 135 | } |
paul@113 | 136 | } |
paul@113 | 137 | else |
paul@113 | 138 | { |
paul@113 | 139 | /* Strict conditions for region navigation in pipes. */ |
paul@113 | 140 | |
paul@113 | 141 | if ((position < file->start_pos) || (position > file->end_pos)) |
paul@113 | 142 | { |
paul@113 | 143 | return -L4_EIO; |
paul@113 | 144 | } |
paul@113 | 145 | |
paul@113 | 146 | /* The next region is only available at the end of the mapped memory. */ |
paul@113 | 147 | |
paul@113 | 148 | else if (position == file->end_pos) |
paul@113 | 149 | { |
paul@116 | 150 | err = client_next_region(file); |
paul@116 | 151 | if (err) |
paul@116 | 152 | return err; |
paul@113 | 153 | } |
paul@113 | 154 | |
paul@113 | 155 | /* Within the current pipe region, synchronise with the pipe object. */ |
paul@113 | 156 | |
paul@108 | 157 | else |
paul@108 | 158 | { |
paul@116 | 159 | err = client_current_region(file); |
paul@116 | 160 | if (err) |
paul@116 | 161 | return err; |
paul@108 | 162 | } |
paul@90 | 163 | } |
paul@90 | 164 | |
paul@90 | 165 | /* Update the current data offset. */ |
paul@90 | 166 | |
paul@108 | 167 | if (file->has_size) |
paul@108 | 168 | file->data_current = position - file->start_pos; |
paul@108 | 169 | else |
paul@108 | 170 | file->data_current = 0; |
paul@108 | 171 | |
paul@90 | 172 | return L4_EOK; |
paul@90 | 173 | } |
paul@90 | 174 | |
paul@90 | 175 | |
paul@90 | 176 | |
paul@90 | 177 | /* Flush data explicitly to the filesystem object. */ |
paul@90 | 178 | |
paul@90 | 179 | long client_flush(file_t *file) |
paul@90 | 180 | { |
paul@90 | 181 | if (file == NULL) |
paul@90 | 182 | return -L4_EINVAL; |
paul@90 | 183 | |
paul@90 | 184 | /* Flush and retain most buffer settings. */ |
paul@90 | 185 | |
paul@90 | 186 | return file_flush(file); |
paul@90 | 187 | } |
paul@90 | 188 | |
paul@90 | 189 | |
paul@90 | 190 | |
paul@90 | 191 | /* Map a memory region to a file. */ |
paul@90 | 192 | |
paul@90 | 193 | void *client_mmap(file_t *file, offset_t position, offset_t length) |
paul@90 | 194 | { |
paul@90 | 195 | if ((file == NULL) || (file_mmap(file, position, length))) |
paul@90 | 196 | return NULL; |
paul@90 | 197 | |
paul@90 | 198 | return file->memory; |
paul@90 | 199 | } |
paul@90 | 200 | |
paul@90 | 201 | |
paul@90 | 202 | |
paul@90 | 203 | /* Obtain the current region of a pipe. */ |
paul@90 | 204 | |
paul@116 | 205 | long client_current_region(file_t *file) |
paul@90 | 206 | { |
paul@116 | 207 | if (file == NULL) |
paul@116 | 208 | return -L4_EINVAL; |
paul@90 | 209 | |
paul@116 | 210 | return pipe_current(file); |
paul@90 | 211 | } |
paul@90 | 212 | |
paul@90 | 213 | |
paul@90 | 214 | |
paul@90 | 215 | /* Obtain the next region of a pipe. */ |
paul@90 | 216 | |
paul@116 | 217 | long client_next_region(file_t *file) |
paul@90 | 218 | { |
paul@116 | 219 | if (file == NULL) |
paul@116 | 220 | return -L4_EINVAL; |
paul@90 | 221 | |
paul@116 | 222 | return pipe_next(file); |
paul@90 | 223 | } |
paul@90 | 224 | |
paul@90 | 225 | |
paul@90 | 226 | |
paul@108 | 227 | /* Ensure that memory is mapped for accessing the given file, using the |
paul@108 | 228 | indicated count as a region size hint. */ |
paul@108 | 229 | |
paul@108 | 230 | static void *_map_memory(file_t *file, offset_t count) |
paul@108 | 231 | { |
paul@108 | 232 | if (file->memory == NULL) |
paul@108 | 233 | { |
paul@108 | 234 | if (file->can_mmap) |
paul@108 | 235 | return client_mmap(file, client_tell(file), count); |
paul@108 | 236 | else if (pipe_current(file)) |
paul@108 | 237 | return NULL; |
paul@108 | 238 | } |
paul@108 | 239 | |
paul@108 | 240 | return file->memory; |
paul@108 | 241 | } |
paul@108 | 242 | |
paul@108 | 243 | |
paul@108 | 244 | |
paul@90 | 245 | /* Read from the filesystem object into the buffer provided. */ |
paul@90 | 246 | |
paul@90 | 247 | offset_t client_read(file_t *file, void *buf, offset_t count) |
paul@90 | 248 | { |
paul@90 | 249 | if (file == NULL) |
paul@90 | 250 | return 0; |
paul@90 | 251 | |
paul@100 | 252 | /* Map memory if none has been mapped so far. */ |
paul@100 | 253 | |
paul@108 | 254 | if (_map_memory(file, count) == NULL) |
paul@100 | 255 | return 0; |
paul@100 | 256 | |
paul@90 | 257 | /* Amount available in the descriptor buffer already. */ |
paul@90 | 258 | |
paul@90 | 259 | offset_t available = file_data_available(file); |
paul@90 | 260 | offset_t to_transfer, total = 0; |
paul@90 | 261 | |
paul@90 | 262 | while (count > 0) |
paul@90 | 263 | { |
paul@90 | 264 | /* If there is no data, try and obtain more data. */ |
paul@90 | 265 | |
paul@90 | 266 | if (!available) |
paul@90 | 267 | { |
paul@90 | 268 | /* Flush any unwritten data, preparing to read from the file position at |
paul@90 | 269 | the end of the data, and returning if no new data is available. */ |
paul@90 | 270 | |
paul@113 | 271 | if (_access(file, file_data_end_position(file))) |
paul@90 | 272 | break; |
paul@90 | 273 | |
paul@90 | 274 | available = file_data_available(file); |
paul@90 | 275 | |
paul@90 | 276 | if (!available) |
paul@90 | 277 | break; |
paul@90 | 278 | } |
paul@90 | 279 | |
paul@90 | 280 | /* Transfer data into the supplied buffer. */ |
paul@90 | 281 | |
paul@90 | 282 | to_transfer = available <= count ? available : count; |
paul@90 | 283 | |
paul@90 | 284 | file_data_read(file, (char *) buf, to_transfer); |
paul@90 | 285 | |
paul@90 | 286 | /* Update counters. */ |
paul@90 | 287 | |
paul@90 | 288 | available -= to_transfer; |
paul@90 | 289 | |
paul@90 | 290 | count -= to_transfer; |
paul@90 | 291 | total += to_transfer; |
paul@90 | 292 | |
paul@90 | 293 | buf = ((char *) buf + to_transfer); |
paul@90 | 294 | } |
paul@90 | 295 | |
paul@90 | 296 | return total; |
paul@90 | 297 | } |
paul@90 | 298 | |
paul@90 | 299 | |
paul@90 | 300 | |
paul@90 | 301 | /* Ensure that the buffer can provide the needed data. */ |
paul@90 | 302 | |
paul@90 | 303 | offset_t client_seek(file_t *file, offset_t offset, int whence) |
paul@90 | 304 | { |
paul@90 | 305 | if (file == NULL) |
paul@90 | 306 | return 0; |
paul@90 | 307 | |
paul@90 | 308 | offset_t position, current = file_data_current_position(file), change; |
paul@90 | 309 | |
paul@90 | 310 | switch (whence) |
paul@90 | 311 | { |
paul@90 | 312 | case SEEK_SET: |
paul@90 | 313 | position = offset; |
paul@90 | 314 | break; |
paul@90 | 315 | |
paul@90 | 316 | case SEEK_CUR: |
paul@90 | 317 | position = current + offset; |
paul@90 | 318 | break; |
paul@90 | 319 | |
paul@90 | 320 | case SEEK_END: |
paul@90 | 321 | position = file->size + offset; |
paul@90 | 322 | break; |
paul@90 | 323 | |
paul@90 | 324 | default: |
paul@90 | 325 | /* NOTE: Set errno to EINVAL. */ |
paul@90 | 326 | return -1; |
paul@90 | 327 | } |
paul@90 | 328 | |
paul@90 | 329 | /* Retain the current position if unchanged. */ |
paul@90 | 330 | |
paul@90 | 331 | if (position == current) |
paul@90 | 332 | return position; |
paul@90 | 333 | |
paul@90 | 334 | /* Move forward in the file. */ |
paul@90 | 335 | |
paul@90 | 336 | if (position > current) |
paul@90 | 337 | { |
paul@90 | 338 | change = position - current; |
paul@90 | 339 | |
paul@90 | 340 | /* Move towards the end of available data. |
paul@90 | 341 | Request new data if not enough is available. */ |
paul@90 | 342 | |
paul@90 | 343 | if (change <= file_data_available(file)) |
paul@90 | 344 | { |
paul@90 | 345 | file->data_current += change; |
paul@90 | 346 | return position; |
paul@90 | 347 | } |
paul@90 | 348 | } |
paul@90 | 349 | |
paul@90 | 350 | /* Move backward in the file. */ |
paul@90 | 351 | |
paul@90 | 352 | else |
paul@90 | 353 | { |
paul@90 | 354 | change = current - position; |
paul@90 | 355 | |
paul@90 | 356 | /* Move towards the start of available data. |
paul@90 | 357 | Request new data if moving beyond the start of the data. */ |
paul@90 | 358 | |
paul@90 | 359 | if (change <= file->data_current) |
paul@90 | 360 | { |
paul@90 | 361 | file->data_current -= change; |
paul@90 | 362 | return position; |
paul@90 | 363 | } |
paul@90 | 364 | } |
paul@90 | 365 | |
paul@90 | 366 | /* Handle unwritten data and reset the buffer for reading. */ |
paul@90 | 367 | |
paul@113 | 368 | _access(file, position); |
paul@90 | 369 | return position; |
paul@90 | 370 | } |
paul@90 | 371 | |
paul@90 | 372 | |
paul@90 | 373 | |
paul@90 | 374 | long client_tell(file_t *file) |
paul@90 | 375 | { |
paul@90 | 376 | if (file == NULL) |
paul@90 | 377 | return -L4_EINVAL; |
paul@90 | 378 | |
paul@90 | 379 | return file_data_current_position(file); |
paul@90 | 380 | } |
paul@90 | 381 | |
paul@90 | 382 | |
paul@90 | 383 | |
paul@90 | 384 | /* Write to the filesystem object from the buffer provided. */ |
paul@90 | 385 | |
paul@90 | 386 | offset_t client_write(file_t *file, const void *buf, offset_t count) |
paul@90 | 387 | { |
paul@90 | 388 | if (file == NULL) |
paul@90 | 389 | return 0; |
paul@90 | 390 | |
paul@100 | 391 | /* Map memory if none has been mapped so far. */ |
paul@100 | 392 | |
paul@108 | 393 | if (_map_memory(file, count) == NULL) |
paul@100 | 394 | return 0; |
paul@100 | 395 | |
paul@90 | 396 | /* Attempt to ensure that the file can accept the amount of data to be |
paul@90 | 397 | written. This may not resize to the needed amount if a file has a fixed |
paul@90 | 398 | size, but data will still be written to any available space. */ |
paul@90 | 399 | |
paul@90 | 400 | offset_t needed_size = file_data_current_position(file) + count; |
paul@90 | 401 | |
paul@108 | 402 | if (file->has_size) |
paul@90 | 403 | { |
paul@108 | 404 | if (file->size < needed_size) |
paul@108 | 405 | { |
paul@108 | 406 | file_resize(file, needed_size); |
paul@90 | 407 | |
paul@108 | 408 | if (file->size < needed_size) |
paul@108 | 409 | count = file->size - file_data_current_position(file); |
paul@108 | 410 | } |
paul@90 | 411 | } |
paul@90 | 412 | |
paul@90 | 413 | /* Space remaining in the descriptor buffer. */ |
paul@90 | 414 | |
paul@90 | 415 | offset_t space = file_data_space(file); |
paul@90 | 416 | offset_t to_transfer, total = 0; |
paul@90 | 417 | |
paul@90 | 418 | while (count > 0) |
paul@90 | 419 | { |
paul@90 | 420 | /* If no space is available, try and send data, reset the buffer. */ |
paul@90 | 421 | |
paul@90 | 422 | if (!space) |
paul@90 | 423 | { |
paul@90 | 424 | /* Flush any unwritten data and continue writing from the current data |
paul@90 | 425 | position. */ |
paul@90 | 426 | |
paul@113 | 427 | if (_access(file, file_data_current_position(file))) |
paul@90 | 428 | break; |
paul@90 | 429 | |
paul@90 | 430 | space = file_data_space(file); |
paul@90 | 431 | } |
paul@90 | 432 | |
paul@90 | 433 | /* Transfer data into the supplied buffer. */ |
paul@90 | 434 | |
paul@90 | 435 | to_transfer = space <= count ? space : count; |
paul@90 | 436 | |
paul@90 | 437 | file_data_write(file, (char *) buf, to_transfer); |
paul@90 | 438 | |
paul@90 | 439 | /* Update counters. */ |
paul@90 | 440 | |
paul@90 | 441 | space -= to_transfer; |
paul@90 | 442 | |
paul@90 | 443 | count -= to_transfer; |
paul@90 | 444 | total += to_transfer; |
paul@90 | 445 | |
paul@90 | 446 | buf = ((char *) buf + to_transfer); |
paul@90 | 447 | } |
paul@90 | 448 | |
paul@90 | 449 | return total; |
paul@90 | 450 | } |
paul@90 | 451 | |
paul@90 | 452 | // vim: tabstop=2 expandtab shiftwidth=2 |