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 client_Notification notify(file->ref); 391 392 return notify.unsubscribe(); 393 } 394 395 /* Wait for notification events on a file. */ 396 397 long file_notify_wait(file_t *file) 398 { 399 long err = ipc_bind_irq(file->irq, (l4_umword_t) &file->irq, pthread_l4_cap(pthread_self())); 400 401 if (err) 402 return err; 403 404 l4_msgtag_t tag = l4_irq_receive(file->irq, L4_IPC_NEVER); 405 406 l4_irq_detach(file->irq); 407 return l4_error(tag); 408 } 409 410 411 412 /* Open two pipe endpoints using the given pipe server. */ 413 414 long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) 415 { 416 if (l4_is_invalid_cap(server)) 417 return -L4_EINVAL; 418 419 client_PipeOpener opener(server); 420 421 file_init(reader); 422 file_init(writer); 423 424 /* Pipes can usually only be accessed via region navigation. */ 425 426 reader->can_mmap = 0; 427 reader->has_size = 0; 428 writer->can_mmap = 0; 429 writer->has_size = 0; 430 431 long err = opener.pipe(size, &reader->ref, &writer->ref); 432 if (err) 433 return err; 434 435 err = pipe_next(writer) || pipe_next(reader); 436 437 if (err) 438 { 439 file_close(reader); 440 file_close(writer); 441 } 442 443 return err; 444 } 445 446 /* Access the current region for a pipe endpoint. */ 447 448 long pipe_current(file_t *pipe) 449 { 450 client_Pipe _pipe(pipe->ref); 451 long err = _pipe.current_region(&pipe->data_end, &pipe->size); 452 453 if (err) 454 return err; 455 456 pipe->end_pos = pipe->size; 457 458 /* Attach memory if necessary. */ 459 460 if (pipe->memory == NULL) 461 { 462 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 463 if (err) 464 return err; 465 } 466 467 return L4_EOK; 468 } 469 470 /* Access the next region for a pipe endpoint, updating the eventual size of 471 the current region. */ 472 473 long pipe_next(file_t *pipe) 474 { 475 client_Pipe _pipe(pipe->ref); 476 long err = _pipe.next_region(&pipe->data_end, &pipe->size); 477 char *memory = pipe->memory; 478 479 if (err) 480 return err; 481 482 pipe->end_pos = pipe->size; 483 484 err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); 485 if (err) 486 return err; 487 488 if (memory != NULL) 489 ipc_detach_dataspace(memory); 490 491 return L4_EOK; 492 } 493 494 /* Set the size of the written region. */ 495 496 long pipe_written(file_t *pipe, offset_t size) 497 { 498 if (size <= pipe->size) 499 { 500 pipe->data_end = size; 501 return L4_EOK; 502 } 503 else 504 return -L4_EINVAL; 505 } 506 507 // vim: tabstop=2 expandtab shiftwidth=2