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

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

error hangling in write operation, adjusted supported features for mounting

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