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