Rev 4432 | Rev 4528 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4432 | Rev 4518 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2007 Jakub Jermar |
2 | * Copyright (c) 2007 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file vfs_file.c |
34 | * @file vfs_file.c |
35 | * @brief Various operations on files have their home in this file. |
35 | * @brief Various operations on files have their home in this file. |
36 | */ |
36 | */ |
37 | 37 | ||
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_sync.h> |
|
43 | #include "vfs.h" |
44 | #include "vfs.h" |
44 | 45 | ||
45 | /** |
46 | /** |
46 | * This is a per-connection table of open files. |
47 | * This is a per-connection table of open files. |
47 | * Our assumption is that each client opens only one connection and therefore |
48 | * Our assumption is that each client opens only one connection and therefore |
48 | * there is one table of open files per task. However, this may not be the case |
49 | * there is one table of open files per task. However, this may not be the case |
49 | * and the client can open more connections to VFS. In that case, there will be |
50 | * and the client can open more connections to VFS. In that case, there will be |
50 | * several tables and several file handle name spaces per task. Besides of this, |
51 | * several tables and several file handle name spaces per task. Besides of this, |
51 | * the functionality will stay unchanged. So unless the client knows what it is |
52 | * the functionality will stay unchanged. So unless the client knows what it is |
52 | * doing, it should open one connection to VFS only. |
53 | * doing, it should open one connection to VFS only. |
53 | * |
54 | * |
54 | * Allocation of the open files table is deferred until the client makes the |
55 | * Allocation of the open files table is deferred until the client makes the |
55 | * first VFS_OPEN operation. |
56 | * first VFS_OPEN operation. |
56 | * |
57 | * |
57 | * This resource being per-connection and, in the first place, per-fibril, we |
58 | * This resource being per-connection and, in the first place, per-fibril, we |
58 | * don't need to protect it by a futex. |
59 | * don't need to protect it by a futex. |
59 | */ |
60 | */ |
60 | __thread vfs_file_t **files = NULL; |
61 | __thread vfs_file_t **files = NULL; |
61 | 62 | ||
62 | /** Initialize the table of open files. */ |
63 | /** Initialize the table of open files. */ |
63 | bool vfs_files_init(void) |
64 | bool vfs_files_init(void) |
64 | { |
65 | { |
65 | if (!files) { |
66 | if (!files) { |
66 | files = malloc(MAX_OPEN_FILES * sizeof(vfs_file_t *)); |
67 | files = malloc(MAX_OPEN_FILES * sizeof(vfs_file_t *)); |
67 | if (!files) |
68 | if (!files) |
68 | return false; |
69 | return false; |
69 | memset(files, 0, MAX_OPEN_FILES * sizeof(vfs_file_t *)); |
70 | memset(files, 0, MAX_OPEN_FILES * sizeof(vfs_file_t *)); |
70 | } |
71 | } |
71 | return true; |
72 | return true; |
72 | } |
73 | } |
73 | 74 | ||
74 | /** Allocate a file descriptor. |
75 | /** Allocate a file descriptor. |
75 | * |
76 | * |
76 | * @return First available file descriptor or a negative error |
77 | * @return First available file descriptor or a negative error |
77 | * code. |
78 | * code. |
78 | */ |
79 | */ |
79 | int vfs_fd_alloc(void) |
80 | int vfs_fd_alloc(void) |
80 | { |
81 | { |
81 | if (!vfs_files_init()) |
82 | if (!vfs_files_init()) |
82 | return ENOMEM; |
83 | return ENOMEM; |
83 | 84 | ||
84 | unsigned int i; |
85 | unsigned int i; |
85 | for (i = 0; i < MAX_OPEN_FILES; i++) { |
86 | for (i = 0; i < MAX_OPEN_FILES; i++) { |
86 | if (!files[i]) { |
87 | if (!files[i]) { |
87 | files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t)); |
88 | files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t)); |
88 | if (!files[i]) |
89 | if (!files[i]) |
89 | return ENOMEM; |
90 | return ENOMEM; |
90 | 91 | ||
91 | memset(files[i], 0, sizeof(vfs_file_t)); |
92 | memset(files[i], 0, sizeof(vfs_file_t)); |
92 | futex_initialize(&files[i]->lock, 1); |
93 | fibril_mutex_initialize(&files[i]->lock); |
93 | vfs_file_addref(files[i]); |
94 | vfs_file_addref(files[i]); |
94 | return (int) i; |
95 | return (int) i; |
95 | } |
96 | } |
96 | } |
97 | } |
97 | 98 | ||
98 | return EMFILE; |
99 | return EMFILE; |
99 | } |
100 | } |
100 | 101 | ||
101 | /** Release file descriptor. |
102 | /** Release file descriptor. |
102 | * |
103 | * |
103 | * @param fd File descriptor being released. |
104 | * @param fd File descriptor being released. |
104 | * |
105 | * |
105 | * @return EOK on success or EBADF if fd is an invalid file |
106 | * @return EOK on success or EBADF if fd is an invalid file |
106 | * descriptor. |
107 | * descriptor. |
107 | */ |
108 | */ |
108 | int vfs_fd_free(int fd) |
109 | int vfs_fd_free(int fd) |
109 | { |
110 | { |
110 | if (!vfs_files_init()) |
111 | if (!vfs_files_init()) |
111 | return ENOMEM; |
112 | return ENOMEM; |
112 | 113 | ||
113 | if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL)) |
114 | if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL)) |
114 | return EBADF; |
115 | return EBADF; |
115 | 116 | ||
116 | vfs_file_delref(files[fd]); |
117 | vfs_file_delref(files[fd]); |
117 | files[fd] = NULL; |
118 | files[fd] = NULL; |
118 | 119 | ||
119 | return EOK; |
120 | return EOK; |
120 | } |
121 | } |
121 | 122 | ||
122 | /** Increment reference count of VFS file structure. |
123 | /** Increment reference count of VFS file structure. |
123 | * |
124 | * |
124 | * @param file File structure that will have reference count |
125 | * @param file File structure that will have reference count |
125 | * incremented. |
126 | * incremented. |
126 | */ |
127 | */ |
127 | void vfs_file_addref(vfs_file_t *file) |
128 | void vfs_file_addref(vfs_file_t *file) |
128 | { |
129 | { |
129 | /* |
130 | /* |
130 | * File structures are per-connection, so no-one, except the current |
131 | * File structures are per-connection, so no-one, except the current |
131 | * fibril, should have a reference to them. This is the reason we don't |
132 | * fibril, should have a reference to them. This is the reason we don't |
132 | * do any synchronization here. |
133 | * do any synchronization here. |
133 | */ |
134 | */ |
134 | file->refcnt++; |
135 | file->refcnt++; |
135 | } |
136 | } |
136 | 137 | ||
137 | /** Decrement reference count of VFS file structure. |
138 | /** Decrement reference count of VFS file structure. |
138 | * |
139 | * |
139 | * @param file File structure that will have reference count |
140 | * @param file File structure that will have reference count |
140 | * decremented. |
141 | * decremented. |
141 | */ |
142 | */ |
142 | void vfs_file_delref(vfs_file_t *file) |
143 | void vfs_file_delref(vfs_file_t *file) |
143 | { |
144 | { |
144 | if (file->refcnt-- == 1) { |
145 | if (file->refcnt-- == 1) { |
145 | /* |
146 | /* |
146 | * Lost the last reference to a file, need to drop our reference |
147 | * Lost the last reference to a file, need to drop our reference |
147 | * to the underlying VFS node. |
148 | * to the underlying VFS node. |
148 | */ |
149 | */ |
149 | vfs_node_delref(file->node); |
150 | vfs_node_delref(file->node); |
150 | free(file); |
151 | free(file); |
151 | } |
152 | } |
152 | } |
153 | } |
153 | 154 | ||
154 | /** Find VFS file structure for a given file descriptor. |
155 | /** Find VFS file structure for a given file descriptor. |
155 | * |
156 | * |
156 | * @param fd File descriptor. |
157 | * @param fd File descriptor. |
157 | * |
158 | * |
158 | * @return VFS file structure corresponding to fd. |
159 | * @return VFS file structure corresponding to fd. |
159 | */ |
160 | */ |
160 | vfs_file_t *vfs_file_get(int fd) |
161 | vfs_file_t *vfs_file_get(int fd) |
161 | { |
162 | { |
162 | if (!vfs_files_init()) |
163 | if (!vfs_files_init()) |
163 | return NULL; |
164 | return NULL; |
164 | 165 | ||
165 | if ((fd >= 0) && (fd < MAX_OPEN_FILES)) |
166 | if ((fd >= 0) && (fd < MAX_OPEN_FILES)) |
166 | return files[fd]; |
167 | return files[fd]; |
167 | 168 | ||
168 | return NULL; |
169 | return NULL; |
169 | } |
170 | } |
170 | 171 | ||
171 | /** |
172 | /** |
172 | * @} |
173 | * @} |
173 | */ |
174 | */ |
174 | 175 |