source: mainline/uspace/srv/vfs/vfs_ops.c@ 818fffe

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

Fix deadlock caused by a too early released exchange.

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