Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/fs/tmpfs/tmpfs_ops.c

    r991f645 r4cac2d69  
    2929/** @addtogroup fs
    3030 * @{
    31  */
     31 */ 
    3232
    3333/**
     
    4040#include "../../vfs/vfs.h"
    4141#include <ipc/ipc.h>
    42 #include <macros.h>
    43 #include <stdint.h>
    4442#include <async.h>
    4543#include <errno.h>
    4644#include <atomic.h>
    4745#include <stdlib.h>
    48 #include <str.h>
     46#include <string.h>
    4947#include <stdio.h>
    5048#include <assert.h>
     
    7068/* Forward declarations of static functions. */
    7169static int tmpfs_match(fs_node_t **, fs_node_t *, const char *);
    72 static int tmpfs_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
     70static int tmpfs_node_get(fs_node_t **, dev_handle_t, fs_index_t);
    7371static int tmpfs_node_open(fs_node_t *);
    7472static int tmpfs_node_put(fs_node_t *);
    75 static int tmpfs_create_node(fs_node_t **, devmap_handle_t, int);
     73static int tmpfs_create_node(fs_node_t **, dev_handle_t, int);
    7674static int tmpfs_destroy_node(fs_node_t *);
    7775static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *);
     
    7977
    8078/* Implementation of helper functions. */
    81 static int tmpfs_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
    82 {
    83         return tmpfs_node_get(rfn, devmap_handle, TMPFS_SOME_ROOT);
     79static int tmpfs_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
     80{
     81        return tmpfs_node_get(rfn, dev_handle, TMPFS_SOME_ROOT);
    8482}
    8583
     
    9593}
    9694
    97 static aoff64_t tmpfs_size_get(fs_node_t *fn)
     95static size_t tmpfs_size_get(fs_node_t *fn)
    9896{
    9997        return TMPFS_NODE(fn)->size;
     
    120118}
    121119
    122 static devmap_handle_t tmpfs_device_get(fs_node_t *fn)
     120static dev_handle_t tmpfs_device_get(fs_node_t *fn)
    123121{
    124122        return 0;
     
    165163        switch (keys) {
    166164        case 1:
    167                 return (nodep->devmap_handle == key[NODES_KEY_DEV]);
     165                return (nodep->dev_handle == key[NODES_KEY_DEV]);
    168166        case 2:
    169                 return ((nodep->devmap_handle == key[NODES_KEY_DEV]) &&
     167                return ((nodep->dev_handle == key[NODES_KEY_DEV]) &&
    170168                    (nodep->index == key[NODES_KEY_INDEX]));
    171169        default:
    172                 assert((keys == 1) || (keys == 2));
    173         }
    174 
    175         return 0;
     170                abort();
     171        }
    176172}
    177173
     
    209205        nodep->bp = NULL;
    210206        nodep->index = 0;
    211         nodep->devmap_handle = 0;
     207        nodep->dev_handle = 0;
    212208        nodep->type = TMPFS_NONE;
    213209        nodep->lnkcnt = 0;
     
    233229}
    234230
    235 static bool tmpfs_instance_init(devmap_handle_t devmap_handle)
     231static bool tmpfs_instance_init(dev_handle_t dev_handle)
    236232{
    237233        fs_node_t *rfn;
    238234        int rc;
    239235       
    240         rc = tmpfs_create_node(&rfn, devmap_handle, L_DIRECTORY);
     236        rc = tmpfs_create_node(&rfn, dev_handle, L_DIRECTORY);
    241237        if (rc != EOK || !rfn)
    242238                return false;
     
    245241}
    246242
    247 static void tmpfs_instance_done(devmap_handle_t devmap_handle)
     243static void tmpfs_instance_done(dev_handle_t dev_handle)
    248244{
    249245        unsigned long key[] = {
    250                 [NODES_KEY_DEV] = devmap_handle
     246                [NODES_KEY_DEV] = dev_handle
    251247        };
    252248        /*
     
    279275}
    280276
    281 int tmpfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
     277int tmpfs_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
    282278{
    283279        unsigned long key[] = {
    284                 [NODES_KEY_DEV] = devmap_handle,
     280                [NODES_KEY_DEV] = dev_handle,
    285281                [NODES_KEY_INDEX] = index
    286282        };
     
    308304}
    309305
    310 int tmpfs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
     306int tmpfs_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int lflag)
    311307{
    312308        fs_node_t *rootfn;
     
    327323        nodep->bp->data = nodep;        /* link the FS and TMPFS nodes */
    328324
    329         rc = tmpfs_root_get(&rootfn, devmap_handle);
     325        rc = tmpfs_root_get(&rootfn, dev_handle);
    330326        assert(rc == EOK);
    331327        if (!rootfn)
     
    333329        else
    334330                nodep->index = tmpfs_next_index++;
    335         nodep->devmap_handle = devmap_handle;
     331        nodep->dev_handle = dev_handle;
    336332        if (lflag & L_DIRECTORY)
    337333                nodep->type = TMPFS_DIRECTORY;
     
    341337        /* Insert the new node into the nodes hash table. */
    342338        unsigned long key[] = {
    343                 [NODES_KEY_DEV] = nodep->devmap_handle,
     339                [NODES_KEY_DEV] = nodep->dev_handle,
    344340                [NODES_KEY_INDEX] = nodep->index
    345341        };
     
    357353
    358354        unsigned long key[] = {
    359                 [NODES_KEY_DEV] = nodep->devmap_handle,
     355                [NODES_KEY_DEV] = nodep->dev_handle,
    360356                [NODES_KEY_INDEX] = nodep->index
    361357        };
     
    442438void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
    443439{
    444         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    445         fs_node_t *rootfn;
    446         int rc;
     440        dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
    447441       
    448         /* Accept the mount options. */
     442        /* Accept the mount options */
    449443        char *opts;
    450         rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
     444        int rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
     445       
    451446        if (rc != EOK) {
    452447                ipc_answer_0(rid, rc);
     
    454449        }
    455450
    456         /* Check if this device is not already mounted. */
    457         rc = tmpfs_root_get(&rootfn, devmap_handle);
    458         if ((rc == EOK) && (rootfn)) {
    459                 (void) tmpfs_node_put(rootfn);
    460                 free(opts);
    461                 ipc_answer_0(rid, EEXIST);
    462                 return;
    463         }
    464 
    465451        /* Initialize TMPFS instance. */
    466         if (!tmpfs_instance_init(devmap_handle)) {
     452        if (!tmpfs_instance_init(dev_handle)) {
    467453                free(opts);
    468454                ipc_answer_0(rid, ENOMEM);
     
    470456        }
    471457
    472         rc = tmpfs_root_get(&rootfn, devmap_handle);
     458        fs_node_t *rootfn;
     459        rc = tmpfs_root_get(&rootfn, dev_handle);
    473460        assert(rc == EOK);
    474461        tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
    475462        if (str_cmp(opts, "restore") == 0) {
    476                 if (tmpfs_restore(devmap_handle))
     463                if (tmpfs_restore(dev_handle))
    477464                        ipc_answer_3(rid, EOK, rootp->index, rootp->size,
    478465                            rootp->lnkcnt);
     
    493480void tmpfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
    494481{
    495         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    496 
    497         tmpfs_instance_done(devmap_handle);
     482        dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
     483
     484        tmpfs_instance_done(dev_handle);
    498485        ipc_answer_0(rid, EOK);
    499486}
     
    511498void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
    512499{
    513         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    514         fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    515         aoff64_t pos =
    516             (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
    517        
     500        dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
     501        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
     502        off_t pos = (off_t)IPC_GET_ARG3(*request);
     503
    518504        /*
    519505         * Lookup the respective TMPFS node.
     
    521507        link_t *hlp;
    522508        unsigned long key[] = {
    523                 [NODES_KEY_DEV] = devmap_handle,
     509                [NODES_KEY_DEV] = dev_handle,
    524510                [NODES_KEY_INDEX] = index
    525511        };
     
    531517        tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
    532518            nh_link);
    533        
     519
    534520        /*
    535521         * Receive the read request.
     
    538524        size_t size;
    539525        if (!async_data_read_receive(&callid, &size)) {
    540                 ipc_answer_0(callid, EINVAL);
     526                ipc_answer_0(callid, EINVAL);   
    541527                ipc_answer_0(rid, EINVAL);
    542528                return;
     
    545531        size_t bytes;
    546532        if (nodep->type == TMPFS_FILE) {
    547                 bytes = min(nodep->size - pos, size);
     533                bytes = max(0, min(nodep->size - pos, size));
    548534                (void) async_data_read_finalize(callid, nodep->data + pos,
    549535                    bytes);
     
    551537                tmpfs_dentry_t *dentryp;
    552538                link_t *lnk;
    553                 aoff64_t i;
     539                int i;
    554540               
    555541                assert(nodep->type == TMPFS_DIRECTORY);
     
    561547                 */
    562548                for (i = 0, lnk = nodep->cs_head.next;
    563                     (i < pos) && (lnk != &nodep->cs_head);
     549                    i < pos && lnk != &nodep->cs_head;
    564550                    i++, lnk = lnk->next)
    565551                        ;
     
    586572void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
    587573{
    588         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    589         fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    590         aoff64_t pos =
    591             (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
    592        
     574        dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
     575        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
     576        off_t pos = (off_t)IPC_GET_ARG3(*request);
     577
    593578        /*
    594579         * Lookup the respective TMPFS node.
     
    596581        link_t *hlp;
    597582        unsigned long key[] = {
    598                 [NODES_KEY_DEV] = devmap_handle,
     583                [NODES_KEY_DEV] = dev_handle,
    599584                [NODES_KEY_INDEX] = index
    600585        };
     
    651636void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
    652637{
    653         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    654         fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    655         aoff64_t size =
    656             (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
    657        
     638        dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
     639        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
     640        size_t size = (off_t)IPC_GET_ARG3(*request);
     641
    658642        /*
    659643         * Lookup the respective TMPFS node.
    660644         */
    661         unsigned long key[] = {
    662                 [NODES_KEY_DEV] = devmap_handle,
    663                 [NODES_KEY_INDEX] = index
    664         };
    665         link_t *hlp = hash_table_find(&nodes, key);
    666         if (!hlp) {
    667                 ipc_answer_0(rid, ENOENT);
    668                 return;
    669         }
    670         tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
    671             nh_link);
    672        
    673         if (size == nodep->size) {
    674                 ipc_answer_0(rid, EOK);
    675                 return;
    676         }
    677        
    678         if (size > SIZE_MAX) {
    679                 ipc_answer_0(rid, ENOMEM);
    680                 return;
    681         }
    682        
    683         void *newdata = realloc(nodep->data, size);
    684         if (!newdata) {
    685                 ipc_answer_0(rid, ENOMEM);
    686                 return;
    687         }
    688        
    689         if (size > nodep->size) {
    690                 size_t delta = size - nodep->size;
    691                 memset(newdata + nodep->size, 0, delta);
    692         }
    693        
    694         nodep->size = size;
    695         nodep->data = newdata;
    696         ipc_answer_0(rid, EOK);
    697 }
    698 
    699 void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
    700 {
    701         ipc_answer_0(rid, EOK);
    702 }
    703 
    704 void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
    705 {
    706         devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
    707         fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
    708         int rc;
    709 
    710645        link_t *hlp;
    711646        unsigned long key[] = {
    712                 [NODES_KEY_DEV] = devmap_handle,
     647                [NODES_KEY_DEV] = dev_handle,
    713648                [NODES_KEY_INDEX] = index
    714649        };
     
    720655        tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
    721656            nh_link);
     657
     658        if (size == nodep->size) {
     659                ipc_answer_0(rid, EOK);
     660                return;
     661        }
     662
     663        void *newdata = realloc(nodep->data, size);
     664        if (!newdata) {
     665                ipc_answer_0(rid, ENOMEM);
     666                return;
     667        }
     668        if (size > nodep->size) {
     669                size_t delta = size - nodep->size;
     670                memset(newdata + nodep->size, 0, delta);
     671        }
     672        nodep->size = size;
     673        nodep->data = newdata;
     674        ipc_answer_0(rid, EOK);
     675}
     676
     677void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
     678{
     679        ipc_answer_0(rid, EOK);
     680}
     681
     682void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
     683{
     684        dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
     685        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
     686        int rc;
     687
     688        link_t *hlp;
     689        unsigned long key[] = {
     690                [NODES_KEY_DEV] = dev_handle,
     691                [NODES_KEY_INDEX] = index
     692        };
     693        hlp = hash_table_find(&nodes, key);
     694        if (!hlp) {
     695                ipc_answer_0(rid, ENOENT);
     696                return;
     697        }
     698        tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
     699            nh_link);
    722700        rc = tmpfs_destroy_node(FS_NODE(nodep));
    723701        ipc_answer_0(rid, rc);
     
    736714void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
    737715{
    738         /*
    739          * TMPFS keeps its data structures always consistent,
    740          * thus the sync operation is a no-op.
    741          */
     716        /* Dummy implementation */
    742717        ipc_answer_0(rid, EOK);
    743718}
Note: See TracChangeset for help on using the changeset viewer.