Ignore:
File:
1 edited

Legend:

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

    r4cac2d69 r991f645  
    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>
    4244#include <async.h>
    4345#include <errno.h>
    4446#include <atomic.h>
    4547#include <stdlib.h>
    46 #include <string.h>
     48#include <str.h>
    4749#include <stdio.h>
    4850#include <assert.h>
     
    6870/* Forward declarations of static functions. */
    6971static int tmpfs_match(fs_node_t **, fs_node_t *, const char *);
    70 static int tmpfs_node_get(fs_node_t **, dev_handle_t, fs_index_t);
     72static int tmpfs_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
    7173static int tmpfs_node_open(fs_node_t *);
    7274static int tmpfs_node_put(fs_node_t *);
    73 static int tmpfs_create_node(fs_node_t **, dev_handle_t, int);
     75static int tmpfs_create_node(fs_node_t **, devmap_handle_t, int);
    7476static int tmpfs_destroy_node(fs_node_t *);
    7577static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *);
     
    7779
    7880/* Implementation of helper functions. */
    79 static 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);
     81static 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);
    8284}
    8385
     
    9395}
    9496
    95 static size_t tmpfs_size_get(fs_node_t *fn)
     97static aoff64_t tmpfs_size_get(fs_node_t *fn)
    9698{
    9799        return TMPFS_NODE(fn)->size;
     
    118120}
    119121
    120 static dev_handle_t tmpfs_device_get(fs_node_t *fn)
     122static devmap_handle_t tmpfs_device_get(fs_node_t *fn)
    121123{
    122124        return 0;
     
    163165        switch (keys) {
    164166        case 1:
    165                 return (nodep->dev_handle == key[NODES_KEY_DEV]);
     167                return (nodep->devmap_handle == key[NODES_KEY_DEV]);
    166168        case 2:
    167                 return ((nodep->dev_handle == key[NODES_KEY_DEV]) &&
     169                return ((nodep->devmap_handle == key[NODES_KEY_DEV]) &&
    168170                    (nodep->index == key[NODES_KEY_INDEX]));
    169171        default:
    170                 abort();
    171         }
     172                assert((keys == 1) || (keys == 2));
     173        }
     174
     175        return 0;
    172176}
    173177
     
    205209        nodep->bp = NULL;
    206210        nodep->index = 0;
    207         nodep->dev_handle = 0;
     211        nodep->devmap_handle = 0;
    208212        nodep->type = TMPFS_NONE;
    209213        nodep->lnkcnt = 0;
     
    229233}
    230234
    231 static bool tmpfs_instance_init(dev_handle_t dev_handle)
     235static bool tmpfs_instance_init(devmap_handle_t devmap_handle)
    232236{
    233237        fs_node_t *rfn;
    234238        int rc;
    235239       
    236         rc = tmpfs_create_node(&rfn, dev_handle, L_DIRECTORY);
     240        rc = tmpfs_create_node(&rfn, devmap_handle, L_DIRECTORY);
    237241        if (rc != EOK || !rfn)
    238242                return false;
     
    241245}
    242246
    243 static void tmpfs_instance_done(dev_handle_t dev_handle)
     247static void tmpfs_instance_done(devmap_handle_t devmap_handle)
    244248{
    245249        unsigned long key[] = {
    246                 [NODES_KEY_DEV] = dev_handle
     250                [NODES_KEY_DEV] = devmap_handle
    247251        };
    248252        /*
     
    275279}
    276280
    277 int tmpfs_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
     281int tmpfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
    278282{
    279283        unsigned long key[] = {
    280                 [NODES_KEY_DEV] = dev_handle,
     284                [NODES_KEY_DEV] = devmap_handle,
    281285                [NODES_KEY_INDEX] = index
    282286        };
     
    304308}
    305309
    306 int tmpfs_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int lflag)
     310int tmpfs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
    307311{
    308312        fs_node_t *rootfn;
     
    323327        nodep->bp->data = nodep;        /* link the FS and TMPFS nodes */
    324328
    325         rc = tmpfs_root_get(&rootfn, dev_handle);
     329        rc = tmpfs_root_get(&rootfn, devmap_handle);
    326330        assert(rc == EOK);
    327331        if (!rootfn)
     
    329333        else
    330334                nodep->index = tmpfs_next_index++;
    331         nodep->dev_handle = dev_handle;
     335        nodep->devmap_handle = devmap_handle;
    332336        if (lflag & L_DIRECTORY)
    333337                nodep->type = TMPFS_DIRECTORY;
     
    337341        /* Insert the new node into the nodes hash table. */
    338342        unsigned long key[] = {
    339                 [NODES_KEY_DEV] = nodep->dev_handle,
     343                [NODES_KEY_DEV] = nodep->devmap_handle,
    340344                [NODES_KEY_INDEX] = nodep->index
    341345        };
     
    353357
    354358        unsigned long key[] = {
    355                 [NODES_KEY_DEV] = nodep->dev_handle,
     359                [NODES_KEY_DEV] = nodep->devmap_handle,
    356360                [NODES_KEY_INDEX] = nodep->index
    357361        };
     
    438442void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
    439443{
    440         dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
    441        
    442         /* Accept the mount options */
     444        devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
     445        fs_node_t *rootfn;
     446        int rc;
     447       
     448        /* Accept the mount options. */
    443449        char *opts;
    444         int rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
    445        
     450        rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
    446451        if (rc != EOK) {
    447452                ipc_answer_0(rid, rc);
     
    449454        }
    450455
     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
    451465        /* Initialize TMPFS instance. */
    452         if (!tmpfs_instance_init(dev_handle)) {
     466        if (!tmpfs_instance_init(devmap_handle)) {
    453467                free(opts);
    454468                ipc_answer_0(rid, ENOMEM);
     
    456470        }
    457471
    458         fs_node_t *rootfn;
    459         rc = tmpfs_root_get(&rootfn, dev_handle);
     472        rc = tmpfs_root_get(&rootfn, devmap_handle);
    460473        assert(rc == EOK);
    461474        tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
    462475        if (str_cmp(opts, "restore") == 0) {
    463                 if (tmpfs_restore(dev_handle))
     476                if (tmpfs_restore(devmap_handle))
    464477                        ipc_answer_3(rid, EOK, rootp->index, rootp->size,
    465478                            rootp->lnkcnt);
     
    480493void tmpfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
    481494{
    482         dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
    483 
    484         tmpfs_instance_done(dev_handle);
     495        devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
     496
     497        tmpfs_instance_done(devmap_handle);
    485498        ipc_answer_0(rid, EOK);
    486499}
     
    498511void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
    499512{
    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 
     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       
    504518        /*
    505519         * Lookup the respective TMPFS node.
     
    507521        link_t *hlp;
    508522        unsigned long key[] = {
    509                 [NODES_KEY_DEV] = dev_handle,
     523                [NODES_KEY_DEV] = devmap_handle,
    510524                [NODES_KEY_INDEX] = index
    511525        };
     
    517531        tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
    518532            nh_link);
    519 
     533       
    520534        /*
    521535         * Receive the read request.
     
    524538        size_t size;
    525539        if (!async_data_read_receive(&callid, &size)) {
    526                 ipc_answer_0(callid, EINVAL);   
     540                ipc_answer_0(callid, EINVAL);
    527541                ipc_answer_0(rid, EINVAL);
    528542                return;
     
    531545        size_t bytes;
    532546        if (nodep->type == TMPFS_FILE) {
    533                 bytes = max(0, min(nodep->size - pos, size));
     547                bytes = min(nodep->size - pos, size);
    534548                (void) async_data_read_finalize(callid, nodep->data + pos,
    535549                    bytes);
     
    537551                tmpfs_dentry_t *dentryp;
    538552                link_t *lnk;
    539                 int i;
     553                aoff64_t i;
    540554               
    541555                assert(nodep->type == TMPFS_DIRECTORY);
     
    547561                 */
    548562                for (i = 0, lnk = nodep->cs_head.next;
    549                     i < pos && lnk != &nodep->cs_head;
     563                    (i < pos) && (lnk != &nodep->cs_head);
    550564                    i++, lnk = lnk->next)
    551565                        ;
     
    572586void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
    573587{
    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 
     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       
    578593        /*
    579594         * Lookup the respective TMPFS node.
     
    581596        link_t *hlp;
    582597        unsigned long key[] = {
    583                 [NODES_KEY_DEV] = dev_handle,
     598                [NODES_KEY_DEV] = devmap_handle,
    584599                [NODES_KEY_INDEX] = index
    585600        };
     
    636651void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
    637652{
    638         dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
     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       
     658        /*
     659         * Lookup the respective TMPFS node.
     660         */
     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
     699void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
     700{
     701        ipc_answer_0(rid, EOK);
     702}
     703
     704void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
     705{
     706        devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
    639707        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
    640         size_t size = (off_t)IPC_GET_ARG3(*request);
    641 
    642         /*
    643          * Lookup the respective TMPFS node.
    644          */
     708        int rc;
     709
    645710        link_t *hlp;
    646711        unsigned long key[] = {
    647                 [NODES_KEY_DEV] = dev_handle,
     712                [NODES_KEY_DEV] = devmap_handle,
    648713                [NODES_KEY_INDEX] = index
    649714        };
     
    655720        tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
    656721            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 
    677 void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
    678 {
    679         ipc_answer_0(rid, EOK);
    680 }
    681 
    682 void 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);
    700722        rc = tmpfs_destroy_node(FS_NODE(nodep));
    701723        ipc_answer_0(rid, rc);
     
    714736void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
    715737{
    716         /* Dummy implementation */
     738        /*
     739         * TMPFS keeps its data structures always consistent,
     740         * thus the sync operation is a no-op.
     741         */
    717742        ipc_answer_0(rid, EOK);
    718743}
Note: See TracChangeset for help on using the changeset viewer.