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

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

Coding style…

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