[320c884] | 1 | /*
|
---|
| 2 | * Copyright (c) 2007 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[b1834a01] | 29 | /** @addtogroup vfs
|
---|
[320c884] | 30 | * @{
|
---|
[1b20da0] | 31 | */
|
---|
[320c884] | 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file vfs_file.c
|
---|
| 35 | * @brief Various operations on files have their home in this file.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
[19f857a] | 40 | #include <str.h>
|
---|
[320c884] | 41 | #include <assert.h>
|
---|
[3e6a98c5] | 42 | #include <stdbool.h>
|
---|
[26360f7] | 43 | #include <fibril.h>
|
---|
[1e4cada] | 44 | #include <fibril_synch.h>
|
---|
[27b76ca] | 45 | #include <adt/list.h>
|
---|
[e2ab36f1] | 46 | #include <task.h>
|
---|
[58898d1d] | 47 | #include <vfs/vfs.h>
|
---|
[320c884] | 48 | #include "vfs.h"
|
---|
| 49 |
|
---|
[79ae36dd] | 50 | #define VFS_DATA ((vfs_client_data_t *) async_get_client_data())
|
---|
[b75e929] | 51 | #define FILES (VFS_DATA->files)
|
---|
| 52 |
|
---|
| 53 | typedef struct {
|
---|
| 54 | fibril_mutex_t lock;
|
---|
[27b76ca] | 55 | fibril_condvar_t cv;
|
---|
| 56 | list_t passed_handles;
|
---|
[b75e929] | 57 | vfs_file_t **files;
|
---|
| 58 | } vfs_client_data_t;
|
---|
[320c884] | 59 |
|
---|
[27b76ca] | 60 | typedef struct {
|
---|
| 61 | link_t link;
|
---|
[bb9ec2d] | 62 | vfs_node_t *node;
|
---|
| 63 | int permissions;
|
---|
[27b76ca] | 64 | } vfs_boxed_handle_t;
|
---|
| 65 |
|
---|
[b7fd2a0] | 66 | static errno_t _vfs_fd_free(vfs_client_data_t *, int);
|
---|
[2bc13887] | 67 |
|
---|
[320c884] | 68 | /** Initialize the table of open files. */
|
---|
[2bc13887] | 69 | static bool vfs_files_init(vfs_client_data_t *vfs_data)
|
---|
[320c884] | 70 | {
|
---|
[2bc13887] | 71 | fibril_mutex_lock(&vfs_data->lock);
|
---|
| 72 | if (!vfs_data->files) {
|
---|
[777832e] | 73 | vfs_data->files = malloc(VFS_MAX_OPEN_FILES * sizeof(vfs_file_t *));
|
---|
[2bc13887] | 74 | if (!vfs_data->files) {
|
---|
| 75 | fibril_mutex_unlock(&vfs_data->lock);
|
---|
[320c884] | 76 | return false;
|
---|
[b75e929] | 77 | }
|
---|
[777832e] | 78 | memset(vfs_data->files, 0, VFS_MAX_OPEN_FILES * sizeof(vfs_file_t *));
|
---|
[320c884] | 79 | }
|
---|
[2bc13887] | 80 | fibril_mutex_unlock(&vfs_data->lock);
|
---|
[320c884] | 81 | return true;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[f29a3a2] | 84 | /** Cleanup the table of open files. */
|
---|
[2bc13887] | 85 | static void vfs_files_done(vfs_client_data_t *vfs_data)
|
---|
[f29a3a2] | 86 | {
|
---|
| 87 | int i;
|
---|
| 88 |
|
---|
[2bc13887] | 89 | if (!vfs_data->files)
|
---|
[f29a3a2] | 90 | return;
|
---|
| 91 |
|
---|
[777832e] | 92 | for (i = 0; i < VFS_MAX_OPEN_FILES; i++) {
|
---|
[2bc13887] | 93 | if (vfs_data->files[i])
|
---|
| 94 | (void) _vfs_fd_free(vfs_data, i);
|
---|
[f29a3a2] | 95 | }
|
---|
[a35b458] | 96 |
|
---|
[2bc13887] | 97 | free(vfs_data->files);
|
---|
[27b76ca] | 98 |
|
---|
| 99 | while (!list_empty(&vfs_data->passed_handles)) {
|
---|
| 100 | link_t *lnk;
|
---|
| 101 | vfs_boxed_handle_t *bh;
|
---|
[a35b458] | 102 |
|
---|
[27b76ca] | 103 | lnk = list_first(&vfs_data->passed_handles);
|
---|
| 104 | list_remove(lnk);
|
---|
| 105 |
|
---|
| 106 | bh = list_get_instance(lnk, vfs_boxed_handle_t, link);
|
---|
| 107 | free(bh);
|
---|
| 108 | }
|
---|
[b75e929] | 109 | }
|
---|
| 110 |
|
---|
| 111 | void *vfs_client_data_create(void)
|
---|
| 112 | {
|
---|
| 113 | vfs_client_data_t *vfs_data;
|
---|
| 114 |
|
---|
| 115 | vfs_data = malloc(sizeof(vfs_client_data_t));
|
---|
| 116 | if (vfs_data) {
|
---|
| 117 | fibril_mutex_initialize(&vfs_data->lock);
|
---|
[27b76ca] | 118 | fibril_condvar_initialize(&vfs_data->cv);
|
---|
| 119 | list_initialize(&vfs_data->passed_handles);
|
---|
[b75e929] | 120 | vfs_data->files = NULL;
|
---|
| 121 | }
|
---|
[a35b458] | 122 |
|
---|
[b75e929] | 123 | return vfs_data;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | void vfs_client_data_destroy(void *data)
|
---|
| 127 | {
|
---|
| 128 | vfs_client_data_t *vfs_data = (vfs_client_data_t *) data;
|
---|
| 129 |
|
---|
[2bc13887] | 130 | vfs_files_done(vfs_data);
|
---|
[b75e929] | 131 | free(vfs_data);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[25bef0ff] | 134 | /** Close the file in the endpoint FS server. */
|
---|
[b7fd2a0] | 135 | static errno_t vfs_file_close_remote(vfs_file_t *file)
|
---|
[25bef0ff] | 136 | {
|
---|
| 137 | assert(!file->refcnt);
|
---|
[a35b458] | 138 |
|
---|
[79ae36dd] | 139 | async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
[a35b458] | 140 |
|
---|
[79ae36dd] | 141 | ipc_call_t answer;
|
---|
[15f3c3f] | 142 | aid_t msg = async_send_2(exch, VFS_OUT_CLOSE, file->node->service_id,
|
---|
[25bef0ff] | 143 | file->node->index, &answer);
|
---|
[a35b458] | 144 |
|
---|
[79ae36dd] | 145 | vfs_exchange_release(exch);
|
---|
[a35b458] | 146 |
|
---|
[b7fd2a0] | 147 | errno_t rc;
|
---|
[25bef0ff] | 148 | async_wait_for(msg, &rc);
|
---|
[a35b458] | 149 |
|
---|
[fafb8e5] | 150 | return ipc_get_retval(&answer);
|
---|
[25bef0ff] | 151 | }
|
---|
| 152 |
|
---|
[b75e929] | 153 | /** Increment reference count of VFS file structure.
|
---|
| 154 | *
|
---|
| 155 | * @param file File structure that will have reference count
|
---|
| 156 | * incremented.
|
---|
| 157 | */
|
---|
[2bc13887] | 158 | static void vfs_file_addref(vfs_client_data_t *vfs_data, vfs_file_t *file)
|
---|
[b75e929] | 159 | {
|
---|
[2bc13887] | 160 | assert(fibril_mutex_is_locked(&vfs_data->lock));
|
---|
[b75e929] | 161 |
|
---|
| 162 | file->refcnt++;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /** Decrement reference count of VFS file structure.
|
---|
| 166 | *
|
---|
| 167 | * @param file File structure that will have reference count
|
---|
| 168 | * decremented.
|
---|
| 169 | */
|
---|
[b7fd2a0] | 170 | static errno_t vfs_file_delref(vfs_client_data_t *vfs_data, vfs_file_t *file)
|
---|
[b75e929] | 171 | {
|
---|
[b7fd2a0] | 172 | errno_t rc = EOK;
|
---|
[25bef0ff] | 173 |
|
---|
[2bc13887] | 174 | assert(fibril_mutex_is_locked(&vfs_data->lock));
|
---|
[b75e929] | 175 |
|
---|
| 176 | if (file->refcnt-- == 1) {
|
---|
| 177 | /*
|
---|
[25bef0ff] | 178 | * Lost the last reference to a file, need to close it in the
|
---|
| 179 | * endpoint FS and drop our reference to the underlying VFS node.
|
---|
[b75e929] | 180 | */
|
---|
[a35b458] | 181 |
|
---|
[5126f80] | 182 | if (file->node != NULL) {
|
---|
| 183 | if (file->open_read || file->open_write) {
|
---|
| 184 | rc = vfs_file_close_remote(file);
|
---|
| 185 | }
|
---|
| 186 | vfs_node_delref(file->node);
|
---|
[1433ecda] | 187 | }
|
---|
[b75e929] | 188 | free(file);
|
---|
| 189 | }
|
---|
[25bef0ff] | 190 |
|
---|
| 191 | return rc;
|
---|
[f29a3a2] | 192 | }
|
---|
| 193 |
|
---|
[b7fd2a0] | 194 | static errno_t _vfs_fd_alloc(vfs_client_data_t *vfs_data, vfs_file_t **file, bool desc, int *out_fd)
|
---|
[320c884] | 195 | {
|
---|
[2bc13887] | 196 | if (!vfs_files_init(vfs_data))
|
---|
[ac23b9d3] | 197 | return ENOMEM;
|
---|
[a35b458] | 198 |
|
---|
[ac23b9d3] | 199 | unsigned int i;
|
---|
[2b88074b] | 200 | if (desc)
|
---|
[777832e] | 201 | i = VFS_MAX_OPEN_FILES - 1;
|
---|
[2b88074b] | 202 | else
|
---|
| 203 | i = 0;
|
---|
[a35b458] | 204 |
|
---|
[2bc13887] | 205 | fibril_mutex_lock(&vfs_data->lock);
|
---|
[2b88074b] | 206 | while (true) {
|
---|
[2bc13887] | 207 | if (!vfs_data->files[i]) {
|
---|
| 208 | vfs_data->files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
|
---|
| 209 | if (!vfs_data->files[i]) {
|
---|
| 210 | fibril_mutex_unlock(&vfs_data->lock);
|
---|
[320c884] | 211 | return ENOMEM;
|
---|
[b75e929] | 212 | }
|
---|
[a35b458] | 213 |
|
---|
[2bc13887] | 214 | memset(vfs_data->files[i], 0, sizeof(vfs_file_t));
|
---|
[a35b458] | 215 |
|
---|
[c577a9a] | 216 | fibril_mutex_initialize(&vfs_data->files[i]->_lock);
|
---|
| 217 | fibril_mutex_lock(&vfs_data->files[i]->_lock);
|
---|
[2bc13887] | 218 | vfs_file_addref(vfs_data, vfs_data->files[i]);
|
---|
[a35b458] | 219 |
|
---|
[c577a9a] | 220 | *file = vfs_data->files[i];
|
---|
| 221 | vfs_file_addref(vfs_data, *file);
|
---|
[a35b458] | 222 |
|
---|
[2bc13887] | 223 | fibril_mutex_unlock(&vfs_data->lock);
|
---|
[6ad454f] | 224 | *out_fd = (int) i;
|
---|
| 225 | return EOK;
|
---|
[320c884] | 226 | }
|
---|
[a35b458] | 227 |
|
---|
[2b88074b] | 228 | if (desc) {
|
---|
| 229 | if (i == 0)
|
---|
| 230 | break;
|
---|
[a35b458] | 231 |
|
---|
[2b88074b] | 232 | i--;
|
---|
| 233 | } else {
|
---|
[777832e] | 234 | if (i == VFS_MAX_OPEN_FILES - 1)
|
---|
[2b88074b] | 235 | break;
|
---|
[a35b458] | 236 |
|
---|
[2b88074b] | 237 | i++;
|
---|
| 238 | }
|
---|
[320c884] | 239 | }
|
---|
[2bc13887] | 240 | fibril_mutex_unlock(&vfs_data->lock);
|
---|
[a35b458] | 241 |
|
---|
[320c884] | 242 | return EMFILE;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[2bc13887] | 245 | /** Allocate a file descriptor.
|
---|
[320c884] | 246 | *
|
---|
[c577a9a] | 247 | * @param file Is set to point to the newly created file structure. Must be put afterwards.
|
---|
[2bc13887] | 248 | * @param desc If true, look for an available file descriptor
|
---|
| 249 | * in a descending order.
|
---|
[b7f9087] | 250 | *
|
---|
[6ad454f] | 251 | * @param[out] out_fd First available file descriptor
|
---|
| 252 | *
|
---|
| 253 | * @return Error code.
|
---|
[320c884] | 254 | */
|
---|
[b7fd2a0] | 255 | errno_t vfs_fd_alloc(vfs_file_t **file, bool desc, int *out_fd)
|
---|
[2bc13887] | 256 | {
|
---|
[6ad454f] | 257 | return _vfs_fd_alloc(VFS_DATA, file, desc, out_fd);
|
---|
[2bc13887] | 258 | }
|
---|
| 259 |
|
---|
[b7fd2a0] | 260 | static errno_t _vfs_fd_free_locked(vfs_client_data_t *vfs_data, int fd)
|
---|
[40feeac] | 261 | {
|
---|
[777832e] | 262 | if ((fd < 0) || (fd >= VFS_MAX_OPEN_FILES) || !vfs_data->files[fd]) {
|
---|
[40feeac] | 263 | return EBADF;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[b7fd2a0] | 266 | errno_t rc = vfs_file_delref(vfs_data, vfs_data->files[fd]);
|
---|
[40feeac] | 267 | vfs_data->files[fd] = NULL;
|
---|
| 268 | return rc;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[b7fd2a0] | 271 | static errno_t _vfs_fd_free(vfs_client_data_t *vfs_data, int fd)
|
---|
[320c884] | 272 | {
|
---|
[b7fd2a0] | 273 | errno_t rc;
|
---|
[25bef0ff] | 274 |
|
---|
[2bc13887] | 275 | if (!vfs_files_init(vfs_data))
|
---|
[ac23b9d3] | 276 | return ENOMEM;
|
---|
[b75e929] | 277 |
|
---|
[1b20da0] | 278 | fibril_mutex_lock(&vfs_data->lock);
|
---|
[40feeac] | 279 | rc = _vfs_fd_free_locked(vfs_data, fd);
|
---|
[2bc13887] | 280 | fibril_mutex_unlock(&vfs_data->lock);
|
---|
[a35b458] | 281 |
|
---|
[25bef0ff] | 282 | return rc;
|
---|
[320c884] | 283 | }
|
---|
| 284 |
|
---|
[2bc13887] | 285 | /** Release file descriptor.
|
---|
| 286 | *
|
---|
| 287 | * @param fd File descriptor being released.
|
---|
| 288 | *
|
---|
| 289 | * @return EOK on success or EBADF if fd is an invalid file
|
---|
| 290 | * descriptor.
|
---|
| 291 | */
|
---|
[b7fd2a0] | 292 | errno_t vfs_fd_free(int fd)
|
---|
[2bc13887] | 293 | {
|
---|
| 294 | return _vfs_fd_free(VFS_DATA, fd);
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[2b88074b] | 297 | /** Assign a file to a file descriptor.
|
---|
| 298 | *
|
---|
| 299 | * @param file File to assign.
|
---|
| 300 | * @param fd File descriptor to assign to.
|
---|
| 301 | *
|
---|
| 302 | * @return EOK on success or EINVAL if fd is an invalid or already
|
---|
| 303 | * used file descriptor.
|
---|
| 304 | *
|
---|
| 305 | */
|
---|
[b7fd2a0] | 306 | errno_t vfs_fd_assign(vfs_file_t *file, int fd)
|
---|
[2b88074b] | 307 | {
|
---|
[2bc13887] | 308 | if (!vfs_files_init(VFS_DATA))
|
---|
[2b88074b] | 309 | return ENOMEM;
|
---|
[b75e929] | 310 |
|
---|
[1b20da0] | 311 | fibril_mutex_lock(&VFS_DATA->lock);
|
---|
[777832e] | 312 | if ((fd < 0) || (fd >= VFS_MAX_OPEN_FILES)) {
|
---|
[b75e929] | 313 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[fcab7ef] | 314 | return EBADF;
|
---|
| 315 | }
|
---|
[40feeac] | 316 |
|
---|
| 317 | /* Make sure fd is closed. */
|
---|
| 318 | (void) _vfs_fd_free_locked(VFS_DATA, fd);
|
---|
| 319 | assert(FILES[fd] == NULL);
|
---|
[a35b458] | 320 |
|
---|
[b75e929] | 321 | FILES[fd] = file;
|
---|
[2bc13887] | 322 | vfs_file_addref(VFS_DATA, FILES[fd]);
|
---|
[b75e929] | 323 | fibril_mutex_unlock(&VFS_DATA->lock);
|
---|
[a35b458] | 324 |
|
---|
[2b88074b] | 325 | return EOK;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[5126f80] | 328 | static void _vfs_file_put(vfs_client_data_t *vfs_data, vfs_file_t *file)
|
---|
| 329 | {
|
---|
| 330 | fibril_mutex_unlock(&file->_lock);
|
---|
[a35b458] | 331 |
|
---|
[5126f80] | 332 | fibril_mutex_lock(&vfs_data->lock);
|
---|
| 333 | vfs_file_delref(vfs_data, file);
|
---|
| 334 | fibril_mutex_unlock(&vfs_data->lock);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[2bc13887] | 337 | static vfs_file_t *_vfs_file_get(vfs_client_data_t *vfs_data, int fd)
|
---|
[320c884] | 338 | {
|
---|
[2bc13887] | 339 | if (!vfs_files_init(vfs_data))
|
---|
[ac23b9d3] | 340 | return NULL;
|
---|
[a35b458] | 341 |
|
---|
[2bc13887] | 342 | fibril_mutex_lock(&vfs_data->lock);
|
---|
[777832e] | 343 | if ((fd >= 0) && (fd < VFS_MAX_OPEN_FILES)) {
|
---|
[2bc13887] | 344 | vfs_file_t *file = vfs_data->files[fd];
|
---|
[6639ae1] | 345 | if (file != NULL) {
|
---|
[2bc13887] | 346 | vfs_file_addref(vfs_data, file);
|
---|
| 347 | fibril_mutex_unlock(&vfs_data->lock);
|
---|
[a35b458] | 348 |
|
---|
[c577a9a] | 349 | fibril_mutex_lock(&file->_lock);
|
---|
[5126f80] | 350 | if (file->node == NULL) {
|
---|
| 351 | _vfs_file_put(vfs_data, file);
|
---|
| 352 | return NULL;
|
---|
| 353 | }
|
---|
| 354 | assert(file != NULL);
|
---|
| 355 | assert(file->node != NULL);
|
---|
[6639ae1] | 356 | return file;
|
---|
| 357 | }
|
---|
[b75e929] | 358 | }
|
---|
[2bc13887] | 359 | fibril_mutex_unlock(&vfs_data->lock);
|
---|
[a35b458] | 360 |
|
---|
[ebd9392] | 361 | return NULL;
|
---|
[320c884] | 362 | }
|
---|
| 363 |
|
---|
[2bc13887] | 364 | /** Find VFS file structure for a given file descriptor.
|
---|
| 365 | *
|
---|
| 366 | * @param fd File descriptor.
|
---|
| 367 | *
|
---|
| 368 | * @return VFS file structure corresponding to fd.
|
---|
| 369 | */
|
---|
| 370 | vfs_file_t *vfs_file_get(int fd)
|
---|
| 371 | {
|
---|
| 372 | return _vfs_file_get(VFS_DATA, fd);
|
---|
| 373 | }
|
---|
| 374 |
|
---|
[4fe94c66] | 375 | /** Stop using a file structure.
|
---|
| 376 | *
|
---|
| 377 | * @param file VFS file structure.
|
---|
| 378 | */
|
---|
| 379 | void vfs_file_put(vfs_file_t *file)
|
---|
| 380 | {
|
---|
[2bc13887] | 381 | _vfs_file_put(VFS_DATA, file);
|
---|
| 382 | }
|
---|
| 383 |
|
---|
[354b642] | 384 | void vfs_op_pass_handle(task_id_t donor_id, task_id_t acceptor_id, int donor_fd)
|
---|
[2bc13887] | 385 | {
|
---|
| 386 | vfs_client_data_t *donor_data = NULL;
|
---|
| 387 | vfs_client_data_t *acceptor_data = NULL;
|
---|
| 388 | vfs_file_t *donor_file = NULL;
|
---|
[27b76ca] | 389 | vfs_boxed_handle_t *bh;
|
---|
[2bc13887] | 390 |
|
---|
[e2ab36f1] | 391 | acceptor_data = async_get_client_data_by_id(acceptor_id);
|
---|
[2bc13887] | 392 | if (!acceptor_data)
|
---|
[27b76ca] | 393 | return;
|
---|
| 394 |
|
---|
| 395 | bh = malloc(sizeof(vfs_boxed_handle_t));
|
---|
| 396 | assert(bh);
|
---|
| 397 |
|
---|
| 398 | link_initialize(&bh->link);
|
---|
[bb9ec2d] | 399 | bh->node = NULL;
|
---|
[27b76ca] | 400 |
|
---|
[e2ab36f1] | 401 | donor_data = async_get_client_data_by_id(donor_id);
|
---|
[27b76ca] | 402 | if (!donor_data)
|
---|
[2bc13887] | 403 | goto out;
|
---|
| 404 |
|
---|
| 405 | donor_file = _vfs_file_get(donor_data, donor_fd);
|
---|
| 406 | if (!donor_file)
|
---|
| 407 | goto out;
|
---|
| 408 |
|
---|
| 409 | /*
|
---|
| 410 | * Add a new reference to the underlying VFS node.
|
---|
| 411 | */
|
---|
| 412 | vfs_node_addref(donor_file->node);
|
---|
[bb9ec2d] | 413 | bh->node = donor_file->node;
|
---|
| 414 | bh->permissions = donor_file->permissions;
|
---|
[354b642] | 415 |
|
---|
[2bc13887] | 416 | out:
|
---|
[27b76ca] | 417 | fibril_mutex_lock(&acceptor_data->lock);
|
---|
| 418 | list_append(&bh->link, &acceptor_data->passed_handles);
|
---|
| 419 | fibril_condvar_broadcast(&acceptor_data->cv);
|
---|
| 420 | fibril_mutex_unlock(&acceptor_data->lock);
|
---|
| 421 |
|
---|
[2bc13887] | 422 | if (donor_data)
|
---|
[e2ab36f1] | 423 | async_put_client_data_by_id(donor_id);
|
---|
[2bc13887] | 424 | if (acceptor_data)
|
---|
[e2ab36f1] | 425 | async_put_client_data_by_id(acceptor_id);
|
---|
[2bc13887] | 426 | if (donor_file)
|
---|
| 427 | _vfs_file_put(donor_data, donor_file);
|
---|
[27b76ca] | 428 | }
|
---|
| 429 |
|
---|
[b7fd2a0] | 430 | errno_t vfs_wait_handle_internal(bool high_fd, int *out_fd)
|
---|
[27b76ca] | 431 | {
|
---|
[1b20da0] | 432 | vfs_client_data_t *vfs_data = VFS_DATA;
|
---|
[a35b458] | 433 |
|
---|
[27b76ca] | 434 | fibril_mutex_lock(&vfs_data->lock);
|
---|
| 435 | while (list_empty(&vfs_data->passed_handles))
|
---|
| 436 | fibril_condvar_wait(&vfs_data->cv, &vfs_data->lock);
|
---|
| 437 | link_t *lnk = list_first(&vfs_data->passed_handles);
|
---|
| 438 | list_remove(lnk);
|
---|
| 439 | fibril_mutex_unlock(&vfs_data->lock);
|
---|
| 440 |
|
---|
| 441 | vfs_boxed_handle_t *bh = list_get_instance(lnk, vfs_boxed_handle_t, link);
|
---|
| 442 |
|
---|
[bb9ec2d] | 443 | vfs_file_t *file;
|
---|
[b7fd2a0] | 444 | errno_t rc = _vfs_fd_alloc(vfs_data, &file, high_fd, out_fd);
|
---|
[6ad454f] | 445 | if (rc != EOK) {
|
---|
[bb9ec2d] | 446 | vfs_node_delref(bh->node);
|
---|
| 447 | free(bh);
|
---|
[6ad454f] | 448 | return rc;
|
---|
[bb9ec2d] | 449 | }
|
---|
[a35b458] | 450 |
|
---|
[bb9ec2d] | 451 | file->node = bh->node;
|
---|
| 452 | file->permissions = bh->permissions;
|
---|
| 453 | vfs_file_put(file);
|
---|
| 454 | free(bh);
|
---|
[6ad454f] | 455 | return EOK;
|
---|
[4fe94c66] | 456 | }
|
---|
| 457 |
|
---|
[320c884] | 458 | /**
|
---|
| 459 | * @}
|
---|
[ac23b9d3] | 460 | */
|
---|