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