[d5cdffe] | 1 | /*
|
---|
[41a0d27] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
[d5cdffe] | 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 tmpfs_ops.c
|
---|
| 35 | * @brief Implementation of VFS operations for the TMPFS file system
|
---|
| 36 | * server.
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | #include "tmpfs.h"
|
---|
| 40 | #include "../../vfs/vfs.h"
|
---|
| 41 | #include <ipc/ipc.h>
|
---|
| 42 | #include <async.h>
|
---|
| 43 | #include <errno.h>
|
---|
[4b11571] | 44 | #include <atomic.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
| 46 | #include <string.h>
|
---|
| 47 | #include <stdio.h>
|
---|
[b8b23c8] | 48 | #include <dirent.h>
|
---|
[5973fd0] | 49 | #include <assert.h>
|
---|
[a4eb8a60] | 50 | #include <sys/types.h>
|
---|
| 51 | #include <libadt/hash_table.h>
|
---|
| 52 | #include <as.h>
|
---|
| 53 |
|
---|
| 54 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
| 55 | #define max(a, b) ((a) > (b) ? (a) : (b))
|
---|
[d5cdffe] | 56 |
|
---|
| 57 | #define PLB_GET_CHAR(i) (tmpfs_reg.plb_ro[(i) % PLB_SIZE])
|
---|
| 58 |
|
---|
[a4eb8a60] | 59 | #define DENTRIES_BUCKETS 256
|
---|
| 60 |
|
---|
[b5553a2] | 61 | #define TMPFS_GET_INDEX(x) (((tmpfs_dentry_t *)(x))->index)
|
---|
| 62 | #define TMPFS_GET_LNKCNT(x) 1
|
---|
| 63 |
|
---|
[fdb7795] | 64 | /* Forward declarations of static functions. */
|
---|
| 65 | static void *create_node(int);
|
---|
| 66 | static bool link_node(void *, void *, const char *);
|
---|
| 67 | static int unlink_node(void *);
|
---|
| 68 | static void destroy_node(void *);
|
---|
| 69 |
|
---|
| 70 | /** Hash table of all directory entries. */
|
---|
[a4eb8a60] | 71 | hash_table_t dentries;
|
---|
| 72 |
|
---|
| 73 | static hash_index_t dentries_hash(unsigned long *key)
|
---|
| 74 | {
|
---|
| 75 | return *key % DENTRIES_BUCKETS;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static int dentries_compare(unsigned long *key, hash_count_t keys,
|
---|
| 79 | link_t *item)
|
---|
| 80 | {
|
---|
| 81 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
|
---|
| 82 | dh_link);
|
---|
| 83 | return dentry->index == *key;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | static void dentries_remove_callback(link_t *item)
|
---|
| 87 | {
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /** TMPFS dentries hash table operations. */
|
---|
| 91 | hash_table_operations_t dentries_ops = {
|
---|
| 92 | .hash = dentries_hash,
|
---|
| 93 | .compare = dentries_compare,
|
---|
| 94 | .remove_callback = dentries_remove_callback
|
---|
| 95 | };
|
---|
| 96 |
|
---|
[4b11571] | 97 | unsigned tmpfs_next_index = 1;
|
---|
| 98 |
|
---|
| 99 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
|
---|
| 100 | {
|
---|
| 101 | dentry->index = 0;
|
---|
| 102 | dentry->parent = NULL;
|
---|
| 103 | dentry->sibling = NULL;
|
---|
| 104 | dentry->child = NULL;
|
---|
| 105 | dentry->name = NULL;
|
---|
| 106 | dentry->type = TMPFS_NONE;
|
---|
| 107 | dentry->size = 0;
|
---|
| 108 | dentry->data = NULL;
|
---|
[a4eb8a60] | 109 | link_initialize(&dentry->dh_link);
|
---|
[4b11571] | 110 | }
|
---|
| 111 |
|
---|
| 112 | /*
|
---|
| 113 | * For now, we don't distinguish between different dev_handles/instances. All
|
---|
| 114 | * requests resolve to the only instance, rooted in the following variable.
|
---|
| 115 | */
|
---|
| 116 | static tmpfs_dentry_t *root;
|
---|
| 117 |
|
---|
| 118 | static bool tmpfs_init(void)
|
---|
| 119 | {
|
---|
[a4eb8a60] | 120 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
|
---|
| 121 | return false;
|
---|
[fdb7795] | 122 | root = (tmpfs_dentry_t *) create_node(L_DIRECTORY);
|
---|
| 123 | return root != NULL;
|
---|
[4b11571] | 124 | }
|
---|
| 125 |
|
---|
[d5cdffe] | 126 | /** Compare one component of path to a directory entry.
|
---|
| 127 | *
|
---|
[b5553a2] | 128 | * @param nodep Node to compare the path component with.
|
---|
[b8b23c8] | 129 | * @param component Array of characters holding component name.
|
---|
[d5cdffe] | 130 | *
|
---|
[b8b23c8] | 131 | * @return True on match, false otherwise.
|
---|
[d5cdffe] | 132 | */
|
---|
[b5553a2] | 133 | static bool match_component(void *nodep, const char *component)
|
---|
[d5cdffe] | 134 | {
|
---|
[b5553a2] | 135 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
|
---|
| 136 |
|
---|
[b8b23c8] | 137 | return !strcmp(dentry->name, component);
|
---|
| 138 | }
|
---|
[4b11571] | 139 |
|
---|
[fdb7795] | 140 | void *create_node(int lflag)
|
---|
[b8b23c8] | 141 | {
|
---|
[72bde81] | 142 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
---|
| 143 |
|
---|
| 144 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
|
---|
| 145 | if (!node)
|
---|
[b5553a2] | 146 | return NULL;
|
---|
[72bde81] | 147 |
|
---|
| 148 | tmpfs_dentry_initialize(node);
|
---|
| 149 | node->index = tmpfs_next_index++;
|
---|
| 150 | if (lflag & L_DIRECTORY)
|
---|
| 151 | node->type = TMPFS_DIRECTORY;
|
---|
| 152 | else
|
---|
| 153 | node->type = TMPFS_FILE;
|
---|
| 154 |
|
---|
[fdb7795] | 155 | /* Insert the new node into the dentry hash table. */
|
---|
| 156 | hash_table_insert(&dentries, &node->index, &node->dh_link);
|
---|
| 157 | return (void *) node;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | bool link_node(void *prnt, void *chld, const char *nm)
|
---|
| 161 | {
|
---|
| 162 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
|
---|
| 163 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
|
---|
| 164 |
|
---|
| 165 | assert(parentp->type == TMPFS_DIRECTORY);
|
---|
| 166 |
|
---|
| 167 | size_t len = strlen(nm);
|
---|
| 168 | char *name = malloc(len + 1);
|
---|
| 169 | if (!name)
|
---|
| 170 | return false;
|
---|
| 171 | strcpy(name, nm);
|
---|
| 172 | childp->name = name;
|
---|
| 173 |
|
---|
[72bde81] | 174 | /* Insert the new node into the namespace. */
|
---|
[fdb7795] | 175 | if (parentp->child) {
|
---|
| 176 | tmpfs_dentry_t *tmp = parentp->child;
|
---|
[72bde81] | 177 | while (tmp->sibling)
|
---|
| 178 | tmp = tmp->sibling;
|
---|
[fdb7795] | 179 | tmp->sibling = childp;
|
---|
[72bde81] | 180 | } else {
|
---|
[fdb7795] | 181 | parentp->child = childp;
|
---|
[72bde81] | 182 | }
|
---|
[fdb7795] | 183 | childp->parent = parentp;
|
---|
[72bde81] | 184 |
|
---|
[fdb7795] | 185 | return true;
|
---|
[b8b23c8] | 186 | }
|
---|
[4b11571] | 187 |
|
---|
[fdb7795] | 188 | int unlink_node(void *nodeptr)
|
---|
[b8b23c8] | 189 | {
|
---|
[16105cba] | 190 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *)nodeptr;
|
---|
| 191 |
|
---|
| 192 | if (dentry->child)
|
---|
| 193 | return ENOTEMPTY;
|
---|
| 194 |
|
---|
| 195 | if (!dentry->parent)
|
---|
| 196 | return EBUSY;
|
---|
| 197 |
|
---|
| 198 | if (dentry->parent->child == dentry) {
|
---|
| 199 | dentry->parent->child = dentry->sibling;
|
---|
| 200 | } else {
|
---|
| 201 | /* TODO: consider doubly linked list for organizing siblings. */
|
---|
| 202 | tmpfs_dentry_t *tmp = dentry->parent->child;
|
---|
| 203 | while (tmp->sibling != dentry)
|
---|
| 204 | tmp = tmp->sibling;
|
---|
| 205 | tmp->sibling = dentry->sibling;
|
---|
| 206 | }
|
---|
[cf19ab5] | 207 | dentry->sibling = NULL;
|
---|
| 208 | dentry->parent = NULL;
|
---|
[16105cba] | 209 |
|
---|
[fdb7795] | 210 | free(dentry->name);
|
---|
| 211 | dentry->name = NULL;
|
---|
| 212 |
|
---|
[16105cba] | 213 | return EOK;
|
---|
[d5cdffe] | 214 | }
|
---|
| 215 |
|
---|
[fdb7795] | 216 | void destroy_node(void *nodep)
|
---|
| 217 | {
|
---|
| 218 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
|
---|
| 219 |
|
---|
| 220 | assert(!dentry->child);
|
---|
| 221 | assert(!dentry->sibling);
|
---|
| 222 |
|
---|
| 223 | unsigned long index = dentry->index;
|
---|
| 224 | hash_table_remove(&dentries, &index, 1);
|
---|
| 225 |
|
---|
| 226 | if (dentry->type == TMPFS_FILE)
|
---|
| 227 | free(dentry->data);
|
---|
| 228 | free(dentry);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[d5cdffe] | 231 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 232 | {
|
---|
[4b11571] | 233 | unsigned next = IPC_GET_ARG1(*request);
|
---|
| 234 | unsigned last = IPC_GET_ARG2(*request);
|
---|
[d5cdffe] | 235 | int dev_handle = IPC_GET_ARG3(*request);
|
---|
[ae78b530] | 236 | int lflag = IPC_GET_ARG4(*request);
|
---|
[d5cdffe] | 237 |
|
---|
[4b11571] | 238 | if (last < next)
|
---|
| 239 | last += PLB_SIZE;
|
---|
| 240 |
|
---|
[b8b23c8] | 241 | /*
|
---|
| 242 | * Initialize TMPFS.
|
---|
| 243 | */
|
---|
[4b11571] | 244 | if (!root && !tmpfs_init()) {
|
---|
| 245 | ipc_answer_0(rid, ENOMEM);
|
---|
| 246 | return;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | tmpfs_dentry_t *dtmp = root->child;
|
---|
| 250 | tmpfs_dentry_t *dcur = root;
|
---|
| 251 |
|
---|
| 252 | if (PLB_GET_CHAR(next) == '/')
|
---|
| 253 | next++; /* eat slash */
|
---|
[d5cdffe] | 254 |
|
---|
[b8b23c8] | 255 | char component[NAME_MAX + 1];
|
---|
| 256 | int len = 0;
|
---|
[1fe186f] | 257 | while (dtmp && next <= last) {
|
---|
[b8b23c8] | 258 |
|
---|
| 259 | /* collect the component */
|
---|
| 260 | if (PLB_GET_CHAR(next) != '/') {
|
---|
| 261 | if (len + 1 == NAME_MAX) {
|
---|
| 262 | /* comopnent length overflow */
|
---|
| 263 | ipc_answer_0(rid, ENAMETOOLONG);
|
---|
| 264 | return;
|
---|
| 265 | }
|
---|
| 266 | component[len++] = PLB_GET_CHAR(next);
|
---|
| 267 | next++; /* process next character */
|
---|
[72bde81] | 268 | if (next <= last)
|
---|
[b8b23c8] | 269 | continue;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | assert(len);
|
---|
| 273 | component[len] = '\0';
|
---|
| 274 | next++; /* eat slash */
|
---|
| 275 | len = 0;
|
---|
| 276 |
|
---|
| 277 | /* match the component */
|
---|
| 278 | while (dtmp && !match_component(dtmp, component))
|
---|
| 279 | dtmp = dtmp->sibling;
|
---|
| 280 |
|
---|
| 281 | /* handle miss: match amongst siblings */
|
---|
| 282 | if (!dtmp) {
|
---|
| 283 | if ((next > last) && (lflag & L_CREATE)) {
|
---|
| 284 | /* no components left and L_CREATE specified */
|
---|
| 285 | if (dcur->type != TMPFS_DIRECTORY) {
|
---|
| 286 | ipc_answer_0(rid, ENOTDIR);
|
---|
| 287 | return;
|
---|
| 288 | }
|
---|
[fdb7795] | 289 | void *nodep = create_node(lflag);
|
---|
[b5553a2] | 290 | if (nodep) {
|
---|
[fdb7795] | 291 | if (!link_node(dcur, nodep,
|
---|
| 292 | component)) {
|
---|
| 293 | destroy_node(nodep);
|
---|
| 294 | ipc_answer_0(rid, ENOSPC);
|
---|
| 295 | } else {
|
---|
| 296 | ipc_answer_5(rid, EOK,
|
---|
| 297 | tmpfs_reg.fs_handle,
|
---|
| 298 | dev_handle,
|
---|
| 299 | TMPFS_GET_INDEX(nodep), 0,
|
---|
| 300 | TMPFS_GET_LNKCNT(nodep));
|
---|
| 301 | }
|
---|
[b8b23c8] | 302 | } else {
|
---|
| 303 | ipc_answer_0(rid, ENOSPC);
|
---|
| 304 | }
|
---|
| 305 | return;
|
---|
| 306 | }
|
---|
| 307 | ipc_answer_0(rid, ENOENT);
|
---|
| 308 | return;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[8ccd2ea] | 311 | /* descend one level */
|
---|
[b8b23c8] | 312 | dcur = dtmp;
|
---|
| 313 | dtmp = dtmp->child;
|
---|
[1fe186f] | 314 | }
|
---|
[b8b23c8] | 315 |
|
---|
[1fe186f] | 316 | /* handle miss: excessive components */
|
---|
| 317 | if (!dtmp && next <= last) {
|
---|
| 318 | if (lflag & L_CREATE) {
|
---|
| 319 | if (dcur->type != TMPFS_DIRECTORY) {
|
---|
| 320 | ipc_answer_0(rid, ENOTDIR);
|
---|
| 321 | return;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | /* collect next component */
|
---|
| 325 | while (next <= last) {
|
---|
| 326 | if (PLB_GET_CHAR(next) == '/') {
|
---|
| 327 | /* more than one component */
|
---|
| 328 | ipc_answer_0(rid, ENOENT);
|
---|
[b8b23c8] | 329 | return;
|
---|
| 330 | }
|
---|
[1fe186f] | 331 | if (len + 1 == NAME_MAX) {
|
---|
| 332 | /* component length overflow */
|
---|
| 333 | ipc_answer_0(rid, ENAMETOOLONG);
|
---|
| 334 | return;
|
---|
[b8b23c8] | 335 | }
|
---|
[1fe186f] | 336 | component[len++] = PLB_GET_CHAR(next);
|
---|
| 337 | next++; /* process next character */
|
---|
| 338 | }
|
---|
| 339 | assert(len);
|
---|
| 340 | component[len] = '\0';
|
---|
| 341 | len = 0;
|
---|
[b8b23c8] | 342 |
|
---|
[fdb7795] | 343 | void *nodep = create_node(lflag);
|
---|
[b5553a2] | 344 | if (nodep) {
|
---|
[fdb7795] | 345 | if (!link_node(dcur, nodep, component)) {
|
---|
| 346 | destroy_node(nodep);
|
---|
| 347 | ipc_answer_0(rid, ENOSPC);
|
---|
| 348 | } else {
|
---|
| 349 | ipc_answer_5(rid, EOK,
|
---|
| 350 | tmpfs_reg.fs_handle,
|
---|
| 351 | dev_handle, TMPFS_GET_INDEX(nodep),
|
---|
| 352 | 0, TMPFS_GET_LNKCNT(nodep));
|
---|
| 353 | }
|
---|
[1fe186f] | 354 | } else {
|
---|
| 355 | ipc_answer_0(rid, ENOSPC);
|
---|
[b8b23c8] | 356 | }
|
---|
[4b11571] | 357 | return;
|
---|
| 358 | }
|
---|
[1fe186f] | 359 | ipc_answer_0(rid, ENOENT);
|
---|
| 360 | return;
|
---|
[b8b23c8] | 361 | }
|
---|
| 362 |
|
---|
| 363 | /* handle hit */
|
---|
| 364 | if (lflag & L_DESTROY) {
|
---|
[f15cf1a6] | 365 | unsigned old_lnkcnt = TMPFS_GET_LNKCNT(dcur);
|
---|
[fdb7795] | 366 | int res = unlink_node(dcur);
|
---|
[16105cba] | 367 | ipc_answer_5(rid, (ipcarg_t)res, tmpfs_reg.fs_handle,
|
---|
[f15cf1a6] | 368 | dev_handle, dcur->index, dcur->size, old_lnkcnt);
|
---|
[b8b23c8] | 369 | return;
|
---|
| 370 | }
|
---|
| 371 | if ((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) {
|
---|
| 372 | ipc_answer_0(rid, EEXIST);
|
---|
| 373 | return;
|
---|
| 374 | }
|
---|
| 375 | if ((lflag & L_FILE) && (dcur->type != TMPFS_FILE)) {
|
---|
| 376 | ipc_answer_0(rid, EISDIR);
|
---|
| 377 | return;
|
---|
| 378 | }
|
---|
| 379 | if ((lflag & L_DIRECTORY) && (dcur->type != TMPFS_DIRECTORY)) {
|
---|
| 380 | ipc_answer_0(rid, ENOTDIR);
|
---|
| 381 | return;
|
---|
[4b11571] | 382 | }
|
---|
| 383 |
|
---|
[b5553a2] | 384 | ipc_answer_5(rid, EOK, tmpfs_reg.fs_handle, dev_handle, dcur->index,
|
---|
| 385 | dcur->size, TMPFS_GET_LNKCNT(dcur));
|
---|
[d5cdffe] | 386 | }
|
---|
| 387 |
|
---|
[a4eb8a60] | 388 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 389 | {
|
---|
| 390 | int dev_handle = IPC_GET_ARG1(*request);
|
---|
| 391 | unsigned long index = IPC_GET_ARG2(*request);
|
---|
| 392 | off_t pos = IPC_GET_ARG3(*request);
|
---|
| 393 |
|
---|
| 394 | /*
|
---|
| 395 | * Lookup the respective dentry.
|
---|
| 396 | */
|
---|
| 397 | link_t *hlp;
|
---|
| 398 | hlp = hash_table_find(&dentries, &index);
|
---|
| 399 | if (!hlp) {
|
---|
| 400 | ipc_answer_0(rid, ENOENT);
|
---|
| 401 | return;
|
---|
| 402 | }
|
---|
| 403 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
| 404 | dh_link);
|
---|
| 405 |
|
---|
| 406 | /*
|
---|
[a92da0a] | 407 | * Receive the read request.
|
---|
[a4eb8a60] | 408 | */
|
---|
| 409 | ipc_callid_t callid;
|
---|
[92688eb] | 410 | size_t len;
|
---|
| 411 | if (!ipc_data_read_receive(&callid, &len)) {
|
---|
[a4eb8a60] | 412 | ipc_answer_0(callid, EINVAL);
|
---|
| 413 | ipc_answer_0(rid, EINVAL);
|
---|
| 414 | return;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[5973fd0] | 417 | size_t bytes;
|
---|
| 418 | if (dentry->type == TMPFS_FILE) {
|
---|
| 419 | bytes = max(0, min(dentry->size - pos, len));
|
---|
| 420 | (void) ipc_data_read_finalize(callid, dentry->data + pos,
|
---|
| 421 | bytes);
|
---|
| 422 | } else {
|
---|
| 423 | int i;
|
---|
[75c426b4] | 424 | tmpfs_dentry_t *cur;
|
---|
[5973fd0] | 425 |
|
---|
| 426 | assert(dentry->type == TMPFS_DIRECTORY);
|
---|
| 427 |
|
---|
| 428 | /*
|
---|
| 429 | * Yes, we really use O(n) algorithm here.
|
---|
| 430 | * If it bothers someone, it could be fixed by introducing a
|
---|
| 431 | * hash table.
|
---|
| 432 | */
|
---|
| 433 | for (i = 0, cur = dentry->child; i < pos && cur; i++,
|
---|
| 434 | cur = cur->sibling)
|
---|
| 435 | ;
|
---|
| 436 |
|
---|
| 437 | if (!cur) {
|
---|
| 438 | ipc_answer_0(callid, ENOENT);
|
---|
| 439 | ipc_answer_1(rid, ENOENT, 0);
|
---|
| 440 | return;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | (void) ipc_data_read_finalize(callid, cur->name,
|
---|
| 444 | strlen(cur->name) + 1);
|
---|
| 445 | bytes = 1;
|
---|
| 446 | }
|
---|
[7dab6b8] | 447 |
|
---|
| 448 | /*
|
---|
| 449 | * Answer the VFS_READ call.
|
---|
| 450 | */
|
---|
| 451 | ipc_answer_1(rid, EOK, bytes);
|
---|
[a4eb8a60] | 452 | }
|
---|
| 453 |
|
---|
[ee1b8ca] | 454 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 455 | {
|
---|
| 456 | int dev_handle = IPC_GET_ARG1(*request);
|
---|
| 457 | unsigned long index = IPC_GET_ARG2(*request);
|
---|
| 458 | off_t pos = IPC_GET_ARG3(*request);
|
---|
| 459 |
|
---|
| 460 | /*
|
---|
| 461 | * Lookup the respective dentry.
|
---|
| 462 | */
|
---|
| 463 | link_t *hlp;
|
---|
| 464 | hlp = hash_table_find(&dentries, &index);
|
---|
| 465 | if (!hlp) {
|
---|
| 466 | ipc_answer_0(rid, ENOENT);
|
---|
| 467 | return;
|
---|
| 468 | }
|
---|
| 469 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
| 470 | dh_link);
|
---|
| 471 |
|
---|
| 472 | /*
|
---|
| 473 | * Receive the write request.
|
---|
| 474 | */
|
---|
| 475 | ipc_callid_t callid;
|
---|
[92688eb] | 476 | size_t len;
|
---|
[3115355] | 477 | if (!ipc_data_write_receive(&callid, &len)) {
|
---|
[ee1b8ca] | 478 | ipc_answer_0(callid, EINVAL);
|
---|
| 479 | ipc_answer_0(rid, EINVAL);
|
---|
| 480 | return;
|
---|
| 481 | }
|
---|
| 482 |
|
---|
[c1bf5cb] | 483 | /*
|
---|
| 484 | * Check whether the file needs to grow.
|
---|
| 485 | */
|
---|
[92688eb] | 486 | if (pos + len <= dentry->size) {
|
---|
[c1bf5cb] | 487 | /* The file size is not changing. */
|
---|
[215e375] | 488 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
|
---|
[f7017572] | 489 | ipc_answer_2(rid, EOK, len, dentry->size);
|
---|
[c1bf5cb] | 490 | return;
|
---|
| 491 | }
|
---|
[92688eb] | 492 | size_t delta = (pos + len) - dentry->size;
|
---|
[ee1b8ca] | 493 | /*
|
---|
| 494 | * At this point, we are deliberately extremely straightforward and
|
---|
[c1bf5cb] | 495 | * simply realloc the contents of the file on every write that grows the
|
---|
| 496 | * file. In the end, the situation might not be as bad as it may look:
|
---|
| 497 | * our heap allocator can save us and just grow the block whenever
|
---|
| 498 | * possible.
|
---|
[ee1b8ca] | 499 | */
|
---|
[c1bf5cb] | 500 | void *newdata = realloc(dentry->data, dentry->size + delta);
|
---|
[ee1b8ca] | 501 | if (!newdata) {
|
---|
| 502 | ipc_answer_0(callid, ENOMEM);
|
---|
[f7017572] | 503 | ipc_answer_2(rid, EOK, 0, dentry->size);
|
---|
[ee1b8ca] | 504 | return;
|
---|
| 505 | }
|
---|
[0ee4322] | 506 | /* Clear any newly allocated memory in order to emulate gaps. */
|
---|
[752ccee] | 507 | memset(newdata + dentry->size, 0, delta);
|
---|
[c1bf5cb] | 508 | dentry->size += delta;
|
---|
[ee1b8ca] | 509 | dentry->data = newdata;
|
---|
[215e375] | 510 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
|
---|
[7fff5eab] | 511 | ipc_answer_2(rid, EOK, len, dentry->size);
|
---|
[ee1b8ca] | 512 | }
|
---|
| 513 |
|
---|
[0ee4322] | 514 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 515 | {
|
---|
| 516 | int dev_handle = IPC_GET_ARG1(*request);
|
---|
| 517 | unsigned long index = IPC_GET_ARG2(*request);
|
---|
| 518 | size_t size = IPC_GET_ARG3(*request);
|
---|
| 519 |
|
---|
| 520 | /*
|
---|
| 521 | * Lookup the respective dentry.
|
---|
| 522 | */
|
---|
| 523 | link_t *hlp;
|
---|
| 524 | hlp = hash_table_find(&dentries, &index);
|
---|
| 525 | if (!hlp) {
|
---|
| 526 | ipc_answer_0(rid, ENOENT);
|
---|
| 527 | return;
|
---|
| 528 | }
|
---|
| 529 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
| 530 | dh_link);
|
---|
| 531 |
|
---|
| 532 | if (size == dentry->size) {
|
---|
| 533 | ipc_answer_0(rid, EOK);
|
---|
| 534 | return;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | void *newdata = realloc(dentry->data, size);
|
---|
| 538 | if (!newdata) {
|
---|
| 539 | ipc_answer_0(rid, ENOMEM);
|
---|
| 540 | return;
|
---|
| 541 | }
|
---|
| 542 | if (size > dentry->size) {
|
---|
| 543 | size_t delta = size - dentry->size;
|
---|
| 544 | memset(newdata + dentry->size, 0, delta);
|
---|
| 545 | }
|
---|
| 546 | dentry->size = size;
|
---|
| 547 | dentry->data = newdata;
|
---|
| 548 | ipc_answer_0(rid, EOK);
|
---|
| 549 | }
|
---|
| 550 |
|
---|
[fdb7795] | 551 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
[f17667a] | 552 | {
|
---|
| 553 | int dev_handle = IPC_GET_ARG1(*request);
|
---|
| 554 | unsigned long index = IPC_GET_ARG2(*request);
|
---|
| 555 |
|
---|
| 556 | link_t *hlp;
|
---|
| 557 | hlp = hash_table_find(&dentries, &index);
|
---|
| 558 | if (!hlp) {
|
---|
| 559 | ipc_answer_0(rid, ENOENT);
|
---|
| 560 | return;
|
---|
| 561 | }
|
---|
| 562 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
| 563 | dh_link);
|
---|
[fdb7795] | 564 | destroy_node(dentry);
|
---|
[f17667a] | 565 | ipc_answer_0(rid, EOK);
|
---|
| 566 | }
|
---|
| 567 |
|
---|
[d5cdffe] | 568 | /**
|
---|
| 569 | * @}
|
---|
| 570 | */
|
---|