Subversion Repositories HelenOS

Rev

Rev 2536 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2007 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.c
  35.  * @brief   VFS_REGISTER method.
  36.  */
  37.  
  38. #include <ipc/ipc.h>
  39. #include <ipc/services.h>
  40. #include <async.h>
  41. #include <errno.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <ctype.h>
  46. #include <bool.h>
  47. #include <futex.h>
  48. #include <as.h>
  49. #include <libadt/list.h>
  50. #include "vfs.h"
  51.  
  52. atomic_t fs_head_futex = FUTEX_INITIALIZER;
  53. link_t fs_head;
  54.  
  55. /** Verify the VFS info structure.
  56.  *
  57.  * @param info      Info structure to be verified.
  58.  *
  59.  * @return      Non-zero if the info structure is sane, zero otherwise.
  60.  */
  61. static bool vfs_info_sane(vfs_info_t *info)
  62. {
  63.     int i;
  64.  
  65.     /*
  66.      * Check if the name is non-empty and is composed solely of ASCII
  67.      * characters [a-z]+[a-z0-9_-]*.
  68.      */
  69.     if (!islower(info->name[0])) {
  70.         dprintf("The name doesn't start with a lowercase character.\n");
  71.         return false;
  72.     }
  73.     for (i = 1; i < FS_NAME_MAXLEN; i++) {
  74.         if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
  75.             (info->name[i] != '-') && (info->name[i] != '_')) {
  76.             if (info->name[i] == '\0') {
  77.                 break;
  78.             } else {
  79.                 dprintf("The name contains illegal "
  80.                     "characters.\n");
  81.                 return false;
  82.             }
  83.         }
  84.     }
  85.    
  86.  
  87.     /*
  88.      * Check if the FS implements mandatory VFS operations.
  89.      */
  90.     if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_REGISTER)] != VFS_OP_DEFINED) {
  91.         dprintf("Operation VFS_REGISTER not defined by the client.\n");
  92.         return false;
  93.     }
  94.     if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_MOUNT)] != VFS_OP_DEFINED) {
  95.         dprintf("Operation VFS_MOUNT not defined by the client.\n");
  96.         return false;
  97.     }
  98.     if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_UNMOUNT)] != VFS_OP_DEFINED) {
  99.         dprintf("Operation VFS_UNMOUNT not defined by the client.\n");
  100.         return false;
  101.     }
  102.     if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) {
  103.         dprintf("Operation VFS_LOOKUP not defined by the client.\n");
  104.         return false;
  105.     }
  106.     if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) {
  107.         dprintf("Operation VFS_OPEN not defined by the client.\n");
  108.         return false;
  109.     }
  110.     if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) {
  111.         dprintf("Operation VFS_CLOSE not defined by the client.\n");
  112.         return false;
  113.     }
  114.     if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
  115.         dprintf("Operation VFS_READ not defined by the client.\n");
  116.         return false;
  117.     }
  118.    
  119.     /*
  120.      * Check if each operation is either not defined, defined or default.
  121.      */
  122.     for (i = VFS_FIRST; i < VFS_LAST; i++) {
  123.         if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) &&
  124.             (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) &&
  125.             (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
  126.             dprintf("Operation info not understood.\n");
  127.             return false;
  128.         }
  129.     }
  130.     return true;
  131. }
  132.  
  133. /** VFS_REGISTER protocol function.
  134.  *
  135.  * @param rid       Hash of the call with the request.
  136.  * @param request   Call structure with the request.
  137.  */
  138. void vfs_register(ipc_callid_t rid, ipc_call_t *request)
  139. {
  140.     ipc_callid_t callid;
  141.     ipc_call_t call;
  142.     int rc;
  143.     size_t size;
  144.  
  145.     dprintf("Processing VFS_REGISTER request received from %p.\n",
  146.         request->in_phone_hash);
  147.  
  148.     /*
  149.      * The first call has to be IPC_M_DATA_SEND in which we receive the
  150.      * VFS info structure from the client FS.
  151.      */
  152.     if (!ipc_data_receive(&callid, &call, NULL, &size)) {
  153.         /*
  154.          * The client doesn't obey the same protocol as we do.
  155.          */
  156.         dprintf("Receiving of VFS info failed.\n");
  157.         ipc_answer_fast(callid, EINVAL, 0, 0);
  158.         ipc_answer_fast(rid, EINVAL, 0, 0);
  159.         return;
  160.     }
  161.    
  162.     dprintf("VFS info received, size = %d\n", size);
  163.    
  164.     /*
  165.      * We know the size of the VFS info structure. See if the client
  166.      * understands this easy concept too.
  167.      */
  168.     if (size != sizeof(vfs_info_t)) {
  169.         /*
  170.          * The client is sending us something, which cannot be
  171.          * the info structure.
  172.          */
  173.         dprintf("Received VFS info has bad size.\n");
  174.         ipc_answer_fast(callid, EINVAL, 0, 0);
  175.         ipc_answer_fast(rid, EINVAL, 0, 0);
  176.         return;
  177.     }
  178.  
  179.     /*
  180.      * Allocate and initialize a buffer for the fs_info structure.
  181.      */
  182.     fs_info_t *fs_info;
  183.     fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
  184.     if (!fs_info) {
  185.         dprintf("Could not allocate memory for FS info.\n");
  186.         ipc_answer_fast(callid, ENOMEM, 0, 0);
  187.         ipc_answer_fast(rid, ENOMEM, 0, 0);
  188.         return;
  189.     }
  190.     link_initialize(&fs_info->fs_link);
  191.        
  192.     rc = ipc_data_deliver(callid, &call, &fs_info->vfs_info, size);
  193.     if (rc != EOK) {
  194.         dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
  195.             rc);
  196.         free(fs_info);
  197.         ipc_answer_fast(callid, rc, 0, 0);
  198.         ipc_answer_fast(rid, rc, 0, 0);
  199.         return;
  200.     }
  201.  
  202.     dprintf("VFS info delivered.\n");
  203.        
  204.     if (!vfs_info_sane(&fs_info->vfs_info)) {
  205.         free(fs_info);
  206.         ipc_answer_fast(callid, EINVAL, 0, 0);
  207.         ipc_answer_fast(rid, EINVAL, 0, 0);
  208.         return;
  209.     }
  210.        
  211.     futex_down(&fs_head_futex);
  212.  
  213.     /*
  214.      * Check for duplicit registrations.
  215.      */
  216.     link_t *cur;
  217.     for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
  218.         fs_info_t *fi = list_get_instance(cur, fs_info_t,
  219.             fs_link);
  220.         /* TODO: replace strcmp with strncmp once we have it */
  221.         if (strcmp(fs_info->vfs_info.name, fi->vfs_info.name) == 0) {
  222.             /*
  223.              * We already register a fs like this.
  224.              */
  225.             dprintf("FS is already registered.\n");
  226.             futex_up(&fs_head_futex);
  227.             free(fs_info);
  228.             ipc_answer_fast(callid, EEXISTS, 0, 0);
  229.             ipc_answer_fast(rid, EEXISTS, 0, 0);
  230.             return;
  231.         }
  232.     }
  233.  
  234.     /*
  235.      * Add fs_info to the list of registered FS's.
  236.      */
  237.     dprintf("Adding FS into the registered list.\n");
  238.     list_append(&fs_info->fs_link, &fs_head);
  239.  
  240.     /*
  241.      * ACK receiving a properly formatted, non-duplicit vfs_info.
  242.      */
  243.     ipc_answer_fast(callid, EOK, 0, 0);
  244.    
  245.     /*
  246.      * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
  247.      * that a callback connection is created and we have a phone through
  248.      * which to forward VFS requests to it.
  249.      */
  250.     callid = async_get_call(&call);
  251.     if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
  252.         dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
  253.         list_remove(&fs_info->fs_link);
  254.         futex_up(&fs_head_futex);
  255.         free(fs_info);
  256.         ipc_answer_fast(callid, EINVAL, 0, 0);
  257.         ipc_answer_fast(rid, EINVAL, 0, 0);
  258.         return;
  259.     }
  260.     fs_info->phone = IPC_GET_ARG3(call);
  261.     ipc_answer_fast(callid, EOK, 0, 0);
  262.  
  263.     dprintf("Callback connection to FS created.\n");
  264.  
  265.     /*
  266.      * The client will want us to send him the address space area with PLB.
  267.      */
  268.     callid = async_get_call(&call);
  269.     if (IPC_GET_METHOD(call) != IPC_M_AS_AREA_RECV) {
  270.         dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
  271.         list_remove(&fs_info->fs_link);
  272.         futex_up(&fs_head_futex);
  273.         ipc_hangup(fs_info->phone);
  274.         free(fs_info);
  275.         ipc_answer_fast(callid, EINVAL, 0, 0);
  276.         ipc_answer_fast(rid, EINVAL, 0, 0);
  277.         return;
  278.     }
  279.    
  280.     /*
  281.      * We can only send the client address space area PLB_SIZE bytes long.
  282.      */
  283.     size = IPC_GET_ARG2(call);
  284.     if (size != PLB_SIZE) {
  285.         dprintf("Client suggests wrong size of PFB, size = %d\n", size);
  286.         list_remove(&fs_info->fs_link);
  287.         futex_up(&fs_head_futex);
  288.         ipc_hangup(fs_info->phone);
  289.         free(fs_info);
  290.         ipc_answer_fast(callid, EINVAL, 0, 0);
  291.         ipc_answer_fast(rid, EINVAL, 0, 0);
  292.         return;
  293.        
  294.     }
  295.  
  296.     /*
  297.      * Commit to read-only sharing the PLB with the client.
  298.      */
  299.     ipc_answer_fast(callid, EOK, (ipcarg_t) plb,
  300.         (ipcarg_t) (AS_AREA_READ | AS_AREA_CACHEABLE));
  301.  
  302.     dprintf("Sharing PLB.\n");
  303.  
  304.     futex_up(&fs_head_futex);
  305.  
  306.     /*
  307.      * That was it. The FS has been registered.
  308.      */
  309.     ipc_answer_fast(rid, EOK, 0, 0);
  310.     dprintf("\"%s\" filesystem successfully registered.\n",
  311.         fs_info->vfs_info.name);
  312.  
  313. }
  314.  
  315. /**
  316.  * @}
  317.  */
  318.