| 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 fs
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file vfs.c
|
|---|
| 35 | * @brief VFS service for HelenOS.
|
|---|
| 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 <bool.h>
|
|---|
| 44 | #include <string.h>
|
|---|
| 45 | #include <as.h>
|
|---|
| 46 | #include <libadt/list.h>
|
|---|
| 47 | #include <atomic.h>
|
|---|
| 48 | #include "vfs.h"
|
|---|
| 49 |
|
|---|
| 50 | #define NAME "vfs"
|
|---|
| 51 |
|
|---|
| 52 | static void vfs_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 53 | {
|
|---|
| 54 | bool keep_on_going = true;
|
|---|
| 55 |
|
|---|
| 56 | /*
|
|---|
| 57 | * The connection was opened via the IPC_CONNECT_ME_TO call.
|
|---|
| 58 | * This call needs to be answered.
|
|---|
| 59 | */
|
|---|
| 60 | ipc_answer_0(iid, EOK);
|
|---|
| 61 |
|
|---|
| 62 | /*
|
|---|
| 63 | * Here we enter the main connection fibril loop.
|
|---|
| 64 | * The logic behind this loop and the protocol is that we'd like to keep
|
|---|
| 65 | * each connection open until the client hangs up. When the client hangs
|
|---|
| 66 | * up, we will free its VFS state. The act of hanging up the connection
|
|---|
| 67 | * by the client is equivalent to client termination because we cannot
|
|---|
| 68 | * distinguish one from the other. On the other hand, the client can
|
|---|
| 69 | * hang up arbitrarily if it has no open files and reestablish the
|
|---|
| 70 | * connection later.
|
|---|
| 71 | */
|
|---|
| 72 | while (keep_on_going) {
|
|---|
| 73 | ipc_call_t call;
|
|---|
| 74 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 75 |
|
|---|
| 76 | fs_handle_t fs_handle;
|
|---|
| 77 | int phone;
|
|---|
| 78 |
|
|---|
| 79 | switch (IPC_GET_METHOD(call)) {
|
|---|
| 80 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 81 | keep_on_going = false;
|
|---|
| 82 | break;
|
|---|
| 83 | case IPC_M_CONNECT_ME_TO:
|
|---|
| 84 | /*
|
|---|
| 85 | * Connect the client file system to another one.
|
|---|
| 86 | */
|
|---|
| 87 | /* FIXME:
|
|---|
| 88 | * Prevent ordinary clients from connecting to file
|
|---|
| 89 | * system servers directly. This should be solved by
|
|---|
| 90 | * applying some security mechanisms.
|
|---|
| 91 | */
|
|---|
| 92 | fs_handle = IPC_GET_ARG1(call);
|
|---|
| 93 | phone = vfs_grab_phone(fs_handle);
|
|---|
| 94 | (void) ipc_forward_fast(callid, phone, 0, 0, 0,
|
|---|
| 95 | IPC_FF_NONE);
|
|---|
| 96 | vfs_release_phone(phone);
|
|---|
| 97 | break;
|
|---|
| 98 | case VFS_REGISTER:
|
|---|
| 99 | vfs_register(callid, &call);
|
|---|
| 100 | /*
|
|---|
| 101 | * Keep the connection open so that a file system can
|
|---|
| 102 | * later ask us to connect it to another file system.
|
|---|
| 103 | * This is necessary to support non-root mounts.
|
|---|
| 104 | */
|
|---|
| 105 | break;
|
|---|
| 106 | case VFS_MOUNT:
|
|---|
| 107 | vfs_mount(callid, &call);
|
|---|
| 108 | break;
|
|---|
| 109 | case VFS_OPEN:
|
|---|
| 110 | vfs_open(callid, &call);
|
|---|
| 111 | break;
|
|---|
| 112 | case VFS_CLOSE:
|
|---|
| 113 | vfs_close(callid, &call);
|
|---|
| 114 | break;
|
|---|
| 115 | case VFS_READ:
|
|---|
| 116 | vfs_read(callid, &call);
|
|---|
| 117 | break;
|
|---|
| 118 | case VFS_WRITE:
|
|---|
| 119 | vfs_write(callid, &call);
|
|---|
| 120 | break;
|
|---|
| 121 | case VFS_SEEK:
|
|---|
| 122 | vfs_seek(callid, &call);
|
|---|
| 123 | break;
|
|---|
| 124 | case VFS_TRUNCATE:
|
|---|
| 125 | vfs_truncate(callid, &call);
|
|---|
| 126 | break;
|
|---|
| 127 | case VFS_MKDIR:
|
|---|
| 128 | vfs_mkdir(callid, &call);
|
|---|
| 129 | break;
|
|---|
| 130 | case VFS_UNLINK:
|
|---|
| 131 | vfs_unlink(callid, &call);
|
|---|
| 132 | break;
|
|---|
| 133 | case VFS_RENAME:
|
|---|
| 134 | vfs_rename(callid, &call);
|
|---|
| 135 | break;
|
|---|
| 136 | default:
|
|---|
| 137 | ipc_answer_0(callid, ENOTSUP);
|
|---|
| 138 | break;
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /* TODO: cleanup after the client */
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | int main(int argc, char **argv)
|
|---|
| 146 | {
|
|---|
| 147 | printf(NAME ": HelenOS VFS server\n");
|
|---|
| 148 |
|
|---|
| 149 | /*
|
|---|
| 150 | * Initialize the list of registered file systems.
|
|---|
| 151 | */
|
|---|
| 152 | list_initialize(&fs_head);
|
|---|
| 153 |
|
|---|
| 154 | /*
|
|---|
| 155 | * Initialize VFS node hash table.
|
|---|
| 156 | */
|
|---|
| 157 | if (!vfs_nodes_init()) {
|
|---|
| 158 | printf(NAME ": Failed to initialize VFS node hash table\n");
|
|---|
| 159 | return ENOMEM;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /*
|
|---|
| 163 | * Allocate and initialize the Path Lookup Buffer.
|
|---|
| 164 | */
|
|---|
| 165 | list_initialize(&plb_head);
|
|---|
| 166 | plb = as_get_mappable_page(PLB_SIZE);
|
|---|
| 167 | if (!plb) {
|
|---|
| 168 | printf(NAME ": Cannot allocate a mappable piece of address space\n");
|
|---|
| 169 | return ENOMEM;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | if (as_area_create(plb, PLB_SIZE, AS_AREA_READ | AS_AREA_WRITE |
|
|---|
| 173 | AS_AREA_CACHEABLE) != plb) {
|
|---|
| 174 | printf(NAME ": Cannot create address space area\n");
|
|---|
| 175 | return ENOMEM;
|
|---|
| 176 | }
|
|---|
| 177 | memset(plb, 0, PLB_SIZE);
|
|---|
| 178 |
|
|---|
| 179 | /*
|
|---|
| 180 | * Set a connectio handling function/fibril.
|
|---|
| 181 | */
|
|---|
| 182 | async_set_client_connection(vfs_connection);
|
|---|
| 183 |
|
|---|
| 184 | /*
|
|---|
| 185 | * Register at the naming service.
|
|---|
| 186 | */
|
|---|
| 187 | ipcarg_t phonead;
|
|---|
| 188 | ipc_connect_to_me(PHONE_NS, SERVICE_VFS, 0, 0, &phonead);
|
|---|
| 189 |
|
|---|
| 190 | /*
|
|---|
| 191 | * Start accepting connections.
|
|---|
| 192 | */
|
|---|
| 193 | printf(NAME ": Accepting connections\n");
|
|---|
| 194 | async_manager();
|
|---|
| 195 | return 0;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /**
|
|---|
| 199 | * @}
|
|---|
| 200 | */
|
|---|