source: mainline/uspace/srv/vfs/vfs_ops.c@ 930f5c3

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

Merge from lp:~zarevucky-jiri/helenos/vfs-2.5/ revision 1938

Original commit message:

1938: Jiri Zarevucky 2013-08-06 Fix a bug in clone.

  • Property mode set to 100644
File size: 32.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 <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 EBADF;
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 bool rlock = read || ((fs_info->concurrent_read_write) && (fs_info->write_retains_size));
709
710 /*
711 * Lock the file's node so that no other client can read/write to it at
712 * the same time unless the FS supports concurrent reads/writes and its
713 * write implementation does not modify the file size.
714 */
715 if (rlock) {
716 fibril_rwlock_read_lock(&file->node->contents_rwlock);
717 } else {
718 fibril_rwlock_write_lock(&file->node->contents_rwlock);
719 }
720
721 if (file->node->type == VFS_NODE_DIRECTORY) {
722 /*
723 * Make sure that no one is modifying the namespace
724 * while we are in readdir().
725 */
726
727 if (!read) {
728 if (rlock) {
729 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
730 } else {
731 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
732 }
733 vfs_file_put(file);
734 return EINVAL;
735 }
736
737 fibril_rwlock_read_lock(&namespace_rwlock);
738 }
739
740 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
741
742 if (!read && file->append)
743 file->pos = file->node->size;
744
745 /*
746 * Handle communication with the endpoint FS.
747 */
748 ipc_call_t answer;
749 int rc = ipc_cb(fs_exch, file, &answer, read, ipc_cb_data);
750
751 vfs_exchange_release(fs_exch);
752
753 size_t bytes = IPC_GET_ARG1(answer);
754
755 if (file->node->type == VFS_NODE_DIRECTORY) {
756 fibril_rwlock_read_unlock(&namespace_rwlock);
757 }
758
759 /* Unlock the VFS node. */
760 if (rlock) {
761 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
762 } else {
763 /* Update the cached version of node's size. */
764 if (rc == EOK) {
765 file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
766 IPC_GET_ARG3(answer));
767 }
768 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
769 }
770
771 /* Update the position pointer and unlock the open file. */
772 if (rc == EOK) {
773 file->pos += bytes;
774 }
775 vfs_file_put(file);
776
777 return rc;
778}
779
780static void vfs_rdwr_client(ipc_callid_t rid, ipc_call_t *request, bool read)
781{
782 size_t bytes = 0;
783 int rc = vfs_rdwr(IPC_GET_ARG1(*request), read, rdwr_ipc_client,
784 &bytes);
785 async_answer_1(rid, rc, bytes);
786}
787
788int vfs_rdwr_internal(int fd, bool read, rdwr_io_chunk_t *chunk)
789{
790 return vfs_rdwr(fd, read, rdwr_ipc_internal, chunk);
791}
792
793void vfs_read(ipc_callid_t rid, ipc_call_t *request)
794{
795 vfs_rdwr_client(rid, request, true);
796}
797
798void vfs_write(ipc_callid_t rid, ipc_call_t *request)
799{
800 vfs_rdwr_client(rid, request, false);
801}
802
803void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
804{
805 int fd = (int) IPC_GET_ARG1(*request);
806 off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
807 IPC_GET_ARG3(*request));
808 int whence = (int) IPC_GET_ARG4(*request);
809
810 /* Lookup the file structure corresponding to the file descriptor. */
811 vfs_file_t *file = vfs_file_get(fd);
812 if (!file) {
813 async_answer_0(rid, ENOENT);
814 return;
815 }
816
817 off64_t newoff;
818 switch (whence) {
819 case SEEK_SET:
820 if (off >= 0) {
821 file->pos = (aoff64_t) off;
822 vfs_file_put(file);
823 async_answer_1(rid, EOK, off);
824 return;
825 }
826 break;
827 case SEEK_CUR:
828 if ((off >= 0) && (file->pos + off < file->pos)) {
829 vfs_file_put(file);
830 async_answer_0(rid, EOVERFLOW);
831 return;
832 }
833
834 if ((off < 0) && (file->pos < (aoff64_t) -off)) {
835 vfs_file_put(file);
836 async_answer_0(rid, EOVERFLOW);
837 return;
838 }
839
840 file->pos += off;
841 newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
842
843 vfs_file_put(file);
844 async_answer_2(rid, EOK, LOWER32(newoff),
845 UPPER32(newoff));
846 return;
847 case SEEK_END:
848 fibril_rwlock_read_lock(&file->node->contents_rwlock);
849 aoff64_t size = vfs_node_get_size(file->node);
850
851 if ((off >= 0) && (size + off < size)) {
852 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
853 vfs_file_put(file);
854 async_answer_0(rid, EOVERFLOW);
855 return;
856 }
857
858 if ((off < 0) && (size < (aoff64_t) -off)) {
859 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
860 vfs_file_put(file);
861 async_answer_0(rid, EOVERFLOW);
862 return;
863 }
864
865 file->pos = size + off;
866 newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
867
868 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
869 vfs_file_put(file);
870 async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
871 return;
872 }
873
874 vfs_file_put(file);
875 async_answer_0(rid, EINVAL);
876}
877
878int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
879 fs_index_t index, aoff64_t size)
880{
881 async_exch_t *exch = vfs_exchange_grab(fs_handle);
882 sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
883 (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
884 UPPER32(size));
885 vfs_exchange_release(exch);
886
887 return (int) rc;
888}
889
890void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
891{
892 int fd = IPC_GET_ARG1(*request);
893 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
894 IPC_GET_ARG3(*request));
895 int rc;
896
897 vfs_file_t *file = vfs_file_get(fd);
898 if (!file) {
899 async_answer_0(rid, ENOENT);
900 return;
901 }
902
903 fibril_rwlock_write_lock(&file->node->contents_rwlock);
904 rc = vfs_truncate_internal(file->node->fs_handle,
905 file->node->service_id, file->node->index, size);
906 if (rc == EOK)
907 file->node->size = size;
908 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
909
910 vfs_file_put(file);
911 async_answer_0(rid, (sysarg_t)rc);
912}
913
914void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
915{
916 int fd = IPC_GET_ARG1(*request);
917 sysarg_t rc;
918
919 vfs_file_t *file = vfs_file_get(fd);
920 if (!file) {
921 async_answer_0(rid, ENOENT);
922 return;
923 }
924 assert(file->node);
925
926 ipc_callid_t callid;
927 if (!async_data_read_receive(&callid, NULL)) {
928 vfs_file_put(file);
929 async_answer_0(callid, EINVAL);
930 async_answer_0(rid, EINVAL);
931 return;
932 }
933
934 async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
935 assert(exch);
936
937 aid_t msg;
938 msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
939 file->node->index, true, NULL);
940 assert(msg);
941 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
942
943 vfs_exchange_release(exch);
944
945 async_wait_for(msg, &rc);
946
947 vfs_file_put(file);
948 async_answer_0(rid, rc);
949}
950
951static void out_destroy(vfs_triplet_t *file)
952{
953 async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
954 async_msg_2(exch, VFS_OUT_DESTROY,
955 (sysarg_t) file->service_id, (sysarg_t) file->index);
956 vfs_exchange_release(exch);
957}
958
959void vfs_unlink2(ipc_callid_t rid, ipc_call_t *request)
960{
961 int rc;
962 char *path;
963 vfs_file_t *parent = NULL;
964 vfs_file_t *expect = NULL;
965 vfs_node_t *parent_node = root;
966
967 int parentfd = IPC_GET_ARG1(*request);
968 int expectfd = IPC_GET_ARG2(*request);
969 int wflag = IPC_GET_ARG3(*request);
970
971 rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
972 if (rc != EOK) {
973 async_answer_0(rid, rc);
974 return;
975 }
976
977 fibril_rwlock_write_lock(&namespace_rwlock);
978
979 int lflag = (wflag&WALK_DIRECTORY) ? L_DIRECTORY: 0;
980
981 /* Files are retrieved in order of file descriptors, to prevent deadlock. */
982 if (parentfd >= 0 && parentfd < expectfd) {
983 parent = vfs_file_get(parentfd);
984 if (!parent) {
985 rc = ENOENT;
986 goto exit;
987 }
988 }
989
990 if (expectfd >= 0) {
991 expect = vfs_file_get(expectfd);
992 if (!expect) {
993 rc = ENOENT;
994 goto exit;
995 }
996 }
997
998 if (parentfd >= 0 && parentfd >= expectfd) {
999 parent = vfs_file_get(parentfd);
1000 if (!parent) {
1001 rc = ENOENT;
1002 goto exit;
1003 }
1004 }
1005
1006 if (parent) {
1007 parent_node = parent->node;
1008 }
1009
1010 if (expectfd >= 0) {
1011 vfs_lookup_res_t lr;
1012 rc = vfs_lookup_internal(parent_node, path, lflag, &lr);
1013 if (rc != EOK) {
1014 goto exit;
1015 }
1016
1017 vfs_node_t *found_node = vfs_node_peek(&lr);
1018 if (expect->node != found_node) {
1019 rc = ENOENT;
1020 goto exit;
1021 }
1022
1023 vfs_file_put(expect);
1024 expect = NULL;
1025 }
1026
1027 vfs_lookup_res_t lr;
1028 rc = vfs_lookup_internal(parent_node, path, lflag | L_UNLINK, &lr);
1029 if (rc != EOK) {
1030 goto exit;
1031 }
1032
1033 /* If the node is not held by anyone, try to destroy it. */
1034 if (vfs_node_peek(&lr) == NULL) {
1035 out_destroy(&lr.triplet);
1036 }
1037
1038exit:
1039 if (path) {
1040 free(path);
1041 }
1042 if (parent) {
1043 vfs_file_put(parent);
1044 }
1045 if (expect) {
1046 vfs_file_put(expect);
1047 }
1048 fibril_rwlock_write_unlock(&namespace_rwlock);
1049 async_answer_0(rid, rc);
1050}
1051
1052static size_t shared_path(char *a, char *b)
1053{
1054 size_t res = 0;
1055
1056 while (a[res] == b[res] && a[res] != 0) {
1057 res++;
1058 }
1059
1060 if (a[res] == b[res]) {
1061 return res;
1062 }
1063
1064 res--;
1065 while (a[res] != '/') {
1066 res--;
1067 }
1068 return res;
1069}
1070
1071static int vfs_rename_internal(vfs_node_t *base, char *old, char *new)
1072{
1073 assert(base != NULL);
1074 assert(old != NULL);
1075 assert(new != NULL);
1076
1077 vfs_lookup_res_t base_lr;
1078 vfs_lookup_res_t old_lr;
1079 vfs_lookup_res_t new_lr_orig;
1080 bool orig_unlinked = false;
1081
1082 int rc;
1083
1084 size_t shared = shared_path(old, new);
1085
1086 /* Do not allow one path to be a prefix of the other. */
1087 if (old[shared] == 0 || new[shared] == 0) {
1088 return EINVAL;
1089 }
1090 assert(old[shared] == '/');
1091 assert(new[shared] == '/');
1092
1093 fibril_rwlock_write_lock(&namespace_rwlock);
1094
1095 /* Resolve the shared portion of the path first. */
1096 if (shared != 0) {
1097 old[shared] = 0;
1098 rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
1099 if (rc != EOK) {
1100 fibril_rwlock_write_unlock(&namespace_rwlock);
1101 return rc;
1102 }
1103
1104 base = vfs_node_get(&base_lr);
1105 old[shared] = '/';
1106 old += shared;
1107 new += shared;
1108 } else {
1109 vfs_node_addref(base);
1110 }
1111
1112
1113 rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS, &new_lr_orig);
1114 if (rc == EOK) {
1115 orig_unlinked = true;
1116 } else if (rc != ENOENT) {
1117 vfs_node_put(base);
1118 fibril_rwlock_write_unlock(&namespace_rwlock);
1119 return rc;
1120 }
1121
1122 rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS, &old_lr);
1123 if (rc != EOK) {
1124 if (orig_unlinked) {
1125 vfs_link_internal(base, new, &new_lr_orig.triplet);
1126 }
1127 vfs_node_put(base);
1128 fibril_rwlock_write_unlock(&namespace_rwlock);
1129 return rc;
1130 }
1131
1132 rc = vfs_link_internal(base, new, &old_lr.triplet);
1133 if (rc != EOK) {
1134 vfs_link_internal(base, old, &old_lr.triplet);
1135 if (orig_unlinked) {
1136 vfs_link_internal(base, new, &new_lr_orig.triplet);
1137 }
1138 vfs_node_put(base);
1139 fibril_rwlock_write_unlock(&namespace_rwlock);
1140 return rc;
1141 }
1142
1143 /* If the node is not held by anyone, try to destroy it. */
1144 if (orig_unlinked && vfs_node_peek(&new_lr_orig) == NULL) {
1145 out_destroy(&new_lr_orig.triplet);
1146 }
1147
1148 vfs_node_put(base);
1149 fibril_rwlock_write_unlock(&namespace_rwlock);
1150 return EOK;
1151}
1152
1153void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1154{
1155 /* The common base directory. */
1156 int basefd;
1157 char *old = NULL;
1158 char *new = NULL;
1159 vfs_file_t *base = NULL;
1160 int rc;
1161
1162 basefd = IPC_GET_ARG1(*request);
1163
1164 /* Retrieve the old path. */
1165 rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
1166 if (rc != EOK) {
1167 goto out;
1168 }
1169
1170 /* Retrieve the new path. */
1171 rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
1172 if (rc != EOK) {
1173 goto out;
1174 }
1175
1176 size_t olen;
1177 size_t nlen;
1178 char *oldc = canonify(old, &olen);
1179 char *newc = canonify(new, &nlen);
1180
1181 if ((!oldc) || (!newc)) {
1182 rc = EINVAL;
1183 goto out;
1184 }
1185
1186 assert(oldc[olen] == '\0');
1187 assert(newc[nlen] == '\0');
1188
1189 /* Lookup the file structure corresponding to the file descriptor. */
1190 vfs_node_t *base_node = root;
1191 // TODO: Client-side root.
1192 if (basefd != -1) {
1193 base = vfs_file_get(basefd);
1194 if (!base) {
1195 rc = EBADF;
1196 goto out;
1197 }
1198 base_node = base->node;
1199 }
1200
1201 rc = vfs_rename_internal(base_node, oldc, newc);
1202
1203out:
1204 async_answer_0(rid, rc);
1205
1206 if (old) {
1207 free(old);
1208 }
1209 if (new) {
1210 free(new);
1211 }
1212 if (base) {
1213 vfs_file_put(base);
1214 }
1215}
1216
1217void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
1218{
1219 int oldfd = IPC_GET_ARG1(*request);
1220 int newfd = IPC_GET_ARG2(*request);
1221
1222 /* If the file descriptors are the same, do nothing. */
1223 if (oldfd == newfd) {
1224 async_answer_1(rid, EOK, newfd);
1225 return;
1226 }
1227
1228 /* Lookup the file structure corresponding to oldfd. */
1229 vfs_file_t *oldfile = vfs_file_get(oldfd);
1230 if (!oldfile) {
1231 async_answer_0(rid, EBADF);
1232 return;
1233 }
1234
1235 /* Make sure newfd is closed. */
1236 (void) vfs_fd_free(newfd);
1237
1238 /* Assign the old file to newfd. */
1239 int ret = vfs_fd_assign(oldfile, newfd);
1240 vfs_file_put(oldfile);
1241
1242 if (ret != EOK)
1243 async_answer_0(rid, ret);
1244 else
1245 async_answer_1(rid, EOK, newfd);
1246}
1247
1248void vfs_wait_handle(ipc_callid_t rid, ipc_call_t *request)
1249{
1250 int fd = vfs_wait_handle_internal();
1251 async_answer_1(rid, EOK, fd);
1252}
1253
1254void vfs_get_mtab(ipc_callid_t rid, ipc_call_t *request)
1255{
1256 ipc_callid_t callid;
1257 ipc_call_t data;
1258 sysarg_t rc = EOK;
1259 size_t len;
1260
1261 fibril_mutex_lock(&mtab_list_lock);
1262
1263 /* Send to the caller the number of mounted filesystems */
1264 callid = async_get_call(&data);
1265 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
1266 rc = ENOTSUP;
1267 async_answer_0(callid, rc);
1268 goto exit;
1269 }
1270 async_answer_1(callid, EOK, mtab_size);
1271
1272 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
1273 rc = ENOTSUP;
1274
1275 if (!async_data_read_receive(&callid, &len)) {
1276 async_answer_0(callid, rc);
1277 goto exit;
1278 }
1279
1280 (void) async_data_read_finalize(callid, mtab_ent->mp,
1281 str_size(mtab_ent->mp));
1282
1283 if (!async_data_read_receive(&callid, &len)) {
1284 async_answer_0(callid, rc);
1285 goto exit;
1286 }
1287
1288 (void) async_data_read_finalize(callid, mtab_ent->opts,
1289 str_size(mtab_ent->opts));
1290
1291 if (!async_data_read_receive(&callid, &len)) {
1292 async_answer_0(callid, rc);
1293 goto exit;
1294 }
1295
1296 (void) async_data_read_finalize(callid, mtab_ent->fs_name,
1297 str_size(mtab_ent->fs_name));
1298
1299 callid = async_get_call(&data);
1300
1301 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
1302 async_answer_0(callid, rc);
1303 goto exit;
1304 }
1305
1306 rc = EOK;
1307 async_answer_2(callid, rc, mtab_ent->instance,
1308 mtab_ent->service_id);
1309 }
1310
1311exit:
1312 fibril_mutex_unlock(&mtab_list_lock);
1313 async_answer_0(rid, rc);
1314}
1315
1316void vfs_statfs(ipc_callid_t rid, ipc_call_t *request)
1317{
1318 char *path;
1319 int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
1320 if (rc != EOK) {
1321 async_answer_0(rid, rc);
1322 return;
1323 }
1324
1325 ipc_callid_t callid;
1326 if (!async_data_read_receive(&callid, NULL)) {
1327 free(path);
1328 async_answer_0(callid, EINVAL);
1329 async_answer_0(rid, EINVAL);
1330 return;
1331 }
1332
1333 vfs_lookup_res_t lr;
1334 fibril_rwlock_read_lock(&namespace_rwlock);
1335 rc = vfs_lookup_internal(root, path, L_NONE, &lr);
1336 free(path);
1337 if (rc != EOK) {
1338 fibril_rwlock_read_unlock(&namespace_rwlock);
1339 async_answer_0(callid, rc);
1340 async_answer_0(rid, rc);
1341 return;
1342 }
1343 vfs_node_t *node = vfs_node_get(&lr);
1344 if (!node) {
1345 fibril_rwlock_read_unlock(&namespace_rwlock);
1346 async_answer_0(callid, ENOMEM);
1347 async_answer_0(rid, ENOMEM);
1348 return;
1349 }
1350
1351 fibril_rwlock_read_unlock(&namespace_rwlock);
1352
1353 async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
1354
1355 aid_t msg;
1356 msg = async_send_3(exch, VFS_OUT_STATFS, node->service_id,
1357 node->index, false, NULL);
1358 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1359
1360 vfs_exchange_release(exch);
1361
1362 sysarg_t rv;
1363 async_wait_for(msg, &rv);
1364
1365 async_answer_0(rid, rv);
1366
1367 vfs_node_put(node);
1368}
1369
1370void vfs_op_clone(ipc_callid_t rid, ipc_call_t *request)
1371{
1372 int oldfd = IPC_GET_ARG1(*request);
1373 bool desc = IPC_GET_ARG2(*request);
1374
1375 /* Lookup the file structure corresponding to fd. */
1376 vfs_file_t *oldfile = vfs_file_get(oldfd);
1377 if (!oldfile) {
1378 async_answer_0(rid, EBADF);
1379 return;
1380 }
1381
1382 vfs_file_t *newfile;
1383 int newfd = vfs_fd_alloc(&newfile, desc);
1384 if (newfd < 0) {
1385 async_answer_0(rid, newfd);
1386 vfs_file_put(oldfile);
1387 return;
1388 }
1389
1390 assert(oldfile->node != NULL);
1391
1392 newfile->node = oldfile->node;
1393 newfile->permissions = oldfile->permissions;
1394 vfs_node_addref(newfile->node);
1395
1396 vfs_file_put(oldfile);
1397 vfs_file_put(newfile);
1398
1399 async_answer_0(rid, newfd);
1400}
1401
1402/**
1403 * @}
1404 */
Note: See TracBrowser for help on using the repository browser.