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

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

Rename L_NOCROSS_LAST_MP to L_MP and introduce L_ROOT.
Lookup the mounted root in vfs_unmount() using L_ROOT.

  • Property mode set to 100644
File size: 36.9 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_ROOT, &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 rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
524 free(mp);
525 if (rc != EOK) {
526 fibril_rwlock_write_unlock(&namespace_rwlock);
527 vfs_node_put(mr_node);
528 ipc_answer_0(rid, rc);
529 return;
530 }
531 vfs_node_t *mp_node = vfs_node_get(&mp_res);
532 if (!mp_node) {
533 fibril_rwlock_write_unlock(&namespace_rwlock);
534 vfs_node_put(mr_node);
535 ipc_answer_0(rid, ENOMEM);
536 return;
537 }
538
539 phone = vfs_grab_phone(mp_node->fs_handle);
540 rc = async_req_2_0(phone, VFS_OUT_UNMOUNT, mp_node->fs_handle,
541 mp_node->dev_handle);
542 vfs_release_phone(phone);
543 if (rc != EOK) {
544 fibril_rwlock_write_unlock(&namespace_rwlock);
545 vfs_node_put(mp_node);
546 vfs_node_put(mr_node);
547 ipc_answer_0(rid, rc);
548 return;
549 }
550
551 /* Drop the reference we got above. */
552 vfs_node_put(mp_node);
553 /* Drop the reference from when the file system was mounted. */
554 vfs_node_put(mp_node);
555 }
556
557
558 /*
559 * All went well, the mounted file system was successfully unmounted.
560 * The only thing left is to forget the unmounted root VFS node.
561 */
562 vfs_node_forget(mr_node);
563
564 fibril_rwlock_write_unlock(&namespace_rwlock);
565 ipc_answer_0(rid, EOK);
566}
567
568void vfs_open(ipc_callid_t rid, ipc_call_t *request)
569{
570 if (!vfs_files_init()) {
571 ipc_answer_0(rid, ENOMEM);
572 return;
573 }
574
575 /*
576 * The POSIX interface is open(path, oflag, mode).
577 * We can receive oflags and mode along with the VFS_IN_OPEN call;
578 * the path will need to arrive in another call.
579 *
580 * We also receive one private, non-POSIX set of flags called lflag
581 * used to pass information to vfs_lookup_internal().
582 */
583 int lflag = IPC_GET_ARG1(*request);
584 int oflag = IPC_GET_ARG2(*request);
585 int mode = IPC_GET_ARG3(*request);
586 size_t len;
587
588 /* Ignore mode for now. */
589 (void) mode;
590
591 /*
592 * Make sure that we are called with exactly one of L_FILE and
593 * L_DIRECTORY. Make sure that the user does not pass L_OPEN,
594 * L_ROOT or L_MP.
595 */
596 if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
597 ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
598 (lflag & (L_OPEN | L_ROOT | L_MP))) {
599 ipc_answer_0(rid, EINVAL);
600 return;
601 }
602
603 if (oflag & O_CREAT)
604 lflag |= L_CREATE;
605 if (oflag & O_EXCL)
606 lflag |= L_EXCLUSIVE;
607
608 ipc_callid_t callid;
609 if (!async_data_write_receive(&callid, &len)) {
610 ipc_answer_0(callid, EINVAL);
611 ipc_answer_0(rid, EINVAL);
612 return;
613 }
614
615 char *path = malloc(len + 1);
616 if (!path) {
617 ipc_answer_0(callid, ENOMEM);
618 ipc_answer_0(rid, ENOMEM);
619 return;
620 }
621
622 int rc;
623 if ((rc = async_data_write_finalize(callid, path, len))) {
624 ipc_answer_0(rid, rc);
625 free(path);
626 return;
627 }
628 path[len] = '\0';
629
630 /*
631 * Avoid the race condition in which the file can be deleted before we
632 * find/create-and-lock the VFS node corresponding to the looked-up
633 * triplet.
634 */
635 if (lflag & L_CREATE)
636 fibril_rwlock_write_lock(&namespace_rwlock);
637 else
638 fibril_rwlock_read_lock(&namespace_rwlock);
639
640 /* The path is now populated and we can call vfs_lookup_internal(). */
641 vfs_lookup_res_t lr;
642 rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
643 if (rc != EOK) {
644 if (lflag & L_CREATE)
645 fibril_rwlock_write_unlock(&namespace_rwlock);
646 else
647 fibril_rwlock_read_unlock(&namespace_rwlock);
648 ipc_answer_0(rid, rc);
649 free(path);
650 return;
651 }
652
653 /* Path is no longer needed. */
654 free(path);
655
656 vfs_node_t *node = vfs_node_get(&lr);
657 if (lflag & L_CREATE)
658 fibril_rwlock_write_unlock(&namespace_rwlock);
659 else
660 fibril_rwlock_read_unlock(&namespace_rwlock);
661
662 /* Truncate the file if requested and if necessary. */
663 if (oflag & O_TRUNC) {
664 fibril_rwlock_write_lock(&node->contents_rwlock);
665 if (node->size) {
666 rc = vfs_truncate_internal(node->fs_handle,
667 node->dev_handle, node->index, 0);
668 if (rc) {
669 fibril_rwlock_write_unlock(&node->contents_rwlock);
670 vfs_node_put(node);
671 ipc_answer_0(rid, rc);
672 return;
673 }
674 node->size = 0;
675 }
676 fibril_rwlock_write_unlock(&node->contents_rwlock);
677 }
678
679 /*
680 * Get ourselves a file descriptor and the corresponding vfs_file_t
681 * structure.
682 */
683 int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
684 if (fd < 0) {
685 vfs_node_put(node);
686 ipc_answer_0(rid, fd);
687 return;
688 }
689 vfs_file_t *file = vfs_file_get(fd);
690 file->node = node;
691 if (oflag & O_APPEND)
692 file->append = true;
693
694 /*
695 * The following increase in reference count is for the fact that the
696 * file is being opened and that a file structure is pointing to it.
697 * It is necessary so that the file will not disappear when
698 * vfs_node_put() is called. The reference will be dropped by the
699 * respective VFS_IN_CLOSE.
700 */
701 vfs_node_addref(node);
702 vfs_node_put(node);
703
704 /* Success! Return the new file descriptor to the client. */
705 ipc_answer_1(rid, EOK, fd);
706}
707
708void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
709{
710 // FIXME: check for sanity of the supplied fs, dev and index
711
712 if (!vfs_files_init()) {
713 ipc_answer_0(rid, ENOMEM);
714 return;
715 }
716
717 /*
718 * The interface is open_node(fs, dev, index, oflag).
719 */
720 vfs_lookup_res_t lr;
721
722 lr.triplet.fs_handle = IPC_GET_ARG1(*request);
723 lr.triplet.dev_handle = IPC_GET_ARG2(*request);
724 lr.triplet.index = IPC_GET_ARG3(*request);
725 int oflag = IPC_GET_ARG4(*request);
726
727 fibril_rwlock_read_lock(&namespace_rwlock);
728
729 int rc = vfs_open_node_internal(&lr);
730 if (rc != EOK) {
731 fibril_rwlock_read_unlock(&namespace_rwlock);
732 ipc_answer_0(rid, rc);
733 return;
734 }
735
736 vfs_node_t *node = vfs_node_get(&lr);
737 fibril_rwlock_read_unlock(&namespace_rwlock);
738
739 /* Truncate the file if requested and if necessary. */
740 if (oflag & O_TRUNC) {
741 fibril_rwlock_write_lock(&node->contents_rwlock);
742 if (node->size) {
743 rc = vfs_truncate_internal(node->fs_handle,
744 node->dev_handle, node->index, 0);
745 if (rc) {
746 fibril_rwlock_write_unlock(&node->contents_rwlock);
747 vfs_node_put(node);
748 ipc_answer_0(rid, rc);
749 return;
750 }
751 node->size = 0;
752 }
753 fibril_rwlock_write_unlock(&node->contents_rwlock);
754 }
755
756 /*
757 * Get ourselves a file descriptor and the corresponding vfs_file_t
758 * structure.
759 */
760 int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
761 if (fd < 0) {
762 vfs_node_put(node);
763 ipc_answer_0(rid, fd);
764 return;
765 }
766 vfs_file_t *file = vfs_file_get(fd);
767 file->node = node;
768 if (oflag & O_APPEND)
769 file->append = true;
770
771 /*
772 * The following increase in reference count is for the fact that the
773 * file is being opened and that a file structure is pointing to it.
774 * It is necessary so that the file will not disappear when
775 * vfs_node_put() is called. The reference will be dropped by the
776 * respective VFS_IN_CLOSE.
777 */
778 vfs_node_addref(node);
779 vfs_node_put(node);
780
781 /* Success! Return the new file descriptor to the client. */
782 ipc_answer_1(rid, EOK, fd);
783}
784
785void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
786{
787 int fd = IPC_GET_ARG1(*request);
788
789 /* Lookup the file structure corresponding to the file descriptor. */
790 vfs_file_t *file = vfs_file_get(fd);
791 if (!file) {
792 ipc_answer_0(rid, ENOENT);
793 return;
794 }
795
796 /*
797 * Lock the open file structure so that no other thread can manipulate
798 * the same open file at a time.
799 */
800 fibril_mutex_lock(&file->lock);
801 int fs_phone = vfs_grab_phone(file->node->fs_handle);
802
803 /* Make a VFS_OUT_SYMC request at the destination FS server. */
804 aid_t msg;
805 ipc_call_t answer;
806 msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->dev_handle,
807 file->node->index, &answer);
808
809 /* Wait for reply from the FS server. */
810 ipcarg_t rc;
811 async_wait_for(msg, &rc);
812
813 vfs_release_phone(fs_phone);
814 fibril_mutex_unlock(&file->lock);
815
816 ipc_answer_0(rid, rc);
817}
818
819static int vfs_close_internal(vfs_file_t *file)
820{
821 /*
822 * Lock the open file structure so that no other thread can manipulate
823 * the same open file at a time.
824 */
825 fibril_mutex_lock(&file->lock);
826
827 if (file->refcnt <= 1) {
828 /* Only close the file on the destination FS server
829 if there are no more file descriptors (except the
830 present one) pointing to this file. */
831
832 int fs_phone = vfs_grab_phone(file->node->fs_handle);
833
834 /* Make a VFS_OUT_CLOSE request at the destination FS server. */
835 aid_t msg;
836 ipc_call_t answer;
837 msg = async_send_2(fs_phone, VFS_OUT_CLOSE, file->node->dev_handle,
838 file->node->index, &answer);
839
840 /* Wait for reply from the FS server. */
841 ipcarg_t rc;
842 async_wait_for(msg, &rc);
843
844 vfs_release_phone(fs_phone);
845 fibril_mutex_unlock(&file->lock);
846
847 return IPC_GET_ARG1(answer);
848 }
849
850 fibril_mutex_unlock(&file->lock);
851 return EOK;
852}
853
854void vfs_close(ipc_callid_t rid, ipc_call_t *request)
855{
856 int fd = IPC_GET_ARG1(*request);
857
858 /* Lookup the file structure corresponding to the file descriptor. */
859 vfs_file_t *file = vfs_file_get(fd);
860 if (!file) {
861 ipc_answer_0(rid, ENOENT);
862 return;
863 }
864
865 int ret = vfs_close_internal(file);
866 if (ret != EOK)
867 ipc_answer_0(rid, ret);
868
869 ret = vfs_fd_free(fd);
870 ipc_answer_0(rid, ret);
871}
872
873static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
874{
875
876 /*
877 * The following code strongly depends on the fact that the files data
878 * structure can be only accessed by a single fibril and all file
879 * operations are serialized (i.e. the reads and writes cannot
880 * interleave and a file cannot be closed while it is being read).
881 *
882 * Additional synchronization needs to be added once the table of
883 * open files supports parallel access!
884 */
885
886 int fd = IPC_GET_ARG1(*request);
887
888 /* Lookup the file structure corresponding to the file descriptor. */
889 vfs_file_t *file = vfs_file_get(fd);
890 if (!file) {
891 ipc_answer_0(rid, ENOENT);
892 return;
893 }
894
895 /*
896 * Now we need to receive a call with client's
897 * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
898 */
899 ipc_callid_t callid;
900 int res;
901 if (read)
902 res = async_data_read_receive(&callid, NULL);
903 else
904 res = async_data_write_receive(&callid, NULL);
905 if (!res) {
906 ipc_answer_0(callid, EINVAL);
907 ipc_answer_0(rid, EINVAL);
908 return;
909 }
910
911 /*
912 * Lock the open file structure so that no other thread can manipulate
913 * the same open file at a time.
914 */
915 fibril_mutex_lock(&file->lock);
916
917 /*
918 * Lock the file's node so that no other client can read/write to it at
919 * the same time.
920 */
921 if (read)
922 fibril_rwlock_read_lock(&file->node->contents_rwlock);
923 else
924 fibril_rwlock_write_lock(&file->node->contents_rwlock);
925
926 if (file->node->type == VFS_NODE_DIRECTORY) {
927 /*
928 * Make sure that no one is modifying the namespace
929 * while we are in readdir().
930 */
931 assert(read);
932 fibril_rwlock_read_lock(&namespace_rwlock);
933 }
934
935 int fs_phone = vfs_grab_phone(file->node->fs_handle);
936
937 /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
938 aid_t msg;
939 ipc_call_t answer;
940 if (!read && file->append)
941 file->pos = file->node->size;
942 msg = async_send_3(fs_phone, read ? VFS_OUT_READ : VFS_OUT_WRITE,
943 file->node->dev_handle, file->node->index, file->pos, &answer);
944
945 /*
946 * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
947 * destination FS server. The call will be routed as if sent by
948 * ourselves. Note that call arguments are immutable in this case so we
949 * don't have to bother.
950 */
951 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
952
953 /* Wait for reply from the FS server. */
954 ipcarg_t rc;
955 async_wait_for(msg, &rc);
956
957 vfs_release_phone(fs_phone);
958
959 size_t bytes = IPC_GET_ARG1(answer);
960
961 if (file->node->type == VFS_NODE_DIRECTORY)
962 fibril_rwlock_read_unlock(&namespace_rwlock);
963
964 /* Unlock the VFS node. */
965 if (read)
966 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
967 else {
968 /* Update the cached version of node's size. */
969 if (rc == EOK)
970 file->node->size = IPC_GET_ARG2(answer);
971 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
972 }
973
974 /* Update the position pointer and unlock the open file. */
975 if (rc == EOK)
976 file->pos += bytes;
977 fibril_mutex_unlock(&file->lock);
978
979 /*
980 * FS server's reply is the final result of the whole operation we
981 * return to the client.
982 */
983 ipc_answer_1(rid, rc, bytes);
984}
985
986void vfs_read(ipc_callid_t rid, ipc_call_t *request)
987{
988 vfs_rdwr(rid, request, true);
989}
990
991void vfs_write(ipc_callid_t rid, ipc_call_t *request)
992{
993 vfs_rdwr(rid, request, false);
994}
995
996void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
997{
998 int fd = (int) IPC_GET_ARG1(*request);
999 off_t off = (off_t) IPC_GET_ARG2(*request);
1000 int whence = (int) IPC_GET_ARG3(*request);
1001
1002
1003 /* Lookup the file structure corresponding to the file descriptor. */
1004 vfs_file_t *file = vfs_file_get(fd);
1005 if (!file) {
1006 ipc_answer_0(rid, ENOENT);
1007 return;
1008 }
1009
1010 off_t newpos;
1011 fibril_mutex_lock(&file->lock);
1012 if (whence == SEEK_SET) {
1013 file->pos = off;
1014 fibril_mutex_unlock(&file->lock);
1015 ipc_answer_1(rid, EOK, off);
1016 return;
1017 }
1018 if (whence == SEEK_CUR) {
1019 if (file->pos + off < file->pos) {
1020 fibril_mutex_unlock(&file->lock);
1021 ipc_answer_0(rid, EOVERFLOW);
1022 return;
1023 }
1024 file->pos += off;
1025 newpos = file->pos;
1026 fibril_mutex_unlock(&file->lock);
1027 ipc_answer_1(rid, EOK, newpos);
1028 return;
1029 }
1030 if (whence == SEEK_END) {
1031 fibril_rwlock_read_lock(&file->node->contents_rwlock);
1032 size_t size = file->node->size;
1033 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
1034 if (size + off < size) {
1035 fibril_mutex_unlock(&file->lock);
1036 ipc_answer_0(rid, EOVERFLOW);
1037 return;
1038 }
1039 newpos = size + off;
1040 file->pos = newpos;
1041 fibril_mutex_unlock(&file->lock);
1042 ipc_answer_1(rid, EOK, newpos);
1043 return;
1044 }
1045 fibril_mutex_unlock(&file->lock);
1046 ipc_answer_0(rid, EINVAL);
1047}
1048
1049int
1050vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
1051 fs_index_t index, size_t size)
1052{
1053 ipcarg_t rc;
1054 int fs_phone;
1055
1056 fs_phone = vfs_grab_phone(fs_handle);
1057 rc = async_req_3_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t)dev_handle,
1058 (ipcarg_t)index, (ipcarg_t)size);
1059 vfs_release_phone(fs_phone);
1060 return (int)rc;
1061}
1062
1063void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
1064{
1065 int fd = IPC_GET_ARG1(*request);
1066 size_t size = IPC_GET_ARG2(*request);
1067 int rc;
1068
1069 vfs_file_t *file = vfs_file_get(fd);
1070 if (!file) {
1071 ipc_answer_0(rid, ENOENT);
1072 return;
1073 }
1074 fibril_mutex_lock(&file->lock);
1075
1076 fibril_rwlock_write_lock(&file->node->contents_rwlock);
1077 rc = vfs_truncate_internal(file->node->fs_handle,
1078 file->node->dev_handle, file->node->index, size);
1079 if (rc == EOK)
1080 file->node->size = size;
1081 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
1082
1083 fibril_mutex_unlock(&file->lock);
1084 ipc_answer_0(rid, (ipcarg_t)rc);
1085}
1086
1087void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
1088{
1089 int fd = IPC_GET_ARG1(*request);
1090 ipcarg_t rc;
1091
1092 vfs_file_t *file = vfs_file_get(fd);
1093 if (!file) {
1094 ipc_answer_0(rid, ENOENT);
1095 return;
1096 }
1097
1098 ipc_callid_t callid;
1099 if (!async_data_read_receive(&callid, NULL)) {
1100 ipc_answer_0(callid, EINVAL);
1101 ipc_answer_0(rid, EINVAL);
1102 return;
1103 }
1104
1105 fibril_mutex_lock(&file->lock);
1106
1107 int fs_phone = vfs_grab_phone(file->node->fs_handle);
1108
1109 aid_t msg;
1110 msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->dev_handle,
1111 file->node->index, true, NULL);
1112 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1113 async_wait_for(msg, &rc);
1114 vfs_release_phone(fs_phone);
1115
1116 fibril_mutex_unlock(&file->lock);
1117 ipc_answer_0(rid, rc);
1118}
1119
1120void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
1121{
1122 size_t len;
1123 ipc_callid_t callid;
1124
1125 if (!async_data_write_receive(&callid, &len)) {
1126 ipc_answer_0(callid, EINVAL);
1127 ipc_answer_0(rid, EINVAL);
1128 return;
1129 }
1130 char *path = malloc(len + 1);
1131 if (!path) {
1132 ipc_answer_0(callid, ENOMEM);
1133 ipc_answer_0(rid, ENOMEM);
1134 return;
1135 }
1136 int rc;
1137 if ((rc = async_data_write_finalize(callid, path, len))) {
1138 ipc_answer_0(rid, rc);
1139 free(path);
1140 return;
1141 }
1142 path[len] = '\0';
1143
1144 if (!async_data_read_receive(&callid, NULL)) {
1145 free(path);
1146 ipc_answer_0(callid, EINVAL);
1147 ipc_answer_0(rid, EINVAL);
1148 return;
1149 }
1150
1151 vfs_lookup_res_t lr;
1152 fibril_rwlock_read_lock(&namespace_rwlock);
1153 rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
1154 free(path);
1155 if (rc != EOK) {
1156 fibril_rwlock_read_unlock(&namespace_rwlock);
1157 ipc_answer_0(callid, rc);
1158 ipc_answer_0(rid, rc);
1159 return;
1160 }
1161 vfs_node_t *node = vfs_node_get(&lr);
1162 if (!node) {
1163 fibril_rwlock_read_unlock(&namespace_rwlock);
1164 ipc_answer_0(callid, ENOMEM);
1165 ipc_answer_0(rid, ENOMEM);
1166 return;
1167 }
1168
1169 fibril_rwlock_read_unlock(&namespace_rwlock);
1170
1171 int fs_phone = vfs_grab_phone(node->fs_handle);
1172 aid_t msg;
1173 msg = async_send_3(fs_phone, VFS_OUT_STAT, node->dev_handle,
1174 node->index, false, NULL);
1175 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1176
1177 ipcarg_t rv;
1178 async_wait_for(msg, &rv);
1179 vfs_release_phone(fs_phone);
1180
1181 ipc_answer_0(rid, rv);
1182
1183 vfs_node_put(node);
1184}
1185
1186void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
1187{
1188 int mode = IPC_GET_ARG1(*request);
1189
1190 size_t len;
1191 ipc_callid_t callid;
1192
1193 if (!async_data_write_receive(&callid, &len)) {
1194 ipc_answer_0(callid, EINVAL);
1195 ipc_answer_0(rid, EINVAL);
1196 return;
1197 }
1198 char *path = malloc(len + 1);
1199 if (!path) {
1200 ipc_answer_0(callid, ENOMEM);
1201 ipc_answer_0(rid, ENOMEM);
1202 return;
1203 }
1204 int rc;
1205 if ((rc = async_data_write_finalize(callid, path, len))) {
1206 ipc_answer_0(rid, rc);
1207 free(path);
1208 return;
1209 }
1210 path[len] = '\0';
1211
1212 /* Ignore mode for now. */
1213 (void) mode;
1214
1215 fibril_rwlock_write_lock(&namespace_rwlock);
1216 int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
1217 rc = vfs_lookup_internal(path, lflag, NULL, NULL);
1218 fibril_rwlock_write_unlock(&namespace_rwlock);
1219 free(path);
1220 ipc_answer_0(rid, rc);
1221}
1222
1223void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1224{
1225 int lflag = IPC_GET_ARG1(*request);
1226
1227 size_t len;
1228 ipc_callid_t callid;
1229
1230 if (!async_data_write_receive(&callid, &len)) {
1231 ipc_answer_0(callid, EINVAL);
1232 ipc_answer_0(rid, EINVAL);
1233 return;
1234 }
1235 char *path = malloc(len + 1);
1236 if (!path) {
1237 ipc_answer_0(callid, ENOMEM);
1238 ipc_answer_0(rid, ENOMEM);
1239 return;
1240 }
1241 int rc;
1242 if ((rc = async_data_write_finalize(callid, path, len))) {
1243 ipc_answer_0(rid, rc);
1244 free(path);
1245 return;
1246 }
1247 path[len] = '\0';
1248
1249 fibril_rwlock_write_lock(&namespace_rwlock);
1250 lflag &= L_DIRECTORY; /* sanitize lflag */
1251 vfs_lookup_res_t lr;
1252 rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
1253 free(path);
1254 if (rc != EOK) {
1255 fibril_rwlock_write_unlock(&namespace_rwlock);
1256 ipc_answer_0(rid, rc);
1257 return;
1258 }
1259
1260 /*
1261 * The name has already been unlinked by vfs_lookup_internal().
1262 * We have to get and put the VFS node to ensure that it is
1263 * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
1264 */
1265 vfs_node_t *node = vfs_node_get(&lr);
1266 fibril_mutex_lock(&nodes_mutex);
1267 node->lnkcnt--;
1268 fibril_mutex_unlock(&nodes_mutex);
1269 fibril_rwlock_write_unlock(&namespace_rwlock);
1270 vfs_node_put(node);
1271 ipc_answer_0(rid, EOK);
1272}
1273
1274void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1275{
1276 size_t olen, nlen;
1277 ipc_callid_t callid;
1278 int rc;
1279
1280 /* Retrieve the old path. */
1281 if (!async_data_write_receive(&callid, &olen)) {
1282 ipc_answer_0(callid, EINVAL);
1283 ipc_answer_0(rid, EINVAL);
1284 return;
1285 }
1286 char *old = malloc(olen + 1);
1287 if (!old) {
1288 ipc_answer_0(callid, ENOMEM);
1289 ipc_answer_0(rid, ENOMEM);
1290 return;
1291 }
1292 if ((rc = async_data_write_finalize(callid, old, olen))) {
1293 ipc_answer_0(rid, rc);
1294 free(old);
1295 return;
1296 }
1297 old[olen] = '\0';
1298
1299 /* Retrieve the new path. */
1300 if (!async_data_write_receive(&callid, &nlen)) {
1301 ipc_answer_0(callid, EINVAL);
1302 ipc_answer_0(rid, EINVAL);
1303 free(old);
1304 return;
1305 }
1306 char *new = malloc(nlen + 1);
1307 if (!new) {
1308 ipc_answer_0(callid, ENOMEM);
1309 ipc_answer_0(rid, ENOMEM);
1310 free(old);
1311 return;
1312 }
1313 if ((rc = async_data_write_finalize(callid, new, nlen))) {
1314 ipc_answer_0(rid, rc);
1315 free(old);
1316 free(new);
1317 return;
1318 }
1319 new[nlen] = '\0';
1320
1321 char *oldc = canonify(old, &olen);
1322 char *newc = canonify(new, &nlen);
1323 if (!oldc || !newc) {
1324 ipc_answer_0(rid, EINVAL);
1325 free(old);
1326 free(new);
1327 return;
1328 }
1329 oldc[olen] = '\0';
1330 newc[nlen] = '\0';
1331 if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
1332 ((newc[str_length(oldc)] == '/') ||
1333 (str_length(oldc) == 1) ||
1334 (str_length(oldc) == str_length(newc)))) {
1335 /*
1336 * oldc is a prefix of newc and either
1337 * - newc continues with a / where oldc ends, or
1338 * - oldc was / itself, or
1339 * - oldc and newc are equal.
1340 */
1341 ipc_answer_0(rid, EINVAL);
1342 free(old);
1343 free(new);
1344 return;
1345 }
1346
1347 vfs_lookup_res_t old_lr;
1348 vfs_lookup_res_t new_lr;
1349 vfs_lookup_res_t new_par_lr;
1350 fibril_rwlock_write_lock(&namespace_rwlock);
1351 /* Lookup the node belonging to the old file name. */
1352 rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1353 if (rc != EOK) {
1354 fibril_rwlock_write_unlock(&namespace_rwlock);
1355 ipc_answer_0(rid, rc);
1356 free(old);
1357 free(new);
1358 return;
1359 }
1360 vfs_node_t *old_node = vfs_node_get(&old_lr);
1361 if (!old_node) {
1362 fibril_rwlock_write_unlock(&namespace_rwlock);
1363 ipc_answer_0(rid, ENOMEM);
1364 free(old);
1365 free(new);
1366 return;
1367 }
1368 /* Determine the path to the parent of the node with the new name. */
1369 char *parentc = str_dup(newc);
1370 if (!parentc) {
1371 fibril_rwlock_write_unlock(&namespace_rwlock);
1372 ipc_answer_0(rid, rc);
1373 free(old);
1374 free(new);
1375 return;
1376 }
1377 char *lastsl = str_rchr(parentc + 1, '/');
1378 if (lastsl)
1379 *lastsl = '\0';
1380 else
1381 parentc[1] = '\0';
1382 /* Lookup parent of the new file name. */
1383 rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
1384 free(parentc); /* not needed anymore */
1385 if (rc != EOK) {
1386 fibril_rwlock_write_unlock(&namespace_rwlock);
1387 ipc_answer_0(rid, rc);
1388 free(old);
1389 free(new);
1390 return;
1391 }
1392 /* Check whether linking to the same file system instance. */
1393 if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1394 (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
1395 fibril_rwlock_write_unlock(&namespace_rwlock);
1396 ipc_answer_0(rid, EXDEV); /* different file systems */
1397 free(old);
1398 free(new);
1399 return;
1400 }
1401 /* Destroy the old link for the new name. */
1402 vfs_node_t *new_node = NULL;
1403 rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1404 switch (rc) {
1405 case ENOENT:
1406 /* simply not in our way */
1407 break;
1408 case EOK:
1409 new_node = vfs_node_get(&new_lr);
1410 if (!new_node) {
1411 fibril_rwlock_write_unlock(&namespace_rwlock);
1412 ipc_answer_0(rid, ENOMEM);
1413 free(old);
1414 free(new);
1415 return;
1416 }
1417 fibril_mutex_lock(&nodes_mutex);
1418 new_node->lnkcnt--;
1419 fibril_mutex_unlock(&nodes_mutex);
1420 break;
1421 default:
1422 fibril_rwlock_write_unlock(&namespace_rwlock);
1423 ipc_answer_0(rid, ENOTEMPTY);
1424 free(old);
1425 free(new);
1426 return;
1427 }
1428 /* Create the new link for the new name. */
1429 rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1430 if (rc != EOK) {
1431 fibril_rwlock_write_unlock(&namespace_rwlock);
1432 if (new_node)
1433 vfs_node_put(new_node);
1434 ipc_answer_0(rid, rc);
1435 free(old);
1436 free(new);
1437 return;
1438 }
1439 fibril_mutex_lock(&nodes_mutex);
1440 old_node->lnkcnt++;
1441 fibril_mutex_unlock(&nodes_mutex);
1442 /* Destroy the link for the old name. */
1443 rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1444 if (rc != EOK) {
1445 fibril_rwlock_write_unlock(&namespace_rwlock);
1446 vfs_node_put(old_node);
1447 if (new_node)
1448 vfs_node_put(new_node);
1449 ipc_answer_0(rid, rc);
1450 free(old);
1451 free(new);
1452 return;
1453 }
1454 fibril_mutex_lock(&nodes_mutex);
1455 old_node->lnkcnt--;
1456 fibril_mutex_unlock(&nodes_mutex);
1457 fibril_rwlock_write_unlock(&namespace_rwlock);
1458 vfs_node_put(old_node);
1459 if (new_node)
1460 vfs_node_put(new_node);
1461 free(old);
1462 free(new);
1463 ipc_answer_0(rid, EOK);
1464}
1465
1466void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
1467{
1468 int oldfd = IPC_GET_ARG1(*request);
1469 int newfd = IPC_GET_ARG2(*request);
1470
1471 /* Lookup the file structure corresponding to oldfd. */
1472 vfs_file_t *oldfile = vfs_file_get(oldfd);
1473 if (!oldfile) {
1474 ipc_answer_0(rid, EBADF);
1475 return;
1476 }
1477
1478 /* If the file descriptors are the same, do nothing. */
1479 if (oldfd == newfd) {
1480 ipc_answer_1(rid, EOK, newfd);
1481 return;
1482 }
1483
1484 /*
1485 * Lock the open file structure so that no other thread can manipulate
1486 * the same open file at a time.
1487 */
1488 fibril_mutex_lock(&oldfile->lock);
1489
1490 /* Lookup an open file structure possibly corresponding to newfd. */
1491 vfs_file_t *newfile = vfs_file_get(newfd);
1492 if (newfile) {
1493 /* Close the originally opened file. */
1494 int ret = vfs_close_internal(newfile);
1495 if (ret != EOK) {
1496 ipc_answer_0(rid, ret);
1497 return;
1498 }
1499
1500 ret = vfs_fd_free(newfd);
1501 if (ret != EOK) {
1502 ipc_answer_0(rid, ret);
1503 return;
1504 }
1505 }
1506
1507 /* Assign the old file to newfd. */
1508 int ret = vfs_fd_assign(oldfile, newfd);
1509 fibril_mutex_unlock(&oldfile->lock);
1510
1511 if (ret != EOK)
1512 ipc_answer_0(rid, ret);
1513 else
1514 ipc_answer_1(rid, EOK, newfd);
1515}
1516
1517/**
1518 * @}
1519 */
Note: See TracBrowser for help on using the repository browser.