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