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

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

Merge from lp:~zarevucky-jiri/helenos/vfs-2.5 revisions 1929-1930

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