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 EXT2FS_NODE(node) ((node) ? (ext2fs_node_t *) (node)->data : NULL)
|
---|
60 | #define EXT2FS_DBG(format, ...) {if (false) printf("ext2fs: %s: " format "\n", __FUNCTION__, ##__VA_ARGS__);}
|
---|
61 |
|
---|
62 | typedef struct ext2fs_instance {
|
---|
63 | link_t link;
|
---|
64 | devmap_handle_t devmap_handle;
|
---|
65 | ext2_filesystem_t *filesystem;
|
---|
66 | } ext2fs_instance_t;
|
---|
67 |
|
---|
68 | typedef struct ext2fs_node {
|
---|
69 | ext2fs_instance_t *instance;
|
---|
70 | ext2_inode_ref_t *inode_ref;
|
---|
71 | } ext2fs_node_t;
|
---|
72 |
|
---|
73 | static int ext2fs_instance_get(devmap_handle_t, ext2fs_instance_t **);
|
---|
74 | static void ext2fs_read_directory(ipc_callid_t, ipc_callid_t, aoff64_t,
|
---|
75 | size_t, ext2fs_instance_t *, ext2_inode_ref_t *);
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Forward declarations of EXT2 libfs operations.
|
---|
79 | */
|
---|
80 | static int ext2fs_root_get(fs_node_t **, devmap_handle_t);
|
---|
81 | static int ext2fs_match(fs_node_t **, fs_node_t *, const char *);
|
---|
82 | static int ext2fs_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
|
---|
83 | static int ext2fs_node_open(fs_node_t *);
|
---|
84 | static int ext2fs_node_put(fs_node_t *);
|
---|
85 | static int ext2fs_create_node(fs_node_t **, devmap_handle_t, int);
|
---|
86 | static int ext2fs_destroy_node(fs_node_t *);
|
---|
87 | static int ext2fs_link(fs_node_t *, fs_node_t *, const char *);
|
---|
88 | static int ext2fs_unlink(fs_node_t *, fs_node_t *, const char *);
|
---|
89 | static int ext2fs_has_children(bool *, fs_node_t *);
|
---|
90 | static fs_index_t ext2fs_index_get(fs_node_t *);
|
---|
91 | static aoff64_t ext2fs_size_get(fs_node_t *);
|
---|
92 | static unsigned ext2fs_lnkcnt_get(fs_node_t *);
|
---|
93 | static char ext2fs_plb_get_char(unsigned);
|
---|
94 | static bool ext2fs_is_directory(fs_node_t *);
|
---|
95 | static bool ext2fs_is_file(fs_node_t *node);
|
---|
96 | static devmap_handle_t ext2fs_device_get(fs_node_t *node);
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Static variables
|
---|
100 | */
|
---|
101 | static LIST_INITIALIZE(instance_list);
|
---|
102 |
|
---|
103 | /**
|
---|
104 | *
|
---|
105 | */
|
---|
106 | int ext2fs_global_init(void)
|
---|
107 | {
|
---|
108 | return EOK;
|
---|
109 | }
|
---|
110 |
|
---|
111 | int ext2fs_global_fini(void)
|
---|
112 | {
|
---|
113 | return EOK;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * EXT2 libfs operations.
|
---|
120 | */
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Find an instance of filesystem for the given devmap_handle
|
---|
124 | */
|
---|
125 | int ext2fs_instance_get(devmap_handle_t devmap_handle, ext2fs_instance_t **inst)
|
---|
126 | {
|
---|
127 | EXT2FS_DBG("(%u, -)", devmap_handle);
|
---|
128 | link_t *link;
|
---|
129 | ext2fs_instance_t *tmp;
|
---|
130 |
|
---|
131 | if (list_empty(&instance_list)) {
|
---|
132 | EXT2FS_DBG("list empty");
|
---|
133 | return EINVAL;
|
---|
134 | }
|
---|
135 |
|
---|
136 | for (link = instance_list.next; link != &instance_list; link = link->next) {
|
---|
137 | tmp = list_get_instance(link, ext2fs_instance_t, link);
|
---|
138 |
|
---|
139 | if (tmp->devmap_handle == devmap_handle) {
|
---|
140 | *inst = tmp;
|
---|
141 | return EOK;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | EXT2FS_DBG("not found");
|
---|
146 | return EINVAL;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 |
|
---|
151 | int ext2fs_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
---|
152 | {
|
---|
153 | EXT2FS_DBG("(-, %u)", devmap_handle);
|
---|
154 | return ext2fs_node_get(rfn, devmap_handle, EXT2_INODE_ROOT_INDEX);
|
---|
155 | }
|
---|
156 |
|
---|
157 | int ext2fs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
158 | {
|
---|
159 | EXT2FS_DBG("(-,-,%s)", component);
|
---|
160 | ext2fs_node_t *eparent = EXT2FS_NODE(pfn);
|
---|
161 | ext2_filesystem_t *fs;
|
---|
162 | ext2_directory_iterator_t it;
|
---|
163 | int rc;
|
---|
164 | size_t name_size;
|
---|
165 | size_t component_size;
|
---|
166 | bool found = false;
|
---|
167 |
|
---|
168 | fs = eparent->instance->filesystem;
|
---|
169 |
|
---|
170 | if (!ext2_inode_is_type(fs->superblock, eparent->inode_ref->inode,
|
---|
171 | EXT2_INODE_MODE_DIRECTORY)) {
|
---|
172 | return ENOTDIR;
|
---|
173 | }
|
---|
174 |
|
---|
175 | rc = ext2_directory_iterator_init(&it, fs, eparent->inode_ref);
|
---|
176 | if (rc != EOK) {
|
---|
177 | return rc;
|
---|
178 | }
|
---|
179 |
|
---|
180 | // Find length of component in bytes
|
---|
181 | // TODO: check for library function call that does this
|
---|
182 | component_size = 0;
|
---|
183 | while (*(component+component_size) != 0) {
|
---|
184 | component_size++;
|
---|
185 | }
|
---|
186 |
|
---|
187 | while (it.current != NULL) {
|
---|
188 | // ignore empty directory entries
|
---|
189 | if (it.current->inode != 0) {
|
---|
190 | name_size = ext2_directory_entry_ll_get_name_length(fs->superblock,
|
---|
191 | it.current);
|
---|
192 |
|
---|
193 | if (name_size == component_size && bcmp(component, &it.current->name,
|
---|
194 | name_size) == 0) {
|
---|
195 | // FIXME: this may be done better (give instance as param)
|
---|
196 | rc = ext2fs_node_get(rfn, eparent->instance->devmap_handle,
|
---|
197 | it.current->inode);
|
---|
198 | if (rc != EOK) {
|
---|
199 | ext2_directory_iterator_fini(&it);
|
---|
200 | return rc;
|
---|
201 | }
|
---|
202 | found = true;
|
---|
203 | break;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | rc = ext2_directory_iterator_next(&it);
|
---|
208 | if (rc != EOK) {
|
---|
209 | ext2_directory_iterator_fini(&it);
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | ext2_directory_iterator_fini(&it);
|
---|
215 |
|
---|
216 | if (!found) {
|
---|
217 | return ENOENT;
|
---|
218 | }
|
---|
219 |
|
---|
220 | return EOK;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Instantiate a EXT2 in-core node. */
|
---|
224 | int ext2fs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
|
---|
225 | {
|
---|
226 | EXT2FS_DBG("(-,%u,%u)", devmap_handle, index);
|
---|
227 | int rc;
|
---|
228 | fs_node_t *node = NULL;
|
---|
229 | ext2fs_node_t *enode = NULL;
|
---|
230 | ext2fs_instance_t *inst = NULL;
|
---|
231 | ext2_inode_ref_t *inode_ref = NULL;
|
---|
232 |
|
---|
233 | enode = malloc(sizeof(ext2fs_node_t));
|
---|
234 | if (enode == NULL) {
|
---|
235 | return ENOMEM;
|
---|
236 | }
|
---|
237 |
|
---|
238 | node = malloc(sizeof(fs_node_t));
|
---|
239 | if (node == NULL) {
|
---|
240 | free(enode);
|
---|
241 | return ENOMEM;
|
---|
242 | }
|
---|
243 |
|
---|
244 | fs_node_initialize(node);
|
---|
245 |
|
---|
246 | rc = ext2fs_instance_get(devmap_handle, &inst);
|
---|
247 | if (rc != EOK) {
|
---|
248 | free(enode);
|
---|
249 | free(node);
|
---|
250 | return rc;
|
---|
251 | }
|
---|
252 |
|
---|
253 | rc = ext2_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
|
---|
254 | if (rc != EOK) {
|
---|
255 | free(enode);
|
---|
256 | free(node);
|
---|
257 | return rc;
|
---|
258 | }
|
---|
259 |
|
---|
260 | enode->inode_ref = inode_ref;
|
---|
261 | enode->instance = inst;
|
---|
262 | node->data = enode;
|
---|
263 | *rfn = node;
|
---|
264 |
|
---|
265 | EXT2FS_DBG("inode: %u", inode_ref->index);
|
---|
266 |
|
---|
267 | EXT2FS_DBG("EOK");
|
---|
268 |
|
---|
269 | return EOK;
|
---|
270 | }
|
---|
271 |
|
---|
272 | int ext2fs_node_open(fs_node_t *fn)
|
---|
273 | {
|
---|
274 | EXT2FS_DBG("");
|
---|
275 | /*
|
---|
276 | * Opening a file is stateless, nothing
|
---|
277 | * to be done here.
|
---|
278 | */
|
---|
279 | return EOK;
|
---|
280 | }
|
---|
281 |
|
---|
282 | int ext2fs_node_put(fs_node_t *fn)
|
---|
283 | {
|
---|
284 | EXT2FS_DBG("");
|
---|
285 | int rc;
|
---|
286 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
287 | rc = ext2_filesystem_put_inode_ref(enode->inode_ref);
|
---|
288 | if (rc != EOK) {
|
---|
289 | EXT2FS_DBG("ext2_filesystem_put_inode_ref failed");
|
---|
290 | }
|
---|
291 | return rc;
|
---|
292 | }
|
---|
293 |
|
---|
294 | int ext2fs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
|
---|
295 | {
|
---|
296 | EXT2FS_DBG("");
|
---|
297 | // TODO
|
---|
298 | return ENOTSUP;
|
---|
299 | }
|
---|
300 |
|
---|
301 | int ext2fs_destroy_node(fs_node_t *fn)
|
---|
302 | {
|
---|
303 | EXT2FS_DBG("");
|
---|
304 | // TODO
|
---|
305 | return ENOTSUP;
|
---|
306 | }
|
---|
307 |
|
---|
308 | int ext2fs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
---|
309 | {
|
---|
310 | EXT2FS_DBG("");
|
---|
311 | // TODO
|
---|
312 | return ENOTSUP;
|
---|
313 | }
|
---|
314 |
|
---|
315 | int ext2fs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
316 | {
|
---|
317 | EXT2FS_DBG("");
|
---|
318 | // TODO
|
---|
319 | return ENOTSUP;
|
---|
320 | }
|
---|
321 |
|
---|
322 | int ext2fs_has_children(bool *has_children, fs_node_t *fn)
|
---|
323 | {
|
---|
324 | EXT2FS_DBG("");
|
---|
325 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
326 | ext2_directory_iterator_t it;
|
---|
327 | ext2_filesystem_t *fs;
|
---|
328 | int rc;
|
---|
329 | bool found = false;
|
---|
330 |
|
---|
331 | fs = enode->instance->filesystem;
|
---|
332 |
|
---|
333 | if (!ext2_inode_is_type(fs->superblock, enode->inode_ref->inode,
|
---|
334 | EXT2_INODE_MODE_DIRECTORY)) {
|
---|
335 | *has_children = false;
|
---|
336 | EXT2FS_DBG("EOK - false");
|
---|
337 | return EOK;
|
---|
338 | }
|
---|
339 |
|
---|
340 | rc = ext2_directory_iterator_init(&it, fs, enode->inode_ref);
|
---|
341 | if (rc != EOK) {
|
---|
342 | EXT2FS_DBG("error %u", rc);
|
---|
343 | return rc;
|
---|
344 | }
|
---|
345 |
|
---|
346 | // Find a non-empty directory entry
|
---|
347 | while (it.current != NULL) {
|
---|
348 | if (it.current->inode != 0) {
|
---|
349 | found = true;
|
---|
350 | break;
|
---|
351 | }
|
---|
352 |
|
---|
353 | rc = ext2_directory_iterator_next(&it);
|
---|
354 | if (rc != EOK) {
|
---|
355 | ext2_directory_iterator_fini(&it);
|
---|
356 | EXT2FS_DBG("error %u", rc);
|
---|
357 | return rc;
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | rc = ext2_directory_iterator_fini(&it);
|
---|
362 | if (rc != EOK) {
|
---|
363 | EXT2FS_DBG("error %u", rc);
|
---|
364 | return rc;
|
---|
365 | }
|
---|
366 |
|
---|
367 | *has_children = found;
|
---|
368 | EXT2FS_DBG("EOK");
|
---|
369 |
|
---|
370 | return EOK;
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | fs_index_t ext2fs_index_get(fs_node_t *fn)
|
---|
375 | {
|
---|
376 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
377 | EXT2FS_DBG("%u", enode->inode_ref->index);
|
---|
378 | return enode->inode_ref->index;
|
---|
379 | }
|
---|
380 |
|
---|
381 | aoff64_t ext2fs_size_get(fs_node_t *fn)
|
---|
382 | {
|
---|
383 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
384 | aoff64_t size = ext2_inode_get_size(enode->instance->filesystem->superblock,
|
---|
385 | enode->inode_ref->inode);
|
---|
386 | EXT2FS_DBG("%" PRIu64, size);
|
---|
387 | return size;
|
---|
388 | }
|
---|
389 |
|
---|
390 | unsigned ext2fs_lnkcnt_get(fs_node_t *fn)
|
---|
391 | {
|
---|
392 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
393 | unsigned count = ext2_inode_get_usage_count(enode->inode_ref->inode);
|
---|
394 | EXT2FS_DBG("%u", count);
|
---|
395 | return count;
|
---|
396 | }
|
---|
397 |
|
---|
398 | char ext2fs_plb_get_char(unsigned pos)
|
---|
399 | {
|
---|
400 | return ext2fs_reg.plb_ro[pos % PLB_SIZE];
|
---|
401 | }
|
---|
402 |
|
---|
403 | bool ext2fs_is_directory(fs_node_t *fn)
|
---|
404 | {
|
---|
405 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
406 | bool is_dir = ext2_inode_is_type(enode->instance->filesystem->superblock,
|
---|
407 | enode->inode_ref->inode, EXT2_INODE_MODE_DIRECTORY);
|
---|
408 | EXT2FS_DBG("%s", is_dir ? "true" : "false");
|
---|
409 | EXT2FS_DBG("%u", enode->inode_ref->index);
|
---|
410 | return is_dir;
|
---|
411 | }
|
---|
412 |
|
---|
413 | bool ext2fs_is_file(fs_node_t *fn)
|
---|
414 | {
|
---|
415 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
416 | bool is_file = ext2_inode_is_type(enode->instance->filesystem->superblock,
|
---|
417 | enode->inode_ref->inode, EXT2_INODE_MODE_FILE);
|
---|
418 | EXT2FS_DBG("%s", is_file ? "true" : "false");
|
---|
419 | return is_file;
|
---|
420 | }
|
---|
421 |
|
---|
422 | devmap_handle_t ext2fs_device_get(fs_node_t *fn)
|
---|
423 | {
|
---|
424 | EXT2FS_DBG("");
|
---|
425 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
426 | return enode->instance->devmap_handle;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /** libfs operations */
|
---|
430 | libfs_ops_t ext2fs_libfs_ops = {
|
---|
431 | .root_get = ext2fs_root_get,
|
---|
432 | .match = ext2fs_match,
|
---|
433 | .node_get = ext2fs_node_get,
|
---|
434 | .node_open = ext2fs_node_open,
|
---|
435 | .node_put = ext2fs_node_put,
|
---|
436 | .create = ext2fs_create_node,
|
---|
437 | .destroy = ext2fs_destroy_node,
|
---|
438 | .link = ext2fs_link,
|
---|
439 | .unlink = ext2fs_unlink,
|
---|
440 | .has_children = ext2fs_has_children,
|
---|
441 | .index_get = ext2fs_index_get,
|
---|
442 | .size_get = ext2fs_size_get,
|
---|
443 | .lnkcnt_get = ext2fs_lnkcnt_get,
|
---|
444 | .plb_get_char = ext2fs_plb_get_char,
|
---|
445 | .is_directory = ext2fs_is_directory,
|
---|
446 | .is_file = ext2fs_is_file,
|
---|
447 | .device_get = ext2fs_device_get
|
---|
448 | };
|
---|
449 |
|
---|
450 | /*
|
---|
451 | * VFS operations.
|
---|
452 | */
|
---|
453 |
|
---|
454 | void ext2fs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
455 | {
|
---|
456 | EXT2FS_DBG("");
|
---|
457 | int rc;
|
---|
458 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
459 | ext2_filesystem_t *fs;
|
---|
460 | ext2fs_instance_t *inst;
|
---|
461 |
|
---|
462 |
|
---|
463 |
|
---|
464 | /* Accept the mount options */
|
---|
465 | char *opts;
|
---|
466 | rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
|
---|
467 |
|
---|
468 | if (rc != EOK) {
|
---|
469 | async_answer_0(rid, rc);
|
---|
470 | return;
|
---|
471 | }
|
---|
472 |
|
---|
473 | free(opts);
|
---|
474 |
|
---|
475 | /* Allocate libext2 filesystem structure */
|
---|
476 | fs = (ext2_filesystem_t *) malloc(sizeof(ext2_filesystem_t));
|
---|
477 | if (fs == NULL) {
|
---|
478 | async_answer_0(rid, ENOMEM);
|
---|
479 | return;
|
---|
480 | }
|
---|
481 |
|
---|
482 | /* Allocate instance structure */
|
---|
483 | inst = (ext2fs_instance_t *) malloc(sizeof(ext2fs_instance_t));
|
---|
484 | if (inst == NULL) {
|
---|
485 | free(fs);
|
---|
486 | async_answer_0(rid, ENOMEM);
|
---|
487 | return;
|
---|
488 | }
|
---|
489 |
|
---|
490 | /* Initialize the filesystem */
|
---|
491 | rc = ext2_filesystem_init(fs, devmap_handle);
|
---|
492 | if (rc != EOK) {
|
---|
493 | free(fs);
|
---|
494 | free(inst);
|
---|
495 | async_answer_0(rid, rc);
|
---|
496 | return;
|
---|
497 | }
|
---|
498 |
|
---|
499 | /* Do some sanity checking */
|
---|
500 | rc = ext2_filesystem_check_sanity(fs);
|
---|
501 | if (rc != EOK) {
|
---|
502 | ext2_filesystem_fini(fs);
|
---|
503 | free(fs);
|
---|
504 | free(inst);
|
---|
505 | async_answer_0(rid, rc);
|
---|
506 | return;
|
---|
507 | }
|
---|
508 |
|
---|
509 | /* Initialize instance and add to the list */
|
---|
510 | link_initialize(&inst->link);
|
---|
511 | inst->devmap_handle = devmap_handle;
|
---|
512 | inst->filesystem = fs;
|
---|
513 | list_append(&inst->link, &instance_list);
|
---|
514 |
|
---|
515 | async_answer_0(rid, EOK);
|
---|
516 | }
|
---|
517 |
|
---|
518 | void ext2fs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
519 | {
|
---|
520 | EXT2FS_DBG("");
|
---|
521 | libfs_mount(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
522 | }
|
---|
523 |
|
---|
524 | void ext2fs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
525 | {
|
---|
526 | EXT2FS_DBG("");
|
---|
527 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
528 | ext2fs_instance_t *inst;
|
---|
529 | int rc;
|
---|
530 |
|
---|
531 | rc = ext2fs_instance_get(devmap_handle, &inst);
|
---|
532 |
|
---|
533 | if (rc != EOK) {
|
---|
534 | async_answer_0(rid, rc);
|
---|
535 | return;
|
---|
536 | }
|
---|
537 |
|
---|
538 | // TODO: check if the fs is busy
|
---|
539 |
|
---|
540 | // Remove the instance from list
|
---|
541 | list_remove(&inst->link);
|
---|
542 | ext2_filesystem_fini(inst->filesystem);
|
---|
543 |
|
---|
544 | async_answer_0(rid, EOK);
|
---|
545 | }
|
---|
546 |
|
---|
547 | void ext2fs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
548 | {
|
---|
549 | EXT2FS_DBG("");
|
---|
550 | libfs_unmount(&ext2fs_libfs_ops, rid, request);
|
---|
551 | }
|
---|
552 |
|
---|
553 | void ext2fs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
554 | {
|
---|
555 | EXT2FS_DBG("");
|
---|
556 | libfs_lookup(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
557 | }
|
---|
558 |
|
---|
559 | void ext2fs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
560 | {
|
---|
561 | EXT2FS_DBG("");
|
---|
562 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
563 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
564 | aoff64_t pos =
|
---|
565 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
566 |
|
---|
567 | ext2fs_instance_t *inst;
|
---|
568 | ext2_inode_ref_t *inode_ref;
|
---|
569 | int rc;
|
---|
570 |
|
---|
571 | /*
|
---|
572 | * Receive the read request.
|
---|
573 | */
|
---|
574 | ipc_callid_t callid;
|
---|
575 | size_t size;
|
---|
576 | if (!async_data_read_receive(&callid, &size)) {
|
---|
577 | async_answer_0(callid, EINVAL);
|
---|
578 | async_answer_0(rid, EINVAL);
|
---|
579 | return;
|
---|
580 | }
|
---|
581 |
|
---|
582 | rc = ext2fs_instance_get(devmap_handle, &inst);
|
---|
583 | if (rc != EOK) {
|
---|
584 | async_answer_0(callid, rc);
|
---|
585 | async_answer_0(rid, rc);
|
---|
586 | return;
|
---|
587 | }
|
---|
588 |
|
---|
589 | rc = ext2_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
|
---|
590 | if (rc != EOK) {
|
---|
591 | async_answer_0(callid, rc);
|
---|
592 | async_answer_0(rid, rc);
|
---|
593 | return;
|
---|
594 | }
|
---|
595 |
|
---|
596 | if (ext2_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
|
---|
597 | EXT2_INODE_MODE_FILE)) {
|
---|
598 | async_answer_0(callid, ENOTSUP);
|
---|
599 | async_answer_0(rid, ENOTSUP);
|
---|
600 | }
|
---|
601 | else if (ext2_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
|
---|
602 | EXT2_INODE_MODE_DIRECTORY)) {
|
---|
603 | ext2fs_read_directory(rid, callid, pos, size, inst, inode_ref);
|
---|
604 | }
|
---|
605 | else {
|
---|
606 | // Other inode types not supported
|
---|
607 | async_answer_0(callid, ENOTSUP);
|
---|
608 | async_answer_0(rid, ENOTSUP);
|
---|
609 | }
|
---|
610 |
|
---|
611 | ext2_filesystem_put_inode_ref(inode_ref);
|
---|
612 |
|
---|
613 | }
|
---|
614 |
|
---|
615 | void ext2fs_read_directory(ipc_callid_t rid, ipc_callid_t callid, aoff64_t pos,
|
---|
616 | size_t size, ext2fs_instance_t *inst, ext2_inode_ref_t *inode_ref)
|
---|
617 | {
|
---|
618 | ext2_directory_iterator_t it;
|
---|
619 | aoff64_t cur;
|
---|
620 | uint8_t *buf;
|
---|
621 | size_t name_size;
|
---|
622 | int rc;
|
---|
623 | bool found = false;
|
---|
624 |
|
---|
625 | rc = ext2_directory_iterator_init(&it, inst->filesystem, inode_ref);
|
---|
626 | if (rc != EOK) {
|
---|
627 | async_answer_0(callid, rc);
|
---|
628 | async_answer_0(rid, rc);
|
---|
629 | return;
|
---|
630 | }
|
---|
631 |
|
---|
632 | // Find the index we want to read
|
---|
633 | // Note that we need to iterate and count as
|
---|
634 | // the underlying structure is a linked list
|
---|
635 | // Moreover, we want to skip . and .. entries
|
---|
636 | // as these are not used in HelenOS
|
---|
637 | cur = 0;
|
---|
638 | while (it.current != NULL) {
|
---|
639 | if (it.current->inode == 0) {
|
---|
640 | goto skip;
|
---|
641 | }
|
---|
642 |
|
---|
643 | name_size = ext2_directory_entry_ll_get_name_length(
|
---|
644 | inst->filesystem->superblock, it.current);
|
---|
645 |
|
---|
646 | // skip . and ..
|
---|
647 | if ((name_size == 1 || name_size == 2) && it.current->name == '.') {
|
---|
648 | if (name_size == 1) {
|
---|
649 | goto skip;
|
---|
650 | }
|
---|
651 | else if (name_size == 2 && *(&it.current->name+1) == '.') {
|
---|
652 | goto skip;
|
---|
653 | }
|
---|
654 | }
|
---|
655 |
|
---|
656 | // Is this the dir entry we want to read?
|
---|
657 | if (cur == pos) {
|
---|
658 | // The on-disk entry does not contain \0 at the end
|
---|
659 | // end of entry name, so we copy it to new buffer
|
---|
660 | // and add the \0 at the end
|
---|
661 | buf = malloc(name_size+1);
|
---|
662 | if (buf == NULL) {
|
---|
663 | ext2_directory_iterator_fini(&it);
|
---|
664 | async_answer_0(callid, ENOMEM);
|
---|
665 | async_answer_0(rid, ENOMEM);
|
---|
666 | return;
|
---|
667 | }
|
---|
668 | memcpy(buf, &it.current->name, name_size);
|
---|
669 | *(buf+name_size) = 0;
|
---|
670 | found = true;
|
---|
671 | (void) async_data_read_finalize(callid, buf, name_size+1);
|
---|
672 | free(buf);
|
---|
673 | break;
|
---|
674 | }
|
---|
675 | cur++;
|
---|
676 |
|
---|
677 | skip:
|
---|
678 | rc = ext2_directory_iterator_next(&it);
|
---|
679 | if (rc != EOK) {
|
---|
680 | ext2_directory_iterator_fini(&it);
|
---|
681 | async_answer_0(callid, rc);
|
---|
682 | async_answer_0(rid, rc);
|
---|
683 | return;
|
---|
684 | }
|
---|
685 | }
|
---|
686 |
|
---|
687 | rc = ext2_directory_iterator_fini(&it);
|
---|
688 | if (rc != EOK) {
|
---|
689 | async_answer_0(rid, rc);
|
---|
690 | return;
|
---|
691 | }
|
---|
692 |
|
---|
693 | if (found) {
|
---|
694 | async_answer_1(rid, EOK, 1);
|
---|
695 | }
|
---|
696 | else {
|
---|
697 | async_answer_0(callid, ENOENT);
|
---|
698 | async_answer_0(rid, ENOENT);
|
---|
699 | }
|
---|
700 | }
|
---|
701 |
|
---|
702 | void ext2fs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
703 | {
|
---|
704 | EXT2FS_DBG("");
|
---|
705 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
706 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
707 | // aoff64_t pos =
|
---|
708 | // (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
709 |
|
---|
710 | // TODO
|
---|
711 | async_answer_0(rid, ENOTSUP);
|
---|
712 | }
|
---|
713 |
|
---|
714 | void ext2fs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
715 | {
|
---|
716 | EXT2FS_DBG("");
|
---|
717 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
718 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
719 | // aoff64_t size =
|
---|
720 | // (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
721 |
|
---|
722 | // TODO
|
---|
723 | async_answer_0(rid, ENOTSUP);
|
---|
724 | }
|
---|
725 |
|
---|
726 | void ext2fs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
727 | {
|
---|
728 | EXT2FS_DBG("");
|
---|
729 | async_answer_0(rid, EOK);
|
---|
730 | }
|
---|
731 |
|
---|
732 | void ext2fs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
733 | {
|
---|
734 | EXT2FS_DBG("");
|
---|
735 | // devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
|
---|
736 | // fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
737 |
|
---|
738 | // TODO
|
---|
739 | async_answer_0(rid, ENOTSUP);
|
---|
740 | }
|
---|
741 |
|
---|
742 | void ext2fs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
743 | {
|
---|
744 | EXT2FS_DBG("");
|
---|
745 | libfs_open_node(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
746 | }
|
---|
747 |
|
---|
748 | void ext2fs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
749 | {
|
---|
750 | EXT2FS_DBG("");
|
---|
751 | libfs_stat(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
752 | }
|
---|
753 |
|
---|
754 | void ext2fs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
755 | {
|
---|
756 | EXT2FS_DBG("");
|
---|
757 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
758 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
759 |
|
---|
760 | // TODO
|
---|
761 | async_answer_0(rid, ENOTSUP);
|
---|
762 | }
|
---|
763 |
|
---|
764 | /**
|
---|
765 | * @}
|
---|
766 | */
|
---|