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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7c62a9 was b7c62a9, checked in by Jiri Zarevucky <zarevucky.jiri@…>, 12 years ago

Make the server oblivious to the link count. It is just another source of potential problems. Also clean up some confusion with file types and node refcount.

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