source: mainline/uspace/srv/vfs/vfs_ops.c@ 10e4cd7

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

Initial implementation of vfs mtab_read support

  • Property mode set to 100644
File size: 33.1 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 /* Do the mount */
361 vfs_mount_internal(rid, service_id, fs_handle, mp, opts);
362
363 /* Add the filesystem info to the list of mounted filesystems */
364 mtab_list_ent_t *mtab_list_ent = malloc(sizeof(mtab_list_ent_t));
365 if (!mtab_list_ent) {
366 async_answer_0(callid, ENOMEM);
367 async_answer_0(rid, ENOMEM);
368 free(mp);
369 free(fs_name);
370 free(opts);
371 return;
372 }
373
374 mtab_ent_t *mtab_ent = &mtab_list_ent->mtab_ent;
375
376 mtab_ent->fs_handle = fs_handle;
377 str_cpy(mtab_ent->mp, MAX_PATH_LEN, mp);
378 str_cpy(mtab_ent->fs_name, FS_NAME_MAXLEN, fs_name);
379 str_cpy(mtab_ent->opts, MAX_MNTOPTS_LEN, opts);
380 mtab_ent->flags = flags;
381 mtab_ent->instance = instance;
382
383 link_initialize(&mtab_list_ent->link);
384
385 fibril_mutex_lock(&mtab_list_lock);
386 list_append(&mtab_list_ent->link, &mtab_list);
387 mtab_size++;
388 fibril_mutex_unlock(&mtab_list_lock);
389
390 free(mp);
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 free(mp);
467
468 exch = vfs_exchange_grab(mr_node->fs_handle);
469 rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
470 mr_node->service_id);
471 vfs_exchange_release(exch);
472
473 if (rc != EOK) {
474 fibril_rwlock_write_unlock(&namespace_rwlock);
475 vfs_node_put(mr_node);
476 async_answer_0(rid, rc);
477 return;
478 }
479
480 rootfs.fs_handle = 0;
481 rootfs.service_id = 0;
482 } else {
483
484 /*
485 * Unmounting a non-root file system.
486 *
487 * We have a regular mount point node representing the parent
488 * file system, so we delegate the operation to it.
489 */
490
491 rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
492 free(mp);
493 if (rc != EOK) {
494 fibril_rwlock_write_unlock(&namespace_rwlock);
495 vfs_node_put(mr_node);
496 async_answer_0(rid, rc);
497 return;
498 }
499
500 vfs_node_t *mp_node = vfs_node_get(&mp_res);
501 if (!mp_node) {
502 fibril_rwlock_write_unlock(&namespace_rwlock);
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 vfs_node_put(mp_node);
516 vfs_node_put(mr_node);
517 async_answer_0(rid, rc);
518 return;
519 }
520
521 /* Drop the reference we got above. */
522 vfs_node_put(mp_node);
523 /* Drop the reference from when the file system was mounted. */
524 vfs_node_put(mp_node);
525 }
526
527 /*
528 * All went well, the mounted file system was successfully unmounted.
529 * The only thing left is to forget the unmounted root VFS node.
530 */
531 vfs_node_forget(mr_node);
532
533 fibril_rwlock_write_unlock(&namespace_rwlock);
534 async_answer_0(rid, EOK);
535}
536
537void vfs_open(ipc_callid_t rid, ipc_call_t *request)
538{
539 /*
540 * The POSIX interface is open(path, oflag, mode).
541 * We can receive oflags and mode along with the VFS_IN_OPEN call;
542 * the path will need to arrive in another call.
543 *
544 * We also receive one private, non-POSIX set of flags called lflag
545 * used to pass information to vfs_lookup_internal().
546 */
547 int lflag = IPC_GET_ARG1(*request);
548 int oflag = IPC_GET_ARG2(*request);
549 int mode = IPC_GET_ARG3(*request);
550
551 /* Ignore mode for now. */
552 (void) mode;
553
554 /*
555 * Make sure that we are called with exactly one of L_FILE and
556 * L_DIRECTORY. Make sure that the user does not pass L_OPEN,
557 * L_ROOT or L_MP.
558 */
559 if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
560 ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
561 (lflag & (L_OPEN | L_ROOT | L_MP))) {
562 async_answer_0(rid, EINVAL);
563 return;
564 }
565
566 if (oflag & O_CREAT)
567 lflag |= L_CREATE;
568 if (oflag & O_EXCL)
569 lflag |= L_EXCLUSIVE;
570
571 char *path;
572 int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
573 if (rc != EOK) {
574 async_answer_0(rid, rc);
575 return;
576 }
577
578 /*
579 * Avoid the race condition in which the file can be deleted before we
580 * find/create-and-lock the VFS node corresponding to the looked-up
581 * triplet.
582 */
583 if (lflag & L_CREATE)
584 fibril_rwlock_write_lock(&namespace_rwlock);
585 else
586 fibril_rwlock_read_lock(&namespace_rwlock);
587
588 /* The path is now populated and we can call vfs_lookup_internal(). */
589 vfs_lookup_res_t lr;
590 rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
591 if (rc != EOK) {
592 if (lflag & L_CREATE)
593 fibril_rwlock_write_unlock(&namespace_rwlock);
594 else
595 fibril_rwlock_read_unlock(&namespace_rwlock);
596 async_answer_0(rid, rc);
597 free(path);
598 return;
599 }
600
601 /* Path is no longer needed. */
602 free(path);
603
604 vfs_node_t *node = vfs_node_get(&lr);
605 if (lflag & L_CREATE)
606 fibril_rwlock_write_unlock(&namespace_rwlock);
607 else
608 fibril_rwlock_read_unlock(&namespace_rwlock);
609
610 /* Truncate the file if requested and if necessary. */
611 if (oflag & O_TRUNC) {
612 fibril_rwlock_write_lock(&node->contents_rwlock);
613 if (node->size) {
614 rc = vfs_truncate_internal(node->fs_handle,
615 node->service_id, node->index, 0);
616 if (rc) {
617 fibril_rwlock_write_unlock(&node->contents_rwlock);
618 vfs_node_put(node);
619 async_answer_0(rid, rc);
620 return;
621 }
622 node->size = 0;
623 }
624 fibril_rwlock_write_unlock(&node->contents_rwlock);
625 }
626
627 /*
628 * Get ourselves a file descriptor and the corresponding vfs_file_t
629 * structure.
630 */
631 int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
632 if (fd < 0) {
633 vfs_node_put(node);
634 async_answer_0(rid, fd);
635 return;
636 }
637 vfs_file_t *file = vfs_file_get(fd);
638 assert(file);
639 file->node = node;
640 if (oflag & O_APPEND)
641 file->append = true;
642
643 /*
644 * The following increase in reference count is for the fact that the
645 * file is being opened and that a file structure is pointing to it.
646 * It is necessary so that the file will not disappear when
647 * vfs_node_put() is called. The reference will be dropped by the
648 * respective VFS_IN_CLOSE.
649 */
650 vfs_node_addref(node);
651 vfs_node_put(node);
652 vfs_file_put(file);
653
654 /* Success! Return the new file descriptor to the client. */
655 async_answer_1(rid, EOK, fd);
656}
657
658void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
659{
660 int fd = IPC_GET_ARG1(*request);
661
662 /* Lookup the file structure corresponding to the file descriptor. */
663 vfs_file_t *file = vfs_file_get(fd);
664 if (!file) {
665 async_answer_0(rid, ENOENT);
666 return;
667 }
668
669 /*
670 * Lock the open file structure so that no other thread can manipulate
671 * the same open file at a time.
672 */
673 fibril_mutex_lock(&file->lock);
674 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
675
676 /* Make a VFS_OUT_SYMC request at the destination FS server. */
677 aid_t msg;
678 ipc_call_t answer;
679 msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
680 file->node->index, &answer);
681
682 vfs_exchange_release(fs_exch);
683
684 /* Wait for reply from the FS server. */
685 sysarg_t rc;
686 async_wait_for(msg, &rc);
687
688 fibril_mutex_unlock(&file->lock);
689
690 vfs_file_put(file);
691 async_answer_0(rid, rc);
692}
693
694void vfs_close(ipc_callid_t rid, ipc_call_t *request)
695{
696 int fd = IPC_GET_ARG1(*request);
697 int ret = vfs_fd_free(fd);
698 async_answer_0(rid, ret);
699}
700
701static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
702{
703 /*
704 * The following code strongly depends on the fact that the files data
705 * structure can be only accessed by a single fibril and all file
706 * operations are serialized (i.e. the reads and writes cannot
707 * interleave and a file cannot be closed while it is being read).
708 *
709 * Additional synchronization needs to be added once the table of
710 * open files supports parallel access!
711 */
712
713 int fd = IPC_GET_ARG1(*request);
714
715 /* Lookup the file structure corresponding to the file descriptor. */
716 vfs_file_t *file = vfs_file_get(fd);
717 if (!file) {
718 async_answer_0(rid, ENOENT);
719 return;
720 }
721
722 /*
723 * Lock the open file structure so that no other thread can manipulate
724 * the same open file at a time.
725 */
726 fibril_mutex_lock(&file->lock);
727
728 vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
729 assert(fs_info);
730
731 /*
732 * Lock the file's node so that no other client can read/write to it at
733 * the same time unless the FS supports concurrent reads/writes and its
734 * write implementation does not modify the file size.
735 */
736 if ((read) ||
737 ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
738 fibril_rwlock_read_lock(&file->node->contents_rwlock);
739 else
740 fibril_rwlock_write_lock(&file->node->contents_rwlock);
741
742 if (file->node->type == VFS_NODE_DIRECTORY) {
743 /*
744 * Make sure that no one is modifying the namespace
745 * while we are in readdir().
746 */
747 assert(read);
748 fibril_rwlock_read_lock(&namespace_rwlock);
749 }
750
751 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
752
753 /*
754 * Make a VFS_READ/VFS_WRITE request at the destination FS server
755 * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
756 * destination FS server. The call will be routed as if sent by
757 * ourselves. Note that call arguments are immutable in this case so we
758 * don't have to bother.
759 */
760 sysarg_t rc;
761 ipc_call_t answer;
762 if (read) {
763 rc = async_data_read_forward_4_1(fs_exch, VFS_OUT_READ,
764 file->node->service_id, file->node->index,
765 LOWER32(file->pos), UPPER32(file->pos), &answer);
766 } else {
767 if (file->append)
768 file->pos = file->node->size;
769
770 rc = async_data_write_forward_4_1(fs_exch, VFS_OUT_WRITE,
771 file->node->service_id, file->node->index,
772 LOWER32(file->pos), UPPER32(file->pos), &answer);
773 }
774
775 vfs_exchange_release(fs_exch);
776
777 size_t bytes = IPC_GET_ARG1(answer);
778
779 if (file->node->type == VFS_NODE_DIRECTORY)
780 fibril_rwlock_read_unlock(&namespace_rwlock);
781
782 /* Unlock the VFS node. */
783 if ((read) ||
784 ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
785 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
786 else {
787 /* Update the cached version of node's size. */
788 if (rc == EOK)
789 file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
790 IPC_GET_ARG3(answer));
791 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
792 }
793
794 /* Update the position pointer and unlock the open file. */
795 if (rc == EOK)
796 file->pos += bytes;
797 fibril_mutex_unlock(&file->lock);
798 vfs_file_put(file);
799
800 /*
801 * FS server's reply is the final result of the whole operation we
802 * return to the client.
803 */
804 async_answer_1(rid, rc, bytes);
805}
806
807void vfs_read(ipc_callid_t rid, ipc_call_t *request)
808{
809 vfs_rdwr(rid, request, true);
810}
811
812void vfs_write(ipc_callid_t rid, ipc_call_t *request)
813{
814 vfs_rdwr(rid, request, false);
815}
816
817void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
818{
819 int fd = (int) IPC_GET_ARG1(*request);
820 off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
821 IPC_GET_ARG3(*request));
822 int whence = (int) IPC_GET_ARG4(*request);
823
824 /* Lookup the file structure corresponding to the file descriptor. */
825 vfs_file_t *file = vfs_file_get(fd);
826 if (!file) {
827 async_answer_0(rid, ENOENT);
828 return;
829 }
830
831 fibril_mutex_lock(&file->lock);
832
833 off64_t newoff;
834 switch (whence) {
835 case SEEK_SET:
836 if (off >= 0) {
837 file->pos = (aoff64_t) off;
838 fibril_mutex_unlock(&file->lock);
839 vfs_file_put(file);
840 async_answer_1(rid, EOK, off);
841 return;
842 }
843 break;
844 case SEEK_CUR:
845 if ((off >= 0) && (file->pos + off < file->pos)) {
846 fibril_mutex_unlock(&file->lock);
847 vfs_file_put(file);
848 async_answer_0(rid, EOVERFLOW);
849 return;
850 }
851
852 if ((off < 0) && (file->pos < (aoff64_t) -off)) {
853 fibril_mutex_unlock(&file->lock);
854 vfs_file_put(file);
855 async_answer_0(rid, EOVERFLOW);
856 return;
857 }
858
859 file->pos += off;
860 newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
861
862 fibril_mutex_unlock(&file->lock);
863 vfs_file_put(file);
864 async_answer_2(rid, EOK, LOWER32(newoff),
865 UPPER32(newoff));
866 return;
867 case SEEK_END:
868 fibril_rwlock_read_lock(&file->node->contents_rwlock);
869 aoff64_t size = file->node->size;
870
871 if ((off >= 0) && (size + off < size)) {
872 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
873 fibril_mutex_unlock(&file->lock);
874 vfs_file_put(file);
875 async_answer_0(rid, EOVERFLOW);
876 return;
877 }
878
879 if ((off < 0) && (size < (aoff64_t) -off)) {
880 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
881 fibril_mutex_unlock(&file->lock);
882 vfs_file_put(file);
883 async_answer_0(rid, EOVERFLOW);
884 return;
885 }
886
887 file->pos = size + off;
888 newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
889
890 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
891 fibril_mutex_unlock(&file->lock);
892 vfs_file_put(file);
893 async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
894 return;
895 }
896
897 fibril_mutex_unlock(&file->lock);
898 vfs_file_put(file);
899 async_answer_0(rid, EINVAL);
900}
901
902int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
903 fs_index_t index, aoff64_t size)
904{
905 async_exch_t *exch = vfs_exchange_grab(fs_handle);
906 sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
907 (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
908 UPPER32(size));
909 vfs_exchange_release(exch);
910
911 return (int) rc;
912}
913
914void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
915{
916 int fd = IPC_GET_ARG1(*request);
917 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
918 IPC_GET_ARG3(*request));
919 int rc;
920
921 vfs_file_t *file = vfs_file_get(fd);
922 if (!file) {
923 async_answer_0(rid, ENOENT);
924 return;
925 }
926 fibril_mutex_lock(&file->lock);
927
928 fibril_rwlock_write_lock(&file->node->contents_rwlock);
929 rc = vfs_truncate_internal(file->node->fs_handle,
930 file->node->service_id, file->node->index, size);
931 if (rc == EOK)
932 file->node->size = size;
933 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
934
935 fibril_mutex_unlock(&file->lock);
936 vfs_file_put(file);
937 async_answer_0(rid, (sysarg_t)rc);
938}
939
940void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
941{
942 int fd = IPC_GET_ARG1(*request);
943 sysarg_t rc;
944
945 vfs_file_t *file = vfs_file_get(fd);
946 if (!file) {
947 async_answer_0(rid, ENOENT);
948 return;
949 }
950
951 ipc_callid_t callid;
952 if (!async_data_read_receive(&callid, NULL)) {
953 vfs_file_put(file);
954 async_answer_0(callid, EINVAL);
955 async_answer_0(rid, EINVAL);
956 return;
957 }
958
959 fibril_mutex_lock(&file->lock);
960
961 async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
962
963 aid_t msg;
964 msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
965 file->node->index, true, NULL);
966 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
967
968 vfs_exchange_release(exch);
969
970 async_wait_for(msg, &rc);
971
972 fibril_mutex_unlock(&file->lock);
973 vfs_file_put(file);
974 async_answer_0(rid, rc);
975}
976
977void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
978{
979 char *path;
980 int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
981 if (rc != EOK) {
982 async_answer_0(rid, rc);
983 return;
984 }
985
986 ipc_callid_t callid;
987 if (!async_data_read_receive(&callid, NULL)) {
988 free(path);
989 async_answer_0(callid, EINVAL);
990 async_answer_0(rid, EINVAL);
991 return;
992 }
993
994 vfs_lookup_res_t lr;
995 fibril_rwlock_read_lock(&namespace_rwlock);
996 rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
997 free(path);
998 if (rc != EOK) {
999 fibril_rwlock_read_unlock(&namespace_rwlock);
1000 async_answer_0(callid, rc);
1001 async_answer_0(rid, rc);
1002 return;
1003 }
1004 vfs_node_t *node = vfs_node_get(&lr);
1005 if (!node) {
1006 fibril_rwlock_read_unlock(&namespace_rwlock);
1007 async_answer_0(callid, ENOMEM);
1008 async_answer_0(rid, ENOMEM);
1009 return;
1010 }
1011
1012 fibril_rwlock_read_unlock(&namespace_rwlock);
1013
1014 async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
1015
1016 aid_t msg;
1017 msg = async_send_3(exch, VFS_OUT_STAT, node->service_id,
1018 node->index, false, NULL);
1019 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1020
1021 vfs_exchange_release(exch);
1022
1023 sysarg_t rv;
1024 async_wait_for(msg, &rv);
1025
1026 async_answer_0(rid, rv);
1027
1028 vfs_node_put(node);
1029}
1030
1031void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
1032{
1033 int mode = IPC_GET_ARG1(*request);
1034
1035 char *path;
1036 int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
1037 if (rc != EOK) {
1038 async_answer_0(rid, rc);
1039 return;
1040 }
1041
1042 /* Ignore mode for now. */
1043 (void) mode;
1044
1045 fibril_rwlock_write_lock(&namespace_rwlock);
1046 int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
1047 rc = vfs_lookup_internal(path, lflag, NULL, NULL);
1048 fibril_rwlock_write_unlock(&namespace_rwlock);
1049 free(path);
1050 async_answer_0(rid, rc);
1051}
1052
1053void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1054{
1055 int lflag = 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 fibril_rwlock_write_lock(&namespace_rwlock);
1065 lflag &= L_DIRECTORY; /* sanitize lflag */
1066 vfs_lookup_res_t lr;
1067 rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
1068 free(path);
1069 if (rc != EOK) {
1070 fibril_rwlock_write_unlock(&namespace_rwlock);
1071 async_answer_0(rid, rc);
1072 return;
1073 }
1074
1075 /*
1076 * The name has already been unlinked by vfs_lookup_internal().
1077 * We have to get and put the VFS node to ensure that it is
1078 * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
1079 */
1080 vfs_node_t *node = vfs_node_get(&lr);
1081 fibril_mutex_lock(&nodes_mutex);
1082 node->lnkcnt--;
1083 fibril_mutex_unlock(&nodes_mutex);
1084 fibril_rwlock_write_unlock(&namespace_rwlock);
1085 vfs_node_put(node);
1086 async_answer_0(rid, EOK);
1087}
1088
1089void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1090{
1091 /* Retrieve the old path. */
1092 char *old;
1093 int rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
1094 if (rc != EOK) {
1095 async_answer_0(rid, rc);
1096 return;
1097 }
1098
1099 /* Retrieve the new path. */
1100 char *new;
1101 rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
1102 if (rc != EOK) {
1103 free(old);
1104 async_answer_0(rid, rc);
1105 return;
1106 }
1107
1108 size_t olen;
1109 size_t nlen;
1110 char *oldc = canonify(old, &olen);
1111 char *newc = canonify(new, &nlen);
1112
1113 if ((!oldc) || (!newc)) {
1114 async_answer_0(rid, EINVAL);
1115 free(old);
1116 free(new);
1117 return;
1118 }
1119
1120 oldc[olen] = '\0';
1121 newc[nlen] = '\0';
1122
1123 if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
1124 ((newc[str_length(oldc)] == '/') ||
1125 (str_length(oldc) == 1) ||
1126 (str_length(oldc) == str_length(newc)))) {
1127 /*
1128 * oldc is a prefix of newc and either
1129 * - newc continues with a / where oldc ends, or
1130 * - oldc was / itself, or
1131 * - oldc and newc are equal.
1132 */
1133 async_answer_0(rid, EINVAL);
1134 free(old);
1135 free(new);
1136 return;
1137 }
1138
1139 vfs_lookup_res_t old_lr;
1140 vfs_lookup_res_t new_lr;
1141 vfs_lookup_res_t new_par_lr;
1142 fibril_rwlock_write_lock(&namespace_rwlock);
1143
1144 /* Lookup the node belonging to the old file name. */
1145 rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1146 if (rc != EOK) {
1147 fibril_rwlock_write_unlock(&namespace_rwlock);
1148 async_answer_0(rid, rc);
1149 free(old);
1150 free(new);
1151 return;
1152 }
1153
1154 vfs_node_t *old_node = vfs_node_get(&old_lr);
1155 if (!old_node) {
1156 fibril_rwlock_write_unlock(&namespace_rwlock);
1157 async_answer_0(rid, ENOMEM);
1158 free(old);
1159 free(new);
1160 return;
1161 }
1162
1163 /* Determine the path to the parent of the node with the new name. */
1164 char *parentc = str_dup(newc);
1165 if (!parentc) {
1166 fibril_rwlock_write_unlock(&namespace_rwlock);
1167 vfs_node_put(old_node);
1168 async_answer_0(rid, rc);
1169 free(old);
1170 free(new);
1171 return;
1172 }
1173
1174 char *lastsl = str_rchr(parentc + 1, '/');
1175 if (lastsl)
1176 *lastsl = '\0';
1177 else
1178 parentc[1] = '\0';
1179
1180 /* Lookup parent of the new file name. */
1181 rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
1182 free(parentc); /* not needed anymore */
1183 if (rc != EOK) {
1184 fibril_rwlock_write_unlock(&namespace_rwlock);
1185 vfs_node_put(old_node);
1186 async_answer_0(rid, rc);
1187 free(old);
1188 free(new);
1189 return;
1190 }
1191
1192 /* Check whether linking to the same file system instance. */
1193 if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1194 (old_node->service_id != new_par_lr.triplet.service_id)) {
1195 fibril_rwlock_write_unlock(&namespace_rwlock);
1196 vfs_node_put(old_node);
1197 async_answer_0(rid, EXDEV); /* different file systems */
1198 free(old);
1199 free(new);
1200 return;
1201 }
1202
1203 /* Destroy the old link for the new name. */
1204 vfs_node_t *new_node = NULL;
1205 rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1206
1207 switch (rc) {
1208 case ENOENT:
1209 /* simply not in our way */
1210 break;
1211 case EOK:
1212 new_node = vfs_node_get(&new_lr);
1213 if (!new_node) {
1214 fibril_rwlock_write_unlock(&namespace_rwlock);
1215 vfs_node_put(old_node);
1216 async_answer_0(rid, ENOMEM);
1217 free(old);
1218 free(new);
1219 return;
1220 }
1221 fibril_mutex_lock(&nodes_mutex);
1222 new_node->lnkcnt--;
1223 fibril_mutex_unlock(&nodes_mutex);
1224 break;
1225 default:
1226 fibril_rwlock_write_unlock(&namespace_rwlock);
1227 vfs_node_put(old_node);
1228 async_answer_0(rid, ENOTEMPTY);
1229 free(old);
1230 free(new);
1231 return;
1232 }
1233
1234 /* Create the new link for the new name. */
1235 rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1236 if (rc != EOK) {
1237 fibril_rwlock_write_unlock(&namespace_rwlock);
1238 vfs_node_put(old_node);
1239 if (new_node)
1240 vfs_node_put(new_node);
1241 async_answer_0(rid, rc);
1242 free(old);
1243 free(new);
1244 return;
1245 }
1246
1247 fibril_mutex_lock(&nodes_mutex);
1248 old_node->lnkcnt++;
1249 fibril_mutex_unlock(&nodes_mutex);
1250
1251 /* Destroy the link for the old name. */
1252 rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1253 if (rc != EOK) {
1254 fibril_rwlock_write_unlock(&namespace_rwlock);
1255 vfs_node_put(old_node);
1256 if (new_node)
1257 vfs_node_put(new_node);
1258 async_answer_0(rid, rc);
1259 free(old);
1260 free(new);
1261 return;
1262 }
1263
1264 fibril_mutex_lock(&nodes_mutex);
1265 old_node->lnkcnt--;
1266 fibril_mutex_unlock(&nodes_mutex);
1267 fibril_rwlock_write_unlock(&namespace_rwlock);
1268 vfs_node_put(old_node);
1269
1270 if (new_node)
1271 vfs_node_put(new_node);
1272
1273 free(old);
1274 free(new);
1275 async_answer_0(rid, EOK);
1276}
1277
1278void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
1279{
1280 int oldfd = IPC_GET_ARG1(*request);
1281 int newfd = IPC_GET_ARG2(*request);
1282
1283 /* If the file descriptors are the same, do nothing. */
1284 if (oldfd == newfd) {
1285 async_answer_1(rid, EOK, newfd);
1286 return;
1287 }
1288
1289 /* Lookup the file structure corresponding to oldfd. */
1290 vfs_file_t *oldfile = vfs_file_get(oldfd);
1291 if (!oldfile) {
1292 async_answer_0(rid, EBADF);
1293 return;
1294 }
1295
1296 /*
1297 * Lock the open file structure so that no other thread can manipulate
1298 * the same open file at a time.
1299 */
1300 fibril_mutex_lock(&oldfile->lock);
1301
1302 /* Make sure newfd is closed. */
1303 (void) vfs_fd_free(newfd);
1304
1305 /* Assign the old file to newfd. */
1306 int ret = vfs_fd_assign(oldfile, newfd);
1307 fibril_mutex_unlock(&oldfile->lock);
1308 vfs_file_put(oldfile);
1309
1310 if (ret != EOK)
1311 async_answer_0(rid, ret);
1312 else
1313 async_answer_1(rid, EOK, newfd);
1314}
1315
1316void vfs_wait_handle(ipc_callid_t rid, ipc_call_t *request)
1317{
1318 int fd = vfs_wait_handle_internal();
1319 async_answer_1(rid, EOK, fd);
1320}
1321
1322/**
1323 * @}
1324 */
Note: See TracBrowser for help on using the repository browser.