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