source: mainline/uspace/srv/fs/ext4fs/ext4fs_ops.c@ 82d7816

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 82d7816 was 82d7816, checked in by Frantisek Princ <frantisek.princ@…>, 14 years ago

some debug messages + death code removal

  • Property mode set to 100644
File size: 25.3 KB
Line 
1/*
2 * Copyright (c) 2011 Frantisek Princ
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 ext4fs_ops.c
35 * @brief VFS operations for EXT4 filesystem.
36 */
37
38#include <errno.h>
39#include <fibril_synch.h>
40#include <libext4.h>
41#include <libfs.h>
42#include <macros.h>
43#include <malloc.h>
44#include <string.h>
45#include <adt/hash_table.h>
46#include <ipc/loc.h>
47#include "ext4fs.h"
48#include "../../vfs/vfs.h"
49
50#define EXT4FS_NODE(node) ((node) ? (ext4fs_node_t *) (node)->data : NULL)
51
52#define OPEN_NODES_KEYS 2
53#define OPEN_NODES_DEV_HANDLE_KEY 0
54#define OPEN_NODES_INODE_KEY 1
55#define OPEN_NODES_BUCKETS 256
56
57typedef struct ext4fs_instance {
58 link_t link;
59 service_id_t service_id;
60 ext4_filesystem_t *filesystem;
61 unsigned int open_nodes_count;
62} ext4fs_instance_t;
63
64typedef struct ext4fs_node {
65 ext4fs_instance_t *instance;
66 ext4_inode_ref_t *inode_ref;
67 fs_node_t *fs_node;
68 link_t link;
69 unsigned int references;
70} ext4fs_node_t;
71
72/*
73 * Forward declarations of auxiliary functions
74 */
75
76static int ext4fs_read_directory(ipc_callid_t, aoff64_t, size_t,
77 ext4fs_instance_t *, ext4_inode_ref_t *, size_t *);
78static int ext4fs_read_file(ipc_callid_t, aoff64_t, size_t, ext4fs_instance_t *,
79 ext4_inode_ref_t *, size_t *);
80static bool ext4fs_is_dots(const uint8_t *, size_t);
81static int ext4fs_instance_get(service_id_t, ext4fs_instance_t **);
82static int ext4fs_node_get_core(fs_node_t **, ext4fs_instance_t *, fs_index_t);
83static int ext4fs_node_put_core(ext4fs_node_t *);
84
85/*
86 * Forward declarations of EXT4 libfs operations.
87 */
88static int ext4fs_root_get(fs_node_t **, service_id_t);
89static int ext4fs_match(fs_node_t **, fs_node_t *, const char *);
90static int ext4fs_node_get(fs_node_t **, service_id_t, fs_index_t);
91static int ext4fs_node_open(fs_node_t *);
92static int ext4fs_node_put(fs_node_t *);
93static int ext4fs_create_node(fs_node_t **, service_id_t, int);
94static int ext4fs_destroy_node(fs_node_t *);
95static int ext4fs_link(fs_node_t *, fs_node_t *, const char *);
96static int ext4fs_unlink(fs_node_t *, fs_node_t *, const char *);
97static int ext4fs_has_children(bool *, fs_node_t *);
98static fs_index_t ext4fs_index_get(fs_node_t *);
99static aoff64_t ext4fs_size_get(fs_node_t *);
100static unsigned ext4fs_lnkcnt_get(fs_node_t *);
101static bool ext4fs_is_directory(fs_node_t *);
102static bool ext4fs_is_file(fs_node_t *node);
103static service_id_t ext4fs_service_get(fs_node_t *node);
104
105/*
106 * Static variables
107 */
108static LIST_INITIALIZE(instance_list);
109static FIBRIL_MUTEX_INITIALIZE(instance_list_mutex);
110static hash_table_t open_nodes;
111static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
112
113/* Hash table interface for open nodes hash table */
114static hash_index_t open_nodes_hash(unsigned long key[])
115{
116 /* TODO: This is very simple and probably can be improved */
117 return key[OPEN_NODES_INODE_KEY] % OPEN_NODES_BUCKETS;
118}
119
120static int open_nodes_compare(unsigned long key[], hash_count_t keys,
121 link_t *item)
122{
123 ext4fs_node_t *enode = hash_table_get_instance(item, ext4fs_node_t, link);
124 assert(keys > 0);
125 if (enode->instance->service_id !=
126 ((service_id_t) key[OPEN_NODES_DEV_HANDLE_KEY])) {
127 return false;
128 }
129 if (keys == 1) {
130 return true;
131 }
132 assert(keys == 2);
133 return (enode->inode_ref->index == key[OPEN_NODES_INODE_KEY]);
134}
135
136static void open_nodes_remove_cb(link_t *link)
137{
138 /* We don't use remove callback for this hash table */
139}
140
141static hash_table_operations_t open_nodes_ops = {
142 .hash = open_nodes_hash,
143 .compare = open_nodes_compare,
144 .remove_callback = open_nodes_remove_cb,
145};
146
147
148int ext4fs_global_init(void)
149{
150 if (!hash_table_create(&open_nodes, OPEN_NODES_BUCKETS,
151 OPEN_NODES_KEYS, &open_nodes_ops)) {
152 return ENOMEM;
153 }
154 return EOK;
155}
156
157
158int ext4fs_global_fini(void)
159{
160 hash_table_destroy(&open_nodes);
161 return EOK;
162}
163
164
165/*
166 * EXT4 libfs operations.
167 */
168
169int ext4fs_instance_get(service_id_t service_id, ext4fs_instance_t **inst)
170{
171 ext4fs_instance_t *tmp;
172
173 fibril_mutex_lock(&instance_list_mutex);
174
175 if (list_empty(&instance_list)) {
176 fibril_mutex_unlock(&instance_list_mutex);
177 return EINVAL;
178 }
179
180 list_foreach(instance_list, link) {
181 tmp = list_get_instance(link, ext4fs_instance_t, link);
182
183 if (tmp->service_id == service_id) {
184 *inst = tmp;
185 fibril_mutex_unlock(&instance_list_mutex);
186 return EOK;
187 }
188 }
189
190 fibril_mutex_unlock(&instance_list_mutex);
191 return EINVAL;
192}
193
194
195int ext4fs_root_get(fs_node_t **rfn, service_id_t service_id)
196{
197 return ext4fs_node_get(rfn, service_id, EXT4_INODE_ROOT_INDEX);
198}
199
200
201int ext4fs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
202{
203 ext4fs_node_t *eparent = EXT4FS_NODE(pfn);
204 ext4_filesystem_t *fs;
205 ext4_directory_iterator_t it;
206 int rc;
207
208 fs = eparent->instance->filesystem;
209
210 if (!ext4_inode_is_type(fs->superblock, eparent->inode_ref->inode,
211 EXT4_INODE_MODE_DIRECTORY)) {
212 return ENOTDIR;
213 }
214
215 rc = ext4_directory_iterator_init(&it, fs, eparent->inode_ref, 0);
216 if (rc != EOK) {
217 return rc;
218 }
219
220 rc = ext4_directory_find_entry(&it, eparent->inode_ref, component);
221 if (rc != EOK) {
222 ext4_directory_iterator_fini(&it);
223 return rc;
224 }
225
226 uint32_t inode = ext4_directory_entry_ll_get_inode(it.current);
227
228 rc = ext4fs_node_get_core(rfn, eparent->instance, inode);
229 if (rc != EOK) {
230 ext4_directory_iterator_fini(&it);
231 return rc;
232 }
233
234 ext4_directory_iterator_fini(&it);
235 return EOK;
236}
237
238
239int ext4fs_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
240{
241 ext4fs_instance_t *inst = NULL;
242 int rc;
243
244 rc = ext4fs_instance_get(service_id, &inst);
245 if (rc != EOK) {
246 return rc;
247 }
248
249 return ext4fs_node_get_core(rfn, inst, index);
250}
251
252
253int ext4fs_node_get_core(fs_node_t **rfn, ext4fs_instance_t *inst,
254 fs_index_t index)
255{
256 int rc;
257 fs_node_t *node = NULL;
258 ext4fs_node_t *enode = NULL;
259
260 ext4_inode_ref_t *inode_ref = NULL;
261
262 fibril_mutex_lock(&open_nodes_lock);
263
264 /* Check if the node is not already open */
265 unsigned long key[] = {
266 [OPEN_NODES_DEV_HANDLE_KEY] = inst->service_id,
267 [OPEN_NODES_INODE_KEY] = index,
268 };
269 link_t *already_open = hash_table_find(&open_nodes, key);
270
271 if (already_open) {
272 enode = hash_table_get_instance(already_open, ext4fs_node_t, link);
273 *rfn = enode->fs_node;
274 enode->references++;
275
276 fibril_mutex_unlock(&open_nodes_lock);
277 return EOK;
278 }
279
280 enode = malloc(sizeof(ext4fs_node_t));
281 if (enode == NULL) {
282 fibril_mutex_unlock(&open_nodes_lock);
283 return ENOMEM;
284 }
285
286 node = malloc(sizeof(fs_node_t));
287 if (node == NULL) {
288 free(enode);
289 fibril_mutex_unlock(&open_nodes_lock);
290 return ENOMEM;
291 }
292 fs_node_initialize(node);
293
294 rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
295 if (rc != EOK) {
296 free(enode);
297 free(node);
298 fibril_mutex_unlock(&open_nodes_lock);
299 return rc;
300 }
301
302 enode->inode_ref = inode_ref;
303 enode->instance = inst;
304 enode->references = 1;
305 enode->fs_node = node;
306 link_initialize(&enode->link);
307
308 node->data = enode;
309 *rfn = node;
310
311 hash_table_insert(&open_nodes, key, &enode->link);
312 inst->open_nodes_count++;
313
314 fibril_mutex_unlock(&open_nodes_lock);
315
316 return EOK;
317}
318
319
320int ext4fs_node_put_core(ext4fs_node_t *enode)
321{
322 int rc;
323 unsigned long key[] = {
324 [OPEN_NODES_DEV_HANDLE_KEY] = enode->instance->service_id,
325 [OPEN_NODES_INODE_KEY] = enode->inode_ref->index,
326 };
327
328 hash_table_remove(&open_nodes, key, OPEN_NODES_KEYS);
329 assert(enode->instance->open_nodes_count > 0);
330 enode->instance->open_nodes_count--;
331
332 rc = ext4_filesystem_put_inode_ref(enode->inode_ref);
333 if (rc != EOK) {
334 return rc;
335 }
336
337 free(enode->fs_node);
338 free(enode);
339
340 return EOK;
341}
342
343
344int ext4fs_node_open(fs_node_t *fn)
345{
346 // Stateless operation
347 return EOK;
348}
349
350int ext4fs_node_put(fs_node_t *fn)
351{
352 int rc;
353 ext4fs_node_t *enode = EXT4FS_NODE(fn);
354
355 fibril_mutex_lock(&open_nodes_lock);
356
357 assert(enode->references > 0);
358 enode->references--;
359 if (enode->references == 0) {
360 rc = ext4fs_node_put_core(enode);
361 if (rc != EOK) {
362 fibril_mutex_unlock(&open_nodes_lock);
363 return rc;
364 }
365 }
366
367 fibril_mutex_unlock(&open_nodes_lock);
368
369 return EOK;
370}
371
372
373int ext4fs_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
374{
375 EXT4FS_DBG("not supported");
376
377 // TODO
378 return ENOTSUP;
379}
380
381
382int ext4fs_destroy_node(fs_node_t *fn)
383{
384 EXT4FS_DBG("");
385
386 int rc;
387
388 bool has_children;
389 rc = ext4fs_has_children(&has_children, fn);
390 if (rc != EOK) {
391 ext4fs_node_put(fn);
392 return rc;
393 }
394
395 if (has_children) {
396 EXT4FS_DBG("destroying non-empty node");
397 ext4fs_node_put(fn);
398 return EINVAL;
399 }
400
401 ext4fs_node_t *enode = EXT4FS_NODE(fn);
402 ext4_filesystem_t *fs = enode->instance->filesystem;
403 ext4_inode_ref_t *inode_ref = enode->inode_ref;
404
405 EXT4FS_DBG("destroying \%u", inode_ref->index);
406
407 rc = ext4_filesystem_truncate_inode(fs, inode_ref, 0);
408 if (rc != EOK) {
409 ext4fs_node_put(fn);
410 return rc;
411 }
412
413 rc = ext4_filesystem_free_inode(fs, inode_ref);
414 if (rc != EOK) {
415 ext4fs_node_put(fn);
416 return rc;
417 }
418
419 ext4fs_node_put(fn);
420 return EOK;
421
422// EXT4FS_DBG("not supported");
423//
424// // TODO
425// return ENOTSUP;
426}
427
428
429int ext4fs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
430{
431 EXT4FS_DBG("not supported");
432
433 // TODO
434 return ENOTSUP;
435}
436
437
438int ext4fs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
439{
440 EXT4FS_DBG("unlinking \%s", name);
441
442 int rc;
443
444 bool has_children;
445 rc = ext4fs_has_children(&has_children, cfn);
446 if (rc != EOK) {
447 EXT4FS_DBG("\%s error: \%u", name, rc);
448 return rc;
449 }
450
451 // Cannot unlink non-empty node
452 if (has_children) {
453 EXT4FS_DBG("\%s is not empty", name);
454 return ENOTEMPTY;
455 }
456
457 // Remove entry from parent directory
458 ext4_inode_ref_t *parent = EXT4FS_NODE(pfn)->inode_ref;
459 ext4_filesystem_t *fs = EXT4FS_NODE(pfn)->instance->filesystem;
460 rc = ext4_directory_remove_entry(fs, parent, name);
461 if (rc != EOK) {
462 EXT4FS_DBG("\%s removing entry failed: \%u", name, rc);
463 return rc;
464 }
465
466 // Decrement links count
467 ext4_inode_ref_t * child_inode_ref = EXT4FS_NODE(cfn)->inode_ref;
468
469 uint32_t lnk_count = ext4_inode_get_links_count(child_inode_ref->inode);
470 EXT4FS_DBG("link count before == \%u", lnk_count);
471 lnk_count--;
472
473
474 // If directory - handle links from parent
475 if (lnk_count <= 1 && ext4fs_is_directory(cfn)) {
476
477 if (lnk_count) {
478 lnk_count--;
479 }
480
481 ext4_inode_ref_t *parent_inode_ref = EXT4FS_NODE(pfn)->inode_ref;
482
483 EXT4FS_DBG("parent index = \%u", parent_inode_ref->index);
484
485 uint32_t parent_lnk_count = ext4_inode_get_links_count(
486 parent_inode_ref->inode);
487
488 EXT4FS_DBG("directory will be removed, parent link count = \%u", parent_lnk_count);
489
490 parent_lnk_count--;
491 ext4_inode_set_links_count(parent_inode_ref->inode, parent_lnk_count);
492
493 parent_inode_ref->dirty = true;
494 }
495
496 ext4_inode_set_links_count(child_inode_ref->inode, lnk_count);
497 child_inode_ref->dirty = true;
498
499 EXT4FS_DBG("link count after == \%u", lnk_count);
500
501 return EOK;
502}
503
504
505int ext4fs_has_children(bool *has_children, fs_node_t *fn)
506{
507 ext4fs_node_t *enode = EXT4FS_NODE(fn);
508 ext4_directory_iterator_t it;
509 ext4_filesystem_t *fs;
510 int rc;
511 bool found = false;
512 size_t name_size;
513
514 fs = enode->instance->filesystem;
515
516 if (!ext4_inode_is_type(fs->superblock, enode->inode_ref->inode,
517 EXT4_INODE_MODE_DIRECTORY)) {
518 *has_children = false;
519 return EOK;
520 }
521
522 rc = ext4_directory_iterator_init(&it, fs, enode->inode_ref, 0);
523 if (rc != EOK) {
524 return rc;
525 }
526
527 /* Find a non-empty directory entry */
528 while (it.current != NULL) {
529 if (it.current->inode != 0) {
530 name_size = ext4_directory_entry_ll_get_name_length(fs->superblock,
531 it.current);
532 if (!ext4fs_is_dots(it.current->name, name_size)) {
533 found = true;
534 break;
535 }
536 }
537
538 rc = ext4_directory_iterator_next(&it);
539 if (rc != EOK) {
540 ext4_directory_iterator_fini(&it);
541 return rc;
542 }
543 }
544
545 rc = ext4_directory_iterator_fini(&it);
546 if (rc != EOK) {
547 return rc;
548 }
549
550 *has_children = found;
551
552 return EOK;
553}
554
555
556fs_index_t ext4fs_index_get(fs_node_t *fn)
557{
558 ext4fs_node_t *enode = EXT4FS_NODE(fn);
559 return enode->inode_ref->index;
560}
561
562
563aoff64_t ext4fs_size_get(fs_node_t *fn)
564{
565 ext4fs_node_t *enode = EXT4FS_NODE(fn);
566 aoff64_t size = ext4_inode_get_size(enode->instance->filesystem->superblock,
567 enode->inode_ref->inode);
568 return size;
569}
570
571
572unsigned ext4fs_lnkcnt_get(fs_node_t *fn)
573{
574 ext4fs_node_t *enode = EXT4FS_NODE(fn);
575 return ext4_inode_get_links_count(enode->inode_ref->inode);
576}
577
578
579bool ext4fs_is_directory(fs_node_t *fn)
580{
581 ext4fs_node_t *enode = EXT4FS_NODE(fn);
582 bool is_dir = ext4_inode_is_type(enode->instance->filesystem->superblock,
583 enode->inode_ref->inode, EXT4_INODE_MODE_DIRECTORY);
584 return is_dir;
585}
586
587
588bool ext4fs_is_file(fs_node_t *fn)
589{
590 ext4fs_node_t *enode = EXT4FS_NODE(fn);
591 bool is_file = ext4_inode_is_type(enode->instance->filesystem->superblock,
592 enode->inode_ref->inode, EXT4_INODE_MODE_FILE);
593 return is_file;
594}
595
596
597service_id_t ext4fs_service_get(fs_node_t *fn)
598{
599 ext4fs_node_t *enode = EXT4FS_NODE(fn);
600 return enode->instance->service_id;
601}
602
603/*
604 * libfs operations.
605 */
606libfs_ops_t ext4fs_libfs_ops = {
607 .root_get = ext4fs_root_get,
608 .match = ext4fs_match,
609 .node_get = ext4fs_node_get,
610 .node_open = ext4fs_node_open,
611 .node_put = ext4fs_node_put,
612 .create = ext4fs_create_node,
613 .destroy = ext4fs_destroy_node,
614 .link = ext4fs_link,
615 .unlink = ext4fs_unlink,
616 .has_children = ext4fs_has_children,
617 .index_get = ext4fs_index_get,
618 .size_get = ext4fs_size_get,
619 .lnkcnt_get = ext4fs_lnkcnt_get,
620 .is_directory = ext4fs_is_directory,
621 .is_file = ext4fs_is_file,
622 .service_get = ext4fs_service_get
623};
624
625
626/*
627 * VFS operations.
628 */
629
630static int ext4fs_mounted(service_id_t service_id, const char *opts,
631 fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
632{
633 int rc;
634 ext4_filesystem_t *fs;
635 ext4fs_instance_t *inst;
636 bool read_only;
637
638 /* Allocate libext4 filesystem structure */
639 fs = (ext4_filesystem_t *) malloc(sizeof(ext4_filesystem_t));
640 if (fs == NULL) {
641 return ENOMEM;
642 }
643
644 /* Allocate instance structure */
645 inst = (ext4fs_instance_t *) malloc(sizeof(ext4fs_instance_t));
646 if (inst == NULL) {
647 free(fs);
648 return ENOMEM;
649 }
650
651 /* Initialize the filesystem */
652 rc = ext4_filesystem_init(fs, service_id);
653 if (rc != EOK) {
654 free(fs);
655 free(inst);
656 return rc;
657 }
658
659 /* Do some sanity checking */
660 rc = ext4_filesystem_check_sanity(fs);
661 if (rc != EOK) {
662 ext4_filesystem_fini(fs, false);
663 free(fs);
664 free(inst);
665 return rc;
666 }
667
668 /* Check flags */
669 rc = ext4_filesystem_check_features(fs, &read_only);
670 if (rc != EOK) {
671 ext4_filesystem_fini(fs, false);
672 free(fs);
673 free(inst);
674 return rc;
675 }
676
677 /* Initialize instance */
678 link_initialize(&inst->link);
679 inst->service_id = service_id;
680 inst->filesystem = fs;
681 inst->open_nodes_count = 0;
682
683 /* Read root node */
684 fs_node_t *root_node;
685 rc = ext4fs_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
686 if (rc != EOK) {
687 ext4_filesystem_fini(fs, false);
688 free(fs);
689 free(inst);
690 return rc;
691 }
692 ext4fs_node_t *enode = EXT4FS_NODE(root_node);
693
694 /* Add instance to the list */
695 fibril_mutex_lock(&instance_list_mutex);
696 list_append(&inst->link, &instance_list);
697 fibril_mutex_unlock(&instance_list_mutex);
698
699 *index = EXT4_INODE_ROOT_INDEX;
700 *size = 0;
701 *lnkcnt = ext4_inode_get_links_count(enode->inode_ref->inode);
702
703 ext4fs_node_put(root_node);
704
705 return EOK;
706}
707
708
709static int ext4fs_unmounted(service_id_t service_id)
710{
711 int rc;
712 ext4fs_instance_t *inst;
713
714 rc = ext4fs_instance_get(service_id, &inst);
715
716 if (rc != EOK) {
717 return rc;
718 }
719
720 fibril_mutex_lock(&open_nodes_lock);
721
722 if (inst->open_nodes_count != 0) {
723 fibril_mutex_unlock(&open_nodes_lock);
724 return EBUSY;
725 }
726
727 /* Remove the instance from the list */
728 fibril_mutex_lock(&instance_list_mutex);
729 list_remove(&inst->link);
730 fibril_mutex_unlock(&instance_list_mutex);
731
732 fibril_mutex_unlock(&open_nodes_lock);
733
734 return ext4_filesystem_fini(inst->filesystem, true);
735}
736
737
738static int
739ext4fs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
740 size_t *rbytes)
741{
742 ext4fs_instance_t *inst;
743 ext4_inode_ref_t *inode_ref;
744 int rc;
745
746 /*
747 * Receive the read request.
748 */
749 ipc_callid_t callid;
750 size_t size;
751 if (!async_data_read_receive(&callid, &size)) {
752 async_answer_0(callid, EINVAL);
753 return EINVAL;
754 }
755
756 rc = ext4fs_instance_get(service_id, &inst);
757 if (rc != EOK) {
758 async_answer_0(callid, rc);
759 return rc;
760 }
761
762 rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
763 if (rc != EOK) {
764 async_answer_0(callid, rc);
765 return rc;
766 }
767
768 if (ext4_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
769 EXT4_INODE_MODE_FILE)) {
770 rc = ext4fs_read_file(callid, pos, size, inst, inode_ref,
771 rbytes);
772 } else if (ext4_inode_is_type(inst->filesystem->superblock,
773 inode_ref->inode, EXT4_INODE_MODE_DIRECTORY)) {
774 rc = ext4fs_read_directory(callid, pos, size, inst, inode_ref,
775 rbytes);
776 } else {
777 /* Other inode types not supported */
778 async_answer_0(callid, ENOTSUP);
779 rc = ENOTSUP;
780 }
781
782 ext4_filesystem_put_inode_ref(inode_ref);
783
784 return rc;
785}
786
787bool ext4fs_is_dots(const uint8_t *name, size_t name_size)
788{
789 if (name_size == 1 && name[0] == '.') {
790 return true;
791 }
792
793 if (name_size == 2 && name[0] == '.' && name[1] == '.') {
794 return true;
795 }
796
797 return false;
798}
799
800int ext4fs_read_directory(ipc_callid_t callid, aoff64_t pos, size_t size,
801 ext4fs_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
802{
803 ext4_directory_iterator_t it;
804 aoff64_t next;
805 uint8_t *buf;
806 size_t name_size;
807 int rc;
808 bool found = false;
809
810 rc = ext4_directory_iterator_init(&it, inst->filesystem, inode_ref, pos);
811 if (rc != EOK) {
812 async_answer_0(callid, rc);
813 return rc;
814 }
815
816 /* Find next interesting directory entry.
817 * We want to skip . and .. entries
818 * as these are not used in HelenOS
819 */
820 while (it.current != NULL) {
821
822 if (it.current->inode == 0) {
823 goto skip;
824 }
825
826 name_size = ext4_directory_entry_ll_get_name_length(
827 inst->filesystem->superblock, it.current);
828
829 /* skip . and .. */
830 if (ext4fs_is_dots(it.current->name, name_size)) {
831 goto skip;
832 }
833
834 /* The on-disk entry does not contain \0 at the end
835 * end of entry name, so we copy it to new buffer
836 * and add the \0 at the end
837 */
838 buf = malloc(name_size+1);
839 if (buf == NULL) {
840 ext4_directory_iterator_fini(&it);
841 async_answer_0(callid, ENOMEM);
842 return ENOMEM;
843 }
844 memcpy(buf, &it.current->name, name_size);
845 *(buf + name_size) = 0;
846 found = true;
847 (void) async_data_read_finalize(callid, buf, name_size + 1);
848 free(buf);
849 break;
850
851skip:
852 rc = ext4_directory_iterator_next(&it);
853 if (rc != EOK) {
854 ext4_directory_iterator_fini(&it);
855 async_answer_0(callid, rc);
856 return rc;
857 }
858 }
859
860 if (found) {
861 rc = ext4_directory_iterator_next(&it);
862 if (rc != EOK) {
863 return rc;
864 }
865 next = it.current_offset;
866 }
867
868 rc = ext4_directory_iterator_fini(&it);
869 if (rc != EOK) {
870 return rc;
871 }
872
873 if (found) {
874 *rbytes = next - pos;
875 return EOK;
876 } else {
877 async_answer_0(callid, ENOENT);
878 return ENOENT;
879 }
880}
881
882int ext4fs_read_file(ipc_callid_t callid, aoff64_t pos, size_t size,
883 ext4fs_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
884{
885 int rc;
886 uint32_t block_size;
887 aoff64_t file_block;
888 uint64_t file_size;
889 uint32_t fs_block;
890 size_t offset_in_block;
891 size_t bytes;
892 block_t *block;
893 uint8_t *buffer;
894
895 file_size = ext4_inode_get_size(inst->filesystem->superblock,
896 inode_ref->inode);
897
898 if (pos >= file_size) {
899 /* Read 0 bytes successfully */
900 async_data_read_finalize(callid, NULL, 0);
901 *rbytes = 0;
902 return EOK;
903 }
904
905 /* For now, we only read data from one block at a time */
906 block_size = ext4_superblock_get_block_size(inst->filesystem->superblock);
907 file_block = pos / block_size;
908 offset_in_block = pos % block_size;
909 bytes = min(block_size - offset_in_block, size);
910
911 /* Handle end of file */
912 if (pos + bytes > file_size) {
913 bytes = file_size - pos;
914 }
915
916 /* Get the real block number */
917 rc = ext4_filesystem_get_inode_data_block_index(inst->filesystem,
918 inode_ref->inode, file_block, &fs_block);
919 if (rc != EOK) {
920 async_answer_0(callid, rc);
921 return rc;
922 }
923
924 /* Check for sparse file
925 * If ext4_filesystem_get_inode_data_block_index returned
926 * fs_block == 0, it means that the given block is not allocated for the
927 * file and we need to return a buffer of zeros
928 */
929 if (fs_block == 0) {
930 buffer = malloc(bytes);
931 if (buffer == NULL) {
932 async_answer_0(callid, ENOMEM);
933 return ENOMEM;
934 }
935
936 memset(buffer, 0, bytes);
937
938 async_data_read_finalize(callid, buffer, bytes);
939 *rbytes = bytes;
940
941 free(buffer);
942
943 return EOK;
944 }
945
946 /* Usual case - we need to read a block from device */
947 rc = block_get(&block, inst->service_id, fs_block, BLOCK_FLAGS_NONE);
948 if (rc != EOK) {
949 async_answer_0(callid, rc);
950 return rc;
951 }
952
953 assert(offset_in_block + bytes <= block_size);
954 async_data_read_finalize(callid, block->data + offset_in_block, bytes);
955
956 rc = block_put(block);
957 if (rc != EOK) {
958 return rc;
959 }
960
961 *rbytes = bytes;
962 return EOK;
963}
964
965static int
966ext4fs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
967 size_t *wbytes, aoff64_t *nsize)
968{
969 int rc;
970 int flags = BLOCK_FLAGS_NONE;
971 fs_node_t *fn;
972 ext4fs_node_t *enode;
973 ext4_filesystem_t *fs;
974 ext4_inode_ref_t *inode_ref;
975 ipc_callid_t callid;
976 size_t len, bytes, block_size;
977 block_t *write_block;
978 uint32_t fblock, iblock;
979 uint32_t old_inode_size;
980
981 rc = ext4fs_node_get(&fn, service_id, index);
982 if (rc != EOK) {
983 EXT4FS_DBG("node get error");
984 return rc;
985 }
986
987 if (!async_data_write_receive(&callid, &len)) {
988 rc = EINVAL;
989 ext4fs_node_put(fn);
990 async_answer_0(callid, rc);
991 EXT4FS_DBG("data write recv");
992 return rc;
993 }
994
995
996 enode = EXT4FS_NODE(fn);
997 inode_ref = enode->inode_ref;
998 fs = enode->instance->filesystem;
999
1000 block_size = ext4_superblock_get_block_size(fs->superblock);
1001
1002 // Prevent writing to more than one block
1003 bytes = min(len, block_size - (pos % block_size));
1004
1005 if (bytes == block_size) {
1006 flags = BLOCK_FLAGS_NOREAD;
1007 }
1008
1009 iblock = pos / block_size;
1010
1011 rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, iblock, &fblock);
1012 if (rc != EOK) {
1013 // TODO error
1014 ext4fs_node_put(fn);
1015 EXT4FS_DBG("error loading block addr");
1016 return rc;
1017 }
1018
1019 if (fblock == 0) {
1020 rc = ext4_balloc_alloc_block(fs, inode_ref, &fblock);
1021 if (rc != EOK) {
1022 ext4fs_node_put(fn);
1023 async_answer_0(callid, rc);
1024 return rc;
1025 }
1026
1027 rc = ext4_filesystem_set_inode_data_block_index(fs, inode_ref, iblock, fblock);
1028 if (rc != EOK) {
1029 EXT4FS_DBG("ERROR: setting index failed");
1030 }
1031 inode_ref->dirty = true;
1032
1033 flags = BLOCK_FLAGS_NOREAD;
1034 }
1035
1036 rc = block_get(&write_block, service_id, fblock, flags);
1037 if (rc != EOK) {
1038 EXT4FS_DBG("error in loading block \%d", rc);
1039 ext4fs_node_put(fn);
1040 async_answer_0(callid, rc);
1041 return rc;
1042 }
1043
1044 if (flags == BLOCK_FLAGS_NOREAD) {
1045 memset(write_block->data, 0, block_size);
1046 }
1047
1048 rc = async_data_write_finalize(callid, write_block->data + (pos % block_size), bytes);
1049 if (rc != EOK) {
1050 // TODO error
1051 EXT4FS_DBG("error in write finalize \%d", rc);
1052 }
1053
1054 write_block->dirty = true;
1055
1056 rc = block_put(write_block);
1057 if (rc != EOK) {
1058 EXT4FS_DBG("error in writing block \%d", rc);
1059 ext4fs_node_put(fn);
1060 return rc;
1061 }
1062
1063 old_inode_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
1064 if (pos + bytes > old_inode_size) {
1065 ext4_inode_set_size(inode_ref->inode, pos + bytes);
1066 inode_ref->dirty = true;
1067 }
1068
1069 *nsize = ext4_inode_get_size(fs->superblock, inode_ref->inode);
1070 *wbytes = bytes;
1071
1072 return ext4fs_node_put(fn);
1073}
1074
1075
1076static int
1077ext4fs_truncate(service_id_t service_id, fs_index_t index, aoff64_t new_size)
1078{
1079 int rc;
1080 fs_node_t *fn;
1081
1082 rc = ext4fs_node_get(&fn, service_id, index);
1083 if (rc != EOK) {
1084 return rc;
1085 }
1086
1087 ext4fs_node_t *enode = EXT4FS_NODE(fn);
1088 ext4_inode_ref_t *inode_ref = enode->inode_ref;
1089 ext4_filesystem_t *fs = enode->instance->filesystem;
1090
1091 rc = ext4_filesystem_truncate_inode(fs, inode_ref, new_size);
1092 ext4fs_node_put(fn);
1093
1094 return rc;
1095}
1096
1097
1098static int ext4fs_close(service_id_t service_id, fs_index_t index)
1099{
1100 return EOK;
1101}
1102
1103
1104static int ext4fs_destroy(service_id_t service_id, fs_index_t index)
1105{
1106 int rc;
1107 fs_node_t *fn;
1108
1109 rc = ext4fs_node_get(&fn, service_id, index);
1110 if (rc != EOK) {
1111 return rc;
1112 }
1113
1114 /* Destroy the inode */
1115 return ext4fs_destroy_node(fn);
1116}
1117
1118
1119static int ext4fs_sync(service_id_t service_id, fs_index_t index)
1120{
1121 int rc;
1122 fs_node_t *fn;
1123 ext4fs_node_t *enode;
1124
1125 rc = ext4fs_node_get(&fn, service_id, index);
1126 if (rc != EOK) {
1127 return rc;
1128 }
1129
1130 enode = EXT4FS_NODE(fn);
1131 enode->inode_ref->dirty = true;
1132
1133 return ext4fs_node_put(fn);
1134}
1135
1136vfs_out_ops_t ext4fs_ops = {
1137 .mounted = ext4fs_mounted,
1138 .unmounted = ext4fs_unmounted,
1139 .read = ext4fs_read,
1140 .write = ext4fs_write,
1141 .truncate = ext4fs_truncate,
1142 .close = ext4fs_close,
1143 .destroy = ext4fs_destroy,
1144 .sync = ext4fs_sync,
1145};
1146
1147/**
1148 * @}
1149 */
Note: See TracBrowser for help on using the repository browser.