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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ce9d803 was 2b88074b, checked in by Martin Decky <martin@…>, 16 years ago

vfs: file descriptors housekeeping changes

  • add support for allocating new file descriptors from the end of the fd range (O_DESC)
  • add support for assigning of an already opened file to a free file descriptor

vfs: VFS_OUT_CLOSE is called only when the file reference count is about to drop to zero
vfs: implement VFS_IN_DUP

libc: optimize current working directory housekeeping

  • using opendir() was an overkill
  • allocate current working directory file descriptor from the end of the fd range using O_DESC (so it doesn't mess with the well-known descriptors 0, 1 and 2)

libc: implement dup2() (via VFS_IN_DUP)

getvc: change stdin/stdout/stderr by a slightly more elegant way (using dup2())

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