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