Rev 2535 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2535 | Rev 2536 | ||
---|---|---|---|
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 <libadt/list.h> |
48 | #include <libadt/list.h> |
49 | #include "vfs.h" |
49 | #include "vfs.h" |
50 | 50 | ||
51 | atomic_t fs_head_futex = FUTEX_INITIALIZER; |
51 | atomic_t fs_head_futex = FUTEX_INITIALIZER; |
52 | link_t fs_head; |
52 | link_t fs_head; |
53 | 53 | ||
54 | /** Verify the VFS info structure. |
54 | /** Verify the VFS info structure. |
55 | * |
55 | * |
56 | * @param info Info structure to be verified. |
56 | * @param info Info structure to be verified. |
57 | * |
57 | * |
58 | * @return Non-zero if the info structure is sane, zero otherwise. |
58 | * @return Non-zero if the info structure is sane, zero otherwise. |
59 | */ |
59 | */ |
60 | static bool vfs_info_sane(vfs_info_t *info) |
60 | static bool vfs_info_sane(vfs_info_t *info) |
61 | { |
61 | { |
62 | int i; |
62 | int i; |
63 | 63 | ||
64 | /* |
64 | /* |
65 | * Check if the name is non-empty and is composed solely of ASCII |
65 | * Check if the name is non-empty and is composed solely of ASCII |
66 | * characters [a-z]+[a-z0-9_-]*. |
66 | * characters [a-z]+[a-z0-9_-]*. |
67 | */ |
67 | */ |
68 | if (!islower(info->name[0])) { |
68 | if (!islower(info->name[0])) { |
69 | dprintf("The name doesn't start with a lowercase character.\n"); |
69 | dprintf("The name doesn't start with a lowercase character.\n"); |
70 | return false; |
70 | return false; |
71 | } |
71 | } |
72 | for (i = 1; i < FS_NAME_MAXLEN; i++) { |
72 | for (i = 1; i < FS_NAME_MAXLEN; i++) { |
73 | if (!(islower(info->name[i]) || isdigit(info->name[i])) && |
73 | if (!(islower(info->name[i]) || isdigit(info->name[i])) && |
74 | (info->name[i] != '-') && (info->name[i] != '_')) { |
74 | (info->name[i] != '-') && (info->name[i] != '_')) { |
75 | if (info->name[i] == '\0') { |
75 | if (info->name[i] == '\0') { |
76 | break; |
76 | break; |
77 | } else { |
77 | } else { |
78 | dprintf("The name contains illegal " |
78 | dprintf("The name contains illegal " |
79 | "characters.\n"); |
79 | "characters.\n"); |
80 | return false; |
80 | return false; |
81 | } |
81 | } |
82 | } |
82 | } |
83 | } |
83 | } |
84 | 84 | ||
85 | 85 | ||
86 | /* |
86 | /* |
87 | * Check if the FS implements mandatory VFS operations. |
87 | * Check if the FS implements mandatory VFS operations. |
88 | */ |
88 | */ |
89 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_REGISTER)] != VFS_OP_DEFINED) { |
89 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_REGISTER)] != VFS_OP_DEFINED) { |
90 | dprintf("Operation VFS_REGISTER not defined by the client.\n"); |
90 | dprintf("Operation VFS_REGISTER not defined by the client.\n"); |
91 | return false; |
91 | return false; |
92 | } |
92 | } |
93 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_MOUNT)] != VFS_OP_DEFINED) { |
93 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_MOUNT)] != VFS_OP_DEFINED) { |
94 | dprintf("Operation VFS_MOUNT not defined by the client.\n"); |
94 | dprintf("Operation VFS_MOUNT not defined by the client.\n"); |
95 | return false; |
95 | return false; |
96 | } |
96 | } |
97 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_UNMOUNT)] != VFS_OP_DEFINED) { |
97 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_UNMOUNT)] != VFS_OP_DEFINED) { |
98 | dprintf("Operation VFS_UNMOUNT not defined by the client.\n"); |
98 | dprintf("Operation VFS_UNMOUNT not defined by the client.\n"); |
99 | return false; |
99 | return false; |
100 | } |
100 | } |
101 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) { |
101 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) { |
102 | dprintf("Operation VFS_LOOKUP not defined by the client.\n"); |
102 | dprintf("Operation VFS_LOOKUP not defined by the client.\n"); |
103 | return false; |
103 | return false; |
104 | } |
104 | } |
105 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) { |
105 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) { |
106 | dprintf("Operation VFS_OPEN not defined by the client.\n"); |
106 | dprintf("Operation VFS_OPEN not defined by the client.\n"); |
107 | return false; |
107 | return false; |
108 | } |
108 | } |
109 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) { |
109 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) { |
110 | dprintf("Operation VFS_CLOSE not defined by the client.\n"); |
110 | dprintf("Operation VFS_CLOSE not defined by the client.\n"); |
111 | return false; |
111 | return false; |
112 | } |
112 | } |
113 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) { |
113 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) { |
114 | dprintf("Operation VFS_READ not defined by the client.\n"); |
114 | dprintf("Operation VFS_READ not defined by the client.\n"); |
115 | return false; |
115 | return false; |
116 | } |
116 | } |
117 | 117 | ||
118 | /* |
118 | /* |
119 | * Check if each operation is either not defined, defined or default. |
119 | * Check if each operation is either not defined, defined or default. |
120 | */ |
120 | */ |
121 | for (i = VFS_FIRST; i < VFS_LAST; i++) { |
121 | for (i = VFS_FIRST; i < VFS_LAST; i++) { |
122 | if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) && |
122 | if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) && |
123 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) && |
123 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) && |
124 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) { |
124 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) { |
125 | dprintf("Operation info not understood.\n"); |
125 | dprintf("Operation info not understood.\n"); |
126 | return false; |
126 | return false; |
127 | } |
127 | } |
128 | } |
128 | } |
129 | return true; |
129 | return true; |
130 | } |
130 | } |
131 | 131 | ||
132 | /** VFS_REGISTER protocol function. |
132 | /** VFS_REGISTER protocol function. |
133 | * |
133 | * |
134 | * @param rid Hash of the call with the request. |
134 | * @param rid Hash of the call with the request. |
135 | * @param request Call structure with the request. |
135 | * @param request Call structure with the request. |
136 | */ |
136 | */ |
137 | void vfs_register(ipc_callid_t rid, ipc_call_t *request) |
137 | void vfs_register(ipc_callid_t rid, ipc_call_t *request) |
138 | { |
138 | { |
139 | ipc_callid_t callid; |
139 | ipc_callid_t callid; |
140 | ipc_call_t call; |
140 | ipc_call_t call; |
141 | int rc; |
141 | int rc; |
142 | size_t size; |
142 | size_t size; |
143 | 143 | ||
144 | dprintf("Processing VFS_REGISTER request received from %p.\n", |
144 | dprintf("Processing VFS_REGISTER request received from %p.\n", |
145 | request->in_phone_hash); |
145 | request->in_phone_hash); |
146 | 146 | ||
147 | /* |
147 | /* |
148 | * The first call has to be IPC_M_DATA_SEND in which we receive the |
148 | * The first call has to be IPC_M_DATA_SEND in which we receive the |
149 | * VFS info structure from the client FS. |
149 | * VFS info structure from the client FS. |
150 | */ |
150 | */ |
151 | if (!ipc_data_receive(&callid, &call, NULL, &size)) { |
151 | if (!ipc_data_receive(&callid, &call, NULL, &size)) { |
152 | /* |
152 | /* |
153 | * The client doesn't obey the same protocol as we do. |
153 | * The client doesn't obey the same protocol as we do. |
154 | */ |
154 | */ |
155 | dprintf("Receiving of VFS info failed.\n"); |
155 | dprintf("Receiving of VFS info failed.\n"); |
156 | ipc_answer_fast(callid, EINVAL, 0, 0); |
156 | ipc_answer_fast(callid, EINVAL, 0, 0); |
157 | ipc_answer_fast(rid, EINVAL, 0, 0); |
157 | ipc_answer_fast(rid, EINVAL, 0, 0); |
158 | return; |
158 | return; |
159 | } |
159 | } |
160 | 160 | ||
161 | dprintf("VFS info received, size = %d\n", size); |
161 | dprintf("VFS info received, size = %d\n", size); |
162 | 162 | ||
163 | /* |
163 | /* |
164 | * We know the size of the VFS info structure. See if the client |
164 | * We know the size of the VFS info structure. See if the client |
165 | * understands this easy concept too. |
165 | * understands this easy concept too. |
166 | */ |
166 | */ |
167 | if (size != sizeof(vfs_info_t)) { |
167 | if (size != sizeof(vfs_info_t)) { |
168 | /* |
168 | /* |
169 | * The client is sending us something, which cannot be |
169 | * The client is sending us something, which cannot be |
170 | * the info structure. |
170 | * the info structure. |
171 | */ |
171 | */ |
172 | dprintf("Received VFS info has bad size.\n"); |
172 | dprintf("Received VFS info has bad size.\n"); |
173 | ipc_answer_fast(callid, EINVAL, 0, 0); |
173 | ipc_answer_fast(callid, EINVAL, 0, 0); |
174 | ipc_answer_fast(rid, EINVAL, 0, 0); |
174 | ipc_answer_fast(rid, EINVAL, 0, 0); |
175 | return; |
175 | return; |
176 | } |
176 | } |
177 | fs_info_t *fs_info; |
177 | fs_info_t *fs_info; |
178 | 178 | ||
179 | /* |
179 | /* |
180 | * Allocate and initialize a buffer for the fs_info structure. |
180 | * Allocate and initialize a buffer for the fs_info structure. |
181 | */ |
181 | */ |
182 | fs_info = (fs_info_t *) malloc(sizeof(fs_info_t)); |
182 | fs_info = (fs_info_t *) malloc(sizeof(fs_info_t)); |
183 | if (!fs_info) { |
183 | if (!fs_info) { |
184 | dprintf("Could not allocate memory for FS info.\n"); |
184 | dprintf("Could not allocate memory for FS info.\n"); |
185 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
185 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
186 | ipc_answer_fast(rid, ENOMEM, 0, 0); |
186 | ipc_answer_fast(rid, ENOMEM, 0, 0); |
187 | return; |
187 | return; |
188 | } |
188 | } |
189 | link_initialize(&fs_info->fs_link); |
189 | link_initialize(&fs_info->fs_link); |
190 | 190 | ||
191 | rc = ipc_data_deliver(callid, &call, &fs_info->vfs_info, size); |
191 | rc = ipc_data_deliver(callid, &call, &fs_info->vfs_info, size); |
192 | if (rc != EOK) { |
192 | if (rc != EOK) { |
193 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n", |
193 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n", |
194 | rc); |
194 | rc); |
195 | free(fs_info); |
195 | free(fs_info); |
196 | ipc_answer_fast(callid, rc, 0, 0); |
196 | ipc_answer_fast(callid, rc, 0, 0); |
197 | ipc_answer_fast(rid, rc, 0, 0); |
197 | ipc_answer_fast(rid, rc, 0, 0); |
198 | return; |
198 | return; |
199 | } |
199 | } |
200 | 200 | ||
201 | dprintf("VFS info delivered.\n"); |
201 | dprintf("VFS info delivered.\n"); |
202 | 202 | ||
203 | if (!vfs_info_sane(&fs_info->vfs_info)) { |
203 | if (!vfs_info_sane(&fs_info->vfs_info)) { |
204 | free(fs_info); |
204 | free(fs_info); |
205 | ipc_answer_fast(callid, EINVAL, 0, 0); |
205 | ipc_answer_fast(callid, EINVAL, 0, 0); |
206 | ipc_answer_fast(rid, EINVAL, 0, 0); |
206 | ipc_answer_fast(rid, EINVAL, 0, 0); |
207 | return; |
207 | return; |
208 | } |
208 | } |
209 | 209 | ||
210 | futex_down(&fs_head_futex); |
210 | futex_down(&fs_head_futex); |
211 | 211 | ||
212 | /* |
212 | /* |
213 | * Check for duplicit registrations. |
213 | * Check for duplicit registrations. |
214 | */ |
214 | */ |
215 | link_t *cur; |
215 | link_t *cur; |
216 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
216 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) { |
217 | fs_info_t *fi = list_get_instance(cur, fs_info_t, |
217 | fs_info_t *fi = list_get_instance(cur, fs_info_t, |
218 | fs_link); |
218 | fs_link); |
219 | /* TODO: replace strcmp with strncmp once we have it */ |
219 | /* TODO: replace strcmp with strncmp once we have it */ |
220 | if (strcmp(fs_info->vfs_info.name, fi->vfs_info.name) == 0) { |
220 | if (strcmp(fs_info->vfs_info.name, fi->vfs_info.name) == 0) { |
221 | /* |
221 | /* |
222 | * We already register a fs like this. |
222 | * We already register a fs like this. |
223 | */ |
223 | */ |
224 | dprintf("FS is already registered.\n"); |
224 | dprintf("FS is already registered.\n"); |
225 | futex_up(&fs_head_futex); |
225 | futex_up(&fs_head_futex); |
226 | free(fs_info); |
226 | free(fs_info); |
227 | ipc_answer_fast(callid, EEXISTS, 0, 0); |
227 | ipc_answer_fast(callid, EEXISTS, 0, 0); |
228 | ipc_answer_fast(rid, EEXISTS, 0, 0); |
228 | ipc_answer_fast(rid, EEXISTS, 0, 0); |
229 | return; |
229 | return; |
230 | } |
230 | } |
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("Adding FS into the registered list.\n"); |
236 | dprintf("Adding FS into the registered list.\n"); |
237 | list_append(&fs_info->fs_link, &fs_head); |
237 | list_append(&fs_info->fs_link, &fs_head); |
238 | 238 | ||
239 | /* |
239 | /* |
240 | * ACK receiving a properly formatted, non-duplicit vfs_info. |
240 | * ACK receiving a properly formatted, non-duplicit vfs_info. |
241 | */ |
241 | */ |
242 | ipc_answer_fast(callid, EOK, 0, 0); |
242 | ipc_answer_fast(callid, EOK, 0, 0); |
243 | 243 | ||
244 | /* |
244 | /* |
245 | * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so |
245 | * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so |
246 | * that a callback connection is created and we have a phone through |
246 | * that a callback connection is created and we have a phone through |
247 | * which to forward VFS requests to it. |
247 | * which to forward VFS requests to it. |
248 | */ |
248 | */ |
249 | callid = async_get_call(&call); |
249 | callid = async_get_call(&call); |
250 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) { |
250 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) { |
251 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
251 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call)); |
252 | list_remove(&fs_info->fs_link); |
252 | list_remove(&fs_info->fs_link); |
253 | futex_up(&fs_head_futex); |
253 | futex_up(&fs_head_futex); |
254 | free(fs_info); |
254 | free(fs_info); |
255 | ipc_answer_fast(callid, EINVAL, 0, 0); |
255 | ipc_answer_fast(callid, EINVAL, 0, 0); |
256 | ipc_answer_fast(rid, EINVAL, 0, 0); |
256 | ipc_answer_fast(rid, EINVAL, 0, 0); |
257 | return; |
257 | return; |
258 | } |
258 | } |
259 | fs_info->phone = IPC_GET_ARG3(call); |
259 | fs_info->phone = IPC_GET_ARG3(call); |
260 | ipc_answer_fast(callid, EOK, 0, 0); |
260 | ipc_answer_fast(callid, EOK, 0, 0); |
261 | 261 | ||
262 | dprintf("Callback connection to FS created.\n"); |
262 | dprintf("Callback connection to FS created.\n"); |
263 | 263 | ||
264 | futex_up(&fs_head_futex); |
264 | futex_up(&fs_head_futex); |
265 | 265 | ||
266 | /* |
266 | /* |
267 | * That was it. The FS has been registered. |
267 | * That was it. The FS has been registered. |
268 | */ |
268 | */ |
269 | ipc_answer_fast(rid, EOK, 0, 0); |
269 | ipc_answer_fast(rid, EOK, 0, 0); |
270 | dprintf("\"%s\" filesystem successfully registered.\n", |
270 | dprintf("\"%s\" filesystem successfully registered.\n", |
271 | fs_info->vfs_info.name); |
271 | fs_info->vfs_info.name); |
- | 272 | ||
272 | } |
273 | } |
273 | 274 | ||
274 | /** |
275 | /** |
275 | * @} |
276 | * @} |
276 | */ |
277 | */ |
277 | 278 |