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

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

improve naming conventions:
merge async_data_receive() and async_string_receive() into async_data_write_accept()
rename async_data_void() to async_data_write_void()
rename async_data_forward_fast() to async_data_write_forward_fast()
rename async_data_forward_n_m to async_data_write_forward_n_m

  • Property mode set to 100644
File size: 33.4 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_MP, &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 char *mp;
269 int rc = async_data_write_accept((char **) &mp, true, 0, MAX_PATH_LEN,
270 0, NULL);
271 if (rc != EOK) {
272 ipc_answer_0(rid, rc);
273 return;
274 }
275
276 /* Now we expect to receive the mount options. */
277 char *opts;
278 rc = async_data_write_accept((char **) &opts, true, 0, MAX_MNTOPTS_LEN,
279 0, NULL);
280 if (rc != EOK) {
281 free(mp);
282 ipc_answer_0(rid, rc);
283 return;
284 }
285
286 /*
287 * Now, we expect the client to send us data with the name of the file
288 * system.
289 */
290 char *fs_name;
291 rc = async_data_write_accept((char **) &fs_name, true, 0, FS_NAME_MAXLEN,
292 0, NULL);
293 if (rc != EOK) {
294 free(mp);
295 free(opts);
296 ipc_answer_0(rid, rc);
297 return;
298 }
299
300 /*
301 * Wait for IPC_M_PING so that we can return an error if we don't know
302 * fs_name.
303 */
304 ipc_call_t data;
305 ipc_callid_t callid = async_get_call(&data);
306 if (IPC_GET_METHOD(data) != IPC_M_PING) {
307 ipc_answer_0(callid, ENOTSUP);
308 ipc_answer_0(rid, ENOTSUP);
309 free(mp);
310 free(opts);
311 free(fs_name);
312 return;
313 }
314
315 /*
316 * Check if we know a file system with the same name as is in fs_name.
317 * This will also give us its file system handle.
318 */
319 fibril_mutex_lock(&fs_head_lock);
320 fs_handle_t fs_handle;
321recheck:
322 fs_handle = fs_name_to_handle(fs_name, false);
323 if (!fs_handle) {
324 if (flags & IPC_FLAG_BLOCKING) {
325 fibril_condvar_wait(&fs_head_cv, &fs_head_lock);
326 goto recheck;
327 }
328
329 fibril_mutex_unlock(&fs_head_lock);
330 ipc_answer_0(callid, ENOENT);
331 ipc_answer_0(rid, ENOENT);
332 free(mp);
333 free(fs_name);
334 free(opts);
335 return;
336 }
337 fibril_mutex_unlock(&fs_head_lock);
338
339 /* Acknowledge that we know fs_name. */
340 ipc_answer_0(callid, EOK);
341
342 /* Do the mount */
343 vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts);
344 free(mp);
345 free(fs_name);
346 free(opts);
347}
348
349void vfs_unmount(ipc_callid_t rid, ipc_call_t *request)
350{
351 int rc;
352 char *mp;
353 vfs_lookup_res_t mp_res;
354 vfs_lookup_res_t mr_res;
355 vfs_node_t *mp_node;
356 vfs_node_t *mr_node;
357 int phone;
358
359 /*
360 * Receive the mount point path.
361 */
362 rc = async_data_write_accept((char **) &mp, true, 0, MAX_PATH_LEN,
363 0, NULL);
364 if (rc != EOK)
365 ipc_answer_0(rid, rc);
366
367 /*
368 * Taking the namespace lock will do two things for us. First, it will
369 * prevent races with other lookup operations. Second, it will stop new
370 * references to already existing VFS nodes and creation of new VFS
371 * nodes. This is because new references are added as a result of some
372 * lookup operation or at least of some operation which is protected by
373 * the namespace lock.
374 */
375 fibril_rwlock_write_lock(&namespace_rwlock);
376
377 /*
378 * Lookup the mounted root and instantiate it.
379 */
380 rc = vfs_lookup_internal(mp, L_ROOT, &mr_res, NULL);
381 if (rc != EOK) {
382 fibril_rwlock_write_unlock(&namespace_rwlock);
383 free(mp);
384 ipc_answer_0(rid, rc);
385 return;
386 }
387 mr_node = vfs_node_get(&mr_res);
388 if (!mr_node) {
389 fibril_rwlock_write_unlock(&namespace_rwlock);
390 free(mp);
391 ipc_answer_0(rid, ENOMEM);
392 return;
393 }
394
395 /*
396 * Count the total number of references for the mounted file system. We
397 * are expecting at least two. One which we got above and one which we
398 * got when the file system was mounted. If we find more, it means that
399 * the file system cannot be gracefully unmounted at the moment because
400 * someone is working with it.
401 */
402 if (vfs_nodes_refcount_sum_get(mr_node->fs_handle,
403 mr_node->dev_handle) != 2) {
404 fibril_rwlock_write_unlock(&namespace_rwlock);
405 vfs_node_put(mr_node);
406 free(mp);
407 ipc_answer_0(rid, EBUSY);
408 return;
409 }
410
411 if (str_cmp(mp, "/") == 0) {
412
413 /*
414 * Unmounting the root file system.
415 *
416 * In this case, there is no mount point node and we send
417 * VFS_OUT_UNMOUNTED directly to the mounted file system.
418 */
419
420 free(mp);
421 phone = vfs_grab_phone(mr_node->fs_handle);
422 rc = async_req_1_0(phone, VFS_OUT_UNMOUNTED,
423 mr_node->dev_handle);
424 vfs_release_phone(phone);
425 if (rc != EOK) {
426 fibril_rwlock_write_unlock(&namespace_rwlock);
427 vfs_node_put(mr_node);
428 ipc_answer_0(rid, rc);
429 return;
430 }
431 rootfs.fs_handle = 0;
432 rootfs.dev_handle = 0;
433 } else {
434
435 /*
436 * Unmounting a non-root file system.
437 *
438 * We have a regular mount point node representing the parent
439 * file system, so we delegate the operation to it.
440 */
441
442 rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
443 free(mp);
444 if (rc != EOK) {
445 fibril_rwlock_write_unlock(&namespace_rwlock);
446 vfs_node_put(mr_node);
447 ipc_answer_0(rid, rc);
448 return;
449 }
450 vfs_node_t *mp_node = vfs_node_get(&mp_res);
451 if (!mp_node) {
452 fibril_rwlock_write_unlock(&namespace_rwlock);
453 vfs_node_put(mr_node);
454 ipc_answer_0(rid, ENOMEM);
455 return;
456 }
457
458 phone = vfs_grab_phone(mp_node->fs_handle);
459 rc = async_req_2_0(phone, VFS_OUT_UNMOUNT, mp_node->dev_handle,
460 mp_node->index);
461 vfs_release_phone(phone);
462 if (rc != EOK) {
463 fibril_rwlock_write_unlock(&namespace_rwlock);
464 vfs_node_put(mp_node);
465 vfs_node_put(mr_node);
466 ipc_answer_0(rid, rc);
467 return;
468 }
469
470 /* Drop the reference we got above. */
471 vfs_node_put(mp_node);
472 /* Drop the reference from when the file system was mounted. */
473 vfs_node_put(mp_node);
474 }
475
476
477 /*
478 * All went well, the mounted file system was successfully unmounted.
479 * The only thing left is to forget the unmounted root VFS node.
480 */
481 vfs_node_forget(mr_node);
482
483 fibril_rwlock_write_unlock(&namespace_rwlock);
484 ipc_answer_0(rid, EOK);
485}
486
487void vfs_open(ipc_callid_t rid, ipc_call_t *request)
488{
489 if (!vfs_files_init()) {
490 ipc_answer_0(rid, ENOMEM);
491 return;
492 }
493
494 /*
495 * The POSIX interface is open(path, oflag, mode).
496 * We can receive oflags and mode along with the VFS_IN_OPEN call;
497 * the path will need to arrive in another call.
498 *
499 * We also receive one private, non-POSIX set of flags called lflag
500 * used to pass information to vfs_lookup_internal().
501 */
502 int lflag = IPC_GET_ARG1(*request);
503 int oflag = IPC_GET_ARG2(*request);
504 int mode = IPC_GET_ARG3(*request);
505 size_t len;
506
507 /* Ignore mode for now. */
508 (void) mode;
509
510 /*
511 * Make sure that we are called with exactly one of L_FILE and
512 * L_DIRECTORY. Make sure that the user does not pass L_OPEN,
513 * L_ROOT or L_MP.
514 */
515 if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
516 ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
517 (lflag & (L_OPEN | L_ROOT | L_MP))) {
518 ipc_answer_0(rid, EINVAL);
519 return;
520 }
521
522 if (oflag & O_CREAT)
523 lflag |= L_CREATE;
524 if (oflag & O_EXCL)
525 lflag |= L_EXCLUSIVE;
526
527 char *path;
528 int rc = async_data_write_accept((char **) &path, true, 0, 0, 0, NULL);
529 if (rc != EOK) {
530 ipc_answer_0(rid, rc);
531 return;
532 }
533
534 /*
535 * Avoid the race condition in which the file can be deleted before we
536 * find/create-and-lock the VFS node corresponding to the looked-up
537 * triplet.
538 */
539 if (lflag & L_CREATE)
540 fibril_rwlock_write_lock(&namespace_rwlock);
541 else
542 fibril_rwlock_read_lock(&namespace_rwlock);
543
544 /* The path is now populated and we can call vfs_lookup_internal(). */
545 vfs_lookup_res_t lr;
546 rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
547 if (rc != EOK) {
548 if (lflag & L_CREATE)
549 fibril_rwlock_write_unlock(&namespace_rwlock);
550 else
551 fibril_rwlock_read_unlock(&namespace_rwlock);
552 ipc_answer_0(rid, rc);
553 free(path);
554 return;
555 }
556
557 /* Path is no longer needed. */
558 free(path);
559
560 vfs_node_t *node = vfs_node_get(&lr);
561 if (lflag & L_CREATE)
562 fibril_rwlock_write_unlock(&namespace_rwlock);
563 else
564 fibril_rwlock_read_unlock(&namespace_rwlock);
565
566 /* Truncate the file if requested and if necessary. */
567 if (oflag & O_TRUNC) {
568 fibril_rwlock_write_lock(&node->contents_rwlock);
569 if (node->size) {
570 rc = vfs_truncate_internal(node->fs_handle,
571 node->dev_handle, node->index, 0);
572 if (rc) {
573 fibril_rwlock_write_unlock(&node->contents_rwlock);
574 vfs_node_put(node);
575 ipc_answer_0(rid, rc);
576 return;
577 }
578 node->size = 0;
579 }
580 fibril_rwlock_write_unlock(&node->contents_rwlock);
581 }
582
583 /*
584 * Get ourselves a file descriptor and the corresponding vfs_file_t
585 * structure.
586 */
587 int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
588 if (fd < 0) {
589 vfs_node_put(node);
590 ipc_answer_0(rid, fd);
591 return;
592 }
593 vfs_file_t *file = vfs_file_get(fd);
594 file->node = node;
595 if (oflag & O_APPEND)
596 file->append = true;
597
598 /*
599 * The following increase in reference count is for the fact that the
600 * file is being opened and that a file structure is pointing to it.
601 * It is necessary so that the file will not disappear when
602 * vfs_node_put() is called. The reference will be dropped by the
603 * respective VFS_IN_CLOSE.
604 */
605 vfs_node_addref(node);
606 vfs_node_put(node);
607
608 /* Success! Return the new file descriptor to the client. */
609 ipc_answer_1(rid, EOK, fd);
610}
611
612void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
613{
614 // FIXME: check for sanity of the supplied fs, dev and index
615
616 if (!vfs_files_init()) {
617 ipc_answer_0(rid, ENOMEM);
618 return;
619 }
620
621 /*
622 * The interface is open_node(fs, dev, index, oflag).
623 */
624 vfs_lookup_res_t lr;
625
626 lr.triplet.fs_handle = IPC_GET_ARG1(*request);
627 lr.triplet.dev_handle = IPC_GET_ARG2(*request);
628 lr.triplet.index = IPC_GET_ARG3(*request);
629 int oflag = IPC_GET_ARG4(*request);
630
631 fibril_rwlock_read_lock(&namespace_rwlock);
632
633 int rc = vfs_open_node_internal(&lr);
634 if (rc != EOK) {
635 fibril_rwlock_read_unlock(&namespace_rwlock);
636 ipc_answer_0(rid, rc);
637 return;
638 }
639
640 vfs_node_t *node = vfs_node_get(&lr);
641 fibril_rwlock_read_unlock(&namespace_rwlock);
642
643 /* Truncate the file if requested and if necessary. */
644 if (oflag & O_TRUNC) {
645 fibril_rwlock_write_lock(&node->contents_rwlock);
646 if (node->size) {
647 rc = vfs_truncate_internal(node->fs_handle,
648 node->dev_handle, node->index, 0);
649 if (rc) {
650 fibril_rwlock_write_unlock(&node->contents_rwlock);
651 vfs_node_put(node);
652 ipc_answer_0(rid, rc);
653 return;
654 }
655 node->size = 0;
656 }
657 fibril_rwlock_write_unlock(&node->contents_rwlock);
658 }
659
660 /*
661 * Get ourselves a file descriptor and the corresponding vfs_file_t
662 * structure.
663 */
664 int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
665 if (fd < 0) {
666 vfs_node_put(node);
667 ipc_answer_0(rid, fd);
668 return;
669 }
670 vfs_file_t *file = vfs_file_get(fd);
671 file->node = node;
672 if (oflag & O_APPEND)
673 file->append = true;
674
675 /*
676 * The following increase in reference count is for the fact that the
677 * file is being opened and that a file structure is pointing to it.
678 * It is necessary so that the file will not disappear when
679 * vfs_node_put() is called. The reference will be dropped by the
680 * respective VFS_IN_CLOSE.
681 */
682 vfs_node_addref(node);
683 vfs_node_put(node);
684
685 /* Success! Return the new file descriptor to the client. */
686 ipc_answer_1(rid, EOK, fd);
687}
688
689void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
690{
691 int fd = IPC_GET_ARG1(*request);
692
693 /* Lookup the file structure corresponding to the file descriptor. */
694 vfs_file_t *file = vfs_file_get(fd);
695 if (!file) {
696 ipc_answer_0(rid, ENOENT);
697 return;
698 }
699
700 /*
701 * Lock the open file structure so that no other thread can manipulate
702 * the same open file at a time.
703 */
704 fibril_mutex_lock(&file->lock);
705 int fs_phone = vfs_grab_phone(file->node->fs_handle);
706
707 /* Make a VFS_OUT_SYMC request at the destination FS server. */
708 aid_t msg;
709 ipc_call_t answer;
710 msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->dev_handle,
711 file->node->index, &answer);
712
713 /* Wait for reply from the FS server. */
714 ipcarg_t rc;
715 async_wait_for(msg, &rc);
716
717 vfs_release_phone(fs_phone);
718 fibril_mutex_unlock(&file->lock);
719
720 ipc_answer_0(rid, rc);
721}
722
723int vfs_close_internal(vfs_file_t *file)
724{
725 /*
726 * Lock the open file structure so that no other thread can manipulate
727 * the same open file at a time.
728 */
729 fibril_mutex_lock(&file->lock);
730
731 if (file->refcnt <= 1) {
732 /* Only close the file on the destination FS server
733 if there are no more file descriptors (except the
734 present one) pointing to this file. */
735
736 int fs_phone = vfs_grab_phone(file->node->fs_handle);
737
738 /* Make a VFS_OUT_CLOSE request at the destination FS server. */
739 aid_t msg;
740 ipc_call_t answer;
741 msg = async_send_2(fs_phone, VFS_OUT_CLOSE, file->node->dev_handle,
742 file->node->index, &answer);
743
744 /* Wait for reply from the FS server. */
745 ipcarg_t rc;
746 async_wait_for(msg, &rc);
747
748 vfs_release_phone(fs_phone);
749 fibril_mutex_unlock(&file->lock);
750
751 return IPC_GET_ARG1(answer);
752 }
753
754 fibril_mutex_unlock(&file->lock);
755 return EOK;
756}
757
758void vfs_close(ipc_callid_t rid, ipc_call_t *request)
759{
760 int fd = IPC_GET_ARG1(*request);
761
762 /* Lookup the file structure corresponding to the file descriptor. */
763 vfs_file_t *file = vfs_file_get(fd);
764 if (!file) {
765 ipc_answer_0(rid, ENOENT);
766 return;
767 }
768
769 int ret = vfs_close_internal(file);
770 if (ret != EOK)
771 ipc_answer_0(rid, ret);
772
773 ret = vfs_fd_free(fd);
774 ipc_answer_0(rid, ret);
775}
776
777static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
778{
779
780 /*
781 * The following code strongly depends on the fact that the files data
782 * structure can be only accessed by a single fibril and all file
783 * operations are serialized (i.e. the reads and writes cannot
784 * interleave and a file cannot be closed while it is being read).
785 *
786 * Additional synchronization needs to be added once the table of
787 * open files supports parallel access!
788 */
789
790 int fd = IPC_GET_ARG1(*request);
791
792 /* Lookup the file structure corresponding to the file descriptor. */
793 vfs_file_t *file = vfs_file_get(fd);
794 if (!file) {
795 ipc_answer_0(rid, ENOENT);
796 return;
797 }
798
799 /*
800 * Lock the open file structure so that no other thread can manipulate
801 * the same open file at a time.
802 */
803 fibril_mutex_lock(&file->lock);
804
805 /*
806 * Lock the file's node so that no other client can read/write to it at
807 * the same time.
808 */
809 if (read)
810 fibril_rwlock_read_lock(&file->node->contents_rwlock);
811 else
812 fibril_rwlock_write_lock(&file->node->contents_rwlock);
813
814 if (file->node->type == VFS_NODE_DIRECTORY) {
815 /*
816 * Make sure that no one is modifying the namespace
817 * while we are in readdir().
818 */
819 assert(read);
820 fibril_rwlock_read_lock(&namespace_rwlock);
821 }
822
823 int fs_phone = vfs_grab_phone(file->node->fs_handle);
824
825 /*
826 * Make a VFS_READ/VFS_WRITE request at the destination FS server
827 * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
828 * destination FS server. The call will be routed as if sent by
829 * ourselves. Note that call arguments are immutable in this case so we
830 * don't have to bother.
831 */
832 ipcarg_t rc;
833 ipc_call_t answer;
834 if (read) {
835 if (file->append)
836 file->pos = file->node->size;
837
838 rc = async_data_read_forward_3_1(fs_phone, VFS_OUT_READ,
839 file->node->dev_handle, file->node->index, file->pos,
840 &answer);
841 } else {
842 rc = async_data_write_forward_3_1(fs_phone, VFS_OUT_WRITE,
843 file->node->dev_handle, file->node->index, file->pos,
844 &answer);
845 }
846
847 vfs_release_phone(fs_phone);
848
849 size_t bytes = IPC_GET_ARG1(answer);
850
851 if (file->node->type == VFS_NODE_DIRECTORY)
852 fibril_rwlock_read_unlock(&namespace_rwlock);
853
854 /* Unlock the VFS node. */
855 if (read)
856 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
857 else {
858 /* Update the cached version of node's size. */
859 if (rc == EOK)
860 file->node->size = IPC_GET_ARG2(answer);
861 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
862 }
863
864 /* Update the position pointer and unlock the open file. */
865 if (rc == EOK)
866 file->pos += bytes;
867 fibril_mutex_unlock(&file->lock);
868
869 /*
870 * FS server's reply is the final result of the whole operation we
871 * return to the client.
872 */
873 ipc_answer_1(rid, rc, bytes);
874}
875
876void vfs_read(ipc_callid_t rid, ipc_call_t *request)
877{
878 vfs_rdwr(rid, request, true);
879}
880
881void vfs_write(ipc_callid_t rid, ipc_call_t *request)
882{
883 vfs_rdwr(rid, request, false);
884}
885
886void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
887{
888 int fd = (int) IPC_GET_ARG1(*request);
889 off_t off = (off_t) IPC_GET_ARG2(*request);
890 int whence = (int) IPC_GET_ARG3(*request);
891
892
893 /* Lookup the file structure corresponding to the file descriptor. */
894 vfs_file_t *file = vfs_file_get(fd);
895 if (!file) {
896 ipc_answer_0(rid, ENOENT);
897 return;
898 }
899
900 off_t newpos;
901 fibril_mutex_lock(&file->lock);
902 if (whence == SEEK_SET) {
903 file->pos = off;
904 fibril_mutex_unlock(&file->lock);
905 ipc_answer_1(rid, EOK, off);
906 return;
907 }
908 if (whence == SEEK_CUR) {
909 if (file->pos + off < file->pos) {
910 fibril_mutex_unlock(&file->lock);
911 ipc_answer_0(rid, EOVERFLOW);
912 return;
913 }
914 file->pos += off;
915 newpos = file->pos;
916 fibril_mutex_unlock(&file->lock);
917 ipc_answer_1(rid, EOK, newpos);
918 return;
919 }
920 if (whence == SEEK_END) {
921 fibril_rwlock_read_lock(&file->node->contents_rwlock);
922 size_t size = file->node->size;
923 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
924 if (size + off < size) {
925 fibril_mutex_unlock(&file->lock);
926 ipc_answer_0(rid, EOVERFLOW);
927 return;
928 }
929 newpos = size + off;
930 file->pos = newpos;
931 fibril_mutex_unlock(&file->lock);
932 ipc_answer_1(rid, EOK, newpos);
933 return;
934 }
935 fibril_mutex_unlock(&file->lock);
936 ipc_answer_0(rid, EINVAL);
937}
938
939int
940vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
941 fs_index_t index, size_t size)
942{
943 ipcarg_t rc;
944 int fs_phone;
945
946 fs_phone = vfs_grab_phone(fs_handle);
947 rc = async_req_3_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t)dev_handle,
948 (ipcarg_t)index, (ipcarg_t)size);
949 vfs_release_phone(fs_phone);
950 return (int)rc;
951}
952
953void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
954{
955 int fd = IPC_GET_ARG1(*request);
956 size_t size = IPC_GET_ARG2(*request);
957 int rc;
958
959 vfs_file_t *file = vfs_file_get(fd);
960 if (!file) {
961 ipc_answer_0(rid, ENOENT);
962 return;
963 }
964 fibril_mutex_lock(&file->lock);
965
966 fibril_rwlock_write_lock(&file->node->contents_rwlock);
967 rc = vfs_truncate_internal(file->node->fs_handle,
968 file->node->dev_handle, file->node->index, size);
969 if (rc == EOK)
970 file->node->size = size;
971 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
972
973 fibril_mutex_unlock(&file->lock);
974 ipc_answer_0(rid, (ipcarg_t)rc);
975}
976
977void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
978{
979 int fd = IPC_GET_ARG1(*request);
980 ipcarg_t rc;
981
982 vfs_file_t *file = vfs_file_get(fd);
983 if (!file) {
984 ipc_answer_0(rid, ENOENT);
985 return;
986 }
987
988 ipc_callid_t callid;
989 if (!async_data_read_receive(&callid, NULL)) {
990 ipc_answer_0(callid, EINVAL);
991 ipc_answer_0(rid, EINVAL);
992 return;
993 }
994
995 fibril_mutex_lock(&file->lock);
996
997 int fs_phone = vfs_grab_phone(file->node->fs_handle);
998
999 aid_t msg;
1000 msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->dev_handle,
1001 file->node->index, true, NULL);
1002 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1003 async_wait_for(msg, &rc);
1004 vfs_release_phone(fs_phone);
1005
1006 fibril_mutex_unlock(&file->lock);
1007 ipc_answer_0(rid, rc);
1008}
1009
1010void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
1011{
1012 char *path;
1013 int rc = async_data_write_accept((char **) &path, true, 0, 0, 0, NULL);
1014 if (rc != EOK) {
1015 ipc_answer_0(rid, rc);
1016 return;
1017 }
1018
1019 ipc_callid_t callid;
1020 if (!async_data_read_receive(&callid, NULL)) {
1021 free(path);
1022 ipc_answer_0(callid, EINVAL);
1023 ipc_answer_0(rid, EINVAL);
1024 return;
1025 }
1026
1027 vfs_lookup_res_t lr;
1028 fibril_rwlock_read_lock(&namespace_rwlock);
1029 rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
1030 free(path);
1031 if (rc != EOK) {
1032 fibril_rwlock_read_unlock(&namespace_rwlock);
1033 ipc_answer_0(callid, rc);
1034 ipc_answer_0(rid, rc);
1035 return;
1036 }
1037 vfs_node_t *node = vfs_node_get(&lr);
1038 if (!node) {
1039 fibril_rwlock_read_unlock(&namespace_rwlock);
1040 ipc_answer_0(callid, ENOMEM);
1041 ipc_answer_0(rid, ENOMEM);
1042 return;
1043 }
1044
1045 fibril_rwlock_read_unlock(&namespace_rwlock);
1046
1047 int fs_phone = vfs_grab_phone(node->fs_handle);
1048 aid_t msg;
1049 msg = async_send_3(fs_phone, VFS_OUT_STAT, node->dev_handle,
1050 node->index, false, NULL);
1051 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1052
1053 ipcarg_t rv;
1054 async_wait_for(msg, &rv);
1055 vfs_release_phone(fs_phone);
1056
1057 ipc_answer_0(rid, rv);
1058
1059 vfs_node_put(node);
1060}
1061
1062void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
1063{
1064 int mode = IPC_GET_ARG1(*request);
1065
1066 char *path;
1067 int rc = async_data_write_accept((char **) &path, true, 0, 0, 0, NULL);
1068 if (rc != EOK) {
1069 ipc_answer_0(rid, rc);
1070 return;
1071 }
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 char *path;
1089 int rc = async_data_write_accept((char **) &path, true, 0, 0, 0, NULL);
1090 if (rc != EOK) {
1091 ipc_answer_0(rid, rc);
1092 return;
1093 }
1094
1095 fibril_rwlock_write_lock(&namespace_rwlock);
1096 lflag &= L_DIRECTORY; /* sanitize lflag */
1097 vfs_lookup_res_t lr;
1098 rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
1099 free(path);
1100 if (rc != EOK) {
1101 fibril_rwlock_write_unlock(&namespace_rwlock);
1102 ipc_answer_0(rid, rc);
1103 return;
1104 }
1105
1106 /*
1107 * The name has already been unlinked by vfs_lookup_internal().
1108 * We have to get and put the VFS node to ensure that it is
1109 * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
1110 */
1111 vfs_node_t *node = vfs_node_get(&lr);
1112 fibril_mutex_lock(&nodes_mutex);
1113 node->lnkcnt--;
1114 fibril_mutex_unlock(&nodes_mutex);
1115 fibril_rwlock_write_unlock(&namespace_rwlock);
1116 vfs_node_put(node);
1117 ipc_answer_0(rid, EOK);
1118}
1119
1120void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1121{
1122 /* Retrieve the old path. */
1123 char *old;
1124 int rc = async_data_write_accept((char **) &old, true, 0, 0, 0, NULL);
1125 if (rc != EOK) {
1126 ipc_answer_0(rid, rc);
1127 return;
1128 }
1129
1130 /* Retrieve the new path. */
1131 char *new;
1132 rc = async_data_write_accept((char **) &new, true, 0, 0, 0, NULL);
1133 if (rc != EOK) {
1134 free(old);
1135 ipc_answer_0(rid, rc);
1136 return;
1137 }
1138
1139 size_t olen;
1140 size_t nlen;
1141 char *oldc = canonify(old, &olen);
1142 char *newc = canonify(new, &nlen);
1143
1144 if ((!oldc) || (!newc)) {
1145 ipc_answer_0(rid, EINVAL);
1146 free(old);
1147 free(new);
1148 return;
1149 }
1150
1151 oldc[olen] = '\0';
1152 newc[nlen] = '\0';
1153
1154 if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
1155 ((newc[str_length(oldc)] == '/') ||
1156 (str_length(oldc) == 1) ||
1157 (str_length(oldc) == str_length(newc)))) {
1158 /*
1159 * oldc is a prefix of newc and either
1160 * - newc continues with a / where oldc ends, or
1161 * - oldc was / itself, or
1162 * - oldc and newc are equal.
1163 */
1164 ipc_answer_0(rid, EINVAL);
1165 free(old);
1166 free(new);
1167 return;
1168 }
1169
1170 vfs_lookup_res_t old_lr;
1171 vfs_lookup_res_t new_lr;
1172 vfs_lookup_res_t new_par_lr;
1173 fibril_rwlock_write_lock(&namespace_rwlock);
1174
1175 /* Lookup the node belonging to the old file name. */
1176 rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1177 if (rc != EOK) {
1178 fibril_rwlock_write_unlock(&namespace_rwlock);
1179 ipc_answer_0(rid, rc);
1180 free(old);
1181 free(new);
1182 return;
1183 }
1184
1185 vfs_node_t *old_node = vfs_node_get(&old_lr);
1186 if (!old_node) {
1187 fibril_rwlock_write_unlock(&namespace_rwlock);
1188 ipc_answer_0(rid, ENOMEM);
1189 free(old);
1190 free(new);
1191 return;
1192 }
1193
1194 /* Determine the path to the parent of the node with the new name. */
1195 char *parentc = str_dup(newc);
1196 if (!parentc) {
1197 fibril_rwlock_write_unlock(&namespace_rwlock);
1198 ipc_answer_0(rid, rc);
1199 free(old);
1200 free(new);
1201 return;
1202 }
1203
1204 char *lastsl = str_rchr(parentc + 1, '/');
1205 if (lastsl)
1206 *lastsl = '\0';
1207 else
1208 parentc[1] = '\0';
1209
1210 /* Lookup parent of the new file name. */
1211 rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
1212 free(parentc); /* not needed anymore */
1213 if (rc != EOK) {
1214 fibril_rwlock_write_unlock(&namespace_rwlock);
1215 ipc_answer_0(rid, rc);
1216 free(old);
1217 free(new);
1218 return;
1219 }
1220
1221 /* Check whether linking to the same file system instance. */
1222 if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1223 (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
1224 fibril_rwlock_write_unlock(&namespace_rwlock);
1225 ipc_answer_0(rid, EXDEV); /* different file systems */
1226 free(old);
1227 free(new);
1228 return;
1229 }
1230
1231 /* Destroy the old link for the new name. */
1232 vfs_node_t *new_node = NULL;
1233 rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1234
1235 switch (rc) {
1236 case ENOENT:
1237 /* simply not in our way */
1238 break;
1239 case EOK:
1240 new_node = vfs_node_get(&new_lr);
1241 if (!new_node) {
1242 fibril_rwlock_write_unlock(&namespace_rwlock);
1243 ipc_answer_0(rid, ENOMEM);
1244 free(old);
1245 free(new);
1246 return;
1247 }
1248 fibril_mutex_lock(&nodes_mutex);
1249 new_node->lnkcnt--;
1250 fibril_mutex_unlock(&nodes_mutex);
1251 break;
1252 default:
1253 fibril_rwlock_write_unlock(&namespace_rwlock);
1254 ipc_answer_0(rid, ENOTEMPTY);
1255 free(old);
1256 free(new);
1257 return;
1258 }
1259
1260 /* Create the new link for the new name. */
1261 rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1262 if (rc != EOK) {
1263 fibril_rwlock_write_unlock(&namespace_rwlock);
1264 if (new_node)
1265 vfs_node_put(new_node);
1266 ipc_answer_0(rid, rc);
1267 free(old);
1268 free(new);
1269 return;
1270 }
1271
1272 fibril_mutex_lock(&nodes_mutex);
1273 old_node->lnkcnt++;
1274 fibril_mutex_unlock(&nodes_mutex);
1275
1276 /* Destroy the link for the old name. */
1277 rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1278 if (rc != EOK) {
1279 fibril_rwlock_write_unlock(&namespace_rwlock);
1280 vfs_node_put(old_node);
1281 if (new_node)
1282 vfs_node_put(new_node);
1283 ipc_answer_0(rid, rc);
1284 free(old);
1285 free(new);
1286 return;
1287 }
1288
1289 fibril_mutex_lock(&nodes_mutex);
1290 old_node->lnkcnt--;
1291 fibril_mutex_unlock(&nodes_mutex);
1292 fibril_rwlock_write_unlock(&namespace_rwlock);
1293 vfs_node_put(old_node);
1294
1295 if (new_node)
1296 vfs_node_put(new_node);
1297
1298 free(old);
1299 free(new);
1300 ipc_answer_0(rid, EOK);
1301}
1302
1303void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
1304{
1305 int oldfd = IPC_GET_ARG1(*request);
1306 int newfd = IPC_GET_ARG2(*request);
1307
1308 /* Lookup the file structure corresponding to oldfd. */
1309 vfs_file_t *oldfile = vfs_file_get(oldfd);
1310 if (!oldfile) {
1311 ipc_answer_0(rid, EBADF);
1312 return;
1313 }
1314
1315 /* If the file descriptors are the same, do nothing. */
1316 if (oldfd == newfd) {
1317 ipc_answer_1(rid, EOK, newfd);
1318 return;
1319 }
1320
1321 /*
1322 * Lock the open file structure so that no other thread can manipulate
1323 * the same open file at a time.
1324 */
1325 fibril_mutex_lock(&oldfile->lock);
1326
1327 /* Lookup an open file structure possibly corresponding to newfd. */
1328 vfs_file_t *newfile = vfs_file_get(newfd);
1329 if (newfile) {
1330 /* Close the originally opened file. */
1331 int ret = vfs_close_internal(newfile);
1332 if (ret != EOK) {
1333 ipc_answer_0(rid, ret);
1334 return;
1335 }
1336
1337 ret = vfs_fd_free(newfd);
1338 if (ret != EOK) {
1339 ipc_answer_0(rid, ret);
1340 return;
1341 }
1342 }
1343
1344 /* Assign the old file to newfd. */
1345 int ret = vfs_fd_assign(oldfile, newfd);
1346 fibril_mutex_unlock(&oldfile->lock);
1347
1348 if (ret != EOK)
1349 ipc_answer_0(rid, ret);
1350 else
1351 ipc_answer_1(rid, EOK, newfd);
1352}
1353
1354/**
1355 * @}
1356 */
Note: See TracBrowser for help on using the repository browser.