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 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file vfs_ops.c
|
---|
35 | * @brief Operations that VFS offers to its clients.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include "vfs.h"
|
---|
39 | #include <macros.h>
|
---|
40 | #include <stdint.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <str.h>
|
---|
46 | #include <bool.h>
|
---|
47 | #include <fibril_synch.h>
|
---|
48 | #include <adt/list.h>
|
---|
49 | #include <unistd.h>
|
---|
50 | #include <ctype.h>
|
---|
51 | #include <fcntl.h>
|
---|
52 | #include <assert.h>
|
---|
53 | #include <vfs/canonify.h>
|
---|
54 |
|
---|
55 | /* Forward declarations of static functions. */
|
---|
56 | static int vfs_truncate_internal(fs_handle_t, devmap_handle_t, fs_index_t,
|
---|
57 | aoff64_t);
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
|
---|
61 | * concurrent VFS operation which modifies the file system namespace.
|
---|
62 | */
|
---|
63 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
|
---|
64 |
|
---|
65 | vfs_pair_t rootfs = {
|
---|
66 | .fs_handle = 0,
|
---|
67 | .devmap_handle = 0
|
---|
68 | };
|
---|
69 |
|
---|
70 | static void vfs_mount_internal(ipc_callid_t rid, devmap_handle_t devmap_handle,
|
---|
71 | fs_handle_t fs_handle, char *mp, char *opts)
|
---|
72 | {
|
---|
73 | vfs_lookup_res_t mp_res;
|
---|
74 | vfs_lookup_res_t mr_res;
|
---|
75 | vfs_node_t *mp_node = NULL;
|
---|
76 | vfs_node_t *mr_node;
|
---|
77 | fs_index_t rindex;
|
---|
78 | aoff64_t rsize;
|
---|
79 | unsigned rlnkcnt;
|
---|
80 | async_exch_t *exch;
|
---|
81 | sysarg_t rc;
|
---|
82 | aid_t msg;
|
---|
83 | ipc_call_t answer;
|
---|
84 |
|
---|
85 | /* Resolve the path to the mountpoint. */
|
---|
86 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
87 | if (rootfs.fs_handle) {
|
---|
88 | /* We already have the root FS. */
|
---|
89 | if (str_cmp(mp, "/") == 0) {
|
---|
90 | /* Trying to mount root FS over root FS */
|
---|
91 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
92 | async_answer_0(rid, EBUSY);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
|
---|
97 | if (rc != EOK) {
|
---|
98 | /* The lookup failed for some reason. */
|
---|
99 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
100 | async_answer_0(rid, rc);
|
---|
101 | return;
|
---|
102 | }
|
---|
103 |
|
---|
104 | mp_node = vfs_node_get(&mp_res);
|
---|
105 | if (!mp_node) {
|
---|
106 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
107 | async_answer_0(rid, ENOMEM);
|
---|
108 | return;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * Now we hold a reference to mp_node.
|
---|
113 | * It will be dropped upon the corresponding VFS_IN_UNMOUNT.
|
---|
114 | * This prevents the mount point from being deleted.
|
---|
115 | */
|
---|
116 | } else {
|
---|
117 | /* We still don't have the root file system mounted. */
|
---|
118 | if (str_cmp(mp, "/") == 0) {
|
---|
119 | /*
|
---|
120 | * For this simple, but important case,
|
---|
121 | * we are almost done.
|
---|
122 | */
|
---|
123 |
|
---|
124 | /* Tell the mountee that it is being mounted. */
|
---|
125 | exch = vfs_exchange_grab(fs_handle);
|
---|
126 | msg = async_send_1(exch, VFS_OUT_MOUNTED,
|
---|
127 | (sysarg_t) devmap_handle, &answer);
|
---|
128 | /* Send the mount options */
|
---|
129 | rc = async_data_write_start(exch, (void *)opts,
|
---|
130 | str_size(opts));
|
---|
131 | vfs_exchange_release(exch);
|
---|
132 |
|
---|
133 | if (rc != EOK) {
|
---|
134 | async_wait_for(msg, NULL);
|
---|
135 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
136 | async_answer_0(rid, rc);
|
---|
137 | return;
|
---|
138 | }
|
---|
139 | async_wait_for(msg, &rc);
|
---|
140 |
|
---|
141 | if (rc != EOK) {
|
---|
142 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
143 | async_answer_0(rid, rc);
|
---|
144 | return;
|
---|
145 | }
|
---|
146 |
|
---|
147 | rindex = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
148 | rsize = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
|
---|
149 | rlnkcnt = (unsigned) IPC_GET_ARG4(answer);
|
---|
150 |
|
---|
151 | mr_res.triplet.fs_handle = fs_handle;
|
---|
152 | mr_res.triplet.devmap_handle = devmap_handle;
|
---|
153 | mr_res.triplet.index = rindex;
|
---|
154 | mr_res.size = rsize;
|
---|
155 | mr_res.lnkcnt = rlnkcnt;
|
---|
156 | mr_res.type = VFS_NODE_DIRECTORY;
|
---|
157 |
|
---|
158 | rootfs.fs_handle = fs_handle;
|
---|
159 | rootfs.devmap_handle = devmap_handle;
|
---|
160 |
|
---|
161 | /* Add reference to the mounted root. */
|
---|
162 | mr_node = vfs_node_get(&mr_res);
|
---|
163 | assert(mr_node);
|
---|
164 |
|
---|
165 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
166 | async_answer_0(rid, rc);
|
---|
167 | return;
|
---|
168 | } else {
|
---|
169 | /*
|
---|
170 | * We can't resolve this without the root filesystem
|
---|
171 | * being mounted first.
|
---|
172 | */
|
---|
173 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
174 | async_answer_0(rid, ENOENT);
|
---|
175 | return;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * At this point, we have all necessary pieces: file system and device
|
---|
181 | * handles, and we know the mount point VFS node.
|
---|
182 | */
|
---|
183 |
|
---|
184 | async_exch_t *mountee_exch = vfs_exchange_grab(fs_handle);
|
---|
185 | assert(mountee_exch);
|
---|
186 |
|
---|
187 | exch = vfs_exchange_grab(mp_res.triplet.fs_handle);
|
---|
188 | msg = async_send_4(exch, VFS_OUT_MOUNT,
|
---|
189 | (sysarg_t) mp_res.triplet.devmap_handle,
|
---|
190 | (sysarg_t) mp_res.triplet.index,
|
---|
191 | (sysarg_t) fs_handle,
|
---|
192 | (sysarg_t) devmap_handle, &answer);
|
---|
193 |
|
---|
194 | /* Send connection */
|
---|
195 | rc = async_exchange_clone(exch, mountee_exch);
|
---|
196 | vfs_exchange_release(mountee_exch);
|
---|
197 |
|
---|
198 | if (rc != EOK) {
|
---|
199 | vfs_exchange_release(exch);
|
---|
200 | async_wait_for(msg, NULL);
|
---|
201 |
|
---|
202 | /* Mount failed, drop reference to mp_node. */
|
---|
203 | if (mp_node)
|
---|
204 | vfs_node_put(mp_node);
|
---|
205 |
|
---|
206 | async_answer_0(rid, rc);
|
---|
207 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
208 | return;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /* send the mount options */
|
---|
212 | rc = async_data_write_start(exch, (void *) opts, str_size(opts));
|
---|
213 | if (rc != EOK) {
|
---|
214 | vfs_exchange_release(exch);
|
---|
215 | async_wait_for(msg, NULL);
|
---|
216 |
|
---|
217 | /* Mount failed, drop reference to mp_node. */
|
---|
218 | if (mp_node)
|
---|
219 | vfs_node_put(mp_node);
|
---|
220 |
|
---|
221 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
222 | async_answer_0(rid, rc);
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 | vfs_exchange_release(exch);
|
---|
227 | async_wait_for(msg, &rc);
|
---|
228 |
|
---|
229 | if (rc == EOK) {
|
---|
230 | rindex = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
231 | rsize = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
232 | IPC_GET_ARG3(answer));
|
---|
233 | rlnkcnt = (unsigned) IPC_GET_ARG4(answer);
|
---|
234 |
|
---|
235 | mr_res.triplet.fs_handle = fs_handle;
|
---|
236 | mr_res.triplet.devmap_handle = devmap_handle;
|
---|
237 | mr_res.triplet.index = rindex;
|
---|
238 | mr_res.size = rsize;
|
---|
239 | mr_res.lnkcnt = rlnkcnt;
|
---|
240 | mr_res.type = VFS_NODE_DIRECTORY;
|
---|
241 |
|
---|
242 | /* Add reference to the mounted root. */
|
---|
243 | mr_node = vfs_node_get(&mr_res);
|
---|
244 | assert(mr_node);
|
---|
245 | } else {
|
---|
246 | /* Mount failed, drop reference to mp_node. */
|
---|
247 | if (mp_node)
|
---|
248 | vfs_node_put(mp_node);
|
---|
249 | }
|
---|
250 |
|
---|
251 | async_answer_0(rid, rc);
|
---|
252 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
253 | }
|
---|
254 |
|
---|
255 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
256 | {
|
---|
257 | devmap_handle_t devmap_handle;
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * We expect the library to do the device-name to device-handle
|
---|
261 | * translation for us, thus the device handle will arrive as ARG1
|
---|
262 | * in the request.
|
---|
263 | */
|
---|
264 | devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * Mount flags are passed as ARG2.
|
---|
268 | */
|
---|
269 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
|
---|
270 |
|
---|
271 | /*
|
---|
272 | * For now, don't make use of ARG3, but it can be used to
|
---|
273 | * carry mount options in the future.
|
---|
274 | */
|
---|
275 |
|
---|
276 | /* We want the client to send us the mount point. */
|
---|
277 | char *mp;
|
---|
278 | int rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
|
---|
279 | 0, NULL);
|
---|
280 | if (rc != EOK) {
|
---|
281 | async_answer_0(rid, rc);
|
---|
282 | return;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /* Now we expect to receive the mount options. */
|
---|
286 | char *opts;
|
---|
287 | rc = async_data_write_accept((void **) &opts, true, 0, MAX_MNTOPTS_LEN,
|
---|
288 | 0, NULL);
|
---|
289 | if (rc != EOK) {
|
---|
290 | free(mp);
|
---|
291 | async_answer_0(rid, rc);
|
---|
292 | return;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /*
|
---|
296 | * Now, we expect the client to send us data with the name of the file
|
---|
297 | * system.
|
---|
298 | */
|
---|
299 | char *fs_name;
|
---|
300 | rc = async_data_write_accept((void **) &fs_name, true, 0,
|
---|
301 | FS_NAME_MAXLEN, 0, NULL);
|
---|
302 | if (rc != EOK) {
|
---|
303 | free(mp);
|
---|
304 | free(opts);
|
---|
305 | async_answer_0(rid, rc);
|
---|
306 | return;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /*
|
---|
310 | * Wait for VFS_IN_PING so that we can return an error if we don't know
|
---|
311 | * fs_name.
|
---|
312 | */
|
---|
313 | ipc_call_t data;
|
---|
314 | ipc_callid_t callid = async_get_call(&data);
|
---|
315 | if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
|
---|
316 | async_answer_0(callid, ENOTSUP);
|
---|
317 | async_answer_0(rid, ENOTSUP);
|
---|
318 | free(mp);
|
---|
319 | free(opts);
|
---|
320 | free(fs_name);
|
---|
321 | return;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /*
|
---|
325 | * Check if we know a file system with the same name as is in fs_name.
|
---|
326 | * This will also give us its file system handle.
|
---|
327 | */
|
---|
328 | fibril_mutex_lock(&fs_list_lock);
|
---|
329 | fs_handle_t fs_handle;
|
---|
330 | recheck:
|
---|
331 | fs_handle = fs_name_to_handle(fs_name, false);
|
---|
332 | if (!fs_handle) {
|
---|
333 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
334 | fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
|
---|
335 | goto recheck;
|
---|
336 | }
|
---|
337 |
|
---|
338 | fibril_mutex_unlock(&fs_list_lock);
|
---|
339 | async_answer_0(callid, ENOENT);
|
---|
340 | async_answer_0(rid, ENOENT);
|
---|
341 | free(mp);
|
---|
342 | free(fs_name);
|
---|
343 | free(opts);
|
---|
344 | return;
|
---|
345 | }
|
---|
346 | fibril_mutex_unlock(&fs_list_lock);
|
---|
347 |
|
---|
348 | /* Acknowledge that we know fs_name. */
|
---|
349 | async_answer_0(callid, EOK);
|
---|
350 |
|
---|
351 | /* Do the mount */
|
---|
352 | vfs_mount_internal(rid, devmap_handle, fs_handle, mp, opts);
|
---|
353 | free(mp);
|
---|
354 | free(fs_name);
|
---|
355 | free(opts);
|
---|
356 | }
|
---|
357 |
|
---|
358 | void vfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
359 | {
|
---|
360 | int rc;
|
---|
361 | char *mp;
|
---|
362 | vfs_lookup_res_t mp_res;
|
---|
363 | vfs_lookup_res_t mr_res;
|
---|
364 | vfs_node_t *mr_node;
|
---|
365 | async_exch_t *exch;
|
---|
366 |
|
---|
367 | /*
|
---|
368 | * Receive the mount point path.
|
---|
369 | */
|
---|
370 | rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
|
---|
371 | 0, NULL);
|
---|
372 | if (rc != EOK)
|
---|
373 | async_answer_0(rid, rc);
|
---|
374 |
|
---|
375 | /*
|
---|
376 | * Taking the namespace lock will do two things for us. First, it will
|
---|
377 | * prevent races with other lookup operations. Second, it will stop new
|
---|
378 | * references to already existing VFS nodes and creation of new VFS
|
---|
379 | * nodes. This is because new references are added as a result of some
|
---|
380 | * lookup operation or at least of some operation which is protected by
|
---|
381 | * the namespace lock.
|
---|
382 | */
|
---|
383 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
384 |
|
---|
385 | /*
|
---|
386 | * Lookup the mounted root and instantiate it.
|
---|
387 | */
|
---|
388 | rc = vfs_lookup_internal(mp, L_ROOT, &mr_res, NULL);
|
---|
389 | if (rc != EOK) {
|
---|
390 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
391 | free(mp);
|
---|
392 | async_answer_0(rid, rc);
|
---|
393 | return;
|
---|
394 | }
|
---|
395 | mr_node = vfs_node_get(&mr_res);
|
---|
396 | if (!mr_node) {
|
---|
397 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
398 | free(mp);
|
---|
399 | async_answer_0(rid, ENOMEM);
|
---|
400 | return;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /*
|
---|
404 | * Count the total number of references for the mounted file system. We
|
---|
405 | * are expecting at least two. One which we got above and one which we
|
---|
406 | * got when the file system was mounted. If we find more, it means that
|
---|
407 | * the file system cannot be gracefully unmounted at the moment because
|
---|
408 | * someone is working with it.
|
---|
409 | */
|
---|
410 | if (vfs_nodes_refcount_sum_get(mr_node->fs_handle,
|
---|
411 | mr_node->devmap_handle) != 2) {
|
---|
412 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
413 | vfs_node_put(mr_node);
|
---|
414 | free(mp);
|
---|
415 | async_answer_0(rid, EBUSY);
|
---|
416 | return;
|
---|
417 | }
|
---|
418 |
|
---|
419 | if (str_cmp(mp, "/") == 0) {
|
---|
420 |
|
---|
421 | /*
|
---|
422 | * Unmounting the root file system.
|
---|
423 | *
|
---|
424 | * In this case, there is no mount point node and we send
|
---|
425 | * VFS_OUT_UNMOUNTED directly to the mounted file system.
|
---|
426 | */
|
---|
427 |
|
---|
428 | free(mp);
|
---|
429 |
|
---|
430 | exch = vfs_exchange_grab(mr_node->fs_handle);
|
---|
431 | rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
|
---|
432 | mr_node->devmap_handle);
|
---|
433 | vfs_exchange_release(exch);
|
---|
434 |
|
---|
435 | if (rc != EOK) {
|
---|
436 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
437 | vfs_node_put(mr_node);
|
---|
438 | async_answer_0(rid, rc);
|
---|
439 | return;
|
---|
440 | }
|
---|
441 |
|
---|
442 | rootfs.fs_handle = 0;
|
---|
443 | rootfs.devmap_handle = 0;
|
---|
444 | } else {
|
---|
445 |
|
---|
446 | /*
|
---|
447 | * Unmounting a non-root file system.
|
---|
448 | *
|
---|
449 | * We have a regular mount point node representing the parent
|
---|
450 | * file system, so we delegate the operation to it.
|
---|
451 | */
|
---|
452 |
|
---|
453 | rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
|
---|
454 | free(mp);
|
---|
455 | if (rc != EOK) {
|
---|
456 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
457 | vfs_node_put(mr_node);
|
---|
458 | async_answer_0(rid, rc);
|
---|
459 | return;
|
---|
460 | }
|
---|
461 |
|
---|
462 | vfs_node_t *mp_node = vfs_node_get(&mp_res);
|
---|
463 | if (!mp_node) {
|
---|
464 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
465 | vfs_node_put(mr_node);
|
---|
466 | async_answer_0(rid, ENOMEM);
|
---|
467 | return;
|
---|
468 | }
|
---|
469 |
|
---|
470 | exch = vfs_exchange_grab(mp_node->fs_handle);
|
---|
471 | rc = async_req_2_0(exch, VFS_OUT_UNMOUNT,
|
---|
472 | mp_node->devmap_handle, mp_node->index);
|
---|
473 | vfs_exchange_release(exch);
|
---|
474 |
|
---|
475 | if (rc != EOK) {
|
---|
476 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
477 | vfs_node_put(mp_node);
|
---|
478 | vfs_node_put(mr_node);
|
---|
479 | async_answer_0(rid, rc);
|
---|
480 | return;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /* Drop the reference we got above. */
|
---|
484 | vfs_node_put(mp_node);
|
---|
485 | /* Drop the reference from when the file system was mounted. */
|
---|
486 | vfs_node_put(mp_node);
|
---|
487 | }
|
---|
488 |
|
---|
489 | /*
|
---|
490 | * All went well, the mounted file system was successfully unmounted.
|
---|
491 | * The only thing left is to forget the unmounted root VFS node.
|
---|
492 | */
|
---|
493 | vfs_node_forget(mr_node);
|
---|
494 |
|
---|
495 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
496 | async_answer_0(rid, EOK);
|
---|
497 | }
|
---|
498 |
|
---|
499 | void vfs_open(ipc_callid_t rid, ipc_call_t *request)
|
---|
500 | {
|
---|
501 | /*
|
---|
502 | * The POSIX interface is open(path, oflag, mode).
|
---|
503 | * We can receive oflags and mode along with the VFS_IN_OPEN call;
|
---|
504 | * the path will need to arrive in another call.
|
---|
505 | *
|
---|
506 | * We also receive one private, non-POSIX set of flags called lflag
|
---|
507 | * used to pass information to vfs_lookup_internal().
|
---|
508 | */
|
---|
509 | int lflag = IPC_GET_ARG1(*request);
|
---|
510 | int oflag = IPC_GET_ARG2(*request);
|
---|
511 | int mode = IPC_GET_ARG3(*request);
|
---|
512 |
|
---|
513 | /* Ignore mode for now. */
|
---|
514 | (void) mode;
|
---|
515 |
|
---|
516 | /*
|
---|
517 | * Make sure that we are called with exactly one of L_FILE and
|
---|
518 | * L_DIRECTORY. Make sure that the user does not pass L_OPEN,
|
---|
519 | * L_ROOT or L_MP.
|
---|
520 | */
|
---|
521 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
|
---|
522 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
|
---|
523 | (lflag & (L_OPEN | L_ROOT | L_MP))) {
|
---|
524 | async_answer_0(rid, EINVAL);
|
---|
525 | return;
|
---|
526 | }
|
---|
527 |
|
---|
528 | if (oflag & O_CREAT)
|
---|
529 | lflag |= L_CREATE;
|
---|
530 | if (oflag & O_EXCL)
|
---|
531 | lflag |= L_EXCLUSIVE;
|
---|
532 |
|
---|
533 | char *path;
|
---|
534 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
535 | if (rc != EOK) {
|
---|
536 | async_answer_0(rid, rc);
|
---|
537 | return;
|
---|
538 | }
|
---|
539 |
|
---|
540 | /*
|
---|
541 | * Avoid the race condition in which the file can be deleted before we
|
---|
542 | * find/create-and-lock the VFS node corresponding to the looked-up
|
---|
543 | * triplet.
|
---|
544 | */
|
---|
545 | if (lflag & L_CREATE)
|
---|
546 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
547 | else
|
---|
548 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
549 |
|
---|
550 | /* The path is now populated and we can call vfs_lookup_internal(). */
|
---|
551 | vfs_lookup_res_t lr;
|
---|
552 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
|
---|
553 | if (rc != EOK) {
|
---|
554 | if (lflag & L_CREATE)
|
---|
555 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
556 | else
|
---|
557 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
558 | async_answer_0(rid, rc);
|
---|
559 | free(path);
|
---|
560 | return;
|
---|
561 | }
|
---|
562 |
|
---|
563 | /* Path is no longer needed. */
|
---|
564 | free(path);
|
---|
565 |
|
---|
566 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
567 | if (lflag & L_CREATE)
|
---|
568 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
569 | else
|
---|
570 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
571 |
|
---|
572 | /* Truncate the file if requested and if necessary. */
|
---|
573 | if (oflag & O_TRUNC) {
|
---|
574 | fibril_rwlock_write_lock(&node->contents_rwlock);
|
---|
575 | if (node->size) {
|
---|
576 | rc = vfs_truncate_internal(node->fs_handle,
|
---|
577 | node->devmap_handle, node->index, 0);
|
---|
578 | if (rc) {
|
---|
579 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
580 | vfs_node_put(node);
|
---|
581 | async_answer_0(rid, rc);
|
---|
582 | return;
|
---|
583 | }
|
---|
584 | node->size = 0;
|
---|
585 | }
|
---|
586 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
587 | }
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
---|
591 | * structure.
|
---|
592 | */
|
---|
593 | int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
|
---|
594 | if (fd < 0) {
|
---|
595 | vfs_node_put(node);
|
---|
596 | async_answer_0(rid, fd);
|
---|
597 | return;
|
---|
598 | }
|
---|
599 | vfs_file_t *file = vfs_file_get(fd);
|
---|
600 | assert(file);
|
---|
601 | file->node = node;
|
---|
602 | if (oflag & O_APPEND)
|
---|
603 | file->append = true;
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * The following increase in reference count is for the fact that the
|
---|
607 | * file is being opened and that a file structure is pointing to it.
|
---|
608 | * It is necessary so that the file will not disappear when
|
---|
609 | * vfs_node_put() is called. The reference will be dropped by the
|
---|
610 | * respective VFS_IN_CLOSE.
|
---|
611 | */
|
---|
612 | vfs_node_addref(node);
|
---|
613 | vfs_node_put(node);
|
---|
614 | vfs_file_put(file);
|
---|
615 |
|
---|
616 | /* Success! Return the new file descriptor to the client. */
|
---|
617 | async_answer_1(rid, EOK, fd);
|
---|
618 | }
|
---|
619 |
|
---|
620 | void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
621 | {
|
---|
622 | // FIXME: check for sanity of the supplied fs, dev and index
|
---|
623 |
|
---|
624 | /*
|
---|
625 | * The interface is open_node(fs, dev, index, oflag).
|
---|
626 | */
|
---|
627 | vfs_lookup_res_t lr;
|
---|
628 |
|
---|
629 | lr.triplet.fs_handle = IPC_GET_ARG1(*request);
|
---|
630 | lr.triplet.devmap_handle = IPC_GET_ARG2(*request);
|
---|
631 | lr.triplet.index = IPC_GET_ARG3(*request);
|
---|
632 | int oflag = IPC_GET_ARG4(*request);
|
---|
633 |
|
---|
634 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
635 |
|
---|
636 | int rc = vfs_open_node_internal(&lr);
|
---|
637 | if (rc != EOK) {
|
---|
638 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
639 | async_answer_0(rid, rc);
|
---|
640 | return;
|
---|
641 | }
|
---|
642 |
|
---|
643 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
644 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
645 |
|
---|
646 | /* Truncate the file if requested and if necessary. */
|
---|
647 | if (oflag & O_TRUNC) {
|
---|
648 | fibril_rwlock_write_lock(&node->contents_rwlock);
|
---|
649 | if (node->size) {
|
---|
650 | rc = vfs_truncate_internal(node->fs_handle,
|
---|
651 | node->devmap_handle, node->index, 0);
|
---|
652 | if (rc) {
|
---|
653 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
654 | vfs_node_put(node);
|
---|
655 | async_answer_0(rid, rc);
|
---|
656 | return;
|
---|
657 | }
|
---|
658 | node->size = 0;
|
---|
659 | }
|
---|
660 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
661 | }
|
---|
662 |
|
---|
663 | /*
|
---|
664 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
---|
665 | * structure.
|
---|
666 | */
|
---|
667 | int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
|
---|
668 | if (fd < 0) {
|
---|
669 | vfs_node_put(node);
|
---|
670 | async_answer_0(rid, fd);
|
---|
671 | return;
|
---|
672 | }
|
---|
673 | vfs_file_t *file = vfs_file_get(fd);
|
---|
674 | file->node = node;
|
---|
675 | if (oflag & O_APPEND)
|
---|
676 | file->append = true;
|
---|
677 |
|
---|
678 | /*
|
---|
679 | * The following increase in reference count is for the fact that the
|
---|
680 | * file is being opened and that a file structure is pointing to it.
|
---|
681 | * It is necessary so that the file will not disappear when
|
---|
682 | * vfs_node_put() is called. The reference will be dropped by the
|
---|
683 | * respective VFS_IN_CLOSE.
|
---|
684 | */
|
---|
685 | vfs_node_addref(node);
|
---|
686 | vfs_node_put(node);
|
---|
687 | vfs_file_put(file);
|
---|
688 |
|
---|
689 | /* Success! Return the new file descriptor to the client. */
|
---|
690 | async_answer_1(rid, EOK, fd);
|
---|
691 | }
|
---|
692 |
|
---|
693 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
694 | {
|
---|
695 | int fd = IPC_GET_ARG1(*request);
|
---|
696 |
|
---|
697 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
698 | vfs_file_t *file = vfs_file_get(fd);
|
---|
699 | if (!file) {
|
---|
700 | async_answer_0(rid, ENOENT);
|
---|
701 | return;
|
---|
702 | }
|
---|
703 |
|
---|
704 | /*
|
---|
705 | * Lock the open file structure so that no other thread can manipulate
|
---|
706 | * the same open file at a time.
|
---|
707 | */
|
---|
708 | fibril_mutex_lock(&file->lock);
|
---|
709 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
710 |
|
---|
711 | /* Make a VFS_OUT_SYMC request at the destination FS server. */
|
---|
712 | aid_t msg;
|
---|
713 | ipc_call_t answer;
|
---|
714 | msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->devmap_handle,
|
---|
715 | file->node->index, &answer);
|
---|
716 |
|
---|
717 | vfs_exchange_release(fs_exch);
|
---|
718 |
|
---|
719 | /* Wait for reply from the FS server. */
|
---|
720 | sysarg_t rc;
|
---|
721 | async_wait_for(msg, &rc);
|
---|
722 |
|
---|
723 | fibril_mutex_unlock(&file->lock);
|
---|
724 |
|
---|
725 | vfs_file_put(file);
|
---|
726 | async_answer_0(rid, rc);
|
---|
727 | }
|
---|
728 |
|
---|
729 | void vfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
730 | {
|
---|
731 | int fd = IPC_GET_ARG1(*request);
|
---|
732 | int ret = vfs_fd_free(fd);
|
---|
733 | async_answer_0(rid, ret);
|
---|
734 | }
|
---|
735 |
|
---|
736 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
|
---|
737 | {
|
---|
738 | /*
|
---|
739 | * The following code strongly depends on the fact that the files data
|
---|
740 | * structure can be only accessed by a single fibril and all file
|
---|
741 | * operations are serialized (i.e. the reads and writes cannot
|
---|
742 | * interleave and a file cannot be closed while it is being read).
|
---|
743 | *
|
---|
744 | * Additional synchronization needs to be added once the table of
|
---|
745 | * open files supports parallel access!
|
---|
746 | */
|
---|
747 |
|
---|
748 | int fd = IPC_GET_ARG1(*request);
|
---|
749 |
|
---|
750 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
751 | vfs_file_t *file = vfs_file_get(fd);
|
---|
752 | if (!file) {
|
---|
753 | async_answer_0(rid, ENOENT);
|
---|
754 | return;
|
---|
755 | }
|
---|
756 |
|
---|
757 | /*
|
---|
758 | * Lock the open file structure so that no other thread can manipulate
|
---|
759 | * the same open file at a time.
|
---|
760 | */
|
---|
761 | fibril_mutex_lock(&file->lock);
|
---|
762 |
|
---|
763 | vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
|
---|
764 | assert(fs_info);
|
---|
765 |
|
---|
766 | /*
|
---|
767 | * Lock the file's node so that no other client can read/write to it at
|
---|
768 | * the same time unless the FS supports concurrent reads/writes and its
|
---|
769 | * write implementation does not modify the file size.
|
---|
770 | */
|
---|
771 | if ((read) ||
|
---|
772 | ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
|
---|
773 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
774 | else
|
---|
775 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
776 |
|
---|
777 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
778 | /*
|
---|
779 | * Make sure that no one is modifying the namespace
|
---|
780 | * while we are in readdir().
|
---|
781 | */
|
---|
782 | assert(read);
|
---|
783 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
784 | }
|
---|
785 |
|
---|
786 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
787 |
|
---|
788 | /*
|
---|
789 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
---|
790 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
791 | * destination FS server. The call will be routed as if sent by
|
---|
792 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
793 | * don't have to bother.
|
---|
794 | */
|
---|
795 | sysarg_t rc;
|
---|
796 | ipc_call_t answer;
|
---|
797 | if (read) {
|
---|
798 | rc = async_data_read_forward_4_1(fs_exch, VFS_OUT_READ,
|
---|
799 | file->node->devmap_handle, file->node->index,
|
---|
800 | LOWER32(file->pos), UPPER32(file->pos), &answer);
|
---|
801 | } else {
|
---|
802 | if (file->append)
|
---|
803 | file->pos = file->node->size;
|
---|
804 |
|
---|
805 | rc = async_data_write_forward_4_1(fs_exch, VFS_OUT_WRITE,
|
---|
806 | file->node->devmap_handle, file->node->index,
|
---|
807 | LOWER32(file->pos), UPPER32(file->pos), &answer);
|
---|
808 | }
|
---|
809 |
|
---|
810 | vfs_exchange_release(fs_exch);
|
---|
811 |
|
---|
812 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
813 |
|
---|
814 | if (file->node->type == VFS_NODE_DIRECTORY)
|
---|
815 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
816 |
|
---|
817 | /* Unlock the VFS node. */
|
---|
818 | if ((read) ||
|
---|
819 | ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
|
---|
820 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
821 | else {
|
---|
822 | /* Update the cached version of node's size. */
|
---|
823 | if (rc == EOK)
|
---|
824 | file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
825 | IPC_GET_ARG3(answer));
|
---|
826 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
827 | }
|
---|
828 |
|
---|
829 | /* Update the position pointer and unlock the open file. */
|
---|
830 | if (rc == EOK)
|
---|
831 | file->pos += bytes;
|
---|
832 | fibril_mutex_unlock(&file->lock);
|
---|
833 | vfs_file_put(file);
|
---|
834 |
|
---|
835 | /*
|
---|
836 | * FS server's reply is the final result of the whole operation we
|
---|
837 | * return to the client.
|
---|
838 | */
|
---|
839 | async_answer_1(rid, rc, bytes);
|
---|
840 | }
|
---|
841 |
|
---|
842 | void vfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
843 | {
|
---|
844 | vfs_rdwr(rid, request, true);
|
---|
845 | }
|
---|
846 |
|
---|
847 | void vfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
848 | {
|
---|
849 | vfs_rdwr(rid, request, false);
|
---|
850 | }
|
---|
851 |
|
---|
852 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
|
---|
853 | {
|
---|
854 | int fd = (int) IPC_GET_ARG1(*request);
|
---|
855 | off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
856 | IPC_GET_ARG3(*request));
|
---|
857 | int whence = (int) IPC_GET_ARG4(*request);
|
---|
858 |
|
---|
859 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
860 | vfs_file_t *file = vfs_file_get(fd);
|
---|
861 | if (!file) {
|
---|
862 | async_answer_0(rid, ENOENT);
|
---|
863 | return;
|
---|
864 | }
|
---|
865 |
|
---|
866 | fibril_mutex_lock(&file->lock);
|
---|
867 |
|
---|
868 | off64_t newoff;
|
---|
869 | switch (whence) {
|
---|
870 | case SEEK_SET:
|
---|
871 | if (off >= 0) {
|
---|
872 | file->pos = (aoff64_t) off;
|
---|
873 | fibril_mutex_unlock(&file->lock);
|
---|
874 | vfs_file_put(file);
|
---|
875 | async_answer_1(rid, EOK, off);
|
---|
876 | return;
|
---|
877 | }
|
---|
878 | break;
|
---|
879 | case SEEK_CUR:
|
---|
880 | if ((off >= 0) && (file->pos + off < file->pos)) {
|
---|
881 | fibril_mutex_unlock(&file->lock);
|
---|
882 | vfs_file_put(file);
|
---|
883 | async_answer_0(rid, EOVERFLOW);
|
---|
884 | return;
|
---|
885 | }
|
---|
886 |
|
---|
887 | if ((off < 0) && (file->pos < (aoff64_t) -off)) {
|
---|
888 | fibril_mutex_unlock(&file->lock);
|
---|
889 | vfs_file_put(file);
|
---|
890 | async_answer_0(rid, EOVERFLOW);
|
---|
891 | return;
|
---|
892 | }
|
---|
893 |
|
---|
894 | file->pos += off;
|
---|
895 | newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
|
---|
896 |
|
---|
897 | fibril_mutex_unlock(&file->lock);
|
---|
898 | vfs_file_put(file);
|
---|
899 | async_answer_2(rid, EOK, LOWER32(newoff),
|
---|
900 | UPPER32(newoff));
|
---|
901 | return;
|
---|
902 | case SEEK_END:
|
---|
903 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
904 | aoff64_t size = file->node->size;
|
---|
905 |
|
---|
906 | if ((off >= 0) && (size + off < size)) {
|
---|
907 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
908 | fibril_mutex_unlock(&file->lock);
|
---|
909 | vfs_file_put(file);
|
---|
910 | async_answer_0(rid, EOVERFLOW);
|
---|
911 | return;
|
---|
912 | }
|
---|
913 |
|
---|
914 | if ((off < 0) && (size < (aoff64_t) -off)) {
|
---|
915 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
916 | fibril_mutex_unlock(&file->lock);
|
---|
917 | vfs_file_put(file);
|
---|
918 | async_answer_0(rid, EOVERFLOW);
|
---|
919 | return;
|
---|
920 | }
|
---|
921 |
|
---|
922 | file->pos = size + off;
|
---|
923 | newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
|
---|
924 |
|
---|
925 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
926 | fibril_mutex_unlock(&file->lock);
|
---|
927 | vfs_file_put(file);
|
---|
928 | async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
|
---|
929 | return;
|
---|
930 | }
|
---|
931 |
|
---|
932 | fibril_mutex_unlock(&file->lock);
|
---|
933 | vfs_file_put(file);
|
---|
934 | async_answer_0(rid, EINVAL);
|
---|
935 | }
|
---|
936 |
|
---|
937 | int vfs_truncate_internal(fs_handle_t fs_handle, devmap_handle_t devmap_handle,
|
---|
938 | fs_index_t index, aoff64_t size)
|
---|
939 | {
|
---|
940 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
941 | sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
---|
942 | (sysarg_t) devmap_handle, (sysarg_t) index, LOWER32(size),
|
---|
943 | UPPER32(size));
|
---|
944 | vfs_exchange_release(exch);
|
---|
945 |
|
---|
946 | return (int) rc;
|
---|
947 | }
|
---|
948 |
|
---|
949 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
950 | {
|
---|
951 | int fd = IPC_GET_ARG1(*request);
|
---|
952 | aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
953 | IPC_GET_ARG3(*request));
|
---|
954 | int rc;
|
---|
955 |
|
---|
956 | vfs_file_t *file = vfs_file_get(fd);
|
---|
957 | if (!file) {
|
---|
958 | async_answer_0(rid, ENOENT);
|
---|
959 | return;
|
---|
960 | }
|
---|
961 | fibril_mutex_lock(&file->lock);
|
---|
962 |
|
---|
963 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
964 | rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
965 | file->node->devmap_handle, file->node->index, size);
|
---|
966 | if (rc == EOK)
|
---|
967 | file->node->size = size;
|
---|
968 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
969 |
|
---|
970 | fibril_mutex_unlock(&file->lock);
|
---|
971 | vfs_file_put(file);
|
---|
972 | async_answer_0(rid, (sysarg_t)rc);
|
---|
973 | }
|
---|
974 |
|
---|
975 | void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
|
---|
976 | {
|
---|
977 | int fd = IPC_GET_ARG1(*request);
|
---|
978 | sysarg_t rc;
|
---|
979 |
|
---|
980 | vfs_file_t *file = vfs_file_get(fd);
|
---|
981 | if (!file) {
|
---|
982 | async_answer_0(rid, ENOENT);
|
---|
983 | return;
|
---|
984 | }
|
---|
985 |
|
---|
986 | ipc_callid_t callid;
|
---|
987 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
988 | vfs_file_put(file);
|
---|
989 | async_answer_0(callid, EINVAL);
|
---|
990 | async_answer_0(rid, EINVAL);
|
---|
991 | return;
|
---|
992 | }
|
---|
993 |
|
---|
994 | fibril_mutex_lock(&file->lock);
|
---|
995 |
|
---|
996 | async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
997 |
|
---|
998 | aid_t msg;
|
---|
999 | msg = async_send_3(exch, VFS_OUT_STAT, file->node->devmap_handle,
|
---|
1000 | file->node->index, true, NULL);
|
---|
1001 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
1002 |
|
---|
1003 | vfs_exchange_release(exch);
|
---|
1004 |
|
---|
1005 | async_wait_for(msg, &rc);
|
---|
1006 |
|
---|
1007 | fibril_mutex_unlock(&file->lock);
|
---|
1008 | vfs_file_put(file);
|
---|
1009 | async_answer_0(rid, rc);
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
1013 | {
|
---|
1014 | char *path;
|
---|
1015 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
1016 | if (rc != EOK) {
|
---|
1017 | async_answer_0(rid, rc);
|
---|
1018 | return;
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | ipc_callid_t callid;
|
---|
1022 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
1023 | free(path);
|
---|
1024 | async_answer_0(callid, EINVAL);
|
---|
1025 | async_answer_0(rid, EINVAL);
|
---|
1026 | return;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | vfs_lookup_res_t lr;
|
---|
1030 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
1031 | rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
|
---|
1032 | free(path);
|
---|
1033 | if (rc != EOK) {
|
---|
1034 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1035 | async_answer_0(callid, rc);
|
---|
1036 | async_answer_0(rid, rc);
|
---|
1037 | return;
|
---|
1038 | }
|
---|
1039 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
1040 | if (!node) {
|
---|
1041 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1042 | async_answer_0(callid, ENOMEM);
|
---|
1043 | async_answer_0(rid, ENOMEM);
|
---|
1044 | return;
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
1048 |
|
---|
1049 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
1050 |
|
---|
1051 | aid_t msg;
|
---|
1052 | msg = async_send_3(exch, VFS_OUT_STAT, node->devmap_handle,
|
---|
1053 | node->index, false, NULL);
|
---|
1054 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
1055 |
|
---|
1056 | vfs_exchange_release(exch);
|
---|
1057 |
|
---|
1058 | sysarg_t rv;
|
---|
1059 | async_wait_for(msg, &rv);
|
---|
1060 |
|
---|
1061 | async_answer_0(rid, rv);
|
---|
1062 |
|
---|
1063 | vfs_node_put(node);
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
|
---|
1067 | {
|
---|
1068 | int mode = IPC_GET_ARG1(*request);
|
---|
1069 |
|
---|
1070 | char *path;
|
---|
1071 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
1072 | if (rc != EOK) {
|
---|
1073 | async_answer_0(rid, rc);
|
---|
1074 | return;
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | /* Ignore mode for now. */
|
---|
1078 | (void) mode;
|
---|
1079 |
|
---|
1080 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
1081 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
|
---|
1082 | rc = vfs_lookup_internal(path, lflag, NULL, NULL);
|
---|
1083 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1084 | free(path);
|
---|
1085 | async_answer_0(rid, rc);
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
|
---|
1089 | {
|
---|
1090 | int lflag = IPC_GET_ARG1(*request);
|
---|
1091 |
|
---|
1092 | char *path;
|
---|
1093 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
1094 | if (rc != EOK) {
|
---|
1095 | async_answer_0(rid, rc);
|
---|
1096 | return;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
1100 | lflag &= L_DIRECTORY; /* sanitize lflag */
|
---|
1101 | vfs_lookup_res_t lr;
|
---|
1102 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
|
---|
1103 | free(path);
|
---|
1104 | if (rc != EOK) {
|
---|
1105 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1106 | async_answer_0(rid, rc);
|
---|
1107 | return;
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | /*
|
---|
1111 | * The name has already been unlinked by vfs_lookup_internal().
|
---|
1112 | * We have to get and put the VFS node to ensure that it is
|
---|
1113 | * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
|
---|
1114 | */
|
---|
1115 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
1116 | fibril_mutex_lock(&nodes_mutex);
|
---|
1117 | node->lnkcnt--;
|
---|
1118 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1119 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1120 | vfs_node_put(node);
|
---|
1121 | async_answer_0(rid, EOK);
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
|
---|
1125 | {
|
---|
1126 | /* Retrieve the old path. */
|
---|
1127 | char *old;
|
---|
1128 | int rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
|
---|
1129 | if (rc != EOK) {
|
---|
1130 | async_answer_0(rid, rc);
|
---|
1131 | return;
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | /* Retrieve the new path. */
|
---|
1135 | char *new;
|
---|
1136 | rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
|
---|
1137 | if (rc != EOK) {
|
---|
1138 | free(old);
|
---|
1139 | async_answer_0(rid, rc);
|
---|
1140 | return;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | size_t olen;
|
---|
1144 | size_t nlen;
|
---|
1145 | char *oldc = canonify(old, &olen);
|
---|
1146 | char *newc = canonify(new, &nlen);
|
---|
1147 |
|
---|
1148 | if ((!oldc) || (!newc)) {
|
---|
1149 | async_answer_0(rid, EINVAL);
|
---|
1150 | free(old);
|
---|
1151 | free(new);
|
---|
1152 | return;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | oldc[olen] = '\0';
|
---|
1156 | newc[nlen] = '\0';
|
---|
1157 |
|
---|
1158 | if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
|
---|
1159 | ((newc[str_length(oldc)] == '/') ||
|
---|
1160 | (str_length(oldc) == 1) ||
|
---|
1161 | (str_length(oldc) == str_length(newc)))) {
|
---|
1162 | /*
|
---|
1163 | * oldc is a prefix of newc and either
|
---|
1164 | * - newc continues with a / where oldc ends, or
|
---|
1165 | * - oldc was / itself, or
|
---|
1166 | * - oldc and newc are equal.
|
---|
1167 | */
|
---|
1168 | async_answer_0(rid, EINVAL);
|
---|
1169 | free(old);
|
---|
1170 | free(new);
|
---|
1171 | return;
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | vfs_lookup_res_t old_lr;
|
---|
1175 | vfs_lookup_res_t new_lr;
|
---|
1176 | vfs_lookup_res_t new_par_lr;
|
---|
1177 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
1178 |
|
---|
1179 | /* Lookup the node belonging to the old file name. */
|
---|
1180 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
|
---|
1181 | if (rc != EOK) {
|
---|
1182 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1183 | async_answer_0(rid, rc);
|
---|
1184 | free(old);
|
---|
1185 | free(new);
|
---|
1186 | return;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | vfs_node_t *old_node = vfs_node_get(&old_lr);
|
---|
1190 | if (!old_node) {
|
---|
1191 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1192 | async_answer_0(rid, ENOMEM);
|
---|
1193 | free(old);
|
---|
1194 | free(new);
|
---|
1195 | return;
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | /* Determine the path to the parent of the node with the new name. */
|
---|
1199 | char *parentc = str_dup(newc);
|
---|
1200 | if (!parentc) {
|
---|
1201 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1202 | vfs_node_put(old_node);
|
---|
1203 | async_answer_0(rid, rc);
|
---|
1204 | free(old);
|
---|
1205 | free(new);
|
---|
1206 | return;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | char *lastsl = str_rchr(parentc + 1, '/');
|
---|
1210 | if (lastsl)
|
---|
1211 | *lastsl = '\0';
|
---|
1212 | else
|
---|
1213 | parentc[1] = '\0';
|
---|
1214 |
|
---|
1215 | /* Lookup parent of the new file name. */
|
---|
1216 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
|
---|
1217 | free(parentc); /* not needed anymore */
|
---|
1218 | if (rc != EOK) {
|
---|
1219 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1220 | vfs_node_put(old_node);
|
---|
1221 | async_answer_0(rid, rc);
|
---|
1222 | free(old);
|
---|
1223 | free(new);
|
---|
1224 | return;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | /* Check whether linking to the same file system instance. */
|
---|
1228 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
|
---|
1229 | (old_node->devmap_handle != new_par_lr.triplet.devmap_handle)) {
|
---|
1230 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1231 | vfs_node_put(old_node);
|
---|
1232 | async_answer_0(rid, EXDEV); /* different file systems */
|
---|
1233 | free(old);
|
---|
1234 | free(new);
|
---|
1235 | return;
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | /* Destroy the old link for the new name. */
|
---|
1239 | vfs_node_t *new_node = NULL;
|
---|
1240 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
|
---|
1241 |
|
---|
1242 | switch (rc) {
|
---|
1243 | case ENOENT:
|
---|
1244 | /* simply not in our way */
|
---|
1245 | break;
|
---|
1246 | case EOK:
|
---|
1247 | new_node = vfs_node_get(&new_lr);
|
---|
1248 | if (!new_node) {
|
---|
1249 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1250 | vfs_node_put(old_node);
|
---|
1251 | async_answer_0(rid, ENOMEM);
|
---|
1252 | free(old);
|
---|
1253 | free(new);
|
---|
1254 | return;
|
---|
1255 | }
|
---|
1256 | fibril_mutex_lock(&nodes_mutex);
|
---|
1257 | new_node->lnkcnt--;
|
---|
1258 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1259 | break;
|
---|
1260 | default:
|
---|
1261 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1262 | vfs_node_put(old_node);
|
---|
1263 | async_answer_0(rid, ENOTEMPTY);
|
---|
1264 | free(old);
|
---|
1265 | free(new);
|
---|
1266 | return;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | /* Create the new link for the new name. */
|
---|
1270 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
|
---|
1271 | if (rc != EOK) {
|
---|
1272 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1273 | vfs_node_put(old_node);
|
---|
1274 | if (new_node)
|
---|
1275 | vfs_node_put(new_node);
|
---|
1276 | async_answer_0(rid, rc);
|
---|
1277 | free(old);
|
---|
1278 | free(new);
|
---|
1279 | return;
|
---|
1280 | }
|
---|
1281 |
|
---|
1282 | fibril_mutex_lock(&nodes_mutex);
|
---|
1283 | old_node->lnkcnt++;
|
---|
1284 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1285 |
|
---|
1286 | /* Destroy the link for the old name. */
|
---|
1287 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
|
---|
1288 | if (rc != EOK) {
|
---|
1289 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1290 | vfs_node_put(old_node);
|
---|
1291 | if (new_node)
|
---|
1292 | vfs_node_put(new_node);
|
---|
1293 | async_answer_0(rid, rc);
|
---|
1294 | free(old);
|
---|
1295 | free(new);
|
---|
1296 | return;
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | fibril_mutex_lock(&nodes_mutex);
|
---|
1300 | old_node->lnkcnt--;
|
---|
1301 | fibril_mutex_unlock(&nodes_mutex);
|
---|
1302 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
1303 | vfs_node_put(old_node);
|
---|
1304 |
|
---|
1305 | if (new_node)
|
---|
1306 | vfs_node_put(new_node);
|
---|
1307 |
|
---|
1308 | free(old);
|
---|
1309 | free(new);
|
---|
1310 | async_answer_0(rid, EOK);
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
|
---|
1314 | {
|
---|
1315 | int oldfd = IPC_GET_ARG1(*request);
|
---|
1316 | int newfd = IPC_GET_ARG2(*request);
|
---|
1317 |
|
---|
1318 | /* If the file descriptors are the same, do nothing. */
|
---|
1319 | if (oldfd == newfd) {
|
---|
1320 | async_answer_1(rid, EOK, newfd);
|
---|
1321 | return;
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | /* Lookup the file structure corresponding to oldfd. */
|
---|
1325 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
1326 | if (!oldfile) {
|
---|
1327 | async_answer_0(rid, EBADF);
|
---|
1328 | return;
|
---|
1329 | }
|
---|
1330 |
|
---|
1331 | /*
|
---|
1332 | * Lock the open file structure so that no other thread can manipulate
|
---|
1333 | * the same open file at a time.
|
---|
1334 | */
|
---|
1335 | fibril_mutex_lock(&oldfile->lock);
|
---|
1336 |
|
---|
1337 | /* Make sure newfd is closed. */
|
---|
1338 | (void) vfs_fd_free(newfd);
|
---|
1339 |
|
---|
1340 | /* Assign the old file to newfd. */
|
---|
1341 | int ret = vfs_fd_assign(oldfile, newfd);
|
---|
1342 | fibril_mutex_unlock(&oldfile->lock);
|
---|
1343 | vfs_file_put(oldfile);
|
---|
1344 |
|
---|
1345 | if (ret != EOK)
|
---|
1346 | async_answer_0(rid, ret);
|
---|
1347 | else
|
---|
1348 | async_answer_1(rid, EOK, newfd);
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | /**
|
---|
1352 | * @}
|
---|
1353 | */
|
---|