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