1 | /*
|
---|
2 | * Copyright (c) 2007 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
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
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
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
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
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
|
---|
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
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file vfs.c
|
---|
35 | * @brief VFS_REGISTER method.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <ipc/ipc.h>
|
---|
39 | #include <ipc/services.h>
|
---|
40 | #include <async.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <string.h>
|
---|
45 | #include <ctype.h>
|
---|
46 | #include <bool.h>
|
---|
47 | #include <futex.h>
|
---|
48 | #include <libadt/list.h>
|
---|
49 | #include "vfs.h"
|
---|
50 |
|
---|
51 | atomic_t fs_head_futex = FUTEX_INITIALIZER;
|
---|
52 | link_t fs_head;
|
---|
53 |
|
---|
54 | /** Verify the VFS info structure.
|
---|
55 | *
|
---|
56 | * @param info Info structure to be verified.
|
---|
57 | *
|
---|
58 | * @return Non-zero if the info structure is sane, zero otherwise.
|
---|
59 | */
|
---|
60 | static bool vfs_info_sane(vfs_info_t *info)
|
---|
61 | {
|
---|
62 | int i;
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * Check if the name is non-empty and is composed solely of ASCII
|
---|
66 | * characters [a-z]+[a-z0-9_-]*.
|
---|
67 | */
|
---|
68 | if (!islower(info->name[0])) {
|
---|
69 | dprintf("The name doesn't start with a lowercase character.\n");
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 | for (i = 1; i < FS_NAME_MAXLEN; i++) {
|
---|
73 | if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
|
---|
74 | (info->name[i] != '-') && (info->name[i] != '_')) {
|
---|
75 | if (info->name[i] == '\0') {
|
---|
76 | break;
|
---|
77 | } else {
|
---|
78 | dprintf("The name contains illegal "
|
---|
79 | "characters.\n");
|
---|
80 | return false;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Check if the FS implements mandatory VFS operations.
|
---|
88 | */
|
---|
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");
|
---|
91 | return false;
|
---|
92 | }
|
---|
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");
|
---|
95 | return false;
|
---|
96 | }
|
---|
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");
|
---|
99 | return false;
|
---|
100 | }
|
---|
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");
|
---|
103 | return false;
|
---|
104 | }
|
---|
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");
|
---|
107 | return false;
|
---|
108 | }
|
---|
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");
|
---|
111 | return false;
|
---|
112 | }
|
---|
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");
|
---|
115 | return false;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Check if each operation is either not defined, defined or default.
|
---|
120 | */
|
---|
121 | for (i = VFS_FIRST; i < VFS_LAST; i++) {
|
---|
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) &&
|
---|
124 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
|
---|
125 | dprintf("Operation info not understood.\n");
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /** VFS_REGISTER protocol function.
|
---|
133 | *
|
---|
134 | * @param rid Hash of the call with the request.
|
---|
135 | * @param request Call structure with the request.
|
---|
136 | */
|
---|
137 | void vfs_register(ipc_callid_t rid, ipc_call_t *request)
|
---|
138 | {
|
---|
139 | ipc_callid_t callid;
|
---|
140 | ipc_call_t call;
|
---|
141 | int rc;
|
---|
142 | size_t size;
|
---|
143 |
|
---|
144 | dprintf("Processing VFS_REGISTER request received from %p.\n",
|
---|
145 | request->in_phone_hash);
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * The first call has to be IPC_M_DATA_SEND in which we receive the
|
---|
149 | * VFS info structure from the client FS.
|
---|
150 | */
|
---|
151 | if (!ipc_data_receive(&callid, &call, NULL, &size)) {
|
---|
152 | /*
|
---|
153 | * The client doesn't obey the same protocol as we do.
|
---|
154 | */
|
---|
155 | dprintf("Receiving of VFS info failed.\n");
|
---|
156 | ipc_answer_fast(callid, EINVAL, 0, 0);
|
---|
157 | ipc_answer_fast(rid, EINVAL, 0, 0);
|
---|
158 | return;
|
---|
159 | }
|
---|
160 |
|
---|
161 | dprintf("VFS info received, size = %d\n", size);
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * We know the size of the VFS info structure. See if the client
|
---|
165 | * understands this easy concept too.
|
---|
166 | */
|
---|
167 | if (size != sizeof(vfs_info_t)) {
|
---|
168 | /*
|
---|
169 | * The client is sending us something, which cannot be
|
---|
170 | * the info structure.
|
---|
171 | */
|
---|
172 | dprintf("Received VFS info has bad size.\n");
|
---|
173 | ipc_answer_fast(callid, EINVAL, 0, 0);
|
---|
174 | ipc_answer_fast(rid, EINVAL, 0, 0);
|
---|
175 | return;
|
---|
176 | }
|
---|
177 | fs_info_t *fs_info;
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * Allocate and initialize a buffer for the fs_info structure.
|
---|
181 | */
|
---|
182 | fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
|
---|
183 | if (!fs_info) {
|
---|
184 | dprintf("Could not allocate memory for FS info.\n");
|
---|
185 | ipc_answer_fast(callid, ENOMEM, 0, 0);
|
---|
186 | ipc_answer_fast(rid, ENOMEM, 0, 0);
|
---|
187 | return;
|
---|
188 | }
|
---|
189 | link_initialize(&fs_info->fs_link);
|
---|
190 |
|
---|
191 | rc = ipc_data_deliver(callid, &call, &fs_info->vfs_info, size);
|
---|
192 | if (rc != EOK) {
|
---|
193 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
|
---|
194 | rc);
|
---|
195 | free(fs_info);
|
---|
196 | ipc_answer_fast(callid, rc, 0, 0);
|
---|
197 | ipc_answer_fast(rid, rc, 0, 0);
|
---|
198 | return;
|
---|
199 | }
|
---|
200 |
|
---|
201 | dprintf("VFS info delivered.\n");
|
---|
202 |
|
---|
203 | if (!vfs_info_sane(&fs_info->vfs_info)) {
|
---|
204 | free(fs_info);
|
---|
205 | ipc_answer_fast(callid, EINVAL, 0, 0);
|
---|
206 | ipc_answer_fast(rid, EINVAL, 0, 0);
|
---|
207 | return;
|
---|
208 | }
|
---|
209 |
|
---|
210 | futex_down(&fs_head_futex);
|
---|
211 |
|
---|
212 | /*
|
---|
213 | * Check for duplicit registrations.
|
---|
214 | */
|
---|
215 | link_t *cur;
|
---|
216 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
|
---|
217 | fs_info_t *fi = list_get_instance(cur, fs_info_t,
|
---|
218 | fs_link);
|
---|
219 | /* TODO: replace strcmp with strncmp once we have it */
|
---|
220 | if (strcmp(fs_info->vfs_info.name, fi->vfs_info.name) == 0) {
|
---|
221 | /*
|
---|
222 | * We already register a fs like this.
|
---|
223 | */
|
---|
224 | dprintf("FS is already registered.\n");
|
---|
225 | futex_up(&fs_head_futex);
|
---|
226 | free(fs_info);
|
---|
227 | ipc_answer_fast(callid, EEXISTS, 0, 0);
|
---|
228 | ipc_answer_fast(rid, EEXISTS, 0, 0);
|
---|
229 | return;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * Add fs_info to the list of registered FS's.
|
---|
235 | */
|
---|
236 | dprintf("Adding FS into the registered list.\n");
|
---|
237 | list_append(&fs_info->fs_link, &fs_head);
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * ACK receiving a properly formatted, non-duplicit vfs_info.
|
---|
241 | */
|
---|
242 | ipc_answer_fast(callid, EOK, 0, 0);
|
---|
243 |
|
---|
244 | /*
|
---|
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
|
---|
247 | * which to forward VFS requests to it.
|
---|
248 | */
|
---|
249 | callid = async_get_call(&call);
|
---|
250 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
|
---|
251 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
|
---|
252 | list_remove(&fs_info->fs_link);
|
---|
253 | futex_up(&fs_head_futex);
|
---|
254 | free(fs_info);
|
---|
255 | ipc_answer_fast(callid, EINVAL, 0, 0);
|
---|
256 | ipc_answer_fast(rid, EINVAL, 0, 0);
|
---|
257 | return;
|
---|
258 | }
|
---|
259 | fs_info->phone = IPC_GET_ARG3(call);
|
---|
260 | ipc_answer_fast(callid, EOK, 0, 0);
|
---|
261 |
|
---|
262 | dprintf("Callback connection to FS created.\n");
|
---|
263 |
|
---|
264 | futex_up(&fs_head_futex);
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * That was it. The FS has been registered.
|
---|
268 | */
|
---|
269 | ipc_answer_fast(rid, EOK, 0, 0);
|
---|
270 | dprintf("\"%s\" filesystem successfully registered.\n",
|
---|
271 | fs_info->vfs_info.name);
|
---|
272 |
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * @}
|
---|
277 | */
|
---|