source: mainline/uspace/srv/vfs/vfs_file.c@ 2ba48bf1

Last change on this file since 2ba48bf1 was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

  • Property mode set to 100644
File size: 10.7 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 vfs
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/vfs.h>
48#include "vfs.h"
49
50#define VFS_DATA ((vfs_client_data_t *) async_get_client_data())
51#define FILES (VFS_DATA->files)
52
53typedef struct {
54 fibril_mutex_t lock;
55 fibril_condvar_t cv;
56 list_t passed_handles;
57 vfs_file_t **files;
58} vfs_client_data_t;
59
60typedef struct {
61 link_t link;
62 vfs_node_t *node;
63 int permissions;
64} vfs_boxed_handle_t;
65
66static errno_t _vfs_fd_free(vfs_client_data_t *, int);
67
68/** Initialize the table of open files. */
69static bool vfs_files_init(vfs_client_data_t *vfs_data)
70{
71 fibril_mutex_lock(&vfs_data->lock);
72 if (!vfs_data->files) {
73 vfs_data->files = malloc(VFS_MAX_OPEN_FILES * sizeof(vfs_file_t *));
74 if (!vfs_data->files) {
75 fibril_mutex_unlock(&vfs_data->lock);
76 return false;
77 }
78 memset(vfs_data->files, 0, VFS_MAX_OPEN_FILES * sizeof(vfs_file_t *));
79 }
80 fibril_mutex_unlock(&vfs_data->lock);
81 return true;
82}
83
84/** Cleanup the table of open files. */
85static void vfs_files_done(vfs_client_data_t *vfs_data)
86{
87 int i;
88
89 if (!vfs_data->files)
90 return;
91
92 for (i = 0; i < VFS_MAX_OPEN_FILES; i++) {
93 if (vfs_data->files[i])
94 (void) _vfs_fd_free(vfs_data, i);
95 }
96
97 free(vfs_data->files);
98
99 while (!list_empty(&vfs_data->passed_handles)) {
100 link_t *lnk;
101 vfs_boxed_handle_t *bh;
102
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 }
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);
118 fibril_condvar_initialize(&vfs_data->cv);
119 list_initialize(&vfs_data->passed_handles);
120 vfs_data->files = NULL;
121 }
122
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
130 vfs_files_done(vfs_data);
131 free(vfs_data);
132}
133
134/** Close the file in the endpoint FS server. */
135static errno_t vfs_file_close_remote(vfs_file_t *file)
136{
137 assert(!file->refcnt);
138
139 async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
140
141 ipc_call_t answer;
142 aid_t msg = async_send_2(exch, VFS_OUT_CLOSE, file->node->service_id,
143 file->node->index, &answer);
144
145 vfs_exchange_release(exch);
146
147 errno_t rc;
148 async_wait_for(msg, &rc);
149
150 return ipc_get_retval(&answer);
151}
152
153/** Increment reference count of VFS file structure.
154 *
155 * @param file File structure that will have reference count
156 * incremented.
157 */
158static void vfs_file_addref(vfs_client_data_t *vfs_data, vfs_file_t *file)
159{
160 assert(fibril_mutex_is_locked(&vfs_data->lock));
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 */
170static errno_t vfs_file_delref(vfs_client_data_t *vfs_data, vfs_file_t *file)
171{
172 errno_t rc = EOK;
173
174 assert(fibril_mutex_is_locked(&vfs_data->lock));
175
176 if (file->refcnt-- == 1) {
177 /*
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.
180 */
181
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);
187 }
188 free(file);
189 }
190
191 return rc;
192}
193
194static errno_t _vfs_fd_alloc(vfs_client_data_t *vfs_data, vfs_file_t **file, bool desc, int *out_fd)
195{
196 if (!vfs_files_init(vfs_data))
197 return ENOMEM;
198
199 unsigned int i;
200 if (desc)
201 i = VFS_MAX_OPEN_FILES - 1;
202 else
203 i = 0;
204
205 fibril_mutex_lock(&vfs_data->lock);
206 while (true) {
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);
211 return ENOMEM;
212 }
213
214 memset(vfs_data->files[i], 0, sizeof(vfs_file_t));
215
216 fibril_mutex_initialize(&vfs_data->files[i]->_lock);
217 fibril_mutex_lock(&vfs_data->files[i]->_lock);
218 vfs_file_addref(vfs_data, vfs_data->files[i]);
219
220 *file = vfs_data->files[i];
221 vfs_file_addref(vfs_data, *file);
222
223 fibril_mutex_unlock(&vfs_data->lock);
224 *out_fd = (int) i;
225 return EOK;
226 }
227
228 if (desc) {
229 if (i == 0)
230 break;
231
232 i--;
233 } else {
234 if (i == VFS_MAX_OPEN_FILES - 1)
235 break;
236
237 i++;
238 }
239 }
240 fibril_mutex_unlock(&vfs_data->lock);
241
242 return EMFILE;
243}
244
245/** Allocate a file descriptor.
246 *
247 * @param file Is set to point to the newly created file structure. Must be put afterwards.
248 * @param desc If true, look for an available file descriptor
249 * in a descending order.
250 *
251 * @param[out] out_fd First available file descriptor
252 *
253 * @return Error code.
254 */
255errno_t vfs_fd_alloc(vfs_file_t **file, bool desc, int *out_fd)
256{
257 return _vfs_fd_alloc(VFS_DATA, file, desc, out_fd);
258}
259
260static errno_t _vfs_fd_free_locked(vfs_client_data_t *vfs_data, int fd)
261{
262 if ((fd < 0) || (fd >= VFS_MAX_OPEN_FILES) || !vfs_data->files[fd]) {
263 return EBADF;
264 }
265
266 errno_t rc = vfs_file_delref(vfs_data, vfs_data->files[fd]);
267 vfs_data->files[fd] = NULL;
268 return rc;
269}
270
271static errno_t _vfs_fd_free(vfs_client_data_t *vfs_data, int fd)
272{
273 errno_t rc;
274
275 if (!vfs_files_init(vfs_data))
276 return ENOMEM;
277
278 fibril_mutex_lock(&vfs_data->lock);
279 rc = _vfs_fd_free_locked(vfs_data, fd);
280 fibril_mutex_unlock(&vfs_data->lock);
281
282 return rc;
283}
284
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 */
292errno_t vfs_fd_free(int fd)
293{
294 return _vfs_fd_free(VFS_DATA, fd);
295}
296
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 */
306errno_t vfs_fd_assign(vfs_file_t *file, int fd)
307{
308 if (!vfs_files_init(VFS_DATA))
309 return ENOMEM;
310
311 fibril_mutex_lock(&VFS_DATA->lock);
312 if ((fd < 0) || (fd >= VFS_MAX_OPEN_FILES)) {
313 fibril_mutex_unlock(&VFS_DATA->lock);
314 return EBADF;
315 }
316
317 /* Make sure fd is closed. */
318 (void) _vfs_fd_free_locked(VFS_DATA, fd);
319 assert(FILES[fd] == NULL);
320
321 FILES[fd] = file;
322 vfs_file_addref(VFS_DATA, FILES[fd]);
323 fibril_mutex_unlock(&VFS_DATA->lock);
324
325 return EOK;
326}
327
328static void _vfs_file_put(vfs_client_data_t *vfs_data, vfs_file_t *file)
329{
330 fibril_mutex_unlock(&file->_lock);
331
332 fibril_mutex_lock(&vfs_data->lock);
333 vfs_file_delref(vfs_data, file);
334 fibril_mutex_unlock(&vfs_data->lock);
335}
336
337static vfs_file_t *_vfs_file_get(vfs_client_data_t *vfs_data, int fd)
338{
339 if (!vfs_files_init(vfs_data))
340 return NULL;
341
342 fibril_mutex_lock(&vfs_data->lock);
343 if ((fd >= 0) && (fd < VFS_MAX_OPEN_FILES)) {
344 vfs_file_t *file = vfs_data->files[fd];
345 if (file != NULL) {
346 vfs_file_addref(vfs_data, file);
347 fibril_mutex_unlock(&vfs_data->lock);
348
349 fibril_mutex_lock(&file->_lock);
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);
356 return file;
357 }
358 }
359 fibril_mutex_unlock(&vfs_data->lock);
360
361 return NULL;
362}
363
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 */
370vfs_file_t *vfs_file_get(int fd)
371{
372 return _vfs_file_get(VFS_DATA, fd);
373}
374
375/** Stop using a file structure.
376 *
377 * @param file VFS file structure.
378 */
379void vfs_file_put(vfs_file_t *file)
380{
381 _vfs_file_put(VFS_DATA, file);
382}
383
384void vfs_op_pass_handle(task_id_t donor_id, task_id_t acceptor_id, int donor_fd)
385{
386 vfs_client_data_t *donor_data = NULL;
387 vfs_client_data_t *acceptor_data = NULL;
388 vfs_file_t *donor_file = NULL;
389 vfs_boxed_handle_t *bh;
390
391 acceptor_data = async_get_client_data_by_id(acceptor_id);
392 if (!acceptor_data)
393 return;
394
395 bh = malloc(sizeof(vfs_boxed_handle_t));
396 assert(bh);
397
398 link_initialize(&bh->link);
399 bh->node = NULL;
400
401 donor_data = async_get_client_data_by_id(donor_id);
402 if (!donor_data)
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);
413 bh->node = donor_file->node;
414 bh->permissions = donor_file->permissions;
415
416out:
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
422 if (donor_data)
423 async_put_client_data_by_id(donor_id);
424 if (acceptor_data)
425 async_put_client_data_by_id(acceptor_id);
426 if (donor_file)
427 _vfs_file_put(donor_data, donor_file);
428}
429
430errno_t vfs_wait_handle_internal(bool high_fd, int *out_fd)
431{
432 vfs_client_data_t *vfs_data = VFS_DATA;
433
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
443 vfs_file_t *file;
444 errno_t rc = _vfs_fd_alloc(vfs_data, &file, high_fd, out_fd);
445 if (rc != EOK) {
446 vfs_node_delref(bh->node);
447 free(bh);
448 return rc;
449 }
450
451 file->node = bh->node;
452 file->permissions = bh->permissions;
453 vfs_file_put(file);
454 free(bh);
455 return EOK;
456}
457
458/**
459 * @}
460 */
Note: See TracBrowser for help on using the repository browser.