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