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