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 <vfs/vfs.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <ipc/services.h>
|
---|
41 | #include <abi/ipc/methods.h>
|
---|
42 | #include <libarch/config.h>
|
---|
43 | #include <ns.h>
|
---|
44 | #include <async.h>
|
---|
45 | #include <errno.h>
|
---|
46 | #include <stdio.h>
|
---|
47 | #include <stdbool.h>
|
---|
48 | #include <str.h>
|
---|
49 | #include <as.h>
|
---|
50 | #include <atomic.h>
|
---|
51 | #include <macros.h>
|
---|
52 | #include "vfs.h"
|
---|
53 |
|
---|
54 | #define NAME "vfs"
|
---|
55 |
|
---|
56 | static void vfs_pager(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
57 | {
|
---|
58 | async_answer_0(iid, EOK);
|
---|
59 |
|
---|
60 | while (true) {
|
---|
61 | ipc_call_t call;
|
---|
62 | ipc_callid_t callid = async_get_call(&call);
|
---|
63 |
|
---|
64 | if (!IPC_GET_IMETHOD(call))
|
---|
65 | break;
|
---|
66 |
|
---|
67 | switch (IPC_GET_IMETHOD(call)) {
|
---|
68 | case IPC_M_PAGE_IN:
|
---|
69 | vfs_page_in(callid, &call);
|
---|
70 | break;
|
---|
71 | default:
|
---|
72 | async_answer_0(callid, ENOTSUP);
|
---|
73 | break;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | static void vfs_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
79 | {
|
---|
80 | bool cont = true;
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * The connection was opened via the IPC_CONNECT_ME_TO call.
|
---|
84 | * This call needs to be answered.
|
---|
85 | */
|
---|
86 | async_answer_0(iid, EOK);
|
---|
87 |
|
---|
88 | while (cont) {
|
---|
89 | ipc_call_t call;
|
---|
90 | ipc_callid_t callid = async_get_call(&call);
|
---|
91 |
|
---|
92 | if (!IPC_GET_IMETHOD(call))
|
---|
93 | break;
|
---|
94 |
|
---|
95 | switch (IPC_GET_IMETHOD(call)) {
|
---|
96 | case VFS_IN_REGISTER:
|
---|
97 | vfs_register(callid, &call);
|
---|
98 | cont = false;
|
---|
99 | break;
|
---|
100 | case VFS_IN_MOUNT:
|
---|
101 | vfs_mount_srv(callid, &call);
|
---|
102 | break;
|
---|
103 | case VFS_IN_UNMOUNT:
|
---|
104 | vfs_unmount_srv(callid, &call);
|
---|
105 | break;
|
---|
106 | case VFS_IN_OPEN:
|
---|
107 | vfs_open(callid, &call);
|
---|
108 | break;
|
---|
109 | case VFS_IN_CLOSE:
|
---|
110 | vfs_close(callid, &call);
|
---|
111 | break;
|
---|
112 | case VFS_IN_READ:
|
---|
113 | vfs_read(callid, &call);
|
---|
114 | break;
|
---|
115 | case VFS_IN_WRITE:
|
---|
116 | vfs_write(callid, &call);
|
---|
117 | break;
|
---|
118 | case VFS_IN_SEEK:
|
---|
119 | vfs_seek(callid, &call);
|
---|
120 | break;
|
---|
121 | case VFS_IN_TRUNCATE:
|
---|
122 | vfs_truncate(callid, &call);
|
---|
123 | break;
|
---|
124 | case VFS_IN_FSTAT:
|
---|
125 | vfs_fstat(callid, &call);
|
---|
126 | break;
|
---|
127 | case VFS_IN_STAT:
|
---|
128 | vfs_stat(callid, &call);
|
---|
129 | break;
|
---|
130 | case VFS_IN_MKDIR:
|
---|
131 | vfs_mkdir(callid, &call);
|
---|
132 | break;
|
---|
133 | case VFS_IN_UNLINK:
|
---|
134 | vfs_unlink(callid, &call);
|
---|
135 | break;
|
---|
136 | case VFS_IN_RENAME:
|
---|
137 | vfs_rename(callid, &call);
|
---|
138 | break;
|
---|
139 | case VFS_IN_SYNC:
|
---|
140 | vfs_sync(callid, &call);
|
---|
141 | break;
|
---|
142 | case VFS_IN_DUP:
|
---|
143 | vfs_dup(callid, &call);
|
---|
144 | break;
|
---|
145 | case VFS_IN_WAIT_HANDLE:
|
---|
146 | vfs_wait_handle(callid, &call);
|
---|
147 | break;
|
---|
148 | case VFS_IN_MTAB_GET:
|
---|
149 | vfs_get_mtab(callid, &call);
|
---|
150 | break;
|
---|
151 | case VFS_IN_STATFS:
|
---|
152 | vfs_statfs(callid, &call);
|
---|
153 | break;
|
---|
154 | default:
|
---|
155 | async_answer_0(callid, ENOTSUP);
|
---|
156 | break;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Open files for this client will be cleaned up when its last
|
---|
162 | * connection fibril terminates.
|
---|
163 | */
|
---|
164 | }
|
---|
165 |
|
---|
166 | static void notification_handler(ipc_callid_t callid, ipc_call_t *call, void *arg)
|
---|
167 | {
|
---|
168 | if (IPC_GET_ARG1(*call) == VFS_PASS_HANDLE)
|
---|
169 | vfs_pass_handle(
|
---|
170 | (task_id_t) MERGE_LOUP32(IPC_GET_ARG4(*call),
|
---|
171 | IPC_GET_ARG5(*call)), call->in_task_id,
|
---|
172 | (int) IPC_GET_ARG2(*call));
|
---|
173 | }
|
---|
174 |
|
---|
175 | int main(int argc, char **argv)
|
---|
176 | {
|
---|
177 | int rc;
|
---|
178 |
|
---|
179 | printf("%s: HelenOS VFS server\n", NAME);
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Initialize VFS node hash table.
|
---|
183 | */
|
---|
184 | if (!vfs_nodes_init()) {
|
---|
185 | printf("%s: Failed to initialize VFS node hash table\n",
|
---|
186 | NAME);
|
---|
187 | return ENOMEM;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Allocate and initialize the Path Lookup Buffer.
|
---|
192 | */
|
---|
193 | plb = as_area_create(AS_AREA_ANY, PLB_SIZE,
|
---|
194 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
|
---|
195 | if (plb == AS_MAP_FAILED) {
|
---|
196 | printf("%s: Cannot create address space area\n", NAME);
|
---|
197 | return ENOMEM;
|
---|
198 | }
|
---|
199 | memset(plb, 0, PLB_SIZE);
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * Set client data constructor and destructor.
|
---|
203 | */
|
---|
204 | async_set_client_data_constructor(vfs_client_data_create);
|
---|
205 | async_set_client_data_destructor(vfs_client_data_destroy);
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Create a port for the pager.
|
---|
209 | */
|
---|
210 | port_id_t port;
|
---|
211 | rc = async_create_port(INTERFACE_PAGER, vfs_pager, NULL, &port);
|
---|
212 | if (rc != EOK)
|
---|
213 | return rc;
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * Set a connection handling function/fibril.
|
---|
217 | */
|
---|
218 | async_set_fallback_port_handler(vfs_connection, NULL);
|
---|
219 |
|
---|
220 | /*
|
---|
221 | * Subscribe to notifications.
|
---|
222 | */
|
---|
223 | async_event_task_subscribe(EVENT_TASK_STATE_CHANGE, notification_handler,
|
---|
224 | NULL);
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * Register at the naming service.
|
---|
228 | */
|
---|
229 | rc = service_register(SERVICE_VFS);
|
---|
230 | if (rc != EOK) {
|
---|
231 | printf("%s: Cannot register VFS service\n", NAME);
|
---|
232 | return rc;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * Start accepting connections.
|
---|
237 | */
|
---|
238 | printf("%s: Accepting connections\n", NAME);
|
---|
239 | async_manager();
|
---|
240 | return 0;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * @}
|
---|
245 | */
|
---|