source: mainline/uspace/srv/vfs/vfs_ops.c@ ae75e2e3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ae75e2e3 was ae75e2e3, checked in by Jakub Jermar <jakub@…>, 15 years ago

Finish implementation of VFS_IN_UNMOUNT in vfs.

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