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

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

Finish implementation of VFS_IN_UNMOUNT in vfs.

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