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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ea75ceb 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
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
[5614c7f]212 ext4_directory_iterator_t it;
[9b9d37bb]213 rc = ext4_directory_iterator_init(&it, fs, eparent->inode_ref, 0);
214 if (rc != EOK) {
215 return rc;
216 }
217
[8be96a0]218 rc = ext4_directory_find_entry(&it, eparent->inode_ref, component);
219 if (rc != EOK) {
220 ext4_directory_iterator_fini(&it);
[ea75ceb]221 if (rc == ENOENT) {
222 *rfn = NULL;
223 return EOK;
224 }
[8be96a0]225 return rc;
[9b9d37bb]226 }
227
[8be96a0]228 uint32_t inode = ext4_directory_entry_ll_get_inode(it.current);
[9b9d37bb]229
[8be96a0]230 rc = ext4fs_node_get_core(rfn, eparent->instance, inode);
231 if (rc != EOK) {
232 ext4_directory_iterator_fini(&it);
233 return rc;
[9b9d37bb]234 }
235
[8be96a0]236 ext4_directory_iterator_fini(&it);
[d3a9ae74]237 return EOK;
238}
239
[3711e7e]240
[d3a9ae74]241int ext4fs_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
242{
[9c0c0e1]243 int rc;
244
[5614c7f]245 ext4fs_instance_t *inst;
[9c0c0e1]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);
[d3a9ae74]252}
253
[3711e7e]254
[6c501f8]255int ext4fs_node_get_core(fs_node_t **rfn, ext4fs_instance_t *inst,
256 fs_index_t index)
257{
[3711e7e]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
[5614c7f]268 link_t *already_open = hash_table_find(&open_nodes, key);
269 ext4fs_node_t *enode = NULL;
[3711e7e]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
[5614c7f]285 fs_node_t *fs_node = malloc(sizeof(fs_node_t));
286 if (fs_node == NULL) {
[3711e7e]287 free(enode);
288 fibril_mutex_unlock(&open_nodes_lock);
289 return ENOMEM;
290 }
[5614c7f]291 fs_node_initialize(fs_node);
[3711e7e]292
[5614c7f]293 ext4_inode_ref_t *inode_ref;
[3711e7e]294 rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
295 if (rc != EOK) {
296 free(enode);
[5614c7f]297 free(fs_node);
[3711e7e]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;
[5614c7f]305 enode->fs_node = fs_node;
[3711e7e]306 link_initialize(&enode->link);
307
[5614c7f]308 fs_node->data = enode;
309 *rfn = fs_node;
[3711e7e]310
311 hash_table_insert(&open_nodes, key, &enode->link);
312 inst->open_nodes_count++;
313
314 fibril_mutex_unlock(&open_nodes_lock);
315
[6c501f8]316 return EOK;
317}
318
[3711e7e]319
[9b9d37bb]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
[9c0c0e1]340 return EOK;
341}
342
[3711e7e]343
[d3a9ae74]344int ext4fs_node_open(fs_node_t *fn)
345{
[2b9e142]346 // Stateless operation
[d3a9ae74]347 return EOK;
348}
349
350int ext4fs_node_put(fs_node_t *fn)
351{
[9c0c0e1]352 int rc;
353
354 fibril_mutex_lock(&open_nodes_lock);
355
[5614c7f]356 ext4fs_node_t *enode = EXT4FS_NODE(fn);
[9c0c0e1]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
[d3a9ae74]369 return EOK;
370}
371
[3711e7e]372
[d3a9ae74]373int ext4fs_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
374{
[47a89fe]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;
[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
[cd1cc4e6]407 EXT4FS_DBG("allocated");
408
[47a89fe]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
[cd1cc4e6]432 EXT4FS_DBG("finished");
433
[47a89fe]434 // TODO
435 return EOK;
[d3a9ae74]436}
437
[3711e7e]438
[d3a9ae74]439int ext4fs_destroy_node(fs_node_t *fn)
440{
[cd1cc4e6]441 EXT4FS_DBG("");
[3d4fd2c]442 int rc;
[9b9d37bb]443
[3d4fd2c]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
[07fd4cd1]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
[3d4fd2c]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;
[d3a9ae74]486}
487
[3711e7e]488
[d3a9ae74]489int ext4fs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
490{
[cd1cc4e6]491 EXT4FS_DBG("");
492
[47a89fe]493 int rc;
[9b9d37bb]494
[47a89fe]495 // Check maximum name length
496 if (strlen(name) > EXT4_DIRECTORY_FILENAME_LEN) {
497 return ENAMETOOLONG;
498 }
499
[cd1cc4e6]500 EXT4FS_DBG("name checked");
501
[47a89fe]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
[cd1cc4e6]507 rc = ext4_directory_add_entry(fs, parent->inode_ref, name, child->inode_ref);
[47a89fe]508 if (rc != EOK) {
509 return rc;
510 }
511
[cd1cc4e6]512 EXT4FS_DBG("dentry added");
513
[47a89fe]514 // Fill new dir -> add '.' and '..' entries
515 if (ext4_inode_is_type(fs->superblock, child->inode_ref->inode, EXT4_INODE_MODE_DIRECTORY)) {
516
[cd1cc4e6]517 rc = ext4_directory_add_entry(fs, child->inode_ref, ".", child->inode_ref);
[47a89fe]518 if (rc != EOK) {
519 ext4_directory_remove_entry(fs, parent->inode_ref, name);
520 return rc;
521 }
522
[cd1cc4e6]523 EXT4FS_DBG("added dot");
524
525 rc = ext4_directory_add_entry(fs, child->inode_ref, "..", parent->inode_ref);
[47a89fe]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
[cd1cc4e6]532 EXT4FS_DBG("added dotdot");
533
[47a89fe]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;
[d3a9ae74]549}
550
[3711e7e]551
[ebeaaa06]552int ext4fs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
[d3a9ae74]553{
[d4d2954]554// EXT4FS_DBG("unlinking \%s", name);
[3d4fd2c]555
[ebeaaa06]556 int rc;
557
558 bool has_children;
559 rc = ext4fs_has_children(&has_children, cfn);
560 if (rc != EOK) {
[82d7816]561 EXT4FS_DBG("\%s error: \%u", name, rc);
[ebeaaa06]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
[f49638e]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) {
[82d7816]575 EXT4FS_DBG("\%s removing entry failed: \%u", name, rc);
[f49638e]576 return rc;
577 }
[ebeaaa06]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)) {
[1e48a07e]587
588 assert(lnk_count == 1);
589 lnk_count--;
590
[82d7816]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
[07fd4cd1]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);
[ebeaaa06]605 }
606
[07fd4cd1]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);
[82d7816]615 ext4_inode_set_links_count(child_inode_ref->inode, lnk_count);
616 child_inode_ref->dirty = true;
617
[ebeaaa06]618 return EOK;
[d3a9ae74]619}
620
[3711e7e]621
[d3a9ae74]622int ext4fs_has_children(bool *has_children, fs_node_t *fn)
623{
[e68c834]624 int rc;
625
[5614c7f]626 ext4fs_node_t *enode = EXT4FS_NODE(fn);
627 ext4_filesystem_t *fs = enode->instance->filesystem;
[e68c834]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
[5614c7f]635 ext4_directory_iterator_t it;
[e68c834]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 */
[5614c7f]642 bool found = false;
[e68c834]643 while (it.current != NULL) {
644 if (it.current->inode != 0) {
[5614c7f]645 uint16_t name_size = ext4_directory_entry_ll_get_name_length(fs->superblock,
[e68c834]646 it.current);
[2ea6392]647 if (!ext4fs_is_dots(it.current->name, name_size)) {
[e68c834]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;
[9b9d37bb]666
[d3a9ae74]667 return EOK;
668}
669
670
671fs_index_t ext4fs_index_get(fs_node_t *fn)
672{
[9b9d37bb]673 ext4fs_node_t *enode = EXT4FS_NODE(fn);
674 return enode->inode_ref->index;
[d3a9ae74]675}
676
[3711e7e]677
[d3a9ae74]678aoff64_t ext4fs_size_get(fs_node_t *fn)
679{
[9b9d37bb]680 ext4fs_node_t *enode = EXT4FS_NODE(fn);
[5614c7f]681 ext4_superblock_t *sb = enode->instance->filesystem->superblock;
682 return ext4_inode_get_size(sb, enode->inode_ref->inode);
[d3a9ae74]683}
684
[3711e7e]685
[d3a9ae74]686unsigned ext4fs_lnkcnt_get(fs_node_t *fn)
687{
[d4d2954]688 ext4fs_node_t *enode = EXT4FS_NODE(fn);
689 uint32_t lnkcnt = ext4_inode_get_links_count(enode->inode_ref->inode);
690
[e5f8762]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
[d4d2954]700 return lnkcnt;
[d3a9ae74]701}
702
[3711e7e]703
[d3a9ae74]704bool ext4fs_is_directory(fs_node_t *fn)
705{
[e68c834]706 ext4fs_node_t *enode = EXT4FS_NODE(fn);
[5614c7f]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);
[d3a9ae74]710}
711
[3711e7e]712
[d3a9ae74]713bool ext4fs_is_file(fs_node_t *fn)
714{
[9b9d37bb]715 ext4fs_node_t *enode = EXT4FS_NODE(fn);
[5614c7f]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);
[d3a9ae74]719}
720
[3711e7e]721
[d3a9ae74]722service_id_t ext4fs_service_get(fs_node_t *fn)
723{
[e68c834]724 ext4fs_node_t *enode = EXT4FS_NODE(fn);
725 return enode->instance->service_id;
[d3a9ae74]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{
[6c501f8]758 int rc;
759
760 /* Allocate libext4 filesystem structure */
[5614c7f]761 ext4_filesystem_t *fs;
[6c501f8]762 fs = (ext4_filesystem_t *) malloc(sizeof(ext4_filesystem_t));
763 if (fs == NULL) {
764 return ENOMEM;
765 }
766
767 /* Allocate instance structure */
[5614c7f]768 ext4fs_instance_t *inst;
[6c501f8]769 inst = (ext4fs_instance_t *) malloc(sizeof(ext4fs_instance_t));
770 if (inst == NULL) {
771 free(fs);
772 return ENOMEM;
773 }
774
[9c0c0e1]775 /* Initialize the filesystem */
[6c501f8]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) {
[ae3d4f8]786 ext4_filesystem_fini(fs, false);
[6c501f8]787 free(fs);
788 free(inst);
789 return rc;
790 }
791
792 /* Check flags */
[5614c7f]793 bool read_only;
[9c0c0e1]794 rc = ext4_filesystem_check_features(fs, &read_only);
[6c501f8]795 if (rc != EOK) {
[ae3d4f8]796 ext4_filesystem_fini(fs, false);
[6c501f8]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;
[3711e7e]810 rc = ext4fs_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
[6c501f8]811 if (rc != EOK) {
[ae3d4f8]812 ext4_filesystem_fini(fs, false);
[6c501f8]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;
[e5f8762]825 *lnkcnt = 1;
[6c501f8]826
827 ext4fs_node_put(root_node);
828
[d3a9ae74]829 return EOK;
830}
831
[3711e7e]832
[d3a9ae74]833static int ext4fs_unmounted(service_id_t service_id)
834{
[6c501f8]835 int rc;
836
[5614c7f]837 ext4fs_instance_t *inst;
[6c501f8]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
[ae3d4f8]857 return ext4_filesystem_fini(inst->filesystem, true);
[d3a9ae74]858}
859
[3711e7e]860
[5f6cb14]861static int ext4fs_read(service_id_t service_id, fs_index_t index,
862 aoff64_t pos, size_t *rbytes)
[d3a9ae74]863{
[9b9d37bb]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
[5614c7f]876 ext4fs_instance_t *inst;
[9b9d37bb]877 rc = ext4fs_instance_get(service_id, &inst);
878 if (rc != EOK) {
879 async_answer_0(callid, rc);
880 return rc;
881 }
882
[5614c7f]883 ext4_inode_ref_t *inode_ref;
[9b9d37bb]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);
[8958a26]905
[9b9d37bb]906 return rc;
907}
908
[8958a26]909bool ext4fs_is_dots(const uint8_t *name, size_t name_size)
910{
[9b9d37bb]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
[5614c7f]927 ext4_directory_iterator_t it;
[9b9d37bb]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 */
[5614c7f]938 bool found = false;
[9b9d37bb]939 while (it.current != NULL) {
[e68c834]940
[9b9d37bb]941 if (it.current->inode == 0) {
942 goto skip;
943 }
944
[5614c7f]945 uint16_t name_size = ext4_directory_entry_ll_get_name_length(
[9b9d37bb]946 inst->filesystem->superblock, it.current);
947
948 /* skip . and .. */
[2ea6392]949 if (ext4fs_is_dots(it.current->name, name_size)) {
[9b9d37bb]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 */
[5614c7f]957 uint8_t *buf = malloc(name_size+1);
[9b9d37bb]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
[5614c7f]979 uint64_t next;
[9b9d37bb]980 if (found) {
981 rc = ext4_directory_iterator_next(&it);
[8958a26]982 if (rc != EOK) {
[9b9d37bb]983 return rc;
[8958a26]984 }
[9b9d37bb]985 next = it.current_offset;
986 }
987
988 rc = ext4_directory_iterator_fini(&it);
[8958a26]989 if (rc != EOK) {
[9b9d37bb]990 return rc;
[8958a26]991 }
[9b9d37bb]992
993 if (found) {
994 *rbytes = next - pos;
995 return EOK;
996 } else {
997 async_answer_0(callid, ENOENT);
998 return ENOENT;
999 }
[d3a9ae74]1000}
1001
[9b9d37bb]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
[5614c7f]1007 ext4_superblock_t *sb = inst->filesystem->superblock;
1008 uint64_t file_size = ext4_inode_get_size(sb, inode_ref->inode);
[9b9d37bb]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 */
[5614c7f]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);
[9b9d37bb]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 */
[5614c7f]1029 uint32_t fs_block;
[9b9d37bb]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
[2b9e142]1038 * If ext4_filesystem_get_inode_data_block_index returned
[9b9d37bb]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 */
[5614c7f]1042 uint8_t *buffer;
[9b9d37bb]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 */
[5614c7f]1061 block_t *block;
[9b9d37bb]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);
[8958a26]1072 if (rc != EOK) {
[9b9d37bb]1073 return rc;
[8958a26]1074 }
[9b9d37bb]1075
1076 *rbytes = bytes;
1077 return EOK;
1078}
[3711e7e]1079
[5f6cb14]1080static int ext4fs_write(service_id_t service_id, fs_index_t index,
1081 aoff64_t pos, size_t *wbytes, aoff64_t *nsize)
[d3a9ae74]1082{
[1c1c736]1083 int rc;
1084
[5614c7f]1085 fs_node_t *fn;
[1c1c736]1086 rc = ext4fs_node_get(&fn, service_id, index);
1087 if (rc != EOK) {
[6088193]1088 EXT4FS_DBG("node get error");
[1c1c736]1089 return rc;
1090 }
1091
[5614c7f]1092 ipc_callid_t callid;
1093 size_t len;
[1c1c736]1094 if (!async_data_write_receive(&callid, &len)) {
1095 rc = EINVAL;
1096 ext4fs_node_put(fn);
1097 async_answer_0(callid, rc);
[6088193]1098 EXT4FS_DBG("data write recv");
[1c1c736]1099 return rc;
1100 }
1101
1102
[5614c7f]1103 ext4fs_node_t *enode = EXT4FS_NODE(fn);
1104 ext4_filesystem_t *fs = enode->instance->filesystem;
[1c1c736]1105
[5614c7f]1106 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
[1c1c736]1107
1108 // Prevent writing to more than one block
[5614c7f]1109 uint32_t bytes = min(len, block_size - (pos % block_size));
[1c1c736]1110
[5614c7f]1111 int flags = BLOCK_FLAGS_NONE;
[35f48f2]1112 if (bytes == block_size) {
1113 flags = BLOCK_FLAGS_NOREAD;
1114 }
1115
[5614c7f]1116 uint32_t iblock = pos / block_size;
1117 uint32_t fblock;
[1c1c736]1118
[5614c7f]1119 ext4_inode_ref_t *inode_ref = enode->inode_ref;
[1c1c736]1120 rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, iblock, &fblock);
[6088193]1121 if (rc != EOK) {
1122 // TODO error
1123 ext4fs_node_put(fn);
1124 EXT4FS_DBG("error loading block addr");
1125 return rc;
1126 }
[1c1c736]1127
1128 if (fblock == 0) {
[b12ca16]1129 rc = ext4_balloc_alloc_block(fs, inode_ref, &fblock);
[35f48f2]1130 if (rc != EOK) {
1131 ext4fs_node_put(fn);
1132 async_answer_0(callid, rc);
1133 return rc;
1134 }
1135
[b12ca16]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 }
[35f48f2]1140 inode_ref->dirty = true;
1141
1142 flags = BLOCK_FLAGS_NOREAD;
[1c1c736]1143 }
1144
[5614c7f]1145 block_t *write_block;
[35f48f2]1146 rc = block_get(&write_block, service_id, fblock, flags);
[1c1c736]1147 if (rc != EOK) {
[6088193]1148 EXT4FS_DBG("error in loading block \%d", rc);
[1c1c736]1149 ext4fs_node_put(fn);
1150 async_answer_0(callid, rc);
1151 return rc;
1152 }
1153
[35f48f2]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) {
[1e65444]1160 // TODO error
[35f48f2]1161 EXT4FS_DBG("error in write finalize \%d", rc);
1162 }
1163
[1c1c736]1164 write_block->dirty = true;
1165
1166 rc = block_put(write_block);
1167 if (rc != EOK) {
[6088193]1168 EXT4FS_DBG("error in writing block \%d", rc);
[1c1c736]1169 ext4fs_node_put(fn);
1170 return rc;
1171 }
1172
[5614c7f]1173 uint32_t old_inode_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
[35f48f2]1174 if (pos + bytes > old_inode_size) {
1175 ext4_inode_set_size(inode_ref->inode, pos + bytes);
1176 inode_ref->dirty = true;
[1c1c736]1177 }
[35f48f2]1178
1179 *nsize = ext4_inode_get_size(fs->superblock, inode_ref->inode);
[1c1c736]1180 *wbytes = bytes;
[35f48f2]1181
[1c1c736]1182 return ext4fs_node_put(fn);
[d3a9ae74]1183}
1184
[3711e7e]1185
[5f6cb14]1186static int ext4fs_truncate(service_id_t service_id, fs_index_t index,
1187 aoff64_t new_size)
[d3a9ae74]1188{
[d5a78e28]1189 int rc;
[e68c834]1190
[5614c7f]1191 fs_node_t *fn;
[d5a78e28]1192 rc = ext4fs_node_get(&fn, service_id, index);
1193 if (rc != EOK) {
1194 return rc;
1195 }
1196
[3d4fd2c]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;
[d5a78e28]1200
[3d4fd2c]1201 rc = ext4_filesystem_truncate_inode(fs, inode_ref, new_size);
[d5a78e28]1202 ext4fs_node_put(fn);
[1c1c736]1203
[3d4fd2c]1204 return rc;
[d3a9ae74]1205}
1206
[3711e7e]1207
[d3a9ae74]1208static int ext4fs_close(service_id_t service_id, fs_index_t index)
1209{
1210 return EOK;
1211}
1212
[3711e7e]1213
[d3a9ae74]1214static int ext4fs_destroy(service_id_t service_id, fs_index_t index)
1215{
[8be96a0]1216 int rc;
[e68c834]1217
[5614c7f]1218 fs_node_t *fn;
[8be96a0]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);
[d3a9ae74]1226}
1227
[3711e7e]1228
[d3a9ae74]1229static int ext4fs_sync(service_id_t service_id, fs_index_t index)
1230{
[35f48f2]1231 int rc;
1232
[5614c7f]1233 fs_node_t *fn;
[35f48f2]1234 rc = ext4fs_node_get(&fn, service_id, index);
1235 if (rc != EOK) {
1236 return rc;
1237 }
1238
[5614c7f]1239 ext4fs_node_t *enode = EXT4FS_NODE(fn);
[35f48f2]1240 enode->inode_ref->dirty = true;
1241
1242 return ext4fs_node_put(fn);
[d3a9ae74]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 * @}
[2b9e142]1258 */
Note: See TracBrowser for help on using the repository browser.