Subversion Repositories HelenOS

Rev

Rev 2635 | 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_read.c
  35.  * @brief
  36.  */
  37.  
  38. #include "vfs.h"
  39. #include <ipc/ipc.h>
  40. #include <async.h>
  41. #include <errno.h>
  42.  
  43. void vfs_read(ipc_callid_t rid, ipc_call_t *request)
  44. {
  45.  
  46.     /*
  47.      * The following code strongly depends on the fact that the files data
  48.      * structure can be only accessed by a single fibril and all file
  49.      * operations are serialized (i.e. the reads and writes cannot
  50.      * interleave and a file cannot be closed while it is being read).
  51.      *
  52.      * Additional synchronization needs to be added once the table of
  53.      * open files supports parallel access!
  54.      */
  55.  
  56.     /*
  57.      * Because we don't support the receive analogy of IPC_M_DATA_SEND,
  58.      * VFS_READ is emulutating its behavior via sharing an address space
  59.      * area.
  60.      */
  61.  
  62.     int fd = IPC_GET_ARG1(*request);
  63.     size_t size = IPC_GET_ARG2(*request);
  64.  
  65.     /*
  66.      * Lookup the file structure corresponding to the file descriptor.
  67.      */
  68.     vfs_file_t *file = vfs_file_get(fd);
  69.     if (!file) {
  70.         ipc_answer_0(rid, ENOENT);
  71.         return;
  72.     }
  73.  
  74.     /*
  75.      * Now we need to receive a call with client's address space area.
  76.      */
  77.     ipc_callid_t callid;
  78.     ipc_call_t call;
  79.     callid = async_get_call(&call);
  80.     if (IPC_GET_METHOD(call) != IPC_M_AS_AREA_SEND) {
  81.         ipc_answer_0(callid, EINVAL);
  82.         ipc_answer_0(rid, EINVAL);
  83.         return;
  84.     }
  85.  
  86.     int fs_phone = vfs_grab_phone(file->node->fs_handle);  
  87.    
  88.     /*
  89.      * Make a VFS_READ request at the destination FS server.
  90.      */
  91.     aid_t msg;
  92.     ipc_call_t answer;
  93.     msg = async_send_4(fs_phone, VFS_READ, file->node->dev_handle,
  94.         file->node->index, file->pos, size, &answer);
  95.    
  96.     /*
  97.      * Forward the address space area offer to the destination FS server.
  98.      * The call will be routed as if sent by ourselves.
  99.      */
  100.     ipc_forward_fast(callid, fs_phone, IPC_GET_METHOD(call),
  101.         IPC_GET_ARG1(call), 0, IPC_FF_ROUTE_FROM_ME);
  102.  
  103.     vfs_release_phone(fs_phone);
  104.  
  105.     /*
  106.      * Wait for reply from the FS server.
  107.      */
  108.     ipcarg_t rc;
  109.     async_wait_for(msg, &rc);
  110.     size_t bytes = IPC_GET_ARG1(answer);
  111.  
  112.     /*
  113.      * FS server's reply is the final result of the whole operation we
  114.      * return to the client.
  115.      */
  116.     ipc_answer_1(rid, rc, bytes);
  117. }
  118.  
  119. /**
  120.  * @}
  121.  */
  122.