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