1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * Copyright (c) 2011 Martin Sucha
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup fs
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @file ext2fs_ops.c
|
---|
36 | * @brief Implementation of VFS operations for the EXT2 file system server.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include "ext2fs.h"
|
---|
40 | #include "../../vfs/vfs.h"
|
---|
41 | #include <libfs.h>
|
---|
42 | #include <libblock.h>
|
---|
43 | #include <libext2.h>
|
---|
44 | #include <ipc/services.h>
|
---|
45 | #include <ipc/devmap.h>
|
---|
46 | #include <macros.h>
|
---|
47 | #include <async.h>
|
---|
48 | #include <errno.h>
|
---|
49 | #include <str.h>
|
---|
50 | #include <byteorder.h>
|
---|
51 | #include <adt/hash_table.h>
|
---|
52 | #include <adt/list.h>
|
---|
53 | #include <assert.h>
|
---|
54 | #include <fibril_synch.h>
|
---|
55 | #include <sys/mman.h>
|
---|
56 | #include <align.h>
|
---|
57 | #include <adt/hash_table.h>
|
---|
58 |
|
---|
59 | #define EXT2_NODE(node) ((node) ? (ext2fs_node_t *) (node)->data : NULL)
|
---|
60 | #define FS_NODE(node) ((node) ? (node)->bp : NULL)
|
---|
61 |
|
---|
62 | #define FS_BUCKETS_LOG 12
|
---|
63 | #define FS_BUCKETS (1 << FS_BUCKETS_LOG)
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Forward declarations of EXT2 libfs operations.
|
---|
67 | */
|
---|
68 | static int ext2fs_root_get(fs_node_t **, devmap_handle_t);
|
---|
69 | static int ext2fs_match(fs_node_t **, fs_node_t *, const char *);
|
---|
70 | static int ext2fs_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
|
---|
71 | static int ext2fs_node_open(fs_node_t *);
|
---|
72 | static int ext2fs_node_put(fs_node_t *);
|
---|
73 | static int ext2fs_create_node(fs_node_t **, devmap_handle_t, int);
|
---|
74 | static int ext2fs_destroy_node(fs_node_t *);
|
---|
75 | static int ext2fs_link(fs_node_t *, fs_node_t *, const char *);
|
---|
76 | static int ext2fs_unlink(fs_node_t *, fs_node_t *, const char *);
|
---|
77 | static int ext2fs_has_children(bool *, fs_node_t *);
|
---|
78 | static fs_index_t ext2fs_index_get(fs_node_t *);
|
---|
79 | static aoff64_t ext2fs_size_get(fs_node_t *);
|
---|
80 | static unsigned ext2fs_lnkcnt_get(fs_node_t *);
|
---|
81 | static char ext2fs_plb_get_char(unsigned);
|
---|
82 | static bool ext2fs_is_directory(fs_node_t *);
|
---|
83 | static bool ext2fs_is_file(fs_node_t *node);
|
---|
84 | static devmap_handle_t ext2fs_device_get(fs_node_t *node);
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Static variables
|
---|
88 | */
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Global hash table holding a mapping from devmap handles to
|
---|
92 | * ext2_filesystem_t structures
|
---|
93 | */
|
---|
94 | //TODO
|
---|
95 | //static hash_table_t fs_hash;
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Mutex protecting fs_hash
|
---|
99 | */
|
---|
100 | static FIBRIL_MUTEX_INITIALIZE(fs_hash_lock);
|
---|
101 |
|
---|
102 | /**
|
---|
103 | *
|
---|
104 | */
|
---|
105 | int ext2fs_global_init(void)
|
---|
106 | {
|
---|
107 | //TODO
|
---|
108 | //if (!hash_table_create(&fs_hash, FS_BUCKETS, 1, &fs_hash_ops)) {
|
---|
109 | // return ENOMEM;
|
---|
110 | //}
|
---|
111 |
|
---|
112 | return EOK;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * EXT2 libfs operations.
|
---|
119 | */
|
---|
120 |
|
---|
121 | int ext2fs_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
---|
122 | {
|
---|
123 | // TODO
|
---|
124 | return 0;
|
---|
125 | //return ext2fs_node_get(rfn, devmap_handle, 0);
|
---|
126 | }
|
---|
127 |
|
---|
128 | int ext2fs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
129 | {
|
---|
130 | // TODO
|
---|
131 | return ENOTSUP;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Instantiate a EXT2 in-core node. */
|
---|
135 | int ext2fs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
|
---|
136 | {
|
---|
137 | // TODO
|
---|
138 | return ENOTSUP;
|
---|
139 | }
|
---|
140 |
|
---|
141 | int ext2fs_node_open(fs_node_t *fn)
|
---|
142 | {
|
---|
143 | /*
|
---|
144 | * Opening a file is stateless, nothing
|
---|
145 | * to be done here.
|
---|
146 | */
|
---|
147 | return EOK;
|
---|
148 | }
|
---|
149 |
|
---|
150 | int ext2fs_node_put(fs_node_t *fn)
|
---|
151 | {
|
---|
152 | // TODO
|
---|
153 | return ENOTSUP;
|
---|
154 | }
|
---|
155 |
|
---|
156 | int ext2fs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
|
---|
157 | {
|
---|
158 | // TODO
|
---|
159 | return ENOTSUP;
|
---|
160 | }
|
---|
161 |
|
---|
162 | int ext2fs_destroy_node(fs_node_t *fn)
|
---|
163 | {
|
---|
164 | // TODO
|
---|
165 | return ENOTSUP;
|
---|
166 | }
|
---|
167 |
|
---|
168 | int ext2fs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
---|
169 | {
|
---|
170 | // TODO
|
---|
171 | return ENOTSUP;
|
---|
172 | }
|
---|
173 |
|
---|
174 | int ext2fs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
175 | {
|
---|
176 | // TODO
|
---|
177 | return ENOTSUP;
|
---|
178 | }
|
---|
179 |
|
---|
180 | int ext2fs_has_children(bool *has_children, fs_node_t *fn)
|
---|
181 | {
|
---|
182 | // TODO
|
---|
183 | return ENOTSUP;
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | fs_index_t ext2fs_index_get(fs_node_t *fn)
|
---|
188 | {
|
---|
189 | // TODO
|
---|
190 | return 0;
|
---|
191 | }
|
---|
192 |
|
---|
193 | aoff64_t ext2fs_size_get(fs_node_t *fn)
|
---|
194 | {
|
---|
195 | // TODO
|
---|
196 | return 0;
|
---|
197 | }
|
---|
198 |
|
---|
199 | unsigned ext2fs_lnkcnt_get(fs_node_t *fn)
|
---|
200 | {
|
---|
201 | // TODO
|
---|
202 | return 0;
|
---|
203 | }
|
---|
204 |
|
---|
205 | char ext2fs_plb_get_char(unsigned pos)
|
---|
206 | {
|
---|
207 | return ext2fs_reg.plb_ro[pos % PLB_SIZE];
|
---|
208 | }
|
---|
209 |
|
---|
210 | bool ext2fs_is_directory(fs_node_t *fn)
|
---|
211 | {
|
---|
212 | // TODO
|
---|
213 | return false;
|
---|
214 | }
|
---|
215 |
|
---|
216 | bool ext2fs_is_file(fs_node_t *fn)
|
---|
217 | {
|
---|
218 | // TODO
|
---|
219 | return false;
|
---|
220 | }
|
---|
221 |
|
---|
222 | devmap_handle_t ext2fs_device_get(fs_node_t *node)
|
---|
223 | {
|
---|
224 | return 0;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /** libfs operations */
|
---|
228 | libfs_ops_t ext2fs_libfs_ops = {
|
---|
229 | .root_get = ext2fs_root_get,
|
---|
230 | .match = ext2fs_match,
|
---|
231 | .node_get = ext2fs_node_get,
|
---|
232 | .node_open = ext2fs_node_open,
|
---|
233 | .node_put = ext2fs_node_put,
|
---|
234 | .create = ext2fs_create_node,
|
---|
235 | .destroy = ext2fs_destroy_node,
|
---|
236 | .link = ext2fs_link,
|
---|
237 | .unlink = ext2fs_unlink,
|
---|
238 | .has_children = ext2fs_has_children,
|
---|
239 | .index_get = ext2fs_index_get,
|
---|
240 | .size_get = ext2fs_size_get,
|
---|
241 | .lnkcnt_get = ext2fs_lnkcnt_get,
|
---|
242 | .plb_get_char = ext2fs_plb_get_char,
|
---|
243 | .is_directory = ext2fs_is_directory,
|
---|
244 | .is_file = ext2fs_is_file,
|
---|
245 | .device_get = ext2fs_device_get
|
---|
246 | };
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * VFS operations.
|
---|
250 | */
|
---|
251 |
|
---|
252 | void ext2fs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
253 | {
|
---|
254 | int rc;
|
---|
255 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
256 | ext2_filesystem_t *fs;
|
---|
257 |
|
---|
258 | /* Accept the mount options */
|
---|
259 | char *opts;
|
---|
260 | rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
|
---|
261 |
|
---|
262 | if (rc != EOK) {
|
---|
263 | async_answer_0(rid, rc);
|
---|
264 | return;
|
---|
265 | }
|
---|
266 |
|
---|
267 | free(opts);
|
---|
268 |
|
---|
269 | /* Allocate libext2 filesystem structure */
|
---|
270 | fs = (ext2_filesystem_t *) malloc(sizeof(ext2_filesystem_t));
|
---|
271 | if (fs == NULL) {
|
---|
272 | async_answer_0(rid, ENOMEM);
|
---|
273 | return;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /* Initialize the filesystem */
|
---|
277 | rc = ext2_filesystem_init(fs, devmap_handle);
|
---|
278 | if (rc != EOK) {
|
---|
279 | async_answer_0(rid, rc);
|
---|
280 | return;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /* Do some sanity checking */
|
---|
284 | rc = ext2_filesystem_check_sanity(fs);
|
---|
285 | if (rc != EOK) {
|
---|
286 | ext2_filesystem_fini(fs);
|
---|
287 | async_answer_0(rid, rc);
|
---|
288 | return;
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 |
|
---|
293 | // TODO
|
---|
294 | async_answer_0(rid, ENOTSUP);
|
---|
295 | }
|
---|
296 |
|
---|
297 | void ext2fs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
298 | {
|
---|
299 | libfs_mount(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
300 | }
|
---|
301 |
|
---|
302 | void ext2fs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
303 | {
|
---|
304 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
305 | // TODO
|
---|
306 | async_answer_0(rid, ENOTSUP);
|
---|
307 | }
|
---|
308 |
|
---|
309 | void ext2fs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
310 | {
|
---|
311 | libfs_unmount(&ext2fs_libfs_ops, rid, request);
|
---|
312 | }
|
---|
313 |
|
---|
314 | void ext2fs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
315 | {
|
---|
316 | libfs_lookup(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
317 | }
|
---|
318 |
|
---|
319 | void ext2fs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
320 | {
|
---|
321 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
322 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
323 | // aoff64_t pos =
|
---|
324 | // (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
325 |
|
---|
326 | // TODO
|
---|
327 | async_answer_0(rid, ENOTSUP);
|
---|
328 | }
|
---|
329 |
|
---|
330 | void ext2fs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
331 | {
|
---|
332 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
333 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
334 | // aoff64_t pos =
|
---|
335 | // (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
336 |
|
---|
337 | // TODO
|
---|
338 | async_answer_0(rid, ENOTSUP);
|
---|
339 | }
|
---|
340 |
|
---|
341 | void ext2fs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
342 | {
|
---|
343 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
344 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
345 | // aoff64_t size =
|
---|
346 | // (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
347 |
|
---|
348 | // TODO
|
---|
349 | async_answer_0(rid, ENOTSUP);
|
---|
350 | }
|
---|
351 |
|
---|
352 | void ext2fs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
353 | {
|
---|
354 | async_answer_0(rid, EOK);
|
---|
355 | }
|
---|
356 |
|
---|
357 | void ext2fs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
358 | {
|
---|
359 | // devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
|
---|
360 | // fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
361 |
|
---|
362 | // TODO
|
---|
363 | async_answer_0(rid, ENOTSUP);
|
---|
364 | }
|
---|
365 |
|
---|
366 | void ext2fs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
367 | {
|
---|
368 | libfs_open_node(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
369 | }
|
---|
370 |
|
---|
371 | void ext2fs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
372 | {
|
---|
373 | libfs_stat(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
374 | }
|
---|
375 |
|
---|
376 | void ext2fs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
377 | {
|
---|
378 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
379 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
380 |
|
---|
381 | // TODO
|
---|
382 | async_answer_0(rid, ENOTSUP);
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * @}
|
---|
387 | */
|
---|