source: mainline/uspace/srv/fs/ext4fs/ext4fs_ops.c@ 8da2c60

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

bugfix in match operation

  • Property mode set to 100644
File size: 28.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 fibril_mutex_lock(&instance_list_mutex);
172
173 if (list_empty(&instance_list)) {
174 fibril_mutex_unlock(&instance_list_mutex);
175 return EINVAL;
176 }
177
178 ext4fs_instance_t *tmp;
179 list_foreach(instance_list, link) {
180 tmp = list_get_instance(link, ext4fs_instance_t, link);
181
182 if (tmp->service_id == service_id) {
183 *inst = tmp;
184 fibril_mutex_unlock(&instance_list_mutex);
185 return EOK;
186 }
187 }
188
189 fibril_mutex_unlock(&instance_list_mutex);
190 return EINVAL;
191}
192
193
194int ext4fs_root_get(fs_node_t **rfn, service_id_t service_id)
195{
196 return ext4fs_node_get(rfn, service_id, EXT4_INODE_ROOT_INDEX);
197}
198
199
200int ext4fs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
201{
202 int rc;
203
204 ext4fs_node_t *eparent = EXT4FS_NODE(pfn);
205 ext4_filesystem_t *fs = eparent->instance->filesystem;
206
207 if (!ext4_inode_is_type(fs->superblock, eparent->inode_ref->inode,
208 EXT4_INODE_MODE_DIRECTORY)) {
209 return ENOTDIR;
210 }
211
212 ext4_directory_iterator_t it;
213 rc = ext4_directory_iterator_init(&it, fs, eparent->inode_ref, 0);
214 if (rc != EOK) {
215 return rc;
216 }
217
218 rc = ext4_directory_find_entry(&it, eparent->inode_ref, component);
219 if (rc != EOK) {
220 ext4_directory_iterator_fini(&it);
221 if (rc == ENOENT) {
222 *rfn = NULL;
223 return EOK;
224 }
225 return rc;
226 }
227
228 uint32_t inode = ext4_directory_entry_ll_get_inode(it.current);
229
230 rc = ext4fs_node_get_core(rfn, eparent->instance, inode);
231 if (rc != EOK) {
232 ext4_directory_iterator_fini(&it);
233 return rc;
234 }
235
236 ext4_directory_iterator_fini(&it);
237 return EOK;
238}
239
240
241int ext4fs_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
242{
243 int rc;
244
245 ext4fs_instance_t *inst;
246 rc = ext4fs_instance_get(service_id, &inst);
247 if (rc != EOK) {
248 return rc;
249 }
250
251 return ext4fs_node_get_core(rfn, inst, index);
252}
253
254
255int ext4fs_node_get_core(fs_node_t **rfn, ext4fs_instance_t *inst,
256 fs_index_t index)
257{
258 int rc;
259
260 fibril_mutex_lock(&open_nodes_lock);
261
262 /* Check if the node is not already open */
263 unsigned long key[] = {
264 [OPEN_NODES_DEV_HANDLE_KEY] = inst->service_id,
265 [OPEN_NODES_INODE_KEY] = index,
266 };
267
268 link_t *already_open = hash_table_find(&open_nodes, key);
269 ext4fs_node_t *enode = NULL;
270 if (already_open) {
271 enode = hash_table_get_instance(already_open, ext4fs_node_t, link);
272 *rfn = enode->fs_node;
273 enode->references++;
274
275 fibril_mutex_unlock(&open_nodes_lock);
276 return EOK;
277 }
278
279 enode = malloc(sizeof(ext4fs_node_t));
280 if (enode == NULL) {
281 fibril_mutex_unlock(&open_nodes_lock);
282 return ENOMEM;
283 }
284
285 fs_node_t *fs_node = malloc(sizeof(fs_node_t));
286 if (fs_node == NULL) {
287 free(enode);
288 fibril_mutex_unlock(&open_nodes_lock);
289 return ENOMEM;
290 }
291 fs_node_initialize(fs_node);
292
293 ext4_inode_ref_t *inode_ref;
294 rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
295 if (rc != EOK) {
296 free(enode);
297 free(fs_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 = fs_node;
306 link_initialize(&enode->link);
307
308 fs_node->data = enode;
309 *rfn = fs_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
354 fibril_mutex_lock(&open_nodes_lock);
355
356 ext4fs_node_t *enode = EXT4FS_NODE(fn);
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("");
376 int rc;
377
378 ext4fs_node_t *enode;
379 enode = malloc(sizeof(ext4fs_node_t));
380 if (enode == NULL) {
381 return ENOMEM;
382 }
383
384 fs_node_t *fs_node;
385 fs_node = malloc(sizeof(fs_node_t));
386 if (fs_node == NULL) {
387 free(enode);
388 return ENOMEM;
389 }
390
391 ext4fs_instance_t *inst;
392 rc = ext4fs_instance_get(service_id, &inst);
393 if (rc != EOK) {
394 free(enode);
395 free(fs_node);
396 return rc;
397 }
398
399 ext4_inode_ref_t *inode_ref;
400 rc = ext4_filesystem_alloc_inode(inst->filesystem, &inode_ref, flags);
401 if (rc != EOK) {
402 free(enode);
403 free(fs_node);
404 return rc;
405 }
406
407 EXT4FS_DBG("allocated");
408
409 enode->inode_ref = inode_ref;
410 enode->instance = inst;
411 enode->references = 1;
412
413 link_initialize(&enode->link);
414
415 unsigned long key[] = {
416 [OPEN_NODES_DEV_HANDLE_KEY] = inst->service_id,
417 [OPEN_NODES_INODE_KEY] = inode_ref->index,
418 };
419
420 fibril_mutex_lock(&open_nodes_lock);
421 hash_table_insert(&open_nodes, key, &enode->link);
422 fibril_mutex_unlock(&open_nodes_lock);
423 inst->open_nodes_count++;
424
425 enode->inode_ref->dirty = true;
426
427 fs_node_initialize(fs_node);
428 fs_node->data = enode;
429 enode->fs_node = fs_node;
430 *rfn = fs_node;
431
432 EXT4FS_DBG("finished");
433
434 // TODO
435 return EOK;
436}
437
438
439int ext4fs_destroy_node(fs_node_t *fn)
440{
441 EXT4FS_DBG("");
442 int rc;
443
444 bool has_children;
445 rc = ext4fs_has_children(&has_children, fn);
446 if (rc != EOK) {
447 ext4fs_node_put(fn);
448 return rc;
449 }
450
451 if (has_children) {
452 EXT4FS_DBG("destroying non-empty node");
453 ext4fs_node_put(fn);
454 return EINVAL;
455 }
456
457 ext4fs_node_t *enode = EXT4FS_NODE(fn);
458 ext4_filesystem_t *fs = enode->instance->filesystem;
459 ext4_inode_ref_t *inode_ref = enode->inode_ref;
460
461 rc = ext4_filesystem_truncate_inode(fs, inode_ref, 0);
462 if (rc != EOK) {
463 ext4fs_node_put(fn);
464 return rc;
465 }
466
467 uint32_t rev_level = ext4_superblock_get_rev_level(fs->superblock);
468 if (rev_level > 0) {
469 ext4_filesystem_delete_orphan(fs, inode_ref);
470 }
471
472 // TODO set real deletion time
473// time_t now = time(NULL);
474 time_t now = ext4_inode_get_change_inode_time(inode_ref->inode);
475 ext4_inode_set_deletion_time(inode_ref->inode, (uint32_t)now);
476 inode_ref->dirty = true;
477
478 rc = ext4_filesystem_free_inode(fs, inode_ref);
479 if (rc != EOK) {
480 ext4fs_node_put(fn);
481 return rc;
482 }
483
484 ext4fs_node_put(fn);
485 return EOK;
486}
487
488
489int ext4fs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
490{
491 EXT4FS_DBG("");
492
493 int rc;
494
495 // Check maximum name length
496 if (strlen(name) > EXT4_DIRECTORY_FILENAME_LEN) {
497 return ENAMETOOLONG;
498 }
499
500 EXT4FS_DBG("name checked");
501
502 ext4fs_node_t *parent = EXT4FS_NODE(pfn);
503 ext4fs_node_t *child = EXT4FS_NODE(cfn);
504 ext4_filesystem_t *fs = parent->instance->filesystem;
505
506 // Add entry to parent directory
507 rc = ext4_directory_add_entry(fs, parent->inode_ref, name, child->inode_ref);
508 if (rc != EOK) {
509 return rc;
510 }
511
512 EXT4FS_DBG("dentry added");
513
514 // Fill new dir -> add '.' and '..' entries
515 if (ext4_inode_is_type(fs->superblock, child->inode_ref->inode, EXT4_INODE_MODE_DIRECTORY)) {
516
517 rc = ext4_directory_add_entry(fs, child->inode_ref, ".", child->inode_ref);
518 if (rc != EOK) {
519 ext4_directory_remove_entry(fs, parent->inode_ref, name);
520 return rc;
521 }
522
523 EXT4FS_DBG("added dot");
524
525 rc = ext4_directory_add_entry(fs, child->inode_ref, "..", parent->inode_ref);
526 if (rc != EOK) {
527 ext4_directory_remove_entry(fs, parent->inode_ref, name);
528 ext4_directory_remove_entry(fs, child->inode_ref, ".");
529 return rc;
530 }
531
532 EXT4FS_DBG("added dotdot");
533
534 uint16_t parent_links = ext4_inode_get_links_count(parent->inode_ref->inode);
535 parent_links++;
536 ext4_inode_set_links_count(parent->inode_ref->inode, parent_links);
537
538 parent->inode_ref->dirty = true;
539
540 }
541
542 uint16_t child_links = ext4_inode_get_links_count(child->inode_ref->inode);
543 child_links++;
544 ext4_inode_set_links_count(child->inode_ref->inode, child_links);
545
546 child->inode_ref->dirty = true;
547
548 return EOK;
549}
550
551
552int ext4fs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
553{
554// EXT4FS_DBG("unlinking \%s", name);
555
556 int rc;
557
558 bool has_children;
559 rc = ext4fs_has_children(&has_children, cfn);
560 if (rc != EOK) {
561 EXT4FS_DBG("\%s error: \%u", name, rc);
562 return rc;
563 }
564
565 // Cannot unlink non-empty node
566 if (has_children) {
567 return ENOTEMPTY;
568 }
569
570 // Remove entry from parent directory
571 ext4_inode_ref_t *parent = EXT4FS_NODE(pfn)->inode_ref;
572 ext4_filesystem_t *fs = EXT4FS_NODE(pfn)->instance->filesystem;
573 rc = ext4_directory_remove_entry(fs, parent, name);
574 if (rc != EOK) {
575 EXT4FS_DBG("\%s removing entry failed: \%u", name, rc);
576 return rc;
577 }
578
579 // Decrement links count
580 ext4_inode_ref_t * child_inode_ref = EXT4FS_NODE(cfn)->inode_ref;
581
582 uint32_t lnk_count = ext4_inode_get_links_count(child_inode_ref->inode);
583 lnk_count--;
584
585 // If directory - handle links from parent
586 if (lnk_count <= 1 && ext4fs_is_directory(cfn)) {
587
588 assert(lnk_count == 1);
589 lnk_count--;
590
591 ext4_inode_ref_t *parent_inode_ref = EXT4FS_NODE(pfn)->inode_ref;
592
593 uint32_t parent_lnk_count = ext4_inode_get_links_count(
594 parent_inode_ref->inode);
595
596 parent_lnk_count--;
597 ext4_inode_set_links_count(parent_inode_ref->inode, parent_lnk_count);
598
599 parent->dirty = true;
600 }
601
602 uint32_t rev_level = ext4_superblock_get_rev_level(fs->superblock);
603 if ((rev_level > 0) && (lnk_count == 0)) {
604 ext4_filesystem_add_orphan(fs, child_inode_ref);
605 }
606
607 // TODO set timestamps for parent (when we have wall-clock time)
608// time_t now = time(NULL);
609// ext4_inode_set_change_inode_time(parent->inode, (uint32_t)now);
610// ext4_inode_set_modification_time(parent->inode, (uint32_t)now);
611// parent->dirty = true;
612
613 // TODO set timestamp for inode
614// ext4_inode_set_change_inode_time(child_inode_ref->inode, (uint32_t)now);
615 ext4_inode_set_links_count(child_inode_ref->inode, lnk_count);
616 child_inode_ref->dirty = true;
617
618 return EOK;
619}
620
621
622int ext4fs_has_children(bool *has_children, fs_node_t *fn)
623{
624 int rc;
625
626 ext4fs_node_t *enode = EXT4FS_NODE(fn);
627 ext4_filesystem_t *fs = enode->instance->filesystem;
628
629 if (!ext4_inode_is_type(fs->superblock, enode->inode_ref->inode,
630 EXT4_INODE_MODE_DIRECTORY)) {
631 *has_children = false;
632 return EOK;
633 }
634
635 ext4_directory_iterator_t it;
636 rc = ext4_directory_iterator_init(&it, fs, enode->inode_ref, 0);
637 if (rc != EOK) {
638 return rc;
639 }
640
641 /* Find a non-empty directory entry */
642 bool found = false;
643 while (it.current != NULL) {
644 if (it.current->inode != 0) {
645 uint16_t name_size = ext4_directory_entry_ll_get_name_length(fs->superblock,
646 it.current);
647 if (!ext4fs_is_dots(it.current->name, name_size)) {
648 found = true;
649 break;
650 }
651 }
652
653 rc = ext4_directory_iterator_next(&it);
654 if (rc != EOK) {
655 ext4_directory_iterator_fini(&it);
656 return rc;
657 }
658 }
659
660 rc = ext4_directory_iterator_fini(&it);
661 if (rc != EOK) {
662 return rc;
663 }
664
665 *has_children = found;
666
667 return EOK;
668}
669
670
671fs_index_t ext4fs_index_get(fs_node_t *fn)
672{
673 ext4fs_node_t *enode = EXT4FS_NODE(fn);
674 return enode->inode_ref->index;
675}
676
677
678aoff64_t ext4fs_size_get(fs_node_t *fn)
679{
680 ext4fs_node_t *enode = EXT4FS_NODE(fn);
681 ext4_superblock_t *sb = enode->instance->filesystem->superblock;
682 return ext4_inode_get_size(sb, enode->inode_ref->inode);
683}
684
685
686unsigned ext4fs_lnkcnt_get(fs_node_t *fn)
687{
688 ext4fs_node_t *enode = EXT4FS_NODE(fn);
689 uint32_t lnkcnt = ext4_inode_get_links_count(enode->inode_ref->inode);
690
691 if (ext4fs_is_directory(fn)) {
692 if (lnkcnt > 1) {
693 return 1;
694 } else {
695 return 0;
696 }
697 }
698
699 // For regular files return real links count
700 return lnkcnt;
701}
702
703
704bool ext4fs_is_directory(fs_node_t *fn)
705{
706 ext4fs_node_t *enode = EXT4FS_NODE(fn);
707 ext4_superblock_t *sb = enode->instance->filesystem->superblock;
708 return ext4_inode_is_type(
709 sb, enode->inode_ref->inode, EXT4_INODE_MODE_DIRECTORY);
710}
711
712
713bool ext4fs_is_file(fs_node_t *fn)
714{
715 ext4fs_node_t *enode = EXT4FS_NODE(fn);
716 ext4_superblock_t *sb = enode->instance->filesystem->superblock;
717 return ext4_inode_is_type(
718 sb, enode->inode_ref->inode, EXT4_INODE_MODE_FILE);
719}
720
721
722service_id_t ext4fs_service_get(fs_node_t *fn)
723{
724 ext4fs_node_t *enode = EXT4FS_NODE(fn);
725 return enode->instance->service_id;
726}
727
728/*
729 * libfs operations.
730 */
731libfs_ops_t ext4fs_libfs_ops = {
732 .root_get = ext4fs_root_get,
733 .match = ext4fs_match,
734 .node_get = ext4fs_node_get,
735 .node_open = ext4fs_node_open,
736 .node_put = ext4fs_node_put,
737 .create = ext4fs_create_node,
738 .destroy = ext4fs_destroy_node,
739 .link = ext4fs_link,
740 .unlink = ext4fs_unlink,
741 .has_children = ext4fs_has_children,
742 .index_get = ext4fs_index_get,
743 .size_get = ext4fs_size_get,
744 .lnkcnt_get = ext4fs_lnkcnt_get,
745 .is_directory = ext4fs_is_directory,
746 .is_file = ext4fs_is_file,
747 .service_get = ext4fs_service_get
748};
749
750
751/*
752 * VFS operations.
753 */
754
755static int ext4fs_mounted(service_id_t service_id, const char *opts,
756 fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
757{
758 int rc;
759
760 /* Allocate libext4 filesystem structure */
761 ext4_filesystem_t *fs;
762 fs = (ext4_filesystem_t *) malloc(sizeof(ext4_filesystem_t));
763 if (fs == NULL) {
764 return ENOMEM;
765 }
766
767 /* Allocate instance structure */
768 ext4fs_instance_t *inst;
769 inst = (ext4fs_instance_t *) malloc(sizeof(ext4fs_instance_t));
770 if (inst == NULL) {
771 free(fs);
772 return ENOMEM;
773 }
774
775 /* Initialize the filesystem */
776 rc = ext4_filesystem_init(fs, service_id);
777 if (rc != EOK) {
778 free(fs);
779 free(inst);
780 return rc;
781 }
782
783 /* Do some sanity checking */
784 rc = ext4_filesystem_check_sanity(fs);
785 if (rc != EOK) {
786 ext4_filesystem_fini(fs, false);
787 free(fs);
788 free(inst);
789 return rc;
790 }
791
792 /* Check flags */
793 bool read_only;
794 rc = ext4_filesystem_check_features(fs, &read_only);
795 if (rc != EOK) {
796 ext4_filesystem_fini(fs, false);
797 free(fs);
798 free(inst);
799 return rc;
800 }
801
802 /* Initialize instance */
803 link_initialize(&inst->link);
804 inst->service_id = service_id;
805 inst->filesystem = fs;
806 inst->open_nodes_count = 0;
807
808 /* Read root node */
809 fs_node_t *root_node;
810 rc = ext4fs_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
811 if (rc != EOK) {
812 ext4_filesystem_fini(fs, false);
813 free(fs);
814 free(inst);
815 return rc;
816 }
817
818 /* Add instance to the list */
819 fibril_mutex_lock(&instance_list_mutex);
820 list_append(&inst->link, &instance_list);
821 fibril_mutex_unlock(&instance_list_mutex);
822
823 *index = EXT4_INODE_ROOT_INDEX;
824 *size = 0;
825 *lnkcnt = 1;
826
827 ext4fs_node_put(root_node);
828
829 return EOK;
830}
831
832
833static int ext4fs_unmounted(service_id_t service_id)
834{
835 int rc;
836
837 ext4fs_instance_t *inst;
838 rc = ext4fs_instance_get(service_id, &inst);
839 if (rc != EOK) {
840 return rc;
841 }
842
843 fibril_mutex_lock(&open_nodes_lock);
844
845 if (inst->open_nodes_count != 0) {
846 fibril_mutex_unlock(&open_nodes_lock);
847 return EBUSY;
848 }
849
850 /* Remove the instance from the list */
851 fibril_mutex_lock(&instance_list_mutex);
852 list_remove(&inst->link);
853 fibril_mutex_unlock(&instance_list_mutex);
854
855 fibril_mutex_unlock(&open_nodes_lock);
856
857 return ext4_filesystem_fini(inst->filesystem, true);
858}
859
860
861static int ext4fs_read(service_id_t service_id, fs_index_t index,
862 aoff64_t pos, size_t *rbytes)
863{
864 int rc;
865
866 /*
867 * Receive the read request.
868 */
869 ipc_callid_t callid;
870 size_t size;
871 if (!async_data_read_receive(&callid, &size)) {
872 async_answer_0(callid, EINVAL);
873 return EINVAL;
874 }
875
876 ext4fs_instance_t *inst;
877 rc = ext4fs_instance_get(service_id, &inst);
878 if (rc != EOK) {
879 async_answer_0(callid, rc);
880 return rc;
881 }
882
883 ext4_inode_ref_t *inode_ref;
884 rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
885 if (rc != EOK) {
886 async_answer_0(callid, rc);
887 return rc;
888 }
889
890 if (ext4_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
891 EXT4_INODE_MODE_FILE)) {
892 rc = ext4fs_read_file(callid, pos, size, inst, inode_ref,
893 rbytes);
894 } else if (ext4_inode_is_type(inst->filesystem->superblock,
895 inode_ref->inode, EXT4_INODE_MODE_DIRECTORY)) {
896 rc = ext4fs_read_directory(callid, pos, size, inst, inode_ref,
897 rbytes);
898 } else {
899 /* Other inode types not supported */
900 async_answer_0(callid, ENOTSUP);
901 rc = ENOTSUP;
902 }
903
904 ext4_filesystem_put_inode_ref(inode_ref);
905
906 return rc;
907}
908
909bool ext4fs_is_dots(const uint8_t *name, size_t name_size)
910{
911 if (name_size == 1 && name[0] == '.') {
912 return true;
913 }
914
915 if (name_size == 2 && name[0] == '.' && name[1] == '.') {
916 return true;
917 }
918
919 return false;
920}
921
922int ext4fs_read_directory(ipc_callid_t callid, aoff64_t pos, size_t size,
923 ext4fs_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
924{
925 int rc;
926
927 ext4_directory_iterator_t it;
928 rc = ext4_directory_iterator_init(&it, inst->filesystem, inode_ref, pos);
929 if (rc != EOK) {
930 async_answer_0(callid, rc);
931 return rc;
932 }
933
934 /* Find next interesting directory entry.
935 * We want to skip . and .. entries
936 * as these are not used in HelenOS
937 */
938 bool found = false;
939 while (it.current != NULL) {
940
941 if (it.current->inode == 0) {
942 goto skip;
943 }
944
945 uint16_t name_size = ext4_directory_entry_ll_get_name_length(
946 inst->filesystem->superblock, it.current);
947
948 /* skip . and .. */
949 if (ext4fs_is_dots(it.current->name, name_size)) {
950 goto skip;
951 }
952
953 /* The on-disk entry does not contain \0 at the end
954 * end of entry name, so we copy it to new buffer
955 * and add the \0 at the end
956 */
957 uint8_t *buf = malloc(name_size+1);
958 if (buf == NULL) {
959 ext4_directory_iterator_fini(&it);
960 async_answer_0(callid, ENOMEM);
961 return ENOMEM;
962 }
963 memcpy(buf, &it.current->name, name_size);
964 *(buf + name_size) = 0;
965 found = true;
966 (void) async_data_read_finalize(callid, buf, name_size + 1);
967 free(buf);
968 break;
969
970skip:
971 rc = ext4_directory_iterator_next(&it);
972 if (rc != EOK) {
973 ext4_directory_iterator_fini(&it);
974 async_answer_0(callid, rc);
975 return rc;
976 }
977 }
978
979 uint64_t next;
980 if (found) {
981 rc = ext4_directory_iterator_next(&it);
982 if (rc != EOK) {
983 return rc;
984 }
985 next = it.current_offset;
986 }
987
988 rc = ext4_directory_iterator_fini(&it);
989 if (rc != EOK) {
990 return rc;
991 }
992
993 if (found) {
994 *rbytes = next - pos;
995 return EOK;
996 } else {
997 async_answer_0(callid, ENOENT);
998 return ENOENT;
999 }
1000}
1001
1002int ext4fs_read_file(ipc_callid_t callid, aoff64_t pos, size_t size,
1003 ext4fs_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
1004{
1005 int rc;
1006
1007 ext4_superblock_t *sb = inst->filesystem->superblock;
1008 uint64_t file_size = ext4_inode_get_size(sb, inode_ref->inode);
1009
1010 if (pos >= file_size) {
1011 /* Read 0 bytes successfully */
1012 async_data_read_finalize(callid, NULL, 0);
1013 *rbytes = 0;
1014 return EOK;
1015 }
1016
1017 /* For now, we only read data from one block at a time */
1018 uint32_t block_size = ext4_superblock_get_block_size(sb);
1019 aoff64_t file_block = pos / block_size;
1020 uint32_t offset_in_block = pos % block_size;
1021 uint32_t bytes = min(block_size - offset_in_block, size);
1022
1023 /* Handle end of file */
1024 if (pos + bytes > file_size) {
1025 bytes = file_size - pos;
1026 }
1027
1028 /* Get the real block number */
1029 uint32_t fs_block;
1030 rc = ext4_filesystem_get_inode_data_block_index(inst->filesystem,
1031 inode_ref->inode, file_block, &fs_block);
1032 if (rc != EOK) {
1033 async_answer_0(callid, rc);
1034 return rc;
1035 }
1036
1037 /* Check for sparse file
1038 * If ext4_filesystem_get_inode_data_block_index returned
1039 * fs_block == 0, it means that the given block is not allocated for the
1040 * file and we need to return a buffer of zeros
1041 */
1042 uint8_t *buffer;
1043 if (fs_block == 0) {
1044 buffer = malloc(bytes);
1045 if (buffer == NULL) {
1046 async_answer_0(callid, ENOMEM);
1047 return ENOMEM;
1048 }
1049
1050 memset(buffer, 0, bytes);
1051
1052 async_data_read_finalize(callid, buffer, bytes);
1053 *rbytes = bytes;
1054
1055 free(buffer);
1056
1057 return EOK;
1058 }
1059
1060 /* Usual case - we need to read a block from device */
1061 block_t *block;
1062 rc = block_get(&block, inst->service_id, fs_block, BLOCK_FLAGS_NONE);
1063 if (rc != EOK) {
1064 async_answer_0(callid, rc);
1065 return rc;
1066 }
1067
1068 assert(offset_in_block + bytes <= block_size);
1069 async_data_read_finalize(callid, block->data + offset_in_block, bytes);
1070
1071 rc = block_put(block);
1072 if (rc != EOK) {
1073 return rc;
1074 }
1075
1076 *rbytes = bytes;
1077 return EOK;
1078}
1079
1080static int ext4fs_write(service_id_t service_id, fs_index_t index,
1081 aoff64_t pos, size_t *wbytes, aoff64_t *nsize)
1082{
1083 int rc;
1084
1085 fs_node_t *fn;
1086 rc = ext4fs_node_get(&fn, service_id, index);
1087 if (rc != EOK) {
1088 EXT4FS_DBG("node get error");
1089 return rc;
1090 }
1091
1092 ipc_callid_t callid;
1093 size_t len;
1094 if (!async_data_write_receive(&callid, &len)) {
1095 rc = EINVAL;
1096 ext4fs_node_put(fn);
1097 async_answer_0(callid, rc);
1098 EXT4FS_DBG("data write recv");
1099 return rc;
1100 }
1101
1102
1103 ext4fs_node_t *enode = EXT4FS_NODE(fn);
1104 ext4_filesystem_t *fs = enode->instance->filesystem;
1105
1106 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
1107
1108 // Prevent writing to more than one block
1109 uint32_t bytes = min(len, block_size - (pos % block_size));
1110
1111 int flags = BLOCK_FLAGS_NONE;
1112 if (bytes == block_size) {
1113 flags = BLOCK_FLAGS_NOREAD;
1114 }
1115
1116 uint32_t iblock = pos / block_size;
1117 uint32_t fblock;
1118
1119 ext4_inode_ref_t *inode_ref = enode->inode_ref;
1120 rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, iblock, &fblock);
1121 if (rc != EOK) {
1122 // TODO error
1123 ext4fs_node_put(fn);
1124 EXT4FS_DBG("error loading block addr");
1125 return rc;
1126 }
1127
1128 if (fblock == 0) {
1129 rc = ext4_balloc_alloc_block(fs, inode_ref, &fblock);
1130 if (rc != EOK) {
1131 ext4fs_node_put(fn);
1132 async_answer_0(callid, rc);
1133 return rc;
1134 }
1135
1136 rc = ext4_filesystem_set_inode_data_block_index(fs, inode_ref, iblock, fblock);
1137 if (rc != EOK) {
1138 EXT4FS_DBG("ERROR: setting index failed");
1139 }
1140 inode_ref->dirty = true;
1141
1142 flags = BLOCK_FLAGS_NOREAD;
1143 }
1144
1145 block_t *write_block;
1146 rc = block_get(&write_block, service_id, fblock, flags);
1147 if (rc != EOK) {
1148 EXT4FS_DBG("error in loading block \%d", rc);
1149 ext4fs_node_put(fn);
1150 async_answer_0(callid, rc);
1151 return rc;
1152 }
1153
1154 if (flags == BLOCK_FLAGS_NOREAD) {
1155 memset(write_block->data, 0, block_size);
1156 }
1157
1158 rc = async_data_write_finalize(callid, write_block->data + (pos % block_size), bytes);
1159 if (rc != EOK) {
1160 // TODO error
1161 EXT4FS_DBG("error in write finalize \%d", rc);
1162 }
1163
1164 write_block->dirty = true;
1165
1166 rc = block_put(write_block);
1167 if (rc != EOK) {
1168 EXT4FS_DBG("error in writing block \%d", rc);
1169 ext4fs_node_put(fn);
1170 return rc;
1171 }
1172
1173 uint32_t old_inode_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
1174 if (pos + bytes > old_inode_size) {
1175 ext4_inode_set_size(inode_ref->inode, pos + bytes);
1176 inode_ref->dirty = true;
1177 }
1178
1179 *nsize = ext4_inode_get_size(fs->superblock, inode_ref->inode);
1180 *wbytes = bytes;
1181
1182 return ext4fs_node_put(fn);
1183}
1184
1185
1186static int ext4fs_truncate(service_id_t service_id, fs_index_t index,
1187 aoff64_t new_size)
1188{
1189 int rc;
1190
1191 fs_node_t *fn;
1192 rc = ext4fs_node_get(&fn, service_id, index);
1193 if (rc != EOK) {
1194 return rc;
1195 }
1196
1197 ext4fs_node_t *enode = EXT4FS_NODE(fn);
1198 ext4_inode_ref_t *inode_ref = enode->inode_ref;
1199 ext4_filesystem_t *fs = enode->instance->filesystem;
1200
1201 rc = ext4_filesystem_truncate_inode(fs, inode_ref, new_size);
1202 ext4fs_node_put(fn);
1203
1204 return rc;
1205}
1206
1207
1208static int ext4fs_close(service_id_t service_id, fs_index_t index)
1209{
1210 return EOK;
1211}
1212
1213
1214static int ext4fs_destroy(service_id_t service_id, fs_index_t index)
1215{
1216 int rc;
1217
1218 fs_node_t *fn;
1219 rc = ext4fs_node_get(&fn, service_id, index);
1220 if (rc != EOK) {
1221 return rc;
1222 }
1223
1224 /* Destroy the inode */
1225 return ext4fs_destroy_node(fn);
1226}
1227
1228
1229static int ext4fs_sync(service_id_t service_id, fs_index_t index)
1230{
1231 int rc;
1232
1233 fs_node_t *fn;
1234 rc = ext4fs_node_get(&fn, service_id, index);
1235 if (rc != EOK) {
1236 return rc;
1237 }
1238
1239 ext4fs_node_t *enode = EXT4FS_NODE(fn);
1240 enode->inode_ref->dirty = true;
1241
1242 return ext4fs_node_put(fn);
1243}
1244
1245vfs_out_ops_t ext4fs_ops = {
1246 .mounted = ext4fs_mounted,
1247 .unmounted = ext4fs_unmounted,
1248 .read = ext4fs_read,
1249 .write = ext4fs_write,
1250 .truncate = ext4fs_truncate,
1251 .close = ext4fs_close,
1252 .destroy = ext4fs_destroy,
1253 .sync = ext4fs_sync,
1254};
1255
1256/**
1257 * @}
1258 */
Note: See TracBrowser for help on using the repository browser.