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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c8a894d was c8a894d, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

Fix IPC answer in case of error

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