1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
|
/*
* Copyright (c) 2011 Martin Sucha
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup fs
* @{
*/
/**
* @file ext2fs_ops.c
* @brief Implementation of VFS operations for the EXT2 file system server.
*/
#include "ext2fs.h"
#include "../../vfs/vfs.h"
#include <libfs.h>
#include <block.h>
#include <libext2.h>
#include <ipc/services.h>
#include <ipc/loc.h>
#include <macros.h>
#include <async.h>
#include <errno.h>
#include <str.h>
#include <byteorder.h>
#include <adt/hash_table.h>
#include <adt/hash.h>
#include <adt/list.h>
#include <assert.h>
#include <fibril_synch.h>
#include <sys/mman.h>
#include <align.h>
#include <adt/hash_table.h>
#include <sys/typefmt.h>
#include <malloc.h>
#include <stdio.h>
#include <inttypes.h>
#define EXT2FS_NODE(node) ((node) ? (ext2fs_node_t *) (node)->data : NULL)
#define EXT2FS_DBG(format, ...) {if (false) printf("ext2fs: %s: " format "\n", __FUNCTION__, ##__VA_ARGS__);}
typedef struct ext2fs_instance {
link_t link;
service_id_t service_id;
ext2_filesystem_t *filesystem;
unsigned int open_nodes_count;
} ext2fs_instance_t;
typedef struct ext2fs_node {
ext2fs_instance_t *instance;
ext2_inode_ref_t *inode_ref;
fs_node_t *fs_node;
ht_link_t link;
unsigned int references;
} ext2fs_node_t;
/*
* Forward declarations of auxiliary functions
*/
static int ext2fs_instance_get(service_id_t, ext2fs_instance_t **);
static int ext2fs_read_directory(ipc_callid_t, aoff64_t, size_t,
ext2fs_instance_t *, ext2_inode_ref_t *, size_t *);
static int ext2fs_read_file(ipc_callid_t, aoff64_t, size_t, ext2fs_instance_t *,
ext2_inode_ref_t *, size_t *);
static bool ext2fs_is_dots(const uint8_t *, size_t);
static int ext2fs_node_get_core(fs_node_t **, ext2fs_instance_t *, fs_index_t);
static int ext2fs_node_put_core(ext2fs_node_t *);
/*
* Forward declarations of EXT2 libfs operations.
*/
static int ext2fs_root_get(fs_node_t **, service_id_t);
static int ext2fs_match(fs_node_t **, fs_node_t *, const char *);
static int ext2fs_node_get(fs_node_t **, service_id_t, fs_index_t);
static int ext2fs_node_open(fs_node_t *);
static int ext2fs_node_put(fs_node_t *);
static int ext2fs_create_node(fs_node_t **, service_id_t, int);
static int ext2fs_destroy_node(fs_node_t *);
static int ext2fs_link(fs_node_t *, fs_node_t *, const char *);
static int ext2fs_unlink(fs_node_t *, fs_node_t *, const char *);
static int ext2fs_has_children(bool *, fs_node_t *);
static fs_index_t ext2fs_index_get(fs_node_t *);
static aoff64_t ext2fs_size_get(fs_node_t *);
static unsigned ext2fs_lnkcnt_get(fs_node_t *);
static bool ext2fs_is_directory(fs_node_t *);
static bool ext2fs_is_file(fs_node_t *node);
static service_id_t ext2fs_service_get(fs_node_t *node);
/*
* Static variables
*/
static LIST_INITIALIZE(instance_list);
static FIBRIL_MUTEX_INITIALIZE(instance_list_mutex);
static hash_table_t open_nodes;
static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
/*
* Hash table interface for open nodes hash table
*/
typedef struct {
service_id_t service_id;
fs_index_t index;
} node_key_t;
static size_t open_nodes_key_hash(void *key)
{
node_key_t *node_key = (node_key_t*)key;
return hash_combine(node_key->service_id, node_key->index);
}
static size_t open_nodes_hash(const ht_link_t *item)
{
ext2fs_node_t *enode = hash_table_get_inst(item, ext2fs_node_t, link);
assert(enode->instance);
assert(enode->inode_ref);
return hash_combine(enode->instance->service_id, enode->inode_ref->index);
}
static bool open_nodes_key_equal(void *key, const ht_link_t *item)
{
node_key_t *node_key = (node_key_t*)key;
ext2fs_node_t *enode = hash_table_get_inst(item, ext2fs_node_t, link);
return node_key->service_id == enode->instance->service_id
&& node_key->index == enode->inode_ref->index;
}
static hash_table_ops_t open_nodes_ops = {
.hash = open_nodes_hash,
.key_hash = open_nodes_key_hash,
.key_equal = open_nodes_key_equal,
.equal = NULL,
.remove_callback = NULL,
};
/**
*
*/
int ext2fs_global_init(void)
{
if (!hash_table_create(&open_nodes, 0, 0, &open_nodes_ops)) {
return ENOMEM;
}
return EOK;
}
int ext2fs_global_fini(void)
{
hash_table_destroy(&open_nodes);
return EOK;
}
/*
* EXT2 libfs operations.
*/
/**
* Find an instance of filesystem for the given service_id
*/
int ext2fs_instance_get(service_id_t service_id, ext2fs_instance_t **inst)
{
EXT2FS_DBG("(%" PRIun ", -)", service_id);
ext2fs_instance_t *tmp;
fibril_mutex_lock(&instance_list_mutex);
if (list_empty(&instance_list)) {
EXT2FS_DBG("list empty");
fibril_mutex_unlock(&instance_list_mutex);
return EINVAL;
}
list_foreach(instance_list, link) {
tmp = list_get_instance(link, ext2fs_instance_t, link);
if (tmp->service_id == service_id) {
*inst = tmp;
fibril_mutex_unlock(&instance_list_mutex);
return EOK;
}
}
EXT2FS_DBG("not found");
fibril_mutex_unlock(&instance_list_mutex);
return EINVAL;
}
int ext2fs_root_get(fs_node_t **rfn, service_id_t service_id)
{
EXT2FS_DBG("(-, %" PRIun ")", service_id);
return ext2fs_node_get(rfn, service_id, EXT2_INODE_ROOT_INDEX);
}
int ext2fs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
{
EXT2FS_DBG("(-,-,%s)", component);
ext2fs_node_t *eparent = EXT2FS_NODE(pfn);
ext2_filesystem_t *fs;
ext2_directory_iterator_t it;
int rc;
size_t name_size;
size_t component_size;
bool found = false;
uint32_t inode;
fs = eparent->instance->filesystem;
if (!ext2_inode_is_type(fs->superblock, eparent->inode_ref->inode,
EXT2_INODE_MODE_DIRECTORY)) {
return ENOTDIR;
}
rc = ext2_directory_iterator_init(&it, fs, eparent->inode_ref, 0);
if (rc != EOK) {
return rc;
}
/* Find length of component in bytes
* TODO: check for library function call that does this
*/
component_size = 0;
while (*(component+component_size) != 0) {
component_size++;
}
while (it.current != NULL) {
inode = ext2_directory_entry_ll_get_inode(it.current);
/* ignore empty directory entries */
if (inode != 0) {
name_size = ext2_directory_entry_ll_get_name_length(fs->superblock,
it.current);
if (name_size == component_size && bcmp(component, &it.current->name,
name_size) == 0) {
rc = ext2fs_node_get_core(rfn, eparent->instance,
inode);
if (rc != EOK) {
ext2_directory_iterator_fini(&it);
return rc;
}
found = true;
break;
}
}
rc = ext2_directory_iterator_next(&it);
if (rc != EOK) {
ext2_directory_iterator_fini(&it);
return rc;
}
}
ext2_directory_iterator_fini(&it);
if (!found) {
return ENOENT;
}
return EOK;
}
/** Instantiate a EXT2 in-core node. */
int ext2fs_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
{
EXT2FS_DBG("(-,%" PRIun ",%u)", service_id, index);
ext2fs_instance_t *inst = NULL;
int rc;
rc = ext2fs_instance_get(service_id, &inst);
if (rc != EOK) {
return rc;
}
return ext2fs_node_get_core(rfn, inst, index);
}
int ext2fs_node_get_core(fs_node_t **rfn, ext2fs_instance_t *inst,
fs_index_t index)
{
int rc;
fs_node_t *node = NULL;
ext2fs_node_t *enode = NULL;
ext2_inode_ref_t *inode_ref = NULL;
fibril_mutex_lock(&open_nodes_lock);
/* Check if the node is not already open */
node_key_t key = {
.service_id = inst->service_id,
.index = index
};
ht_link_t *already_open = hash_table_find(&open_nodes, &key);
if (already_open) {
enode = hash_table_get_inst(already_open, ext2fs_node_t, link);
*rfn = enode->fs_node;
enode->references++;
fibril_mutex_unlock(&open_nodes_lock);
return EOK;
}
enode = malloc(sizeof(ext2fs_node_t));
if (enode == NULL) {
fibril_mutex_unlock(&open_nodes_lock);
return ENOMEM;
}
node = malloc(sizeof(fs_node_t));
if (node == NULL) {
free(enode);
fibril_mutex_unlock(&open_nodes_lock);
return ENOMEM;
}
fs_node_initialize(node);
rc = ext2_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
if (rc != EOK) {
free(enode);
free(node);
fibril_mutex_unlock(&open_nodes_lock);
return rc;
}
enode->inode_ref = inode_ref;
enode->instance = inst;
enode->references = 1;
enode->fs_node = node;
node->data = enode;
*rfn = node;
hash_table_insert(&open_nodes, &enode->link);
inst->open_nodes_count++;
EXT2FS_DBG("inode: %u", inode_ref->index);
EXT2FS_DBG("EOK");
fibril_mutex_unlock(&open_nodes_lock);
return EOK;
}
int ext2fs_node_open(fs_node_t *fn)
{
EXT2FS_DBG("");
/*
* Opening a file is stateless, nothing
* to be done here.
*/
return EOK;
}
int ext2fs_node_put(fs_node_t *fn)
{
EXT2FS_DBG("");
int rc;
ext2fs_node_t *enode = EXT2FS_NODE(fn);
fibril_mutex_lock(&open_nodes_lock);
assert(enode->references > 0);
enode->references--;
if (enode->references == 0) {
rc = ext2fs_node_put_core(enode);
if (rc != EOK) {
fibril_mutex_unlock(&open_nodes_lock);
return rc;
}
}
fibril_mutex_unlock(&open_nodes_lock);
return EOK;
}
int ext2fs_node_put_core(ext2fs_node_t *enode)
{
node_key_t key = {
.service_id = enode->instance->service_id,
.index = enode->inode_ref->index
};
hash_table_remove(&open_nodes, &key);
assert(enode->instance->open_nodes_count > 0);
enode->instance->open_nodes_count--;
int rc = ext2_filesystem_put_inode_ref(enode->inode_ref);
if (rc != EOK) {
EXT2FS_DBG("ext2_filesystem_put_inode_ref failed");
return rc;
}
free(enode->fs_node);
free(enode);
return EOK;
}
int ext2fs_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
{
EXT2FS_DBG("");
// TODO
return ENOTSUP;
}
int ext2fs_destroy_node(fs_node_t *fn)
{
EXT2FS_DBG("");
// TODO
return ENOTSUP;
}
int ext2fs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
{
EXT2FS_DBG("");
// TODO
return ENOTSUP;
}
int ext2fs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
{
EXT2FS_DBG("");
// TODO
return ENOTSUP;
}
int ext2fs_has_children(bool *has_children, fs_node_t *fn)
{
EXT2FS_DBG("");
ext2fs_node_t *enode = EXT2FS_NODE(fn);
ext2_directory_iterator_t it;
ext2_filesystem_t *fs;
int rc;
bool found = false;
size_t name_size;
fs = enode->instance->filesystem;
if (!ext2_inode_is_type(fs->superblock, enode->inode_ref->inode,
EXT2_INODE_MODE_DIRECTORY)) {
*has_children = false;
EXT2FS_DBG("EOK - false");
return EOK;
}
rc = ext2_directory_iterator_init(&it, fs, enode->inode_ref, 0);
if (rc != EOK) {
EXT2FS_DBG("error %u", rc);
return rc;
}
/* Find a non-empty directory entry */
while (it.current != NULL) {
if (it.current->inode != 0) {
name_size = ext2_directory_entry_ll_get_name_length(fs->superblock,
it.current);
if (!ext2fs_is_dots(&it.current->name, name_size)) {
found = true;
break;
}
}
rc = ext2_directory_iterator_next(&it);
if (rc != EOK) {
ext2_directory_iterator_fini(&it);
EXT2FS_DBG("error %u", rc);
return rc;
}
}
rc = ext2_directory_iterator_fini(&it);
if (rc != EOK) {
EXT2FS_DBG("error %u", rc);
return rc;
}
*has_children = found;
EXT2FS_DBG("EOK");
return EOK;
}
fs_index_t ext2fs_index_get(fs_node_t *fn)
{
ext2fs_node_t *enode = EXT2FS_NODE(fn);
EXT2FS_DBG("%u", enode->inode_ref->index);
return enode->inode_ref->index;
}
aoff64_t ext2fs_size_get(fs_node_t *fn)
{
ext2fs_node_t *enode = EXT2FS_NODE(fn);
aoff64_t size = ext2_inode_get_size(enode->instance->filesystem->superblock,
enode->inode_ref->inode);
EXT2FS_DBG("%" PRIu64, size);
return size;
}
unsigned ext2fs_lnkcnt_get(fs_node_t *fn)
{
ext2fs_node_t *enode = EXT2FS_NODE(fn);
unsigned count = ext2_inode_get_usage_count(enode->inode_ref->inode);
EXT2FS_DBG("%u", count);
return count;
}
bool ext2fs_is_directory(fs_node_t *fn)
{
ext2fs_node_t *enode = EXT2FS_NODE(fn);
bool is_dir = ext2_inode_is_type(enode->instance->filesystem->superblock,
enode->inode_ref->inode, EXT2_INODE_MODE_DIRECTORY);
EXT2FS_DBG("%s", is_dir ? "true" : "false");
EXT2FS_DBG("%u", enode->inode_ref->index);
return is_dir;
}
bool ext2fs_is_file(fs_node_t *fn)
{
ext2fs_node_t *enode = EXT2FS_NODE(fn);
bool is_file = ext2_inode_is_type(enode->instance->filesystem->superblock,
enode->inode_ref->inode, EXT2_INODE_MODE_FILE);
EXT2FS_DBG("%s", is_file ? "true" : "false");
return is_file;
}
service_id_t ext2fs_service_get(fs_node_t *fn)
{
EXT2FS_DBG("");
ext2fs_node_t *enode = EXT2FS_NODE(fn);
return enode->instance->service_id;
}
/** libfs operations */
libfs_ops_t ext2fs_libfs_ops = {
.root_get = ext2fs_root_get,
.match = ext2fs_match,
.node_get = ext2fs_node_get,
.node_open = ext2fs_node_open,
.node_put = ext2fs_node_put,
.create = ext2fs_create_node,
.destroy = ext2fs_destroy_node,
.link = ext2fs_link,
.unlink = ext2fs_unlink,
.has_children = ext2fs_has_children,
.index_get = ext2fs_index_get,
.size_get = ext2fs_size_get,
.lnkcnt_get = ext2fs_lnkcnt_get,
.is_directory = ext2fs_is_directory,
.is_file = ext2fs_is_file,
.service_get = ext2fs_service_get
};
/*
* VFS operations.
*/
static int ext2fs_mounted(service_id_t service_id, const char *opts,
fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
{
EXT2FS_DBG("");
int rc;
ext2_filesystem_t *fs;
ext2fs_instance_t *inst;
bool read_only;
/* Allocate libext2 filesystem structure */
fs = (ext2_filesystem_t *) malloc(sizeof(ext2_filesystem_t));
if (fs == NULL)
return ENOMEM;
/* Allocate instance structure */
inst = (ext2fs_instance_t *) malloc(sizeof(ext2fs_instance_t));
if (inst == NULL) {
free(fs);
return ENOMEM;
}
/* Initialize the filesystem */
rc = ext2_filesystem_init(fs, service_id);
if (rc != EOK) {
free(fs);
free(inst);
return rc;
}
/* Do some sanity checking */
rc = ext2_filesystem_check_sanity(fs);
if (rc != EOK) {
ext2_filesystem_fini(fs);
free(fs);
free(inst);
return rc;
}
/* Check flags */
rc = ext2_filesystem_check_flags(fs, &read_only);
if (rc != EOK) {
ext2_filesystem_fini(fs);
free(fs);
free(inst);
return rc;
}
/* Initialize instance */
link_initialize(&inst->link);
inst->service_id = service_id;
inst->filesystem = fs;
inst->open_nodes_count = 0;
/* Read root node */
fs_node_t *root_node;
rc = ext2fs_node_get_core(&root_node, inst, EXT2_INODE_ROOT_INDEX);
if (rc != EOK) {
ext2_filesystem_fini(fs);
free(fs);
free(inst);
return rc;
}
ext2fs_node_t *enode = EXT2FS_NODE(root_node);
/* Add instance to the list */
fibril_mutex_lock(&instance_list_mutex);
list_append(&inst->link, &instance_list);
fibril_mutex_unlock(&instance_list_mutex);
*index = EXT2_INODE_ROOT_INDEX;
*size = 0;
*lnkcnt = ext2_inode_get_usage_count(enode->inode_ref->inode);
ext2fs_node_put(root_node);
return EOK;
}
static int ext2fs_unmounted(service_id_t service_id)
{
EXT2FS_DBG("");
ext2fs_instance_t *inst;
int rc;
rc = ext2fs_instance_get(service_id, &inst);
if (rc != EOK)
return rc;
fibril_mutex_lock(&open_nodes_lock);
EXT2FS_DBG("open_nodes_count = %d", inst->open_nodes_count)
if (inst->open_nodes_count != 0) {
fibril_mutex_unlock(&open_nodes_lock);
return EBUSY;
}
/* Remove the instance from the list */
fibril_mutex_lock(&instance_list_mutex);
list_remove(&inst->link);
fibril_mutex_unlock(&instance_list_mutex);
fibril_mutex_unlock(&open_nodes_lock);
ext2_filesystem_fini(inst->filesystem);
return EOK;
}
static int
ext2fs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
size_t *rbytes)
{
EXT2FS_DBG("");
ext2fs_instance_t *inst;
ext2_inode_ref_t *inode_ref;
int rc;
/*
* Receive the read request.
*/
ipc_callid_t callid;
size_t size;
if (!async_data_read_receive(&callid, &size)) {
async_answer_0(callid, EINVAL);
return EINVAL;
}
rc = ext2fs_instance_get(service_id, &inst);
if (rc != EOK) {
async_answer_0(callid, rc);
return rc;
}
rc = ext2_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
if (rc != EOK) {
async_answer_0(callid, rc);
return rc;
}
if (ext2_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
EXT2_INODE_MODE_FILE)) {
rc = ext2fs_read_file(callid, pos, size, inst, inode_ref,
rbytes);
} else if (ext2_inode_is_type(inst->filesystem->superblock,
inode_ref->inode, EXT2_INODE_MODE_DIRECTORY)) {
rc = ext2fs_read_directory(callid, pos, size, inst, inode_ref,
rbytes);
} else {
/* Other inode types not supported */
async_answer_0(callid, ENOTSUP);
rc = ENOTSUP;
}
ext2_filesystem_put_inode_ref(inode_ref);
return rc;
}
/**
* Determine whether given directory entry name is . or ..
*/
bool ext2fs_is_dots(const uint8_t *name, size_t name_size) {
if (name_size == 1 && name[0] == '.') {
return true;
}
if (name_size == 2 && name[0] == '.' && name[1] == '.') {
return true;
}
return false;
}
int ext2fs_read_directory(ipc_callid_t callid, aoff64_t pos, size_t size,
ext2fs_instance_t *inst, ext2_inode_ref_t *inode_ref, size_t *rbytes)
{
ext2_directory_iterator_t it;
aoff64_t next;
uint8_t *buf;
size_t name_size;
int rc;
bool found = false;
rc = ext2_directory_iterator_init(&it, inst->filesystem, inode_ref, pos);
if (rc != EOK) {
async_answer_0(callid, rc);
return rc;
}
/* Find next interesting directory entry.
* We want to skip . and .. entries
* as these are not used in HelenOS
*/
while (it.current != NULL) {
if (it.current->inode == 0) {
goto skip;
}
name_size = ext2_directory_entry_ll_get_name_length(
inst->filesystem->superblock, it.current);
/* skip . and .. */
if (ext2fs_is_dots(&it.current->name, name_size)) {
goto skip;
}
/* The on-disk entry does not contain \0 at the end
* end of entry name, so we copy it to new buffer
* and add the \0 at the end
*/
buf = malloc(name_size+1);
if (buf == NULL) {
ext2_directory_iterator_fini(&it);
async_answer_0(callid, ENOMEM);
return ENOMEM;
}
memcpy(buf, &it.current->name, name_size);
*(buf + name_size) = 0;
found = true;
(void) async_data_read_finalize(callid, buf, name_size + 1);
free(buf);
break;
skip:
rc = ext2_directory_iterator_next(&it);
if (rc != EOK) {
ext2_directory_iterator_fini(&it);
async_answer_0(callid, rc);
return rc;
}
}
if (found) {
rc = ext2_directory_iterator_next(&it);
if (rc != EOK)
return rc;
next = it.current_offset;
}
rc = ext2_directory_iterator_fini(&it);
if (rc != EOK)
return rc;
if (found) {
*rbytes = next - pos;
return EOK;
} else {
async_answer_0(callid, ENOENT);
return ENOENT;
}
}
int ext2fs_read_file(ipc_callid_t callid, aoff64_t pos, size_t size,
ext2fs_instance_t *inst, ext2_inode_ref_t *inode_ref, size_t *rbytes)
{
int rc;
uint32_t block_size;
aoff64_t file_block;
uint64_t file_size;
uint32_t fs_block;
size_t offset_in_block;
size_t bytes;
block_t *block;
uint8_t *buffer;
file_size = ext2_inode_get_size(inst->filesystem->superblock,
inode_ref->inode);
if (pos >= file_size) {
/* Read 0 bytes successfully */
async_data_read_finalize(callid, NULL, 0);
*rbytes = 0;
return EOK;
}
/* For now, we only read data from one block at a time */
block_size = ext2_superblock_get_block_size(inst->filesystem->superblock);
file_block = pos / block_size;
offset_in_block = pos % block_size;
bytes = min(block_size - offset_in_block, size);
/* Handle end of file */
if (pos + bytes > file_size) {
bytes = file_size - pos;
}
/* Get the real block number */
rc = ext2_filesystem_get_inode_data_block_index(inst->filesystem,
inode_ref->inode, file_block, &fs_block);
if (rc != EOK) {
async_answer_0(callid, rc);
return rc;
}
/* Check for sparse file
* If ext2_filesystem_get_inode_data_block_index returned
* fs_block == 0, it means that the given block is not allocated for the
* file and we need to return a buffer of zeros
*/
if (fs_block == 0) {
buffer = malloc(bytes);
if (buffer == NULL) {
async_answer_0(callid, ENOMEM);
return ENOMEM;
}
memset(buffer, 0, bytes);
async_data_read_finalize(callid, buffer, bytes);
*rbytes = bytes;
free(buffer);
return EOK;
}
/* Usual case - we need to read a block from device */
rc = block_get(&block, inst->service_id, fs_block, BLOCK_FLAGS_NONE);
if (rc != EOK) {
async_answer_0(callid, rc);
return rc;
}
assert(offset_in_block + bytes <= block_size);
async_data_read_finalize(callid, block->data + offset_in_block, bytes);
rc = block_put(block);
if (rc != EOK)
return rc;
*rbytes = bytes;
return EOK;
}
static int
ext2fs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
size_t *wbytes, aoff64_t *nsize)
{
EXT2FS_DBG("");
return ENOTSUP;
}
static int
ext2fs_truncate(service_id_t service_id, fs_index_t index, aoff64_t size)
{
EXT2FS_DBG("");
return ENOTSUP;
}
static int ext2fs_close(service_id_t service_id, fs_index_t index)
{
EXT2FS_DBG("");
return EOK;
}
static int ext2fs_destroy(service_id_t service_id, fs_index_t index)
{
EXT2FS_DBG("");
return ENOTSUP;
}
static int ext2fs_sync(service_id_t service_id, fs_index_t index)
{
EXT2FS_DBG("");
return ENOTSUP;
}
vfs_out_ops_t ext2fs_ops = {
.mounted = ext2fs_mounted,
.unmounted = ext2fs_unmounted,
.read = ext2fs_read,
.write = ext2fs_write,
.truncate = ext2fs_truncate,
.close = ext2fs_close,
.destroy = ext2fs_destroy,
.sync = ext2fs_sync,
};
/**
* @}
*/
|