source: mainline/uspace/srv/vfs/vfs_file.c@ 9ae22ba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9ae22ba was 230260ac, checked in by Jakub Jermar <jakub@…>, 16 years ago

Make VFS use the new synchronization for fibrils. Now there should be no (or
only secondary) fibril serialization. Code reorganized not to hold the phone
lock during async_wait_for() in most cases. Tested on ia32. On amd64, VFS
crashes, but I think it is an unrelated problem.

  • Property mode set to 100644
File size: 4.6 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 <string.h>
41#include <assert.h>
42#include <bool.h>
43#include <fibril_sync.h>
44#include "vfs.h"
45
46/**
47 * This is a per-connection table of open files.
48 * Our assumption is that each client opens only one connection and therefore
49 * there is one table of open files per task. However, this may not be the case
50 * and the client can open more connections to VFS. In that case, there will be
51 * several tables and several file handle name spaces per task. Besides of this,
52 * the functionality will stay unchanged. So unless the client knows what it is
53 * doing, it should open one connection to VFS only.
54 *
55 * Allocation of the open files table is deferred until the client makes the
56 * first VFS_OPEN operation.
57 *
58 * This resource being per-connection and, in the first place, per-fibril, we
59 * don't need to protect it by a futex.
60 */
61__thread vfs_file_t **files = NULL;
62
63/** Initialize the table of open files. */
64bool vfs_files_init(void)
65{
66 if (!files) {
67 files = malloc(MAX_OPEN_FILES * sizeof(vfs_file_t *));
68 if (!files)
69 return false;
70 memset(files, 0, MAX_OPEN_FILES * sizeof(vfs_file_t *));
71 }
72 return true;
73}
74
75/** Allocate a file descriptor.
76 *
77 * @return First available file descriptor or a negative error
78 * code.
79 */
80int vfs_fd_alloc(void)
81{
82 if (!vfs_files_init())
83 return ENOMEM;
84
85 unsigned int i;
86 for (i = 0; i < MAX_OPEN_FILES; i++) {
87 if (!files[i]) {
88 files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
89 if (!files[i])
90 return ENOMEM;
91
92 memset(files[i], 0, sizeof(vfs_file_t));
93 fibril_mutex_initialize(&files[i]->lock);
94 vfs_file_addref(files[i]);
95 return (int) i;
96 }
97 }
98
99 return EMFILE;
100}
101
102/** Release file descriptor.
103 *
104 * @param fd File descriptor being released.
105 *
106 * @return EOK on success or EBADF if fd is an invalid file
107 * descriptor.
108 */
109int vfs_fd_free(int fd)
110{
111 if (!vfs_files_init())
112 return ENOMEM;
113
114 if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL))
115 return EBADF;
116
117 vfs_file_delref(files[fd]);
118 files[fd] = NULL;
119
120 return EOK;
121}
122
123/** Increment reference count of VFS file structure.
124 *
125 * @param file File structure that will have reference count
126 * incremented.
127 */
128void vfs_file_addref(vfs_file_t *file)
129{
130 /*
131 * File structures are per-connection, so no-one, except the current
132 * fibril, should have a reference to them. This is the reason we don't
133 * do any synchronization here.
134 */
135 file->refcnt++;
136}
137
138/** Decrement reference count of VFS file structure.
139 *
140 * @param file File structure that will have reference count
141 * decremented.
142 */
143void vfs_file_delref(vfs_file_t *file)
144{
145 if (file->refcnt-- == 1) {
146 /*
147 * Lost the last reference to a file, need to drop our reference
148 * to the underlying VFS node.
149 */
150 vfs_node_delref(file->node);
151 free(file);
152 }
153}
154
155/** Find VFS file structure for a given file descriptor.
156 *
157 * @param fd File descriptor.
158 *
159 * @return VFS file structure corresponding to fd.
160 */
161vfs_file_t *vfs_file_get(int fd)
162{
163 if (!vfs_files_init())
164 return NULL;
165
166 if ((fd >= 0) && (fd < MAX_OPEN_FILES))
167 return files[fd];
168
169 return NULL;
170}
171
172/**
173 * @}
174 */
Note: See TracBrowser for help on using the repository browser.