source: mainline/uspace/srv/vfs/vfs_ops.c@ 472c09d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 472c09d was 472c09d, checked in by Martin Decky <martin@…>, 15 years ago

more consistent naming scheme:

async_data_blob_receive → async_data_receive
async_data_string_receive → async_string_receive
CLIPBOARD_TAG_BLOB → CLIPBOARD_TAG_DATA

async_data_receive can now check the granularity of the received data
async_string_receive can now return the raw size of the received data

replace several common patterns of async_data_write_receive/_finalize
with a single async_data_receive/_string_receive (this greatly improves
code readability)

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