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

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

Make sure to send VFS_OUT_CLOSE upon dropping the last file reference.

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