Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2009 Jiri Svoboda
  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 bd
  30.  * @{
  31.  */
  32.  
  33. /**
  34.  * @file
  35.  * @brief File-backed block device driver
  36.  *
  37.  * Allows accessing a file as a block device. Useful for, e.g., mounting
  38.  * a disk image.
  39.  */
  40.  
  41. #include <stdio.h>
  42. #include <unistd.h>
  43. #include <ipc/ipc.h>
  44. #include <ipc/bd.h>
  45. #include <async.h>
  46. #include <as.h>
  47. #include <fibril_sync.h>
  48. #include <devmap.h>
  49. #include <sys/types.h>
  50. #include <errno.h>
  51. #include <bool.h>
  52.  
  53. #define NAME "file_bd"
  54.  
  55. static size_t comm_size;
  56. static FILE *img;
  57.  
  58. static dev_handle_t dev_handle;
  59. static fibril_mutex_t dev_lock;
  60.  
  61. static int file_bd_init(const char *fname);
  62. static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
  63. static int file_bd_read(off_t blk_idx, size_t size, void *buf);
  64. static int file_bd_write(off_t blk_idx, size_t size, void *buf);
  65.  
  66. int main(int argc, char **argv)
  67. {
  68.     int rc;
  69.  
  70.     printf(NAME ": File-backed block device driver\n");
  71.  
  72.     if (argc != 3) {
  73.         printf("Expected two arguments (image name, device name).\n");
  74.         return -1;
  75.     }
  76.  
  77.     if (file_bd_init(argv[1]) != EOK)
  78.         return -1;
  79.  
  80.     rc = devmap_device_register(argv[2], &dev_handle);
  81.     if (rc != EOK) {
  82.         devmap_hangup_phone(DEVMAP_DRIVER);
  83.         printf(NAME ": Unable to register device %s.\n",
  84.             argv[2]);
  85.         return rc;
  86.     }
  87.  
  88.     printf(NAME ": Accepting connections\n");
  89.     async_manager();
  90.  
  91.     /* Not reached */
  92.     return 0;
  93. }
  94.  
  95. static int file_bd_init(const char *fname)
  96. {
  97.     int rc;
  98.  
  99.     rc = devmap_driver_register(NAME, file_bd_connection);
  100.     if (rc < 0) {
  101.         printf(NAME ": Unable to register driver.\n");
  102.         return rc;
  103.     }
  104.  
  105.     img = fopen(fname, "rb+");
  106.     if (img == NULL)
  107.         return EINVAL;
  108.  
  109.     fibril_mutex_initialize(&dev_lock);
  110.  
  111.     return EOK;
  112. }
  113.  
  114. static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
  115. {
  116.     void *fs_va = NULL;
  117.     ipc_callid_t callid;
  118.     ipc_call_t call;
  119.     ipcarg_t method;
  120.     int flags;
  121.     int retval;
  122.     off_t idx;
  123.     size_t size;
  124.  
  125.     /* Answer the IPC_M_CONNECT_ME_TO call. */
  126.     ipc_answer_0(iid, EOK);
  127.  
  128.     if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
  129.         ipc_answer_0(callid, EHANGUP);
  130.         return;
  131.     }
  132.  
  133.     fs_va = as_get_mappable_page(comm_size);
  134.     if (fs_va == NULL) {
  135.         ipc_answer_0(callid, EHANGUP);
  136.         return;
  137.     }
  138.  
  139.     (void) ipc_share_out_finalize(callid, fs_va);
  140.  
  141.     while (1) {
  142.         callid = async_get_call(&call);
  143.         method = IPC_GET_METHOD(call);
  144.         switch (method) {
  145.         case IPC_M_PHONE_HUNGUP:
  146.             /* The other side has hung up. */
  147.             ipc_answer_0(callid, EOK);
  148.             return;
  149.         case BD_READ_BLOCK:
  150.         case BD_WRITE_BLOCK:
  151.             idx = IPC_GET_ARG1(call);
  152.             size = IPC_GET_ARG2(call);
  153.             if (size > comm_size) {
  154.                 retval = EINVAL;
  155.                 break;
  156.             }
  157.             if (method == BD_READ_BLOCK)
  158.                 retval = file_bd_read(idx, size, fs_va);
  159.             else
  160.                 retval = file_bd_write(idx, size, fs_va);
  161.             break;
  162.         default:
  163.             retval = EINVAL;
  164.             break;
  165.         }
  166.         ipc_answer_0(callid, retval);
  167.     }
  168. }
  169.  
  170. static int file_bd_read(off_t blk_idx, size_t size, void *buf)
  171. {
  172.     size_t n_rd;
  173.  
  174.     fibril_mutex_lock(&dev_lock);
  175.  
  176.     fseek(img, blk_idx * size, SEEK_SET);
  177.     n_rd = fread(buf, 1, size, img);
  178.  
  179.     if (ferror(img)) {
  180.         fibril_mutex_unlock(&dev_lock);
  181.         return EIO; /* Read error */
  182.     }
  183.  
  184.     fibril_mutex_unlock(&dev_lock);
  185.  
  186.     if (n_rd < size)
  187.         return EINVAL;  /* Read beyond end of disk */
  188.  
  189.     return EOK;
  190. }
  191.  
  192. static int file_bd_write(off_t blk_idx, size_t size, void *buf)
  193. {
  194.     size_t n_wr;
  195.  
  196.     fibril_mutex_lock(&dev_lock);
  197.  
  198.     fseek(img, blk_idx * size, SEEK_SET);
  199.     n_wr = fread(buf, 1, size, img);
  200.  
  201.     if (ferror(img) || n_wr < size) {
  202.         fibril_mutex_unlock(&dev_lock);
  203.         return EIO; /* Write error */
  204.     }
  205.  
  206.     fibril_mutex_unlock(&dev_lock);
  207.  
  208.     return EOK;
  209. }
  210.  
  211. /**
  212.  * @}
  213.  */
  214.