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