Rev 2768 | Rev 4010 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2768 | Rev 2770 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 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_register.c |
34 | * @file vfs_register.c |
35 | * @brief |
35 | * @brief |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <ipc/ipc.h> |
38 | #include <ipc/ipc.h> |
39 | #include <ipc/services.h> |
39 | #include <ipc/services.h> |
40 | #include <async.h> |
40 | #include <async.h> |
41 | #include <fibril.h> |
41 | #include <fibril.h> |
42 | #include <errno.h> |
42 | #include <errno.h> |
43 | #include <stdio.h> |
43 | #include <stdio.h> |
44 | #include <stdlib.h> |
44 | #include <stdlib.h> |
45 | #include <string.h> |
45 | #include <string.h> |
46 | #include <ctype.h> |
46 | #include <ctype.h> |
47 | #include <bool.h> |
47 | #include <bool.h> |
48 | #include <futex.h> |
48 | #include <futex.h> |
49 | #include <libadt/list.h> |
49 | #include <libadt/list.h> |
50 | #include <as.h> |
50 | #include <as.h> |
51 | #include <assert.h> |
51 | #include <assert.h> |
52 | #include <atomic.h> |
52 | #include <atomic.h> |
53 | #include "vfs.h" |
53 | #include "vfs.h" |
54 | 54 | ||
55 | atomic_t fs_head_futex = FUTEX_INITIALIZER; |
55 | atomic_t fs_head_futex = FUTEX_INITIALIZER; |
56 | link_t fs_head; |
56 | link_t fs_head; |
57 | 57 | ||
58 | atomic_t fs_handle_next = { |
58 | atomic_t fs_handle_next = { |
59 | .count = 1 |
59 | .count = 1 |
60 | }; |
60 | }; |
61 | 61 | ||
62 | /** Verify the VFS info structure. |
62 | /** Verify the VFS info structure. |
63 | * |
63 | * |
64 | * @param info Info structure to be verified. |
64 | * @param info Info structure to be verified. |
65 | * |
65 | * |
66 | * @return Non-zero if the info structure is sane, zero otherwise. |
66 | * @return Non-zero if the info structure is sane, zero otherwise. |
67 | */ |
67 | */ |
68 | static bool vfs_info_sane(vfs_info_t *info) |
68 | static bool vfs_info_sane(vfs_info_t *info) |
69 | { |
69 | { |
70 | int i; |
70 | int i; |
71 | 71 | ||
72 | /* |
72 | /* |
73 | * Check if the name is non-empty and is composed solely of ASCII |
73 | * Check if the name is non-empty and is composed solely of ASCII |
74 | * characters [a-z]+[a-z0-9_-]*. |
74 | * characters [a-z]+[a-z0-9_-]*. |
75 | */ |
75 | */ |
76 | if (!islower(info->name[0])) { |
76 | if (!islower(info->name[0])) { |
77 | dprintf("The name doesn't start with a lowercase character.\n"); |
77 | dprintf("The name doesn't start with a lowercase character.\n"); |
78 | return false; |
78 | return false; |
79 | } |
79 | } |
80 | for (i = 1; i < FS_NAME_MAXLEN; i++) { |
80 | for (i = 1; i < FS_NAME_MAXLEN; i++) { |
81 | if (!(islower(info->name[i]) || isdigit(info->name[i])) && |
81 | if (!(islower(info->name[i]) || isdigit(info->name[i])) && |
82 | (info->name[i] != '-') && (info->name[i] != '_')) { |
82 | (info->name[i] != '-') && (info->name[i] != '_')) { |
83 | if (info->name[i] == '\0') { |
83 | if (info->name[i] == '\0') { |
84 | break; |
84 | break; |
85 | } else { |
85 | } else { |
86 | dprintf("The name contains illegal " |
86 | dprintf("The name contains illegal " |
87 | "characters.\n"); |
87 | "characters.\n"); |
88 | return false; |
88 | return false; |
89 | } |
89 | } |
90 | } |
90 | } |
91 | } |
91 | } |
92 | /* |
92 | /* |
93 | * This check is not redundant. It ensures that the name is |
93 | * This check is not redundant. It ensures that the name is |
94 | * NULL-terminated, even if FS_NAME_MAXLEN characters are used. |
94 | * NULL-terminated, even if FS_NAME_MAXLEN characters are used. |
95 | */ |
95 | */ |
96 | if (info->name[i] != '\0') { |
96 | if (info->name[i] != '\0') { |
97 | dprintf("The name is not properly NULL-terminated.\n"); |
97 | dprintf("The name is not properly NULL-terminated.\n"); |
98 | return false; |
98 | return false; |
99 | } |
99 | } |
100 | 100 | ||
101 | 101 | ||
102 | /* |
102 | /* |
103 | * Check if the FS implements mandatory VFS operations. |
103 | * Check if the FS implements mandatory VFS operations. |
104 | */ |
104 | */ |
105 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) { |
105 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) { |
106 | dprintf("Operation VFS_LOOKUP not defined by the client.\n"); |
106 | dprintf("Operation VFS_LOOKUP not defined by the client.\n"); |
107 | return false; |
107 | return false; |
108 | } |
108 | } |
109 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) { |
109 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) { |
110 | dprintf("Operation VFS_READ not defined by the client.\n"); |
110 | dprintf("Operation VFS_READ not defined by the client.\n"); |
111 | return false; |
111 | return false; |
112 | } |
112 | } |
113 | 113 | ||
114 | /* |
114 | /* |
115 | * Check if each operation is either not defined, defined or default. |
115 | * Check if each operation is either not defined, defined or default. |
116 | */ |
116 | */ |
117 | for (i = VFS_FIRST; i < VFS_LAST_CLNT; i++) { |
117 | for (i = VFS_FIRST; i < VFS_LAST_CLNT; i++) { |
118 | if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) && |
118 | if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) && |
119 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) && |
119 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) && |
120 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) { |
120 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) { |
121 | dprintf("Operation info not understood.\n"); |
121 | dprintf("Operation info not understood.\n"); |
122 | return false; |
122 | return false; |
123 | } |
123 | } |
124 | } |
124 | } |
125 | return true; |
125 | return true; |
126 | } |
126 | } |
127 | 127 | ||
128 | /** VFS_REGISTER protocol function. |
128 | /** VFS_REGISTER protocol function. |
129 | * |
129 | * |
130 | * @param rid Hash of the call with the request. |
130 | * @param rid Hash of the call with the request. |
131 | * @param request Call structure with the request. |
131 | * @param request Call structure with the request. |
132 | */ |
132 | */ |
133 | void vfs_register(ipc_callid_t rid, ipc_call_t *request) |
133 | void vfs_register(ipc_callid_t rid, ipc_call_t *request) |
134 | { |
134 | { |
135 | ipc_callid_t callid; |
135 | ipc_callid_t callid; |
136 | ipc_call_t call; |
136 | ipc_call_t call; |
137 | int rc; |
137 | int rc; |
138 | size_t size; |
138 | size_t size; |
139 | 139 | ||
140 | dprintf("Processing VFS_REGISTER request received from %p.\n", |
140 | dprintf("Processing VFS_REGISTER request received from %p.\n", |
141 | request->in_phone_hash); |
141 | request->in_phone_hash); |
142 | 142 | ||
143 | /* |
143 | /* |
144 | * The first call has to be IPC_M_DATA_SEND in which we receive the |
144 | * The first call has to be IPC_M_DATA_SEND in which we receive the |
145 | * VFS info structure from the client FS. |
145 | * VFS info structure from the client FS. |
146 | */ |
146 | */ |
147 | if (!ipc_data_write_receive(&callid, &size)) { |
147 | if (!ipc_data_write_receive(&callid, &size)) { |
148 | /* |
148 | /* |
149 | * The client doesn't obey the same protocol as we do. |
149 | * The client doesn't obey the same protocol as we do. |
150 | */ |
150 | */ |
151 | dprintf("Receiving of VFS info failed.\n"); |
151 | dprintf("Receiving of VFS info failed.\n"); |
152 | ipc_answer_0(callid, EINVAL); |
152 | ipc_answer_0(callid, EINVAL); |
153 | ipc_answer_0(rid, EINVAL); |
153 | ipc_answer_0(rid, EINVAL); |
154 | return; |
154 | return; |
155 | } |
155 | } |
156 | 156 | ||
157 | dprintf("VFS info received, size = %d\n", size); |
157 | dprintf("VFS info received, size = %d\n", size); |
158 | 158 | ||
159 | /* |
159 | /* |
160 | * We know the size of the VFS info structure. See if the client |
160 | * We know the size of the VFS info structure. See if the client |
161 | * understands this easy concept too. |
161 | * understands this easy concept too. |
162 | */ |
162 | */ |
163 | if (size != sizeof(vfs_info_t)) { |
163 | if (size != sizeof(vfs_info_t)) { |
164 | /* |
164 | /* |
165 | * The client is sending us something, which cannot be |
165 | * The client is sending us something, which cannot be |
166 | * the info structure. |
166 | * the info structure. |
167 | */ |
167 | */ |
168 | dprintf("Received VFS info has bad size.\n"); |
168 | dprintf("Received VFS info has bad size.\n"); |
169 | ipc_answer_0(callid, EINVAL); |
169 | ipc_answer_0(callid, EINVAL); |
170 | ipc_answer_0(rid, EINVAL); |
170 | ipc_answer_0(rid, EINVAL); |
171 | return; |
171 | return; |
172 | } |
172 | } |
173 | 173 | ||
174 | /* |
174 | /* |
175 | * Allocate and initialize a buffer for the fs_info structure. |
175 | * Allocate and initialize a buffer for the fs_info structure. |
176 | */ |
176 | */ |
177 | fs_info_t *fs_info; |
177 | fs_info_t *fs_info; |
178 | fs_info = (fs_info_t *) malloc(sizeof(fs_info_t)); |
178 | fs_info = (fs_info_t *) malloc(sizeof(fs_info_t)); |
179 | if (!fs_info) { |
179 | if (!fs_info) { |
180 | dprintf("Could not allocate memory for FS info.\n"); |
180 | dprintf("Could not allocate memory for FS info.\n"); |
181 | ipc_answer_0(callid, ENOMEM); |
181 | ipc_answer_0(callid, ENOMEM); |
182 | ipc_answer_0(rid, ENOMEM); |
182 | ipc_answer_0(rid, ENOMEM); |
183 | return; |
183 | return; |
184 | } |
184 | } |
185 | link_initialize(&fs_info->fs_link); |
185 | link_initialize(&fs_info->fs_link); |
186 | futex_initialize(&fs_info->phone_futex, 1); |
186 | futex_initialize(&fs_info->phone_futex, 1); |
187 | 187 | ||
188 | rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size); |
188 | rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size); |
189 | if (rc != EOK) { |
189 | if (rc != EOK) { |
190 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n", |
190 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n", |
191 | rc); |
191 | rc); |
192 | free(fs_info); |
192 | free(fs_info); |
193 | ipc_answer_0(callid, rc); |
193 | ipc_answer_0(callid, rc); |
194 | ipc_answer_0(rid, rc); |
194 | ipc_answer_0(rid, rc); |
195 | return; |
195 | return; |
196 | } |
196 | } |
197 | 197 | ||
198 | dprintf("VFS info delivered.\n"); |
198 | dprintf("VFS info delivered.\n"); |
199 | 199 | ||
200 | if (!vfs_info_sane(&fs_info->vfs_info)) { |
200 | if (!vfs_info_sane(&fs_info->vfs_info)) { |
201 | free(fs_info); |
201 | free(fs_info); |
202 | ipc_answer_0(callid, EINVAL); |
202 | ipc_answer_0(callid, EINVAL); |
203 | ipc_answer_0(rid, EINVAL); |
203 | ipc_answer_0(rid, EINVAL); |
204 | return; |
204 | return; |
205 | } |
205 | } |
206 | 206 | ||
207 | futex_down(&fs_head_futex); |
207 | futex_down(&fs_head_futex); |
208 | fibril_inc_sercount(); |
208 | fibril_inc_sercount(); |
209 | 209 | ||
210 | /* |
210 | /* |
211 | * Check for duplicit registrations. |
211 | * Check for duplicit registrations. |
212 | */ |
212 | */ |
213 | if (fs_name_to_handle(fs_info->vfs_info.name, false)) { |
213 | if (fs_name_to_handle(fs_info->vfs_info.name, false)) { |
214 | /* |
214 | /* |
215 | * We already register a fs like this. |
215 | * We already register a fs like this. |
216 | */ |
216 | */ |
217 | dprintf("FS is already registered.\n"); |
217 | dprintf("FS is already registered.\n"); |
218 | fibril_dec_sercount(); |
218 | fibril_dec_sercount(); |
219 | futex_up(&fs_head_futex); |
219 | futex_up(&fs_head_futex); |
220 | free(fs_info); |
220 | free(fs_info); |
221 | ipc_answer_0(callid, EEXISTS); |
221 | ipc_answer_0(callid, EEXISTS); |
222 | ipc_answer_0(rid, EEXISTS); |
222 | ipc_answer_0(rid, EEXISTS); |
223 | return; |
223 | return; |
224 | } |
224 | } |
225 | 225 | ||
226 | /* |
226 | /* |
227 | * Add fs_info to the list of registered FS's. |
227 | * Add fs_info to the list of registered FS's. |
228 | */ |
228 | */ |
229 | dprintf("Inserting FS into the list of registered file systems.\n"); |
229 | dprintf("Inserting FS into the list of registered file systems.\n"); |
230 | list_append(&fs_info->fs_link, &fs_head); |
230 | list_append(&fs_info->fs_link, &fs_head); |
231 | 231 | ||
232 | /* |
232 | /* |
233 | * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so |
233 | * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so |
234 | * that a callback connection is created and we have a phone through |
234 | * that a callback connection is created and we have a phone through |
235 | * which to forward VFS requests to it. |
235 | * which to forward VFS requests to it. |
236 | */ |
236 | */ |
237 | callid = async_get_call(&call); |
237 | callid = async_get_call(&call); |
238 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) { |
238 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) { |
239 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
239 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
240 | list_remove(&fs_info->fs_link); |
240 | list_remove(&fs_info->fs_link); |
241 | fibril_dec_sercount(); |
241 | fibril_dec_sercount(); |
242 | futex_up(&fs_head_futex); |
242 | futex_up(&fs_head_futex); |
243 | free(fs_info); |
243 | free(fs_info); |
244 | ipc_answer_0(callid, EINVAL); |
244 | ipc_answer_0(callid, EINVAL); |
245 | ipc_answer_0(rid, EINVAL); |
245 | ipc_answer_0(rid, EINVAL); |
246 | return; |
246 | return; |
247 | } |
247 | } |
248 | fs_info->phone = IPC_GET_ARG5(call); |
248 | fs_info->phone = IPC_GET_ARG5(call); |
249 | ipc_answer_0(callid, EOK); |
249 | ipc_answer_0(callid, EOK); |
250 | 250 | ||
251 | dprintf("Callback connection to FS created.\n"); |
251 | dprintf("Callback connection to FS created.\n"); |
252 | 252 | ||
253 | /* |
253 | /* |
254 | * The client will want us to send him the address space area with PLB. |
254 | * The client will want us to send him the address space area with PLB. |
255 | */ |
255 | */ |
256 | 256 | ||
257 | if (!ipc_share_in_receive(&callid, &size)) { |
257 | if (!ipc_share_in_receive(&callid, &size)) { |
258 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
258 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
259 | list_remove(&fs_info->fs_link); |
259 | list_remove(&fs_info->fs_link); |
260 | fibril_dec_sercount(); |
260 | fibril_dec_sercount(); |
261 | futex_up(&fs_head_futex); |
261 | futex_up(&fs_head_futex); |
262 | ipc_hangup(fs_info->phone); |
262 | ipc_hangup(fs_info->phone); |
263 | free(fs_info); |
263 | free(fs_info); |
264 | ipc_answer_0(callid, EINVAL); |
264 | ipc_answer_0(callid, EINVAL); |
265 | ipc_answer_0(rid, EINVAL); |
265 | ipc_answer_0(rid, EINVAL); |
266 | return; |
266 | return; |
267 | } |
267 | } |
268 | 268 | ||
269 | /* |
269 | /* |
270 | * We can only send the client address space area PLB_SIZE bytes long. |
270 | * We can only send the client address space area PLB_SIZE bytes long. |
271 | */ |
271 | */ |
272 | if (size != PLB_SIZE) { |
272 | if (size != PLB_SIZE) { |
273 | dprintf("Client suggests wrong size of PFB, size = %d\n", size); |
273 | dprintf("Client suggests wrong size of PFB, size = %d\n", size); |
274 | list_remove(&fs_info->fs_link); |
274 | list_remove(&fs_info->fs_link); |
275 | fibril_dec_sercount(); |
275 | fibril_dec_sercount(); |
276 | futex_up(&fs_head_futex); |
276 | futex_up(&fs_head_futex); |
277 | ipc_hangup(fs_info->phone); |
277 | ipc_hangup(fs_info->phone); |
278 | free(fs_info); |
278 | free(fs_info); |
279 | ipc_answer_0(callid, EINVAL); |
279 | ipc_answer_0(callid, EINVAL); |
280 | ipc_answer_0(rid, EINVAL); |
280 | ipc_answer_0(rid, EINVAL); |
281 | return; |
281 | return; |
282 | } |
282 | } |
283 | 283 | ||
284 | /* |
284 | /* |
285 | * Commit to read-only sharing the PLB with the client. |
285 | * Commit to read-only sharing the PLB with the client. |
286 | */ |
286 | */ |
287 | (void) ipc_share_in_finalize(callid, plb, |
287 | (void) ipc_share_in_finalize(callid, plb, |
288 | AS_AREA_READ | AS_AREA_CACHEABLE); |
288 | AS_AREA_READ | AS_AREA_CACHEABLE); |
289 | 289 | ||
290 | dprintf("Sharing PLB.\n"); |
290 | dprintf("Sharing PLB.\n"); |
291 | 291 | ||
292 | /* |
292 | /* |
293 | * That was it. The FS has been registered. |
293 | * That was it. The FS has been registered. |
294 | * In reply to the VFS_REGISTER request, we assign the client file |
294 | * In reply to the VFS_REGISTER request, we assign the client file |
295 | * system a global file system handle. |
295 | * system a global file system handle. |
296 | */ |
296 | */ |
297 | fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next); |
297 | fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next); |
298 | ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle); |
298 | ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle); |
299 | 299 | ||
300 | fibril_dec_sercount(); |
300 | fibril_dec_sercount(); |
301 | futex_up(&fs_head_futex); |
301 | futex_up(&fs_head_futex); |
302 | 302 | ||
303 | dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n", |
303 | dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n", |
304 | FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle); |
304 | FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle); |
305 | } |
305 | } |
306 | 306 | ||
307 | /** For a given file system handle, implement policy for allocating a phone. |
307 | /** For a given file system handle, implement policy for allocating a phone. |
308 | * |
308 | * |
309 | * @param handle File system handle. |
309 | * @param handle File system handle. |
310 | * |
310 | * |
311 | * @return Phone over which a multi-call request can be safely |
311 | * @return Phone over which a multi-call request can be safely |
312 | * sent. Return 0 if no phone was found. |
312 | * sent. Return 0 if no phone was found. |
313 | */ |
313 | */ |
314 | int vfs_grab_phone(int handle) |
314 | int vfs_grab_phone(fs_handle_t handle) |
315 | { |
315 | { |
316 | /* |
316 | /* |
317 | * For now, we don't try to be very clever and very fast. |
317 | * For now, we don't try to be very clever and very fast. |
318 | * We simply lookup the phone in the fs_head list. We currently don't |
318 | * We simply lookup the phone in the fs_head list. We currently don't |
319 | * open any additional phones (even though that itself would be pretty |
319 | * open any additional phones (even though that itself would be pretty |
320 | * straightforward; housekeeping multiple open phones to a FS task would |
320 | * straightforward; housekeeping multiple open phones to a FS task would |
321 | * be more demanding). Instead, we simply take the respective |
321 | * be more demanding). Instead, we simply take the respective |
322 | * phone_futex and keep it until vfs_release_phone(). |
322 | * phone_futex and keep it until vfs_release_phone(). |
323 | */ |
323 | */ |
324 | futex_down(&fs_head_futex); |
324 | futex_down(&fs_head_futex); |
325 | link_t *cur; |
325 | link_t *cur; |
326 | fs_info_t *fs; |
326 | fs_info_t *fs; |
327 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
327 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
328 | fs = list_get_instance(cur, fs_info_t, fs_link); |
328 | fs = list_get_instance(cur, fs_info_t, fs_link); |
329 | if (fs->fs_handle == handle) { |
329 | if (fs->fs_handle == handle) { |
330 | futex_up(&fs_head_futex); |
330 | futex_up(&fs_head_futex); |
331 | /* |
331 | /* |
332 | * For now, take the futex unconditionally. |
332 | * For now, take the futex unconditionally. |
333 | * Oh yeah, serialization rocks. |
333 | * Oh yeah, serialization rocks. |
334 | * It will be up'ed in vfs_release_phone(). |
334 | * It will be up'ed in vfs_release_phone(). |
335 | */ |
335 | */ |
336 | futex_down(&fs->phone_futex); |
336 | futex_down(&fs->phone_futex); |
337 | /* |
337 | /* |
338 | * Avoid deadlock with other fibrils in the same thread |
338 | * Avoid deadlock with other fibrils in the same thread |
339 | * by disabling fibril preemption. |
339 | * by disabling fibril preemption. |
340 | */ |
340 | */ |
341 | fibril_inc_sercount(); |
341 | fibril_inc_sercount(); |
342 | return fs->phone; |
342 | return fs->phone; |
343 | } |
343 | } |
344 | } |
344 | } |
345 | futex_up(&fs_head_futex); |
345 | futex_up(&fs_head_futex); |
346 | return 0; |
346 | return 0; |
347 | } |
347 | } |
348 | 348 | ||
349 | /** Tell VFS that the phone is in use for any request. |
349 | /** Tell VFS that the phone is in use for any request. |
350 | * |
350 | * |
351 | * @param phone Phone to FS task. |
351 | * @param phone Phone to FS task. |
352 | */ |
352 | */ |
353 | void vfs_release_phone(int phone) |
353 | void vfs_release_phone(int phone) |
354 | { |
354 | { |
355 | bool found = false; |
355 | bool found = false; |
356 | 356 | ||
357 | /* |
357 | /* |
358 | * Undo the fibril_inc_sercount() done in vfs_grab_phone(). |
358 | * Undo the fibril_inc_sercount() done in vfs_grab_phone(). |
359 | */ |
359 | */ |
360 | fibril_dec_sercount(); |
360 | fibril_dec_sercount(); |
361 | 361 | ||
362 | futex_down(&fs_head_futex); |
362 | futex_down(&fs_head_futex); |
363 | link_t *cur; |
363 | link_t *cur; |
364 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
364 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
365 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link); |
365 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link); |
366 | if (fs->phone == phone) { |
366 | if (fs->phone == phone) { |
367 | found = true; |
367 | found = true; |
368 | futex_up(&fs_head_futex); |
368 | futex_up(&fs_head_futex); |
369 | futex_up(&fs->phone_futex); |
369 | futex_up(&fs->phone_futex); |
370 | return; |
370 | return; |
371 | } |
371 | } |
372 | } |
372 | } |
373 | futex_up(&fs_head_futex); |
373 | futex_up(&fs_head_futex); |
374 | 374 | ||
375 | /* |
375 | /* |
376 | * Not good to get here. |
376 | * Not good to get here. |
377 | */ |
377 | */ |
378 | assert(found == true); |
378 | assert(found == true); |
379 | } |
379 | } |
380 | 380 | ||
381 | /** Convert file system name to its handle. |
381 | /** Convert file system name to its handle. |
382 | * |
382 | * |
383 | * @param name File system name. |
383 | * @param name File system name. |
384 | * @param lock If true, the function will down and up the |
384 | * @param lock If true, the function will down and up the |
385 | * fs_head_futex. |
385 | * fs_head_futex. |
386 | * |
386 | * |
387 | * @return File system handle or zero if file system not found. |
387 | * @return File system handle or zero if file system not found. |
388 | */ |
388 | */ |
389 | int fs_name_to_handle(char *name, bool lock) |
389 | fs_handle_t fs_name_to_handle(char *name, bool lock) |
390 | { |
390 | { |
391 | int handle = 0; |
391 | int handle = 0; |
392 | 392 | ||
393 | if (lock) |
393 | if (lock) |
394 | futex_down(&fs_head_futex); |
394 | futex_down(&fs_head_futex); |
395 | link_t *cur; |
395 | link_t *cur; |
396 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
396 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
397 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link); |
397 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link); |
398 | if (strncmp(fs->vfs_info.name, name, |
398 | if (strncmp(fs->vfs_info.name, name, |
399 | sizeof(fs->vfs_info.name)) == 0) { |
399 | sizeof(fs->vfs_info.name)) == 0) { |
400 | handle = fs->fs_handle; |
400 | handle = fs->fs_handle; |
401 | break; |
401 | break; |
402 | } |
402 | } |
403 | } |
403 | } |
404 | if (lock) |
404 | if (lock) |
405 | futex_up(&fs_head_futex); |
405 | futex_up(&fs_head_futex); |
406 | return handle; |
406 | return handle; |
407 | } |
407 | } |
408 | 408 | ||
409 | /** |
409 | /** |
410 | * @} |
410 | * @} |
411 | */ |
411 | */ |
412 | 412 |