source: mainline/uspace/srv/vfs/vfs_ops.c@ 64aed80

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

Clean up the table of open files after the client closes its VFS connection.

  • Property mode set to 100644
File size: 36.8 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
[ea44bd1]94 rc = vfs_lookup_internal(mp, L_MP, &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 */
[f7376cbf]461 rc = vfs_lookup_internal(mp, L_ROOT, &mr_res, NULL);
[ae75e2e3]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
[f7376cbf]523 rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
[ae75e2e3]524 free(mp);
525 if (rc != EOK) {
526 fibril_rwlock_write_unlock(&namespace_rwlock);
527 vfs_node_put(mr_node);
528 ipc_answer_0(rid, rc);
529 return;
530 }
531 vfs_node_t *mp_node = vfs_node_get(&mp_res);
532 if (!mp_node) {
533 fibril_rwlock_write_unlock(&namespace_rwlock);
534 vfs_node_put(mr_node);
535 ipc_answer_0(rid, ENOMEM);
536 return;
537 }
538
539 phone = vfs_grab_phone(mp_node->fs_handle);
[6a4e972]540 rc = async_req_2_0(phone, VFS_OUT_UNMOUNT, mp_node->dev_handle,
541 mp_node->index);
[ae75e2e3]542 vfs_release_phone(phone);
543 if (rc != EOK) {
544 fibril_rwlock_write_unlock(&namespace_rwlock);
545 vfs_node_put(mp_node);
546 vfs_node_put(mr_node);
547 ipc_answer_0(rid, rc);
548 return;
549 }
550
551 /* Drop the reference we got above. */
552 vfs_node_put(mp_node);
553 /* Drop the reference from when the file system was mounted. */
554 vfs_node_put(mp_node);
555 }
556
557
558 /*
559 * All went well, the mounted file system was successfully unmounted.
560 * The only thing left is to forget the unmounted root VFS node.
561 */
562 vfs_node_forget(mr_node);
563
564 fibril_rwlock_write_unlock(&namespace_rwlock);
565 ipc_answer_0(rid, EOK);
[7f5e070]566}
567
[861e7d1]568void vfs_open(ipc_callid_t rid, ipc_call_t *request)
569{
570 if (!vfs_files_init()) {
571 ipc_answer_0(rid, ENOMEM);
572 return;
573 }
[05b9912]574
[861e7d1]575 /*
[ae78b530]576 * The POSIX interface is open(path, oflag, mode).
[4198f9c3]577 * We can receive oflags and mode along with the VFS_IN_OPEN call;
578 * the path will need to arrive in another call.
[ae78b530]579 *
580 * We also receive one private, non-POSIX set of flags called lflag
581 * used to pass information to vfs_lookup_internal().
[861e7d1]582 */
[ae78b530]583 int lflag = IPC_GET_ARG1(*request);
584 int oflag = IPC_GET_ARG2(*request);
585 int mode = IPC_GET_ARG3(*request);
[861e7d1]586 size_t len;
[dd2cfa7]587
588 /* Ignore mode for now. */
589 (void) mode;
[05b9912]590
[b17186d]591 /*
592 * Make sure that we are called with exactly one of L_FILE and
[f7376cbf]593 * L_DIRECTORY. Make sure that the user does not pass L_OPEN,
594 * L_ROOT or L_MP.
[b17186d]595 */
[230260ac]596 if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
597 ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
[f7376cbf]598 (lflag & (L_OPEN | L_ROOT | L_MP))) {
[b17186d]599 ipc_answer_0(rid, EINVAL);
600 return;
601 }
[05b9912]602
[2db4ac8]603 if (oflag & O_CREAT)
604 lflag |= L_CREATE;
605 if (oflag & O_EXCL)
606 lflag |= L_EXCLUSIVE;
[05b9912]607
[861e7d1]608 ipc_callid_t callid;
[0da4e41]609 if (!async_data_write_receive(&callid, &len)) {
[861e7d1]610 ipc_answer_0(callid, EINVAL);
611 ipc_answer_0(rid, EINVAL);
612 return;
613 }
[05b9912]614
[d6084ef]615 char *path = malloc(len + 1);
[861e7d1]616 if (!path) {
617 ipc_answer_0(callid, ENOMEM);
618 ipc_answer_0(rid, ENOMEM);
619 return;
620 }
[05b9912]621
[861e7d1]622 int rc;
[0da4e41]623 if ((rc = async_data_write_finalize(callid, path, len))) {
[861e7d1]624 ipc_answer_0(rid, rc);
625 free(path);
626 return;
627 }
[d6084ef]628 path[len] = '\0';
[861e7d1]629
630 /*
631 * Avoid the race condition in which the file can be deleted before we
632 * find/create-and-lock the VFS node corresponding to the looked-up
633 * triplet.
634 */
[2db4ac8]635 if (lflag & L_CREATE)
[230260ac]636 fibril_rwlock_write_lock(&namespace_rwlock);
[2db4ac8]637 else
[230260ac]638 fibril_rwlock_read_lock(&namespace_rwlock);
[05b9912]639
[72bde81]640 /* The path is now populated and we can call vfs_lookup_internal(). */
[eb27ce5a]641 vfs_lookup_res_t lr;
[05b9912]642 rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
643 if (rc != EOK) {
[2db4ac8]644 if (lflag & L_CREATE)
[230260ac]645 fibril_rwlock_write_unlock(&namespace_rwlock);
[2db4ac8]646 else
[230260ac]647 fibril_rwlock_read_unlock(&namespace_rwlock);
[861e7d1]648 ipc_answer_0(rid, rc);
649 free(path);
650 return;
651 }
[05b9912]652
[7fe1f75]653 /* Path is no longer needed. */
[861e7d1]654 free(path);
[05b9912]655
[eb27ce5a]656 vfs_node_t *node = vfs_node_get(&lr);
[2db4ac8]657 if (lflag & L_CREATE)
[230260ac]658 fibril_rwlock_write_unlock(&namespace_rwlock);
[2db4ac8]659 else
[230260ac]660 fibril_rwlock_read_unlock(&namespace_rwlock);
[05b9912]661
[7fe1f75]662 /* Truncate the file if requested and if necessary. */
663 if (oflag & O_TRUNC) {
[230260ac]664 fibril_rwlock_write_lock(&node->contents_rwlock);
[7fe1f75]665 if (node->size) {
666 rc = vfs_truncate_internal(node->fs_handle,
667 node->dev_handle, node->index, 0);
668 if (rc) {
[230260ac]669 fibril_rwlock_write_unlock(&node->contents_rwlock);
[7fe1f75]670 vfs_node_put(node);
671 ipc_answer_0(rid, rc);
672 return;
673 }
674 node->size = 0;
675 }
[230260ac]676 fibril_rwlock_write_unlock(&node->contents_rwlock);
[7fe1f75]677 }
[05b9912]678
[861e7d1]679 /*
680 * Get ourselves a file descriptor and the corresponding vfs_file_t
681 * structure.
682 */
[2b88074b]683 int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
[861e7d1]684 if (fd < 0) {
685 vfs_node_put(node);
686 ipc_answer_0(rid, fd);
687 return;
688 }
689 vfs_file_t *file = vfs_file_get(fd);
690 file->node = node;
[05b9912]691 if (oflag & O_APPEND)
[15b9970]692 file->append = true;
[05b9912]693
[861e7d1]694 /*
695 * The following increase in reference count is for the fact that the
696 * file is being opened and that a file structure is pointing to it.
697 * It is necessary so that the file will not disappear when
698 * vfs_node_put() is called. The reference will be dropped by the
[4198f9c3]699 * respective VFS_IN_CLOSE.
[861e7d1]700 */
701 vfs_node_addref(node);
702 vfs_node_put(node);
[05b9912]703
704 /* Success! Return the new file descriptor to the client. */
705 ipc_answer_1(rid, EOK, fd);
706}
[861e7d1]707
[05b9912]708void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
709{
710 // FIXME: check for sanity of the supplied fs, dev and index
711
712 if (!vfs_files_init()) {
713 ipc_answer_0(rid, ENOMEM);
714 return;
715 }
716
717 /*
718 * The interface is open_node(fs, dev, index, oflag).
719 */
720 vfs_lookup_res_t lr;
721
722 lr.triplet.fs_handle = IPC_GET_ARG1(*request);
723 lr.triplet.dev_handle = IPC_GET_ARG2(*request);
724 lr.triplet.index = IPC_GET_ARG3(*request);
725 int oflag = IPC_GET_ARG4(*request);
726
[230260ac]727 fibril_rwlock_read_lock(&namespace_rwlock);
[05b9912]728
729 int rc = vfs_open_node_internal(&lr);
730 if (rc != EOK) {
[230260ac]731 fibril_rwlock_read_unlock(&namespace_rwlock);
[05b9912]732 ipc_answer_0(rid, rc);
733 return;
734 }
735
736 vfs_node_t *node = vfs_node_get(&lr);
[230260ac]737 fibril_rwlock_read_unlock(&namespace_rwlock);
[05b9912]738
739 /* Truncate the file if requested and if necessary. */
740 if (oflag & O_TRUNC) {
[230260ac]741 fibril_rwlock_write_lock(&node->contents_rwlock);
[05b9912]742 if (node->size) {
743 rc = vfs_truncate_internal(node->fs_handle,
744 node->dev_handle, node->index, 0);
745 if (rc) {
[230260ac]746 fibril_rwlock_write_unlock(&node->contents_rwlock);
[05b9912]747 vfs_node_put(node);
748 ipc_answer_0(rid, rc);
749 return;
750 }
751 node->size = 0;
752 }
[230260ac]753 fibril_rwlock_write_unlock(&node->contents_rwlock);
[05b9912]754 }
755
756 /*
757 * Get ourselves a file descriptor and the corresponding vfs_file_t
758 * structure.
759 */
[2b88074b]760 int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
[05b9912]761 if (fd < 0) {
762 vfs_node_put(node);
763 ipc_answer_0(rid, fd);
764 return;
765 }
766 vfs_file_t *file = vfs_file_get(fd);
767 file->node = node;
768 if (oflag & O_APPEND)
769 file->append = true;
770
771 /*
772 * The following increase in reference count is for the fact that the
773 * file is being opened and that a file structure is pointing to it.
774 * It is necessary so that the file will not disappear when
775 * vfs_node_put() is called. The reference will be dropped by the
[4198f9c3]776 * respective VFS_IN_CLOSE.
[05b9912]777 */
778 vfs_node_addref(node);
779 vfs_node_put(node);
780
[72bde81]781 /* Success! Return the new file descriptor to the client. */
[861e7d1]782 ipc_answer_1(rid, EOK, fd);
783}
784
[05b9912]785void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
786{
787 int fd = IPC_GET_ARG1(*request);
788
789 /* Lookup the file structure corresponding to the file descriptor. */
790 vfs_file_t *file = vfs_file_get(fd);
791 if (!file) {
792 ipc_answer_0(rid, ENOENT);
793 return;
794 }
795
796 /*
797 * Lock the open file structure so that no other thread can manipulate
798 * the same open file at a time.
799 */
[230260ac]800 fibril_mutex_lock(&file->lock);
[05b9912]801 int fs_phone = vfs_grab_phone(file->node->fs_handle);
802
[4198f9c3]803 /* Make a VFS_OUT_SYMC request at the destination FS server. */
[05b9912]804 aid_t msg;
805 ipc_call_t answer;
[4198f9c3]806 msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->dev_handle,
807 file->node->index, &answer);
[230260ac]808
[05b9912]809 /* Wait for reply from the FS server. */
810 ipcarg_t rc;
811 async_wait_for(msg, &rc);
812
[34ca870]813 vfs_release_phone(fs_phone);
[230260ac]814 fibril_mutex_unlock(&file->lock);
[05b9912]815
[b7f9087]816 ipc_answer_0(rid, rc);
[e704503]817}
818
[f29a3a2]819int vfs_close_internal(vfs_file_t *file)
[2b88074b]820{
821 /*
822 * Lock the open file structure so that no other thread can manipulate
823 * the same open file at a time.
824 */
825 fibril_mutex_lock(&file->lock);
826
827 if (file->refcnt <= 1) {
828 /* Only close the file on the destination FS server
829 if there are no more file descriptors (except the
830 present one) pointing to this file. */
831
832 int fs_phone = vfs_grab_phone(file->node->fs_handle);
833
834 /* Make a VFS_OUT_CLOSE request at the destination FS server. */
835 aid_t msg;
836 ipc_call_t answer;
837 msg = async_send_2(fs_phone, VFS_OUT_CLOSE, file->node->dev_handle,
838 file->node->index, &answer);
839
840 /* Wait for reply from the FS server. */
841 ipcarg_t rc;
842 async_wait_for(msg, &rc);
843
844 vfs_release_phone(fs_phone);
845 fibril_mutex_unlock(&file->lock);
846
847 return IPC_GET_ARG1(answer);
848 }
849
850 fibril_mutex_unlock(&file->lock);
851 return EOK;
852}
853
[05b9912]854void vfs_close(ipc_callid_t rid, ipc_call_t *request)
855{
856 int fd = IPC_GET_ARG1(*request);
857
858 /* Lookup the file structure corresponding to the file descriptor. */
859 vfs_file_t *file = vfs_file_get(fd);
860 if (!file) {
861 ipc_answer_0(rid, ENOENT);
862 return;
863 }
864
[2b88074b]865 int ret = vfs_close_internal(file);
866 if (ret != EOK)
867 ipc_answer_0(rid, ret);
[05b9912]868
[2b88074b]869 ret = vfs_fd_free(fd);
870 ipc_answer_0(rid, ret);
[05b9912]871}
872
[861e7d1]873static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
874{
875
876 /*
877 * The following code strongly depends on the fact that the files data
878 * structure can be only accessed by a single fibril and all file
879 * operations are serialized (i.e. the reads and writes cannot
880 * interleave and a file cannot be closed while it is being read).
881 *
882 * Additional synchronization needs to be added once the table of
883 * open files supports parallel access!
884 */
885
886 int fd = IPC_GET_ARG1(*request);
[6c89f20]887
[72bde81]888 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]889 vfs_file_t *file = vfs_file_get(fd);
890 if (!file) {
891 ipc_answer_0(rid, ENOENT);
892 return;
893 }
[6c89f20]894
[861e7d1]895 /*
896 * Now we need to receive a call with client's
897 * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
898 */
899 ipc_callid_t callid;
900 int res;
901 if (read)
[0da4e41]902 res = async_data_read_receive(&callid, NULL);
[861e7d1]903 else
[0da4e41]904 res = async_data_write_receive(&callid, NULL);
[861e7d1]905 if (!res) {
906 ipc_answer_0(callid, EINVAL);
907 ipc_answer_0(rid, EINVAL);
908 return;
909 }
[6c89f20]910
[861e7d1]911 /*
912 * Lock the open file structure so that no other thread can manipulate
913 * the same open file at a time.
914 */
[230260ac]915 fibril_mutex_lock(&file->lock);
[b17186d]916
[861e7d1]917 /*
918 * Lock the file's node so that no other client can read/write to it at
919 * the same time.
920 */
921 if (read)
[230260ac]922 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[861e7d1]923 else
[230260ac]924 fibril_rwlock_write_lock(&file->node->contents_rwlock);
[b17186d]925
926 if (file->node->type == VFS_NODE_DIRECTORY) {
927 /*
928 * Make sure that no one is modifying the namespace
929 * while we are in readdir().
930 */
931 assert(read);
[230260ac]932 fibril_rwlock_read_lock(&namespace_rwlock);
[b17186d]933 }
[6c89f20]934
[861e7d1]935 int fs_phone = vfs_grab_phone(file->node->fs_handle);
936
[72bde81]937 /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
[861e7d1]938 aid_t msg;
939 ipc_call_t answer;
[15b9970]940 if (!read && file->append)
941 file->pos = file->node->size;
[4198f9c3]942 msg = async_send_3(fs_phone, read ? VFS_OUT_READ : VFS_OUT_WRITE,
[861e7d1]943 file->node->dev_handle, file->node->index, file->pos, &answer);
944
945 /*
946 * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
947 * destination FS server. The call will be routed as if sent by
948 * ourselves. Note that call arguments are immutable in this case so we
949 * don't have to bother.
950 */
951 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
[230260ac]952
[72bde81]953 /* Wait for reply from the FS server. */
[861e7d1]954 ipcarg_t rc;
955 async_wait_for(msg, &rc);
[05b9912]956
[34ca870]957 vfs_release_phone(fs_phone);
958
[861e7d1]959 size_t bytes = IPC_GET_ARG1(answer);
[b17186d]960
961 if (file->node->type == VFS_NODE_DIRECTORY)
[230260ac]962 fibril_rwlock_read_unlock(&namespace_rwlock);
[6c89f20]963
[72bde81]964 /* Unlock the VFS node. */
[861e7d1]965 if (read)
[230260ac]966 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[861e7d1]967 else {
968 /* Update the cached version of node's size. */
[f7017572]969 if (rc == EOK)
970 file->node->size = IPC_GET_ARG2(answer);
[230260ac]971 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
[861e7d1]972 }
[6c89f20]973
[72bde81]974 /* Update the position pointer and unlock the open file. */
[f7017572]975 if (rc == EOK)
976 file->pos += bytes;
[230260ac]977 fibril_mutex_unlock(&file->lock);
[6c89f20]978
[861e7d1]979 /*
980 * FS server's reply is the final result of the whole operation we
981 * return to the client.
982 */
983 ipc_answer_1(rid, rc, bytes);
984}
985
986void vfs_read(ipc_callid_t rid, ipc_call_t *request)
987{
988 vfs_rdwr(rid, request, true);
989}
990
991void vfs_write(ipc_callid_t rid, ipc_call_t *request)
992{
993 vfs_rdwr(rid, request, false);
994}
995
996void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
997{
998 int fd = (int) IPC_GET_ARG1(*request);
999 off_t off = (off_t) IPC_GET_ARG2(*request);
1000 int whence = (int) IPC_GET_ARG3(*request);
1001
1002
[72bde81]1003 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]1004 vfs_file_t *file = vfs_file_get(fd);
1005 if (!file) {
1006 ipc_answer_0(rid, ENOENT);
1007 return;
1008 }
1009
1010 off_t newpos;
[230260ac]1011 fibril_mutex_lock(&file->lock);
[861e7d1]1012 if (whence == SEEK_SET) {
1013 file->pos = off;
[230260ac]1014 fibril_mutex_unlock(&file->lock);
[861e7d1]1015 ipc_answer_1(rid, EOK, off);
1016 return;
1017 }
1018 if (whence == SEEK_CUR) {
1019 if (file->pos + off < file->pos) {
[230260ac]1020 fibril_mutex_unlock(&file->lock);
[861e7d1]1021 ipc_answer_0(rid, EOVERFLOW);
1022 return;
1023 }
1024 file->pos += off;
1025 newpos = file->pos;
[230260ac]1026 fibril_mutex_unlock(&file->lock);
[861e7d1]1027 ipc_answer_1(rid, EOK, newpos);
1028 return;
1029 }
1030 if (whence == SEEK_END) {
[230260ac]1031 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[861e7d1]1032 size_t size = file->node->size;
[230260ac]1033 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[861e7d1]1034 if (size + off < size) {
[230260ac]1035 fibril_mutex_unlock(&file->lock);
[861e7d1]1036 ipc_answer_0(rid, EOVERFLOW);
1037 return;
1038 }
1039 newpos = size + off;
[08232ee]1040 file->pos = newpos;
[230260ac]1041 fibril_mutex_unlock(&file->lock);
[861e7d1]1042 ipc_answer_1(rid, EOK, newpos);
1043 return;
1044 }
[230260ac]1045 fibril_mutex_unlock(&file->lock);
[861e7d1]1046 ipc_answer_0(rid, EINVAL);
1047}
1048
[f2ec8c8]1049int
1050vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
1051 fs_index_t index, size_t size)
[7fe1f75]1052{
1053 ipcarg_t rc;
1054 int fs_phone;
1055
1056 fs_phone = vfs_grab_phone(fs_handle);
[4198f9c3]1057 rc = async_req_3_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t)dev_handle,
[7fe1f75]1058 (ipcarg_t)index, (ipcarg_t)size);
1059 vfs_release_phone(fs_phone);
1060 return (int)rc;
1061}
1062
[0ee4322]1063void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
1064{
1065 int fd = IPC_GET_ARG1(*request);
1066 size_t size = IPC_GET_ARG2(*request);
[7fe1f75]1067 int rc;
[0ee4322]1068
1069 vfs_file_t *file = vfs_file_get(fd);
1070 if (!file) {
1071 ipc_answer_0(rid, ENOENT);
1072 return;
1073 }
[230260ac]1074 fibril_mutex_lock(&file->lock);
[0ee4322]1075
[230260ac]1076 fibril_rwlock_write_lock(&file->node->contents_rwlock);
[7fe1f75]1077 rc = vfs_truncate_internal(file->node->fs_handle,
1078 file->node->dev_handle, file->node->index, size);
[0ee4322]1079 if (rc == EOK)
1080 file->node->size = size;
[230260ac]1081 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
[0ee4322]1082
[230260ac]1083 fibril_mutex_unlock(&file->lock);
[7fe1f75]1084 ipc_answer_0(rid, (ipcarg_t)rc);
[861e7d1]1085}
1086
[852b801]1087void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
1088{
1089 int fd = IPC_GET_ARG1(*request);
1090 ipcarg_t rc;
1091
1092 vfs_file_t *file = vfs_file_get(fd);
1093 if (!file) {
1094 ipc_answer_0(rid, ENOENT);
1095 return;
1096 }
1097
1098 ipc_callid_t callid;
[0da4e41]1099 if (!async_data_read_receive(&callid, NULL)) {
[852b801]1100 ipc_answer_0(callid, EINVAL);
1101 ipc_answer_0(rid, EINVAL);
1102 return;
1103 }
1104
1105 fibril_mutex_lock(&file->lock);
1106
1107 int fs_phone = vfs_grab_phone(file->node->fs_handle);
1108
1109 aid_t msg;
1110 msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->dev_handle,
1111 file->node->index, true, NULL);
1112 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1113 async_wait_for(msg, &rc);
1114 vfs_release_phone(fs_phone);
1115
1116 fibril_mutex_unlock(&file->lock);
1117 ipc_answer_0(rid, rc);
1118}
1119
1120void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
1121{
[415c7e0d]1122 size_t len;
1123 ipc_callid_t callid;
1124
[0da4e41]1125 if (!async_data_write_receive(&callid, &len)) {
[415c7e0d]1126 ipc_answer_0(callid, EINVAL);
1127 ipc_answer_0(rid, EINVAL);
1128 return;
1129 }
1130 char *path = malloc(len + 1);
1131 if (!path) {
1132 ipc_answer_0(callid, ENOMEM);
1133 ipc_answer_0(rid, ENOMEM);
1134 return;
1135 }
1136 int rc;
[0da4e41]1137 if ((rc = async_data_write_finalize(callid, path, len))) {
[415c7e0d]1138 ipc_answer_0(rid, rc);
1139 free(path);
1140 return;
1141 }
1142 path[len] = '\0';
1143
[0da4e41]1144 if (!async_data_read_receive(&callid, NULL)) {
[415c7e0d]1145 free(path);
1146 ipc_answer_0(callid, EINVAL);
1147 ipc_answer_0(rid, EINVAL);
1148 return;
1149 }
1150
1151 vfs_lookup_res_t lr;
1152 fibril_rwlock_read_lock(&namespace_rwlock);
1153 rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
1154 free(path);
1155 if (rc != EOK) {
1156 fibril_rwlock_read_unlock(&namespace_rwlock);
1157 ipc_answer_0(callid, rc);
1158 ipc_answer_0(rid, rc);
1159 return;
1160 }
1161 vfs_node_t *node = vfs_node_get(&lr);
1162 if (!node) {
1163 fibril_rwlock_read_unlock(&namespace_rwlock);
1164 ipc_answer_0(callid, ENOMEM);
1165 ipc_answer_0(rid, ENOMEM);
1166 return;
1167 }
1168
1169 fibril_rwlock_read_unlock(&namespace_rwlock);
1170
1171 int fs_phone = vfs_grab_phone(node->fs_handle);
1172 aid_t msg;
1173 msg = async_send_3(fs_phone, VFS_OUT_STAT, node->dev_handle,
1174 node->index, false, NULL);
1175 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
[057760d3]1176
1177 ipcarg_t rv;
1178 async_wait_for(msg, &rv);
[415c7e0d]1179 vfs_release_phone(fs_phone);
1180
[057760d3]1181 ipc_answer_0(rid, rv);
[415c7e0d]1182
1183 vfs_node_put(node);
[852b801]1184}
1185
[72bde81]1186void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
1187{
1188 int mode = IPC_GET_ARG1(*request);
1189
[f15cf1a6]1190 size_t len;
[72bde81]1191 ipc_callid_t callid;
1192
[0da4e41]1193 if (!async_data_write_receive(&callid, &len)) {
[72bde81]1194 ipc_answer_0(callid, EINVAL);
1195 ipc_answer_0(rid, EINVAL);
1196 return;
1197 }
[d6084ef]1198 char *path = malloc(len + 1);
[72bde81]1199 if (!path) {
1200 ipc_answer_0(callid, ENOMEM);
1201 ipc_answer_0(rid, ENOMEM);
1202 return;
1203 }
1204 int rc;
[0da4e41]1205 if ((rc = async_data_write_finalize(callid, path, len))) {
[72bde81]1206 ipc_answer_0(rid, rc);
1207 free(path);
1208 return;
1209 }
[d6084ef]1210 path[len] = '\0';
[dd2cfa7]1211
1212 /* Ignore mode for now. */
1213 (void) mode;
[72bde81]1214
[230260ac]1215 fibril_rwlock_write_lock(&namespace_rwlock);
[72bde81]1216 int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
[d6084ef]1217 rc = vfs_lookup_internal(path, lflag, NULL, NULL);
[230260ac]1218 fibril_rwlock_write_unlock(&namespace_rwlock);
[72bde81]1219 free(path);
1220 ipc_answer_0(rid, rc);
1221}
1222
[f15cf1a6]1223void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1224{
1225 int lflag = IPC_GET_ARG1(*request);
1226
1227 size_t len;
1228 ipc_callid_t callid;
1229
[0da4e41]1230 if (!async_data_write_receive(&callid, &len)) {
[f15cf1a6]1231 ipc_answer_0(callid, EINVAL);
1232 ipc_answer_0(rid, EINVAL);
1233 return;
1234 }
[d6084ef]1235 char *path = malloc(len + 1);
[f15cf1a6]1236 if (!path) {
1237 ipc_answer_0(callid, ENOMEM);
1238 ipc_answer_0(rid, ENOMEM);
1239 return;
1240 }
1241 int rc;
[0da4e41]1242 if ((rc = async_data_write_finalize(callid, path, len))) {
[f15cf1a6]1243 ipc_answer_0(rid, rc);
1244 free(path);
1245 return;
1246 }
[d6084ef]1247 path[len] = '\0';
[f15cf1a6]1248
[230260ac]1249 fibril_rwlock_write_lock(&namespace_rwlock);
[f15cf1a6]1250 lflag &= L_DIRECTORY; /* sanitize lflag */
1251 vfs_lookup_res_t lr;
[a8e9ab8d]1252 rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
[f15cf1a6]1253 free(path);
1254 if (rc != EOK) {
[230260ac]1255 fibril_rwlock_write_unlock(&namespace_rwlock);
[f15cf1a6]1256 ipc_answer_0(rid, rc);
1257 return;
1258 }
1259
1260 /*
1261 * The name has already been unlinked by vfs_lookup_internal().
1262 * We have to get and put the VFS node to ensure that it is
[4198f9c3]1263 * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
[f15cf1a6]1264 */
1265 vfs_node_t *node = vfs_node_get(&lr);
[553492be]1266 fibril_mutex_lock(&nodes_mutex);
[f15cf1a6]1267 node->lnkcnt--;
[553492be]1268 fibril_mutex_unlock(&nodes_mutex);
[230260ac]1269 fibril_rwlock_write_unlock(&namespace_rwlock);
[f15cf1a6]1270 vfs_node_put(node);
1271 ipc_answer_0(rid, EOK);
1272}
1273
[a8e9ab8d]1274void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1275{
[732bb0c]1276 size_t olen, nlen;
[a8e9ab8d]1277 ipc_callid_t callid;
1278 int rc;
1279
1280 /* Retrieve the old path. */
[0da4e41]1281 if (!async_data_write_receive(&callid, &olen)) {
[a8e9ab8d]1282 ipc_answer_0(callid, EINVAL);
1283 ipc_answer_0(rid, EINVAL);
1284 return;
1285 }
[732bb0c]1286 char *old = malloc(olen + 1);
[a8e9ab8d]1287 if (!old) {
1288 ipc_answer_0(callid, ENOMEM);
1289 ipc_answer_0(rid, ENOMEM);
1290 return;
1291 }
[0da4e41]1292 if ((rc = async_data_write_finalize(callid, old, olen))) {
[a8e9ab8d]1293 ipc_answer_0(rid, rc);
1294 free(old);
1295 return;
1296 }
[732bb0c]1297 old[olen] = '\0';
[a8e9ab8d]1298
1299 /* Retrieve the new path. */
[0da4e41]1300 if (!async_data_write_receive(&callid, &nlen)) {
[a8e9ab8d]1301 ipc_answer_0(callid, EINVAL);
1302 ipc_answer_0(rid, EINVAL);
1303 free(old);
1304 return;
1305 }
[732bb0c]1306 char *new = malloc(nlen + 1);
[a8e9ab8d]1307 if (!new) {
1308 ipc_answer_0(callid, ENOMEM);
1309 ipc_answer_0(rid, ENOMEM);
1310 free(old);
1311 return;
1312 }
[0da4e41]1313 if ((rc = async_data_write_finalize(callid, new, nlen))) {
[a8e9ab8d]1314 ipc_answer_0(rid, rc);
1315 free(old);
1316 free(new);
1317 return;
1318 }
[732bb0c]1319 new[nlen] = '\0';
[a8e9ab8d]1320
[732bb0c]1321 char *oldc = canonify(old, &olen);
1322 char *newc = canonify(new, &nlen);
[a8e9ab8d]1323 if (!oldc || !newc) {
1324 ipc_answer_0(rid, EINVAL);
1325 free(old);
1326 free(new);
1327 return;
1328 }
[732bb0c]1329 oldc[olen] = '\0';
1330 newc[nlen] = '\0';
[14040e5]1331 if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
1332 ((newc[str_length(oldc)] == '/') ||
1333 (str_length(oldc) == 1) ||
1334 (str_length(oldc) == str_length(newc)))) {
1335 /*
1336 * oldc is a prefix of newc and either
1337 * - newc continues with a / where oldc ends, or
1338 * - oldc was / itself, or
1339 * - oldc and newc are equal.
1340 */
[a8e9ab8d]1341 ipc_answer_0(rid, EINVAL);
1342 free(old);
1343 free(new);
1344 return;
1345 }
1346
1347 vfs_lookup_res_t old_lr;
1348 vfs_lookup_res_t new_lr;
1349 vfs_lookup_res_t new_par_lr;
[230260ac]1350 fibril_rwlock_write_lock(&namespace_rwlock);
[a8e9ab8d]1351 /* Lookup the node belonging to the old file name. */
1352 rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1353 if (rc != EOK) {
[230260ac]1354 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1355 ipc_answer_0(rid, rc);
1356 free(old);
1357 free(new);
1358 return;
1359 }
1360 vfs_node_t *old_node = vfs_node_get(&old_lr);
1361 if (!old_node) {
[230260ac]1362 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1363 ipc_answer_0(rid, ENOMEM);
1364 free(old);
1365 free(new);
1366 return;
1367 }
[4f46695e]1368 /* Determine the path to the parent of the node with the new name. */
1369 char *parentc = str_dup(newc);
1370 if (!parentc) {
[230260ac]1371 fibril_rwlock_write_unlock(&namespace_rwlock);
[4f46695e]1372 ipc_answer_0(rid, rc);
1373 free(old);
1374 free(new);
1375 return;
1376 }
[ae55ee8]1377 char *lastsl = str_rchr(parentc + 1, '/');
[4f46695e]1378 if (lastsl)
1379 *lastsl = '\0';
1380 else
1381 parentc[1] = '\0';
[a8e9ab8d]1382 /* Lookup parent of the new file name. */
[4f46695e]1383 rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
1384 free(parentc); /* not needed anymore */
[a8e9ab8d]1385 if (rc != EOK) {
[230260ac]1386 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1387 ipc_answer_0(rid, rc);
1388 free(old);
1389 free(new);
1390 return;
1391 }
1392 /* Check whether linking to the same file system instance. */
1393 if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1394 (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
[230260ac]1395 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1396 ipc_answer_0(rid, EXDEV); /* different file systems */
1397 free(old);
1398 free(new);
1399 return;
1400 }
1401 /* Destroy the old link for the new name. */
1402 vfs_node_t *new_node = NULL;
1403 rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1404 switch (rc) {
1405 case ENOENT:
1406 /* simply not in our way */
1407 break;
1408 case EOK:
1409 new_node = vfs_node_get(&new_lr);
1410 if (!new_node) {
[230260ac]1411 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1412 ipc_answer_0(rid, ENOMEM);
1413 free(old);
1414 free(new);
1415 return;
1416 }
[553492be]1417 fibril_mutex_lock(&nodes_mutex);
[a8e9ab8d]1418 new_node->lnkcnt--;
[553492be]1419 fibril_mutex_unlock(&nodes_mutex);
[a8e9ab8d]1420 break;
1421 default:
[230260ac]1422 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1423 ipc_answer_0(rid, ENOTEMPTY);
1424 free(old);
1425 free(new);
1426 return;
1427 }
1428 /* Create the new link for the new name. */
1429 rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1430 if (rc != EOK) {
[230260ac]1431 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1432 if (new_node)
1433 vfs_node_put(new_node);
1434 ipc_answer_0(rid, rc);
1435 free(old);
1436 free(new);
1437 return;
1438 }
[553492be]1439 fibril_mutex_lock(&nodes_mutex);
[a8e9ab8d]1440 old_node->lnkcnt++;
[553492be]1441 fibril_mutex_unlock(&nodes_mutex);
[a8e9ab8d]1442 /* Destroy the link for the old name. */
1443 rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1444 if (rc != EOK) {
[230260ac]1445 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1446 vfs_node_put(old_node);
1447 if (new_node)
1448 vfs_node_put(new_node);
1449 ipc_answer_0(rid, rc);
1450 free(old);
1451 free(new);
1452 return;
1453 }
[553492be]1454 fibril_mutex_lock(&nodes_mutex);
[a8e9ab8d]1455 old_node->lnkcnt--;
[553492be]1456 fibril_mutex_unlock(&nodes_mutex);
[230260ac]1457 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1458 vfs_node_put(old_node);
1459 if (new_node)
1460 vfs_node_put(new_node);
1461 free(old);
1462 free(new);
1463 ipc_answer_0(rid, EOK);
1464}
1465
[2b88074b]1466void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
1467{
1468 int oldfd = IPC_GET_ARG1(*request);
1469 int newfd = IPC_GET_ARG2(*request);
1470
1471 /* Lookup the file structure corresponding to oldfd. */
1472 vfs_file_t *oldfile = vfs_file_get(oldfd);
1473 if (!oldfile) {
1474 ipc_answer_0(rid, EBADF);
1475 return;
1476 }
1477
1478 /* If the file descriptors are the same, do nothing. */
1479 if (oldfd == newfd) {
1480 ipc_answer_1(rid, EOK, newfd);
1481 return;
1482 }
1483
1484 /*
1485 * Lock the open file structure so that no other thread can manipulate
1486 * the same open file at a time.
1487 */
1488 fibril_mutex_lock(&oldfile->lock);
1489
1490 /* Lookup an open file structure possibly corresponding to newfd. */
1491 vfs_file_t *newfile = vfs_file_get(newfd);
1492 if (newfile) {
1493 /* Close the originally opened file. */
1494 int ret = vfs_close_internal(newfile);
1495 if (ret != EOK) {
1496 ipc_answer_0(rid, ret);
1497 return;
1498 }
1499
1500 ret = vfs_fd_free(newfd);
1501 if (ret != EOK) {
1502 ipc_answer_0(rid, ret);
1503 return;
1504 }
1505 }
1506
1507 /* Assign the old file to newfd. */
1508 int ret = vfs_fd_assign(oldfile, newfd);
1509 fibril_mutex_unlock(&oldfile->lock);
1510
1511 if (ret != EOK)
1512 ipc_answer_0(rid, ret);
1513 else
1514 ipc_answer_1(rid, EOK, newfd);
1515}
1516
[861e7d1]1517/**
1518 * @}
[05b9912]1519 */
Note: See TracBrowser for help on using the repository browser.