source: mainline/uspace/srv/vfs/vfs_file.c@ 1433ecda

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1433ecda was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 10.7 KB
RevLine 
[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
29/** @addtogroup fs
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
53typedef 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]60typedef struct {
61 link_t link;
[bb9ec2d]62 vfs_node_t *node;
63 int permissions;
[27b76ca]64} vfs_boxed_handle_t;
65
[b7fd2a0]66static errno_t _vfs_fd_free(vfs_client_data_t *, int);
[2bc13887]67
[320c884]68/** Initialize the table of open files. */
[2bc13887]69static 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) {
73 vfs_data->files = malloc(MAX_OPEN_FILES * sizeof(vfs_file_t *));
74 if (!vfs_data->files) {
75 fibril_mutex_unlock(&vfs_data->lock);
[320c884]76 return false;
[b75e929]77 }
[2bc13887]78 memset(vfs_data->files, 0, 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]85static 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
92 for (i = 0; i < 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
111void *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
126void 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]135static 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
[d9f0894]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]158static 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]170static 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]194static 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)
[42fa698]201 i = 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
214
[2bc13887]215 memset(vfs_data->files[i], 0, sizeof(vfs_file_t));
[a35b458]216
[c577a9a]217 fibril_mutex_initialize(&vfs_data->files[i]->_lock);
218 fibril_mutex_lock(&vfs_data->files[i]->_lock);
[2bc13887]219 vfs_file_addref(vfs_data, vfs_data->files[i]);
[a35b458]220
[c577a9a]221 *file = vfs_data->files[i];
222 vfs_file_addref(vfs_data, *file);
[a35b458]223
[2bc13887]224 fibril_mutex_unlock(&vfs_data->lock);
[6ad454f]225 *out_fd = (int) i;
226 return EOK;
[320c884]227 }
[a35b458]228
[2b88074b]229 if (desc) {
230 if (i == 0)
231 break;
[a35b458]232
[2b88074b]233 i--;
234 } else {
[d8f92868]235 if (i == MAX_OPEN_FILES - 1)
[2b88074b]236 break;
[a35b458]237
[2b88074b]238 i++;
239 }
[320c884]240 }
[2bc13887]241 fibril_mutex_unlock(&vfs_data->lock);
[a35b458]242
[320c884]243 return EMFILE;
244}
245
[2bc13887]246/** Allocate a file descriptor.
[320c884]247 *
[c577a9a]248 * @param file Is set to point to the newly created file structure. Must be put afterwards.
[2bc13887]249 * @param desc If true, look for an available file descriptor
250 * in a descending order.
[b7f9087]251 *
[6ad454f]252 * @param[out] out_fd First available file descriptor
253 *
254 * @return Error code.
[320c884]255 */
[b7fd2a0]256errno_t vfs_fd_alloc(vfs_file_t **file, bool desc, int *out_fd)
[2bc13887]257{
[6ad454f]258 return _vfs_fd_alloc(VFS_DATA, file, desc, out_fd);
[2bc13887]259}
260
[b7fd2a0]261static errno_t _vfs_fd_free_locked(vfs_client_data_t *vfs_data, int fd)
[40feeac]262{
263 if ((fd < 0) || (fd >= MAX_OPEN_FILES) || !vfs_data->files[fd]) {
264 return EBADF;
265 }
266
[b7fd2a0]267 errno_t rc = vfs_file_delref(vfs_data, vfs_data->files[fd]);
[40feeac]268 vfs_data->files[fd] = NULL;
269 return rc;
270}
271
[b7fd2a0]272static errno_t _vfs_fd_free(vfs_client_data_t *vfs_data, int fd)
[320c884]273{
[b7fd2a0]274 errno_t rc;
[25bef0ff]275
[2bc13887]276 if (!vfs_files_init(vfs_data))
[ac23b9d3]277 return ENOMEM;
[b75e929]278
[1b20da0]279 fibril_mutex_lock(&vfs_data->lock);
[40feeac]280 rc = _vfs_fd_free_locked(vfs_data, fd);
[2bc13887]281 fibril_mutex_unlock(&vfs_data->lock);
[a35b458]282
[25bef0ff]283 return rc;
[320c884]284}
285
[2bc13887]286/** Release file descriptor.
287 *
288 * @param fd File descriptor being released.
289 *
290 * @return EOK on success or EBADF if fd is an invalid file
291 * descriptor.
292 */
[b7fd2a0]293errno_t vfs_fd_free(int fd)
[2bc13887]294{
295 return _vfs_fd_free(VFS_DATA, fd);
296}
297
[2b88074b]298/** Assign a file to a file descriptor.
299 *
300 * @param file File to assign.
301 * @param fd File descriptor to assign to.
302 *
303 * @return EOK on success or EINVAL if fd is an invalid or already
304 * used file descriptor.
305 *
306 */
[b7fd2a0]307errno_t vfs_fd_assign(vfs_file_t *file, int fd)
[2b88074b]308{
[2bc13887]309 if (!vfs_files_init(VFS_DATA))
[2b88074b]310 return ENOMEM;
[b75e929]311
[1b20da0]312 fibril_mutex_lock(&VFS_DATA->lock);
[fcab7ef]313 if ((fd < 0) || (fd >= MAX_OPEN_FILES)) {
[b75e929]314 fibril_mutex_unlock(&VFS_DATA->lock);
[fcab7ef]315 return EBADF;
316 }
[40feeac]317
318 /* Make sure fd is closed. */
319 (void) _vfs_fd_free_locked(VFS_DATA, fd);
320 assert(FILES[fd] == NULL);
[a35b458]321
[b75e929]322 FILES[fd] = file;
[2bc13887]323 vfs_file_addref(VFS_DATA, FILES[fd]);
[b75e929]324 fibril_mutex_unlock(&VFS_DATA->lock);
[a35b458]325
[2b88074b]326 return EOK;
327}
328
[5126f80]329static void _vfs_file_put(vfs_client_data_t *vfs_data, vfs_file_t *file)
330{
331 fibril_mutex_unlock(&file->_lock);
[a35b458]332
[5126f80]333 fibril_mutex_lock(&vfs_data->lock);
334 vfs_file_delref(vfs_data, file);
335 fibril_mutex_unlock(&vfs_data->lock);
336}
337
[2bc13887]338static vfs_file_t *_vfs_file_get(vfs_client_data_t *vfs_data, int fd)
[320c884]339{
[2bc13887]340 if (!vfs_files_init(vfs_data))
[ac23b9d3]341 return NULL;
[a35b458]342
[2bc13887]343 fibril_mutex_lock(&vfs_data->lock);
[b75e929]344 if ((fd >= 0) && (fd < MAX_OPEN_FILES)) {
[2bc13887]345 vfs_file_t *file = vfs_data->files[fd];
[6639ae1]346 if (file != NULL) {
[2bc13887]347 vfs_file_addref(vfs_data, file);
348 fibril_mutex_unlock(&vfs_data->lock);
[a35b458]349
[c577a9a]350 fibril_mutex_lock(&file->_lock);
[5126f80]351 if (file->node == NULL) {
352 _vfs_file_put(vfs_data, file);
353 return NULL;
354 }
355 assert(file != NULL);
356 assert(file->node != NULL);
[6639ae1]357 return file;
358 }
[b75e929]359 }
[2bc13887]360 fibril_mutex_unlock(&vfs_data->lock);
[a35b458]361
[ebd9392]362 return NULL;
[320c884]363}
364
[2bc13887]365/** Find VFS file structure for a given file descriptor.
366 *
367 * @param fd File descriptor.
368 *
369 * @return VFS file structure corresponding to fd.
370 */
371vfs_file_t *vfs_file_get(int fd)
372{
373 return _vfs_file_get(VFS_DATA, fd);
374}
375
[4fe94c66]376/** Stop using a file structure.
377 *
378 * @param file VFS file structure.
379 */
380void vfs_file_put(vfs_file_t *file)
381{
[2bc13887]382 _vfs_file_put(VFS_DATA, file);
383}
384
[354b642]385void vfs_op_pass_handle(task_id_t donor_id, task_id_t acceptor_id, int donor_fd)
[2bc13887]386{
387 vfs_client_data_t *donor_data = NULL;
388 vfs_client_data_t *acceptor_data = NULL;
389 vfs_file_t *donor_file = NULL;
[27b76ca]390 vfs_boxed_handle_t *bh;
[2bc13887]391
[e2ab36f1]392 acceptor_data = async_get_client_data_by_id(acceptor_id);
[2bc13887]393 if (!acceptor_data)
[27b76ca]394 return;
395
396 bh = malloc(sizeof(vfs_boxed_handle_t));
397 assert(bh);
398
399 link_initialize(&bh->link);
[bb9ec2d]400 bh->node = NULL;
[27b76ca]401
[e2ab36f1]402 donor_data = async_get_client_data_by_id(donor_id);
[27b76ca]403 if (!donor_data)
[2bc13887]404 goto out;
405
406 donor_file = _vfs_file_get(donor_data, donor_fd);
407 if (!donor_file)
408 goto out;
409
410 /*
411 * Add a new reference to the underlying VFS node.
412 */
413 vfs_node_addref(donor_file->node);
[bb9ec2d]414 bh->node = donor_file->node;
415 bh->permissions = donor_file->permissions;
[354b642]416
[2bc13887]417out:
[27b76ca]418 fibril_mutex_lock(&acceptor_data->lock);
419 list_append(&bh->link, &acceptor_data->passed_handles);
420 fibril_condvar_broadcast(&acceptor_data->cv);
421 fibril_mutex_unlock(&acceptor_data->lock);
422
[2bc13887]423 if (donor_data)
[e2ab36f1]424 async_put_client_data_by_id(donor_id);
[2bc13887]425 if (acceptor_data)
[e2ab36f1]426 async_put_client_data_by_id(acceptor_id);
[2bc13887]427 if (donor_file)
428 _vfs_file_put(donor_data, donor_file);
[27b76ca]429}
430
[b7fd2a0]431errno_t vfs_wait_handle_internal(bool high_fd, int *out_fd)
[27b76ca]432{
[1b20da0]433 vfs_client_data_t *vfs_data = VFS_DATA;
[a35b458]434
[27b76ca]435 fibril_mutex_lock(&vfs_data->lock);
436 while (list_empty(&vfs_data->passed_handles))
437 fibril_condvar_wait(&vfs_data->cv, &vfs_data->lock);
438 link_t *lnk = list_first(&vfs_data->passed_handles);
439 list_remove(lnk);
440 fibril_mutex_unlock(&vfs_data->lock);
441
442 vfs_boxed_handle_t *bh = list_get_instance(lnk, vfs_boxed_handle_t, link);
443
[bb9ec2d]444 vfs_file_t *file;
[b7fd2a0]445 errno_t rc = _vfs_fd_alloc(vfs_data, &file, high_fd, out_fd);
[6ad454f]446 if (rc != EOK) {
[bb9ec2d]447 vfs_node_delref(bh->node);
448 free(bh);
[6ad454f]449 return rc;
[bb9ec2d]450 }
[a35b458]451
[bb9ec2d]452 file->node = bh->node;
453 file->permissions = bh->permissions;
454 vfs_file_put(file);
455 free(bh);
[6ad454f]456 return EOK;
[4fe94c66]457}
458
[320c884]459/**
460 * @}
[ac23b9d3]461 */
Note: See TracBrowser for help on using the repository browser.