Subversion Repositories HelenOS

Rev

Rev 2700 | Rev 2710 | 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 libc
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <vfs.h>
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <dirent.h>
  39. #include <fcntl.h>
  40. #include <sys/stat.h>
  41. #include <sys/types.h>
  42. #include <ipc/ipc.h>
  43. #include <ipc/services.h>
  44. #include <async.h>
  45. #include <atomic.h>
  46. #include <futex.h>
  47. #include <errno.h>
  48. #include <string.h>
  49. #include "../../srv/vfs/vfs.h"
  50.  
  51. int vfs_phone = -1;
  52. atomic_t vfs_phone_futex = FUTEX_INITIALIZER;
  53.  
  54. static int vfs_connect(void)
  55. {
  56.     if (vfs_phone < 0)
  57.         vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0);
  58.     return vfs_phone;
  59. }
  60.  
  61. int mount(const char *fs_name, const char *mp, const char *dev)
  62. {
  63.     int res;
  64.     ipcarg_t rc;
  65.     aid_t req;
  66.  
  67.     int dev_handle = 0; /* TODO */
  68.  
  69.     futex_down(&vfs_phone_futex);
  70.     async_serialize_start();
  71.     if (vfs_phone < 0) {
  72.         res = vfs_connect();
  73.         if (res < 0) {
  74.             async_serialize_end();
  75.             futex_up(&vfs_phone_futex);
  76.             return res;
  77.         }
  78.     }
  79.     req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL);
  80.     rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name));
  81.     if (rc != EOK) {
  82.         async_wait_for(req, NULL);
  83.         async_serialize_end();
  84.         futex_up(&vfs_phone_futex);
  85.         return (int) rc;
  86.     }
  87.     rc = ipc_data_write_start(vfs_phone, (void *)mp, strlen(mp));
  88.     if (rc != EOK) {
  89.         async_wait_for(req, NULL);
  90.         async_serialize_end();
  91.         futex_up(&vfs_phone_futex);
  92.         return (int) rc;
  93.     }
  94.     async_wait_for(req, &rc);
  95.     async_serialize_end();
  96.     futex_up(&vfs_phone_futex);
  97.     return (int) rc;
  98. }
  99.  
  100. static int _open(const char *path, int lflag, int oflag, ...)
  101. {
  102.     int res;
  103.     ipcarg_t rc;
  104.     ipc_call_t answer;
  105.     aid_t req;
  106.    
  107.     futex_down(&vfs_phone_futex);
  108.     async_serialize_start();
  109.     if (vfs_phone < 0) {
  110.         res = vfs_connect();
  111.         if (res < 0) {
  112.             async_serialize_end();
  113.             futex_up(&vfs_phone_futex);
  114.             return res;
  115.         }
  116.     }
  117.     req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer);
  118.     rc = ipc_data_write_start(vfs_phone, path, strlen(path));
  119.     if (rc != EOK) {
  120.         async_wait_for(req, NULL);
  121.         async_serialize_end();
  122.         futex_up(&vfs_phone_futex);
  123.         return (int) rc;
  124.     }
  125.     async_wait_for(req, &rc);
  126.     async_serialize_end();
  127.     futex_up(&vfs_phone_futex);
  128.     return (int) IPC_GET_ARG1(answer);
  129. }
  130.  
  131. int open(const char *path, int oflag, ...)
  132. {
  133.     return _open(path, L_FILE, oflag);
  134. }
  135.  
  136. int close(int fildes)
  137. {
  138.     return 0;   /* TODO */
  139. }
  140.  
  141. ssize_t read(int fildes, void *buf, size_t nbyte)
  142. {
  143.     int res;
  144.     ipcarg_t rc;
  145.     ipc_call_t answer;
  146.     aid_t req;
  147.  
  148.     futex_down(&vfs_phone_futex);
  149.     async_serialize_start();
  150.     if (vfs_phone < 0) {
  151.         res = vfs_connect();
  152.         if (res < 0) {
  153.             async_serialize_end();
  154.             futex_up(&vfs_phone_futex);
  155.             return res;
  156.         }
  157.     }
  158.     req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
  159.     if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) {
  160.         async_wait_for(req, NULL);
  161.         async_serialize_end();
  162.         futex_up(&vfs_phone_futex);
  163.         return (ssize_t) rc;
  164.     }
  165.     async_wait_for(req, &rc);
  166.     async_serialize_end();
  167.     futex_up(&vfs_phone_futex);
  168.     return (ssize_t) IPC_GET_ARG1(answer);
  169. }
  170.  
  171. ssize_t write(int fildes, const void *buf, size_t nbyte)
  172. {
  173.     int res;
  174.     ipcarg_t rc;
  175.     ipc_call_t answer;
  176.     aid_t req;
  177.  
  178.     futex_down(&vfs_phone_futex);
  179.     async_serialize_start();
  180.     if (vfs_phone < 0) {
  181.         res = vfs_connect();
  182.         if (res < 0) {
  183.             async_serialize_end();
  184.             futex_up(&vfs_phone_futex);
  185.             return res;
  186.         }
  187.     }
  188.     req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
  189.     if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) {
  190.         async_wait_for(req, NULL);
  191.         async_serialize_end();
  192.         futex_up(&vfs_phone_futex);
  193.         return (ssize_t) rc;
  194.     }
  195.     async_wait_for(req, &rc);
  196.     async_serialize_end();
  197.     futex_up(&vfs_phone_futex);
  198.     return (ssize_t) IPC_GET_ARG1(answer);
  199. }
  200.  
  201. off_t lseek(int fildes, off_t offset, int whence)
  202. {
  203.     int res;
  204.     ipcarg_t rc;
  205.  
  206.     futex_down(&vfs_phone_futex);
  207.     async_serialize_start();
  208.     if (vfs_phone < 0) {
  209.         res = vfs_connect();
  210.         if (res < 0) {
  211.             async_serialize_end();
  212.             futex_up(&vfs_phone_futex);
  213.             return res;
  214.         }
  215.     }
  216.        
  217.     off_t newoffs;
  218.     rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
  219.         (ipcarg_t)&newoffs);
  220.  
  221.     async_serialize_end();
  222.     futex_up(&vfs_phone_futex);
  223.  
  224.     if (rc != EOK)
  225.         return (off_t) -1;
  226.    
  227.     return newoffs;
  228. }
  229.  
  230. int ftruncate(int fildes, off_t length)
  231. {
  232.     int res;
  233.     ipcarg_t rc;
  234.    
  235.     futex_down(&vfs_phone_futex);
  236.     async_serialize_start();
  237.     if (vfs_phone < 0) {
  238.         res = vfs_connect();
  239.         if (res < 0) {
  240.             async_serialize_end();
  241.             futex_up(&vfs_phone_futex);
  242.             return res;
  243.         }
  244.     }
  245.     rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
  246.     async_serialize_end();
  247.     futex_up(&vfs_phone_futex);
  248.     return (int) rc;
  249. }
  250.  
  251. DIR *opendir(const char *dirname)
  252. {
  253.     DIR *dirp = malloc(sizeof(DIR));
  254.     if (!dirp)
  255.         return NULL;
  256.     dirp->fd = _open(dirname, L_DIRECTORY, 0);
  257.     if (dirp->fd < 0) {
  258.         free(dirp);
  259.         return NULL;
  260.     }
  261.     return dirp;
  262. }
  263.  
  264. struct dirent *readdir(DIR *dirp)
  265. {
  266.     ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
  267.     if (len <= 0)
  268.         return NULL;
  269.     return &dirp->res;
  270. }
  271.  
  272. void rewinddir(DIR *dirp)
  273. {
  274.     (void) lseek(dirp->fd, 0, SEEK_SET);
  275. }
  276.  
  277. int closedir(DIR *dirp)
  278. {
  279.     (void) close(dirp->fd);
  280.     free(dirp);
  281.     return 0;
  282. }
  283.  
  284. int mkdir(const char *path, mode_t mode)
  285. {
  286.     int res;
  287.     ipcarg_t rc;
  288.     ipc_call_t answer;
  289.     aid_t req;
  290.    
  291.     futex_down(&vfs_phone_futex);
  292.     async_serialize_start();
  293.     if (vfs_phone < 0) {
  294.         res = vfs_connect();
  295.         if (res < 0) {
  296.             async_serialize_end();
  297.             futex_up(&vfs_phone_futex);
  298.             return res;
  299.         }
  300.     }
  301.     req = async_send_1(vfs_phone, VFS_MKDIR, mode, &answer);
  302.     rc = ipc_data_write_start(vfs_phone, path, strlen(path));
  303.     if (rc != EOK) {
  304.         async_wait_for(req, NULL);
  305.         async_serialize_end();
  306.         futex_up(&vfs_phone_futex);
  307.         return (int) rc;
  308.     }
  309.     async_wait_for(req, &rc);
  310.     async_serialize_end();
  311.     futex_up(&vfs_phone_futex);
  312.     return EOK;
  313. }
  314.  
  315. /** @}
  316.  */
  317.