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