Subversion Repositories HelenOS

Rev

Rev 4389 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4389 Rev 4691
Line 38... Line 38...
38
#include <errno.h>
38
#include <errno.h>
39
#include <stdlib.h>
39
#include <stdlib.h>
40
#include <string.h>
40
#include <string.h>
41
#include <assert.h>
41
#include <assert.h>
42
#include <bool.h>
42
#include <bool.h>
-
 
43
#include <fibril.h>
-
 
44
#include <fibril_sync.h>
43
#include "vfs.h"
45
#include "vfs.h"
44
 
46
 
45
/**
47
/**
46
 * This is a per-connection table of open files.
48
 * This is a per-connection table of open files.
47
 * Our assumption is that each client opens only one connection and therefore
49
 * Our assumption is that each client opens only one connection and therefore
Line 53... Line 55...
53
 *
55
 *
54
 * Allocation of the open files table is deferred until the client makes the
56
 * Allocation of the open files table is deferred until the client makes the
55
 * first VFS_OPEN operation.
57
 * first VFS_OPEN operation.
56
 *
58
 *
57
 * This resource being per-connection and, in the first place, per-fibril, we
59
 * This resource being per-connection and, in the first place, per-fibril, we
58
 * don't need to protect it by a futex.
60
 * don't need to protect it by a mutex.
59
 */
61
 */
60
__thread vfs_file_t **files = NULL;
62
fibril_local vfs_file_t **files = NULL;
61
 
63
 
62
/** Initialize the table of open files. */
64
/** Initialize the table of open files. */
63
bool vfs_files_init(void)
65
bool vfs_files_init(void)
64
{
66
{
65
    if (!files) {
67
    if (!files) {
Line 76... Line 78...
76
 * @return      First available file descriptor or a negative error
78
 * @return      First available file descriptor or a negative error
77
 *          code.
79
 *          code.
78
 */
80
 */
79
int vfs_fd_alloc(void)
81
int vfs_fd_alloc(void)
80
{
82
{
-
 
83
    if (!vfs_files_init())
81
    int i;
84
        return ENOMEM;
82
 
85
   
-
 
86
    unsigned int i;
83
    for (i = 0; i < MAX_OPEN_FILES; i++) {
87
    for (i = 0; i < MAX_OPEN_FILES; i++) {
84
        if (!files[i]) {
88
        if (!files[i]) {
85
            files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
89
            files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
86
            if (!files[i])
90
            if (!files[i])
87
                return ENOMEM;
91
                return ENOMEM;
-
 
92
           
88
            memset(files[i], 0, sizeof(vfs_file_t));
93
            memset(files[i], 0, sizeof(vfs_file_t));
89
            futex_initialize(&files[i]->lock, 1);
94
            fibril_mutex_initialize(&files[i]->lock);
90
            vfs_file_addref(files[i]);
95
            vfs_file_addref(files[i]);
91
            return i;
96
            return (int) i;
92
        }
97
        }
93
    }
98
    }
-
 
99
   
94
    return EMFILE;
100
    return EMFILE;
95
}
101
}
96
 
102
 
97
/** Release file descriptor.
103
/** Release file descriptor.
98
 *
104
 *
Line 101... Line 107...
101
 * @return      EOK on success or EBADF if fd is an invalid file
107
 * @return      EOK on success or EBADF if fd is an invalid file
102
 *          descriptor.
108
 *          descriptor.
103
 */
109
 */
104
int vfs_fd_free(int fd)
110
int vfs_fd_free(int fd)
105
{
111
{
-
 
112
    if (!vfs_files_init())
-
 
113
        return ENOMEM;
-
 
114
   
106
    if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL))
115
    if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL))
107
        return EBADF;
116
        return EBADF;
-
 
117
   
108
    vfs_file_delref(files[fd]);
118
    vfs_file_delref(files[fd]);
109
    files[fd] = NULL;
119
    files[fd] = NULL;
-
 
120
   
110
    return EOK;
121
    return EOK;
111
}
122
}
112
 
123
 
113
/** Increment reference count of VFS file structure.
124
/** Increment reference count of VFS file structure.
114
 *
125
 *
Line 148... Line 159...
148
 *
159
 *
149
 * @return      VFS file structure corresponding to fd.
160
 * @return      VFS file structure corresponding to fd.
150
 */
161
 */
151
vfs_file_t *vfs_file_get(int fd)
162
vfs_file_t *vfs_file_get(int fd)
152
{
163
{
-
 
164
    if (!vfs_files_init())
-
 
165
        return NULL;
-
 
166
   
153
    if ((fd >= 0) && (fd < MAX_OPEN_FILES))
167
    if ((fd >= 0) && (fd < MAX_OPEN_FILES))
154
        return files[fd];
168
        return files[fd];
-
 
169
   
155
    return NULL;
170
    return NULL;
156
}
171
}
157
 
172
 
158
/**
173
/**
159
 * @}
174
 * @}
160
 */
175
 */