Rev 4537 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4537 | Rev 4668 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Martin Decky |
2 | * Copyright (c) 2009 Martin Decky |
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 devfs_ops.c |
34 | * @file devfs_ops.c |
35 | * @brief Implementation of VFS operations for the devfs file system server. |
35 | * @brief Implementation of VFS operations for the devfs file system server. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <ipc/ipc.h> |
38 | #include <ipc/ipc.h> |
39 | #include <bool.h> |
39 | #include <bool.h> |
40 | #include <errno.h> |
40 | #include <errno.h> |
41 | #include <malloc.h> |
41 | #include <malloc.h> |
42 | #include <string.h> |
42 | #include <string.h> |
43 | #include <libfs.h> |
43 | #include <libfs.h> |
- | 44 | #include <fibril_sync.h> |
|
44 | #include <adt/hash_table.h> |
45 | #include <adt/hash_table.h> |
- | 46 | #include <sys/stat.h> |
|
45 | #include "devfs.h" |
47 | #include "devfs.h" |
46 | #include "devfs_ops.h" |
48 | #include "devfs_ops.h" |
47 | 49 | ||
48 | #define PLB_GET_CHAR(pos) (devfs_reg.plb_ro[pos % PLB_SIZE]) |
50 | #define PLB_GET_CHAR(pos) (devfs_reg.plb_ro[pos % PLB_SIZE]) |
49 | 51 | ||
50 | /** Opened devices structure */ |
52 | /** Opened devices structure */ |
51 | typedef struct { |
53 | typedef struct { |
52 | dev_handle_t handle; |
54 | dev_handle_t handle; |
53 | int phone; |
55 | int phone; |
54 | size_t refcount; |
56 | size_t refcount; |
55 | link_t link; |
57 | link_t link; |
56 | } device_t; |
58 | } device_t; |
57 | 59 | ||
58 | /** Hash table of opened devices */ |
60 | /** Hash table of opened devices */ |
59 | static hash_table_t devices; |
61 | static hash_table_t devices; |
60 | 62 | ||
- | 63 | /** Hash table mutex */ |
|
- | 64 | static FIBRIL_MUTEX_INITIALIZE(devices_mutex); |
|
- | 65 | ||
61 | #define DEVICES_KEYS 1 |
66 | #define DEVICES_KEYS 1 |
62 | #define DEVICES_KEY_HANDLE 0 |
67 | #define DEVICES_KEY_HANDLE 0 |
63 | #define DEVICES_BUCKETS 256 |
68 | #define DEVICES_BUCKETS 256 |
64 | 69 | ||
65 | /* Implementation of hash table interface for the nodes hash table. */ |
70 | /* Implementation of hash table interface for the nodes hash table. */ |
66 | static hash_index_t devices_hash(unsigned long key[]) |
71 | static hash_index_t devices_hash(unsigned long key[]) |
67 | { |
72 | { |
68 | return key[DEVICES_KEY_HANDLE] % DEVICES_BUCKETS; |
73 | return key[DEVICES_KEY_HANDLE] % DEVICES_BUCKETS; |
69 | } |
74 | } |
70 | 75 | ||
71 | static int devices_compare(unsigned long key[], hash_count_t keys, link_t *item) |
76 | static int devices_compare(unsigned long key[], hash_count_t keys, link_t *item) |
72 | { |
77 | { |
73 | device_t *dev = hash_table_get_instance(item, device_t, link); |
78 | device_t *dev = hash_table_get_instance(item, device_t, link); |
74 | return (dev->handle == (dev_handle_t) key[DEVICES_KEY_HANDLE]); |
79 | return (dev->handle == (dev_handle_t) key[DEVICES_KEY_HANDLE]); |
75 | } |
80 | } |
76 | 81 | ||
77 | static void devices_remove_callback(link_t *item) |
82 | static void devices_remove_callback(link_t *item) |
78 | { |
83 | { |
79 | free(hash_table_get_instance(item, device_t, link)); |
84 | free(hash_table_get_instance(item, device_t, link)); |
80 | } |
85 | } |
81 | 86 | ||
82 | static hash_table_operations_t devices_ops = { |
87 | static hash_table_operations_t devices_ops = { |
83 | .hash = devices_hash, |
88 | .hash = devices_hash, |
84 | .compare = devices_compare, |
89 | .compare = devices_compare, |
85 | .remove_callback = devices_remove_callback |
90 | .remove_callback = devices_remove_callback |
86 | }; |
91 | }; |
87 | 92 | ||
88 | bool devfs_init(void) |
93 | bool devfs_init(void) |
89 | { |
94 | { |
90 | if (!hash_table_create(&devices, DEVICES_BUCKETS, |
95 | if (!hash_table_create(&devices, DEVICES_BUCKETS, |
91 | DEVICES_KEYS, &devices_ops)) |
96 | DEVICES_KEYS, &devices_ops)) |
92 | return false; |
97 | return false; |
93 | 98 | ||
94 | if (devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING) < 0) |
99 | if (devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING) < 0) |
95 | return false; |
100 | return false; |
96 | 101 | ||
97 | return true; |
102 | return true; |
98 | } |
103 | } |
99 | 104 | ||
100 | void devfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
105 | void devfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
101 | { |
106 | { |
102 | /* Accept the mount options */ |
107 | /* Accept the mount options */ |
103 | ipc_callid_t callid; |
108 | ipc_callid_t callid; |
104 | size_t size; |
109 | size_t size; |
105 | if (!ipc_data_write_receive(&callid, &size)) { |
110 | if (!ipc_data_write_receive(&callid, &size)) { |
106 | ipc_answer_0(callid, EINVAL); |
111 | ipc_answer_0(callid, EINVAL); |
107 | ipc_answer_0(rid, EINVAL); |
112 | ipc_answer_0(rid, EINVAL); |
108 | return; |
113 | return; |
109 | } |
114 | } |
110 | 115 | ||
111 | char *opts = malloc(size + 1); |
116 | char *opts = malloc(size + 1); |
112 | if (!opts) { |
117 | if (!opts) { |
113 | ipc_answer_0(callid, ENOMEM); |
118 | ipc_answer_0(callid, ENOMEM); |
114 | ipc_answer_0(rid, ENOMEM); |
119 | ipc_answer_0(rid, ENOMEM); |
115 | return; |
120 | return; |
116 | } |
121 | } |
117 | 122 | ||
118 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
123 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
119 | if (retval != EOK) { |
124 | if (retval != EOK) { |
120 | ipc_answer_0(rid, retval); |
125 | ipc_answer_0(rid, retval); |
121 | free(opts); |
126 | free(opts); |
122 | return; |
127 | return; |
123 | } |
128 | } |
124 | 129 | ||
125 | free(opts); |
130 | free(opts); |
126 | 131 | ||
127 | ipc_answer_3(rid, EOK, 0, 0, 0); |
132 | ipc_answer_3(rid, EOK, 0, 0, 0); |
128 | } |
133 | } |
129 | 134 | ||
130 | void devfs_mount(ipc_callid_t rid, ipc_call_t *request) |
135 | void devfs_mount(ipc_callid_t rid, ipc_call_t *request) |
131 | { |
136 | { |
132 | ipc_answer_0(rid, ENOTSUP); |
137 | ipc_answer_0(rid, ENOTSUP); |
133 | } |
138 | } |
134 | 139 | ||
135 | void devfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
140 | void devfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
136 | { |
141 | { |
137 | ipcarg_t first = IPC_GET_ARG1(*request); |
142 | ipcarg_t first = IPC_GET_ARG1(*request); |
138 | ipcarg_t last = IPC_GET_ARG2(*request); |
143 | ipcarg_t last = IPC_GET_ARG2(*request); |
139 | dev_handle_t dev_handle = IPC_GET_ARG3(*request); |
144 | dev_handle_t dev_handle = IPC_GET_ARG3(*request); |
140 | ipcarg_t lflag = IPC_GET_ARG4(*request); |
145 | ipcarg_t lflag = IPC_GET_ARG4(*request); |
141 | fs_index_t index = IPC_GET_ARG5(*request); |
146 | fs_index_t index = IPC_GET_ARG5(*request); |
142 | 147 | ||
143 | /* Hierarchy is flat, no altroot is supported */ |
148 | /* Hierarchy is flat, no altroot is supported */ |
144 | if (index != 0) { |
149 | if (index != 0) { |
145 | ipc_answer_0(rid, ENOENT); |
150 | ipc_answer_0(rid, ENOENT); |
146 | return; |
151 | return; |
147 | } |
152 | } |
148 | 153 | ||
149 | if ((lflag & L_LINK) || (lflag & L_UNLINK)) { |
154 | if ((lflag & L_LINK) || (lflag & L_UNLINK)) { |
150 | ipc_answer_0(rid, ENOTSUP); |
155 | ipc_answer_0(rid, ENOTSUP); |
151 | return; |
156 | return; |
152 | } |
157 | } |
153 | 158 | ||
154 | /* Eat slash */ |
159 | /* Eat slash */ |
155 | if (PLB_GET_CHAR(first) == '/') { |
160 | if (PLB_GET_CHAR(first) == '/') { |
156 | first++; |
161 | first++; |
157 | first %= PLB_SIZE; |
162 | first %= PLB_SIZE; |
158 | } |
163 | } |
159 | 164 | ||
160 | if (first >= last) { |
165 | if (first >= last) { |
161 | /* Root entry */ |
166 | /* Root entry */ |
162 | if (lflag & L_DIRECTORY) |
167 | if (!(lflag & L_FILE)) |
163 | ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, 0, 0, 0); |
168 | ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, 0, 0, 0); |
164 | else |
169 | else |
165 | ipc_answer_0(rid, ENOENT); |
170 | ipc_answer_0(rid, ENOENT); |
166 | } else { |
171 | } else { |
167 | if (lflag & L_FILE) { |
172 | if (!(lflag & L_DIRECTORY)) { |
168 | size_t len; |
173 | size_t len; |
169 | if (last >= first) |
174 | if (last >= first) |
170 | len = last - first + 1; |
175 | len = last - first + 1; |
171 | else |
176 | else |
172 | len = first + PLB_SIZE - last + 1; |
177 | len = first + PLB_SIZE - last + 1; |
173 | 178 | ||
174 | char *name = (char *) malloc(len + 1); |
179 | char *name = (char *) malloc(len + 1); |
175 | if (name == NULL) { |
180 | if (name == NULL) { |
176 | ipc_answer_0(rid, ENOMEM); |
181 | ipc_answer_0(rid, ENOMEM); |
177 | return; |
182 | return; |
178 | } |
183 | } |
179 | 184 | ||
180 | size_t i; |
185 | size_t i; |
181 | for (i = 0; i < len; i++) |
186 | for (i = 0; i < len; i++) |
182 | name[i] = PLB_GET_CHAR(first + i); |
187 | name[i] = PLB_GET_CHAR(first + i); |
183 | 188 | ||
184 | name[len] = 0; |
189 | name[len] = 0; |
185 | 190 | ||
186 | dev_handle_t handle; |
191 | dev_handle_t handle; |
187 | if (devmap_device_get_handle(name, &handle, 0) != EOK) { |
192 | if (devmap_device_get_handle(name, &handle, 0) != EOK) { |
188 | free(name); |
193 | free(name); |
189 | ipc_answer_0(rid, ENOENT); |
194 | ipc_answer_0(rid, ENOENT); |
190 | return; |
195 | return; |
191 | } |
196 | } |
192 | 197 | ||
193 | if (lflag & L_OPEN) { |
198 | if (lflag & L_OPEN) { |
194 | unsigned long key[] = { |
199 | unsigned long key[] = { |
195 | [DEVICES_KEY_HANDLE] = (unsigned long) handle |
200 | [DEVICES_KEY_HANDLE] = (unsigned long) handle |
196 | }; |
201 | }; |
197 | 202 | ||
- | 203 | fibril_mutex_lock(&devices_mutex); |
|
198 | link_t *lnk = hash_table_find(&devices, key); |
204 | link_t *lnk = hash_table_find(&devices, key); |
199 | if (lnk == NULL) { |
205 | if (lnk == NULL) { |
200 | int phone = devmap_device_connect(handle, 0); |
206 | int phone = devmap_device_connect(handle, 0); |
201 | if (phone < 0) { |
207 | if (phone < 0) { |
- | 208 | fibril_mutex_unlock(&devices_mutex); |
|
202 | free(name); |
209 | free(name); |
203 | ipc_answer_0(rid, ENOENT); |
210 | ipc_answer_0(rid, ENOENT); |
204 | return; |
211 | return; |
205 | } |
212 | } |
206 | 213 | ||
207 | device_t *dev = (device_t *) malloc(sizeof(device_t)); |
214 | device_t *dev = (device_t *) malloc(sizeof(device_t)); |
208 | if (dev == NULL) { |
215 | if (dev == NULL) { |
- | 216 | fibril_mutex_unlock(&devices_mutex); |
|
209 | free(name); |
217 | free(name); |
210 | ipc_answer_0(rid, ENOMEM); |
218 | ipc_answer_0(rid, ENOMEM); |
211 | return; |
219 | return; |
212 | } |
220 | } |
213 | 221 | ||
214 | dev->handle = handle; |
222 | dev->handle = handle; |
215 | dev->phone = phone; |
223 | dev->phone = phone; |
216 | dev->refcount = 1; |
224 | dev->refcount = 1; |
217 | 225 | ||
218 | hash_table_insert(&devices, key, &dev->link); |
226 | hash_table_insert(&devices, key, &dev->link); |
219 | } else { |
227 | } else { |
220 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
228 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
221 | dev->refcount++; |
229 | dev->refcount++; |
222 | } |
230 | } |
- | 231 | fibril_mutex_unlock(&devices_mutex); |
|
223 | } |
232 | } |
224 | 233 | ||
225 | free(name); |
234 | free(name); |
226 | 235 | ||
227 | ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, handle, 0, 1); |
236 | ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, handle, 0, 1); |
228 | } else |
237 | } else |
229 | ipc_answer_0(rid, ENOENT); |
238 | ipc_answer_0(rid, ENOENT); |
230 | } |
239 | } |
231 | } |
240 | } |
232 | 241 | ||
233 | void devfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
242 | void devfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
234 | { |
243 | { |
235 | dev_handle_t handle = IPC_GET_ARG2(*request); |
244 | dev_handle_t handle = IPC_GET_ARG2(*request); |
236 | 245 | ||
237 | unsigned long key[] = { |
246 | unsigned long key[] = { |
238 | [DEVICES_KEY_HANDLE] = (unsigned long) handle |
247 | [DEVICES_KEY_HANDLE] = (unsigned long) handle |
239 | }; |
248 | }; |
240 | 249 | ||
- | 250 | fibril_mutex_lock(&devices_mutex); |
|
241 | link_t *lnk = hash_table_find(&devices, key); |
251 | link_t *lnk = hash_table_find(&devices, key); |
242 | if (lnk == NULL) { |
252 | if (lnk == NULL) { |
243 | int phone = devmap_device_connect(handle, 0); |
253 | int phone = devmap_device_connect(handle, 0); |
244 | if (phone < 0) { |
254 | if (phone < 0) { |
- | 255 | fibril_mutex_unlock(&devices_mutex); |
|
245 | ipc_answer_0(rid, ENOENT); |
256 | ipc_answer_0(rid, ENOENT); |
246 | return; |
257 | return; |
247 | } |
258 | } |
248 | 259 | ||
249 | device_t *dev = (device_t *) malloc(sizeof(device_t)); |
260 | device_t *dev = (device_t *) malloc(sizeof(device_t)); |
250 | if (dev == NULL) { |
261 | if (dev == NULL) { |
- | 262 | fibril_mutex_unlock(&devices_mutex); |
|
251 | ipc_answer_0(rid, ENOMEM); |
263 | ipc_answer_0(rid, ENOMEM); |
252 | return; |
264 | return; |
253 | } |
265 | } |
254 | 266 | ||
255 | dev->handle = handle; |
267 | dev->handle = handle; |
256 | dev->phone = phone; |
268 | dev->phone = phone; |
257 | dev->refcount = 1; |
269 | dev->refcount = 1; |
258 | 270 | ||
259 | hash_table_insert(&devices, key, &dev->link); |
271 | hash_table_insert(&devices, key, &dev->link); |
260 | } else { |
272 | } else { |
261 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
273 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
262 | dev->refcount++; |
274 | dev->refcount++; |
263 | } |
275 | } |
- | 276 | fibril_mutex_unlock(&devices_mutex); |
|
264 | 277 | ||
265 | ipc_answer_3(rid, EOK, 0, 1, L_FILE); |
278 | ipc_answer_3(rid, EOK, 0, 1, L_FILE); |
266 | } |
279 | } |
267 | 280 | ||
268 | void devfs_device(ipc_callid_t rid, ipc_call_t *request) |
281 | void devfs_stat(ipc_callid_t rid, ipc_call_t *request) |
269 | { |
282 | { |
- | 283 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
|
270 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
284 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
271 | 285 | ||
- | 286 | ipc_callid_t callid; |
|
- | 287 | size_t size; |
|
- | 288 | if (!ipc_data_read_receive(&callid, &size) || |
|
- | 289 | size != sizeof(struct stat)) { |
|
- | 290 | ipc_answer_0(callid, EINVAL); |
|
- | 291 | ipc_answer_0(rid, EINVAL); |
|
- | 292 | return; |
|
- | 293 | } |
|
- | 294 | ||
- | 295 | struct stat stat; |
|
- | 296 | memset(&stat, 0, sizeof(struct stat)); |
|
- | 297 | ||
- | 298 | stat.fs_handle = devfs_reg.fs_handle; |
|
- | 299 | stat.dev_handle = dev_handle; |
|
- | 300 | stat.index = index; |
|
- | 301 | stat.lnkcnt = 1; |
|
- | 302 | stat.is_file = (index != 0); |
|
- | 303 | stat.size = 0; |
|
- | 304 | ||
272 | if (index != 0) { |
305 | if (index != 0) { |
273 | unsigned long key[] = { |
306 | unsigned long key[] = { |
274 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
307 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
275 | }; |
308 | }; |
276 | 309 | ||
- | 310 | fibril_mutex_lock(&devices_mutex); |
|
277 | link_t *lnk = hash_table_find(&devices, key); |
311 | link_t *lnk = hash_table_find(&devices, key); |
278 | if (lnk == NULL) { |
312 | if (lnk != NULL) |
279 | ipc_answer_0(rid, ENOENT); |
313 | stat.devfs_stat.device = (dev_handle_t)index; |
280 | return; |
314 | fibril_mutex_unlock(&devices_mutex); |
281 | } |
315 | } |
282 | 316 | ||
283 | ipc_answer_1(rid, EOK, (ipcarg_t) index); |
317 | ipc_data_read_finalize(callid, &stat, sizeof(struct stat)); |
284 | } else |
- | |
285 | ipc_answer_0(rid, ENOTSUP); |
318 | ipc_answer_0(rid, EOK); |
286 | } |
319 | } |
287 | 320 | ||
288 | void devfs_read(ipc_callid_t rid, ipc_call_t *request) |
321 | void devfs_read(ipc_callid_t rid, ipc_call_t *request) |
289 | { |
322 | { |
290 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
323 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
291 | off_t pos = (off_t) IPC_GET_ARG3(*request); |
324 | off_t pos = (off_t) IPC_GET_ARG3(*request); |
292 | 325 | ||
293 | if (index != 0) { |
326 | if (index != 0) { |
294 | unsigned long key[] = { |
327 | unsigned long key[] = { |
295 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
328 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
296 | }; |
329 | }; |
297 | 330 | ||
- | 331 | fibril_mutex_lock(&devices_mutex); |
|
298 | link_t *lnk = hash_table_find(&devices, key); |
332 | link_t *lnk = hash_table_find(&devices, key); |
299 | if (lnk == NULL) { |
333 | if (lnk == NULL) { |
- | 334 | fibril_mutex_unlock(&devices_mutex); |
|
300 | ipc_answer_0(rid, ENOENT); |
335 | ipc_answer_0(rid, ENOENT); |
301 | return; |
336 | return; |
302 | } |
337 | } |
303 | 338 | ||
304 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
339 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
305 | 340 | ||
306 | ipc_callid_t callid; |
341 | ipc_callid_t callid; |
307 | if (!ipc_data_read_receive(&callid, NULL)) { |
342 | if (!ipc_data_read_receive(&callid, NULL)) { |
- | 343 | fibril_mutex_unlock(&devices_mutex); |
|
308 | ipc_answer_0(callid, EINVAL); |
344 | ipc_answer_0(callid, EINVAL); |
309 | ipc_answer_0(rid, EINVAL); |
345 | ipc_answer_0(rid, EINVAL); |
310 | return; |
346 | return; |
311 | } |
347 | } |
312 | 348 | ||
313 | /* Make a request at the driver */ |
349 | /* Make a request at the driver */ |
314 | ipc_call_t answer; |
350 | ipc_call_t answer; |
315 | aid_t msg = async_send_3(dev->phone, IPC_GET_METHOD(*request), |
351 | aid_t msg = async_send_3(dev->phone, IPC_GET_METHOD(*request), |
316 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), |
352 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), |
317 | IPC_GET_ARG3(*request), &answer); |
353 | IPC_GET_ARG3(*request), &answer); |
318 | 354 | ||
319 | /* Forward the IPC_M_DATA_READ request to the driver */ |
355 | /* Forward the IPC_M_DATA_READ request to the driver */ |
320 | ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
356 | ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
- | 357 | fibril_mutex_unlock(&devices_mutex); |
|
321 | 358 | ||
322 | /* Wait for reply from the driver. */ |
359 | /* Wait for reply from the driver. */ |
323 | ipcarg_t rc; |
360 | ipcarg_t rc; |
324 | async_wait_for(msg, &rc); |
361 | async_wait_for(msg, &rc); |
325 | size_t bytes = IPC_GET_ARG1(answer); |
362 | size_t bytes = IPC_GET_ARG1(answer); |
326 | 363 | ||
327 | /* Driver reply is the final result of the whole operation */ |
364 | /* Driver reply is the final result of the whole operation */ |
328 | ipc_answer_1(rid, rc, bytes); |
365 | ipc_answer_1(rid, rc, bytes); |
329 | } else { |
366 | } else { |
330 | ipc_callid_t callid; |
367 | ipc_callid_t callid; |
331 | size_t size; |
368 | size_t size; |
332 | if (!ipc_data_read_receive(&callid, &size)) { |
369 | if (!ipc_data_read_receive(&callid, &size)) { |
333 | ipc_answer_0(callid, EINVAL); |
370 | ipc_answer_0(callid, EINVAL); |
334 | ipc_answer_0(rid, EINVAL); |
371 | ipc_answer_0(rid, EINVAL); |
335 | return; |
372 | return; |
336 | } |
373 | } |
337 | 374 | ||
338 | size_t count = devmap_device_get_count(); |
375 | size_t count = devmap_device_get_count(); |
339 | dev_desc_t *desc = malloc(count * sizeof(dev_desc_t)); |
376 | dev_desc_t *desc = malloc(count * sizeof(dev_desc_t)); |
340 | if (desc == NULL) { |
377 | if (desc == NULL) { |
341 | ipc_answer_0(callid, ENOMEM); |
378 | ipc_answer_0(callid, ENOMEM); |
342 | ipc_answer_1(rid, ENOMEM, 0); |
379 | ipc_answer_1(rid, ENOMEM, 0); |
343 | return; |
380 | return; |
344 | } |
381 | } |
345 | 382 | ||
346 | size_t max = devmap_device_get_devices(count, desc); |
383 | size_t max = devmap_device_get_devices(count, desc); |
347 | 384 | ||
348 | if (pos < max) { |
385 | if (pos < max) { |
349 | ipc_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1); |
386 | ipc_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1); |
350 | } else { |
387 | } else { |
351 | ipc_answer_0(callid, ENOENT); |
388 | ipc_answer_0(callid, ENOENT); |
352 | ipc_answer_1(rid, ENOENT, 0); |
389 | ipc_answer_1(rid, ENOENT, 0); |
353 | return; |
390 | return; |
354 | } |
391 | } |
355 | 392 | ||
356 | free(desc); |
393 | free(desc); |
357 | 394 | ||
358 | ipc_answer_1(rid, EOK, 1); |
395 | ipc_answer_1(rid, EOK, 1); |
359 | } |
396 | } |
360 | } |
397 | } |
361 | 398 | ||
362 | void devfs_write(ipc_callid_t rid, ipc_call_t *request) |
399 | void devfs_write(ipc_callid_t rid, ipc_call_t *request) |
363 | { |
400 | { |
364 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
401 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
365 | off_t pos = (off_t) IPC_GET_ARG3(*request); |
402 | off_t pos = (off_t) IPC_GET_ARG3(*request); |
366 | 403 | ||
367 | if (index != 0) { |
404 | if (index != 0) { |
368 | unsigned long key[] = { |
405 | unsigned long key[] = { |
369 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
406 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
370 | }; |
407 | }; |
371 | 408 | ||
- | 409 | fibril_mutex_lock(&devices_mutex); |
|
372 | link_t *lnk = hash_table_find(&devices, key); |
410 | link_t *lnk = hash_table_find(&devices, key); |
373 | if (lnk == NULL) { |
411 | if (lnk == NULL) { |
- | 412 | fibril_mutex_unlock(&devices_mutex); |
|
374 | ipc_answer_0(rid, ENOENT); |
413 | ipc_answer_0(rid, ENOENT); |
375 | return; |
414 | return; |
376 | } |
415 | } |
377 | 416 | ||
378 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
417 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
379 | 418 | ||
380 | ipc_callid_t callid; |
419 | ipc_callid_t callid; |
381 | if (!ipc_data_write_receive(&callid, NULL)) { |
420 | if (!ipc_data_write_receive(&callid, NULL)) { |
- | 421 | fibril_mutex_unlock(&devices_mutex); |
|
382 | ipc_answer_0(callid, EINVAL); |
422 | ipc_answer_0(callid, EINVAL); |
383 | ipc_answer_0(rid, EINVAL); |
423 | ipc_answer_0(rid, EINVAL); |
384 | return; |
424 | return; |
385 | } |
425 | } |
386 | 426 | ||
387 | /* Make a request at the driver */ |
427 | /* Make a request at the driver */ |
388 | ipc_call_t answer; |
428 | ipc_call_t answer; |
389 | aid_t msg = async_send_3(dev->phone, IPC_GET_METHOD(*request), |
429 | aid_t msg = async_send_3(dev->phone, IPC_GET_METHOD(*request), |
390 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), |
430 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), |
391 | IPC_GET_ARG3(*request), &answer); |
431 | IPC_GET_ARG3(*request), &answer); |
392 | 432 | ||
393 | /* Forward the IPC_M_DATA_WRITE request to the driver */ |
433 | /* Forward the IPC_M_DATA_WRITE request to the driver */ |
394 | ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
434 | ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
395 | 435 | ||
- | 436 | fibril_mutex_unlock(&devices_mutex); |
|
- | 437 | ||
396 | /* Wait for reply from the driver. */ |
438 | /* Wait for reply from the driver. */ |
397 | ipcarg_t rc; |
439 | ipcarg_t rc; |
398 | async_wait_for(msg, &rc); |
440 | async_wait_for(msg, &rc); |
399 | size_t bytes = IPC_GET_ARG1(answer); |
441 | size_t bytes = IPC_GET_ARG1(answer); |
400 | 442 | ||
401 | /* Driver reply is the final result of the whole operation */ |
443 | /* Driver reply is the final result of the whole operation */ |
402 | ipc_answer_1(rid, rc, bytes); |
444 | ipc_answer_1(rid, rc, bytes); |
403 | } else { |
445 | } else { |
404 | /* Read-only filesystem */ |
446 | /* Read-only filesystem */ |
405 | ipc_answer_0(rid, ENOTSUP); |
447 | ipc_answer_0(rid, ENOTSUP); |
406 | } |
448 | } |
407 | } |
449 | } |
408 | 450 | ||
409 | void devfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
451 | void devfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
410 | { |
452 | { |
411 | ipc_answer_0(rid, ENOTSUP); |
453 | ipc_answer_0(rid, ENOTSUP); |
412 | } |
454 | } |
413 | 455 | ||
414 | void devfs_close(ipc_callid_t rid, ipc_call_t *request) |
456 | void devfs_close(ipc_callid_t rid, ipc_call_t *request) |
415 | { |
457 | { |
416 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
458 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
417 | 459 | ||
418 | if (index != 0) { |
460 | if (index != 0) { |
419 | unsigned long key[] = { |
461 | unsigned long key[] = { |
420 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
462 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
421 | }; |
463 | }; |
422 | 464 | ||
- | 465 | fibril_mutex_lock(&devices_mutex); |
|
423 | link_t *lnk = hash_table_find(&devices, key); |
466 | link_t *lnk = hash_table_find(&devices, key); |
424 | if (lnk == NULL) { |
467 | if (lnk == NULL) { |
- | 468 | fibril_mutex_unlock(&devices_mutex); |
|
425 | ipc_answer_0(rid, ENOENT); |
469 | ipc_answer_0(rid, ENOENT); |
426 | return; |
470 | return; |
427 | } |
471 | } |
428 | 472 | ||
429 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
473 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
430 | dev->refcount--; |
474 | dev->refcount--; |
431 | 475 | ||
432 | if (dev->refcount == 0) { |
476 | if (dev->refcount == 0) { |
433 | ipc_hangup(dev->phone); |
477 | ipc_hangup(dev->phone); |
434 | hash_table_remove(&devices, key, DEVICES_KEYS); |
478 | hash_table_remove(&devices, key, DEVICES_KEYS); |
435 | } |
479 | } |
436 | 480 | ||
- | 481 | fibril_mutex_unlock(&devices_mutex); |
|
- | 482 | ||
437 | ipc_answer_0(rid, EOK); |
483 | ipc_answer_0(rid, EOK); |
438 | } else |
484 | } else |
439 | ipc_answer_0(rid, ENOTSUP); |
485 | ipc_answer_0(rid, ENOTSUP); |
440 | } |
486 | } |
441 | 487 | ||
442 | void devfs_sync(ipc_callid_t rid, ipc_call_t *request) |
488 | void devfs_sync(ipc_callid_t rid, ipc_call_t *request) |
443 | { |
489 | { |
444 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
490 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
445 | 491 | ||
446 | if (index != 0) { |
492 | if (index != 0) { |
447 | unsigned long key[] = { |
493 | unsigned long key[] = { |
448 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
494 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
449 | }; |
495 | }; |
450 | 496 | ||
- | 497 | fibril_mutex_lock(&devices_mutex); |
|
451 | link_t *lnk = hash_table_find(&devices, key); |
498 | link_t *lnk = hash_table_find(&devices, key); |
452 | if (lnk == NULL) { |
499 | if (lnk == NULL) { |
- | 500 | fibril_mutex_unlock(&devices_mutex); |
|
453 | ipc_answer_0(rid, ENOENT); |
501 | ipc_answer_0(rid, ENOENT); |
454 | return; |
502 | return; |
455 | } |
503 | } |
456 | 504 | ||
457 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
505 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
458 | 506 | ||
459 | /* Make a request at the driver */ |
507 | /* Make a request at the driver */ |
460 | ipc_call_t answer; |
508 | ipc_call_t answer; |
461 | aid_t msg = async_send_2(dev->phone, IPC_GET_METHOD(*request), |
509 | aid_t msg = async_send_2(dev->phone, IPC_GET_METHOD(*request), |
462 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer); |
510 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer); |
463 | 511 | ||
- | 512 | fibril_mutex_unlock(&devices_mutex); |
|
- | 513 | ||
464 | /* Wait for reply from the driver */ |
514 | /* Wait for reply from the driver */ |
465 | ipcarg_t rc; |
515 | ipcarg_t rc; |
466 | async_wait_for(msg, &rc); |
516 | async_wait_for(msg, &rc); |
467 | 517 | ||
468 | /* Driver reply is the final result of the whole operation */ |
518 | /* Driver reply is the final result of the whole operation */ |
469 | ipc_answer_0(rid, rc); |
519 | ipc_answer_0(rid, rc); |
470 | } else |
520 | } else |
471 | ipc_answer_0(rid, ENOTSUP); |
521 | ipc_answer_0(rid, ENOTSUP); |
472 | } |
522 | } |
473 | 523 | ||
474 | void devfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
524 | void devfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
475 | { |
525 | { |
476 | ipc_answer_0(rid, ENOTSUP); |
526 | ipc_answer_0(rid, ENOTSUP); |
477 | } |
527 | } |
478 | 528 | ||
479 | /** |
529 | /** |
480 | * @} |
530 | * @} |
481 | */ |
531 | */ |
482 | 532 |