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