[d3a9ae74] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Frantisek Princ
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file ext4fs_ops.c
|
---|
| 35 | * @brief VFS operations for EXT4 filesystem.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <errno.h>
|
---|
[6c501f8] | 39 | #include <fibril_synch.h>
|
---|
| 40 | #include <libext4.h>
|
---|
[d3a9ae74] | 41 | #include <libfs.h>
|
---|
[9b9d37bb] | 42 | #include <macros.h>
|
---|
[6c501f8] | 43 | #include <malloc.h>
|
---|
[2b9e142] | 44 | #include <string.h>
|
---|
[3711e7e] | 45 | #include <adt/hash_table.h>
|
---|
[d3a9ae74] | 46 | #include <ipc/loc.h>
|
---|
| 47 | #include "ext4fs.h"
|
---|
| 48 | #include "../../vfs/vfs.h"
|
---|
| 49 |
|
---|
[6c501f8] | 50 | #define EXT4FS_NODE(node) ((node) ? (ext4fs_node_t *) (node)->data : NULL)
|
---|
| 51 |
|
---|
[3711e7e] | 52 | #define OPEN_NODES_KEYS 2
|
---|
| 53 | #define OPEN_NODES_DEV_HANDLE_KEY 0
|
---|
| 54 | #define OPEN_NODES_INODE_KEY 1
|
---|
| 55 | #define OPEN_NODES_BUCKETS 256
|
---|
| 56 |
|
---|
[6c501f8] | 57 | typedef struct ext4fs_instance {
|
---|
| 58 | link_t link;
|
---|
| 59 | service_id_t service_id;
|
---|
| 60 | ext4_filesystem_t *filesystem;
|
---|
| 61 | unsigned int open_nodes_count;
|
---|
| 62 | } ext4fs_instance_t;
|
---|
| 63 |
|
---|
| 64 | typedef struct ext4fs_node {
|
---|
| 65 | ext4fs_instance_t *instance;
|
---|
| 66 | ext4_inode_ref_t *inode_ref;
|
---|
| 67 | fs_node_t *fs_node;
|
---|
| 68 | link_t link;
|
---|
| 69 | unsigned int references;
|
---|
| 70 | } ext4fs_node_t;
|
---|
| 71 |
|
---|
| 72 | /*
|
---|
| 73 | * Forward declarations of auxiliary functions
|
---|
| 74 | */
|
---|
[9b9d37bb] | 75 |
|
---|
| 76 | static int ext4fs_read_directory(ipc_callid_t, aoff64_t, size_t,
|
---|
| 77 | ext4fs_instance_t *, ext4_inode_ref_t *, size_t *);
|
---|
| 78 | static int ext4fs_read_file(ipc_callid_t, aoff64_t, size_t, ext4fs_instance_t *,
|
---|
| 79 | ext4_inode_ref_t *, size_t *);
|
---|
| 80 | static bool ext4fs_is_dots(const uint8_t *, size_t);
|
---|
[6c501f8] | 81 | static int ext4fs_instance_get(service_id_t, ext4fs_instance_t **);
|
---|
| 82 | static int ext4fs_node_get_core(fs_node_t **, ext4fs_instance_t *, fs_index_t);
|
---|
[9c0c0e1] | 83 | static int ext4fs_node_put_core(ext4fs_node_t *);
|
---|
[d3a9ae74] | 84 |
|
---|
| 85 | /*
|
---|
| 86 | * Forward declarations of EXT4 libfs operations.
|
---|
| 87 | */
|
---|
| 88 | static int ext4fs_root_get(fs_node_t **, service_id_t);
|
---|
| 89 | static int ext4fs_match(fs_node_t **, fs_node_t *, const char *);
|
---|
| 90 | static int ext4fs_node_get(fs_node_t **, service_id_t, fs_index_t);
|
---|
| 91 | static int ext4fs_node_open(fs_node_t *);
|
---|
| 92 | static int ext4fs_node_put(fs_node_t *);
|
---|
| 93 | static int ext4fs_create_node(fs_node_t **, service_id_t, int);
|
---|
| 94 | static int ext4fs_destroy_node(fs_node_t *);
|
---|
| 95 | static int ext4fs_link(fs_node_t *, fs_node_t *, const char *);
|
---|
| 96 | static int ext4fs_unlink(fs_node_t *, fs_node_t *, const char *);
|
---|
| 97 | static int ext4fs_has_children(bool *, fs_node_t *);
|
---|
| 98 | static fs_index_t ext4fs_index_get(fs_node_t *);
|
---|
| 99 | static aoff64_t ext4fs_size_get(fs_node_t *);
|
---|
| 100 | static unsigned ext4fs_lnkcnt_get(fs_node_t *);
|
---|
| 101 | static bool ext4fs_is_directory(fs_node_t *);
|
---|
| 102 | static bool ext4fs_is_file(fs_node_t *node);
|
---|
| 103 | static service_id_t ext4fs_service_get(fs_node_t *node);
|
---|
| 104 |
|
---|
[6c501f8] | 105 | /*
|
---|
| 106 | * Static variables
|
---|
| 107 | */
|
---|
| 108 | static LIST_INITIALIZE(instance_list);
|
---|
| 109 | static FIBRIL_MUTEX_INITIALIZE(instance_list_mutex);
|
---|
[3711e7e] | 110 | static hash_table_t open_nodes;
|
---|
[6c501f8] | 111 | static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
|
---|
| 112 |
|
---|
[3711e7e] | 113 | /* Hash table interface for open nodes hash table */
|
---|
| 114 | static hash_index_t open_nodes_hash(unsigned long key[])
|
---|
| 115 | {
|
---|
| 116 | /* TODO: This is very simple and probably can be improved */
|
---|
| 117 | return key[OPEN_NODES_INODE_KEY] % OPEN_NODES_BUCKETS;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | static int open_nodes_compare(unsigned long key[], hash_count_t keys,
|
---|
| 121 | link_t *item)
|
---|
| 122 | {
|
---|
| 123 | ext4fs_node_t *enode = hash_table_get_instance(item, ext4fs_node_t, link);
|
---|
| 124 | assert(keys > 0);
|
---|
| 125 | if (enode->instance->service_id !=
|
---|
| 126 | ((service_id_t) key[OPEN_NODES_DEV_HANDLE_KEY])) {
|
---|
| 127 | return false;
|
---|
| 128 | }
|
---|
| 129 | if (keys == 1) {
|
---|
| 130 | return true;
|
---|
| 131 | }
|
---|
| 132 | assert(keys == 2);
|
---|
| 133 | return (enode->inode_ref->index == key[OPEN_NODES_INODE_KEY]);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | static void open_nodes_remove_cb(link_t *link)
|
---|
| 137 | {
|
---|
| 138 | /* We don't use remove callback for this hash table */
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | static hash_table_operations_t open_nodes_ops = {
|
---|
| 142 | .hash = open_nodes_hash,
|
---|
| 143 | .compare = open_nodes_compare,
|
---|
| 144 | .remove_callback = open_nodes_remove_cb,
|
---|
| 145 | };
|
---|
| 146 |
|
---|
[6c501f8] | 147 |
|
---|
[d3a9ae74] | 148 | int ext4fs_global_init(void)
|
---|
| 149 | {
|
---|
[3711e7e] | 150 | if (!hash_table_create(&open_nodes, OPEN_NODES_BUCKETS,
|
---|
| 151 | OPEN_NODES_KEYS, &open_nodes_ops)) {
|
---|
| 152 | return ENOMEM;
|
---|
| 153 | }
|
---|
[d3a9ae74] | 154 | return EOK;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[3711e7e] | 157 |
|
---|
[d3a9ae74] | 158 | int ext4fs_global_fini(void)
|
---|
| 159 | {
|
---|
[3711e7e] | 160 | hash_table_destroy(&open_nodes);
|
---|
[d3a9ae74] | 161 | return EOK;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | /*
|
---|
| 166 | * EXT4 libfs operations.
|
---|
| 167 | */
|
---|
| 168 |
|
---|
[6c501f8] | 169 | int ext4fs_instance_get(service_id_t service_id, ext4fs_instance_t **inst)
|
---|
| 170 | {
|
---|
| 171 | ext4fs_instance_t *tmp;
|
---|
| 172 |
|
---|
| 173 | fibril_mutex_lock(&instance_list_mutex);
|
---|
| 174 |
|
---|
| 175 | if (list_empty(&instance_list)) {
|
---|
| 176 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
| 177 | return EINVAL;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | list_foreach(instance_list, link) {
|
---|
| 181 | tmp = list_get_instance(link, ext4fs_instance_t, link);
|
---|
| 182 |
|
---|
| 183 | if (tmp->service_id == service_id) {
|
---|
| 184 | *inst = tmp;
|
---|
| 185 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
| 186 | return EOK;
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
| 191 | return EINVAL;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[3711e7e] | 194 |
|
---|
[d3a9ae74] | 195 | int ext4fs_root_get(fs_node_t **rfn, service_id_t service_id)
|
---|
| 196 | {
|
---|
[01ab41b] | 197 | return ext4fs_node_get(rfn, service_id, EXT4_INODE_ROOT_INDEX);
|
---|
[d3a9ae74] | 198 | }
|
---|
| 199 |
|
---|
[3711e7e] | 200 |
|
---|
[d3a9ae74] | 201 | int ext4fs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
| 202 | {
|
---|
[9b9d37bb] | 203 | ext4fs_node_t *eparent = EXT4FS_NODE(pfn);
|
---|
| 204 | ext4_filesystem_t *fs;
|
---|
| 205 | ext4_directory_iterator_t it;
|
---|
| 206 | int rc;
|
---|
| 207 | size_t name_size;
|
---|
| 208 | size_t component_size;
|
---|
| 209 | bool found = false;
|
---|
| 210 | uint32_t inode;
|
---|
| 211 |
|
---|
| 212 | fs = eparent->instance->filesystem;
|
---|
| 213 |
|
---|
| 214 | if (!ext4_inode_is_type(fs->superblock, eparent->inode_ref->inode,
|
---|
| 215 | EXT4_INODE_MODE_DIRECTORY)) {
|
---|
| 216 | return ENOTDIR;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[2b9e142] | 219 | component_size = strlen(component);
|
---|
[8158db7] | 220 |
|
---|
[c25e39b] | 221 | if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_COMPAT_DIR_INDEX) &&
|
---|
| 222 | ext4_inode_has_flag(eparent->inode_ref->inode, EXT4_INODE_FLAG_INDEX)) {
|
---|
[7bc4508] | 223 |
|
---|
[8158db7] | 224 | rc = ext4_directory_dx_find_entry(&it, fs, eparent->inode_ref, component_size, component);
|
---|
[7bc4508] | 225 |
|
---|
| 226 | // Index isn't corrupted
|
---|
| 227 | if (rc != EXT4_ERR_BAD_DX_DIR) {
|
---|
| 228 |
|
---|
| 229 | if (rc != EOK) {
|
---|
| 230 | return rc;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | inode = ext4_directory_entry_ll_get_inode(it.current);
|
---|
[36d2c6f] | 234 |
|
---|
| 235 | rc = ext4fs_node_get_core(rfn, eparent->instance, inode);
|
---|
| 236 | ext4_directory_iterator_fini(&it);
|
---|
| 237 | return rc;
|
---|
[7bc4508] | 238 | }
|
---|
| 239 |
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[9b9d37bb] | 242 | rc = ext4_directory_iterator_init(&it, fs, eparent->inode_ref, 0);
|
---|
| 243 | if (rc != EOK) {
|
---|
| 244 | return rc;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | while (it.current != NULL) {
|
---|
| 248 | inode = ext4_directory_entry_ll_get_inode(it.current);
|
---|
| 249 |
|
---|
| 250 | /* ignore empty directory entries */
|
---|
| 251 | if (inode != 0) {
|
---|
| 252 | name_size = ext4_directory_entry_ll_get_name_length(fs->superblock,
|
---|
| 253 | it.current);
|
---|
| 254 |
|
---|
| 255 | if (name_size == component_size && bcmp(component, &it.current->name,
|
---|
| 256 | name_size) == 0) {
|
---|
| 257 | rc = ext4fs_node_get_core(rfn, eparent->instance,
|
---|
| 258 | inode);
|
---|
| 259 | if (rc != EOK) {
|
---|
| 260 | ext4_directory_iterator_fini(&it);
|
---|
| 261 | return rc;
|
---|
| 262 | }
|
---|
| 263 | found = true;
|
---|
| 264 | break;
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | rc = ext4_directory_iterator_next(&it);
|
---|
| 269 | if (rc != EOK) {
|
---|
| 270 | ext4_directory_iterator_fini(&it);
|
---|
| 271 | return rc;
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | ext4_directory_iterator_fini(&it);
|
---|
| 276 |
|
---|
| 277 | if (!found) {
|
---|
| 278 | return ENOENT;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[d3a9ae74] | 281 | return EOK;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[3711e7e] | 284 |
|
---|
[d3a9ae74] | 285 | int ext4fs_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
|
---|
| 286 | {
|
---|
[9c0c0e1] | 287 | ext4fs_instance_t *inst = NULL;
|
---|
| 288 | int rc;
|
---|
| 289 |
|
---|
| 290 | rc = ext4fs_instance_get(service_id, &inst);
|
---|
| 291 | if (rc != EOK) {
|
---|
| 292 | return rc;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | return ext4fs_node_get_core(rfn, inst, index);
|
---|
[d3a9ae74] | 296 | }
|
---|
| 297 |
|
---|
[3711e7e] | 298 |
|
---|
[6c501f8] | 299 | int ext4fs_node_get_core(fs_node_t **rfn, ext4fs_instance_t *inst,
|
---|
| 300 | fs_index_t index)
|
---|
| 301 | {
|
---|
[3711e7e] | 302 | int rc;
|
---|
| 303 | fs_node_t *node = NULL;
|
---|
| 304 | ext4fs_node_t *enode = NULL;
|
---|
| 305 |
|
---|
| 306 | ext4_inode_ref_t *inode_ref = NULL;
|
---|
| 307 |
|
---|
| 308 | fibril_mutex_lock(&open_nodes_lock);
|
---|
| 309 |
|
---|
| 310 | /* Check if the node is not already open */
|
---|
| 311 | unsigned long key[] = {
|
---|
| 312 | [OPEN_NODES_DEV_HANDLE_KEY] = inst->service_id,
|
---|
| 313 | [OPEN_NODES_INODE_KEY] = index,
|
---|
| 314 | };
|
---|
| 315 | link_t *already_open = hash_table_find(&open_nodes, key);
|
---|
| 316 |
|
---|
| 317 | if (already_open) {
|
---|
| 318 | enode = hash_table_get_instance(already_open, ext4fs_node_t, link);
|
---|
| 319 | *rfn = enode->fs_node;
|
---|
| 320 | enode->references++;
|
---|
| 321 |
|
---|
| 322 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 323 | return EOK;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | enode = malloc(sizeof(ext4fs_node_t));
|
---|
| 327 | if (enode == NULL) {
|
---|
| 328 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 329 | return ENOMEM;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | node = malloc(sizeof(fs_node_t));
|
---|
| 333 | if (node == NULL) {
|
---|
| 334 | free(enode);
|
---|
| 335 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 336 | return ENOMEM;
|
---|
| 337 | }
|
---|
| 338 | fs_node_initialize(node);
|
---|
| 339 |
|
---|
| 340 | rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
|
---|
| 341 | if (rc != EOK) {
|
---|
| 342 | free(enode);
|
---|
| 343 | free(node);
|
---|
| 344 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 345 | return rc;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | enode->inode_ref = inode_ref;
|
---|
| 349 | enode->instance = inst;
|
---|
| 350 | enode->references = 1;
|
---|
| 351 | enode->fs_node = node;
|
---|
| 352 | link_initialize(&enode->link);
|
---|
| 353 |
|
---|
| 354 | node->data = enode;
|
---|
| 355 | *rfn = node;
|
---|
| 356 |
|
---|
| 357 | hash_table_insert(&open_nodes, key, &enode->link);
|
---|
| 358 | inst->open_nodes_count++;
|
---|
| 359 |
|
---|
| 360 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 361 |
|
---|
[6c501f8] | 362 | return EOK;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[3711e7e] | 365 |
|
---|
[9b9d37bb] | 366 | int ext4fs_node_put_core(ext4fs_node_t *enode)
|
---|
| 367 | {
|
---|
| 368 | int rc;
|
---|
| 369 | unsigned long key[] = {
|
---|
| 370 | [OPEN_NODES_DEV_HANDLE_KEY] = enode->instance->service_id,
|
---|
| 371 | [OPEN_NODES_INODE_KEY] = enode->inode_ref->index,
|
---|
| 372 | };
|
---|
| 373 |
|
---|
| 374 | hash_table_remove(&open_nodes, key, OPEN_NODES_KEYS);
|
---|
| 375 | assert(enode->instance->open_nodes_count > 0);
|
---|
| 376 | enode->instance->open_nodes_count--;
|
---|
| 377 |
|
---|
| 378 | rc = ext4_filesystem_put_inode_ref(enode->inode_ref);
|
---|
| 379 | if (rc != EOK) {
|
---|
| 380 | return rc;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | free(enode->fs_node);
|
---|
| 384 | free(enode);
|
---|
| 385 |
|
---|
[9c0c0e1] | 386 | return EOK;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[3711e7e] | 389 |
|
---|
[d3a9ae74] | 390 | int ext4fs_node_open(fs_node_t *fn)
|
---|
| 391 | {
|
---|
[2b9e142] | 392 | // Stateless operation
|
---|
[d3a9ae74] | 393 | return EOK;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | int ext4fs_node_put(fs_node_t *fn)
|
---|
| 397 | {
|
---|
[9c0c0e1] | 398 | int rc;
|
---|
| 399 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
| 400 |
|
---|
| 401 | fibril_mutex_lock(&open_nodes_lock);
|
---|
| 402 |
|
---|
| 403 | assert(enode->references > 0);
|
---|
| 404 | enode->references--;
|
---|
| 405 | if (enode->references == 0) {
|
---|
| 406 | rc = ext4fs_node_put_core(enode);
|
---|
| 407 | if (rc != EOK) {
|
---|
| 408 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 409 | return rc;
|
---|
| 410 | }
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 414 |
|
---|
[d3a9ae74] | 415 | return EOK;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
[3711e7e] | 418 |
|
---|
[d3a9ae74] | 419 | int ext4fs_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
|
---|
| 420 | {
|
---|
[e68c834] | 421 | EXT4FS_DBG("not supported");
|
---|
[9b9d37bb] | 422 |
|
---|
[d3a9ae74] | 423 | // TODO
|
---|
| 424 | return ENOTSUP;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
[3711e7e] | 427 |
|
---|
[d3a9ae74] | 428 | int ext4fs_destroy_node(fs_node_t *fn)
|
---|
| 429 | {
|
---|
[e68c834] | 430 | EXT4FS_DBG("not supported");
|
---|
[9b9d37bb] | 431 |
|
---|
[d3a9ae74] | 432 | // TODO
|
---|
| 433 | return ENOTSUP;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
[3711e7e] | 436 |
|
---|
[d3a9ae74] | 437 | int ext4fs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
---|
| 438 | {
|
---|
[e68c834] | 439 | EXT4FS_DBG("not supported");
|
---|
[9b9d37bb] | 440 |
|
---|
[d3a9ae74] | 441 | // TODO
|
---|
| 442 | return ENOTSUP;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[3711e7e] | 445 |
|
---|
[ebeaaa06] | 446 | int ext4fs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
---|
[d3a9ae74] | 447 | {
|
---|
[9b9d37bb] | 448 |
|
---|
[d3a9ae74] | 449 | return ENOTSUP;
|
---|
[ebeaaa06] | 450 |
|
---|
| 451 | int rc;
|
---|
| 452 |
|
---|
| 453 | bool has_children;
|
---|
| 454 | rc = ext4fs_has_children(&has_children, cfn);
|
---|
| 455 | if (rc != EOK) {
|
---|
| 456 | return rc;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | // Cannot unlink non-empty node
|
---|
| 460 | if (has_children) {
|
---|
| 461 | return ENOTEMPTY;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | // Remove entry from parent directory
|
---|
| 465 | // TODO
|
---|
| 466 | // rc = ext4_directory_remove_entry(EXT4FS_NODE(pfn), name);
|
---|
| 467 | // if (rc != EOK) {
|
---|
| 468 | // return rc;
|
---|
| 469 | // }
|
---|
| 470 |
|
---|
| 471 | // Decrement links count
|
---|
| 472 | ext4_inode_ref_t * child_inode_ref = EXT4FS_NODE(cfn)->inode_ref;
|
---|
| 473 |
|
---|
| 474 | uint32_t lnk_count = ext4_inode_get_links_count(child_inode_ref->inode);
|
---|
| 475 | lnk_count--;
|
---|
| 476 | ext4_inode_set_links_count(child_inode_ref->inode, lnk_count);
|
---|
| 477 |
|
---|
| 478 | child_inode_ref->dirty = true;
|
---|
| 479 |
|
---|
| 480 | // If directory - handle links from parent
|
---|
| 481 | if (lnk_count <= 1 && ext4fs_is_directory(cfn)) {
|
---|
| 482 |
|
---|
| 483 | ext4_inode_ref_t *parent_inode_ref = EXT4FS_NODE(pfn)->inode_ref;
|
---|
| 484 | uint32_t parent_lnk_count = ext4_inode_get_links_count(
|
---|
| 485 | parent_inode_ref->inode);
|
---|
| 486 | parent_lnk_count--;
|
---|
| 487 | ext4_inode_set_links_count(parent_inode_ref->inode, parent_lnk_count);
|
---|
| 488 |
|
---|
| 489 | parent_inode_ref->dirty = true;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 |
|
---|
| 493 | return EOK;
|
---|
[d3a9ae74] | 494 | }
|
---|
| 495 |
|
---|
[3711e7e] | 496 |
|
---|
[d3a9ae74] | 497 | int ext4fs_has_children(bool *has_children, fs_node_t *fn)
|
---|
| 498 | {
|
---|
[e68c834] | 499 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
| 500 | ext4_directory_iterator_t it;
|
---|
| 501 | ext4_filesystem_t *fs;
|
---|
| 502 | int rc;
|
---|
| 503 | bool found = false;
|
---|
| 504 | size_t name_size;
|
---|
| 505 |
|
---|
| 506 | fs = enode->instance->filesystem;
|
---|
| 507 |
|
---|
| 508 | if (!ext4_inode_is_type(fs->superblock, enode->inode_ref->inode,
|
---|
| 509 | EXT4_INODE_MODE_DIRECTORY)) {
|
---|
| 510 | *has_children = false;
|
---|
| 511 | return EOK;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | rc = ext4_directory_iterator_init(&it, fs, enode->inode_ref, 0);
|
---|
| 515 | if (rc != EOK) {
|
---|
| 516 | return rc;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | /* Find a non-empty directory entry */
|
---|
| 520 | while (it.current != NULL) {
|
---|
| 521 | if (it.current->inode != 0) {
|
---|
| 522 | name_size = ext4_directory_entry_ll_get_name_length(fs->superblock,
|
---|
| 523 | it.current);
|
---|
[2ea6392] | 524 | if (!ext4fs_is_dots(it.current->name, name_size)) {
|
---|
[e68c834] | 525 | found = true;
|
---|
| 526 | break;
|
---|
| 527 | }
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | rc = ext4_directory_iterator_next(&it);
|
---|
| 531 | if (rc != EOK) {
|
---|
| 532 | ext4_directory_iterator_fini(&it);
|
---|
| 533 | return rc;
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | rc = ext4_directory_iterator_fini(&it);
|
---|
| 538 | if (rc != EOK) {
|
---|
| 539 | return rc;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | *has_children = found;
|
---|
[9b9d37bb] | 543 |
|
---|
[d3a9ae74] | 544 | return EOK;
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 |
|
---|
| 548 | fs_index_t ext4fs_index_get(fs_node_t *fn)
|
---|
| 549 | {
|
---|
[9b9d37bb] | 550 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
| 551 | return enode->inode_ref->index;
|
---|
[d3a9ae74] | 552 | }
|
---|
| 553 |
|
---|
[3711e7e] | 554 |
|
---|
[d3a9ae74] | 555 | aoff64_t ext4fs_size_get(fs_node_t *fn)
|
---|
| 556 | {
|
---|
[9b9d37bb] | 557 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
| 558 | aoff64_t size = ext4_inode_get_size(enode->instance->filesystem->superblock,
|
---|
| 559 | enode->inode_ref->inode);
|
---|
| 560 | return size;
|
---|
[d3a9ae74] | 561 | }
|
---|
| 562 |
|
---|
[3711e7e] | 563 |
|
---|
[d3a9ae74] | 564 | unsigned ext4fs_lnkcnt_get(fs_node_t *fn)
|
---|
| 565 | {
|
---|
[9b9d37bb] | 566 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
| 567 | unsigned count = ext4_inode_get_links_count(enode->inode_ref->inode);
|
---|
| 568 | return count;
|
---|
[d3a9ae74] | 569 | }
|
---|
| 570 |
|
---|
[3711e7e] | 571 |
|
---|
[d3a9ae74] | 572 | bool ext4fs_is_directory(fs_node_t *fn)
|
---|
| 573 | {
|
---|
[e68c834] | 574 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
| 575 | bool is_dir = ext4_inode_is_type(enode->instance->filesystem->superblock,
|
---|
| 576 | enode->inode_ref->inode, EXT4_INODE_MODE_DIRECTORY);
|
---|
| 577 | return is_dir;
|
---|
[d3a9ae74] | 578 | }
|
---|
| 579 |
|
---|
[3711e7e] | 580 |
|
---|
[d3a9ae74] | 581 | bool ext4fs_is_file(fs_node_t *fn)
|
---|
| 582 | {
|
---|
[9b9d37bb] | 583 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
| 584 | bool is_file = ext4_inode_is_type(enode->instance->filesystem->superblock,
|
---|
| 585 | enode->inode_ref->inode, EXT4_INODE_MODE_FILE);
|
---|
| 586 | return is_file;
|
---|
[d3a9ae74] | 587 | }
|
---|
| 588 |
|
---|
[3711e7e] | 589 |
|
---|
[d3a9ae74] | 590 | service_id_t ext4fs_service_get(fs_node_t *fn)
|
---|
| 591 | {
|
---|
[e68c834] | 592 | ext4fs_node_t *enode = EXT4FS_NODE(fn);
|
---|
| 593 | return enode->instance->service_id;
|
---|
[d3a9ae74] | 594 | }
|
---|
| 595 |
|
---|
| 596 | /*
|
---|
| 597 | * libfs operations.
|
---|
| 598 | */
|
---|
| 599 | libfs_ops_t ext4fs_libfs_ops = {
|
---|
| 600 | .root_get = ext4fs_root_get,
|
---|
| 601 | .match = ext4fs_match,
|
---|
| 602 | .node_get = ext4fs_node_get,
|
---|
| 603 | .node_open = ext4fs_node_open,
|
---|
| 604 | .node_put = ext4fs_node_put,
|
---|
| 605 | .create = ext4fs_create_node,
|
---|
| 606 | .destroy = ext4fs_destroy_node,
|
---|
| 607 | .link = ext4fs_link,
|
---|
| 608 | .unlink = ext4fs_unlink,
|
---|
| 609 | .has_children = ext4fs_has_children,
|
---|
| 610 | .index_get = ext4fs_index_get,
|
---|
| 611 | .size_get = ext4fs_size_get,
|
---|
| 612 | .lnkcnt_get = ext4fs_lnkcnt_get,
|
---|
| 613 | .is_directory = ext4fs_is_directory,
|
---|
| 614 | .is_file = ext4fs_is_file,
|
---|
| 615 | .service_get = ext4fs_service_get
|
---|
| 616 | };
|
---|
| 617 |
|
---|
| 618 |
|
---|
| 619 | /*
|
---|
| 620 | * VFS operations.
|
---|
| 621 | */
|
---|
| 622 |
|
---|
| 623 | static int ext4fs_mounted(service_id_t service_id, const char *opts,
|
---|
| 624 | fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
|
---|
| 625 | {
|
---|
[6c501f8] | 626 | int rc;
|
---|
| 627 | ext4_filesystem_t *fs;
|
---|
| 628 | ext4fs_instance_t *inst;
|
---|
| 629 | bool read_only;
|
---|
| 630 |
|
---|
| 631 | /* Allocate libext4 filesystem structure */
|
---|
| 632 | fs = (ext4_filesystem_t *) malloc(sizeof(ext4_filesystem_t));
|
---|
| 633 | if (fs == NULL) {
|
---|
| 634 | return ENOMEM;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | /* Allocate instance structure */
|
---|
| 638 | inst = (ext4fs_instance_t *) malloc(sizeof(ext4fs_instance_t));
|
---|
| 639 | if (inst == NULL) {
|
---|
| 640 | free(fs);
|
---|
| 641 | return ENOMEM;
|
---|
| 642 | }
|
---|
| 643 |
|
---|
[9c0c0e1] | 644 | /* Initialize the filesystem */
|
---|
[6c501f8] | 645 | rc = ext4_filesystem_init(fs, service_id);
|
---|
| 646 | if (rc != EOK) {
|
---|
| 647 | free(fs);
|
---|
| 648 | free(inst);
|
---|
| 649 | return rc;
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 | /* Do some sanity checking */
|
---|
| 653 | rc = ext4_filesystem_check_sanity(fs);
|
---|
| 654 | if (rc != EOK) {
|
---|
[ae3d4f8] | 655 | ext4_filesystem_fini(fs, false);
|
---|
[6c501f8] | 656 | free(fs);
|
---|
| 657 | free(inst);
|
---|
| 658 | return rc;
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | /* Check flags */
|
---|
[9c0c0e1] | 662 | rc = ext4_filesystem_check_features(fs, &read_only);
|
---|
[6c501f8] | 663 | if (rc != EOK) {
|
---|
[ae3d4f8] | 664 | ext4_filesystem_fini(fs, false);
|
---|
[6c501f8] | 665 | free(fs);
|
---|
| 666 | free(inst);
|
---|
| 667 | return rc;
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | /* Initialize instance */
|
---|
| 671 | link_initialize(&inst->link);
|
---|
| 672 | inst->service_id = service_id;
|
---|
| 673 | inst->filesystem = fs;
|
---|
| 674 | inst->open_nodes_count = 0;
|
---|
| 675 |
|
---|
| 676 | /* Read root node */
|
---|
| 677 | fs_node_t *root_node;
|
---|
[3711e7e] | 678 | rc = ext4fs_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
|
---|
[6c501f8] | 679 | if (rc != EOK) {
|
---|
[ae3d4f8] | 680 | ext4_filesystem_fini(fs, false);
|
---|
[6c501f8] | 681 | free(fs);
|
---|
| 682 | free(inst);
|
---|
| 683 | return rc;
|
---|
| 684 | }
|
---|
| 685 | ext4fs_node_t *enode = EXT4FS_NODE(root_node);
|
---|
| 686 |
|
---|
| 687 | /* Add instance to the list */
|
---|
| 688 | fibril_mutex_lock(&instance_list_mutex);
|
---|
| 689 | list_append(&inst->link, &instance_list);
|
---|
| 690 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
| 691 |
|
---|
| 692 | *index = EXT4_INODE_ROOT_INDEX;
|
---|
| 693 | *size = 0;
|
---|
[3712434] | 694 | *lnkcnt = ext4_inode_get_links_count(enode->inode_ref->inode);
|
---|
[6c501f8] | 695 |
|
---|
| 696 | ext4fs_node_put(root_node);
|
---|
| 697 |
|
---|
[d3a9ae74] | 698 | return EOK;
|
---|
| 699 | }
|
---|
| 700 |
|
---|
[3711e7e] | 701 |
|
---|
[d3a9ae74] | 702 | static int ext4fs_unmounted(service_id_t service_id)
|
---|
| 703 | {
|
---|
[6c501f8] | 704 | int rc;
|
---|
| 705 | ext4fs_instance_t *inst;
|
---|
| 706 |
|
---|
| 707 | rc = ext4fs_instance_get(service_id, &inst);
|
---|
| 708 |
|
---|
| 709 | if (rc != EOK) {
|
---|
| 710 | return rc;
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | fibril_mutex_lock(&open_nodes_lock);
|
---|
| 714 |
|
---|
| 715 | if (inst->open_nodes_count != 0) {
|
---|
| 716 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 717 | return EBUSY;
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 | /* Remove the instance from the list */
|
---|
| 721 | fibril_mutex_lock(&instance_list_mutex);
|
---|
| 722 | list_remove(&inst->link);
|
---|
| 723 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
| 724 |
|
---|
| 725 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 726 |
|
---|
[ae3d4f8] | 727 | return ext4_filesystem_fini(inst->filesystem, true);
|
---|
[d3a9ae74] | 728 | }
|
---|
| 729 |
|
---|
[3711e7e] | 730 |
|
---|
[d3a9ae74] | 731 | static int
|
---|
| 732 | ext4fs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
|
---|
| 733 | size_t *rbytes)
|
---|
| 734 | {
|
---|
[9b9d37bb] | 735 | ext4fs_instance_t *inst;
|
---|
| 736 | ext4_inode_ref_t *inode_ref;
|
---|
| 737 | int rc;
|
---|
| 738 |
|
---|
| 739 | /*
|
---|
| 740 | * Receive the read request.
|
---|
| 741 | */
|
---|
| 742 | ipc_callid_t callid;
|
---|
| 743 | size_t size;
|
---|
| 744 | if (!async_data_read_receive(&callid, &size)) {
|
---|
| 745 | async_answer_0(callid, EINVAL);
|
---|
| 746 | return EINVAL;
|
---|
| 747 | }
|
---|
| 748 |
|
---|
| 749 | rc = ext4fs_instance_get(service_id, &inst);
|
---|
| 750 | if (rc != EOK) {
|
---|
| 751 | async_answer_0(callid, rc);
|
---|
| 752 | return rc;
|
---|
| 753 | }
|
---|
| 754 |
|
---|
| 755 | rc = ext4_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
|
---|
| 756 | if (rc != EOK) {
|
---|
| 757 | async_answer_0(callid, rc);
|
---|
| 758 | return rc;
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | if (ext4_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
|
---|
| 762 | EXT4_INODE_MODE_FILE)) {
|
---|
| 763 | rc = ext4fs_read_file(callid, pos, size, inst, inode_ref,
|
---|
| 764 | rbytes);
|
---|
| 765 | } else if (ext4_inode_is_type(inst->filesystem->superblock,
|
---|
| 766 | inode_ref->inode, EXT4_INODE_MODE_DIRECTORY)) {
|
---|
| 767 | rc = ext4fs_read_directory(callid, pos, size, inst, inode_ref,
|
---|
| 768 | rbytes);
|
---|
| 769 | } else {
|
---|
| 770 | /* Other inode types not supported */
|
---|
| 771 | async_answer_0(callid, ENOTSUP);
|
---|
| 772 | rc = ENOTSUP;
|
---|
| 773 | }
|
---|
| 774 |
|
---|
| 775 | ext4_filesystem_put_inode_ref(inode_ref);
|
---|
[8958a26] | 776 |
|
---|
[9b9d37bb] | 777 | return rc;
|
---|
| 778 | }
|
---|
| 779 |
|
---|
[8958a26] | 780 | bool ext4fs_is_dots(const uint8_t *name, size_t name_size)
|
---|
| 781 | {
|
---|
[9b9d37bb] | 782 | if (name_size == 1 && name[0] == '.') {
|
---|
| 783 | return true;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | if (name_size == 2 && name[0] == '.' && name[1] == '.') {
|
---|
| 787 | return true;
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 | return false;
|
---|
| 791 | }
|
---|
| 792 |
|
---|
| 793 | int ext4fs_read_directory(ipc_callid_t callid, aoff64_t pos, size_t size,
|
---|
| 794 | ext4fs_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
|
---|
| 795 | {
|
---|
| 796 |
|
---|
| 797 | ext4_directory_iterator_t it;
|
---|
| 798 | aoff64_t next;
|
---|
| 799 | uint8_t *buf;
|
---|
| 800 | size_t name_size;
|
---|
| 801 | int rc;
|
---|
| 802 | bool found = false;
|
---|
| 803 |
|
---|
| 804 | rc = ext4_directory_iterator_init(&it, inst->filesystem, inode_ref, pos);
|
---|
| 805 | if (rc != EOK) {
|
---|
| 806 | async_answer_0(callid, rc);
|
---|
| 807 | return rc;
|
---|
| 808 | }
|
---|
| 809 |
|
---|
| 810 | /* Find next interesting directory entry.
|
---|
| 811 | * We want to skip . and .. entries
|
---|
| 812 | * as these are not used in HelenOS
|
---|
| 813 | */
|
---|
| 814 | while (it.current != NULL) {
|
---|
[e68c834] | 815 |
|
---|
[9b9d37bb] | 816 | if (it.current->inode == 0) {
|
---|
| 817 | goto skip;
|
---|
| 818 | }
|
---|
| 819 |
|
---|
| 820 | name_size = ext4_directory_entry_ll_get_name_length(
|
---|
| 821 | inst->filesystem->superblock, it.current);
|
---|
| 822 |
|
---|
| 823 | /* skip . and .. */
|
---|
[2ea6392] | 824 | if (ext4fs_is_dots(it.current->name, name_size)) {
|
---|
[9b9d37bb] | 825 | goto skip;
|
---|
| 826 | }
|
---|
| 827 |
|
---|
| 828 | /* The on-disk entry does not contain \0 at the end
|
---|
| 829 | * end of entry name, so we copy it to new buffer
|
---|
| 830 | * and add the \0 at the end
|
---|
| 831 | */
|
---|
| 832 | buf = malloc(name_size+1);
|
---|
| 833 | if (buf == NULL) {
|
---|
| 834 | ext4_directory_iterator_fini(&it);
|
---|
| 835 | async_answer_0(callid, ENOMEM);
|
---|
| 836 | return ENOMEM;
|
---|
| 837 | }
|
---|
| 838 | memcpy(buf, &it.current->name, name_size);
|
---|
| 839 | *(buf + name_size) = 0;
|
---|
| 840 | found = true;
|
---|
| 841 | (void) async_data_read_finalize(callid, buf, name_size + 1);
|
---|
| 842 | free(buf);
|
---|
| 843 | break;
|
---|
| 844 |
|
---|
| 845 | skip:
|
---|
| 846 | rc = ext4_directory_iterator_next(&it);
|
---|
| 847 | if (rc != EOK) {
|
---|
| 848 | ext4_directory_iterator_fini(&it);
|
---|
| 849 | async_answer_0(callid, rc);
|
---|
| 850 | return rc;
|
---|
| 851 | }
|
---|
| 852 | }
|
---|
| 853 |
|
---|
| 854 | if (found) {
|
---|
| 855 | rc = ext4_directory_iterator_next(&it);
|
---|
[8958a26] | 856 | if (rc != EOK) {
|
---|
[9b9d37bb] | 857 | return rc;
|
---|
[8958a26] | 858 | }
|
---|
[9b9d37bb] | 859 | next = it.current_offset;
|
---|
| 860 | }
|
---|
| 861 |
|
---|
| 862 | rc = ext4_directory_iterator_fini(&it);
|
---|
[8958a26] | 863 | if (rc != EOK) {
|
---|
[9b9d37bb] | 864 | return rc;
|
---|
[8958a26] | 865 | }
|
---|
[9b9d37bb] | 866 |
|
---|
| 867 | if (found) {
|
---|
| 868 | *rbytes = next - pos;
|
---|
| 869 | return EOK;
|
---|
| 870 | } else {
|
---|
| 871 | async_answer_0(callid, ENOENT);
|
---|
| 872 | return ENOENT;
|
---|
| 873 | }
|
---|
[d3a9ae74] | 874 | }
|
---|
| 875 |
|
---|
[9b9d37bb] | 876 | int ext4fs_read_file(ipc_callid_t callid, aoff64_t pos, size_t size,
|
---|
| 877 | ext4fs_instance_t *inst, ext4_inode_ref_t *inode_ref, size_t *rbytes)
|
---|
| 878 | {
|
---|
| 879 | int rc;
|
---|
| 880 | uint32_t block_size;
|
---|
| 881 | aoff64_t file_block;
|
---|
| 882 | uint64_t file_size;
|
---|
| 883 | uint32_t fs_block;
|
---|
| 884 | size_t offset_in_block;
|
---|
| 885 | size_t bytes;
|
---|
| 886 | block_t *block;
|
---|
| 887 | uint8_t *buffer;
|
---|
| 888 |
|
---|
| 889 | file_size = ext4_inode_get_size(inst->filesystem->superblock,
|
---|
| 890 | inode_ref->inode);
|
---|
| 891 |
|
---|
| 892 | if (pos >= file_size) {
|
---|
| 893 | /* Read 0 bytes successfully */
|
---|
| 894 | async_data_read_finalize(callid, NULL, 0);
|
---|
| 895 | *rbytes = 0;
|
---|
| 896 | return EOK;
|
---|
| 897 | }
|
---|
| 898 |
|
---|
| 899 | /* For now, we only read data from one block at a time */
|
---|
| 900 | block_size = ext4_superblock_get_block_size(inst->filesystem->superblock);
|
---|
| 901 | file_block = pos / block_size;
|
---|
| 902 | offset_in_block = pos % block_size;
|
---|
| 903 | bytes = min(block_size - offset_in_block, size);
|
---|
| 904 |
|
---|
| 905 | /* Handle end of file */
|
---|
| 906 | if (pos + bytes > file_size) {
|
---|
| 907 | bytes = file_size - pos;
|
---|
| 908 | }
|
---|
| 909 |
|
---|
| 910 | /* Get the real block number */
|
---|
| 911 | rc = ext4_filesystem_get_inode_data_block_index(inst->filesystem,
|
---|
| 912 | inode_ref->inode, file_block, &fs_block);
|
---|
| 913 | if (rc != EOK) {
|
---|
| 914 | async_answer_0(callid, rc);
|
---|
| 915 | return rc;
|
---|
| 916 | }
|
---|
| 917 |
|
---|
| 918 | /* Check for sparse file
|
---|
[2b9e142] | 919 | * If ext4_filesystem_get_inode_data_block_index returned
|
---|
[9b9d37bb] | 920 | * fs_block == 0, it means that the given block is not allocated for the
|
---|
| 921 | * file and we need to return a buffer of zeros
|
---|
| 922 | */
|
---|
| 923 | if (fs_block == 0) {
|
---|
| 924 | buffer = malloc(bytes);
|
---|
| 925 | if (buffer == NULL) {
|
---|
| 926 | async_answer_0(callid, ENOMEM);
|
---|
| 927 | return ENOMEM;
|
---|
| 928 | }
|
---|
| 929 |
|
---|
| 930 | memset(buffer, 0, bytes);
|
---|
| 931 |
|
---|
| 932 | async_data_read_finalize(callid, buffer, bytes);
|
---|
| 933 | *rbytes = bytes;
|
---|
| 934 |
|
---|
| 935 | free(buffer);
|
---|
| 936 |
|
---|
| 937 | return EOK;
|
---|
| 938 | }
|
---|
| 939 |
|
---|
| 940 | /* Usual case - we need to read a block from device */
|
---|
| 941 | rc = block_get(&block, inst->service_id, fs_block, BLOCK_FLAGS_NONE);
|
---|
| 942 | if (rc != EOK) {
|
---|
| 943 | async_answer_0(callid, rc);
|
---|
| 944 | return rc;
|
---|
| 945 | }
|
---|
| 946 |
|
---|
| 947 | assert(offset_in_block + bytes <= block_size);
|
---|
| 948 | async_data_read_finalize(callid, block->data + offset_in_block, bytes);
|
---|
| 949 |
|
---|
| 950 | rc = block_put(block);
|
---|
[8958a26] | 951 | if (rc != EOK) {
|
---|
[9b9d37bb] | 952 | return rc;
|
---|
[8958a26] | 953 | }
|
---|
[9b9d37bb] | 954 |
|
---|
| 955 | *rbytes = bytes;
|
---|
| 956 | return EOK;
|
---|
| 957 | }
|
---|
[3711e7e] | 958 |
|
---|
[d3a9ae74] | 959 | static int
|
---|
| 960 | ext4fs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
|
---|
| 961 | size_t *wbytes, aoff64_t *nsize)
|
---|
| 962 | {
|
---|
[1c1c736] | 963 | int rc;
|
---|
[35f48f2] | 964 | int flags = BLOCK_FLAGS_NONE;
|
---|
[1c1c736] | 965 | fs_node_t *fn;
|
---|
| 966 | ext4fs_node_t *enode;
|
---|
| 967 | ext4_filesystem_t *fs;
|
---|
| 968 | ext4_inode_ref_t *inode_ref;
|
---|
| 969 | ipc_callid_t callid;
|
---|
| 970 | size_t len, bytes, block_size;
|
---|
| 971 | block_t *write_block;
|
---|
| 972 | uint32_t fblock, iblock;
|
---|
[35f48f2] | 973 | uint32_t old_inode_size;
|
---|
[1c1c736] | 974 |
|
---|
| 975 | rc = ext4fs_node_get(&fn, service_id, index);
|
---|
| 976 | if (rc != EOK) {
|
---|
[6088193] | 977 | EXT4FS_DBG("node get error");
|
---|
[1c1c736] | 978 | return rc;
|
---|
| 979 | }
|
---|
| 980 |
|
---|
| 981 | if (!async_data_write_receive(&callid, &len)) {
|
---|
| 982 | rc = EINVAL;
|
---|
| 983 | ext4fs_node_put(fn);
|
---|
| 984 | async_answer_0(callid, rc);
|
---|
[6088193] | 985 | EXT4FS_DBG("data write recv");
|
---|
[1c1c736] | 986 | return rc;
|
---|
| 987 | }
|
---|
| 988 |
|
---|
| 989 |
|
---|
| 990 | enode = EXT4FS_NODE(fn);
|
---|
| 991 | inode_ref = enode->inode_ref;
|
---|
| 992 | fs = enode->instance->filesystem;
|
---|
| 993 |
|
---|
| 994 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 995 |
|
---|
| 996 | // Prevent writing to more than one block
|
---|
| 997 | bytes = min(len, block_size - (pos % block_size));
|
---|
| 998 |
|
---|
[35f48f2] | 999 | if (bytes == block_size) {
|
---|
| 1000 | flags = BLOCK_FLAGS_NOREAD;
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
[1c1c736] | 1003 | iblock = pos / block_size;
|
---|
| 1004 |
|
---|
| 1005 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, iblock, &fblock);
|
---|
[6088193] | 1006 | if (rc != EOK) {
|
---|
| 1007 | // TODO error
|
---|
| 1008 | ext4fs_node_put(fn);
|
---|
| 1009 | EXT4FS_DBG("error loading block addr");
|
---|
| 1010 | return rc;
|
---|
| 1011 | }
|
---|
[1c1c736] | 1012 |
|
---|
| 1013 | if (fblock == 0) {
|
---|
[b12ca16] | 1014 | rc = ext4_balloc_alloc_block(fs, inode_ref, &fblock);
|
---|
[35f48f2] | 1015 | if (rc != EOK) {
|
---|
| 1016 | ext4fs_node_put(fn);
|
---|
| 1017 | async_answer_0(callid, rc);
|
---|
| 1018 | return rc;
|
---|
| 1019 | }
|
---|
| 1020 |
|
---|
[b12ca16] | 1021 | rc = ext4_filesystem_set_inode_data_block_index(fs, inode_ref, iblock, fblock);
|
---|
| 1022 | if (rc != EOK) {
|
---|
| 1023 | EXT4FS_DBG("ERROR: setting index failed");
|
---|
| 1024 | }
|
---|
[35f48f2] | 1025 | inode_ref->dirty = true;
|
---|
| 1026 |
|
---|
| 1027 | flags = BLOCK_FLAGS_NOREAD;
|
---|
[1c1c736] | 1028 | }
|
---|
| 1029 |
|
---|
[35f48f2] | 1030 | rc = block_get(&write_block, service_id, fblock, flags);
|
---|
[1c1c736] | 1031 | if (rc != EOK) {
|
---|
[6088193] | 1032 | EXT4FS_DBG("error in loading block \%d", rc);
|
---|
[1c1c736] | 1033 | ext4fs_node_put(fn);
|
---|
| 1034 | async_answer_0(callid, rc);
|
---|
| 1035 | return rc;
|
---|
| 1036 | }
|
---|
| 1037 |
|
---|
[35f48f2] | 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) {
|
---|
[1e65444] | 1044 | // TODO error
|
---|
[35f48f2] | 1045 | EXT4FS_DBG("error in write finalize \%d", rc);
|
---|
| 1046 | }
|
---|
| 1047 |
|
---|
[1c1c736] | 1048 | write_block->dirty = true;
|
---|
| 1049 |
|
---|
| 1050 | rc = block_put(write_block);
|
---|
| 1051 | if (rc != EOK) {
|
---|
[6088193] | 1052 | EXT4FS_DBG("error in writing block \%d", rc);
|
---|
[1c1c736] | 1053 | ext4fs_node_put(fn);
|
---|
| 1054 | return rc;
|
---|
| 1055 | }
|
---|
| 1056 |
|
---|
[35f48f2] | 1057 | 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;
|
---|
[1c1c736] | 1061 | }
|
---|
[35f48f2] | 1062 |
|
---|
| 1063 | *nsize = ext4_inode_get_size(fs->superblock, inode_ref->inode);
|
---|
[1c1c736] | 1064 | *wbytes = bytes;
|
---|
[35f48f2] | 1065 |
|
---|
[1c1c736] | 1066 | return ext4fs_node_put(fn);
|
---|
[d3a9ae74] | 1067 | }
|
---|
| 1068 |
|
---|
[3711e7e] | 1069 |
|
---|
[d3a9ae74] | 1070 | static int
|
---|
[d5a78e28] | 1071 | ext4fs_truncate(service_id_t service_id, fs_index_t index, aoff64_t new_size)
|
---|
[d3a9ae74] | 1072 | {
|
---|
[d5a78e28] | 1073 | fs_node_t *fn;
|
---|
| 1074 | ext4fs_node_t *enode;
|
---|
[052e82d] | 1075 | ext4_inode_ref_t *inode_ref;
|
---|
[d5a78e28] | 1076 | ext4_filesystem_t* fs;
|
---|
| 1077 | aoff64_t old_size;
|
---|
| 1078 | aoff64_t size_diff;
|
---|
| 1079 | int rc;
|
---|
[e68c834] | 1080 |
|
---|
[d5a78e28] | 1081 | rc = ext4fs_node_get(&fn, service_id, index);
|
---|
| 1082 | if (rc != EOK) {
|
---|
| 1083 | return rc;
|
---|
| 1084 | }
|
---|
| 1085 |
|
---|
| 1086 | enode = EXT4FS_NODE(fn);
|
---|
[052e82d] | 1087 | inode_ref = enode->inode_ref;
|
---|
[d5a78e28] | 1088 | fs = enode->instance->filesystem;
|
---|
| 1089 |
|
---|
| 1090 |
|
---|
[43a9968] | 1091 | if (! ext4_inode_can_truncate(fs->superblock, inode_ref->inode)) {
|
---|
[12b4a7f] | 1092 | // Unable to truncate
|
---|
[1c1c736] | 1093 | ext4fs_node_put(fn);
|
---|
[12b4a7f] | 1094 | return EINVAL;
|
---|
| 1095 | }
|
---|
| 1096 |
|
---|
| 1097 | old_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
|
---|
[d5a78e28] | 1098 |
|
---|
| 1099 | if (old_size == new_size) {
|
---|
[1c1c736] | 1100 | ext4fs_node_put(fn);
|
---|
| 1101 | return EOK;
|
---|
[d5a78e28] | 1102 | } else {
|
---|
| 1103 |
|
---|
| 1104 | uint32_t block_size;
|
---|
| 1105 | uint32_t blocks_count, total_blocks;
|
---|
| 1106 | uint32_t i;
|
---|
| 1107 |
|
---|
| 1108 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 1109 |
|
---|
| 1110 | if (old_size < new_size) {
|
---|
[12b4a7f] | 1111 | // Currently not supported to expand the file
|
---|
| 1112 | // TODO
|
---|
| 1113 | EXT4FS_DBG("trying to expand the file");
|
---|
[d5a78e28] | 1114 | return EINVAL;
|
---|
| 1115 | }
|
---|
| 1116 |
|
---|
| 1117 | size_diff = old_size - new_size;
|
---|
| 1118 | blocks_count = size_diff / block_size;
|
---|
| 1119 | if (size_diff % block_size != 0) {
|
---|
| 1120 | blocks_count++;
|
---|
| 1121 | }
|
---|
| 1122 |
|
---|
| 1123 | total_blocks = old_size / block_size;
|
---|
| 1124 | if (old_size % block_size != 0) {
|
---|
| 1125 | total_blocks++;
|
---|
| 1126 | }
|
---|
| 1127 |
|
---|
[052e82d] | 1128 | inode_ref->dirty = true;
|
---|
[d5a78e28] | 1129 |
|
---|
[12b4a7f] | 1130 | // starting from 1 because of logical blocks are numbered from 0
|
---|
| 1131 | for (i = 1; i <= blocks_count; ++i) {
|
---|
[d5a78e28] | 1132 | // TODO check retval
|
---|
[12b4a7f] | 1133 | // TODO decrement inode->blocks_count
|
---|
| 1134 |
|
---|
[052e82d] | 1135 | ext4_filesystem_release_inode_block(fs, inode_ref, total_blocks - i);
|
---|
[d5a78e28] | 1136 | }
|
---|
| 1137 |
|
---|
[052e82d] | 1138 | ext4_inode_set_size(inode_ref->inode, new_size);
|
---|
[d5a78e28] | 1139 |
|
---|
| 1140 | }
|
---|
| 1141 |
|
---|
| 1142 | ext4fs_node_put(fn);
|
---|
[1c1c736] | 1143 |
|
---|
| 1144 | return EOK;
|
---|
[d3a9ae74] | 1145 | }
|
---|
| 1146 |
|
---|
[3711e7e] | 1147 |
|
---|
[d3a9ae74] | 1148 | static int ext4fs_close(service_id_t service_id, fs_index_t index)
|
---|
| 1149 | {
|
---|
| 1150 | return EOK;
|
---|
| 1151 | }
|
---|
| 1152 |
|
---|
[3711e7e] | 1153 |
|
---|
[d3a9ae74] | 1154 | static int ext4fs_destroy(service_id_t service_id, fs_index_t index)
|
---|
| 1155 | {
|
---|
[e68c834] | 1156 | EXT4FS_DBG("not supported");
|
---|
| 1157 |
|
---|
[d3a9ae74] | 1158 | //TODO
|
---|
| 1159 | return ENOTSUP;
|
---|
| 1160 | }
|
---|
| 1161 |
|
---|
[3711e7e] | 1162 |
|
---|
[d3a9ae74] | 1163 | static int ext4fs_sync(service_id_t service_id, fs_index_t index)
|
---|
| 1164 | {
|
---|
[35f48f2] | 1165 | int rc;
|
---|
| 1166 | fs_node_t *fn;
|
---|
| 1167 | ext4fs_node_t *enode;
|
---|
| 1168 |
|
---|
| 1169 | rc = ext4fs_node_get(&fn, service_id, index);
|
---|
| 1170 | if (rc != EOK) {
|
---|
| 1171 | return rc;
|
---|
| 1172 | }
|
---|
| 1173 |
|
---|
| 1174 | enode = EXT4FS_NODE(fn);
|
---|
| 1175 | enode->inode_ref->dirty = true;
|
---|
| 1176 |
|
---|
| 1177 | return ext4fs_node_put(fn);
|
---|
[d3a9ae74] | 1178 | }
|
---|
| 1179 |
|
---|
| 1180 | vfs_out_ops_t ext4fs_ops = {
|
---|
| 1181 | .mounted = ext4fs_mounted,
|
---|
| 1182 | .unmounted = ext4fs_unmounted,
|
---|
| 1183 | .read = ext4fs_read,
|
---|
| 1184 | .write = ext4fs_write,
|
---|
| 1185 | .truncate = ext4fs_truncate,
|
---|
| 1186 | .close = ext4fs_close,
|
---|
| 1187 | .destroy = ext4fs_destroy,
|
---|
| 1188 | .sync = ext4fs_sync,
|
---|
| 1189 | };
|
---|
| 1190 |
|
---|
| 1191 | /**
|
---|
| 1192 | * @}
|
---|
[2b9e142] | 1193 | */
|
---|