[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>
|
---|
| 58 |
|
---|
[4dc6a32] | 59 | #define EXT2FS_NODE(node) ((node) ? (ext2fs_node_t *) (node)->data : NULL)
|
---|
| 60 | #define EXT2FS_DBG(format, ...) {if (false) printf("ext2fs: %s: " format "\n", __FUNCTION__, ##__VA_ARGS__);}
|
---|
[796c276] | 61 |
|
---|
[4dc6a32] | 62 | typedef struct ext2fs_instance {
|
---|
| 63 | link_t link;
|
---|
| 64 | devmap_handle_t devmap_handle;
|
---|
| 65 | ext2_filesystem_t *filesystem;
|
---|
| 66 | } ext2fs_instance_t;
|
---|
| 67 |
|
---|
| 68 | typedef struct ext2fs_node {
|
---|
| 69 | ext2fs_instance_t *instance;
|
---|
| 70 | ext2_inode_ref_t *inode_ref;
|
---|
| 71 | } ext2fs_node_t;
|
---|
| 72 |
|
---|
[e2abab03] | 73 | /*
|
---|
| 74 | * Forward declarations of auxiliary functions
|
---|
| 75 | */
|
---|
[4dc6a32] | 76 | static int ext2fs_instance_get(devmap_handle_t, ext2fs_instance_t **);
|
---|
[b83e16ff] | 77 | static void ext2fs_read_directory(ipc_callid_t, ipc_callid_t, aoff64_t,
|
---|
| 78 | size_t, ext2fs_instance_t *, ext2_inode_ref_t *);
|
---|
[529edc66] | 79 | static void ext2fs_read_file(ipc_callid_t, ipc_callid_t, aoff64_t,
|
---|
| 80 | size_t, ext2fs_instance_t *, ext2_inode_ref_t *);
|
---|
[84226f0] | 81 | static bool ext2fs_is_dots(const uint8_t *, size_t);
|
---|
[e2abab03] | 82 | static int ext2fs_node_get_core(fs_node_t **, ext2fs_instance_t *, fs_index_t);
|
---|
[796c276] | 83 |
|
---|
| 84 | /*
|
---|
| 85 | * Forward declarations of EXT2 libfs operations.
|
---|
| 86 | */
|
---|
| 87 | static int ext2fs_root_get(fs_node_t **, devmap_handle_t);
|
---|
| 88 | static int ext2fs_match(fs_node_t **, fs_node_t *, const char *);
|
---|
| 89 | static int ext2fs_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
|
---|
| 90 | static int ext2fs_node_open(fs_node_t *);
|
---|
| 91 | static int ext2fs_node_put(fs_node_t *);
|
---|
| 92 | static int ext2fs_create_node(fs_node_t **, devmap_handle_t, int);
|
---|
| 93 | static int ext2fs_destroy_node(fs_node_t *);
|
---|
| 94 | static int ext2fs_link(fs_node_t *, fs_node_t *, const char *);
|
---|
| 95 | static int ext2fs_unlink(fs_node_t *, fs_node_t *, const char *);
|
---|
| 96 | static int ext2fs_has_children(bool *, fs_node_t *);
|
---|
| 97 | static fs_index_t ext2fs_index_get(fs_node_t *);
|
---|
| 98 | static aoff64_t ext2fs_size_get(fs_node_t *);
|
---|
| 99 | static unsigned ext2fs_lnkcnt_get(fs_node_t *);
|
---|
| 100 | static char ext2fs_plb_get_char(unsigned);
|
---|
| 101 | static bool ext2fs_is_directory(fs_node_t *);
|
---|
| 102 | static bool ext2fs_is_file(fs_node_t *node);
|
---|
| 103 | static devmap_handle_t ext2fs_device_get(fs_node_t *node);
|
---|
| 104 |
|
---|
| 105 | /*
|
---|
| 106 | * Static variables
|
---|
| 107 | */
|
---|
[4dc6a32] | 108 | static LIST_INITIALIZE(instance_list);
|
---|
[a210bc7] | 109 | static FIBRIL_MUTEX_INITIALIZE(instance_list_mutex);
|
---|
[796c276] | 110 |
|
---|
| 111 | /**
|
---|
| 112 | *
|
---|
| 113 | */
|
---|
| 114 | int ext2fs_global_init(void)
|
---|
| 115 | {
|
---|
[4dc6a32] | 116 | return EOK;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | int ext2fs_global_fini(void)
|
---|
| 120 | {
|
---|
[796c276] | 121 | return EOK;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | /*
|
---|
| 127 | * EXT2 libfs operations.
|
---|
| 128 | */
|
---|
| 129 |
|
---|
[4dc6a32] | 130 | /**
|
---|
| 131 | * Find an instance of filesystem for the given devmap_handle
|
---|
| 132 | */
|
---|
| 133 | int ext2fs_instance_get(devmap_handle_t devmap_handle, ext2fs_instance_t **inst)
|
---|
| 134 | {
|
---|
| 135 | EXT2FS_DBG("(%u, -)", devmap_handle);
|
---|
| 136 | link_t *link;
|
---|
| 137 | ext2fs_instance_t *tmp;
|
---|
[a210bc7] | 138 |
|
---|
| 139 | fibril_mutex_lock(&instance_list_mutex);
|
---|
[4dc6a32] | 140 |
|
---|
| 141 | if (list_empty(&instance_list)) {
|
---|
| 142 | EXT2FS_DBG("list empty");
|
---|
[a210bc7] | 143 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
[4dc6a32] | 144 | return EINVAL;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | for (link = instance_list.next; link != &instance_list; link = link->next) {
|
---|
| 148 | tmp = list_get_instance(link, ext2fs_instance_t, link);
|
---|
| 149 |
|
---|
| 150 | if (tmp->devmap_handle == devmap_handle) {
|
---|
| 151 | *inst = tmp;
|
---|
[a210bc7] | 152 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
[4dc6a32] | 153 | return EOK;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | EXT2FS_DBG("not found");
|
---|
[a210bc7] | 158 |
|
---|
| 159 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
[4dc6a32] | 160 | return EINVAL;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 |
|
---|
[796c276] | 165 | int ext2fs_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
---|
| 166 | {
|
---|
[4dc6a32] | 167 | EXT2FS_DBG("(-, %u)", devmap_handle);
|
---|
| 168 | return ext2fs_node_get(rfn, devmap_handle, EXT2_INODE_ROOT_INDEX);
|
---|
[796c276] | 169 | }
|
---|
| 170 |
|
---|
| 171 | int ext2fs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
| 172 | {
|
---|
[4dc6a32] | 173 | EXT2FS_DBG("(-,-,%s)", component);
|
---|
| 174 | ext2fs_node_t *eparent = EXT2FS_NODE(pfn);
|
---|
| 175 | ext2_filesystem_t *fs;
|
---|
| 176 | ext2_directory_iterator_t it;
|
---|
| 177 | int rc;
|
---|
| 178 | size_t name_size;
|
---|
| 179 | size_t component_size;
|
---|
| 180 | bool found = false;
|
---|
| 181 |
|
---|
| 182 | fs = eparent->instance->filesystem;
|
---|
| 183 |
|
---|
| 184 | if (!ext2_inode_is_type(fs->superblock, eparent->inode_ref->inode,
|
---|
| 185 | EXT2_INODE_MODE_DIRECTORY)) {
|
---|
| 186 | return ENOTDIR;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | rc = ext2_directory_iterator_init(&it, fs, eparent->inode_ref);
|
---|
| 190 | if (rc != EOK) {
|
---|
| 191 | return rc;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | // Find length of component in bytes
|
---|
| 195 | // TODO: check for library function call that does this
|
---|
| 196 | component_size = 0;
|
---|
| 197 | while (*(component+component_size) != 0) {
|
---|
| 198 | component_size++;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | while (it.current != NULL) {
|
---|
| 202 | // ignore empty directory entries
|
---|
| 203 | if (it.current->inode != 0) {
|
---|
| 204 | name_size = ext2_directory_entry_ll_get_name_length(fs->superblock,
|
---|
| 205 | it.current);
|
---|
| 206 |
|
---|
| 207 | if (name_size == component_size && bcmp(component, &it.current->name,
|
---|
| 208 | name_size) == 0) {
|
---|
[e2abab03] | 209 | rc = ext2fs_node_get_core(rfn, eparent->instance,
|
---|
[4dc6a32] | 210 | it.current->inode);
|
---|
| 211 | if (rc != EOK) {
|
---|
| 212 | ext2_directory_iterator_fini(&it);
|
---|
| 213 | return rc;
|
---|
| 214 | }
|
---|
| 215 | found = true;
|
---|
| 216 | break;
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | rc = ext2_directory_iterator_next(&it);
|
---|
| 221 | if (rc != EOK) {
|
---|
| 222 | ext2_directory_iterator_fini(&it);
|
---|
| 223 | return rc;
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | ext2_directory_iterator_fini(&it);
|
---|
| 228 |
|
---|
| 229 | if (!found) {
|
---|
| 230 | return ENOENT;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | return EOK;
|
---|
[796c276] | 234 | }
|
---|
| 235 |
|
---|
| 236 | /** Instantiate a EXT2 in-core node. */
|
---|
| 237 | int ext2fs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
|
---|
| 238 | {
|
---|
[4dc6a32] | 239 | EXT2FS_DBG("(-,%u,%u)", devmap_handle, index);
|
---|
[e2abab03] | 240 |
|
---|
| 241 | ext2fs_instance_t *inst = NULL;
|
---|
| 242 | int rc;
|
---|
| 243 |
|
---|
| 244 | rc = ext2fs_instance_get(devmap_handle, &inst);
|
---|
| 245 | if (rc != EOK) {
|
---|
| 246 | return rc;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | return ext2fs_node_get_core(rfn, inst, index);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | int ext2fs_node_get_core(fs_node_t **rfn, ext2fs_instance_t *inst,
|
---|
| 253 | fs_index_t index)
|
---|
| 254 | {
|
---|
[4dc6a32] | 255 | int rc;
|
---|
| 256 | fs_node_t *node = NULL;
|
---|
| 257 | ext2fs_node_t *enode = NULL;
|
---|
[e2abab03] | 258 |
|
---|
[4dc6a32] | 259 | ext2_inode_ref_t *inode_ref = NULL;
|
---|
| 260 |
|
---|
| 261 | enode = malloc(sizeof(ext2fs_node_t));
|
---|
| 262 | if (enode == NULL) {
|
---|
| 263 | return ENOMEM;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | node = malloc(sizeof(fs_node_t));
|
---|
| 267 | if (node == NULL) {
|
---|
| 268 | free(enode);
|
---|
| 269 | return ENOMEM;
|
---|
[e2abab03] | 270 | }
|
---|
[4dc6a32] | 271 | fs_node_initialize(node);
|
---|
| 272 |
|
---|
| 273 | rc = ext2_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
|
---|
| 274 | if (rc != EOK) {
|
---|
| 275 | free(enode);
|
---|
| 276 | free(node);
|
---|
| 277 | return rc;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | enode->inode_ref = inode_ref;
|
---|
| 281 | enode->instance = inst;
|
---|
| 282 | node->data = enode;
|
---|
| 283 | *rfn = node;
|
---|
| 284 |
|
---|
| 285 | EXT2FS_DBG("inode: %u", inode_ref->index);
|
---|
| 286 |
|
---|
| 287 | EXT2FS_DBG("EOK");
|
---|
| 288 |
|
---|
| 289 | return EOK;
|
---|
[796c276] | 290 | }
|
---|
| 291 |
|
---|
| 292 | int ext2fs_node_open(fs_node_t *fn)
|
---|
| 293 | {
|
---|
[4dc6a32] | 294 | EXT2FS_DBG("");
|
---|
[796c276] | 295 | /*
|
---|
| 296 | * Opening a file is stateless, nothing
|
---|
| 297 | * to be done here.
|
---|
| 298 | */
|
---|
| 299 | return EOK;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | int ext2fs_node_put(fs_node_t *fn)
|
---|
| 303 | {
|
---|
[4dc6a32] | 304 | EXT2FS_DBG("");
|
---|
| 305 | int rc;
|
---|
| 306 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 307 | rc = ext2_filesystem_put_inode_ref(enode->inode_ref);
|
---|
| 308 | if (rc != EOK) {
|
---|
| 309 | EXT2FS_DBG("ext2_filesystem_put_inode_ref failed");
|
---|
| 310 | }
|
---|
| 311 | return rc;
|
---|
[796c276] | 312 | }
|
---|
| 313 |
|
---|
| 314 | int ext2fs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
|
---|
| 315 | {
|
---|
[4dc6a32] | 316 | EXT2FS_DBG("");
|
---|
[796c276] | 317 | // TODO
|
---|
| 318 | return ENOTSUP;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | int ext2fs_destroy_node(fs_node_t *fn)
|
---|
| 322 | {
|
---|
[4dc6a32] | 323 | EXT2FS_DBG("");
|
---|
[796c276] | 324 | // TODO
|
---|
| 325 | return ENOTSUP;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | int ext2fs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
---|
| 329 | {
|
---|
[4dc6a32] | 330 | EXT2FS_DBG("");
|
---|
[796c276] | 331 | // TODO
|
---|
| 332 | return ENOTSUP;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | int ext2fs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
| 336 | {
|
---|
[4dc6a32] | 337 | EXT2FS_DBG("");
|
---|
[796c276] | 338 | // TODO
|
---|
| 339 | return ENOTSUP;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | int ext2fs_has_children(bool *has_children, fs_node_t *fn)
|
---|
| 343 | {
|
---|
[4dc6a32] | 344 | EXT2FS_DBG("");
|
---|
| 345 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 346 | ext2_directory_iterator_t it;
|
---|
| 347 | ext2_filesystem_t *fs;
|
---|
| 348 | int rc;
|
---|
| 349 | bool found = false;
|
---|
[84226f0] | 350 | size_t name_size;
|
---|
[4dc6a32] | 351 |
|
---|
| 352 | fs = enode->instance->filesystem;
|
---|
| 353 |
|
---|
| 354 | if (!ext2_inode_is_type(fs->superblock, enode->inode_ref->inode,
|
---|
| 355 | EXT2_INODE_MODE_DIRECTORY)) {
|
---|
| 356 | *has_children = false;
|
---|
| 357 | EXT2FS_DBG("EOK - false");
|
---|
| 358 | return EOK;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | rc = ext2_directory_iterator_init(&it, fs, enode->inode_ref);
|
---|
| 362 | if (rc != EOK) {
|
---|
| 363 | EXT2FS_DBG("error %u", rc);
|
---|
| 364 | return rc;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | // Find a non-empty directory entry
|
---|
| 368 | while (it.current != NULL) {
|
---|
| 369 | if (it.current->inode != 0) {
|
---|
[84226f0] | 370 | name_size = ext2_directory_entry_ll_get_name_length(fs->superblock,
|
---|
| 371 | it.current);
|
---|
| 372 | if (!ext2fs_is_dots(&it.current->name, name_size)) {
|
---|
| 373 | found = true;
|
---|
| 374 | break;
|
---|
| 375 | }
|
---|
[4dc6a32] | 376 | }
|
---|
| 377 |
|
---|
| 378 | rc = ext2_directory_iterator_next(&it);
|
---|
| 379 | if (rc != EOK) {
|
---|
| 380 | ext2_directory_iterator_fini(&it);
|
---|
| 381 | EXT2FS_DBG("error %u", rc);
|
---|
| 382 | return rc;
|
---|
| 383 | }
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | rc = ext2_directory_iterator_fini(&it);
|
---|
| 387 | if (rc != EOK) {
|
---|
| 388 | EXT2FS_DBG("error %u", rc);
|
---|
| 389 | return rc;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | *has_children = found;
|
---|
| 393 | EXT2FS_DBG("EOK");
|
---|
| 394 |
|
---|
| 395 | return EOK;
|
---|
[796c276] | 396 | }
|
---|
| 397 |
|
---|
| 398 |
|
---|
| 399 | fs_index_t ext2fs_index_get(fs_node_t *fn)
|
---|
| 400 | {
|
---|
[4dc6a32] | 401 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 402 | EXT2FS_DBG("%u", enode->inode_ref->index);
|
---|
| 403 | return enode->inode_ref->index;
|
---|
[796c276] | 404 | }
|
---|
| 405 |
|
---|
| 406 | aoff64_t ext2fs_size_get(fs_node_t *fn)
|
---|
| 407 | {
|
---|
[4dc6a32] | 408 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 409 | aoff64_t size = ext2_inode_get_size(enode->instance->filesystem->superblock,
|
---|
| 410 | enode->inode_ref->inode);
|
---|
| 411 | EXT2FS_DBG("%" PRIu64, size);
|
---|
| 412 | return size;
|
---|
[796c276] | 413 | }
|
---|
| 414 |
|
---|
| 415 | unsigned ext2fs_lnkcnt_get(fs_node_t *fn)
|
---|
| 416 | {
|
---|
[4dc6a32] | 417 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 418 | unsigned count = ext2_inode_get_usage_count(enode->inode_ref->inode);
|
---|
| 419 | EXT2FS_DBG("%u", count);
|
---|
| 420 | return count;
|
---|
[796c276] | 421 | }
|
---|
| 422 |
|
---|
| 423 | char ext2fs_plb_get_char(unsigned pos)
|
---|
| 424 | {
|
---|
| 425 | return ext2fs_reg.plb_ro[pos % PLB_SIZE];
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | bool ext2fs_is_directory(fs_node_t *fn)
|
---|
| 429 | {
|
---|
[4dc6a32] | 430 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 431 | bool is_dir = ext2_inode_is_type(enode->instance->filesystem->superblock,
|
---|
| 432 | enode->inode_ref->inode, EXT2_INODE_MODE_DIRECTORY);
|
---|
| 433 | EXT2FS_DBG("%s", is_dir ? "true" : "false");
|
---|
| 434 | EXT2FS_DBG("%u", enode->inode_ref->index);
|
---|
| 435 | return is_dir;
|
---|
[796c276] | 436 | }
|
---|
| 437 |
|
---|
| 438 | bool ext2fs_is_file(fs_node_t *fn)
|
---|
| 439 | {
|
---|
[4dc6a32] | 440 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 441 | bool is_file = ext2_inode_is_type(enode->instance->filesystem->superblock,
|
---|
| 442 | enode->inode_ref->inode, EXT2_INODE_MODE_FILE);
|
---|
| 443 | EXT2FS_DBG("%s", is_file ? "true" : "false");
|
---|
| 444 | return is_file;
|
---|
[796c276] | 445 | }
|
---|
| 446 |
|
---|
[4dc6a32] | 447 | devmap_handle_t ext2fs_device_get(fs_node_t *fn)
|
---|
[796c276] | 448 | {
|
---|
[4dc6a32] | 449 | EXT2FS_DBG("");
|
---|
| 450 | ext2fs_node_t *enode = EXT2FS_NODE(fn);
|
---|
| 451 | return enode->instance->devmap_handle;
|
---|
[796c276] | 452 | }
|
---|
| 453 |
|
---|
| 454 | /** libfs operations */
|
---|
| 455 | libfs_ops_t ext2fs_libfs_ops = {
|
---|
| 456 | .root_get = ext2fs_root_get,
|
---|
| 457 | .match = ext2fs_match,
|
---|
| 458 | .node_get = ext2fs_node_get,
|
---|
| 459 | .node_open = ext2fs_node_open,
|
---|
| 460 | .node_put = ext2fs_node_put,
|
---|
| 461 | .create = ext2fs_create_node,
|
---|
| 462 | .destroy = ext2fs_destroy_node,
|
---|
| 463 | .link = ext2fs_link,
|
---|
| 464 | .unlink = ext2fs_unlink,
|
---|
| 465 | .has_children = ext2fs_has_children,
|
---|
| 466 | .index_get = ext2fs_index_get,
|
---|
| 467 | .size_get = ext2fs_size_get,
|
---|
| 468 | .lnkcnt_get = ext2fs_lnkcnt_get,
|
---|
| 469 | .plb_get_char = ext2fs_plb_get_char,
|
---|
| 470 | .is_directory = ext2fs_is_directory,
|
---|
| 471 | .is_file = ext2fs_is_file,
|
---|
| 472 | .device_get = ext2fs_device_get
|
---|
| 473 | };
|
---|
| 474 |
|
---|
| 475 | /*
|
---|
| 476 | * VFS operations.
|
---|
| 477 | */
|
---|
| 478 |
|
---|
| 479 | void ext2fs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 480 | {
|
---|
[4dc6a32] | 481 | EXT2FS_DBG("");
|
---|
[796c276] | 482 | int rc;
|
---|
| 483 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 484 | ext2_filesystem_t *fs;
|
---|
[4dc6a32] | 485 | ext2fs_instance_t *inst;
|
---|
| 486 |
|
---|
[796c276] | 487 | /* Accept the mount options */
|
---|
| 488 | char *opts;
|
---|
| 489 | rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
|
---|
| 490 |
|
---|
| 491 | if (rc != EOK) {
|
---|
| 492 | async_answer_0(rid, rc);
|
---|
| 493 | return;
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | free(opts);
|
---|
| 497 |
|
---|
| 498 | /* Allocate libext2 filesystem structure */
|
---|
| 499 | fs = (ext2_filesystem_t *) malloc(sizeof(ext2_filesystem_t));
|
---|
| 500 | if (fs == NULL) {
|
---|
| 501 | async_answer_0(rid, ENOMEM);
|
---|
| 502 | return;
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[4dc6a32] | 505 | /* Allocate instance structure */
|
---|
| 506 | inst = (ext2fs_instance_t *) malloc(sizeof(ext2fs_instance_t));
|
---|
| 507 | if (inst == NULL) {
|
---|
| 508 | free(fs);
|
---|
| 509 | async_answer_0(rid, ENOMEM);
|
---|
| 510 | return;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[796c276] | 513 | /* Initialize the filesystem */
|
---|
| 514 | rc = ext2_filesystem_init(fs, devmap_handle);
|
---|
| 515 | if (rc != EOK) {
|
---|
[4dc6a32] | 516 | free(fs);
|
---|
| 517 | free(inst);
|
---|
[796c276] | 518 | async_answer_0(rid, rc);
|
---|
| 519 | return;
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | /* Do some sanity checking */
|
---|
| 523 | rc = ext2_filesystem_check_sanity(fs);
|
---|
| 524 | if (rc != EOK) {
|
---|
| 525 | ext2_filesystem_fini(fs);
|
---|
[4dc6a32] | 526 | free(fs);
|
---|
| 527 | free(inst);
|
---|
[796c276] | 528 | async_answer_0(rid, rc);
|
---|
| 529 | return;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
[4dc6a32] | 532 | /* Initialize instance and add to the list */
|
---|
| 533 | link_initialize(&inst->link);
|
---|
| 534 | inst->devmap_handle = devmap_handle;
|
---|
| 535 | inst->filesystem = fs;
|
---|
[a210bc7] | 536 |
|
---|
| 537 | fibril_mutex_lock(&instance_list_mutex);
|
---|
[4dc6a32] | 538 | list_append(&inst->link, &instance_list);
|
---|
[a210bc7] | 539 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
[796c276] | 540 |
|
---|
[4dc6a32] | 541 | async_answer_0(rid, EOK);
|
---|
[796c276] | 542 | }
|
---|
| 543 |
|
---|
| 544 | void ext2fs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 545 | {
|
---|
[4dc6a32] | 546 | EXT2FS_DBG("");
|
---|
[796c276] | 547 | libfs_mount(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | void ext2fs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 551 | {
|
---|
[4dc6a32] | 552 | EXT2FS_DBG("");
|
---|
| 553 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 554 | ext2fs_instance_t *inst;
|
---|
| 555 | int rc;
|
---|
| 556 |
|
---|
| 557 | rc = ext2fs_instance_get(devmap_handle, &inst);
|
---|
| 558 |
|
---|
| 559 | if (rc != EOK) {
|
---|
| 560 | async_answer_0(rid, rc);
|
---|
| 561 | return;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | // TODO: check if the fs is busy
|
---|
| 565 |
|
---|
| 566 | // Remove the instance from list
|
---|
[a210bc7] | 567 | fibril_mutex_lock(&instance_list_mutex);
|
---|
[4dc6a32] | 568 | list_remove(&inst->link);
|
---|
[a210bc7] | 569 | fibril_mutex_unlock(&instance_list_mutex);
|
---|
| 570 |
|
---|
[4dc6a32] | 571 | ext2_filesystem_fini(inst->filesystem);
|
---|
| 572 |
|
---|
| 573 | async_answer_0(rid, EOK);
|
---|
[796c276] | 574 | }
|
---|
| 575 |
|
---|
| 576 | void ext2fs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 577 | {
|
---|
[4dc6a32] | 578 | EXT2FS_DBG("");
|
---|
[796c276] | 579 | libfs_unmount(&ext2fs_libfs_ops, rid, request);
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | void ext2fs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 583 | {
|
---|
[4dc6a32] | 584 | EXT2FS_DBG("");
|
---|
[796c276] | 585 | libfs_lookup(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | void ext2fs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 589 | {
|
---|
[4dc6a32] | 590 | EXT2FS_DBG("");
|
---|
| 591 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 592 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 593 | aoff64_t pos =
|
---|
| 594 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
[796c276] | 595 |
|
---|
[4dc6a32] | 596 | ext2fs_instance_t *inst;
|
---|
| 597 | ext2_inode_ref_t *inode_ref;
|
---|
| 598 | int rc;
|
---|
[b83e16ff] | 599 |
|
---|
[4dc6a32] | 600 | /*
|
---|
| 601 | * Receive the read request.
|
---|
| 602 | */
|
---|
| 603 | ipc_callid_t callid;
|
---|
| 604 | size_t size;
|
---|
| 605 | if (!async_data_read_receive(&callid, &size)) {
|
---|
| 606 | async_answer_0(callid, EINVAL);
|
---|
| 607 | async_answer_0(rid, EINVAL);
|
---|
| 608 | return;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | rc = ext2fs_instance_get(devmap_handle, &inst);
|
---|
| 612 | if (rc != EOK) {
|
---|
| 613 | async_answer_0(callid, rc);
|
---|
| 614 | async_answer_0(rid, rc);
|
---|
| 615 | return;
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | rc = ext2_filesystem_get_inode_ref(inst->filesystem, index, &inode_ref);
|
---|
| 619 | if (rc != EOK) {
|
---|
| 620 | async_answer_0(callid, rc);
|
---|
| 621 | async_answer_0(rid, rc);
|
---|
| 622 | return;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | if (ext2_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
|
---|
| 626 | EXT2_INODE_MODE_FILE)) {
|
---|
[529edc66] | 627 | ext2fs_read_file(rid, callid, pos, size, inst, inode_ref);
|
---|
[4dc6a32] | 628 | }
|
---|
| 629 | else if (ext2_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
|
---|
| 630 | EXT2_INODE_MODE_DIRECTORY)) {
|
---|
[b83e16ff] | 631 | ext2fs_read_directory(rid, callid, pos, size, inst, inode_ref);
|
---|
| 632 | }
|
---|
[df38657] | 633 | else {
|
---|
| 634 | // Other inode types not supported
|
---|
| 635 | async_answer_0(callid, ENOTSUP);
|
---|
| 636 | async_answer_0(rid, ENOTSUP);
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | ext2_filesystem_put_inode_ref(inode_ref);
|
---|
[b83e16ff] | 640 |
|
---|
| 641 | }
|
---|
| 642 |
|
---|
[84226f0] | 643 | /**
|
---|
| 644 | * Determine whether given directory entry name is . or ..
|
---|
| 645 | */
|
---|
| 646 | bool ext2fs_is_dots(const uint8_t *name, size_t name_size) {
|
---|
| 647 | if (name_size == 1 && name[0] == '.') {
|
---|
| 648 | return true;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | if (name_size == 2 && name[0] == '.' && name[1] == '.') {
|
---|
| 652 | return true;
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | return false;
|
---|
| 656 | }
|
---|
| 657 |
|
---|
[b83e16ff] | 658 | void ext2fs_read_directory(ipc_callid_t rid, ipc_callid_t callid, aoff64_t pos,
|
---|
| 659 | size_t size, ext2fs_instance_t *inst, ext2_inode_ref_t *inode_ref)
|
---|
| 660 | {
|
---|
| 661 | ext2_directory_iterator_t it;
|
---|
| 662 | aoff64_t cur;
|
---|
| 663 | uint8_t *buf;
|
---|
| 664 | size_t name_size;
|
---|
| 665 | int rc;
|
---|
[df38657] | 666 | bool found = false;
|
---|
[b83e16ff] | 667 |
|
---|
| 668 | rc = ext2_directory_iterator_init(&it, inst->filesystem, inode_ref);
|
---|
| 669 | if (rc != EOK) {
|
---|
| 670 | async_answer_0(callid, rc);
|
---|
| 671 | async_answer_0(rid, rc);
|
---|
| 672 | return;
|
---|
| 673 | }
|
---|
| 674 |
|
---|
[df38657] | 675 | // Find the index we want to read
|
---|
| 676 | // Note that we need to iterate and count as
|
---|
| 677 | // the underlying structure is a linked list
|
---|
| 678 | // Moreover, we want to skip . and .. entries
|
---|
| 679 | // as these are not used in HelenOS
|
---|
[b83e16ff] | 680 | cur = 0;
|
---|
| 681 | while (it.current != NULL) {
|
---|
[df38657] | 682 | if (it.current->inode == 0) {
|
---|
| 683 | goto skip;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 | name_size = ext2_directory_entry_ll_get_name_length(
|
---|
| 687 | inst->filesystem->superblock, it.current);
|
---|
| 688 |
|
---|
| 689 | // skip . and ..
|
---|
[84226f0] | 690 | if (ext2fs_is_dots(&it.current->name, name_size)) {
|
---|
| 691 | goto skip;
|
---|
[4dc6a32] | 692 | }
|
---|
| 693 |
|
---|
[df38657] | 694 | // Is this the dir entry we want to read?
|
---|
| 695 | if (cur == pos) {
|
---|
| 696 | // The on-disk entry does not contain \0 at the end
|
---|
| 697 | // end of entry name, so we copy it to new buffer
|
---|
| 698 | // and add the \0 at the end
|
---|
| 699 | buf = malloc(name_size+1);
|
---|
| 700 | if (buf == NULL) {
|
---|
| 701 | ext2_directory_iterator_fini(&it);
|
---|
| 702 | async_answer_0(callid, ENOMEM);
|
---|
| 703 | async_answer_0(rid, ENOMEM);
|
---|
| 704 | return;
|
---|
| 705 | }
|
---|
| 706 | memcpy(buf, &it.current->name, name_size);
|
---|
| 707 | *(buf+name_size) = 0;
|
---|
| 708 | found = true;
|
---|
| 709 | (void) async_data_read_finalize(callid, buf, name_size+1);
|
---|
| 710 | free(buf);
|
---|
| 711 | break;
|
---|
| 712 | }
|
---|
| 713 | cur++;
|
---|
| 714 |
|
---|
| 715 | skip:
|
---|
[b83e16ff] | 716 | rc = ext2_directory_iterator_next(&it);
|
---|
[4dc6a32] | 717 | if (rc != EOK) {
|
---|
[b83e16ff] | 718 | ext2_directory_iterator_fini(&it);
|
---|
| 719 | async_answer_0(callid, rc);
|
---|
| 720 | async_answer_0(rid, rc);
|
---|
[4dc6a32] | 721 | return;
|
---|
| 722 | }
|
---|
| 723 | }
|
---|
| 724 |
|
---|
[b83e16ff] | 725 | rc = ext2_directory_iterator_fini(&it);
|
---|
| 726 | if (rc != EOK) {
|
---|
[df38657] | 727 | async_answer_0(rid, rc);
|
---|
[b83e16ff] | 728 | return;
|
---|
| 729 | }
|
---|
| 730 |
|
---|
[df38657] | 731 | if (found) {
|
---|
| 732 | async_answer_1(rid, EOK, 1);
|
---|
| 733 | }
|
---|
| 734 | else {
|
---|
| 735 | async_answer_0(callid, ENOENT);
|
---|
| 736 | async_answer_0(rid, ENOENT);
|
---|
| 737 | }
|
---|
[796c276] | 738 | }
|
---|
| 739 |
|
---|
[529edc66] | 740 | void ext2fs_read_file(ipc_callid_t rid, ipc_callid_t callid, aoff64_t pos,
|
---|
| 741 | size_t size, ext2fs_instance_t *inst, ext2_inode_ref_t *inode_ref)
|
---|
| 742 | {
|
---|
| 743 | int rc;
|
---|
| 744 | uint32_t block_size;
|
---|
| 745 | aoff64_t file_block;
|
---|
| 746 | uint64_t file_size;
|
---|
| 747 | uint32_t fs_block;
|
---|
| 748 | size_t offset_in_block;
|
---|
| 749 | size_t bytes;
|
---|
| 750 | block_t *block;
|
---|
| 751 |
|
---|
| 752 | file_size = ext2_inode_get_size(inst->filesystem->superblock,
|
---|
| 753 | inode_ref->inode);
|
---|
| 754 |
|
---|
| 755 | if (pos >= file_size) {
|
---|
[242b4bb] | 756 | // Read 0 bytes successfully
|
---|
[529edc66] | 757 | async_data_read_finalize(callid, NULL, 0);
|
---|
| 758 | async_answer_1(rid, EOK, 0);
|
---|
| 759 | return;
|
---|
| 760 | }
|
---|
| 761 |
|
---|
| 762 | // For now, we only read data from one block at a time
|
---|
| 763 | block_size = ext2_superblock_get_block_size(inst->filesystem->superblock);
|
---|
| 764 | file_block = pos / block_size;
|
---|
| 765 | offset_in_block = pos % block_size;
|
---|
| 766 | bytes = min(block_size - offset_in_block, size);
|
---|
| 767 |
|
---|
| 768 | rc = ext2_filesystem_get_inode_data_block_index(inst->filesystem,
|
---|
| 769 | inode_ref->inode, file_block, &fs_block);
|
---|
| 770 | if (rc != EOK) {
|
---|
| 771 | async_answer_0(callid, rc);
|
---|
| 772 | async_answer_0(rid, rc);
|
---|
| 773 | return;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | rc = block_get(&block, inst->devmap_handle, fs_block, BLOCK_FLAGS_NONE);
|
---|
| 777 | if (rc != EOK) {
|
---|
| 778 | async_answer_0(callid, rc);
|
---|
| 779 | async_answer_0(rid, rc);
|
---|
| 780 | return;
|
---|
| 781 | }
|
---|
| 782 |
|
---|
| 783 | async_data_read_finalize(callid, block->data, bytes);
|
---|
| 784 |
|
---|
| 785 | rc = block_put(block);
|
---|
| 786 | if (rc != EOK) {
|
---|
| 787 | async_answer_0(rid, rc);
|
---|
| 788 | return;
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | async_answer_1(rid, EOK, bytes);
|
---|
| 792 | }
|
---|
| 793 |
|
---|
[796c276] | 794 | void ext2fs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 795 | {
|
---|
[4dc6a32] | 796 | EXT2FS_DBG("");
|
---|
[796c276] | 797 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 798 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 799 | // aoff64_t pos =
|
---|
| 800 | // (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
| 801 |
|
---|
| 802 | // TODO
|
---|
| 803 | async_answer_0(rid, ENOTSUP);
|
---|
| 804 | }
|
---|
| 805 |
|
---|
| 806 | void ext2fs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 807 | {
|
---|
[4dc6a32] | 808 | EXT2FS_DBG("");
|
---|
[796c276] | 809 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 810 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 811 | // aoff64_t size =
|
---|
| 812 | // (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
---|
| 813 |
|
---|
| 814 | // TODO
|
---|
| 815 | async_answer_0(rid, ENOTSUP);
|
---|
| 816 | }
|
---|
| 817 |
|
---|
| 818 | void ext2fs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 819 | {
|
---|
[4dc6a32] | 820 | EXT2FS_DBG("");
|
---|
[796c276] | 821 | async_answer_0(rid, EOK);
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | void ext2fs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 825 | {
|
---|
[4dc6a32] | 826 | EXT2FS_DBG("");
|
---|
[796c276] | 827 | // devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
|
---|
| 828 | // fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 829 |
|
---|
| 830 | // TODO
|
---|
| 831 | async_answer_0(rid, ENOTSUP);
|
---|
| 832 | }
|
---|
| 833 |
|
---|
| 834 | void ext2fs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 835 | {
|
---|
[4dc6a32] | 836 | EXT2FS_DBG("");
|
---|
[796c276] | 837 | libfs_open_node(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
| 838 | }
|
---|
| 839 |
|
---|
| 840 | void ext2fs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 841 | {
|
---|
[4dc6a32] | 842 | EXT2FS_DBG("");
|
---|
[796c276] | 843 | libfs_stat(&ext2fs_libfs_ops, ext2fs_reg.fs_handle, rid, request);
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 | void ext2fs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 847 | {
|
---|
[4dc6a32] | 848 | EXT2FS_DBG("");
|
---|
[796c276] | 849 | // devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
| 850 | // fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 851 |
|
---|
| 852 | // TODO
|
---|
| 853 | async_answer_0(rid, ENOTSUP);
|
---|
| 854 | }
|
---|
| 855 |
|
---|
| 856 | /**
|
---|
| 857 | * @}
|
---|
| 858 | */
|
---|