source: mainline/uspace/lib/ext4/src/ops.c@ de5b708

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since de5b708 was de5b708, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Restructure ext4 filesystem opening/closing.

  • Property mode set to 100644
File size: 34.9 KB
RevLine 
[d3a9ae74]1/*
[d1538a1]2 * Copyright (c) 2011 Martin Sucha
[f22d5ef0]3 * Copyright (c) 2012 Frantisek Princ
[d3a9ae74]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[81dd2ed]30/** @addtogroup libext4
[d3a9ae74]31 * @{
[38542dc]32 */
[d3a9ae74]33/**
[4bfad34]34 * @file ops.c
[81dd2ed]35 * @brief Operations for ext4 filesystem.
[d3a9ae74]36 */
37
[be39fc6]38#include <adt/hash_table.h>
39#include <adt/hash.h>
[d3a9ae74]40#include <errno.h>
[6c501f8]41#include <fibril_synch.h>
[d3a9ae74]42#include <libfs.h>
[9b9d37bb]43#include <macros.h>
[6c501f8]44#include <malloc.h>
[be39fc6]45#include <mem.h>
46#include <str.h>
[d3a9ae74]47#include <ipc/loc.h>
[be39fc6]48#include "ext4/balloc.h"
49#include "ext4/directory.h"
50#include "ext4/directory_index.h"
51#include "ext4/extent.h"
52#include "ext4/inode.h"
[81dd2ed]53#include "ext4/ops.h"
[be39fc6]54#include "ext4/filesystem.h"
[4bfad34]55#include "ext4/fstypes.h"
[be39fc6]56#include "ext4/superblock.h"
[6c501f8]57
[06d85e5]58/* Forward declarations of auxiliary functions */
[9b9d37bb]59
[be39fc6]60static int ext4_read_directory(ipc_callid_t, aoff64_t, size_t,
61 ext4_instance_t *, ext4_inode_ref_t *, size_t *);
62static int ext4_read_file(ipc_callid_t, aoff64_t, size_t, ext4_instance_t *,
[9b9d37bb]63 ext4_inode_ref_t *, size_t *);
[be39fc6]64static bool ext4_is_dots(const uint8_t *, size_t);
65static int ext4_instance_get(service_id_t, ext4_instance_t **);
[d3a9ae74]66
[38542dc]67/* Forward declarations of ext4 libfs operations. */
[1fff583]68
[be39fc6]69static int ext4_root_get(fs_node_t **, service_id_t);
70static int ext4_match(fs_node_t **, fs_node_t *, const char *);
71static int ext4_node_get(fs_node_t **, service_id_t, fs_index_t);
72static int ext4_node_open(fs_node_t *);
73 int ext4_node_put(fs_node_t *);
74static int ext4_create_node(fs_node_t **, service_id_t, int);
75static int ext4_destroy_node(fs_node_t *);
76static int ext4_link(fs_node_t *, fs_node_t *, const char *);
77static int ext4_unlink(fs_node_t *, fs_node_t *, const char *);
78static int ext4_has_children(bool *, fs_node_t *);
79static fs_index_t ext4_index_get(fs_node_t *);
80static aoff64_t ext4_size_get(fs_node_t *);
81static unsigned ext4_lnkcnt_get(fs_node_t *);
82static bool ext4_is_directory(fs_node_t *);
83static bool ext4_is_file(fs_node_t *node);
84static service_id_t ext4_service_get(fs_node_t *node);
85static int ext4_size_block(service_id_t, uint32_t *);
86static int ext4_total_block_count(service_id_t, uint64_t *);
87static int ext4_free_block_count(service_id_t, uint64_t *);
[d3a9ae74]88
[06d85e5]89/* Static variables */
[1fff583]90
[6c501f8]91static LIST_INITIALIZE(instance_list);
92static FIBRIL_MUTEX_INITIALIZE(instance_list_mutex);
[3711e7e]93static hash_table_t open_nodes;
[6c501f8]94static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
95
[3711e7e]96/* Hash table interface for open nodes hash table */
[062d900]97
98typedef struct {
99 service_id_t service_id;
100 fs_index_t index;
101} node_key_t;
102
103static size_t open_nodes_key_hash(void *key_arg)
[3711e7e]104{
[062d900]105 node_key_t *key = (node_key_t *)key_arg;
106 return hash_combine(key->service_id, key->index);
[3711e7e]107}
108
[062d900]109static size_t open_nodes_hash(const ht_link_t *item)
[3711e7e]110{
[be39fc6]111 ext4_node_t *enode = hash_table_get_inst(item, ext4_node_t, link);
[062d900]112 return hash_combine(enode->instance->service_id, enode->inode_ref->index);
[3711e7e]113}
114
[062d900]115static bool open_nodes_key_equal(void *key_arg, const ht_link_t *item)
[3711e7e]116{
[062d900]117 node_key_t *key = (node_key_t *)key_arg;
[be39fc6]118 ext4_node_t *enode = hash_table_get_inst(item, ext4_node_t, link);
[062d900]119
120 return key->service_id == enode->instance->service_id
121 && key->index == enode->inode_ref->index;
[3711e7e]122}
123
[062d900]124static hash_table_ops_t open_nodes_ops = {
[3711e7e]125 .hash = open_nodes_hash,
[062d900]126 .key_hash = open_nodes_key_hash,
127 .key_equal = open_nodes_key_equal,
128 .equal = NULL,
129 .remove_callback = NULL,
[3711e7e]130};
131
[1fff583]132/** Basic initialization of the driver.
133 *
[38542dc]134 * This is only needed to create the hash table
135 * for storing open nodes.
136 *
137 * @return Error code
[1fff583]138 *
139 */
[be39fc6]140int ext4_global_init(void)
[d3a9ae74]141{
[062d900]142 if (!hash_table_create(&open_nodes, 0, 0, &open_nodes_ops))
[3711e7e]143 return ENOMEM;
[38542dc]144
[d3a9ae74]145 return EOK;
146}
147
[1fff583]148/* Finalization of the driver.
149 *
[38542dc]150 * This is only needed to destroy the hash table.
[1fff583]151 *
[38542dc]152 * @return Error code
[1fff583]153 */
[be39fc6]154int ext4_global_fini(void)
[d3a9ae74]155{
[3711e7e]156 hash_table_destroy(&open_nodes);
[d3a9ae74]157 return EOK;
158}
159
160/*
[38542dc]161 * Ext4 libfs operations.
[d3a9ae74]162 */
163
[1fff583]164/** Get instance from internal table by service_id.
165 *
[38542dc]166 * @param service_id Device identifier
167 * @param inst Output instance if successful operation
168 *
169 * @return Error code
170 *
[1fff583]171 */
[be39fc6]172int ext4_instance_get(service_id_t service_id, ext4_instance_t **inst)
[6c501f8]173{
174 fibril_mutex_lock(&instance_list_mutex);
[38542dc]175
[6c501f8]176 if (list_empty(&instance_list)) {
177 fibril_mutex_unlock(&instance_list_mutex);
178 return EINVAL;
179 }
[38542dc]180
[be39fc6]181 list_foreach(instance_list, link, ext4_instance_t, tmp) {
[6c501f8]182 if (tmp->service_id == service_id) {
183 *inst = tmp;
184 fibril_mutex_unlock(&instance_list_mutex);
185 return EOK;
186 }
187 }
[38542dc]188
[6c501f8]189 fibril_mutex_unlock(&instance_list_mutex);
190 return EINVAL;
191}
192
[1fff583]193/** Get root node of filesystem specified by service_id.
194 *
[38542dc]195 * @param rfn Output pointer to loaded node
196 * @param service_id Device to load root node from
197 *
198 * @return Error code
199 *
[1fff583]200 */
[be39fc6]201int ext4_root_get(fs_node_t **rfn, service_id_t service_id)
[d3a9ae74]202{
[be39fc6]203 return ext4_node_get(rfn, service_id, EXT4_INODE_ROOT_INDEX);
[d3a9ae74]204}
205
[1fff583]206/** Check if specified name (component) matches with any directory entry.
207 *
208 * If match is found, load and return matching node.
209 *
[38542dc]210 * @param rfn Output pointer to node if operation successful
211 * @param pfn Parent directory node
212 * @param component Name to check directory for
213 *
214 * @return Error code
215 *
[1fff583]216 */
[be39fc6]217int ext4_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
[d3a9ae74]218{
[be39fc6]219 ext4_node_t *eparent = EXT4_NODE(pfn);
[5614c7f]220 ext4_filesystem_t *fs = eparent->instance->filesystem;
[38542dc]221
[9b9d37bb]222 if (!ext4_inode_is_type(fs->superblock, eparent->inode_ref->inode,
[38542dc]223 EXT4_INODE_MODE_DIRECTORY))
[9b9d37bb]224 return ENOTDIR;
[38542dc]225
[06d85e5]226 /* Try to find entry */
[7689590]227 ext4_directory_search_result_t result;
[38542dc]228 int rc = ext4_directory_find_entry(&result, eparent->inode_ref,
229 component);
[8be96a0]230 if (rc != EOK) {
[ea75ceb]231 if (rc == ENOENT) {
232 *rfn = NULL;
233 return EOK;
234 }
[38542dc]235
[8be96a0]236 return rc;
[9b9d37bb]237 }
[38542dc]238
[06d85e5]239 /* Load node from search result */
[7689590]240 uint32_t inode = ext4_directory_entry_ll_get_inode(result.dentry);
[be39fc6]241 rc = ext4_node_get_core(rfn, eparent->instance, inode);
[38542dc]242 if (rc != EOK)
[532f53d]243 goto exit;
244
245exit:
246 ;
247
[06d85e5]248 /* Destroy search result structure */
[532f53d]249 int const rc2 = ext4_directory_destroy_result(&result);
250 return rc == EOK ? rc2 : rc;
[d3a9ae74]251}
252
[1fff583]253/** Get node specified by index
254 *
255 * It's wrapper for node_put_core operation
256 *
[38542dc]257 * @param rfn Output pointer to loaded node if operation successful
258 * @param service_id Device identifier
259 * @param index Node index (here i-node number)
260 *
261 * @return Error code
262 *
[1fff583]263 */
[be39fc6]264int ext4_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
[d3a9ae74]265{
[be39fc6]266 ext4_instance_t *inst;
267 int rc = ext4_instance_get(service_id, &inst);
[38542dc]268 if (rc != EOK)
[9c0c0e1]269 return rc;
[38542dc]270
[be39fc6]271 return ext4_node_get_core(rfn, inst, index);
[d3a9ae74]272}
273
[1fff583]274/** Main function for getting node from the filesystem.
275 *
[38542dc]276 * @param rfn Output point to loaded node if operation successful
277 * @param inst Instance of filesystem
278 * @param index Index of node (i-node number)
279 *
280 * @return Error code
281 *
[1fff583]282 */
[be39fc6]283int ext4_node_get_core(fs_node_t **rfn, ext4_instance_t *inst,
[38542dc]284 fs_index_t index)
[6c501f8]285{
[3711e7e]286 fibril_mutex_lock(&open_nodes_lock);
[38542dc]287
[3711e7e]288 /* Check if the node is not already open */
[062d900]289 node_key_t key = {
290 .service_id = inst->service_id,
291 .index = index
[3711e7e]292 };
[38542dc]293
[062d900]294 ht_link_t *already_open = hash_table_find(&open_nodes, &key);
[be39fc6]295 ext4_node_t *enode = NULL;
[3711e7e]296 if (already_open) {
[be39fc6]297 enode = hash_table_get_inst(already_open, ext4_node_t, link);
[3711e7e]298 *rfn = enode->fs_node;
299 enode->references++;
[38542dc]300
[3711e7e]301 fibril_mutex_unlock(&open_nodes_lock);
302 return EOK;
303 }
[38542dc]304
[06d85e5]305 /* Prepare new enode */
[be39fc6]306 enode = malloc(sizeof(ext4_node_t));
[3711e7e]307 if (enode == NULL) {
308 fibril_mutex_unlock(&open_nodes_lock);
309 return ENOMEM;
310 }
[38542dc]311
[06d85e5]312 /* Prepare new fs_node and initialize */
[5614c7f]313 fs_node_t *fs_node = malloc(sizeof(fs_node_t));
314 if (fs_node == NULL) {
[3711e7e]315 free(enode);
316 fibril_mutex_unlock(&open_nodes_lock);
317 return ENOMEM;
318 }
[38542dc]319
[5614c7f]320 fs_node_initialize(fs_node);
[38542dc]321
[06d85e5]322 /* Load i-node from filesystem */
[5614c7f]323 ext4_inode_ref_t *inode_ref;
[38542dc]324 int rc = ext4_filesystem_get_inode_ref(inst->filesystem, index,
325 &inode_ref);
[3711e7e]326 if (rc != EOK) {
327 free(enode);
[5614c7f]328 free(fs_node);
[3711e7e]329 fibril_mutex_unlock(&open_nodes_lock);
330 return rc;
331 }
[38542dc]332
[06d85e5]333 /* Initialize enode */
[3711e7e]334 enode->inode_ref = inode_ref;
335 enode->instance = inst;
336 enode->references = 1;
[5614c7f]337 enode->fs_node = fs_node;
[38542dc]338
[5614c7f]339 fs_node->data = enode;
340 *rfn = fs_node;
[38542dc]341
[062d900]342 hash_table_insert(&open_nodes, &enode->link);
[3711e7e]343 inst->open_nodes_count++;
[38542dc]344
[3711e7e]345 fibril_mutex_unlock(&open_nodes_lock);
[38542dc]346
[6c501f8]347 return EOK;
348}
349
[1fff583]350/** Put previously loaded node.
351 *
[38542dc]352 * @param enode Node to put back
353 *
354 * @return Error code
355 *
[1fff583]356 */
[be39fc6]357static int ext4_node_put_core(ext4_node_t *enode)
[9b9d37bb]358{
[062d900]359 hash_table_remove_item(&open_nodes, &enode->link);
[9b9d37bb]360 assert(enode->instance->open_nodes_count > 0);
361 enode->instance->open_nodes_count--;
[38542dc]362
[06d85e5]363 /* Put inode back in filesystem */
[38542dc]364 int rc = ext4_filesystem_put_inode_ref(enode->inode_ref);
365 if (rc != EOK)
[9b9d37bb]366 return rc;
[38542dc]367
[06d85e5]368 /* Destroy data structure */
[9b9d37bb]369 free(enode->fs_node);
370 free(enode);
[38542dc]371
[9c0c0e1]372 return EOK;
373}
374
[1fff583]375/** Open node.
376 *
377 * This operation is stateless in this driver.
378 *
[38542dc]379 * @param fn Node to open
380 *
381 * @return EOK
382 *
[1fff583]383 */
[be39fc6]384int ext4_node_open(fs_node_t *fn)
[d3a9ae74]385{
[06d85e5]386 /* Stateless operation */
[d3a9ae74]387 return EOK;
388}
389
[1fff583]390/** Put previously loaded node.
391 *
[38542dc]392 * A wrapper for node_put_core operation
393 *
394 * @param fn Node to put back
395 * @return Error code
[1fff583]396 *
397 */
[be39fc6]398int ext4_node_put(fs_node_t *fn)
[d3a9ae74]399{
[9c0c0e1]400 fibril_mutex_lock(&open_nodes_lock);
[38542dc]401
[be39fc6]402 ext4_node_t *enode = EXT4_NODE(fn);
[9c0c0e1]403 assert(enode->references > 0);
404 enode->references--;
405 if (enode->references == 0) {
[be39fc6]406 int rc = ext4_node_put_core(enode);
[9c0c0e1]407 if (rc != EOK) {
408 fibril_mutex_unlock(&open_nodes_lock);
409 return rc;
410 }
411 }
[38542dc]412
[9c0c0e1]413 fibril_mutex_unlock(&open_nodes_lock);
[38542dc]414
[d3a9ae74]415 return EOK;
416}
417
[1fff583]418/** Create new node in filesystem.
419 *
[38542dc]420 * @param rfn Output pointer to newly created node if successful
421 * @param service_id Device identifier, where the filesystem is
422 * @param flags Flags for specification of new node parameters
423 *
424 * @return Error code
425 *
[1fff583]426 */
[be39fc6]427int ext4_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
[d3a9ae74]428{
[06d85e5]429 /* Allocate enode */
[be39fc6]430 ext4_node_t *enode;
431 enode = malloc(sizeof(ext4_node_t));
[38542dc]432 if (enode == NULL)
[47a89fe]433 return ENOMEM;
[38542dc]434
[06d85e5]435 /* Allocate fs_node */
[47a89fe]436 fs_node_t *fs_node;
437 fs_node = malloc(sizeof(fs_node_t));
438 if (fs_node == NULL) {
439 free(enode);
440 return ENOMEM;
441 }
[38542dc]442
[06d85e5]443 /* Load instance */
[be39fc6]444 ext4_instance_t *inst;
445 int rc = ext4_instance_get(service_id, &inst);
[47a89fe]446 if (rc != EOK) {
447 free(enode);
448 free(fs_node);
449 return rc;
450 }
[38542dc]451
[06d85e5]452 /* Allocate new i-node in filesystem */
[47a89fe]453 ext4_inode_ref_t *inode_ref;
[304faab]454 rc = ext4_filesystem_alloc_inode(inst->filesystem, &inode_ref, flags);
[47a89fe]455 if (rc != EOK) {
456 free(enode);
457 free(fs_node);
458 return rc;
459 }
[38542dc]460
[06d85e5]461 /* Do some interconnections in references */
[47a89fe]462 enode->inode_ref = inode_ref;
463 enode->instance = inst;
464 enode->references = 1;
[38542dc]465
[47a89fe]466 fibril_mutex_lock(&open_nodes_lock);
[062d900]467 hash_table_insert(&open_nodes, &enode->link);
[47a89fe]468 fibril_mutex_unlock(&open_nodes_lock);
469 inst->open_nodes_count++;
[38542dc]470
[47a89fe]471 enode->inode_ref->dirty = true;
[38542dc]472
[47a89fe]473 fs_node_initialize(fs_node);
474 fs_node->data = enode;
475 enode->fs_node = fs_node;
476 *rfn = fs_node;
[38542dc]477
[47a89fe]478 return EOK;
[d3a9ae74]479}
480
[1fff583]481/** Destroy existing node.
482 *
[38542dc]483 * @param fs Node to destroy
484 *
485 * @return Error code
486 *
[1fff583]487 */
[be39fc6]488int ext4_destroy_node(fs_node_t *fn)
[d3a9ae74]489{
[06d85e5]490 /* If directory, check for children */
[3d4fd2c]491 bool has_children;
[be39fc6]492 int rc = ext4_has_children(&has_children, fn);
[3d4fd2c]493 if (rc != EOK) {
[be39fc6]494 ext4_node_put(fn);
[3d4fd2c]495 return rc;
496 }
[38542dc]497
[3d4fd2c]498 if (has_children) {
[be39fc6]499 ext4_node_put(fn);
[3d4fd2c]500 return EINVAL;
501 }
[38542dc]502
[be39fc6]503 ext4_node_t *enode = EXT4_NODE(fn);
[3d4fd2c]504 ext4_inode_ref_t *inode_ref = enode->inode_ref;
[38542dc]505
[06d85e5]506 /* Release data blocks */
[1ac1ab4]507 rc = ext4_filesystem_truncate_inode(inode_ref, 0);
[3d4fd2c]508 if (rc != EOK) {
[be39fc6]509 ext4_node_put(fn);
[3d4fd2c]510 return rc;
511 }
[38542dc]512
513 /*
514 * TODO: Sset real deletion time when it will be supported.
515 * Temporary set fake deletion time.
516 */
[ca47f656]517 ext4_inode_set_deletion_time(inode_ref->inode, 0xdeadbeef);
[07fd4cd1]518 inode_ref->dirty = true;
[38542dc]519
[06d85e5]520 /* Free inode */
[1ac1ab4]521 rc = ext4_filesystem_free_inode(inode_ref);
[3d4fd2c]522 if (rc != EOK) {
[be39fc6]523 ext4_node_put(fn);
[3d4fd2c]524 return rc;
525 }
[38542dc]526
[be39fc6]527 return ext4_node_put(fn);
[d3a9ae74]528}
529
[1fff583]530/** Link the specfied node to directory.
531 *
[38542dc]532 * @param pfn Parent node to link in
533 * @param cfn Node to be linked
534 * @param name Name which will be assigned to directory entry
535 *
536 * @return Error code
537 *
[1fff583]538 */
[be39fc6]539int ext4_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
[d3a9ae74]540{
[06d85e5]541 /* Check maximum name length */
[38542dc]542 if (str_size(name) > EXT4_DIRECTORY_FILENAME_LEN)
[47a89fe]543 return ENAMETOOLONG;
[38542dc]544
[be39fc6]545 ext4_node_t *parent = EXT4_NODE(pfn);
546 ext4_node_t *child = EXT4_NODE(cfn);
[47a89fe]547 ext4_filesystem_t *fs = parent->instance->filesystem;
[38542dc]548
[06d85e5]549 /* Add entry to parent directory */
[38542dc]550 int rc = ext4_directory_add_entry(parent->inode_ref, name,
551 child->inode_ref);
552 if (rc != EOK)
[47a89fe]553 return rc;
[38542dc]554
[06d85e5]555 /* Fill new dir -> add '.' and '..' entries */
[38542dc]556 if (ext4_inode_is_type(fs->superblock, child->inode_ref->inode,
557 EXT4_INODE_MODE_DIRECTORY)) {
558 rc = ext4_directory_add_entry(child->inode_ref, ".",
559 child->inode_ref);
[47a89fe]560 if (rc != EOK) {
[1ac1ab4]561 ext4_directory_remove_entry(parent->inode_ref, name);
[47a89fe]562 return rc;
563 }
[38542dc]564
565 rc = ext4_directory_add_entry(child->inode_ref, "..",
566 parent->inode_ref);
[47a89fe]567 if (rc != EOK) {
[1ac1ab4]568 ext4_directory_remove_entry(parent->inode_ref, name);
569 ext4_directory_remove_entry(child->inode_ref, ".");
[47a89fe]570 return rc;
571 }
[38542dc]572
[06d85e5]573 /* Initialize directory index if supported */
[38542dc]574 if (ext4_superblock_has_feature_compatible(fs->superblock,
575 EXT4_FEATURE_COMPAT_DIR_INDEX)) {
[b6d7b7c]576 rc = ext4_directory_dx_init(child->inode_ref);
[38542dc]577 if (rc != EOK)
[b6d7b7c]578 return rc;
[38542dc]579
580 ext4_inode_set_flag(child->inode_ref->inode,
581 EXT4_INODE_FLAG_INDEX);
[b6d7b7c]582 child->inode_ref->dirty = true;
583 }
[38542dc]584
585 uint16_t parent_links =
586 ext4_inode_get_links_count(parent->inode_ref->inode);
[47a89fe]587 parent_links++;
588 ext4_inode_set_links_count(parent->inode_ref->inode, parent_links);
[38542dc]589
[47a89fe]590 parent->inode_ref->dirty = true;
591 }
[38542dc]592
593 uint16_t child_links =
594 ext4_inode_get_links_count(child->inode_ref->inode);
[47a89fe]595 child_links++;
596 ext4_inode_set_links_count(child->inode_ref->inode, child_links);
[38542dc]597
[47a89fe]598 child->inode_ref->dirty = true;
[38542dc]599
[47a89fe]600 return EOK;
[d3a9ae74]601}
602
[1fff583]603/** Unlink node from specified directory.
604 *
[38542dc]605 * @param pfn Parent node to delete node from
606 * @param cfn Child node to be unlinked from directory
607 * @param name Name of entry that will be removed
608 *
609 * @return Error code
610 *
[1fff583]611 */
[be39fc6]612int ext4_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
[d3a9ae74]613{
[ebeaaa06]614 bool has_children;
[be39fc6]615 int rc = ext4_has_children(&has_children, cfn);
[38542dc]616 if (rc != EOK)
[ebeaaa06]617 return rc;
[38542dc]618
[06d85e5]619 /* Cannot unlink non-empty node */
[38542dc]620 if (has_children)
[ebeaaa06]621 return ENOTEMPTY;
[38542dc]622
[06d85e5]623 /* Remove entry from parent directory */
[be39fc6]624 ext4_inode_ref_t *parent = EXT4_NODE(pfn)->inode_ref;
[1ac1ab4]625 rc = ext4_directory_remove_entry(parent, name);
[38542dc]626 if (rc != EOK)
[f49638e]627 return rc;
[38542dc]628
[06d85e5]629 /* Decrement links count */
[be39fc6]630 ext4_inode_ref_t *child_inode_ref = EXT4_NODE(cfn)->inode_ref;
[38542dc]631
632 uint32_t lnk_count =
633 ext4_inode_get_links_count(child_inode_ref->inode);
[ebeaaa06]634 lnk_count--;
[38542dc]635
[06d85e5]636 /* If directory - handle links from parent */
[be39fc6]637 if ((lnk_count <= 1) && (ext4_is_directory(cfn))) {
[1e48a07e]638 assert(lnk_count == 1);
[38542dc]639
[1e48a07e]640 lnk_count--;
[38542dc]641
[be39fc6]642 ext4_inode_ref_t *parent_inode_ref = EXT4_NODE(pfn)->inode_ref;
[38542dc]643
[82d7816]644 uint32_t parent_lnk_count = ext4_inode_get_links_count(
[38542dc]645 parent_inode_ref->inode);
646
[82d7816]647 parent_lnk_count--;
648 ext4_inode_set_links_count(parent_inode_ref->inode, parent_lnk_count);
[38542dc]649
[07fd4cd1]650 parent->dirty = true;
651 }
652
[38542dc]653 /*
654 * TODO: Update timestamps of the parent
655 * (when we have wall-clock time).
656 *
657 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
658 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
659 * parent->dirty = true;
660 */
661
662 /*
663 * TODO: Update timestamp for inode.
664 *
665 * ext4_inode_set_change_inode_time(child_inode_ref->inode,
666 * (uint32_t) now);
667 */
668
[82d7816]669 ext4_inode_set_links_count(child_inode_ref->inode, lnk_count);
670 child_inode_ref->dirty = true;
[38542dc]671
[ebeaaa06]672 return EOK;
[d3a9ae74]673}
674
[1fff583]675/** Check if specified node has children.
[38542dc]676 *
[1fff583]677 * For files is response allways false and check is executed only for directories.
678 *
[38542dc]679 * @param has_children Output value for response
680 * @param fn Node to check
681 *
682 * @return Error code
683 *
[1fff583]684 */
[be39fc6]685int ext4_has_children(bool *has_children, fs_node_t *fn)
[d3a9ae74]686{
[be39fc6]687 ext4_node_t *enode = EXT4_NODE(fn);
[5614c7f]688 ext4_filesystem_t *fs = enode->instance->filesystem;
[38542dc]689
[06d85e5]690 /* Check if node is directory */
[e68c834]691 if (!ext4_inode_is_type(fs->superblock, enode->inode_ref->inode,
692 EXT4_INODE_MODE_DIRECTORY)) {
693 *has_children = false;
694 return EOK;
695 }
[38542dc]696
[5614c7f]697 ext4_directory_iterator_t it;
[38542dc]698 int rc = ext4_directory_iterator_init(&it, enode->inode_ref, 0);
699 if (rc != EOK)
[e68c834]700 return rc;
[38542dc]701
[e68c834]702 /* Find a non-empty directory entry */
[5614c7f]703 bool found = false;
[e68c834]704 while (it.current != NULL) {
705 if (it.current->inode != 0) {
[38542dc]706 uint16_t name_size =
707 ext4_directory_entry_ll_get_name_length(fs->superblock,
708 it.current);
[be39fc6]709 if (!ext4_is_dots(it.current->name, name_size)) {
[e68c834]710 found = true;
711 break;
712 }
713 }
[38542dc]714
[e68c834]715 rc = ext4_directory_iterator_next(&it);
716 if (rc != EOK) {
717 ext4_directory_iterator_fini(&it);
718 return rc;
719 }
720 }
[38542dc]721
[e68c834]722 rc = ext4_directory_iterator_fini(&it);
[38542dc]723 if (rc != EOK)
[e68c834]724 return rc;
[38542dc]725
[e68c834]726 *has_children = found;
[38542dc]727
[d3a9ae74]728 return EOK;
729}
730
[1fff583]731/** Unpack index number from node.
[38542dc]732 *
733 * @param fn Node to load index from
734 *
735 * @return Index number of i-node
736 *
[1fff583]737 */
[be39fc6]738fs_index_t ext4_index_get(fs_node_t *fn)
[d3a9ae74]739{
[be39fc6]740 ext4_node_t *enode = EXT4_NODE(fn);
[9b9d37bb]741 return enode->inode_ref->index;
[d3a9ae74]742}
743
[1fff583]744/** Get real size of file / directory.
745 *
[38542dc]746 * @param fn Node to get size of
747 *
748 * @return Real size of node
749 *
[1fff583]750 */
[be39fc6]751aoff64_t ext4_size_get(fs_node_t *fn)
[d3a9ae74]752{
[be39fc6]753 ext4_node_t *enode = EXT4_NODE(fn);
[5614c7f]754 ext4_superblock_t *sb = enode->instance->filesystem->superblock;
755 return ext4_inode_get_size(sb, enode->inode_ref->inode);
[d3a9ae74]756}
757
[1fff583]758/** Get number of links to specified node.
759 *
[38542dc]760 * @param fn Node to get links to
761 *
762 * @return Number of links
763 *
[1fff583]764 */
[be39fc6]765unsigned ext4_lnkcnt_get(fs_node_t *fn)
[d3a9ae74]766{
[be39fc6]767 ext4_node_t *enode = EXT4_NODE(fn);
[d4d2954]768 uint32_t lnkcnt = ext4_inode_get_links_count(enode->inode_ref->inode);
[38542dc]769
[be39fc6]770 if (ext4_is_directory(fn)) {
[38542dc]771 if (lnkcnt > 1)
[e5f8762]772 return 1;
[38542dc]773 else
[e5f8762]774 return 0;
775 }
[38542dc]776
[06d85e5]777 /* For regular files return real links count */
[d4d2954]778 return lnkcnt;
[d3a9ae74]779}
780
[1fff583]781/** Check if node is directory.
782 *
[38542dc]783 * @param fn Node to check
784 *
785 * @return Result of check
786 *
[1fff583]787 */
[be39fc6]788bool ext4_is_directory(fs_node_t *fn)
[d3a9ae74]789{
[be39fc6]790 ext4_node_t *enode = EXT4_NODE(fn);
[5614c7f]791 ext4_superblock_t *sb = enode->instance->filesystem->superblock;
[38542dc]792
793 return ext4_inode_is_type(sb, enode->inode_ref->inode,
794 EXT4_INODE_MODE_DIRECTORY);
[d3a9ae74]795}
796
[1fff583]797/** Check if node is regular file.
798 *
[38542dc]799 * @param fn Node to check
800 *
801 * @return Result of check
802 *
[1fff583]803 */
[be39fc6]804bool ext4_is_file(fs_node_t *fn)
[d3a9ae74]805{
[be39fc6]806 ext4_node_t *enode = EXT4_NODE(fn);
[5614c7f]807 ext4_superblock_t *sb = enode->instance->filesystem->superblock;
[38542dc]808
809 return ext4_inode_is_type(sb, enode->inode_ref->inode,
810 EXT4_INODE_MODE_FILE);
[d3a9ae74]811}
812
[1fff583]813/** Extract device identifier from node.
814 *
[38542dc]815 * @param node Node to extract id from
816 *
817 * @return id of device, where is the filesystem
818 *
[1fff583]819 */
[be39fc6]820service_id_t ext4_service_get(fs_node_t *fn)
[d3a9ae74]821{
[be39fc6]822 ext4_node_t *enode = EXT4_NODE(fn);
[e68c834]823 return enode->instance->service_id;
[d3a9ae74]824}
825
[be39fc6]826int ext4_size_block(service_id_t service_id, uint32_t *size)
[5930e3f]827{
[be39fc6]828 ext4_instance_t *inst;
829 int rc = ext4_instance_get(service_id, &inst);
[5930e3f]830 if (rc != EOK)
831 return rc;
[3dd148d]832
[5930e3f]833 if (NULL == inst)
834 return ENOENT;
[e8f0158]835
[5930e3f]836 ext4_superblock_t *sb = inst->filesystem->superblock;
[3dd148d]837 *size = ext4_superblock_get_block_size(sb);
[e8f0158]838
[3dd148d]839 return EOK;
[5930e3f]840}
841
[be39fc6]842int ext4_total_block_count(service_id_t service_id, uint64_t *count)
[e8f0158]843{
[be39fc6]844 ext4_instance_t *inst;
845 int rc = ext4_instance_get(service_id, &inst);
[e8f0158]846 if (rc != EOK)
847 return rc;
[3dd148d]848
[e8f0158]849 if (NULL == inst)
850 return ENOENT;
851
852 ext4_superblock_t *sb = inst->filesystem->superblock;
[3dd148d]853 *count = ext4_superblock_get_blocks_count(sb);
[e8f0158]854
[3dd148d]855 return EOK;
[e8f0158]856}
857
[be39fc6]858int ext4_free_block_count(service_id_t service_id, uint64_t *count)
[e8f0158]859{
[be39fc6]860 ext4_instance_t *inst;
861 int rc = ext4_instance_get(service_id, &inst);
[e8f0158]862 if (rc != EOK)
863 return rc;
864
865 ext4_superblock_t *sb = inst->filesystem->superblock;
[3dd148d]866 *count = ext4_superblock_get_free_blocks_count(sb);
[e8f0158]867
[3dd148d]868 return EOK;
[e8f0158]869}
870
[d3a9ae74]871/*
872 * libfs operations.
873 */
[be39fc6]874libfs_ops_t ext4_libfs_ops = {
875 .root_get = ext4_root_get,
876 .match = ext4_match,
877 .node_get = ext4_node_get,
878 .node_open = ext4_node_open,
879 .node_put = ext4_node_put,
880 .create = ext4_create_node,
881 .destroy = ext4_destroy_node,
882 .link = ext4_link,
883 .unlink = ext4_unlink,
884 .has_children = ext4_has_children,
885 .index_get = ext4_index_get,
886 .size_get = ext4_size_get,
887 .lnkcnt_get = ext4_lnkcnt_get,
888 .is_directory = ext4_is_directory,
889 .is_file = ext4_is_file,
890 .service_get = ext4_service_get,
891 .size_block = ext4_size_block,
892 .total_block_count = ext4_total_block_count,
893 .free_block_count = ext4_free_block_count
[d3a9ae74]894};
895
896/*
897 * VFS operations.
898 */
899
[d2c8533]900/** Probe operation.
901 *
902 * Try to get information about specified filesystem from device.
903 *
904 * @param sevice_id Service ID
905 * @param info Place to store information
906 *
907 * @return Error code
908 */
[be39fc6]909static int ext4_fsprobe(service_id_t service_id, vfs_fs_probe_info_t *info)
[d2c8533]910{
911 return ENOTSUP;
912}
913
[38542dc]914/** Mount operation.
[1fff583]915 *
916 * Try to mount specified filesystem from device.
[38542dc]917 *
918 * @param service_id Identifier of device
919 * @param opts Mount options
920 * @param index Output value - index of root node
921 * @param size Output value - size of root node
922 * @param lnkcnt Output value - link count of root node
923 *
924 * @return Error code
925 *
[1fff583]926 */
[be39fc6]927static int ext4_mounted(service_id_t service_id, const char *opts,
[38542dc]928 fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
[d3a9ae74]929{
[de5b708]930 ext4_filesystem_t *fs;
[38542dc]931
[6c501f8]932 /* Allocate instance structure */
[be39fc6]933 ext4_instance_t *inst = (ext4_instance_t *)
934 malloc(sizeof(ext4_instance_t));
[de5b708]935 if (inst == NULL)
[6c501f8]936 return ENOMEM;
[38542dc]937
[0b293a6]938 enum cache_mode cmode;
[38542dc]939 if (str_cmp(opts, "wtcache") == 0)
[0b293a6]940 cmode = CACHE_MODE_WT;
[38542dc]941 else
[0b293a6]942 cmode = CACHE_MODE_WB;
[38542dc]943
[6c501f8]944 /* Initialize instance */
945 link_initialize(&inst->link);
946 inst->service_id = service_id;
947 inst->open_nodes_count = 0;
[38542dc]948
[4bfad34]949 /* Initialize the filesystem */
950 aoff64_t rnsize;
[de5b708]951 int rc = ext4_filesystem_open(inst, service_id, cmode, &rnsize, &fs);
[6c501f8]952 if (rc != EOK) {
953 free(inst);
954 return rc;
955 }
[38542dc]956
[6c501f8]957 /* Add instance to the list */
958 fibril_mutex_lock(&instance_list_mutex);
959 list_append(&inst->link, &instance_list);
960 fibril_mutex_unlock(&instance_list_mutex);
[38542dc]961
[6c501f8]962 *index = EXT4_INODE_ROOT_INDEX;
[4bfad34]963 *size = rnsize;
[e5f8762]964 *lnkcnt = 1;
[38542dc]965
[4bfad34]966 return EOK;
[d3a9ae74]967}
968
[1fff583]969/** Unmount operation.
970 *
971 * Correctly release the filesystem.
972 *
[38542dc]973 * @param service_id Device to be unmounted
974 *
975 * @return Error code
976 *
[1fff583]977 */
[be39fc6]978static int ext4_unmounted(service_id_t service_id)
[d3a9ae74]979{
[be39fc6]980 ext4_instance_t *inst;
981 int rc = ext4_instance_get(service_id, &inst);
[38542dc]982 if (rc != EOK)
[6c501f8]983 return rc;
[38542dc]984
[6c501f8]985 fibril_mutex_lock(&open_nodes_lock);
[38542dc]986
[6c501f8]987 if (inst->open_nodes_count != 0) {
988 fibril_mutex_unlock(&open_nodes_lock);
989 return EBUSY;
990 }
[38542dc]991
[6c501f8]992 /* Remove the instance from the list */
993 fibril_mutex_lock(&instance_list_mutex);
994 list_remove(&inst->link);
995 fibril_mutex_unlock(&instance_list_mutex);
[38542dc]996
[6c501f8]997 fibril_mutex_unlock(&open_nodes_lock);
[38542dc]998
[de5b708]999 rc = ext4_filesystem_close(inst->filesystem);
1000 if (rc != EOK) {
1001 fibril_mutex_lock(&instance_list_mutex);
1002 list_append(&inst->link, &instance_list);
1003 fibril_mutex_unlock(&instance_list_mutex);
1004 }
1005
1006 free(inst);
1007 return EOK;
[d3a9ae74]1008}
1009
[1fff583]1010/** Read bytes from node.
1011 *
[38542dc]1012 * @param service_id Device to read data from
1013 * @param index Number of node to read from
1014 * @param pos Position where the read should be started
1015 * @param rbytes Output value, where the real size was returned
1016 *
1017 * @return Error code
1018 *
[1fff583]1019 */
[be39fc6]1020static int ext4_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
[38542dc]1021 size_t *rbytes)
[d3a9ae74]1022{
[9b9d37bb]1023 /*
1024 * Receive the read request.
1025 */
1026 ipc_callid_t callid;
1027 size_t size;
1028 if (!async_data_read_receive(&callid, &size)) {
1029 async_answer_0(callid, EINVAL);
1030 return EINVAL;
1031 }
[38542dc]1032
[be39fc6]1033 ext4_instance_t *inst;
1034 int rc = ext4_instance_get(service_id, &inst);
[9b9d37bb]1035 if (rc != EOK) {
1036 async_answer_0(callid, rc);
1037 return rc;
1038 }
[38542dc]1039
[06d85e5]1040 /* Load i-node */
[5614c7f]1041 ext4_inode_ref_t *inode_ref;
[9b9d37bb]1042 rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
1043 if (rc != EOK) {
1044 async_answer_0(callid, rc);
1045 return rc;
1046 }
[38542dc]1047
[06d85e5]1048 /* Read from i-node by type */
[9b9d37bb]1049 if (ext4_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
[38542dc]1050 EXT4_INODE_MODE_FILE)) {
[be39fc6]1051 rc = ext4_read_file(callid, pos, size, inst, inode_ref,
[38542dc]1052 rbytes);
[9b9d37bb]1053 } else if (ext4_inode_is_type(inst->filesystem->superblock,
[38542dc]1054 inode_ref->inode, EXT4_INODE_MODE_DIRECTORY)) {
[be39fc6]1055 rc = ext4_read_directory(callid, pos, size, inst, inode_ref,
[38542dc]1056 rbytes);
[9b9d37bb]1057 } else {
1058 /* Other inode types not supported */
1059 async_answer_0(callid, ENOTSUP);
1060 rc = ENOTSUP;
1061 }
[38542dc]1062
[532f53d]1063 int const rc2 = ext4_filesystem_put_inode_ref(inode_ref);
[38542dc]1064
[532f53d]1065 return rc == EOK ? rc2 : rc;
[9b9d37bb]1066}
1067
[1fff583]1068/** Check if filename is dot or dotdot (reserved names).
1069 *
[38542dc]1070 * @param name Name to check
1071 * @param name_size Length of string name
1072 *
1073 * @return Result of the check
1074 *
[1fff583]1075 */
[be39fc6]1076bool ext4_is_dots(const uint8_t *name, size_t name_size)
[8958a26]1077{
[38542dc]1078 if ((name_size == 1) && (name[0] == '.'))
[9b9d37bb]1079 return true;
[38542dc]1080
1081 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
[9b9d37bb]1082 return true;
[38542dc]1083
[9b9d37bb]1084 return false;
1085}
1086
[1fff583]1087/** Read data from directory.
1088 *
[38542dc]1089 * @param callid IPC id of call (for communication)
1090 * @param pos Position to start reading from
1091 * @param size How many bytes to read
1092 * @param inst Filesystem instance
1093 * @param inode_ref Node to read data from
1094 * @param rbytes Output value to return real number of bytes was read
1095 *
1096 * @return Error code
1097 *
[1fff583]1098 */
[be39fc6]1099int ext4_read_directory(ipc_callid_t callid, aoff64_t pos, size_t size,
1100 ext4_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
[9b9d37bb]1101{
[5614c7f]1102 ext4_directory_iterator_t it;
[38542dc]1103 int rc = ext4_directory_iterator_init(&it, inode_ref, pos);
[9b9d37bb]1104 if (rc != EOK) {
1105 async_answer_0(callid, rc);
1106 return rc;
1107 }
[38542dc]1108
1109 /*
1110 * Find next interesting directory entry.
[9b9d37bb]1111 * We want to skip . and .. entries
1112 * as these are not used in HelenOS
1113 */
[5614c7f]1114 bool found = false;
[9b9d37bb]1115 while (it.current != NULL) {
[38542dc]1116 if (it.current->inode == 0)
[9b9d37bb]1117 goto skip;
[38542dc]1118
[5614c7f]1119 uint16_t name_size = ext4_directory_entry_ll_get_name_length(
[9b9d37bb]1120 inst->filesystem->superblock, it.current);
[38542dc]1121
1122 /* Skip . and .. */
[be39fc6]1123 if (ext4_is_dots(it.current->name, name_size))
[9b9d37bb]1124 goto skip;
[38542dc]1125
1126 /*
1127 * The on-disk entry does not contain \0 at the end
[9b9d37bb]1128 * end of entry name, so we copy it to new buffer
1129 * and add the \0 at the end
1130 */
[38542dc]1131 uint8_t *buf = malloc(name_size + 1);
[9b9d37bb]1132 if (buf == NULL) {
1133 ext4_directory_iterator_fini(&it);
1134 async_answer_0(callid, ENOMEM);
1135 return ENOMEM;
1136 }
[38542dc]1137
[9b9d37bb]1138 memcpy(buf, &it.current->name, name_size);
1139 *(buf + name_size) = 0;
1140 found = true;
[38542dc]1141
[9b9d37bb]1142 (void) async_data_read_finalize(callid, buf, name_size + 1);
1143 free(buf);
1144 break;
[38542dc]1145
[9b9d37bb]1146skip:
1147 rc = ext4_directory_iterator_next(&it);
1148 if (rc != EOK) {
1149 ext4_directory_iterator_fini(&it);
1150 async_answer_0(callid, rc);
1151 return rc;
1152 }
1153 }
[38542dc]1154
[5614c7f]1155 uint64_t next;
[9b9d37bb]1156 if (found) {
1157 rc = ext4_directory_iterator_next(&it);
[38542dc]1158 if (rc != EOK)
[9b9d37bb]1159 return rc;
[38542dc]1160
[9b9d37bb]1161 next = it.current_offset;
1162 }
[38542dc]1163
[9b9d37bb]1164 rc = ext4_directory_iterator_fini(&it);
[38542dc]1165 if (rc != EOK)
[9b9d37bb]1166 return rc;
[38542dc]1167
[06d85e5]1168 /* Prepare return values */
[9b9d37bb]1169 if (found) {
1170 *rbytes = next - pos;
1171 return EOK;
1172 } else {
1173 async_answer_0(callid, ENOENT);
1174 return ENOENT;
1175 }
[d3a9ae74]1176}
1177
[1fff583]1178/** Read data from file.
1179 *
[38542dc]1180 * @param callid IPC id of call (for communication)
1181 * @param pos Position to start reading from
1182 * @param size How many bytes to read
1183 * @param inst Filesystem instance
1184 * @param inode_ref Node to read data from
1185 * @param rbytes Output value to return real number of bytes was read
1186 *
1187 * @return Error code
1188 *
[1fff583]1189 */
[be39fc6]1190int ext4_read_file(ipc_callid_t callid, aoff64_t pos, size_t size,
1191 ext4_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
[9b9d37bb]1192{
[5614c7f]1193 ext4_superblock_t *sb = inst->filesystem->superblock;
1194 uint64_t file_size = ext4_inode_get_size(sb, inode_ref->inode);
[38542dc]1195
[9b9d37bb]1196 if (pos >= file_size) {
1197 /* Read 0 bytes successfully */
1198 async_data_read_finalize(callid, NULL, 0);
1199 *rbytes = 0;
1200 return EOK;
1201 }
[38542dc]1202
[9b9d37bb]1203 /* For now, we only read data from one block at a time */
[5614c7f]1204 uint32_t block_size = ext4_superblock_get_block_size(sb);
1205 aoff64_t file_block = pos / block_size;
1206 uint32_t offset_in_block = pos % block_size;
1207 uint32_t bytes = min(block_size - offset_in_block, size);
[38542dc]1208
[9b9d37bb]1209 /* Handle end of file */
[38542dc]1210 if (pos + bytes > file_size)
[9b9d37bb]1211 bytes = file_size - pos;
[38542dc]1212
[9b9d37bb]1213 /* Get the real block number */
[5614c7f]1214 uint32_t fs_block;
[38542dc]1215 int rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
1216 file_block, &fs_block);
[9b9d37bb]1217 if (rc != EOK) {
1218 async_answer_0(callid, rc);
1219 return rc;
1220 }
[38542dc]1221
1222 /*
1223 * Check for sparse file.
[2b9e142]1224 * If ext4_filesystem_get_inode_data_block_index returned
[9b9d37bb]1225 * fs_block == 0, it means that the given block is not allocated for the
1226 * file and we need to return a buffer of zeros
1227 */
[5614c7f]1228 uint8_t *buffer;
[9b9d37bb]1229 if (fs_block == 0) {
1230 buffer = malloc(bytes);
1231 if (buffer == NULL) {
1232 async_answer_0(callid, ENOMEM);
1233 return ENOMEM;
1234 }
[38542dc]1235
[9b9d37bb]1236 memset(buffer, 0, bytes);
[38542dc]1237
[532f53d]1238 rc = async_data_read_finalize(callid, buffer, bytes);
[9b9d37bb]1239 *rbytes = bytes;
[38542dc]1240
[9b9d37bb]1241 free(buffer);
[532f53d]1242 return rc;
[9b9d37bb]1243 }
[38542dc]1244
[9b9d37bb]1245 /* Usual case - we need to read a block from device */
[5614c7f]1246 block_t *block;
[9b9d37bb]1247 rc = block_get(&block, inst->service_id, fs_block, BLOCK_FLAGS_NONE);
1248 if (rc != EOK) {
1249 async_answer_0(callid, rc);
1250 return rc;
1251 }
[38542dc]1252
[9b9d37bb]1253 assert(offset_in_block + bytes <= block_size);
[532f53d]1254 rc = async_data_read_finalize(callid, block->data + offset_in_block, bytes);
1255 if (rc != EOK) {
1256 block_put(block);
1257 return rc;
1258 }
[38542dc]1259
[9b9d37bb]1260 rc = block_put(block);
[38542dc]1261 if (rc != EOK)
[9b9d37bb]1262 return rc;
[38542dc]1263
[9b9d37bb]1264 *rbytes = bytes;
1265 return EOK;
1266}
[3711e7e]1267
[1fff583]1268/** Write bytes to file
1269 *
[38542dc]1270 * @param service_id Device identifier
1271 * @param index I-node number of file
1272 * @param pos Position in file to start reading from
1273 * @param wbytes Output value - real number of written bytes
1274 * @param nsize Output value - new size of i-node
1275 *
1276 * @return Error code
1277 *
[1fff583]1278 */
[be39fc6]1279static int ext4_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
[38542dc]1280 size_t *wbytes, aoff64_t *nsize)
[d3a9ae74]1281{
[5614c7f]1282 fs_node_t *fn;
[be39fc6]1283 int rc = ext4_node_get(&fn, service_id, index);
[38542dc]1284 if (rc != EOK)
[1c1c736]1285 return rc;
[38542dc]1286
[5614c7f]1287 ipc_callid_t callid;
1288 size_t len;
[1c1c736]1289 if (!async_data_write_receive(&callid, &len)) {
[532f53d]1290 rc = EINVAL;
1291 async_answer_0(callid, rc);
1292 goto exit;
[1c1c736]1293 }
[38542dc]1294
[be39fc6]1295 ext4_node_t *enode = EXT4_NODE(fn);
[5614c7f]1296 ext4_filesystem_t *fs = enode->instance->filesystem;
[38542dc]1297
[5614c7f]1298 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
[38542dc]1299
[06d85e5]1300 /* Prevent writing to more than one block */
[5614c7f]1301 uint32_t bytes = min(len, block_size - (pos % block_size));
[38542dc]1302
[5614c7f]1303 int flags = BLOCK_FLAGS_NONE;
[38542dc]1304 if (bytes == block_size)
[35f48f2]1305 flags = BLOCK_FLAGS_NOREAD;
[38542dc]1306
[5614c7f]1307 uint32_t iblock = pos / block_size;
1308 uint32_t fblock;
[38542dc]1309
[06d85e5]1310 /* Load inode */
[5614c7f]1311 ext4_inode_ref_t *inode_ref = enode->inode_ref;
[38542dc]1312 rc = ext4_filesystem_get_inode_data_block_index(inode_ref, iblock,
1313 &fblock);
[6088193]1314 if (rc != EOK) {
[e63ce679]1315 async_answer_0(callid, rc);
[532f53d]1316 goto exit;
[6088193]1317 }
[38542dc]1318
[06d85e5]1319 /* Check for sparse file */
[1c1c736]1320 if (fblock == 0) {
[38542dc]1321 if ((ext4_superblock_has_feature_incompatible(fs->superblock,
1322 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1323 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1324 uint32_t last_iblock =
1325 ext4_inode_get_size(fs->superblock, inode_ref->inode) /
1326 block_size;
1327
[d510ac01]1328 while (last_iblock < iblock) {
[38542dc]1329 rc = ext4_extent_append_block(inode_ref, &last_iblock,
1330 &fblock, true);
[1196df6]1331 if (rc != EOK) {
1332 async_answer_0(callid, rc);
[532f53d]1333 goto exit;
[1196df6]1334 }
[d510ac01]1335 }
[38542dc]1336
1337 rc = ext4_extent_append_block(inode_ref, &last_iblock,
1338 &fblock, false);
[d510ac01]1339 if (rc != EOK) {
1340 async_answer_0(callid, rc);
[532f53d]1341 goto exit;
[d510ac01]1342 }
[1196df6]1343 } else {
[38542dc]1344 rc = ext4_balloc_alloc_block(inode_ref, &fblock);
[1196df6]1345 if (rc != EOK) {
1346 async_answer_0(callid, rc);
[532f53d]1347 goto exit;
[1196df6]1348 }
[38542dc]1349
1350 rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
1351 iblock, fblock);
[1196df6]1352 if (rc != EOK) {
1353 ext4_balloc_free_block(inode_ref, fblock);
1354 async_answer_0(callid, rc);
[532f53d]1355 goto exit;
[1196df6]1356 }
[b12ca16]1357 }
[38542dc]1358
[35f48f2]1359 flags = BLOCK_FLAGS_NOREAD;
[1196df6]1360 inode_ref->dirty = true;
[1c1c736]1361 }
[38542dc]1362
[06d85e5]1363 /* Load target block */
[5614c7f]1364 block_t *write_block;
[35f48f2]1365 rc = block_get(&write_block, service_id, fblock, flags);
[1c1c736]1366 if (rc != EOK) {
1367 async_answer_0(callid, rc);
[532f53d]1368 goto exit;
[1c1c736]1369 }
[38542dc]1370
[89dcf93]1371 if (flags == BLOCK_FLAGS_NOREAD)
[35f48f2]1372 memset(write_block->data, 0, block_size);
[532f53d]1373
[38542dc]1374 rc = async_data_write_finalize(callid, write_block->data +
1375 (pos % block_size), bytes);
[2f591127]1376 if (rc != EOK) {
1377 block_put(write_block);
[532f53d]1378 goto exit;
[2f591127]1379 }
[532f53d]1380
[89dcf93]1381 write_block->dirty = true;
1382
[1c1c736]1383 rc = block_put(write_block);
[532f53d]1384 if (rc != EOK)
1385 goto exit;
1386
[06d85e5]1387 /* Do some counting */
[38542dc]1388 uint32_t old_inode_size = ext4_inode_get_size(fs->superblock,
1389 inode_ref->inode);
[35f48f2]1390 if (pos + bytes > old_inode_size) {
1391 ext4_inode_set_size(inode_ref->inode, pos + bytes);
1392 inode_ref->dirty = true;
[1c1c736]1393 }
[532f53d]1394
[35f48f2]1395 *nsize = ext4_inode_get_size(fs->superblock, inode_ref->inode);
[1c1c736]1396 *wbytes = bytes;
[532f53d]1397
1398exit:
1399 ;
1400
[be39fc6]1401 int const rc2 = ext4_node_put(fn);
[532f53d]1402 return rc == EOK ? rc2 : rc;
[d3a9ae74]1403}
1404
[1fff583]1405/** Truncate file.
1406 *
1407 * Only the direction to shorter file is supported.
1408 *
[38542dc]1409 * @param service_id Device identifier
1410 * @param index Index if node to truncated
1411 * @param new_size New size of file
1412 *
1413 * @return Error code
1414 *
[1fff583]1415 */
[be39fc6]1416static int ext4_truncate(service_id_t service_id, fs_index_t index,
[38542dc]1417 aoff64_t new_size)
[d3a9ae74]1418{
[5614c7f]1419 fs_node_t *fn;
[be39fc6]1420 int rc = ext4_node_get(&fn, service_id, index);
[38542dc]1421 if (rc != EOK)
[d5a78e28]1422 return rc;
[38542dc]1423
[be39fc6]1424 ext4_node_t *enode = EXT4_NODE(fn);
[3d4fd2c]1425 ext4_inode_ref_t *inode_ref = enode->inode_ref;
[38542dc]1426
[1ac1ab4]1427 rc = ext4_filesystem_truncate_inode(inode_ref, new_size);
[be39fc6]1428 int const rc2 = ext4_node_put(fn);
[38542dc]1429
[532f53d]1430 return rc == EOK ? rc2 : rc;
[d3a9ae74]1431}
1432
[1fff583]1433/** Close file.
[81a7858]1434 *
[38542dc]1435 * @param service_id Device identifier
1436 * @param index I-node number
1437 *
1438 * @return Error code
1439 *
[1fff583]1440 */
[be39fc6]1441static int ext4_close(service_id_t service_id, fs_index_t index)
[d3a9ae74]1442{
1443 return EOK;
1444}
1445
[1fff583]1446/** Destroy node specified by index.
1447 *
[38542dc]1448 * @param service_id Device identifier
1449 * @param index I-node to destroy
1450 *
1451 * @return Error code
1452 *
[1fff583]1453 */
[be39fc6]1454static int ext4_destroy(service_id_t service_id, fs_index_t index)
[d3a9ae74]1455{
[5614c7f]1456 fs_node_t *fn;
[be39fc6]1457 int rc = ext4_node_get(&fn, service_id, index);
[38542dc]1458 if (rc != EOK)
[8be96a0]1459 return rc;
[38542dc]1460
[8be96a0]1461 /* Destroy the inode */
[be39fc6]1462 return ext4_destroy_node(fn);
[d3a9ae74]1463}
1464
[38542dc]1465/** Enforce inode synchronization (write) to device.
1466 *
1467 * @param service_id Device identifier
1468 * @param index I-node number.
[1fff583]1469 *
1470 */
[be39fc6]1471static int ext4_sync(service_id_t service_id, fs_index_t index)
[d3a9ae74]1472{
[5614c7f]1473 fs_node_t *fn;
[be39fc6]1474 int rc = ext4_node_get(&fn, service_id, index);
[38542dc]1475 if (rc != EOK)
[35f48f2]1476 return rc;
[38542dc]1477
[be39fc6]1478 ext4_node_t *enode = EXT4_NODE(fn);
[35f48f2]1479 enode->inode_ref->dirty = true;
[38542dc]1480
[be39fc6]1481 return ext4_node_put(fn);
[d3a9ae74]1482}
1483
[1fff583]1484/** VFS operations
1485 *
1486 */
[be39fc6]1487vfs_out_ops_t ext4_ops = {
1488 .fsprobe = ext4_fsprobe,
1489 .mounted = ext4_mounted,
1490 .unmounted = ext4_unmounted,
1491 .read = ext4_read,
1492 .write = ext4_write,
1493 .truncate = ext4_truncate,
1494 .close = ext4_close,
1495 .destroy = ext4_destroy,
1496 .sync = ext4_sync
[d3a9ae74]1497};
1498
1499/**
1500 * @}
[2b9e142]1501 */
Note: See TracBrowser for help on using the repository browser.