Subversion Repositories HelenOS

Rev

Rev 4377 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 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 libfs
  30.  * @{
  31.  */
  32. /**
  33.  * @file
  34.  * Glue code which is commonod to all FS implementations.
  35.  */
  36.  
  37. #include "libfs.h"
  38. #include "../../srv/vfs/vfs.h"
  39. #include <errno.h>
  40. #include <async.h>
  41. #include <ipc/ipc.h>
  42. #include <as.h>
  43. #include <assert.h>
  44. #include <dirent.h>
  45. #include <mem.h>
  46. #include <sys/stat.h>
  47.  
  48. /** Register file system server.
  49.  *
  50.  * This function abstracts away the tedious registration protocol from
  51.  * file system implementations and lets them to reuse this registration glue
  52.  * code.
  53.  *
  54.  * @param vfs_phone Open phone for communication with VFS.
  55.  * @param reg       File system registration structure. It will be
  56.  *          initialized by this function.
  57.  * @param info      VFS info structure supplied by the file system
  58.  *          implementation.
  59.  * @param conn      Connection fibril for handling all calls originating in
  60.  *          VFS.
  61.  *
  62.  * @return      EOK on success or a non-zero error code on errror.
  63.  */
  64. int fs_register(int vfs_phone, fs_reg_t *reg, vfs_info_t *info,
  65.     async_client_conn_t conn)
  66. {
  67.     /*
  68.      * Tell VFS that we are here and want to get registered.
  69.      * We use the async framework because VFS will answer the request
  70.      * out-of-order, when it knows that the operation succeeded or failed.
  71.      */
  72.     ipc_call_t answer;
  73.     aid_t req = async_send_0(vfs_phone, VFS_IN_REGISTER, &answer);
  74.  
  75.     /*
  76.      * Send our VFS info structure to VFS.
  77.      */
  78.     int rc = ipc_data_write_start(vfs_phone, info, sizeof(*info));
  79.     if (rc != EOK) {
  80.         async_wait_for(req, NULL);
  81.         return rc;
  82.     }
  83.  
  84.     /*
  85.      * Ask VFS for callback connection.
  86.      */
  87.     ipc_connect_to_me(vfs_phone, 0, 0, 0, &reg->vfs_phonehash);
  88.  
  89.     /*
  90.      * Allocate piece of address space for PLB.
  91.      */
  92.     reg->plb_ro = as_get_mappable_page(PLB_SIZE);
  93.     if (!reg->plb_ro) {
  94.         async_wait_for(req, NULL);
  95.         return ENOMEM;
  96.     }
  97.  
  98.     /*
  99.      * Request sharing the Path Lookup Buffer with VFS.
  100.      */
  101.     rc = ipc_share_in_start_0_0(vfs_phone, reg->plb_ro, PLB_SIZE);
  102.     if (rc) {
  103.         async_wait_for(req, NULL);
  104.         return rc;
  105.     }
  106.      
  107.     /*
  108.      * Pick up the answer for the request to the VFS_IN_REQUEST call.
  109.      */
  110.     async_wait_for(req, NULL);
  111.     reg->fs_handle = (int) IPC_GET_ARG1(answer);
  112.    
  113.     /*
  114.      * Create a connection fibril to handle the callback connection.
  115.      */
  116.     async_new_connection(reg->vfs_phonehash, 0, NULL, conn);
  117.    
  118.     /*
  119.      * Tell the async framework that other connections are to be handled by
  120.      * the same connection fibril as well.
  121.      */
  122.     async_set_client_connection(conn);
  123.  
  124.     return IPC_GET_RETVAL(answer);
  125. }
  126.  
  127. void fs_node_initialize(fs_node_t *fn)
  128. {
  129.     memset(fn, 0, sizeof(fs_node_t));
  130. }
  131.  
  132. void libfs_mount(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
  133.     ipc_call_t *request)
  134. {
  135.     dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
  136.     fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*request);
  137.     fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
  138.     dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
  139.     int res;
  140.     ipcarg_t rc;
  141.  
  142.     ipc_call_t call;
  143.     ipc_callid_t callid;
  144.  
  145.     /* accept the phone */
  146.     callid = async_get_call(&call);
  147.     int mountee_phone = (int)IPC_GET_ARG1(call);
  148.     if ((IPC_GET_METHOD(call) != IPC_M_CONNECTION_CLONE) ||
  149.         mountee_phone < 0) {
  150.         ipc_answer_0(callid, EINVAL);
  151.         ipc_answer_0(rid, EINVAL);
  152.         return;
  153.     }
  154.     ipc_answer_0(callid, EOK);  /* acknowledge the mountee_phone */
  155.    
  156.     res = ipc_data_write_receive(&callid, NULL);
  157.     if (!res) {
  158.         ipc_hangup(mountee_phone);
  159.         ipc_answer_0(callid, EINVAL);
  160.         ipc_answer_0(rid, EINVAL);
  161.         return;
  162.     }
  163.  
  164.     fs_node_t *fn = ops->node_get(mp_dev_handle, mp_fs_index);
  165.     if (!fn) {
  166.         ipc_hangup(mountee_phone);
  167.         ipc_answer_0(callid, ENOENT);
  168.         ipc_answer_0(rid, ENOENT);
  169.         return;
  170.     }
  171.  
  172.     if (fn->mp_data.mp_active) {
  173.         ipc_hangup(mountee_phone);
  174.         ops->node_put(fn);
  175.         ipc_answer_0(callid, EBUSY);
  176.         ipc_answer_0(rid, EBUSY);
  177.         return;
  178.     }
  179.  
  180.     rc = async_req_0_0(mountee_phone, IPC_M_CONNECT_ME);
  181.     if (rc != 0) {
  182.         ipc_hangup(mountee_phone);
  183.         ops->node_put(fn);
  184.         ipc_answer_0(callid, rc);
  185.         ipc_answer_0(rid, rc);
  186.         return;
  187.     }
  188.    
  189.     ipc_call_t answer;
  190.     aid_t msg = async_send_1(mountee_phone, VFS_OUT_MOUNTED, mr_dev_handle,
  191.         &answer);
  192.     ipc_forward_fast(callid, mountee_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
  193.     async_wait_for(msg, &rc);
  194.    
  195.     if (rc == EOK) {
  196.         fn->mp_data.mp_active = true;
  197.         fn->mp_data.fs_handle = mr_fs_handle;
  198.         fn->mp_data.dev_handle = mr_dev_handle;
  199.         fn->mp_data.phone = mountee_phone;
  200.     }
  201.     /*
  202.      * Do not release the FS node so that it stays in memory.
  203.      */
  204.     ipc_answer_3(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
  205.         IPC_GET_ARG3(answer));
  206. }
  207.  
  208. /** Lookup VFS triplet by name in the file system name space.
  209.  *
  210.  * The path passed in the PLB must be in the canonical file system path format
  211.  * as returned by the canonify() function.
  212.  *
  213.  * @param ops       libfs operations structure with function pointers to
  214.  *                  file system implementation
  215.  * @param fs_handle File system handle of the file system where to perform
  216.  *                  the lookup.
  217.  * @param rid       Request ID of the VFS_OUT_LOOKUP request.
  218.  * @param request   VFS_OUT_LOOKUP request data itself.
  219.  *
  220.  */
  221. void libfs_lookup(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
  222.     ipc_call_t *request)
  223. {
  224.     unsigned first = IPC_GET_ARG1(*request);
  225.     unsigned last = IPC_GET_ARG2(*request);
  226.     unsigned next = first;
  227.     dev_handle_t dev_handle = IPC_GET_ARG3(*request);
  228.     int lflag = IPC_GET_ARG4(*request);
  229.     fs_index_t index = IPC_GET_ARG5(*request); /* when L_LINK specified */
  230.     char component[NAME_MAX + 1];
  231.     int len;
  232.  
  233.     if (last < next)
  234.         last += PLB_SIZE;
  235.  
  236.     fs_node_t *par = NULL;
  237.     fs_node_t *cur = ops->root_get(dev_handle);
  238.     fs_node_t *tmp = NULL;
  239.  
  240.     if (cur->mp_data.mp_active) {
  241.         ipc_forward_slow(rid, cur->mp_data.phone, VFS_OUT_LOOKUP,
  242.             next, last, cur->mp_data.dev_handle, lflag, index,
  243.             IPC_FF_ROUTE_FROM_ME);
  244.         ops->node_put(cur);
  245.         return;
  246.     }
  247.  
  248.     if (ops->plb_get_char(next) == '/')
  249.         next++;     /* eat slash */
  250.    
  251.     while (next <= last && ops->has_children(cur)) {
  252.         /* collect the component */
  253.         len = 0;
  254.         while ((next <= last) &&  (ops->plb_get_char(next) != '/')) {
  255.             if (len + 1 == NAME_MAX) {
  256.                 /* component length overflow */
  257.                 ipc_answer_0(rid, ENAMETOOLONG);
  258.                 goto out;
  259.             }
  260.             component[len++] = ops->plb_get_char(next);
  261.             next++; /* process next character */
  262.         }
  263.  
  264.         assert(len);
  265.         component[len] = '\0';
  266.         next++;     /* eat slash */
  267.  
  268.         /* match the component */
  269.         tmp = ops->match(cur, component);
  270.         if (tmp && tmp->mp_data.mp_active) {
  271.             if (next > last)
  272.                 next = last = first;
  273.             else
  274.                 next--;
  275.                
  276.             ipc_forward_slow(rid, tmp->mp_data.phone,
  277.                 VFS_OUT_LOOKUP, next, last, tmp->mp_data.dev_handle,
  278.                 lflag, index, IPC_FF_ROUTE_FROM_ME);
  279.             ops->node_put(cur);
  280.             ops->node_put(tmp);
  281.             if (par)
  282.                 ops->node_put(par);
  283.             return;
  284.         }
  285.  
  286.         /* handle miss: match amongst siblings */
  287.         if (!tmp) {
  288.             if (next <= last) {
  289.                 /* there are unprocessed components */
  290.                 ipc_answer_0(rid, ENOENT);
  291.                 goto out;
  292.             }
  293.             /* miss in the last component */
  294.             if (lflag & (L_CREATE | L_LINK)) {
  295.                 /* request to create a new link */
  296.                 if (!ops->is_directory(cur)) {
  297.                     ipc_answer_0(rid, ENOTDIR);
  298.                     goto out;
  299.                 }
  300.                 fs_node_t *fn;
  301.                 if (lflag & L_CREATE)
  302.                     fn = ops->create(dev_handle, lflag);
  303.                 else
  304.                     fn = ops->node_get(dev_handle,
  305.                         index);
  306.                 if (fn) {
  307.                     int rc;
  308.  
  309.                     rc = ops->link(cur, fn, component);
  310.                     if (rc != EOK) {
  311.                         if (lflag & L_CREATE) {
  312.                             (void)ops->destroy(fn);
  313.                         }
  314.                         ipc_answer_0(rid, rc);
  315.                     } else {
  316.                         ipc_answer_5(rid, EOK,
  317.                             fs_handle, dev_handle,
  318.                             ops->index_get(fn),
  319.                             ops->size_get(fn),
  320.                             ops->lnkcnt_get(fn));
  321.                         ops->node_put(fn);
  322.                     }
  323.                 } else {
  324.                     ipc_answer_0(rid, ENOSPC);
  325.                 }
  326.                 goto out;
  327.             }
  328.             ipc_answer_0(rid, ENOENT);
  329.             goto out;
  330.         }
  331.  
  332.         if (par)
  333.             ops->node_put(par);
  334.  
  335.         /* descend one level */
  336.         par = cur;
  337.         cur = tmp;
  338.         tmp = NULL;
  339.     }
  340.  
  341.     /* handle miss: excessive components */
  342.     if (next <= last && !ops->has_children(cur)) {
  343.         if (lflag & (L_CREATE | L_LINK)) {
  344.             if (!ops->is_directory(cur)) {
  345.                 ipc_answer_0(rid, ENOTDIR);
  346.                 goto out;
  347.             }
  348.  
  349.             /* collect next component */
  350.             len = 0;
  351.             while (next <= last) {
  352.                 if (ops->plb_get_char(next) == '/') {
  353.                     /* more than one component */
  354.                     ipc_answer_0(rid, ENOENT);
  355.                     goto out;
  356.                 }
  357.                 if (len + 1 == NAME_MAX) {
  358.                     /* component length overflow */
  359.                     ipc_answer_0(rid, ENAMETOOLONG);
  360.                     goto out;
  361.                 }
  362.                 component[len++] = ops->plb_get_char(next);
  363.                 next++; /* process next character */
  364.             }
  365.             assert(len);
  366.             component[len] = '\0';
  367.                
  368.             fs_node_t *fn;
  369.             if (lflag & L_CREATE)
  370.                 fn = ops->create(dev_handle, lflag);
  371.             else
  372.                 fn = ops->node_get(dev_handle, index);
  373.             if (fn) {
  374.                 int rc;
  375.  
  376.                 rc = ops->link(cur, fn, component);
  377.                 if (rc != EOK) {
  378.                     if (lflag & L_CREATE)
  379.                         (void)ops->destroy(fn);
  380.                     ipc_answer_0(rid, rc);
  381.                 } else {
  382.                     ipc_answer_5(rid, EOK,
  383.                         fs_handle, dev_handle,
  384.                         ops->index_get(fn),
  385.                         ops->size_get(fn),
  386.                         ops->lnkcnt_get(fn));
  387.                     ops->node_put(fn);
  388.                 }
  389.             } else {
  390.                 ipc_answer_0(rid, ENOSPC);
  391.             }
  392.             goto out;
  393.         }
  394.         ipc_answer_0(rid, ENOENT);
  395.         goto out;
  396.     }
  397.  
  398.     /* handle hit */
  399.     if (lflag & L_UNLINK) {
  400.         unsigned old_lnkcnt = ops->lnkcnt_get(cur);
  401.         int res = ops->unlink(par, cur, component);
  402.         ipc_answer_5(rid, (ipcarg_t)res, fs_handle, dev_handle,
  403.             ops->index_get(cur), ops->size_get(cur), old_lnkcnt);
  404.         goto out;
  405.     }
  406.     if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
  407.         (lflag & L_LINK)) {
  408.         ipc_answer_0(rid, EEXIST);
  409.         goto out;
  410.     }
  411.     if ((lflag & L_FILE) && (ops->is_directory(cur))) {
  412.         ipc_answer_0(rid, EISDIR);
  413.         goto out;
  414.     }
  415.     if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
  416.         ipc_answer_0(rid, ENOTDIR);
  417.         goto out;
  418.     }
  419.  
  420.     ipc_answer_5(rid, EOK, fs_handle, dev_handle, ops->index_get(cur),
  421.         ops->size_get(cur), ops->lnkcnt_get(cur));
  422.  
  423. out:
  424.     if (par)
  425.         ops->node_put(par);
  426.     if (cur)
  427.         ops->node_put(cur);
  428.     if (tmp)
  429.         ops->node_put(tmp);
  430. }
  431.  
  432. void libfs_stat(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
  433.     ipc_call_t *request)
  434. {
  435.     dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
  436.     fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
  437.     fs_node_t *fn = ops->node_get(dev_handle, index);
  438.  
  439.     ipc_callid_t callid;
  440.     size_t size;
  441.     if (!ipc_data_read_receive(&callid, &size) ||
  442.         size != sizeof(struct stat)) {
  443.         ipc_answer_0(callid, EINVAL);
  444.         ipc_answer_0(rid, EINVAL);
  445.         return;
  446.     }
  447.  
  448.     struct stat stat;
  449.     memset(&stat, 0, sizeof(struct stat));
  450.    
  451.     stat.fs_handle = fs_handle;
  452.     stat.dev_handle = dev_handle;
  453.     stat.index = index;
  454.     stat.lnkcnt = ops->lnkcnt_get(fn);
  455.     stat.is_file = ops->is_file(fn);
  456.     stat.size = ops->size_get(fn);
  457.  
  458.     ipc_data_read_finalize(callid, &stat, sizeof(struct stat));
  459.     ipc_answer_0(rid, EOK);
  460. }
  461.  
  462. /** Open VFS triplet.
  463.  *
  464.  * @param ops       libfs operations structure with function pointers to
  465.  *                  file system implementation
  466.  * @param rid       Request ID of the VFS_OUT_OPEN_NODE request.
  467.  * @param request   VFS_OUT_OPEN_NODE request data itself.
  468.  *
  469.  */
  470. void libfs_open_node(libfs_ops_t *ops, fs_handle_t fs_handle, ipc_callid_t rid,
  471.     ipc_call_t *request)
  472. {
  473.     dev_handle_t dev_handle = IPC_GET_ARG1(*request);
  474.     fs_index_t index = IPC_GET_ARG2(*request);
  475.    
  476.     fs_node_t *node = ops->node_get(dev_handle, index);
  477.    
  478.     if (node == NULL) {
  479.         ipc_answer_0(rid, ENOENT);
  480.         return;
  481.     }
  482.    
  483.     ipc_answer_5(rid, EOK, fs_handle, dev_handle, index,
  484.         ops->size_get(node), ops->lnkcnt_get(node));
  485.    
  486.     ops->node_put(node);
  487. }
  488.  
  489. /** @}
  490.  */
  491.