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