source: mainline/uspace/srv/vfs/vfs_file.c@ 1dff985

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1dff985 was 1393bbb, checked in by Ji?? Z?rev?cky <zarevucky.jiri@…>, 12 years ago

Fix a bug in passing files through IPC.

  • Property mode set to 100644
File size: 10.3 KB
Line 
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 * @{
31 */
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>
40#include <str.h>
41#include <assert.h>
42#include <stdbool.h>
43#include <fibril.h>
44#include <fibril_synch.h>
45#include <adt/list.h>
46#include <task.h>
47#include "vfs.h"
48
49#define VFS_DATA ((vfs_client_data_t *) async_get_client_data())
50#define FILES (VFS_DATA->files)
51
52typedef struct {
53 fibril_mutex_t lock;
54 fibril_condvar_t cv;
55 list_t passed_handles;
56 vfs_file_t **files;
57} vfs_client_data_t;
58
59typedef struct {
60 link_t link;
61 int handle;
62} vfs_boxed_handle_t;
63
64static int _vfs_fd_free(vfs_client_data_t *, int);
65
66/** Initialize the table of open files. */
67static bool vfs_files_init(vfs_client_data_t *vfs_data)
68{
69 fibril_mutex_lock(&vfs_data->lock);
70 if (!vfs_data->files) {
71 vfs_data->files = malloc(MAX_OPEN_FILES * sizeof(vfs_file_t *));
72 if (!vfs_data->files) {
73 fibril_mutex_unlock(&vfs_data->lock);
74 return false;
75 }
76 memset(vfs_data->files, 0, MAX_OPEN_FILES * sizeof(vfs_file_t *));
77 }
78 fibril_mutex_unlock(&vfs_data->lock);
79 return true;
80}
81
82/** Cleanup the table of open files. */
83static void vfs_files_done(vfs_client_data_t *vfs_data)
84{
85 int i;
86
87 if (!vfs_data->files)
88 return;
89
90 for (i = 0; i < MAX_OPEN_FILES; i++) {
91 if (vfs_data->files[i])
92 (void) _vfs_fd_free(vfs_data, i);
93 }
94
95 free(vfs_data->files);
96
97 while (!list_empty(&vfs_data->passed_handles)) {
98 link_t *lnk;
99 vfs_boxed_handle_t *bh;
100
101 lnk = list_first(&vfs_data->passed_handles);
102 list_remove(lnk);
103
104 bh = list_get_instance(lnk, vfs_boxed_handle_t, link);
105 free(bh);
106 }
107}
108
109void *vfs_client_data_create(void)
110{
111 vfs_client_data_t *vfs_data;
112
113 vfs_data = malloc(sizeof(vfs_client_data_t));
114 if (vfs_data) {
115 fibril_mutex_initialize(&vfs_data->lock);
116 fibril_condvar_initialize(&vfs_data->cv);
117 list_initialize(&vfs_data->passed_handles);
118 vfs_data->files = NULL;
119 }
120
121 return vfs_data;
122}
123
124void vfs_client_data_destroy(void *data)
125{
126 vfs_client_data_t *vfs_data = (vfs_client_data_t *) data;
127
128 vfs_files_done(vfs_data);
129 free(vfs_data);
130}
131
132/** Close the file in the endpoint FS server. */
133static int vfs_file_close_remote(vfs_file_t *file)
134{
135 assert(!file->refcnt);
136
137 async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
138
139 ipc_call_t answer;
140 aid_t msg = async_send_2(exch, VFS_OUT_CLOSE, file->node->service_id,
141 file->node->index, &answer);
142
143 vfs_exchange_release(exch);
144
145 sysarg_t rc;
146 async_wait_for(msg, &rc);
147
148 return IPC_GET_ARG1(answer);
149}
150
151/** Increment reference count of VFS file structure.
152 *
153 * @param file File structure that will have reference count
154 * incremented.
155 */
156static void vfs_file_addref(vfs_client_data_t *vfs_data, vfs_file_t *file)
157{
158 assert(fibril_mutex_is_locked(&vfs_data->lock));
159
160 file->refcnt++;
161}
162
163/** Decrement reference count of VFS file structure.
164 *
165 * @param file File structure that will have reference count
166 * decremented.
167 */
168static int vfs_file_delref(vfs_client_data_t *vfs_data, vfs_file_t *file)
169{
170 int rc = EOK;
171
172 assert(fibril_mutex_is_locked(&vfs_data->lock));
173
174 if (file->refcnt-- == 1) {
175 /*
176 * Lost the last reference to a file, need to close it in the
177 * endpoint FS and drop our reference to the underlying VFS node.
178 */
179 if (file->open_read || file->open_write) {
180 rc = vfs_file_close_remote(file);
181 }
182 vfs_node_delref(file->node);
183 free(file);
184 }
185
186 return rc;
187}
188
189static int _vfs_fd_alloc(vfs_client_data_t *vfs_data, bool desc)
190{
191 if (!vfs_files_init(vfs_data))
192 return ENOMEM;
193
194 unsigned int i;
195 if (desc)
196 i = MAX_OPEN_FILES - 1;
197 else
198 i = 0;
199
200 fibril_mutex_lock(&vfs_data->lock);
201 while (true) {
202 if (!vfs_data->files[i]) {
203 vfs_data->files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
204 if (!vfs_data->files[i]) {
205 fibril_mutex_unlock(&vfs_data->lock);
206 return ENOMEM;
207 }
208
209 memset(vfs_data->files[i], 0, sizeof(vfs_file_t));
210 fibril_mutex_initialize(&vfs_data->files[i]->lock);
211 vfs_file_addref(vfs_data, vfs_data->files[i]);
212 fibril_mutex_unlock(&vfs_data->lock);
213 return (int) i;
214 }
215
216 if (desc) {
217 if (i == 0)
218 break;
219
220 i--;
221 } else {
222 if (i == MAX_OPEN_FILES - 1)
223 break;
224
225 i++;
226 }
227 }
228 fibril_mutex_unlock(&vfs_data->lock);
229
230 return EMFILE;
231}
232
233/** Allocate a file descriptor.
234 *
235 * @param desc If true, look for an available file descriptor
236 * in a descending order.
237 *
238 * @return First available file descriptor or a negative error
239 * code.
240 */
241int vfs_fd_alloc(bool desc)
242{
243 return _vfs_fd_alloc(VFS_DATA, desc);
244}
245
246static int _vfs_fd_free(vfs_client_data_t *vfs_data, int fd)
247{
248 int rc;
249
250 if (!vfs_files_init(vfs_data))
251 return ENOMEM;
252
253 fibril_mutex_lock(&vfs_data->lock);
254 if ((fd < 0) || (fd >= MAX_OPEN_FILES) || !vfs_data->files[fd]) {
255 fibril_mutex_unlock(&vfs_data->lock);
256 return EBADF;
257 }
258
259 rc = vfs_file_delref(vfs_data, vfs_data->files[fd]);
260 vfs_data->files[fd] = NULL;
261 fibril_mutex_unlock(&vfs_data->lock);
262
263 return rc;
264}
265
266/** Release file descriptor.
267 *
268 * @param fd File descriptor being released.
269 *
270 * @return EOK on success or EBADF if fd is an invalid file
271 * descriptor.
272 */
273int vfs_fd_free(int fd)
274{
275 return _vfs_fd_free(VFS_DATA, fd);
276}
277
278/** Assign a file to a file descriptor.
279 *
280 * @param file File to assign.
281 * @param fd File descriptor to assign to.
282 *
283 * @return EOK on success or EINVAL if fd is an invalid or already
284 * used file descriptor.
285 *
286 */
287int vfs_fd_assign(vfs_file_t *file, int fd)
288{
289 if (!vfs_files_init(VFS_DATA))
290 return ENOMEM;
291
292 fibril_mutex_lock(&VFS_DATA->lock);
293 if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (FILES[fd] != NULL)) {
294 fibril_mutex_unlock(&VFS_DATA->lock);
295 return EINVAL;
296 }
297
298 FILES[fd] = file;
299 vfs_file_addref(VFS_DATA, FILES[fd]);
300 fibril_mutex_unlock(&VFS_DATA->lock);
301
302 return EOK;
303}
304
305static vfs_file_t *_vfs_file_get(vfs_client_data_t *vfs_data, int fd)
306{
307 if (!vfs_files_init(vfs_data))
308 return NULL;
309
310 fibril_mutex_lock(&vfs_data->lock);
311 if ((fd >= 0) && (fd < MAX_OPEN_FILES)) {
312 vfs_file_t *file = vfs_data->files[fd];
313 if (file != NULL) {
314 vfs_file_addref(vfs_data, file);
315 fibril_mutex_unlock(&vfs_data->lock);
316 return file;
317 }
318 }
319 fibril_mutex_unlock(&vfs_data->lock);
320
321 return NULL;
322}
323
324/** Find VFS file structure for a given file descriptor.
325 *
326 * @param fd File descriptor.
327 *
328 * @return VFS file structure corresponding to fd.
329 */
330vfs_file_t *vfs_file_get(int fd)
331{
332 return _vfs_file_get(VFS_DATA, fd);
333}
334
335static void _vfs_file_put(vfs_client_data_t *vfs_data, vfs_file_t *file)
336{
337 fibril_mutex_lock(&vfs_data->lock);
338 vfs_file_delref(vfs_data, file);
339 fibril_mutex_unlock(&vfs_data->lock);
340}
341
342/** Stop using a file structure.
343 *
344 * @param file VFS file structure.
345 */
346void vfs_file_put(vfs_file_t *file)
347{
348 _vfs_file_put(VFS_DATA, file);
349}
350
351void vfs_pass_handle(task_id_t donor_id, task_id_t acceptor_id, int donor_fd)
352{
353 vfs_client_data_t *donor_data = NULL;
354 vfs_client_data_t *acceptor_data = NULL;
355 vfs_file_t *donor_file = NULL;
356 vfs_file_t *acceptor_file = NULL;
357 vfs_boxed_handle_t *bh;
358 int acceptor_fd;
359
360 acceptor_data = async_get_client_data_by_id(acceptor_id);
361 if (!acceptor_data)
362 return;
363
364 bh = malloc(sizeof(vfs_boxed_handle_t));
365 assert(bh);
366
367 link_initialize(&bh->link);
368 bh->handle = -1;
369
370 donor_data = async_get_client_data_by_id(donor_id);
371 if (!donor_data)
372 goto out;
373
374 donor_file = _vfs_file_get(donor_data, donor_fd);
375 if (!donor_file)
376 goto out;
377
378 acceptor_fd = _vfs_fd_alloc(acceptor_data, false);
379 if (acceptor_fd < 0)
380 goto out;
381
382 bh->handle = acceptor_fd;
383
384 /*
385 * Add a new reference to the underlying VFS node.
386 */
387 vfs_node_addref(donor_file->node);
388 (void) vfs_open_node_remote(donor_file->node);
389
390 acceptor_file = _vfs_file_get(acceptor_data, acceptor_fd);
391 assert(acceptor_file);
392
393 /*
394 * Inherit attributes from the donor.
395 */
396 acceptor_file->node = donor_file->node;
397 acceptor_file->permissions = donor_file->permissions;
398
399 // TODO: The file should not inherit its open status, but clients depend on this.
400 acceptor_file->pos = donor_file->pos;
401 acceptor_file->append = donor_file->append;
402 acceptor_file->open_read = donor_file->open_read;
403 acceptor_file->open_write = donor_file->open_write;
404
405out:
406 fibril_mutex_lock(&acceptor_data->lock);
407 list_append(&bh->link, &acceptor_data->passed_handles);
408 fibril_condvar_broadcast(&acceptor_data->cv);
409 fibril_mutex_unlock(&acceptor_data->lock);
410
411 if (donor_data)
412 async_put_client_data_by_id(donor_id);
413 if (acceptor_data)
414 async_put_client_data_by_id(acceptor_id);
415 if (donor_file)
416 _vfs_file_put(donor_data, donor_file);
417 if (acceptor_file)
418 _vfs_file_put(acceptor_data, acceptor_file);
419
420}
421
422int vfs_wait_handle_internal(void)
423{
424 vfs_client_data_t *vfs_data = VFS_DATA;
425 int fd;
426
427 fibril_mutex_lock(&vfs_data->lock);
428 while (list_empty(&vfs_data->passed_handles))
429 fibril_condvar_wait(&vfs_data->cv, &vfs_data->lock);
430 link_t *lnk = list_first(&vfs_data->passed_handles);
431 list_remove(lnk);
432 fibril_mutex_unlock(&vfs_data->lock);
433
434 vfs_boxed_handle_t *bh = list_get_instance(lnk, vfs_boxed_handle_t, link);
435 fd = bh->handle;
436 free(bh);
437
438 return fd;
439}
440
441/**
442 * @}
443 */
Note: See TracBrowser for help on using the repository browser.