Subversion Repositories HelenOS

Rev

Rev 4539 | Rev 4555 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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