paul@31 | 1 | /* |
paul@31 | 2 | * File access convenience functions. |
paul@31 | 3 | * |
paul@31 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@31 | 5 | * |
paul@31 | 6 | * This program is free software; you can redistribute it and/or |
paul@31 | 7 | * modify it under the terms of the GNU General Public License as |
paul@31 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@31 | 9 | * the License, or (at your option) any later version. |
paul@31 | 10 | * |
paul@31 | 11 | * This program is distributed in the hope that it will be useful, |
paul@31 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@31 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@31 | 14 | * GNU General Public License for more details. |
paul@31 | 15 | * |
paul@31 | 16 | * You should have received a copy of the GNU General Public License |
paul@31 | 17 | * along with this program; if not, write to the Free Software |
paul@31 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@31 | 19 | * Boston, MA 02110-1301, USA |
paul@31 | 20 | */ |
paul@31 | 21 | |
paul@31 | 22 | #include <ipc/cap_alloc.h> |
paul@31 | 23 | #include <ipc/mem_ipc.h> |
paul@117 | 24 | #include <ipc/irq.h> |
paul@117 | 25 | |
paul@117 | 26 | #include <l4/sys/irq.h> |
paul@117 | 27 | |
paul@117 | 28 | #include <pthread.h> |
paul@117 | 29 | #include <pthread-l4.h> |
paul@31 | 30 | |
paul@46 | 31 | #include <string.h> |
paul@46 | 32 | |
paul@31 | 33 | #include "dataspace_client.h" |
paul@55 | 34 | #include "file_client.h" |
paul@117 | 35 | #include "flush_client.h" |
paul@117 | 36 | #include "notification_client.h" |
paul@31 | 37 | #include "opener_client.h" |
paul@31 | 38 | #include "opener_context_client.h" |
paul@65 | 39 | #include "pipe_client.h" |
paul@65 | 40 | #include "pipe_opener_client.h" |
paul@31 | 41 | #include "mapped_file_client.h" |
paul@31 | 42 | |
paul@31 | 43 | #include "file.h" |
paul@31 | 44 | |
paul@31 | 45 | |
paul@31 | 46 | |
paul@90 | 47 | /* Update the extent of the file in a region using the region start and end |
paul@90 | 48 | positions and the file size. */ |
paul@90 | 49 | |
paul@90 | 50 | static void _update_extent(file_t *file) |
paul@90 | 51 | { |
paul@90 | 52 | /* Handle files ending after or within the region. */ |
paul@90 | 53 | |
paul@90 | 54 | if (file->size > file->start_pos) |
paul@90 | 55 | { |
paul@90 | 56 | if (file->size > file->end_pos) |
paul@90 | 57 | file->data_end = file->end_pos - file->start_pos; |
paul@90 | 58 | else |
paul@90 | 59 | file->data_end = file->size - file->start_pos; |
paul@90 | 60 | } |
paul@90 | 61 | |
paul@90 | 62 | /* Handle files ending before the region. */ |
paul@90 | 63 | |
paul@90 | 64 | else |
paul@90 | 65 | file->data_end = 0; |
paul@90 | 66 | } |
paul@90 | 67 | |
paul@90 | 68 | |
paul@90 | 69 | |
paul@90 | 70 | /* Initialise the given file structure. */ |
paul@90 | 71 | |
paul@90 | 72 | void file_init(file_t *file) |
paul@90 | 73 | { |
paul@90 | 74 | file->memory = NULL; |
paul@90 | 75 | file->ref = L4_INVALID_CAP; |
paul@117 | 76 | file->irq = L4_INVALID_CAP; |
paul@90 | 77 | file->start_pos = 0; |
paul@90 | 78 | file->end_pos = 0; |
paul@90 | 79 | file->data_end = 0; |
paul@90 | 80 | file->data_current = 0; |
paul@90 | 81 | file->can_mmap = 1; |
paul@108 | 82 | file->has_size = 1; |
paul@117 | 83 | file->can_block = 0; |
paul@90 | 84 | } |
paul@90 | 85 | |
paul@90 | 86 | |
paul@90 | 87 | |
paul@33 | 88 | /* Release resources for the given file. */ |
paul@31 | 89 | |
paul@31 | 90 | void file_close(file_t *file) |
paul@31 | 91 | { |
paul@31 | 92 | if (l4_is_valid_cap(file->ref)) |
paul@31 | 93 | ipc_cap_free_um(file->ref); |
paul@31 | 94 | |
paul@117 | 95 | if (l4_is_valid_cap(file->irq)) |
paul@117 | 96 | ipc_cap_free_um(file->irq); |
paul@117 | 97 | |
paul@31 | 98 | if (file->memory != NULL) |
paul@31 | 99 | ipc_detach_dataspace(file->memory); |
paul@31 | 100 | |
paul@31 | 101 | file_init(file); |
paul@31 | 102 | } |
paul@31 | 103 | |
paul@90 | 104 | /* Open a file using the given structure, indicating the filename and |
paul@90 | 105 | filesystem server. The file_mmap function should be used to obtain access to |
paul@90 | 106 | memory providing file data. This is a convenience function invoking |
paul@90 | 107 | file_context and file_context_open. */ |
paul@90 | 108 | |
paul@90 | 109 | long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) |
paul@90 | 110 | { |
paul@90 | 111 | file_t context; |
paul@90 | 112 | long err; |
paul@90 | 113 | |
paul@90 | 114 | err = file_context(&context, server); |
paul@90 | 115 | if (err) |
paul@90 | 116 | return err; |
paul@90 | 117 | |
paul@90 | 118 | if (!file_string_set(&context, filename, 0, NULL)) |
paul@90 | 119 | return -L4_ENOMEM; |
paul@90 | 120 | |
paul@90 | 121 | err = file_context_open(file, flags, &context); |
paul@90 | 122 | |
paul@90 | 123 | /* Close the context, although a separate mechanism could permit contexts to |
paul@90 | 124 | open several files. */ |
paul@90 | 125 | |
paul@90 | 126 | file_close(&context); |
paul@90 | 127 | return err; |
paul@90 | 128 | } |
paul@90 | 129 | |
paul@90 | 130 | |
paul@90 | 131 | |
paul@90 | 132 | /* Initialise a file structure for a context obtained from the given server |
paul@90 | 133 | attaching memory to communicate filename information. */ |
paul@31 | 134 | |
paul@33 | 135 | long file_context(file_t *file, l4_cap_idx_t server) |
paul@31 | 136 | { |
paul@85 | 137 | if (l4_is_invalid_cap(server)) |
paul@85 | 138 | return -L4_EINVAL; |
paul@85 | 139 | |
paul@31 | 140 | client_Opener opener(server); |
paul@92 | 141 | offset_t size; |
paul@92 | 142 | flags_t flags; |
paul@31 | 143 | long err; |
paul@31 | 144 | |
paul@31 | 145 | file_init(file); |
paul@47 | 146 | |
paul@31 | 147 | err = opener.context(&file->ref); |
paul@31 | 148 | if (err) |
paul@31 | 149 | return err; |
paul@31 | 150 | |
paul@31 | 151 | client_Dataspace context_ds(file->ref); |
paul@31 | 152 | |
paul@31 | 153 | err = context_ds.info(&size, &flags); |
paul@31 | 154 | if (err) |
paul@31 | 155 | return err; |
paul@31 | 156 | |
paul@47 | 157 | file->start_pos = 0; |
paul@47 | 158 | file->end_pos = size; |
paul@47 | 159 | |
paul@31 | 160 | return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); |
paul@31 | 161 | } |
paul@31 | 162 | |
paul@47 | 163 | /* Open a file using the given structure and context. */ |
paul@47 | 164 | |
paul@85 | 165 | long file_context_open(file_t *file, flags_t flags, file_t *context) |
paul@47 | 166 | { |
paul@47 | 167 | client_OpenerContext openercontext(context->ref); |
paul@47 | 168 | file_init(file); |
paul@85 | 169 | return openercontext.open(flags, &file->size, &file->ref); |
paul@47 | 170 | } |
paul@47 | 171 | |
paul@46 | 172 | |
paul@46 | 173 | |
paul@90 | 174 | /* Flush populated data and obtain an updated file size and populated data |
paul@90 | 175 | details. */ |
paul@33 | 176 | |
paul@90 | 177 | long file_flush(file_t *file) |
paul@33 | 178 | { |
paul@117 | 179 | client_Flush _file(file->ref); |
paul@90 | 180 | long err = _file.flush(file->data_current, &file->size); |
paul@33 | 181 | |
paul@33 | 182 | if (err) |
paul@33 | 183 | return err; |
paul@33 | 184 | |
paul@90 | 185 | _update_extent(file); |
paul@90 | 186 | |
paul@90 | 187 | return L4_EOK; |
paul@33 | 188 | } |
paul@33 | 189 | |
paul@90 | 190 | /* Map a region of the given file to a memory region, obtaining an updated file |
paul@90 | 191 | size and populated data details. Unmap any previously mapped region. */ |
paul@90 | 192 | |
paul@90 | 193 | long file_mmap(file_t *file, offset_t position, offset_t length) |
paul@90 | 194 | { |
paul@90 | 195 | char *memory = file->memory; |
paul@90 | 196 | client_MappedFile mapped_file(file->ref); |
paul@90 | 197 | long err = mapped_file.mmap(position, length, &file->start_pos, |
paul@90 | 198 | &file->end_pos, &file->size); |
paul@90 | 199 | |
paul@90 | 200 | if (err) |
paul@90 | 201 | return err; |
paul@90 | 202 | |
paul@90 | 203 | _update_extent(file); |
paul@90 | 204 | |
paul@90 | 205 | err = ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); |
paul@90 | 206 | if (err) |
paul@90 | 207 | return err; |
paul@90 | 208 | |
paul@90 | 209 | if (memory != NULL) |
paul@90 | 210 | ipc_detach_dataspace(memory); |
paul@90 | 211 | |
paul@90 | 212 | return L4_EOK; |
paul@90 | 213 | } |
paul@90 | 214 | |
paul@90 | 215 | /* Resize a file, obtaining updated file size and populated data details. */ |
paul@55 | 216 | |
paul@55 | 217 | long file_resize(file_t *file, offset_t size) |
paul@55 | 218 | { |
paul@108 | 219 | if (!file->has_size) |
paul@108 | 220 | return -L4_EIO; |
paul@108 | 221 | |
paul@55 | 222 | client_File _file(file->ref); |
paul@55 | 223 | offset_t file_size = size; |
paul@55 | 224 | long err = _file.resize(&file_size); |
paul@55 | 225 | |
paul@90 | 226 | if (err) |
paul@90 | 227 | return err; |
paul@55 | 228 | |
paul@90 | 229 | file->size = file_size; |
paul@90 | 230 | _update_extent(file); |
paul@90 | 231 | return L4_EOK; |
paul@55 | 232 | } |
paul@55 | 233 | |
paul@55 | 234 | |
paul@55 | 235 | |
paul@33 | 236 | /* Return the amount of data in the mapped region for the given file. */ |
paul@33 | 237 | |
paul@45 | 238 | offset_t file_populated_span(file_t *file) |
paul@33 | 239 | { |
paul@45 | 240 | offset_t size = file_span(file); |
paul@33 | 241 | return (file->data_end < size) ? file->data_end : size; |
paul@33 | 242 | } |
paul@33 | 243 | |
paul@45 | 244 | /* Return the size of the mapped region for the given file. */ |
paul@45 | 245 | |
paul@45 | 246 | offset_t file_span(file_t *file) |
paul@45 | 247 | { |
paul@45 | 248 | return file->end_pos - file->start_pos; |
paul@45 | 249 | } |
paul@45 | 250 | |
paul@46 | 251 | |
paul@46 | 252 | |
paul@46 | 253 | /* Get a pointer to any terminated string at the given offset or NULL if the |
paul@46 | 254 | data from offset is not terminated. */ |
paul@46 | 255 | |
paul@46 | 256 | char *file_string_get(file_t *file, offset_t offset) |
paul@46 | 257 | { |
paul@46 | 258 | offset_t limit = file_span(file) - offset; |
paul@46 | 259 | |
paul@46 | 260 | if (strnlen(file->memory + offset, limit) < limit) |
paul@46 | 261 | return file->memory + offset; |
paul@46 | 262 | else |
paul@46 | 263 | return NULL; |
paul@46 | 264 | } |
paul@46 | 265 | |
paul@47 | 266 | /* Copy a string to the mapped region at the given offset, returning 1 (true) |
paul@47 | 267 | where all characters were copied, 0 (false) otherwise. The precise number of |
paul@47 | 268 | characters copied, excluding the zero terminator is provided via the written |
paul@47 | 269 | parameter if it is not specified as NULL. */ |
paul@46 | 270 | |
paul@47 | 271 | int file_string_set(file_t *file, const char *data, offset_t offset, |
paul@47 | 272 | offset_t *written) |
paul@46 | 273 | { |
paul@47 | 274 | offset_t i, pos, limit = file_span(file); |
paul@46 | 275 | |
paul@46 | 276 | /* Do not attempt to copy data with an invalid offset. */ |
paul@46 | 277 | |
paul@47 | 278 | if (offset >= limit) |
paul@47 | 279 | { |
paul@47 | 280 | if (written != NULL) |
paul@47 | 281 | *written = 0; |
paul@46 | 282 | return 0; |
paul@47 | 283 | } |
paul@46 | 284 | |
paul@47 | 285 | /* Copy the data to the given offset, stopping at the end of the region. */ |
paul@46 | 286 | |
paul@46 | 287 | for (i = 0, pos = offset; pos < limit; i++, pos++) |
paul@46 | 288 | { |
paul@46 | 289 | file->memory[pos] = data[i]; |
paul@46 | 290 | |
paul@46 | 291 | /* Terminator written, can return immediately. */ |
paul@46 | 292 | |
paul@46 | 293 | if (!data[i]) |
paul@47 | 294 | { |
paul@47 | 295 | if (written != NULL) |
paul@47 | 296 | *written = pos - offset; |
paul@47 | 297 | return 1; |
paul@47 | 298 | } |
paul@46 | 299 | } |
paul@46 | 300 | |
paul@46 | 301 | /* Terminate the incomplete string at the end of the region. */ |
paul@46 | 302 | |
paul@47 | 303 | file->memory[limit - 1] = '\0'; |
paul@47 | 304 | if (written != NULL) |
paul@47 | 305 | *written = limit - 1 - offset; |
paul@47 | 306 | return 0; |
paul@46 | 307 | } |
paul@46 | 308 | |
paul@65 | 309 | |
paul@65 | 310 | |
paul@90 | 311 | /* Return the number of remaining populated bytes in the region. */ |
paul@90 | 312 | |
paul@90 | 313 | offset_t file_data_available(file_t *file) |
paul@90 | 314 | { |
paul@90 | 315 | return file_populated_span(file) - file->data_current; |
paul@90 | 316 | } |
paul@90 | 317 | |
paul@90 | 318 | /* Return the current data offset in the region. */ |
paul@90 | 319 | |
paul@90 | 320 | char *file_data_current(file_t *file) |
paul@90 | 321 | { |
paul@90 | 322 | return file->memory + file->data_current; |
paul@90 | 323 | } |
paul@90 | 324 | |
paul@90 | 325 | /* Return the current access position in the file. */ |
paul@90 | 326 | |
paul@90 | 327 | offset_t file_data_current_position(file_t *file) |
paul@90 | 328 | { |
paul@90 | 329 | return file->start_pos + file->data_current; |
paul@90 | 330 | } |
paul@90 | 331 | |
paul@90 | 332 | /* Return the position of the end of the populated bytes in the region. */ |
paul@90 | 333 | |
paul@90 | 334 | offset_t file_data_end_position(file_t *file) |
paul@90 | 335 | { |
paul@90 | 336 | return file->start_pos + file->data_end; |
paul@90 | 337 | } |
paul@90 | 338 | |
paul@90 | 339 | /* Return the amount of remaining space in the region. */ |
paul@90 | 340 | |
paul@90 | 341 | offset_t file_data_space(file_t *file) |
paul@90 | 342 | { |
paul@90 | 343 | return file_span(file) - file->data_current; |
paul@90 | 344 | } |
paul@90 | 345 | |
paul@90 | 346 | |
paul@90 | 347 | |
paul@90 | 348 | /* Copy data to the given buffer from the current data position, updating the |
paul@90 | 349 | position. */ |
paul@90 | 350 | |
paul@98 | 351 | void file_data_read(file_t *file, char *buf, offset_t to_transfer) |
paul@90 | 352 | { |
paul@90 | 353 | memcpy(buf, file_data_current(file), to_transfer); |
paul@90 | 354 | |
paul@90 | 355 | /* Update position details. */ |
paul@90 | 356 | |
paul@90 | 357 | file->data_current += to_transfer; |
paul@90 | 358 | } |
paul@90 | 359 | |
paul@90 | 360 | /* Copy data from the given buffer to the current data position, updating the |
paul@90 | 361 | position and the extent of populated data if this was exceeded. */ |
paul@90 | 362 | |
paul@98 | 363 | void file_data_write(file_t *file, char *buf, offset_t to_transfer) |
paul@90 | 364 | { |
paul@90 | 365 | memcpy(file_data_current(file), buf, to_transfer); |
paul@90 | 366 | |
paul@90 | 367 | /* Update position details. */ |
paul@90 | 368 | |
paul@90 | 369 | file->data_current += to_transfer; |
paul@90 | 370 | |
paul@90 | 371 | if (file->data_current > file->data_end) |
paul@90 | 372 | file->data_end = file->data_current; |
paul@90 | 373 | } |
paul@90 | 374 | |
paul@90 | 375 | |
paul@90 | 376 | |
paul@117 | 377 | /* Subscribe to notification events on a file. */ |
paul@117 | 378 | |
paul@121 | 379 | long file_notify_subscribe(file_t *file, notify_flags_t flags) |
paul@117 | 380 | { |
paul@117 | 381 | client_Notification notify(file->ref); |
paul@117 | 382 | |
paul@117 | 383 | return notify.subscribe(flags, &file->irq); |
paul@117 | 384 | } |
paul@117 | 385 | |
paul@117 | 386 | /* Unsubscribe from notification events on a file. */ |
paul@117 | 387 | |
paul@117 | 388 | long file_notify_unsubscribe(file_t *file) |
paul@117 | 389 | { |
paul@117 | 390 | client_Notification notify(file->ref); |
paul@117 | 391 | |
paul@117 | 392 | return notify.unsubscribe(); |
paul@117 | 393 | } |
paul@117 | 394 | |
paul@117 | 395 | /* Wait for notification events on a file. */ |
paul@117 | 396 | |
paul@117 | 397 | long file_notify_wait(file_t *file) |
paul@117 | 398 | { |
paul@117 | 399 | long err = ipc_bind_irq(file->irq, (l4_umword_t) &file->irq, pthread_l4_cap(pthread_self())); |
paul@117 | 400 | |
paul@117 | 401 | if (err) |
paul@117 | 402 | return err; |
paul@117 | 403 | |
paul@117 | 404 | l4_msgtag_t tag = l4_irq_receive(file->irq, L4_IPC_NEVER); |
paul@117 | 405 | |
paul@117 | 406 | l4_irq_detach(file->irq); |
paul@117 | 407 | return l4_error(tag); |
paul@117 | 408 | } |
paul@117 | 409 | |
paul@117 | 410 | |
paul@117 | 411 | |
paul@65 | 412 | /* Open two pipe endpoints using the given pipe server. */ |
paul@65 | 413 | |
paul@66 | 414 | long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) |
paul@65 | 415 | { |
paul@85 | 416 | if (l4_is_invalid_cap(server)) |
paul@85 | 417 | return -L4_EINVAL; |
paul@85 | 418 | |
paul@65 | 419 | client_PipeOpener opener(server); |
paul@65 | 420 | |
paul@65 | 421 | file_init(reader); |
paul@65 | 422 | file_init(writer); |
paul@65 | 423 | |
paul@90 | 424 | /* Pipes can usually only be accessed via region navigation. */ |
paul@90 | 425 | |
paul@90 | 426 | reader->can_mmap = 0; |
paul@108 | 427 | reader->has_size = 0; |
paul@90 | 428 | writer->can_mmap = 0; |
paul@108 | 429 | writer->has_size = 0; |
paul@90 | 430 | |
paul@66 | 431 | long err = opener.pipe(size, &reader->ref, &writer->ref); |
paul@65 | 432 | if (err) |
paul@65 | 433 | return err; |
paul@65 | 434 | |
paul@65 | 435 | err = pipe_next(writer) || pipe_next(reader); |
paul@65 | 436 | |
paul@65 | 437 | if (err) |
paul@65 | 438 | { |
paul@65 | 439 | file_close(reader); |
paul@65 | 440 | file_close(writer); |
paul@65 | 441 | } |
paul@65 | 442 | |
paul@65 | 443 | return err; |
paul@65 | 444 | } |
paul@65 | 445 | |
paul@65 | 446 | /* Access the current region for a pipe endpoint. */ |
paul@65 | 447 | |
paul@65 | 448 | long pipe_current(file_t *pipe) |
paul@65 | 449 | { |
paul@65 | 450 | client_Pipe _pipe(pipe->ref); |
paul@65 | 451 | long err = _pipe.current_region(&pipe->data_end, &pipe->size); |
paul@65 | 452 | |
paul@65 | 453 | if (err) |
paul@65 | 454 | return err; |
paul@65 | 455 | |
paul@65 | 456 | pipe->end_pos = pipe->size; |
paul@65 | 457 | |
paul@117 | 458 | /* Attach memory if necessary. */ |
paul@65 | 459 | |
paul@117 | 460 | if (pipe->memory == NULL) |
paul@117 | 461 | { |
paul@117 | 462 | err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); |
paul@117 | 463 | if (err) |
paul@117 | 464 | return err; |
paul@117 | 465 | } |
paul@65 | 466 | |
paul@65 | 467 | return L4_EOK; |
paul@65 | 468 | } |
paul@65 | 469 | |
paul@65 | 470 | /* Access the next region for a pipe endpoint, updating the eventual size of |
paul@65 | 471 | the current region. */ |
paul@65 | 472 | |
paul@65 | 473 | long pipe_next(file_t *pipe) |
paul@65 | 474 | { |
paul@65 | 475 | client_Pipe _pipe(pipe->ref); |
paul@65 | 476 | long err = _pipe.next_region(&pipe->data_end, &pipe->size); |
paul@65 | 477 | char *memory = pipe->memory; |
paul@65 | 478 | |
paul@65 | 479 | if (err) |
paul@65 | 480 | return err; |
paul@65 | 481 | |
paul@65 | 482 | pipe->end_pos = pipe->size; |
paul@65 | 483 | |
paul@65 | 484 | err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); |
paul@65 | 485 | if (err) |
paul@65 | 486 | return err; |
paul@65 | 487 | |
paul@65 | 488 | if (memory != NULL) |
paul@65 | 489 | ipc_detach_dataspace(memory); |
paul@65 | 490 | |
paul@65 | 491 | return L4_EOK; |
paul@65 | 492 | } |
paul@65 | 493 | |
paul@65 | 494 | /* Set the size of the written region. */ |
paul@65 | 495 | |
paul@65 | 496 | long pipe_written(file_t *pipe, offset_t size) |
paul@65 | 497 | { |
paul@65 | 498 | if (size <= pipe->size) |
paul@65 | 499 | { |
paul@65 | 500 | pipe->data_end = size; |
paul@65 | 501 | return L4_EOK; |
paul@65 | 502 | } |
paul@65 | 503 | else |
paul@65 | 504 | return -L4_EINVAL; |
paul@65 | 505 | } |
paul@65 | 506 | |
paul@31 | 507 | // vim: tabstop=2 expandtab shiftwidth=2 |