1 | /*
|
---|
2 | * Copyright (c) 2008 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 | #include "vfs.h"
|
---|
30 |
|
---|
31 | #include <errno.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <str.h>
|
---|
34 | #include <vfs/canonify.h>
|
---|
35 |
|
---|
36 | static void vfs_in_clone(ipc_callid_t rid, ipc_call_t *request)
|
---|
37 | {
|
---|
38 | int oldfd = IPC_GET_ARG1(*request);
|
---|
39 | bool desc = IPC_GET_ARG2(*request);
|
---|
40 |
|
---|
41 | int ret = vfs_op_clone(oldfd, desc);
|
---|
42 | async_answer_0(rid, ret);
|
---|
43 | }
|
---|
44 |
|
---|
45 | static void vfs_in_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
46 | {
|
---|
47 | int fd = IPC_GET_ARG1(*request);
|
---|
48 | int rc = vfs_op_close(fd);
|
---|
49 | async_answer_0(rid, rc);
|
---|
50 | }
|
---|
51 |
|
---|
52 | static void vfs_in_dup(ipc_callid_t rid, ipc_call_t *request)
|
---|
53 | {
|
---|
54 | int oldfd = IPC_GET_ARG1(*request);
|
---|
55 | int newfd = IPC_GET_ARG2(*request);
|
---|
56 | int rc = vfs_op_dup(oldfd, newfd);
|
---|
57 | async_answer_1(rid, rc, newfd);
|
---|
58 | }
|
---|
59 |
|
---|
60 | static void vfs_in_fstat(ipc_callid_t rid, ipc_call_t *request)
|
---|
61 | {
|
---|
62 | int fd = IPC_GET_ARG1(*request);
|
---|
63 | int rc = vfs_op_fstat(fd);
|
---|
64 | async_answer_0(rid, rc);
|
---|
65 | }
|
---|
66 |
|
---|
67 | static void vfs_in_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
68 | {
|
---|
69 | int mpfd = IPC_GET_ARG1(*request);
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * We expect the library to do the device-name to device-handle
|
---|
73 | * translation for us, thus the device handle will arrive as ARG1
|
---|
74 | * in the request.
|
---|
75 | */
|
---|
76 | service_id_t service_id = (service_id_t) IPC_GET_ARG2(*request);
|
---|
77 |
|
---|
78 | unsigned int flags = (unsigned int) IPC_GET_ARG3(*request);
|
---|
79 | unsigned int instance = IPC_GET_ARG4(*request);
|
---|
80 |
|
---|
81 | char *opts = NULL;
|
---|
82 | char *fs_name = NULL;
|
---|
83 |
|
---|
84 | /* Now we expect to receive the mount options. */
|
---|
85 | int rc = async_data_write_accept((void **) &opts, true, 0,
|
---|
86 | MAX_MNTOPTS_LEN, 0, NULL);
|
---|
87 | if (rc != EOK) {
|
---|
88 | async_answer_0(rid, rc);
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Now, we expect the client to send us data with the name of the file
|
---|
93 | * system.
|
---|
94 | */
|
---|
95 | rc = async_data_write_accept((void **) &fs_name, true, 0,
|
---|
96 | FS_NAME_MAXLEN, 0, NULL);
|
---|
97 | if (rc != EOK) {
|
---|
98 | free(opts);
|
---|
99 | async_answer_0(rid, rc);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 |
|
---|
103 | int outfd = 0;
|
---|
104 | rc = vfs_op_mount(mpfd, service_id, flags, instance, opts, fs_name,
|
---|
105 | &outfd);
|
---|
106 | async_answer_1(rid, rc, outfd);
|
---|
107 |
|
---|
108 | free(opts);
|
---|
109 | free(fs_name);
|
---|
110 | }
|
---|
111 |
|
---|
112 | static void vfs_in_open2(ipc_callid_t rid, ipc_call_t *request)
|
---|
113 | {
|
---|
114 | int fd = IPC_GET_ARG1(*request);
|
---|
115 | int flags = IPC_GET_ARG2(*request);
|
---|
116 |
|
---|
117 | int rc = vfs_op_open2(fd, flags);
|
---|
118 | async_answer_0(rid, rc);
|
---|
119 | }
|
---|
120 |
|
---|
121 | static void vfs_in_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
122 | {
|
---|
123 | int fd = IPC_GET_ARG1(*request);
|
---|
124 |
|
---|
125 | size_t bytes = 0;
|
---|
126 | int rc = vfs_op_read(fd, &bytes);
|
---|
127 | async_answer_1(rid, rc, bytes);
|
---|
128 | }
|
---|
129 |
|
---|
130 | static void vfs_in_rename(ipc_callid_t rid, ipc_call_t *request)
|
---|
131 | {
|
---|
132 | /* The common base directory. */
|
---|
133 | int basefd;
|
---|
134 | char *old = NULL;
|
---|
135 | char *new = NULL;
|
---|
136 | int rc;
|
---|
137 |
|
---|
138 | basefd = IPC_GET_ARG1(*request);
|
---|
139 |
|
---|
140 | /* Retrieve the old path. */
|
---|
141 | rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
|
---|
142 | if (rc != EOK)
|
---|
143 | goto out;
|
---|
144 |
|
---|
145 | /* Retrieve the new path. */
|
---|
146 | rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
|
---|
147 | if (rc != EOK)
|
---|
148 | goto out;
|
---|
149 |
|
---|
150 | size_t olen;
|
---|
151 | size_t nlen;
|
---|
152 | char *oldc = canonify(old, &olen);
|
---|
153 | char *newc = canonify(new, &nlen);
|
---|
154 |
|
---|
155 | if ((!oldc) || (!newc)) {
|
---|
156 | rc = EINVAL;
|
---|
157 | goto out;
|
---|
158 | }
|
---|
159 |
|
---|
160 | assert(oldc[olen] == '\0');
|
---|
161 | assert(newc[nlen] == '\0');
|
---|
162 |
|
---|
163 | rc = vfs_op_rename(basefd, oldc, newc);
|
---|
164 |
|
---|
165 | out:
|
---|
166 | async_answer_0(rid, rc);
|
---|
167 |
|
---|
168 | if (old)
|
---|
169 | free(old);
|
---|
170 | if (new)
|
---|
171 | free(new);
|
---|
172 | }
|
---|
173 |
|
---|
174 | static void vfs_in_seek(ipc_callid_t rid, ipc_call_t *request)
|
---|
175 | {
|
---|
176 | int fd = (int) IPC_GET_ARG1(*request);
|
---|
177 | int64_t off = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(*request), IPC_GET_ARG3(*request));
|
---|
178 | int whence = (int) IPC_GET_ARG4(*request);
|
---|
179 |
|
---|
180 | int64_t new_offset = 0;
|
---|
181 | int rc = vfs_op_seek(fd, off, whence, &new_offset);
|
---|
182 | async_answer_2(rid, rc, LOWER32(new_offset), UPPER32(new_offset));
|
---|
183 | }
|
---|
184 |
|
---|
185 | static void vfs_in_statfs(ipc_callid_t rid, ipc_call_t *request)
|
---|
186 | {
|
---|
187 | int fd = (int) IPC_GET_ARG1(*request);
|
---|
188 |
|
---|
189 | int rc = vfs_op_statfs(fd);
|
---|
190 | async_answer_0(rid, rc);
|
---|
191 | }
|
---|
192 |
|
---|
193 | static void vfs_in_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
194 | {
|
---|
195 | int fd = IPC_GET_ARG1(*request);
|
---|
196 | int rc = vfs_op_sync(fd);
|
---|
197 | async_answer_0(rid, rc);
|
---|
198 | }
|
---|
199 |
|
---|
200 | static void vfs_in_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
201 | {
|
---|
202 | int fd = IPC_GET_ARG1(*request);
|
---|
203 | int64_t size = MERGE_LOUP32(IPC_GET_ARG2(*request), IPC_GET_ARG3(*request));
|
---|
204 | int rc = vfs_op_truncate(fd, size);
|
---|
205 | async_answer_0(rid, rc);
|
---|
206 | }
|
---|
207 |
|
---|
208 | static void vfs_in_unlink2(ipc_callid_t rid, ipc_call_t *request)
|
---|
209 | {
|
---|
210 | int parentfd = IPC_GET_ARG1(*request);
|
---|
211 | int expectfd = IPC_GET_ARG2(*request);
|
---|
212 | int wflag = IPC_GET_ARG3(*request);
|
---|
213 |
|
---|
214 | char *path;
|
---|
215 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
216 | if (rc == EOK)
|
---|
217 | rc = vfs_op_unlink2(parentfd, expectfd, wflag, path);
|
---|
218 |
|
---|
219 | async_answer_0(rid, rc);
|
---|
220 | }
|
---|
221 |
|
---|
222 | static void vfs_in_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
223 | {
|
---|
224 | int mpfd = IPC_GET_ARG1(*request);
|
---|
225 | int rc = vfs_op_unmount(mpfd);
|
---|
226 | async_answer_0(rid, rc);
|
---|
227 | }
|
---|
228 |
|
---|
229 | static void vfs_in_wait_handle(ipc_callid_t rid, ipc_call_t *request)
|
---|
230 | {
|
---|
231 | bool high_fd = IPC_GET_ARG1(*request);
|
---|
232 | int fd = vfs_op_wait_handle(high_fd);
|
---|
233 | async_answer_1(rid, EOK, fd);
|
---|
234 | }
|
---|
235 |
|
---|
236 | static void vfs_in_walk(ipc_callid_t rid, ipc_call_t *request)
|
---|
237 | {
|
---|
238 | /*
|
---|
239 | * Parent is our relative root for file lookup.
|
---|
240 | * For defined flags, see <ipc/vfs.h>.
|
---|
241 | */
|
---|
242 | int parentfd = IPC_GET_ARG1(*request);
|
---|
243 | int flags = IPC_GET_ARG2(*request);
|
---|
244 |
|
---|
245 | int fd = 0;
|
---|
246 | char *path;
|
---|
247 | int rc = async_data_write_accept((void **)&path, true, 0, 0, 0, NULL);
|
---|
248 | if (rc == EOK) {
|
---|
249 | rc = vfs_op_walk(parentfd, flags, path, &fd);
|
---|
250 | free(path);
|
---|
251 | }
|
---|
252 | async_answer_1(rid, rc, fd);
|
---|
253 | }
|
---|
254 |
|
---|
255 | static void vfs_in_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
256 | {
|
---|
257 | int fd = IPC_GET_ARG1(*request);
|
---|
258 |
|
---|
259 | size_t bytes = 0;
|
---|
260 | int rc = vfs_op_write(fd, &bytes);
|
---|
261 | async_answer_1(rid, rc, bytes);
|
---|
262 | }
|
---|
263 |
|
---|
264 | void vfs_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
265 | {
|
---|
266 | bool cont = true;
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * The connection was opened via the IPC_CONNECT_ME_TO call.
|
---|
270 | * This call needs to be answered.
|
---|
271 | */
|
---|
272 | async_answer_0(iid, EOK);
|
---|
273 |
|
---|
274 | while (cont) {
|
---|
275 | ipc_call_t call;
|
---|
276 | ipc_callid_t callid = async_get_call(&call);
|
---|
277 |
|
---|
278 | if (!IPC_GET_IMETHOD(call))
|
---|
279 | break;
|
---|
280 |
|
---|
281 | switch (IPC_GET_IMETHOD(call)) {
|
---|
282 | case VFS_IN_CLONE:
|
---|
283 | vfs_in_clone(callid, &call);
|
---|
284 | break;
|
---|
285 | case VFS_IN_CLOSE:
|
---|
286 | vfs_in_close(callid, &call);
|
---|
287 | break;
|
---|
288 | case VFS_IN_DUP:
|
---|
289 | vfs_in_dup(callid, &call);
|
---|
290 | break;
|
---|
291 | case VFS_IN_FSTAT:
|
---|
292 | vfs_in_fstat(callid, &call);
|
---|
293 | break;
|
---|
294 | case VFS_IN_MOUNT:
|
---|
295 | vfs_in_mount(callid, &call);
|
---|
296 | break;
|
---|
297 | case VFS_IN_OPEN2:
|
---|
298 | vfs_in_open2(callid, &call);
|
---|
299 | break;
|
---|
300 | case VFS_IN_READ:
|
---|
301 | vfs_in_read(callid, &call);
|
---|
302 | break;
|
---|
303 | case VFS_IN_REGISTER:
|
---|
304 | vfs_register(callid, &call);
|
---|
305 | cont = false;
|
---|
306 | break;
|
---|
307 | case VFS_IN_RENAME:
|
---|
308 | vfs_in_rename(callid, &call);
|
---|
309 | break;
|
---|
310 | case VFS_IN_SEEK:
|
---|
311 | vfs_in_seek(callid, &call);
|
---|
312 | break;
|
---|
313 | case VFS_IN_STATFS:
|
---|
314 | vfs_in_statfs(callid, &call);
|
---|
315 | break;
|
---|
316 | case VFS_IN_SYNC:
|
---|
317 | vfs_in_sync(callid, &call);
|
---|
318 | break;
|
---|
319 | case VFS_IN_TRUNCATE:
|
---|
320 | vfs_in_truncate(callid, &call);
|
---|
321 | break;
|
---|
322 | case VFS_IN_UNLINK2:
|
---|
323 | vfs_in_unlink2(callid, &call);
|
---|
324 | break;
|
---|
325 | case VFS_IN_UNMOUNT:
|
---|
326 | vfs_in_unmount(callid, &call);
|
---|
327 | break;
|
---|
328 | case VFS_IN_WAIT_HANDLE:
|
---|
329 | vfs_in_wait_handle(callid, &call);
|
---|
330 | break;
|
---|
331 | case VFS_IN_WALK:
|
---|
332 | vfs_in_walk(callid, &call);
|
---|
333 | break;
|
---|
334 | case VFS_IN_WRITE:
|
---|
335 | vfs_in_write(callid, &call);
|
---|
336 | break;
|
---|
337 | default:
|
---|
338 | async_answer_0(callid, ENOTSUP);
|
---|
339 | break;
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | /*
|
---|
344 | * Open files for this client will be cleaned up when its last
|
---|
345 | * connection fibril terminates.
|
---|
346 | */
|
---|
347 | }
|
---|