source: mainline/uspace/srv/fs/ext2fs/ext2fs_ops.c@ e2abab03

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e2abab03 was e2abab03, checked in by Martin Sucha <sucha14@…>, 14 years ago

Split getting instance out of ext2fs_node_get so we don't unncesarily get instance again in ext2fs_match

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