[796c276] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2011 Martin Sucha
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup fs
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * @file ext2fs_ops.c
|
---|
| 36 | * @brief Implementation of VFS operations for the EXT2 file system server.
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | #include "ext2fs.h"
|
---|
| 40 | #include "../../vfs/vfs.h"
|
---|
| 41 | #include <libfs.h>
|
---|
| 42 | #include <libblock.h>
|
---|
| 43 | #include <libext2.h>
|
---|
| 44 | #include <ipc/services.h>
|
---|
| 45 | #include <ipc/devmap.h>
|
---|
| 46 | #include <macros.h>
|
---|
| 47 | #include <async.h>
|
---|
| 48 | #include <errno.h>
|
---|
| 49 | #include <str.h>
|
---|
| 50 | #include <byteorder.h>
|
---|
| 51 | #include <adt/hash_table.h>
|
---|
| 52 | #include <adt/list.h>
|
---|
| 53 | #include <assert.h>
|
---|
| 54 | #include <fibril_synch.h>
|
---|
| 55 | #include <sys/mman.h>
|
---|
| 56 | #include <align.h>
|
---|
| 57 | #include <adt/hash_table.h>
|
---|
[7ea69a6] | 58 | #include <sys/typefmt.h>
|
---|
[0e26444] | 59 | #include <malloc.h>
|
---|
| 60 | #include <stdio.h>
|
---|
[f6fece9] | 61 | #include <inttypes.h>
|
---|
[796c276] | 62 |
|
---|
[4dc6a32] | 63 | #define EXT2FS_NODE(node) ((node) ? (ext2fs_node_t *) (node)->data : NULL)
|
---|
| 64 | #define EXT2FS_DBG(format, ...) {if (false) printf("ext2fs: %s: " format "\n", __FUNCTION__, ##__VA_ARGS__);}
|
---|
[d1ac0d7] | 65 | #define OPEN_NODES_KEYS 2
|
---|
| 66 | #define OPEN_NODES_DEV_HANDLE_KEY 0
|
---|
| 67 | #define OPEN_NODES_INODE_KEY 1
|
---|
| 68 | #define OPEN_NODES_BUCKETS 256
|
---|
[796c276] | 69 |
|
---|
[4dc6a32] | 70 | typedef struct ext2fs_instance {
|
---|
| 71 | link_t link;
|
---|
| 72 | devmap_handle_t devmap_handle;
|
---|
| 73 | ext2_filesystem_t *filesystem;
|
---|
[d1ac0d7] | 74 | unsigned int open_nodes_count;
|
---|
[4dc6a32] | 75 | } ext2fs_instance_t;
|
---|
| 76 |
|
---|
| 77 | typedef struct ext2fs_node {
|
---|
| 78 | ext2fs_instance_t *instance;
|
---|
| 79 | ext2_inode_ref_t *inode_ref;
|
---|
[d1ac0d7] | 80 | fs_node_t *fs_node;
|
---|
| 81 | link_t link;
|
---|
| 82 | unsigned int references;
|
---|
[4dc6a32] | 83 | } ext2fs_node_t;
|
---|
| 84 |
|
---|
[e2abab03] | 85 | /*
|
---|
| 86 | * Forward declarations of auxiliary functions
|
---|
| 87 | */
|
---|
[4dc6a32] | 88 | static int ext2fs_instance_get(devmap_handle_t, ext2fs_instance_t **);
|
---|
[b83e16ff] | 89 | static void ext2fs_read_directory(ipc_callid_t, ipc_callid_t, aoff64_t,
|
---|
| 90 | size_t, ext2fs_instance_t *, ext2_inode_ref_t *);
|
---|
[529edc66] | 91 | static void ext2fs_read_file(ipc_callid_t, ipc_callid_t, aoff64_t,
|
---|
| 92 | size_t, ext2fs_instance_t *, ext2_inode_ref_t *);
|
---|
[84226f0] | 93 | static bool ext2fs_is_dots(const uint8_t *, size_t);
|
---|
[e2abab03] | 94 | static int ext2fs_node_get_core(fs_node_t **, ext2fs_instance_t *, fs_index_t);
|
---|
[d1ac0d7] | 95 | static int ext2fs_node_put_core(ext2fs_node_t *);
|
---|
[796c276] | 96 |
|
---|
| 97 | /*
|
---|
| 98 | * Forward declarations of EXT2 libfs operations.
|
---|
| 99 | */
|
---|
| 100 | static int ext2fs_root_get(fs_node_t **, devmap_handle_t);
|
---|
| 101 | static int ext2fs_match(fs_node_t **, fs_node_t *, const char *);
|
---|
| 102 | static int ext2fs_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
|
---|
| 103 | static int ext2fs_node_open(fs_node_t *);
|
---|
| 104 | static int ext2fs_node_put(fs_node_t *);
|
---|
| 105 | static int ext2fs_create_node(fs_node_t **, devmap_handle_t, int);
|
---|
| 106 | static int ext2fs_destroy_node(fs_node_t *);
|
---|
| 107 | static int ext2fs_link(fs_node_t *, fs_node_t *, const char *);
|
---|
| 108 | static int ext2fs_unlink(fs_node_t *, fs_node_t *, const char *);
|
---|
| 109 | static int ext2fs_has_children(bool *, fs_node_t *);
|
---|
| 110 | static fs_index_t ext2fs_index_get(fs_node_t *);
|
---|
| 111 | static aoff64_t ext2fs_size_get(fs_node_t *);
|
---|
| 112 | static unsigned ext2fs_lnkcnt_get(fs_node_t *);
|
---|
| 113 | static char ext2fs_plb_get_char(unsigned);
|
---|
| 114 | static bool ext2fs_is_directory(fs_node_t *);
|
---|
| 115 | static bool ext2fs_is_file(fs_node_t *node);
|
---|
| 116 | static devmap_handle_t ext2fs_device_get(fs_node_t *node);
|
---|
| 117 |
|
---|
| 118 | /*
|
---|
| 119 | * Static variables
|
---|
| 120 | */
|
---|
[4dc6a32] | 121 | static LIST_INITIALIZE(instance_list);
|
---|
[a210bc7] | 122 | static FIBRIL_MUTEX_INITIALIZE(instance_list_mutex);
|
---|
[d1ac0d7] | 123 | static hash_table_t open_nodes;
|
---|
| 124 | static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
|
---|
| 125 |
|
---|
| 126 | /* Hash table interface for open nodes hash table */
|
---|
| 127 | static hash_index_t open_nodes_hash(unsigned long key[])
|
---|
| 128 | {
|
---|
| 129 | /* TODO: This is very simple and probably can be improved */
|
---|
| 130 | return key[OPEN_NODES_INODE_KEY] % OPEN_NODES_BUCKETS;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | static int open_nodes_compare(unsigned long key[], hash_count_t keys,
|
---|
| 134 | link_t *item)
|
---|
| 135 | {
|
---|
| 136 | ext2fs_node_t *enode = hash_table_get_instance(item, ext2fs_node_t, link);
|
---|
| 137 | assert(keys > 0);
|
---|
| 138 | if (enode->instance->devmap_handle !=
|
---|
| 139 | ((devmap_handle_t) key[OPEN_NODES_DEV_HANDLE_KEY])) {
|
---|
| 140 | return false;
|
---|
| 141 | }
|
---|
| 142 | if (keys == 1) {
|
---|
| 143 | return true;
|
---|
| 144 | }
|
---|
| 145 | assert(keys == 2);
|
---|
| 146 | return (enode->inode_ref->index == key[OPEN_NODES_INODE_KEY]);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | static void open_nodes_remove_cb(link_t *link)
|
---|
| 150 | {
|
---|
| 151 | /* We don't use remove callback for this hash table */
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | static hash_table_operations_t open_nodes_ops = {
|
---|
| 155 | .hash = open_nodes_hash,
|
---|
| 156 | .compare = open_nodes_compare,
|
---|
| 157 | .remove_callback = open_nodes_remove_cb,
|
---|
| 158 | };
|
---|
[796c276] | 159 |
|
---|
| 160 | /**
|
---|
| 161 | *
|
---|
| 162 | */
|
---|
| 163 | int ext2fs_global_init(void)
|
---|
| 164 | {
|
---|
[d1ac0d7] | 165 | if (!hash_table_create(&open_nodes, OPEN_NODES_BUCKETS,
|
---|
| 166 | OPEN_NODES_KEYS, &open_nodes_ops)) {
|
---|
| 167 | return ENOMEM;
|
---|
| 168 | }
|
---|
[4dc6a32] | 169 | return EOK;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | int ext2fs_global_fini(void)
|
---|
| 173 | {
|
---|
[d1ac0d7] | 174 | hash_table_destroy(&open_nodes);
|
---|
[796c276] | 175 | return EOK;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 | /*
|
---|
| 180 | * EXT2 libfs operations.
|
---|
| 181 | */
|
---|
| 182 |
|
---|
[4dc6a32] | 183 | /**
|
---|
| 184 | * Find an instance of filesystem for the given devmap_handle
|
---|
| 185 | */
|
---|
| 186 | int ext2fs_instance_get(devmap_handle_t devmap_handle, ext2fs_instance_t **inst)
|
---|
| 187 | {
|
---|
[f6fece9] | 188 | EXT2FS_DBG("(%" PRIun ", -)", devmap_handle);
|
---|
[4dc6a32] | 189 | link_t *link;
|
---|
| 190 | ext2fs_instance_t *tmp;
|
---|
[a210bc7] | 191 |
|
---|
| 192 | fibril_mutex_lock(&instance_list_mutex);
|
---|
[4dc6a32] | 193 |
|
---|
| 194 | if (list_empty(&instance_list)) {
|
---|
| 195 | EXT2FS_DBG("list empty");
|
---|
[a210bc7] | 196 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
[4dc6a32] | 197 | return EINVAL;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | for (link = instance_list.next; link != &instance_list; link = link->next) {
|
---|
| 201 | tmp = list_get_instance(link, ext2fs_instance_t, link);
|
---|
| 202 |
|
---|
| 203 | if (tmp->devmap_handle == devmap_handle) {
|
---|
| 204 | *inst = tmp;
|
---|
[a210bc7] | 205 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
[4dc6a32] | 206 | return EOK;
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | EXT2FS_DBG("not found");
|
---|
[a210bc7] | 211 |
|
---|
| 212 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
[4dc6a32] | 213 | return EINVAL;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 |
|
---|
[796c276] | 218 | int ext2fs_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
---|
| 219 | {
|
---|
[f6fece9] | 220 | EXT2FS_DBG("(-, %" PRIun ")", devmap_handle);
|
---|
[4dc6a32] | 221 | return ext2fs_node_get(rfn, devmap_handle, EXT2_INODE_ROOT_INDEX);
|
---|
[796c276] | 222 | }
|
---|
| 223 |
|
---|
| 224 | int ext2fs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
| 225 | {
|
---|
[4dc6a32] | 226 | EXT2FS_DBG("(-,-,%s)", component);
|
---|
| 227 | ext2fs_node_t *eparent = EXT2FS_NODE(pfn);
|
---|
| 228 | ext2_filesystem_t *fs;
|
---|
| 229 | ext2_directory_iterator_t it;
|
---|
| 230 | int rc;
|
---|
| 231 | size_t name_size;
|
---|
| 232 | size_t component_size;
|
---|
| 233 | bool found = false;
|
---|
[82a3b31f] | 234 | uint32_t inode;
|
---|
[4dc6a32] | 235 |
|
---|
| 236 | fs = eparent->instance->filesystem;
|
---|
| 237 |
|
---|
| 238 | if (!ext2_inode_is_type(fs->superblock, eparent->inode_ref->inode,
|
---|
| 239 | EXT2_INODE_MODE_DIRECTORY)) {
|
---|
| 240 | return ENOTDIR;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | rc = ext2_directory_iterator_init(&it, fs, eparent->inode_ref);
|
---|
| 244 | if (rc != EOK) {
|
---|
| 245 | return rc;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[1a86c50] | 248 | /* Find length of component in bytes
|
---|
| 249 | * TODO: check for library function call that does this
|
---|
| 250 | */
|
---|
[4dc6a32] | 251 | component_size = 0;
|
---|
| 252 | while (*(component+component_size) != 0) {
|
---|
| 253 | component_size++;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | while (it.current != NULL) {
|
---|
[82a3b31f] | 257 | inode = ext2_directory_entry_ll_get_inode(it.current);
|
---|
| 258 |
|
---|
[1a86c50] | 259 | /* ignore empty directory entries */
|
---|
[82a3b31f] | 260 | if (inode != 0) {
|
---|
[4dc6a32] | 261 | name_size = ext2_directory_entry_ll_get_name_length(fs->superblock,
|
---|
| 262 | it.current);
|
---|
| 263 |
|
---|
| 264 | if (name_size == component_size && bcmp(component, &it.current->name,
|
---|
| 265 | name_size) == 0) {
|
---|
[e2abab03] | 266 | rc = ext2fs_node_get_core(rfn, eparent->instance,
|
---|
[82a3b31f] | 267 | inode);
|
---|
[4dc6a32] | 268 | if (rc != EOK) {
|
---|
| 269 | ext2_directory_iterator_fini(&it);
|
---|
| 270 | return rc;
|
---|
| 271 | }
|
---|
| 272 | found = true;
|
---|
| 273 | break;
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | rc = ext2_directory_iterator_next(&it);
|
---|
| 278 | if (rc != EOK) {
|
---|
| 279 | ext2_directory_iterator_fini(&it);
|
---|
| 280 | return rc;
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | ext2_directory_iterator_fini(&it);
|
---|
| 285 |
|
---|
| 286 | if (!found) {
|
---|
| 287 | return ENOENT;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | return EOK;
|
---|
[796c276] | 291 | }
|
---|
| 292 |
|
---|
| 293 | /** Instantiate a EXT2 in-core node. */
|
---|
| 294 | int ext2fs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
|
---|
| 295 | {
|
---|
[f6fece9] | 296 | EXT2FS_DBG("(-,%" PRIun ",%u)", devmap_handle, index);
|
---|
[e2abab03] | 297 |
|
---|
| 298 | ext2fs_instance_t *inst = NULL;
|
---|
| 299 | int rc;
|
---|
| 300 |
|
---|
| 301 | rc = ext2fs_instance_get(devmap_handle, &inst);
|
---|
| 302 | if (rc != EOK) {
|
---|
| 303 | return rc;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | return ext2fs_node_get_core(rfn, inst, index);
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | int ext2fs_node_get_core(fs_node_t **rfn, ext2fs_instance_t *inst,
|
---|
| 310 | fs_index_t index)
|
---|
| 311 | {
|
---|
[4dc6a32] | 312 | int rc;
|
---|
| 313 | fs_node_t *node = NULL;
|
---|
| 314 | ext2fs_node_t *enode = NULL;
|
---|
[e2abab03] | 315 |
|
---|
[4dc6a32] | 316 | ext2_inode_ref_t *inode_ref = NULL;
|
---|
| 317 |
|
---|
[d1ac0d7] | 318 | fibril_mutex_lock(&open_nodes_lock);
|
---|
| 319 |
|
---|
| 320 | /* Check if the node is not already open */
|
---|
| 321 | unsigned long key[] = {
|
---|
| 322 | [OPEN_NODES_DEV_HANDLE_KEY] = inst->devmap_handle,
|
---|
| 323 | [OPEN_NODES_INODE_KEY] = index,
|
---|
| 324 | };
|
---|
| 325 | link_t *already_open = hash_table_find(&open_nodes, key);
|
---|
| 326 |
|
---|
| 327 | if (already_open) {
|
---|
| 328 | enode = hash_table_get_instance(already_open, ext2fs_node_t, link);
|
---|
| 329 | *rfn = enode->fs_node;
|
---|
| 330 | enode->references++;
|
---|
| 331 |
|
---|
| 332 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 333 | return EOK;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[4dc6a32] | 336 | enode = malloc(sizeof(ext2fs_node_t));
|
---|
| 337 | if (enode == NULL) {
|
---|
[d1ac0d7] | 338 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
[4dc6a32] | 339 | return ENOMEM;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | node = malloc(sizeof(fs_node_t));
|
---|
| 343 | if (node == NULL) {
|
---|
| 344 | free(enode);
|
---|
[d1ac0d7] | 345 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
[4dc6a32] | 346 | return ENOMEM;
|
---|
[e2abab03] | 347 | }
|
---|
[4dc6a32] | 348 | fs_node_initialize(node);
|
---|
| 349 |
|
---|
| 350 | rc = ext2_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
|
---|
| 351 | if (rc != EOK) {
|
---|
| 352 | free(enode);
|
---|
| 353 | free(node);
|
---|
[d1ac0d7] | 354 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
[4dc6a32] | 355 | return rc;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | enode->inode_ref = inode_ref;
|
---|
| 359 | enode->instance = inst;
|
---|
[d1ac0d7] | 360 | enode->references = 1;
|
---|
| 361 | enode->fs_node = node;
|
---|
| 362 | link_initialize(&enode->link);
|
---|
| 363 |
|
---|
[4dc6a32] | 364 | node->data = enode;
|
---|
| 365 | *rfn = node;
|
---|
| 366 |
|
---|
[d1ac0d7] | 367 | hash_table_insert(&open_nodes, key, &enode->link);
|
---|
| 368 | inst->open_nodes_count++;
|
---|
| 369 |
|
---|
[4dc6a32] | 370 | EXT2FS_DBG("inode: %u", inode_ref->index);
|
---|
| 371 |
|
---|
| 372 | EXT2FS_DBG("EOK");
|
---|
| 373 |
|
---|
[d1ac0d7] | 374 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
[4dc6a32] | 375 | return EOK;
|
---|
[796c276] | 376 | }
|
---|
| 377 |
|
---|
| 378 | int ext2fs_node_open(fs_node_t *fn)
|
---|
| 379 | {
|
---|
[4dc6a32] | 380 | EXT2FS_DBG("");
|
---|
[796c276] | 381 | /*
|
---|
| 382 | * Opening a file is stateless, nothing
|
---|
| 383 | * to be done here.
|
---|
| 384 | */
|
---|
| 385 | return EOK;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | int ext2fs_node_put(fs_node_t *fn)
|
---|
| 389 | {
|
---|
[4dc6a32] | 390 | EXT2FS_DBG("");
|
---|
| 391 | int rc;
|
---|
| 392 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
[d1ac0d7] | 393 |
|
---|
| 394 | fibril_mutex_lock(&open_nodes_lock);
|
---|
| 395 |
|
---|
| 396 | assert(enode->references > 0);
|
---|
| 397 | enode->references--;
|
---|
| 398 | if (enode->references == 0) {
|
---|
| 399 | rc = ext2fs_node_put_core(enode);
|
---|
| 400 | if (rc != EOK) {
|
---|
| 401 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 402 | return rc;
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 407 |
|
---|
| 408 | return EOK;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | int ext2fs_node_put_core(ext2fs_node_t *enode)
|
---|
| 412 | {
|
---|
| 413 | int rc;
|
---|
| 414 |
|
---|
| 415 | unsigned long key[] = {
|
---|
| 416 | [OPEN_NODES_DEV_HANDLE_KEY] = enode->instance->devmap_handle,
|
---|
| 417 | [OPEN_NODES_INODE_KEY] = enode->inode_ref->index,
|
---|
| 418 | };
|
---|
| 419 | hash_table_remove(&open_nodes, key, OPEN_NODES_KEYS);
|
---|
| 420 | assert(enode->instance->open_nodes_count > 0);
|
---|
| 421 | enode->instance->open_nodes_count--;
|
---|
| 422 |
|
---|
[4dc6a32] | 423 | rc = ext2_filesystem_put_inode_ref(enode->inode_ref);
|
---|
| 424 | if (rc != EOK) {
|
---|
| 425 | EXT2FS_DBG("ext2_filesystem_put_inode_ref failed");
|
---|
[d1ac0d7] | 426 | return rc;
|
---|
[4dc6a32] | 427 | }
|
---|
[d1ac0d7] | 428 |
|
---|
| 429 | free(enode->fs_node);
|
---|
| 430 | free(enode);
|
---|
| 431 | return EOK;
|
---|
[796c276] | 432 | }
|
---|
| 433 |
|
---|
| 434 | int ext2fs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
|
---|
| 435 | {
|
---|
[4dc6a32] | 436 | EXT2FS_DBG("");
|
---|
[796c276] | 437 | // TODO
|
---|
| 438 | return ENOTSUP;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | int ext2fs_destroy_node(fs_node_t *fn)
|
---|
| 442 | {
|
---|
[4dc6a32] | 443 | EXT2FS_DBG("");
|
---|
[796c276] | 444 | // TODO
|
---|
| 445 | return ENOTSUP;
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | int ext2fs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
---|
| 449 | {
|
---|
[4dc6a32] | 450 | EXT2FS_DBG("");
|
---|
[796c276] | 451 | // TODO
|
---|
| 452 | return ENOTSUP;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | int ext2fs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
| 456 | {
|
---|
[4dc6a32] | 457 | EXT2FS_DBG("");
|
---|
[796c276] | 458 | // TODO
|
---|
| 459 | return ENOTSUP;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | int ext2fs_has_children(bool *has_children, fs_node_t *fn)
|
---|
| 463 | {
|
---|
[4dc6a32] | 464 | EXT2FS_DBG("");
|
---|
| 465 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 466 | ext2_directory_iterator_t it;
|
---|
| 467 | ext2_filesystem_t *fs;
|
---|
| 468 | int rc;
|
---|
| 469 | bool found = false;
|
---|
[84226f0] | 470 | size_t name_size;
|
---|
[4dc6a32] | 471 |
|
---|
| 472 | fs = enode->instance->filesystem;
|
---|
| 473 |
|
---|
| 474 | if (!ext2_inode_is_type(fs->superblock, enode->inode_ref->inode,
|
---|
| 475 | EXT2_INODE_MODE_DIRECTORY)) {
|
---|
| 476 | *has_children = false;
|
---|
| 477 | EXT2FS_DBG("EOK - false");
|
---|
| 478 | return EOK;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | rc = ext2_directory_iterator_init(&it, fs, enode->inode_ref);
|
---|
| 482 | if (rc != EOK) {
|
---|
| 483 | EXT2FS_DBG("error %u", rc);
|
---|
| 484 | return rc;
|
---|
| 485 | }
|
---|
| 486 |
|
---|
[1a86c50] | 487 | /* Find a non-empty directory entry */
|
---|
[4dc6a32] | 488 | while (it.current != NULL) {
|
---|
| 489 | if (it.current->inode != 0) {
|
---|
[84226f0] | 490 | name_size = ext2_directory_entry_ll_get_name_length(fs->superblock,
|
---|
| 491 | it.current);
|
---|
| 492 | if (!ext2fs_is_dots(&it.current->name, name_size)) {
|
---|
| 493 | found = true;
|
---|
| 494 | break;
|
---|
| 495 | }
|
---|
[4dc6a32] | 496 | }
|
---|
| 497 |
|
---|
| 498 | rc = ext2_directory_iterator_next(&it);
|
---|
| 499 | if (rc != EOK) {
|
---|
| 500 | ext2_directory_iterator_fini(&it);
|
---|
| 501 | EXT2FS_DBG("error %u", rc);
|
---|
| 502 | return rc;
|
---|
| 503 | }
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | rc = ext2_directory_iterator_fini(&it);
|
---|
| 507 | if (rc != EOK) {
|
---|
| 508 | EXT2FS_DBG("error %u", rc);
|
---|
| 509 | return rc;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | *has_children = found;
|
---|
| 513 | EXT2FS_DBG("EOK");
|
---|
| 514 |
|
---|
| 515 | return EOK;
|
---|
[796c276] | 516 | }
|
---|
| 517 |
|
---|
| 518 |
|
---|
| 519 | fs_index_t ext2fs_index_get(fs_node_t *fn)
|
---|
| 520 | {
|
---|
[4dc6a32] | 521 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 522 | EXT2FS_DBG("%u", enode->inode_ref->index);
|
---|
| 523 | return enode->inode_ref->index;
|
---|
[796c276] | 524 | }
|
---|
| 525 |
|
---|
| 526 | aoff64_t ext2fs_size_get(fs_node_t *fn)
|
---|
| 527 | {
|
---|
[4dc6a32] | 528 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 529 | aoff64_t size = ext2_inode_get_size(enode->instance->filesystem->superblock,
|
---|
| 530 | enode->inode_ref->inode);
|
---|
| 531 | EXT2FS_DBG("%" PRIu64, size);
|
---|
| 532 | return size;
|
---|
[796c276] | 533 | }
|
---|
| 534 |
|
---|
| 535 | unsigned ext2fs_lnkcnt_get(fs_node_t *fn)
|
---|
| 536 | {
|
---|
[4dc6a32] | 537 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 538 | unsigned count = ext2_inode_get_usage_count(enode->inode_ref->inode);
|
---|
| 539 | EXT2FS_DBG("%u", count);
|
---|
| 540 | return count;
|
---|
[796c276] | 541 | }
|
---|
| 542 |
|
---|
| 543 | char ext2fs_plb_get_char(unsigned pos)
|
---|
| 544 | {
|
---|
| 545 | return ext2fs_reg.plb_ro[pos % PLB_SIZE];
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | bool ext2fs_is_directory(fs_node_t *fn)
|
---|
| 549 | {
|
---|
[4dc6a32] | 550 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 551 | bool is_dir = ext2_inode_is_type(enode->instance->filesystem->superblock,
|
---|
| 552 | enode->inode_ref->inode, EXT2_INODE_MODE_DIRECTORY);
|
---|
| 553 | EXT2FS_DBG("%s", is_dir ? "true" : "false");
|
---|
| 554 | EXT2FS_DBG("%u", enode->inode_ref->index);
|
---|
| 555 | return is_dir;
|
---|
[796c276] | 556 | }
|
---|
| 557 |
|
---|
| 558 | bool ext2fs_is_file(fs_node_t *fn)
|
---|
| 559 | {
|
---|
[4dc6a32] | 560 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 561 | bool is_file = ext2_inode_is_type(enode->instance->filesystem->superblock,
|
---|
| 562 | enode->inode_ref->inode, EXT2_INODE_MODE_FILE);
|
---|
| 563 | EXT2FS_DBG("%s", is_file ? "true" : "false");
|
---|
| 564 | return is_file;
|
---|
[796c276] | 565 | }
|
---|
| 566 |
|
---|
[4dc6a32] | 567 | devmap_handle_t ext2fs_device_get(fs_node_t *fn)
|
---|
[796c276] | 568 | {
|
---|
[4dc6a32] | 569 | EXT2FS_DBG("");
|
---|
| 570 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 571 | return enode->instance->devmap_handle;
|
---|
[796c276] | 572 | }
|
---|
| 573 |
|
---|
| 574 | /** libfs operations */
|
---|
| 575 | libfs_ops_t ext2fs_libfs_ops = {
|
---|
| 576 | .root_get = ext2fs_root_get,
|
---|
| 577 | .match = ext2fs_match,
|
---|
| 578 | .node_get = ext2fs_node_get,
|
---|
| 579 | .node_open = ext2fs_node_open,
|
---|
| 580 | .node_put = ext2fs_node_put,
|
---|
| 581 | .create = ext2fs_create_node,
|
---|
| 582 | .destroy = ext2fs_destroy_node,
|
---|
| 583 | .link = ext2fs_link,
|
---|
| 584 | .unlink = ext2fs_unlink,
|
---|
| 585 | .has_children = ext2fs_has_children,
|
---|
| 586 | .index_get = ext2fs_index_get,
|
---|
| 587 | .size_get = ext2fs_size_get,
|
---|
| 588 | .lnkcnt_get = ext2fs_lnkcnt_get,
|
---|
| 589 | .plb_get_char = ext2fs_plb_get_char,
|
---|
| 590 | .is_directory = ext2fs_is_directory,
|
---|
| 591 | .is_file = ext2fs_is_file,
|
---|
| 592 | .device_get = ext2fs_device_get
|
---|
| 593 | };
|
---|
| 594 |
|
---|
| 595 | /*
|
---|
| 596 | * VFS operations.
|
---|
| 597 | */
|
---|
| 598 |
|
---|
| 599 | void ext2fs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 600 | {
|
---|
[4dc6a32] | 601 | EXT2FS_DBG("");
|
---|
[796c276] | 602 | int rc;
|
---|
| 603 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 604 | ext2_filesystem_t *fs;
|
---|
[4dc6a32] | 605 | ext2fs_instance_t *inst;
|
---|
[eef306c] | 606 | bool read_only;
|
---|
[4dc6a32] | 607 |
|
---|
[796c276] | 608 | /* Accept the mount options */
|
---|
| 609 | char *opts;
|
---|
| 610 | rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
|
---|
| 611 |
|
---|
| 612 | if (rc != EOK) {
|
---|
| 613 | async_answer_0(rid, rc);
|
---|
| 614 | return;
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | free(opts);
|
---|
| 618 |
|
---|
| 619 | /* Allocate libext2 filesystem structure */
|
---|
| 620 | fs = (ext2_filesystem_t *) malloc(sizeof(ext2_filesystem_t));
|
---|
| 621 | if (fs == NULL) {
|
---|
| 622 | async_answer_0(rid, ENOMEM);
|
---|
| 623 | return;
|
---|
| 624 | }
|
---|
| 625 |
|
---|
[4dc6a32] | 626 | /* Allocate instance structure */
|
---|
| 627 | inst = (ext2fs_instance_t *) malloc(sizeof(ext2fs_instance_t));
|
---|
| 628 | if (inst == NULL) {
|
---|
| 629 | free(fs);
|
---|
| 630 | async_answer_0(rid, ENOMEM);
|
---|
| 631 | return;
|
---|
| 632 | }
|
---|
| 633 |
|
---|
[796c276] | 634 | /* Initialize the filesystem */
|
---|
| 635 | rc = ext2_filesystem_init(fs, devmap_handle);
|
---|
| 636 | if (rc != EOK) {
|
---|
[4dc6a32] | 637 | free(fs);
|
---|
| 638 | free(inst);
|
---|
[796c276] | 639 | async_answer_0(rid, rc);
|
---|
| 640 | return;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | /* Do some sanity checking */
|
---|
| 644 | rc = ext2_filesystem_check_sanity(fs);
|
---|
| 645 | if (rc != EOK) {
|
---|
| 646 | ext2_filesystem_fini(fs);
|
---|
[4dc6a32] | 647 | free(fs);
|
---|
| 648 | free(inst);
|
---|
[796c276] | 649 | async_answer_0(rid, rc);
|
---|
| 650 | return;
|
---|
| 651 | }
|
---|
| 652 |
|
---|
[eef306c] | 653 | /* Check flags */
|
---|
| 654 | rc = ext2_filesystem_check_flags(fs, &read_only);
|
---|
| 655 | if (rc != EOK) {
|
---|
| 656 | ext2_filesystem_fini(fs);
|
---|
| 657 | free(fs);
|
---|
| 658 | free(inst);
|
---|
| 659 | async_answer_0(rid, rc);
|
---|
| 660 | return;
|
---|
| 661 | }
|
---|
| 662 |
|
---|
[d1ac0d7] | 663 | /* Initialize instance */
|
---|
[4dc6a32] | 664 | link_initialize(&inst->link);
|
---|
| 665 | inst->devmap_handle = devmap_handle;
|
---|
| 666 | inst->filesystem = fs;
|
---|
[d1ac0d7] | 667 | inst->open_nodes_count = 0;
|
---|
[a210bc7] | 668 |
|
---|
[d1ac0d7] | 669 | /* Read root node */
|
---|
| 670 | fs_node_t *root_node;
|
---|
| 671 | rc = ext2fs_node_get_core(&root_node, inst, EXT2_INODE_ROOT_INDEX);
|
---|
| 672 | if (rc != EOK) {
|
---|
| 673 | ext2_filesystem_fini(fs);
|
---|
| 674 | free(fs);
|
---|
| 675 | free(inst);
|
---|
| 676 | async_answer_0(rid, rc);
|
---|
| 677 | return;
|
---|
| 678 | }
|
---|
| 679 | ext2fs_node_t *enode = EXT2FS_NODE(root_node);
|
---|
| 680 |
|
---|
| 681 | /* Add instance to the list */
|
---|
[a210bc7] | 682 | fibril_mutex_lock(&instance_list_mutex);
|
---|
[4dc6a32] | 683 | list_append(&inst->link, &instance_list);
|
---|
[a210bc7] | 684 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
[796c276] | 685 |
|
---|
[d1ac0d7] | 686 | async_answer_3(rid, EOK,
|
---|
| 687 | EXT2_INODE_ROOT_INDEX,
|
---|
| 688 | 0,
|
---|
| 689 | ext2_inode_get_usage_count(enode->inode_ref->inode));
|
---|
| 690 |
|
---|
| 691 | ext2fs_node_put(root_node);
|
---|
[796c276] | 692 | }
|
---|
| 693 |
|
---|
| 694 | void ext2fs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 695 | {
|
---|
[4dc6a32] | 696 | EXT2FS_DBG("");
|
---|
[796c276] | 697 | libfs_mount(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
| 698 | }
|
---|
| 699 |
|
---|
| 700 | void ext2fs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 701 | {
|
---|
[4dc6a32] | 702 | EXT2FS_DBG("");
|
---|
| 703 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 704 | ext2fs_instance_t *inst;
|
---|
| 705 | int rc;
|
---|
| 706 |
|
---|
| 707 | rc = ext2fs_instance_get(devmap_handle, &inst);
|
---|
| 708 |
|
---|
| 709 | if (rc != EOK) {
|
---|
| 710 | async_answer_0(rid, rc);
|
---|
| 711 | return;
|
---|
| 712 | }
|
---|
| 713 |
|
---|
[d1ac0d7] | 714 | fibril_mutex_lock(&open_nodes_lock);
|
---|
| 715 |
|
---|
| 716 | EXT2FS_DBG("open_nodes_count = %d", inst->open_nodes_count)
|
---|
| 717 | if (inst->open_nodes_count != 0) {
|
---|
| 718 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
| 719 | async_answer_0(rid, EBUSY);
|
---|
| 720 | return;
|
---|
| 721 | }
|
---|
[4dc6a32] | 722 |
|
---|
[1a86c50] | 723 | /* Remove the instance from the list */
|
---|
[a210bc7] | 724 | fibril_mutex_lock(&instance_list_mutex);
|
---|
[4dc6a32] | 725 | list_remove(&inst->link);
|
---|
[a210bc7] | 726 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
[d1ac0d7] | 727 |
|
---|
| 728 | fibril_mutex_unlock(&open_nodes_lock);
|
---|
[a210bc7] | 729 |
|
---|
[4dc6a32] | 730 | ext2_filesystem_fini(inst->filesystem);
|
---|
| 731 |
|
---|
| 732 | async_answer_0(rid, EOK);
|
---|
[796c276] | 733 | }
|
---|
| 734 |
|
---|
| 735 | void ext2fs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 736 | {
|
---|
[4dc6a32] | 737 | EXT2FS_DBG("");
|
---|
[796c276] | 738 | libfs_unmount(&ext2fs_libfs_ops, rid, request);
|
---|
| 739 | }
|
---|
| 740 |
|
---|
| 741 | void ext2fs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 742 | {
|
---|
[4dc6a32] | 743 | EXT2FS_DBG("");
|
---|
[796c276] | 744 | libfs_lookup(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
| 745 | }
|
---|
| 746 |
|
---|
| 747 | void ext2fs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 748 | {
|
---|
[4dc6a32] | 749 | EXT2FS_DBG("");
|
---|
| 750 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 751 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 752 | aoff64_t pos =
|
---|
| 753 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
[796c276] | 754 |
|
---|
[4dc6a32] | 755 | ext2fs_instance_t *inst;
|
---|
| 756 | ext2_inode_ref_t *inode_ref;
|
---|
| 757 | int rc;
|
---|
[b83e16ff] | 758 |
|
---|
[4dc6a32] | 759 | /*
|
---|
| 760 | * Receive the read request.
|
---|
| 761 | */
|
---|
| 762 | ipc_callid_t callid;
|
---|
| 763 | size_t size;
|
---|
| 764 | if (!async_data_read_receive(&callid, &size)) {
|
---|
| 765 | async_answer_0(callid, EINVAL);
|
---|
| 766 | async_answer_0(rid, EINVAL);
|
---|
| 767 | return;
|
---|
| 768 | }
|
---|
| 769 |
|
---|
| 770 | rc = ext2fs_instance_get(devmap_handle, &inst);
|
---|
| 771 | if (rc != EOK) {
|
---|
| 772 | async_answer_0(callid, rc);
|
---|
| 773 | async_answer_0(rid, rc);
|
---|
| 774 | return;
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 | rc = ext2_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
|
---|
| 778 | if (rc != EOK) {
|
---|
| 779 | async_answer_0(callid, rc);
|
---|
| 780 | async_answer_0(rid, rc);
|
---|
| 781 | return;
|
---|
| 782 | }
|
---|
| 783 |
|
---|
| 784 | if (ext2_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
|
---|
| 785 | EXT2_INODE_MODE_FILE)) {
|
---|
[529edc66] | 786 | ext2fs_read_file(rid, callid, pos, size, inst, inode_ref);
|
---|
[4dc6a32] | 787 | }
|
---|
| 788 | else if (ext2_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
|
---|
| 789 | EXT2_INODE_MODE_DIRECTORY)) {
|
---|
[b83e16ff] | 790 | ext2fs_read_directory(rid, callid, pos, size, inst, inode_ref);
|
---|
| 791 | }
|
---|
[df38657] | 792 | else {
|
---|
[1a86c50] | 793 | /* Other inode types not supported */
|
---|
[df38657] | 794 | async_answer_0(callid, ENOTSUP);
|
---|
| 795 | async_answer_0(rid, ENOTSUP);
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | ext2_filesystem_put_inode_ref(inode_ref);
|
---|
[b83e16ff] | 799 |
|
---|
| 800 | }
|
---|
| 801 |
|
---|
[84226f0] | 802 | /**
|
---|
| 803 | * Determine whether given directory entry name is . or ..
|
---|
| 804 | */
|
---|
| 805 | bool ext2fs_is_dots(const uint8_t *name, size_t name_size) {
|
---|
| 806 | if (name_size == 1 && name[0] == '.') {
|
---|
| 807 | return true;
|
---|
| 808 | }
|
---|
| 809 |
|
---|
| 810 | if (name_size == 2 && name[0] == '.' && name[1] == '.') {
|
---|
| 811 | return true;
|
---|
| 812 | }
|
---|
| 813 |
|
---|
| 814 | return false;
|
---|
| 815 | }
|
---|
| 816 |
|
---|
[b83e16ff] | 817 | void ext2fs_read_directory(ipc_callid_t rid, ipc_callid_t callid, aoff64_t pos,
|
---|
| 818 | size_t size, ext2fs_instance_t *inst, ext2_inode_ref_t *inode_ref)
|
---|
| 819 | {
|
---|
| 820 | ext2_directory_iterator_t it;
|
---|
| 821 | aoff64_t cur;
|
---|
| 822 | uint8_t *buf;
|
---|
| 823 | size_t name_size;
|
---|
| 824 | int rc;
|
---|
[df38657] | 825 | bool found = false;
|
---|
[b83e16ff] | 826 |
|
---|
| 827 | rc = ext2_directory_iterator_init(&it, inst->filesystem, inode_ref);
|
---|
| 828 | if (rc != EOK) {
|
---|
| 829 | async_answer_0(callid, rc);
|
---|
| 830 | async_answer_0(rid, rc);
|
---|
| 831 | return;
|
---|
| 832 | }
|
---|
| 833 |
|
---|
[1a86c50] | 834 | /* Find the index we want to read
|
---|
| 835 | * Note that we need to iterate and count as
|
---|
| 836 | * the underlying structure is a linked list
|
---|
| 837 | * Moreover, we want to skip . and .. entries
|
---|
| 838 | * as these are not used in HelenOS
|
---|
| 839 | */
|
---|
[b83e16ff] | 840 | cur = 0;
|
---|
| 841 | while (it.current != NULL) {
|
---|
[df38657] | 842 | if (it.current->inode == 0) {
|
---|
| 843 | goto skip;
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 | name_size = ext2_directory_entry_ll_get_name_length(
|
---|
| 847 | inst->filesystem->superblock, it.current);
|
---|
| 848 |
|
---|
[1a86c50] | 849 | /* skip . and .. */
|
---|
[84226f0] | 850 | if (ext2fs_is_dots(&it.current->name, name_size)) {
|
---|
| 851 | goto skip;
|
---|
[4dc6a32] | 852 | }
|
---|
| 853 |
|
---|
[1a86c50] | 854 | /* Is this the dir entry we want to read? */
|
---|
[df38657] | 855 | if (cur == pos) {
|
---|
[1a86c50] | 856 | /* The on-disk entry does not contain \0 at the end
|
---|
| 857 | * end of entry name, so we copy it to new buffer
|
---|
| 858 | * and add the \0 at the end
|
---|
| 859 | */
|
---|
[df38657] | 860 | buf = malloc(name_size+1);
|
---|
| 861 | if (buf == NULL) {
|
---|
| 862 | ext2_directory_iterator_fini(&it);
|
---|
| 863 | async_answer_0(callid, ENOMEM);
|
---|
| 864 | async_answer_0(rid, ENOMEM);
|
---|
| 865 | return;
|
---|
| 866 | }
|
---|
| 867 | memcpy(buf, &it.current->name, name_size);
|
---|
| 868 | *(buf+name_size) = 0;
|
---|
| 869 | found = true;
|
---|
| 870 | (void) async_data_read_finalize(callid, buf, name_size+1);
|
---|
| 871 | free(buf);
|
---|
| 872 | break;
|
---|
| 873 | }
|
---|
| 874 | cur++;
|
---|
| 875 |
|
---|
| 876 | skip:
|
---|
[b83e16ff] | 877 | rc = ext2_directory_iterator_next(&it);
|
---|
[4dc6a32] | 878 | if (rc != EOK) {
|
---|
[b83e16ff] | 879 | ext2_directory_iterator_fini(&it);
|
---|
| 880 | async_answer_0(callid, rc);
|
---|
| 881 | async_answer_0(rid, rc);
|
---|
[4dc6a32] | 882 | return;
|
---|
| 883 | }
|
---|
| 884 | }
|
---|
| 885 |
|
---|
[b83e16ff] | 886 | rc = ext2_directory_iterator_fini(&it);
|
---|
| 887 | if (rc != EOK) {
|
---|
[df38657] | 888 | async_answer_0(rid, rc);
|
---|
[b83e16ff] | 889 | return;
|
---|
| 890 | }
|
---|
| 891 |
|
---|
[df38657] | 892 | if (found) {
|
---|
| 893 | async_answer_1(rid, EOK, 1);
|
---|
| 894 | }
|
---|
| 895 | else {
|
---|
| 896 | async_answer_0(callid, ENOENT);
|
---|
| 897 | async_answer_0(rid, ENOENT);
|
---|
| 898 | }
|
---|
[796c276] | 899 | }
|
---|
| 900 |
|
---|
[529edc66] | 901 | void ext2fs_read_file(ipc_callid_t rid, ipc_callid_t callid, aoff64_t pos,
|
---|
| 902 | size_t size, ext2fs_instance_t *inst, ext2_inode_ref_t *inode_ref)
|
---|
| 903 | {
|
---|
| 904 | int rc;
|
---|
| 905 | uint32_t block_size;
|
---|
| 906 | aoff64_t file_block;
|
---|
| 907 | uint64_t file_size;
|
---|
| 908 | uint32_t fs_block;
|
---|
| 909 | size_t offset_in_block;
|
---|
| 910 | size_t bytes;
|
---|
| 911 | block_t *block;
|
---|
[7ea69a6] | 912 | uint8_t *buffer;
|
---|
[529edc66] | 913 |
|
---|
| 914 | file_size = ext2_inode_get_size(inst->filesystem->superblock,
|
---|
| 915 | inode_ref->inode);
|
---|
| 916 |
|
---|
| 917 | if (pos >= file_size) {
|
---|
[1a86c50] | 918 | /* Read 0 bytes successfully */
|
---|
[529edc66] | 919 | async_data_read_finalize(callid, NULL, 0);
|
---|
| 920 | async_answer_1(rid, EOK, 0);
|
---|
| 921 | return;
|
---|
| 922 | }
|
---|
| 923 |
|
---|
[1a86c50] | 924 | /* For now, we only read data from one block at a time */
|
---|
[529edc66] | 925 | block_size = ext2_superblock_get_block_size(inst->filesystem->superblock);
|
---|
| 926 | file_block = pos / block_size;
|
---|
| 927 | offset_in_block = pos % block_size;
|
---|
| 928 | bytes = min(block_size - offset_in_block, size);
|
---|
| 929 |
|
---|
[1a86c50] | 930 | /* Handle end of file */
|
---|
[7ea69a6] | 931 | if (pos + bytes > file_size) {
|
---|
| 932 | bytes = file_size - pos;
|
---|
| 933 | }
|
---|
| 934 |
|
---|
[1a86c50] | 935 | /* Get the real block number */
|
---|
[529edc66] | 936 | rc = ext2_filesystem_get_inode_data_block_index(inst->filesystem,
|
---|
| 937 | inode_ref->inode, file_block, &fs_block);
|
---|
| 938 | if (rc != EOK) {
|
---|
| 939 | async_answer_0(callid, rc);
|
---|
| 940 | async_answer_0(rid, rc);
|
---|
| 941 | return;
|
---|
| 942 | }
|
---|
| 943 |
|
---|
[1a86c50] | 944 | /* Check for sparse file
|
---|
| 945 | * If ext2_filesystem_get_inode_data_block_index returned
|
---|
| 946 | * fs_block == 0, it means that the given block is not allocated for the
|
---|
| 947 | * file and we need to return a buffer of zeros
|
---|
| 948 | */
|
---|
[7ea69a6] | 949 | if (fs_block == 0) {
|
---|
| 950 | buffer = malloc(bytes);
|
---|
| 951 | if (buffer == NULL) {
|
---|
| 952 | async_answer_0(callid, ENOMEM);
|
---|
| 953 | async_answer_0(rid, ENOMEM);
|
---|
| 954 | return;
|
---|
| 955 | }
|
---|
| 956 |
|
---|
| 957 | memset(buffer, 0, bytes);
|
---|
| 958 |
|
---|
| 959 | async_data_read_finalize(callid, buffer, bytes);
|
---|
| 960 | async_answer_1(rid, EOK, bytes);
|
---|
| 961 |
|
---|
| 962 | free(buffer);
|
---|
| 963 |
|
---|
| 964 | return;
|
---|
| 965 | }
|
---|
| 966 |
|
---|
[1a86c50] | 967 | /* Usual case - we need to read a block from device */
|
---|
[529edc66] | 968 | rc = block_get(&block, inst->devmap_handle, fs_block, BLOCK_FLAGS_NONE);
|
---|
| 969 | if (rc != EOK) {
|
---|
| 970 | async_answer_0(callid, rc);
|
---|
| 971 | async_answer_0(rid, rc);
|
---|
| 972 | return;
|
---|
| 973 | }
|
---|
| 974 |
|
---|
[7ea69a6] | 975 | assert(offset_in_block + bytes <= block_size);
|
---|
| 976 | async_data_read_finalize(callid, block->data + offset_in_block, bytes);
|
---|
[529edc66] | 977 |
|
---|
| 978 | rc = block_put(block);
|
---|
| 979 | if (rc != EOK) {
|
---|
| 980 | async_answer_0(rid, rc);
|
---|
| 981 | return;
|
---|
| 982 | }
|
---|
| 983 |
|
---|
| 984 | async_answer_1(rid, EOK, bytes);
|
---|
| 985 | }
|
---|
| 986 |
|
---|
[796c276] | 987 | void ext2fs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 988 | {
|
---|
[4dc6a32] | 989 | EXT2FS_DBG("");
|
---|
[796c276] | 990 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 991 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 992 | // aoff64_t pos =
|
---|
| 993 | // (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
| 994 |
|
---|
| 995 | // TODO
|
---|
| 996 | async_answer_0(rid, ENOTSUP);
|
---|
| 997 | }
|
---|
| 998 |
|
---|
| 999 | void ext2fs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1000 | {
|
---|
[4dc6a32] | 1001 | EXT2FS_DBG("");
|
---|
[796c276] | 1002 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 1003 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 1004 | // aoff64_t size =
|
---|
| 1005 | // (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
| 1006 |
|
---|
| 1007 | // TODO
|
---|
| 1008 | async_answer_0(rid, ENOTSUP);
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | void ext2fs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1012 | {
|
---|
[4dc6a32] | 1013 | EXT2FS_DBG("");
|
---|
[796c276] | 1014 | async_answer_0(rid, EOK);
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
| 1017 | void ext2fs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1018 | {
|
---|
[4dc6a32] | 1019 | EXT2FS_DBG("");
|
---|
[796c276] | 1020 | // devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
|
---|
| 1021 | // fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 1022 |
|
---|
| 1023 | // TODO
|
---|
| 1024 | async_answer_0(rid, ENOTSUP);
|
---|
| 1025 | }
|
---|
| 1026 |
|
---|
| 1027 | void ext2fs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1028 | {
|
---|
[4dc6a32] | 1029 | EXT2FS_DBG("");
|
---|
[796c276] | 1030 | libfs_open_node(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
| 1031 | }
|
---|
| 1032 |
|
---|
| 1033 | void ext2fs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1034 | {
|
---|
[4dc6a32] | 1035 | EXT2FS_DBG("");
|
---|
[796c276] | 1036 | libfs_stat(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
| 1039 | void ext2fs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1040 | {
|
---|
[4dc6a32] | 1041 | EXT2FS_DBG("");
|
---|
[796c276] | 1042 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 1043 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 1044 |
|
---|
| 1045 | // TODO
|
---|
| 1046 | async_answer_0(rid, ENOTSUP);
|
---|
| 1047 | }
|
---|
| 1048 |
|
---|
| 1049 | /**
|
---|
| 1050 | * @}
|
---|
| 1051 | */
|
---|