paul@90 | 1 | /* |
paul@90 | 2 | * Filesystem client functions. |
paul@90 | 3 | * |
paul@607 | 4 | * Copyright (C) 2018-2024 Paul Boddie <paul@boddie.org.uk> |
paul@90 | 5 | * |
paul@90 | 6 | * This program is free software; you can redistribute it and/or |
paul@90 | 7 | * modify it under the terms of the GNU General Public License as |
paul@90 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@90 | 9 | * the License, or (at your option) any later version. |
paul@90 | 10 | * |
paul@90 | 11 | * This program is distributed in the hope that it will be useful, |
paul@90 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@90 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@90 | 14 | * GNU General Public License for more details. |
paul@90 | 15 | * |
paul@90 | 16 | * You should have received a copy of the GNU General Public License |
paul@90 | 17 | * along with this program; if not, write to the Free Software |
paul@90 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@90 | 19 | * Boston, MA 02110-1301, USA |
paul@90 | 20 | */ |
paul@90 | 21 | |
paul@90 | 22 | #include <l4/re/env.h> |
paul@90 | 23 | |
paul@90 | 24 | #include <stdio.h> |
paul@90 | 25 | #include <stdlib.h> |
paul@174 | 26 | #include <string.h> |
paul@90 | 27 | |
paul@521 | 28 | #include <mem/memory_utils.h> |
paul@507 | 29 | #include <systypes/env.h> |
paul@175 | 30 | #include <systypes/fcntl.h> |
paul@175 | 31 | |
paul@90 | 32 | #include "client.h" |
paul@90 | 33 | |
paul@90 | 34 | |
paul@90 | 35 | |
paul@90 | 36 | /* Default size of pipe regions. */ |
paul@90 | 37 | |
paul@646 | 38 | static const offset_t DEFAULT_PIPE_SIZE = 4096; |
paul@90 | 39 | |
paul@174 | 40 | /* Size of the core member region of a directory entry structure. */ |
paul@174 | 41 | |
paul@646 | 42 | static const offset_t DIRENT_CORE_SIZE = (sizeof(struct dirent) - sizeof(((struct dirent *) 0)->d_name)); |
paul@174 | 43 | |
paul@90 | 44 | |
paul@90 | 45 | |
paul@330 | 46 | /* Merging of region flags from protection and access flags. */ |
paul@330 | 47 | |
paul@634 | 48 | static rm_flags_t _combine_region_flags(rm_flags_t region_flags, flags_t flags) |
paul@330 | 49 | { |
paul@330 | 50 | return region_flags & (file_region_flags(flags) | L4RE_RM_F_X); |
paul@330 | 51 | } |
paul@330 | 52 | |
paul@330 | 53 | /* Conversion of protection and access flags to region flags. */ |
paul@330 | 54 | |
paul@634 | 55 | rm_flags_t client_region_flags(prot_t prot, flags_t flags) |
paul@330 | 56 | { |
paul@634 | 57 | rm_flags_t rm_flags = 0; |
paul@330 | 58 | |
paul@330 | 59 | if (prot & PROT_READ) |
paul@330 | 60 | rm_flags |= L4RE_RM_F_R; |
paul@330 | 61 | if (prot & PROT_WRITE) |
paul@330 | 62 | rm_flags |= L4RE_RM_F_W; |
paul@330 | 63 | if (prot & PROT_EXEC) |
paul@330 | 64 | rm_flags |= L4RE_RM_F_X; |
paul@330 | 65 | |
paul@330 | 66 | return _combine_region_flags(rm_flags, flags); |
paul@330 | 67 | } |
paul@330 | 68 | |
paul@330 | 69 | |
paul@330 | 70 | |
paul@169 | 71 | /* Access the given position and synchronise state with the file object. Pipe |
paul@169 | 72 | objects may return busy conditions indicating that the desired access cannot |
paul@169 | 73 | yet be fulfilled. */ |
paul@117 | 74 | |
paul@117 | 75 | static long _access(file_t *file, offset_t position) |
paul@117 | 76 | { |
paul@117 | 77 | long err; |
paul@117 | 78 | |
paul@171 | 79 | if (file->object_flags & OBJECT_SUPPORTS_MMAP) |
paul@117 | 80 | { |
paul@521 | 81 | offset_t adjusted_position = position; |
paul@521 | 82 | |
paul@117 | 83 | /* Where the position is outside the current region, re-map. */ |
paul@117 | 84 | |
paul@117 | 85 | if ((position < file->start_pos) || (position >= file->end_pos)) |
paul@117 | 86 | { |
paul@521 | 87 | offset_t length = file_span(file); |
paul@521 | 88 | |
paul@521 | 89 | if (!length) |
paul@521 | 90 | length = PAGE_SIZE; |
paul@521 | 91 | |
paul@521 | 92 | /* Avoid growth of the mapped region when the end of the region is |
paul@521 | 93 | calculated to be position + span, which is then rounded up, whereas the |
paul@521 | 94 | start of the region is rounded down. */ |
paul@521 | 95 | |
paul@521 | 96 | adjusted_position = trunc(position, PAGE_SIZE); |
paul@521 | 97 | |
paul@521 | 98 | if (file_mmap(file, adjusted_position, length, 0, 0, |
paul@330 | 99 | file_region_flags(file->flags))) |
paul@117 | 100 | return -L4_EIO; |
paul@117 | 101 | } |
paul@117 | 102 | |
paul@117 | 103 | /* Otherwise, flush any written data in the current region and update the |
paul@117 | 104 | file size details. */ |
paul@117 | 105 | |
paul@117 | 106 | else |
paul@117 | 107 | { |
paul@117 | 108 | err = client_flush(file); |
paul@117 | 109 | if (err) |
paul@117 | 110 | return err; |
paul@117 | 111 | } |
paul@165 | 112 | |
paul@165 | 113 | /* Update the current data offset. */ |
paul@165 | 114 | |
paul@165 | 115 | file->data_current = position - file->start_pos; |
paul@165 | 116 | |
paul@165 | 117 | return L4_EOK; |
paul@117 | 118 | } |
paul@117 | 119 | else |
paul@117 | 120 | { |
paul@643 | 121 | bool initial = (file->memory == NULL); |
paul@643 | 122 | |
paul@192 | 123 | /* Handle the initial condition with no current region. */ |
paul@192 | 124 | |
paul@643 | 125 | if (initial) |
paul@192 | 126 | { |
paul@192 | 127 | err = client_current_region(file); |
paul@192 | 128 | if (err) |
paul@192 | 129 | return err; |
paul@192 | 130 | } |
paul@192 | 131 | |
paul@117 | 132 | /* Strict conditions for region navigation in pipes. */ |
paul@117 | 133 | |
paul@117 | 134 | if ((position < file->start_pos) || (position > file->end_pos)) |
paul@117 | 135 | { |
paul@117 | 136 | return -L4_EIO; |
paul@117 | 137 | } |
paul@117 | 138 | |
paul@117 | 139 | /* The next region is only available at the end of the mapped memory. */ |
paul@117 | 140 | |
paul@117 | 141 | else if (position == file->end_pos) |
paul@117 | 142 | { |
paul@663 | 143 | return client_next_region(file); |
paul@117 | 144 | } |
paul@117 | 145 | |
paul@117 | 146 | /* Within the current pipe region, synchronise with the pipe object. */ |
paul@117 | 147 | |
paul@117 | 148 | else |
paul@402 | 149 | { |
paul@643 | 150 | if (initial) |
paul@643 | 151 | return L4_EOK; |
paul@643 | 152 | else |
paul@643 | 153 | return client_current_region(file); |
paul@402 | 154 | } |
paul@117 | 155 | } |
paul@117 | 156 | } |
paul@117 | 157 | |
paul@117 | 158 | |
paul@117 | 159 | |
paul@162 | 160 | /* Return whether an operation on file should block for more content or more |
paul@169 | 161 | space. A file must be configured for blocking, not be closed, and must either |
paul@169 | 162 | be lacking content (if reading) or space (if writing). */ |
paul@162 | 163 | |
paul@162 | 164 | static int _operation_blocking(file_t *file, int reading) |
paul@162 | 165 | { |
paul@479 | 166 | return (file->can_block && !(file->notifiable.notifications & NOTIFY_PEER_CLOSED) && ( |
paul@402 | 167 | (reading && !file_data_available(file)) || |
paul@402 | 168 | (!reading && !file_data_space(file)))); |
paul@162 | 169 | } |
paul@162 | 170 | |
paul@162 | 171 | |
paul@162 | 172 | |
paul@117 | 173 | /* Return whether an access could occur, blocking if necessary. */ |
paul@117 | 174 | |
paul@162 | 175 | static int _access_blocking(file_t *file, offset_t position, int reading) |
paul@117 | 176 | { |
paul@117 | 177 | long err; |
paul@117 | 178 | |
paul@162 | 179 | /* Attempt to access the position, handling an error condition or a blocking |
paul@162 | 180 | condition. */ |
paul@162 | 181 | |
paul@162 | 182 | while ((err = _access(file, position)) || _operation_blocking(file, reading)) |
paul@117 | 183 | { |
paul@165 | 184 | position = file->data_current; |
paul@165 | 185 | |
paul@117 | 186 | /* Exit if blocking is not configured or suitable. */ |
paul@117 | 187 | |
paul@162 | 188 | if ((err && (err != -L4_EBUSY)) || !file->can_block) |
paul@117 | 189 | return 0; |
paul@117 | 190 | |
paul@117 | 191 | /* Handle an inability to access by blocking, exiting if waiting failed. */ |
paul@117 | 192 | |
paul@180 | 193 | if (client_wait_file(file, client_notifier_task())) |
paul@117 | 194 | return 0; |
paul@117 | 195 | } |
paul@117 | 196 | |
paul@117 | 197 | return 1; |
paul@117 | 198 | } |
paul@117 | 199 | |
paul@117 | 200 | |
paul@117 | 201 | |
paul@117 | 202 | /* Ensure that memory is mapped for accessing the given file, using the |
paul@117 | 203 | indicated count as a region size hint. */ |
paul@117 | 204 | |
paul@117 | 205 | static void *_map_memory(file_t *file, offset_t count) |
paul@117 | 206 | { |
paul@117 | 207 | if (file->memory == NULL) |
paul@117 | 208 | { |
paul@171 | 209 | if (file->object_flags & OBJECT_SUPPORTS_MMAP) |
paul@339 | 210 | return client_mmap(file, client_tell(file), count, 0, 0, |
paul@330 | 211 | file_region_flags(file->flags)); |
paul@675 | 212 | else if (pipe_current(file)) |
paul@117 | 213 | return NULL; |
paul@117 | 214 | } |
paul@117 | 215 | |
paul@117 | 216 | return file->memory; |
paul@117 | 217 | } |
paul@117 | 218 | |
paul@117 | 219 | |
paul@117 | 220 | |
paul@144 | 221 | /* Open a file opening object. */ |
paul@144 | 222 | |
paul@150 | 223 | l4_cap_idx_t client_open_for_user(user_t user) |
paul@144 | 224 | { |
paul@507 | 225 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@144 | 226 | |
paul@150 | 227 | return client_open_for_user_using(user, server); |
paul@144 | 228 | } |
paul@144 | 229 | |
paul@144 | 230 | /* Open a file opening object via a named capability. */ |
paul@144 | 231 | |
paul@150 | 232 | l4_cap_idx_t client_open_for_user_using(user_t user, l4_cap_idx_t server) |
paul@144 | 233 | { |
paul@144 | 234 | if (l4_is_invalid_cap(server)) |
paul@144 | 235 | return L4_INVALID_CAP; |
paul@144 | 236 | |
paul@144 | 237 | l4_cap_idx_t opener; |
paul@150 | 238 | long err = file_open_for_user(user, server, &opener); |
paul@144 | 239 | |
paul@144 | 240 | if (err) |
paul@144 | 241 | return L4_INVALID_CAP; |
paul@144 | 242 | |
paul@144 | 243 | return opener; |
paul@144 | 244 | } |
paul@144 | 245 | |
paul@144 | 246 | |
paul@144 | 247 | |
paul@90 | 248 | /* Close a filesystem object. */ |
paul@90 | 249 | |
paul@90 | 250 | void client_close(file_t *file) |
paul@90 | 251 | { |
paul@90 | 252 | if (file == NULL) |
paul@90 | 253 | return; |
paul@90 | 254 | |
paul@658 | 255 | if (file->metadata != NULL) |
paul@658 | 256 | client_close(file->metadata); |
paul@658 | 257 | |
paul@159 | 258 | file_flush(file); |
paul@90 | 259 | file_close(file); |
paul@90 | 260 | free(file); |
paul@90 | 261 | } |
paul@90 | 262 | |
paul@90 | 263 | |
paul@90 | 264 | |
paul@584 | 265 | /* Obtain a stream from the environment. */ |
paul@584 | 266 | |
paul@584 | 267 | file_t *client_get_stream(const char *name, flags_t flags) |
paul@584 | 268 | { |
paul@613 | 269 | l4_cap_idx_t ref = l4re_env_get_cap(name); |
paul@613 | 270 | file_t *stream; |
paul@613 | 271 | |
paul@613 | 272 | if (l4_is_invalid_cap(ref)) |
paul@613 | 273 | return NULL; |
paul@613 | 274 | |
paul@613 | 275 | stream = (file_t *) malloc(sizeof(file_t)); |
paul@584 | 276 | |
paul@584 | 277 | file_init(stream); |
paul@584 | 278 | stream->flags = flags; |
paul@613 | 279 | stream->ref = ref; |
paul@584 | 280 | |
paul@675 | 281 | /* Restore the state of the stream. */ |
paul@614 | 282 | |
paul@673 | 283 | client_sync_stream(stream); |
paul@614 | 284 | |
paul@584 | 285 | /* Enforce blocking if necessary. |
paul@584 | 286 | NOTE: Ignoring any event subscription error. */ |
paul@584 | 287 | |
paul@584 | 288 | if (!(flags & O_NONBLOCK)) |
paul@584 | 289 | { |
paul@584 | 290 | notify_flags_t nflags = 0; |
paul@584 | 291 | |
paul@584 | 292 | if ((flags & O_WRONLY) || (flags & O_RDWR)) |
paul@584 | 293 | nflags |= NOTIFY_SPACE_AVAILABLE; |
paul@584 | 294 | |
paul@584 | 295 | if ((flags & O_RDONLY) || (flags & O_RDWR)) |
paul@584 | 296 | nflags |= NOTIFY_CONTENT_AVAILABLE; |
paul@584 | 297 | |
paul@584 | 298 | client_set_blocking(stream, nflags | NOTIFY_PEER_CLOSED); |
paul@584 | 299 | } |
paul@584 | 300 | |
paul@584 | 301 | return stream; |
paul@584 | 302 | } |
paul@584 | 303 | |
paul@665 | 304 | /* Initialise the stream data position to the end of any existing data for an |
paul@665 | 305 | output stream. For an input stream, initialise the position to the end of |
paul@665 | 306 | consumed data, preserving any unconsumed data. */ |
paul@663 | 307 | |
paul@663 | 308 | long client_sync_stream(file_t *file) |
paul@663 | 309 | { |
paul@665 | 310 | if (!client_opened(file)) |
paul@665 | 311 | return -L4_EINVAL; |
paul@663 | 312 | |
paul@673 | 313 | return file_refresh(file); |
paul@663 | 314 | } |
paul@663 | 315 | |
paul@584 | 316 | |
paul@584 | 317 | |
paul@90 | 318 | /* Open a filesystem object. */ |
paul@90 | 319 | |
paul@90 | 320 | file_t *client_open(const char *name, flags_t flags) |
paul@90 | 321 | { |
paul@507 | 322 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@144 | 323 | |
paul@144 | 324 | return client_open_using(name, flags, server); |
paul@103 | 325 | } |
paul@103 | 326 | |
paul@103 | 327 | /* Open a filesystem object via a named capability. */ |
paul@103 | 328 | |
paul@144 | 329 | file_t *client_open_using(const char *name, flags_t flags, l4_cap_idx_t server) |
paul@103 | 330 | { |
paul@144 | 331 | if (l4_is_invalid_cap(server)) |
paul@144 | 332 | return NULL; |
paul@144 | 333 | |
paul@90 | 334 | file_t *file = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 335 | |
paul@90 | 336 | if (file == NULL) |
paul@90 | 337 | return NULL; |
paul@90 | 338 | |
paul@402 | 339 | /* Return any allocated structure even if an error occurs. */ |
paul@90 | 340 | |
paul@402 | 341 | file->error = file_open(file, name, flags, server); |
paul@90 | 342 | return file; |
paul@90 | 343 | } |
paul@90 | 344 | |
paul@90 | 345 | |
paul@90 | 346 | |
paul@204 | 347 | /* Open a directory listing stream via the given named directory. */ |
paul@204 | 348 | |
paul@204 | 349 | file_t *client_opendir(const char *name) |
paul@204 | 350 | { |
paul@507 | 351 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@204 | 352 | |
paul@204 | 353 | return client_opendir_using(name, server); |
paul@204 | 354 | } |
paul@204 | 355 | |
paul@204 | 356 | /* Open a directory listing stream via the given named directory and a named |
paul@658 | 357 | capability, returning the directory object with a metadata file object |
paul@658 | 358 | reference to the directory listing stream. */ |
paul@204 | 359 | |
paul@204 | 360 | file_t *client_opendir_using(const char *name, l4_cap_idx_t server) |
paul@204 | 361 | { |
paul@204 | 362 | file_t *file = client_open_using(name, O_DIRECTORY, server); |
paul@204 | 363 | |
paul@437 | 364 | /* Return the directory structure itself for error handling. */ |
paul@437 | 365 | |
paul@437 | 366 | if (!client_opened(file)) |
paul@437 | 367 | return file; |
paul@437 | 368 | |
paul@658 | 369 | file->metadata = client_opendir_reader(file); |
paul@204 | 370 | |
paul@658 | 371 | /* Return the directory even if an error occurs. */ |
paul@204 | 372 | |
paul@658 | 373 | return file; |
paul@204 | 374 | } |
paul@204 | 375 | |
paul@204 | 376 | |
paul@204 | 377 | |
paul@202 | 378 | /* Open a directory listing stream via the given directory. */ |
paul@175 | 379 | |
paul@658 | 380 | file_t *client_opendir_reader(file_t *file) |
paul@175 | 381 | { |
paul@202 | 382 | file_t *reader = (file_t *) malloc(sizeof(file_t)); |
paul@175 | 383 | |
paul@202 | 384 | if (reader == NULL) |
paul@202 | 385 | return NULL; |
paul@175 | 386 | |
paul@402 | 387 | /* Return any allocated structure even if an error occurs. */ |
paul@202 | 388 | |
paul@402 | 389 | reader->error = directory_opendir(file, reader); |
paul@175 | 390 | |
paul@175 | 391 | /* Set blocking read mode to be able to conveniently read directory entries |
paul@402 | 392 | from the stream. If this fails, the error is set on the structure, but the |
paul@402 | 393 | stream will be open. */ |
paul@175 | 394 | |
paul@402 | 395 | if (!reader->error) |
paul@402 | 396 | reader->error = client_set_blocking(reader, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@175 | 397 | |
paul@202 | 398 | return reader; |
paul@175 | 399 | } |
paul@175 | 400 | |
paul@175 | 401 | |
paul@175 | 402 | |
paul@475 | 403 | /* Open another instance of an opened filesystem object. */ |
paul@475 | 404 | |
paul@475 | 405 | file_t *client_reopen(file_t *file, flags_t flags) |
paul@475 | 406 | { |
paul@475 | 407 | if (file == NULL) |
paul@475 | 408 | return NULL; |
paul@475 | 409 | |
paul@475 | 410 | file_t *new_file = (file_t *) malloc(sizeof(file_t)); |
paul@475 | 411 | |
paul@475 | 412 | if (new_file == NULL) |
paul@475 | 413 | return NULL; |
paul@475 | 414 | |
paul@475 | 415 | /* Return any allocated structure even if an error occurs. */ |
paul@475 | 416 | |
paul@475 | 417 | new_file->error = file_reopen(file, new_file, flags); |
paul@475 | 418 | return new_file; |
paul@475 | 419 | } |
paul@475 | 420 | |
paul@475 | 421 | |
paul@475 | 422 | |
paul@402 | 423 | /* Open a pipe object, returning any error condition. */ |
paul@90 | 424 | |
paul@178 | 425 | long client_pipe(file_t **reader, file_t **writer, flags_t flags) |
paul@90 | 426 | { |
paul@507 | 427 | l4_cap_idx_t server = l4re_env_get_cap(ENV_PIPE_SERVER_NAME); |
paul@144 | 428 | |
paul@178 | 429 | return client_pipe_using(reader, writer, flags, server); |
paul@135 | 430 | } |
paul@135 | 431 | |
paul@178 | 432 | long client_pipe_using(file_t **reader, file_t **writer, flags_t flags, l4_cap_idx_t server) |
paul@135 | 433 | { |
paul@402 | 434 | *reader = NULL; |
paul@402 | 435 | *writer = NULL; |
paul@402 | 436 | |
paul@144 | 437 | if (l4_is_invalid_cap(server)) |
paul@144 | 438 | return -L4_EINVAL; |
paul@144 | 439 | |
paul@90 | 440 | *reader = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 441 | |
paul@90 | 442 | if (*reader == NULL) |
paul@90 | 443 | return -L4_ENOMEM; |
paul@90 | 444 | |
paul@90 | 445 | *writer = (file_t *) malloc(sizeof(file_t)); |
paul@90 | 446 | |
paul@90 | 447 | if (*writer == NULL) |
paul@90 | 448 | { |
paul@90 | 449 | free(*reader); |
paul@90 | 450 | return -L4_ENOMEM; |
paul@90 | 451 | } |
paul@90 | 452 | |
paul@90 | 453 | long err = pipe_open(DEFAULT_PIPE_SIZE, *reader, *writer, server); |
paul@90 | 454 | |
paul@178 | 455 | /* Set blocking if successful and non-blocking is not indicated. */ |
paul@178 | 456 | |
paul@178 | 457 | if (!err && !(flags & O_NONBLOCK)) |
paul@178 | 458 | { |
paul@178 | 459 | err = client_set_blocking(*reader, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@178 | 460 | if (!err) |
paul@178 | 461 | err = client_set_blocking(*writer, NOTIFY_SPACE_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@178 | 462 | } |
paul@178 | 463 | |
paul@90 | 464 | if (err) |
paul@90 | 465 | { |
paul@90 | 466 | free(*reader); |
paul@90 | 467 | free(*writer); |
paul@90 | 468 | } |
paul@90 | 469 | |
paul@90 | 470 | return err; |
paul@90 | 471 | } |
paul@90 | 472 | |
paul@90 | 473 | |
paul@90 | 474 | |
paul@402 | 475 | /* Determine whether a file has been successfully opened. */ |
paul@402 | 476 | |
paul@402 | 477 | int client_opened(file_t *file) |
paul@402 | 478 | { |
paul@402 | 479 | return (file != NULL) && !file->error; |
paul@402 | 480 | } |
paul@402 | 481 | |
paul@402 | 482 | |
paul@402 | 483 | |
paul@271 | 484 | /* Make a directory in the filesystem. */ |
paul@271 | 485 | |
paul@271 | 486 | long client_mkdir(const char *path, mode_t mode) |
paul@271 | 487 | { |
paul@507 | 488 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@271 | 489 | |
paul@271 | 490 | return client_mkdir_using(path, mode, server); |
paul@271 | 491 | } |
paul@271 | 492 | |
paul@271 | 493 | /* Make a directory in the filesystem via a named capability. */ |
paul@271 | 494 | |
paul@271 | 495 | long client_mkdir_using(const char *path, mode_t mode, l4_cap_idx_t server) |
paul@271 | 496 | { |
paul@271 | 497 | return file_mkdir(path, mode, server); |
paul@271 | 498 | } |
paul@271 | 499 | |
paul@271 | 500 | |
paul@271 | 501 | |
paul@232 | 502 | /* Remove a file from the filesystem. */ |
paul@232 | 503 | |
paul@266 | 504 | long client_remove(const char *path) |
paul@232 | 505 | { |
paul@507 | 506 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@232 | 507 | |
paul@266 | 508 | return client_remove_using(path, server); |
paul@232 | 509 | } |
paul@232 | 510 | |
paul@232 | 511 | /* Remove a file from the filesystem via a named capability. */ |
paul@232 | 512 | |
paul@266 | 513 | long client_remove_using(const char *path, l4_cap_idx_t server) |
paul@232 | 514 | { |
paul@266 | 515 | return file_remove(path, server); |
paul@232 | 516 | } |
paul@232 | 517 | |
paul@232 | 518 | |
paul@232 | 519 | |
paul@236 | 520 | /* Rename a file in the filesystem. */ |
paul@236 | 521 | |
paul@236 | 522 | long client_rename(const char *source, const char *target) |
paul@236 | 523 | { |
paul@507 | 524 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@236 | 525 | |
paul@236 | 526 | return client_rename_using(source, target, server); |
paul@236 | 527 | } |
paul@236 | 528 | |
paul@236 | 529 | /* Rename a file in the filesystem via a named capability. */ |
paul@236 | 530 | |
paul@236 | 531 | long client_rename_using(const char *source, const char *target, l4_cap_idx_t server) |
paul@236 | 532 | { |
paul@236 | 533 | return file_rename(source, target, server); |
paul@236 | 534 | } |
paul@236 | 535 | |
paul@236 | 536 | |
paul@236 | 537 | |
paul@266 | 538 | /* Obtain filesystem object statistics. */ |
paul@266 | 539 | |
paul@266 | 540 | long client_stat(const char *path, struct stat *st) |
paul@266 | 541 | { |
paul@507 | 542 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@266 | 543 | |
paul@266 | 544 | return client_stat_using(path, st, server); |
paul@266 | 545 | } |
paul@266 | 546 | |
paul@266 | 547 | /* Obtain object statistics from the filesystem via a named capability. */ |
paul@266 | 548 | |
paul@266 | 549 | long client_stat_using(const char *path, struct stat *st, l4_cap_idx_t server) |
paul@266 | 550 | { |
paul@266 | 551 | return file_stat(path, st, server); |
paul@266 | 552 | } |
paul@266 | 553 | |
paul@266 | 554 | |
paul@266 | 555 | |
paul@665 | 556 | /* Obtain the current region of a pipe, updating the extent of populated data |
paul@665 | 557 | and only changing the current position if no populated data exists. */ |
paul@174 | 558 | |
paul@174 | 559 | long client_current_region(file_t *file) |
paul@174 | 560 | { |
paul@402 | 561 | if (!client_opened(file)) |
paul@174 | 562 | return -L4_EINVAL; |
paul@174 | 563 | |
paul@675 | 564 | return pipe_current(file); |
paul@174 | 565 | } |
paul@174 | 566 | |
paul@174 | 567 | |
paul@174 | 568 | |
paul@90 | 569 | /* Flush data explicitly to the filesystem object. */ |
paul@90 | 570 | |
paul@90 | 571 | long client_flush(file_t *file) |
paul@90 | 572 | { |
paul@402 | 573 | if (!client_opened(file)) |
paul@90 | 574 | return -L4_EINVAL; |
paul@90 | 575 | |
paul@90 | 576 | /* Flush and retain most buffer settings. */ |
paul@90 | 577 | |
paul@90 | 578 | return file_flush(file); |
paul@90 | 579 | } |
paul@90 | 580 | |
paul@90 | 581 | |
paul@90 | 582 | |
paul@90 | 583 | /* Map a memory region to a file. */ |
paul@90 | 584 | |
paul@339 | 585 | void *client_mmap(file_t *file, offset_t position, offset_t length, |
paul@339 | 586 | offset_t start_visible, offset_t end_visible, |
paul@634 | 587 | rm_flags_t region_flags) |
paul@90 | 588 | { |
paul@402 | 589 | if (!client_opened(file) || file_mmap(file, position, length, start_visible, |
paul@402 | 590 | end_visible, region_flags)) |
paul@90 | 591 | return NULL; |
paul@90 | 592 | |
paul@90 | 593 | return file->memory; |
paul@90 | 594 | } |
paul@90 | 595 | |
paul@90 | 596 | |
paul@90 | 597 | |
paul@90 | 598 | /* Obtain the next region of a pipe. */ |
paul@90 | 599 | |
paul@116 | 600 | long client_next_region(file_t *file) |
paul@90 | 601 | { |
paul@402 | 602 | if (!client_opened(file)) |
paul@116 | 603 | return -L4_EINVAL; |
paul@90 | 604 | |
paul@665 | 605 | return pipe_next(file); |
paul@90 | 606 | } |
paul@90 | 607 | |
paul@90 | 608 | |
paul@90 | 609 | |
paul@180 | 610 | /* Close a notifier object. */ |
paul@180 | 611 | |
paul@575 | 612 | void client_notifier_close(notifier_t *notifier) |
paul@180 | 613 | { |
paul@575 | 614 | notify_close(notifier); |
paul@180 | 615 | } |
paul@180 | 616 | |
paul@180 | 617 | /* Obtain a local notifier object. */ |
paul@180 | 618 | |
paul@575 | 619 | notifier_t *client_notifier_local() |
paul@180 | 620 | { |
paul@575 | 621 | return notify_get_local(); |
paul@180 | 622 | } |
paul@180 | 623 | |
paul@180 | 624 | /* Obtain a task-wide notifier object. */ |
paul@180 | 625 | |
paul@575 | 626 | notifier_t *client_notifier_task() |
paul@180 | 627 | { |
paul@575 | 628 | return notify_get_task(); |
paul@180 | 629 | } |
paul@180 | 630 | |
paul@180 | 631 | |
paul@180 | 632 | |
paul@174 | 633 | /* Read a directory entry. This must be freed by the caller after use. */ |
paul@174 | 634 | |
paul@174 | 635 | struct dirent *client_readdir(file_t *file) |
paul@174 | 636 | { |
paul@658 | 637 | if ((file == NULL) || (file->metadata == NULL)) |
paul@658 | 638 | return NULL; |
paul@658 | 639 | |
paul@174 | 640 | char buffer[DIRENT_CORE_SIZE]; |
paul@658 | 641 | offset_t nread = client_read(file->metadata, buffer, DIRENT_CORE_SIZE); |
paul@174 | 642 | |
paul@174 | 643 | /* Stop if no new structure can be successfully read. */ |
paul@174 | 644 | |
paul@174 | 645 | if (nread != DIRENT_CORE_SIZE) |
paul@174 | 646 | return NULL; |
paul@174 | 647 | |
paul@174 | 648 | struct dirent *dirent = (struct dirent *) buffer; |
paul@174 | 649 | offset_t remaining = dirent->d_reclen - DIRENT_CORE_SIZE; |
paul@174 | 650 | |
paul@174 | 651 | /* Allocate a buffer for the complete structure. */ |
paul@174 | 652 | |
paul@174 | 653 | char *entry = (char *) calloc(DIRENT_CORE_SIZE + remaining, sizeof(char)); |
paul@174 | 654 | |
paul@174 | 655 | if (entry == NULL) |
paul@174 | 656 | return NULL; |
paul@174 | 657 | |
paul@174 | 658 | /* Copy the start of the entry into a new buffer. */ |
paul@174 | 659 | |
paul@174 | 660 | memcpy(entry, buffer, DIRENT_CORE_SIZE); |
paul@174 | 661 | |
paul@174 | 662 | /* Append to the entry buffer. */ |
paul@174 | 663 | |
paul@174 | 664 | char *current = entry + DIRENT_CORE_SIZE; |
paul@174 | 665 | |
paul@658 | 666 | nread = client_read(file->metadata, current, remaining); |
paul@174 | 667 | |
paul@174 | 668 | /* Stop if no complete structure can be successfully read. */ |
paul@174 | 669 | |
paul@174 | 670 | if (nread != remaining) |
paul@174 | 671 | { |
paul@174 | 672 | free(entry); |
paul@174 | 673 | return NULL; |
paul@174 | 674 | } |
paul@174 | 675 | |
paul@174 | 676 | return (struct dirent *) entry; |
paul@174 | 677 | } |
paul@174 | 678 | |
paul@658 | 679 | /* Rewind directory reading. */ |
paul@658 | 680 | |
paul@658 | 681 | void client_rewinddir(file_t *file) |
paul@658 | 682 | { |
paul@658 | 683 | if ((file == NULL) || (file->metadata == NULL)) |
paul@658 | 684 | return; |
paul@658 | 685 | |
paul@658 | 686 | client_close(file->metadata); |
paul@658 | 687 | |
paul@658 | 688 | file->metadata = client_opendir_reader(file); |
paul@658 | 689 | } |
paul@658 | 690 | |
paul@174 | 691 | |
paul@174 | 692 | |
paul@90 | 693 | /* Read from the filesystem object into the buffer provided. */ |
paul@90 | 694 | |
paul@90 | 695 | offset_t client_read(file_t *file, void *buf, offset_t count) |
paul@90 | 696 | { |
paul@402 | 697 | if (!client_opened(file)) |
paul@90 | 698 | return 0; |
paul@90 | 699 | |
paul@100 | 700 | /* Map memory if none has been mapped so far. */ |
paul@100 | 701 | |
paul@108 | 702 | if (_map_memory(file, count) == NULL) |
paul@100 | 703 | return 0; |
paul@100 | 704 | |
paul@90 | 705 | /* Amount available in the descriptor buffer already. */ |
paul@90 | 706 | |
paul@90 | 707 | offset_t available = file_data_available(file); |
paul@90 | 708 | offset_t to_transfer, total = 0; |
paul@90 | 709 | |
paul@90 | 710 | while (count > 0) |
paul@90 | 711 | { |
paul@90 | 712 | /* If there is no data, try and obtain more data. */ |
paul@90 | 713 | |
paul@90 | 714 | if (!available) |
paul@90 | 715 | { |
paul@90 | 716 | /* Flush any unwritten data, preparing to read from the file position at |
paul@90 | 717 | the end of the data, and returning if no new data is available. */ |
paul@90 | 718 | |
paul@162 | 719 | if (!_access_blocking(file, file_data_end_position(file), 1)) |
paul@90 | 720 | break; |
paul@90 | 721 | |
paul@90 | 722 | available = file_data_available(file); |
paul@90 | 723 | |
paul@90 | 724 | if (!available) |
paul@90 | 725 | break; |
paul@90 | 726 | } |
paul@90 | 727 | |
paul@90 | 728 | /* Transfer data into the supplied buffer. */ |
paul@90 | 729 | |
paul@90 | 730 | to_transfer = available <= count ? available : count; |
paul@90 | 731 | |
paul@90 | 732 | file_data_read(file, (char *) buf, to_transfer); |
paul@90 | 733 | |
paul@90 | 734 | /* Update counters. */ |
paul@90 | 735 | |
paul@90 | 736 | available -= to_transfer; |
paul@90 | 737 | |
paul@90 | 738 | count -= to_transfer; |
paul@90 | 739 | total += to_transfer; |
paul@90 | 740 | |
paul@90 | 741 | buf = ((char *) buf + to_transfer); |
paul@90 | 742 | } |
paul@90 | 743 | |
paul@90 | 744 | return total; |
paul@90 | 745 | } |
paul@90 | 746 | |
paul@90 | 747 | |
paul@90 | 748 | |
paul@90 | 749 | /* Ensure that the buffer can provide the needed data. */ |
paul@90 | 750 | |
paul@90 | 751 | offset_t client_seek(file_t *file, offset_t offset, int whence) |
paul@90 | 752 | { |
paul@402 | 753 | if (!client_opened(file)) |
paul@90 | 754 | return 0; |
paul@90 | 755 | |
paul@90 | 756 | offset_t position, current = file_data_current_position(file), change; |
paul@90 | 757 | |
paul@90 | 758 | switch (whence) |
paul@90 | 759 | { |
paul@90 | 760 | case SEEK_SET: |
paul@90 | 761 | position = offset; |
paul@90 | 762 | break; |
paul@90 | 763 | |
paul@90 | 764 | case SEEK_CUR: |
paul@90 | 765 | position = current + offset; |
paul@90 | 766 | break; |
paul@90 | 767 | |
paul@90 | 768 | case SEEK_END: |
paul@90 | 769 | position = file->size + offset; |
paul@90 | 770 | break; |
paul@90 | 771 | |
paul@90 | 772 | default: |
paul@90 | 773 | /* NOTE: Set errno to EINVAL. */ |
paul@117 | 774 | return current; |
paul@90 | 775 | } |
paul@90 | 776 | |
paul@90 | 777 | /* Retain the current position if unchanged. */ |
paul@90 | 778 | |
paul@90 | 779 | if (position == current) |
paul@90 | 780 | return position; |
paul@90 | 781 | |
paul@90 | 782 | /* Move forward in the file. */ |
paul@90 | 783 | |
paul@90 | 784 | if (position > current) |
paul@90 | 785 | { |
paul@90 | 786 | change = position - current; |
paul@90 | 787 | |
paul@90 | 788 | /* Move towards the end of available data. |
paul@90 | 789 | Request new data if not enough is available. */ |
paul@90 | 790 | |
paul@90 | 791 | if (change <= file_data_available(file)) |
paul@90 | 792 | { |
paul@90 | 793 | file->data_current += change; |
paul@90 | 794 | return position; |
paul@90 | 795 | } |
paul@90 | 796 | } |
paul@90 | 797 | |
paul@90 | 798 | /* Move backward in the file. */ |
paul@90 | 799 | |
paul@90 | 800 | else |
paul@90 | 801 | { |
paul@90 | 802 | change = current - position; |
paul@90 | 803 | |
paul@90 | 804 | /* Move towards the start of available data. |
paul@90 | 805 | Request new data if moving beyond the start of the data. */ |
paul@90 | 806 | |
paul@90 | 807 | if (change <= file->data_current) |
paul@90 | 808 | { |
paul@90 | 809 | file->data_current -= change; |
paul@90 | 810 | return position; |
paul@90 | 811 | } |
paul@90 | 812 | } |
paul@90 | 813 | |
paul@90 | 814 | /* Handle unwritten data and reset the buffer for reading. */ |
paul@90 | 815 | |
paul@117 | 816 | if (_access(file, position)) |
paul@117 | 817 | return current; |
paul@117 | 818 | |
paul@90 | 819 | return position; |
paul@90 | 820 | } |
paul@90 | 821 | |
paul@90 | 822 | |
paul@90 | 823 | |
paul@117 | 824 | /* Set or unset blocking access for a file. */ |
paul@117 | 825 | |
paul@122 | 826 | long client_set_blocking(file_t *file, notify_flags_t flags) |
paul@117 | 827 | { |
paul@117 | 828 | long err; |
paul@117 | 829 | |
paul@122 | 830 | if (file->can_block == flags) |
paul@117 | 831 | return L4_EOK; |
paul@117 | 832 | |
paul@177 | 833 | /* Since blocking access is used with specific file notifications, the |
paul@177 | 834 | per-task notifier is used. */ |
paul@117 | 835 | |
paul@575 | 836 | notifier_t *notifier = client_notifier_task(); |
paul@180 | 837 | |
paul@122 | 838 | if (flags) |
paul@180 | 839 | err = client_subscribe(file, flags, notifier); |
paul@117 | 840 | else |
paul@180 | 841 | err = client_unsubscribe(file, notifier); |
paul@117 | 842 | |
paul@117 | 843 | if (err) |
paul@117 | 844 | return err; |
paul@117 | 845 | |
paul@122 | 846 | file->can_block = flags; |
paul@117 | 847 | return L4_EOK; |
paul@117 | 848 | } |
paul@117 | 849 | |
paul@117 | 850 | |
paul@117 | 851 | |
paul@607 | 852 | /* Subscribe to events concerning a file. */ |
paul@117 | 853 | |
paul@575 | 854 | long client_subscribe(file_t *file, notify_flags_t flags, notifier_t *notifier) |
paul@117 | 855 | { |
paul@402 | 856 | if (!client_opened(file)) |
paul@117 | 857 | return -L4_EINVAL; |
paul@117 | 858 | |
paul@575 | 859 | return notify_subscribe(file_notifiable(file), flags, notifier); |
paul@117 | 860 | } |
paul@117 | 861 | |
paul@117 | 862 | |
paul@117 | 863 | |
paul@117 | 864 | /* Return the current position in the file. */ |
paul@117 | 865 | |
paul@411 | 866 | offset_t client_tell(file_t *file) |
paul@90 | 867 | { |
paul@402 | 868 | if (!client_opened(file)) |
paul@90 | 869 | return -L4_EINVAL; |
paul@90 | 870 | |
paul@90 | 871 | return file_data_current_position(file); |
paul@90 | 872 | } |
paul@90 | 873 | |
paul@90 | 874 | |
paul@90 | 875 | |
paul@117 | 876 | /* Unsubscribe from events concerning a file. */ |
paul@117 | 877 | |
paul@575 | 878 | long client_unsubscribe(file_t *file, notifier_t *notifier) |
paul@117 | 879 | { |
paul@402 | 880 | if (!client_opened(file)) |
paul@117 | 881 | return -L4_EINVAL; |
paul@117 | 882 | |
paul@575 | 883 | return notify_unsubscribe(file_notifiable(file), notifier); |
paul@117 | 884 | } |
paul@117 | 885 | |
paul@117 | 886 | |
paul@117 | 887 | |
paul@123 | 888 | /* Wait for events involving a specific file. */ |
paul@123 | 889 | |
paul@575 | 890 | long client_wait_file(file_t *file, notifier_t *notifier) |
paul@123 | 891 | { |
paul@402 | 892 | if (!client_opened(file)) |
paul@123 | 893 | return -L4_EINVAL; |
paul@123 | 894 | |
paul@180 | 895 | return file_notify_wait_file(file, notifier); |
paul@123 | 896 | } |
paul@123 | 897 | |
paul@123 | 898 | /* Wait for events concerning files, referencing a file object if an event is |
paul@123 | 899 | delivered. */ |
paul@123 | 900 | |
paul@575 | 901 | long client_wait_files(file_t **file, notifier_t *notifier) |
paul@123 | 902 | { |
paul@180 | 903 | return file_notify_wait_files(file, notifier); |
paul@117 | 904 | } |
paul@117 | 905 | |
paul@117 | 906 | |
paul@117 | 907 | |
paul@90 | 908 | /* Write to the filesystem object from the buffer provided. */ |
paul@90 | 909 | |
paul@90 | 910 | offset_t client_write(file_t *file, const void *buf, offset_t count) |
paul@90 | 911 | { |
paul@402 | 912 | if (!client_opened(file)) |
paul@90 | 913 | return 0; |
paul@90 | 914 | |
paul@100 | 915 | /* Map memory if none has been mapped so far. */ |
paul@100 | 916 | |
paul@108 | 917 | if (_map_memory(file, count) == NULL) |
paul@100 | 918 | return 0; |
paul@100 | 919 | |
paul@90 | 920 | /* Attempt to ensure that the file can accept the amount of data to be |
paul@90 | 921 | written. This may not resize to the needed amount if a file has a fixed |
paul@90 | 922 | size, but data will still be written to any available space. */ |
paul@90 | 923 | |
paul@90 | 924 | offset_t needed_size = file_data_current_position(file) + count; |
paul@90 | 925 | |
paul@171 | 926 | if (file->object_flags & OBJECT_HAS_SIZE) |
paul@90 | 927 | { |
paul@108 | 928 | if (file->size < needed_size) |
paul@108 | 929 | { |
paul@108 | 930 | file_resize(file, needed_size); |
paul@90 | 931 | |
paul@108 | 932 | if (file->size < needed_size) |
paul@108 | 933 | count = file->size - file_data_current_position(file); |
paul@108 | 934 | } |
paul@90 | 935 | } |
paul@90 | 936 | |
paul@90 | 937 | /* Space remaining in the descriptor buffer. */ |
paul@90 | 938 | |
paul@90 | 939 | offset_t space = file_data_space(file); |
paul@90 | 940 | offset_t to_transfer, total = 0; |
paul@90 | 941 | |
paul@90 | 942 | while (count > 0) |
paul@90 | 943 | { |
paul@90 | 944 | /* If no space is available, try and send data, reset the buffer. */ |
paul@90 | 945 | |
paul@90 | 946 | if (!space) |
paul@90 | 947 | { |
paul@90 | 948 | /* Flush any unwritten data and continue writing from the current data |
paul@90 | 949 | position. */ |
paul@90 | 950 | |
paul@162 | 951 | if (!_access_blocking(file, file_data_current_position(file), 0)) |
paul@90 | 952 | break; |
paul@90 | 953 | |
paul@90 | 954 | space = file_data_space(file); |
paul@90 | 955 | } |
paul@90 | 956 | |
paul@90 | 957 | /* Transfer data into the supplied buffer. */ |
paul@90 | 958 | |
paul@90 | 959 | to_transfer = space <= count ? space : count; |
paul@90 | 960 | |
paul@90 | 961 | file_data_write(file, (char *) buf, to_transfer); |
paul@90 | 962 | |
paul@90 | 963 | /* Update counters. */ |
paul@90 | 964 | |
paul@90 | 965 | space -= to_transfer; |
paul@90 | 966 | |
paul@90 | 967 | count -= to_transfer; |
paul@90 | 968 | total += to_transfer; |
paul@90 | 969 | |
paul@90 | 970 | buf = ((char *) buf + to_transfer); |
paul@90 | 971 | } |
paul@90 | 972 | |
paul@90 | 973 | return total; |
paul@90 | 974 | } |
paul@90 | 975 | |
paul@90 | 976 | // vim: tabstop=2 expandtab shiftwidth=2 |