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

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

directory entry delete operation (without HTree index modification)

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