Subversion Repositories HelenOS

Rev

Rev 2752 | Rev 2763 | 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_lookup.c
  35.  * @brief
  36.  */
  37.  
  38. #include "vfs.h"
  39. #include <ipc/ipc.h>
  40. #include <async.h>
  41. #include <errno.h>
  42. #include <string.h>
  43. #include <bool.h>
  44. #include <futex.h>
  45. #include <libadt/list.h>
  46. #include <atomic.h>
  47. #include <vfs/canonify.h>
  48.  
  49. #define min(a, b)   ((a) < (b) ? (a) : (b))
  50.  
  51. atomic_t plb_futex = FUTEX_INITIALIZER;
  52. link_t plb_head;    /**< PLB entry ring buffer. */
  53. uint8_t *plb = NULL;
  54.  
  55. /** Perform a path lookup.
  56.  *
  57.  * @param path      Path to be resolved; it must be a NULL-terminated
  58.  *          string.
  59.  * @param lflag     Flags to be used during lookup.
  60.  * @param result    Empty structure where the lookup result will be stored.
  61.  *          Can be NULL.
  62.  * @param altroot   If non-empty, will be used instead of rootfs as the root
  63.  *          of the whole VFS tree.
  64.  *
  65.  * @return      EOK on success or an error code from errno.h.
  66.  */
  67. int vfs_lookup_internal(char *path, int lflag, vfs_lookup_res_t *result,
  68.     vfs_pair_t *altroot)
  69. {
  70.     vfs_pair_t *root;
  71.  
  72.     if (altroot)
  73.         root = altroot;
  74.     else
  75.         root = (vfs_pair_t *) &rootfs;
  76.  
  77.     if (!root->fs_handle)
  78.         return ENOENT;
  79.    
  80.     size_t len;
  81.     path = canonify(path, &len);
  82.     if (!path)
  83.         return EINVAL;
  84.    
  85.     futex_down(&plb_futex);
  86.  
  87.     plb_entry_t entry;
  88.     link_initialize(&entry.plb_link);
  89.     entry.len = len;
  90.  
  91.     off_t first;    /* the first free index */
  92.     off_t last; /* the last free index */
  93.  
  94.     if (list_empty(&plb_head)) {
  95.         first = 0;
  96.         last = PLB_SIZE - 1;
  97.     } else {
  98.         plb_entry_t *oldest = list_get_instance(plb_head.next,
  99.             plb_entry_t, plb_link);
  100.         plb_entry_t *newest = list_get_instance(plb_head.prev,
  101.             plb_entry_t, plb_link);
  102.  
  103.         first = (newest->index + newest->len) % PLB_SIZE;
  104.         last = (oldest->index - 1) % PLB_SIZE;
  105.     }
  106.  
  107.     if (first <= last) {
  108.         if ((last - first) + 1 < len) {
  109.             /*
  110.              * The buffer cannot absorb the path.
  111.              */
  112.             futex_up(&plb_futex);
  113.             return ELIMIT;
  114.         }
  115.     } else {
  116.         if (PLB_SIZE - ((first - last) + 1) < len) {
  117.             /*
  118.              * The buffer cannot absorb the path.
  119.              */
  120.             futex_up(&plb_futex);
  121.             return ELIMIT;
  122.         }
  123.     }
  124.  
  125.     /*
  126.      * We know the first free index in PLB and we also know that there is
  127.      * enough space in the buffer to hold our path.
  128.      */
  129.  
  130.     entry.index = first;
  131.     entry.len = len;
  132.  
  133.     /*
  134.      * Claim PLB space by inserting the entry into the PLB entry ring
  135.      * buffer.
  136.      */
  137.     list_append(&entry.plb_link, &plb_head);
  138.    
  139.     futex_up(&plb_futex);
  140.  
  141.     /*
  142.      * Copy the path into PLB.
  143.      */
  144.     size_t cnt1 = min(len, (PLB_SIZE - first) + 1);
  145.     size_t cnt2 = len - cnt1;
  146.    
  147.     memcpy(&plb[first], path, cnt1);
  148.     memcpy(plb, &path[cnt1], cnt2);
  149.  
  150.     ipc_call_t answer;
  151.     int phone = vfs_grab_phone(root->fs_handle);
  152.     aid_t req = async_send_4(phone, VFS_LOOKUP, (ipcarg_t) first,
  153.         (ipcarg_t) (first + len - 1) % PLB_SIZE,
  154.         (ipcarg_t) root->dev_handle, (ipcarg_t) lflag, &answer);
  155.     vfs_release_phone(phone);
  156.  
  157.     ipcarg_t rc;
  158.     async_wait_for(req, &rc);
  159.  
  160.     futex_down(&plb_futex);
  161.     list_remove(&entry.plb_link);
  162.     /*
  163.      * Erasing the path from PLB will come handy for debugging purposes.
  164.      */
  165.     memset(&plb[first], 0, cnt1);
  166.     memset(plb, 0, cnt2);
  167.     futex_up(&plb_futex);
  168.  
  169.     if ((rc == EOK) && result) {
  170.         result->triplet.fs_handle = (int) IPC_GET_ARG1(answer);
  171.         result->triplet.dev_handle = (int) IPC_GET_ARG2(answer);
  172.         result->triplet.index = (int) IPC_GET_ARG3(answer);
  173.         result->size = (size_t) IPC_GET_ARG4(answer);
  174.         result->lnkcnt = (unsigned) IPC_GET_ARG5(answer);
  175.     }
  176.  
  177.     return rc;
  178. }
  179.  
  180. /**
  181.  * @}
  182.  */
  183.