Changeset 985e26d2 in mainline for uspace/lib/libfs/libfs.c


Ignore:
Timestamp:
2010-01-07T19:06:59Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8190e63
Parents:
743e17b (diff), eca2435 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libfs/libfs.c

    r743e17b r985e26d2  
    11/*
    2  * Copyright (c) 2009 Jakub Jermar 
     2 * Copyright (c) 2009 Jakub Jermar
    33 * All rights reserved.
    44 *
     
    2727 */
    2828
    29 /** @addtogroup libfs 
     29/** @addtogroup libfs
    3030 * @{
    31  */ 
     31 */
    3232/**
    3333 * @file
    34  * Glue code which is commonod to all FS implementations.
    35  */
    36 
    37 #include "libfs.h" 
     34 * Glue code which is common to all FS implementations.
     35 */
     36
     37#include "libfs.h"
    3838#include "../../srv/vfs/vfs.h"
    3939#include <errno.h>
     
    4646#include <sys/stat.h>
    4747
     48#define on_error(rc, action) \
     49        do { \
     50                if ((rc) != EOK) \
     51                        action; \
     52        } while (0)
     53
     54#define combine_rc(rc1, rc2) \
     55        ((rc1) == EOK ? (rc2) : (rc1))
     56
     57#define answer_and_return(rid, rc) \
     58        do { \
     59                ipc_answer_0((rid), (rc)); \
     60                return; \
     61        } while (0)
     62
    4863/** Register file system server.
    4964 *
     
    5267 * code.
    5368 *
    54  * @param vfs_phone     Open phone for communication with VFS.
    55  * @param reg           File system registration structure. It will be
    56  *                      initialized by this function.
    57  * @param info          VFS info structure supplied by the file system
    58  *                      implementation.
    59  * @param conn          Connection fibril for handling all calls originating in
    60  *                      VFS.
    61  *
    62  * @return              EOK on success or a non-zero error code on errror.
     69 * @param vfs_phone Open phone for communication with VFS.
     70 * @param reg       File system registration structure. It will be
     71 *                  initialized by this function.
     72 * @param info      VFS info structure supplied by the file system
     73 *                  implementation.
     74 * @param conn      Connection fibril for handling all calls originating in
     75 *                  VFS.
     76 *
     77 * @return EOK on success or a non-zero error code on errror.
     78 *
    6379 */
    6480int fs_register(int vfs_phone, fs_reg_t *reg, vfs_info_t *info,
     
    7288        ipc_call_t answer;
    7389        aid_t req = async_send_0(vfs_phone, VFS_IN_REGISTER, &answer);
    74 
     90       
    7591        /*
    7692         * Send our VFS info structure to VFS.
    7793         */
    78         int rc = ipc_data_write_start(vfs_phone, info, sizeof(*info));
     94        int rc = async_data_write_start(vfs_phone, info, sizeof(*info));
    7995        if (rc != EOK) {
    8096                async_wait_for(req, NULL);
    8197                return rc;
    8298        }
    83 
     99       
    84100        /*
    85101         * Ask VFS for callback connection.
    86102         */
    87103        ipc_connect_to_me(vfs_phone, 0, 0, 0, &reg->vfs_phonehash);
    88 
     104       
    89105        /*
    90106         * Allocate piece of address space for PLB.
     
    95111                return ENOMEM;
    96112        }
    97 
     113       
    98114        /*
    99115         * Request sharing the Path Lookup Buffer with VFS.
    100116         */
    101         rc = ipc_share_in_start_0_0(vfs_phone, reg->plb_ro, PLB_SIZE);
     117        rc = async_share_in_start_0_0(vfs_phone, reg->plb_ro, PLB_SIZE);
    102118        if (rc) {
    103119                async_wait_for(req, NULL);
     
    121137         */
    122138        async_set_client_connection(conn);
    123 
     139       
    124140        return IPC_GET_RETVAL(answer);
    125141}
     
    139155        int res;
    140156        ipcarg_t rc;
    141 
     157       
    142158        ipc_call_t call;
    143159        ipc_callid_t callid;
    144 
    145         /* accept the phone */
     160       
     161        /* Accept the phone */
    146162        callid = async_get_call(&call);
    147163        int mountee_phone = (int)IPC_GET_ARG1(call);
    148164        if ((IPC_GET_METHOD(call) != IPC_M_CONNECTION_CLONE) ||
    149             mountee_phone < 0) {
     165            (mountee_phone < 0)) {
    150166                ipc_answer_0(callid, EINVAL);
    151167                ipc_answer_0(rid, EINVAL);
    152168                return;
    153169        }
    154         ipc_answer_0(callid, EOK);      /* acknowledge the mountee_phone */
    155        
    156         res = ipc_data_write_receive(&callid, NULL);
     170       
     171        /* Acknowledge the mountee_phone */
     172        ipc_answer_0(callid, EOK);
     173       
     174        res = async_data_write_receive(&callid, NULL);
    157175        if (!res) {
    158176                ipc_hangup(mountee_phone);
     
    161179                return;
    162180        }
    163 
    164         fs_node_t *fn = ops->node_get(mp_dev_handle, mp_fs_index);
    165         if (!fn) {
     181       
     182        fs_node_t *fn;
     183        res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
     184        if ((res != EOK) || (!fn)) {
    166185                ipc_hangup(mountee_phone);
    167                 ipc_answer_0(callid, ENOENT);
    168                 ipc_answer_0(rid, ENOENT);
    169                 return;
    170         }
    171 
     186                ipc_answer_0(callid, combine_rc(res, ENOENT));
     187                ipc_answer_0(rid, combine_rc(res, ENOENT));
     188                return;
     189        }
     190       
    172191        if (fn->mp_data.mp_active) {
    173192                ipc_hangup(mountee_phone);
    174                 ops->node_put(fn);
     193                (void) ops->node_put(fn);
    175194                ipc_answer_0(callid, EBUSY);
    176195                ipc_answer_0(rid, EBUSY);
    177196                return;
    178197        }
    179 
     198       
    180199        rc = async_req_0_0(mountee_phone, IPC_M_CONNECT_ME);
    181         if (rc != 0) {
     200        if (rc != EOK) {
    182201                ipc_hangup(mountee_phone);
    183                 ops->node_put(fn);
     202                (void) ops->node_put(fn);
    184203                ipc_answer_0(callid, rc);
    185204                ipc_answer_0(rid, rc);
     
    199218                fn->mp_data.phone = mountee_phone;
    200219        }
     220       
    201221        /*
    202222         * Do not release the FS node so that it stays in memory.
     
    222242    ipc_call_t *request)
    223243{
    224         unsigned first = IPC_GET_ARG1(*request);
    225         unsigned last = IPC_GET_ARG2(*request);
    226         unsigned next = first;
     244        unsigned int first = IPC_GET_ARG1(*request);
     245        unsigned int last = IPC_GET_ARG2(*request);
     246        unsigned int next = first;
    227247        dev_handle_t dev_handle = IPC_GET_ARG3(*request);
    228248        int lflag = IPC_GET_ARG4(*request);
    229         fs_index_t index = IPC_GET_ARG5(*request); /* when L_LINK specified */
     249        fs_index_t index = IPC_GET_ARG5(*request);
    230250        char component[NAME_MAX + 1];
    231251        int len;
    232 
     252        int rc;
     253       
    233254        if (last < next)
    234255                last += PLB_SIZE;
    235 
     256       
    236257        fs_node_t *par = NULL;
    237         fs_node_t *cur = ops->root_get(dev_handle);
     258        fs_node_t *cur = NULL;
    238259        fs_node_t *tmp = NULL;
    239 
     260       
     261        rc = ops->root_get(&cur, dev_handle);
     262        on_error(rc, goto out_with_answer);
     263       
    240264        if (cur->mp_data.mp_active) {
    241265                ipc_forward_slow(rid, cur->mp_data.phone, VFS_OUT_LOOKUP,
    242266                    next, last, cur->mp_data.dev_handle, lflag, index,
    243267                    IPC_FF_ROUTE_FROM_ME);
    244                 ops->node_put(cur);
    245                 return;
    246         }
    247 
     268                (void) ops->node_put(cur);
     269                return;
     270        }
     271       
     272        /* Eat slash */
    248273        if (ops->plb_get_char(next) == '/')
    249                 next++;         /* eat slash */
    250        
    251         while (next <= last && ops->has_children(cur)) {
    252                 /* collect the component */
     274                next++;
     275       
     276        while (next <= last) {
     277                bool has_children;
     278               
     279                rc = ops->has_children(&has_children, cur);
     280                on_error(rc, goto out_with_answer);
     281                if (!has_children)
     282                        break;
     283               
     284                /* Collect the component */
    253285                len = 0;
    254                 while ((next <= last) &&  (ops->plb_get_char(next) != '/')) {
     286                while ((next <= last) && (ops->plb_get_char(next) != '/')) {
    255287                        if (len + 1 == NAME_MAX) {
    256                                 /* component length overflow */
     288                                /* Component length overflow */
    257289                                ipc_answer_0(rid, ENAMETOOLONG);
    258290                                goto out;
    259291                        }
    260292                        component[len++] = ops->plb_get_char(next);
    261                         next++; /* process next character */
     293                        /* Process next character */
     294                        next++;
    262295                }
    263 
     296               
    264297                assert(len);
    265298                component[len] = '\0';
    266                 next++;         /* eat slash */
    267 
    268                 /* match the component */
    269                 tmp = ops->match(cur, component);
    270                 if (tmp && tmp->mp_data.mp_active) {
     299                /* Eat slash */
     300                next++;
     301               
     302                /* Match the component */
     303                rc = ops->match(&tmp, cur, component);
     304                on_error(rc, goto out_with_answer);
     305               
     306                if ((tmp) && (tmp->mp_data.mp_active)) {
    271307                        if (next > last)
    272308                                next = last = first;
    273309                        else
    274310                                next--;
    275                                
     311                       
    276312                        ipc_forward_slow(rid, tmp->mp_data.phone,
    277313                            VFS_OUT_LOOKUP, next, last, tmp->mp_data.dev_handle,
    278314                            lflag, index, IPC_FF_ROUTE_FROM_ME);
    279                         ops->node_put(cur);
    280                         ops->node_put(tmp);
     315                        (void) ops->node_put(cur);
     316                        (void) ops->node_put(tmp);
    281317                        if (par)
    282                                 ops->node_put(par);
     318                                (void) ops->node_put(par);
    283319                        return;
    284320                }
    285 
    286                 /* handle miss: match amongst siblings */
     321               
     322                /* Handle miss: match amongst siblings */
    287323                if (!tmp) {
    288324                        if (next <= last) {
    289                                 /* there are unprocessed components */
     325                                /* There are unprocessed components */
    290326                                ipc_answer_0(rid, ENOENT);
    291327                                goto out;
    292328                        }
    293                         /* miss in the last component */
    294                         if (lflag & (L_CREATE | L_LINK)) {
    295                                 /* request to create a new link */
     329                       
     330                        /* Miss in the last component */
     331                        if (lflag & (L_CREATE | L_LINK)) {
     332                                /* Request to create a new link */
    296333                                if (!ops->is_directory(cur)) {
    297334                                        ipc_answer_0(rid, ENOTDIR);
    298335                                        goto out;
    299336                                }
     337                               
    300338                                fs_node_t *fn;
    301339                                if (lflag & L_CREATE)
    302                                         fn = ops->create(dev_handle, lflag);
     340                                        rc = ops->create(&fn, dev_handle,
     341                                            lflag);
    303342                                else
    304                                         fn = ops->node_get(dev_handle,
     343                                        rc = ops->node_get(&fn, dev_handle,
    305344                                            index);
     345                                on_error(rc, goto out_with_answer);
     346                               
    306347                                if (fn) {
    307                                         int rc;
    308 
    309348                                        rc = ops->link(cur, fn, component);
    310349                                        if (rc != EOK) {
    311                                                 if (lflag & L_CREATE) {
    312                                                         (void)ops->destroy(fn);
    313                                                 }
     350                                                if (lflag & L_CREATE)
     351                                                        (void) ops->destroy(fn);
    314352                                                ipc_answer_0(rid, rc);
    315353                                        } else {
     
    319357                                                    ops->size_get(fn),
    320358                                                    ops->lnkcnt_get(fn));
    321                                                 ops->node_put(fn);
     359                                                (void) ops->node_put(fn);
    322360                                        }
    323                                 } else {
     361                                } else
    324362                                        ipc_answer_0(rid, ENOSPC);
    325                                 }
     363                               
    326364                                goto out;
    327                         }
     365                        }
     366                       
    328367                        ipc_answer_0(rid, ENOENT);
    329368                        goto out;
    330369                }
    331 
    332                 if (par)
    333                         ops->node_put(par);
    334 
    335                 /* descend one level */
     370               
     371                if (par) {
     372                        rc = ops->node_put(par);
     373                        on_error(rc, goto out_with_answer);
     374                }
     375               
     376                /* Descend one level */
    336377                par = cur;
    337378                cur = tmp;
    338379                tmp = NULL;
    339380        }
    340 
    341         /* handle miss: excessive components */
    342         if (next <= last && !ops->has_children(cur)) {
     381       
     382        /* Handle miss: excessive components */
     383        if (next <= last) {
     384                bool has_children;
     385                rc = ops->has_children(&has_children, cur);
     386                on_error(rc, goto out_with_answer);
     387               
     388                if (has_children)
     389                        goto skip_miss;
     390               
    343391                if (lflag & (L_CREATE | L_LINK)) {
    344392                        if (!ops->is_directory(cur)) {
     
    346394                                goto out;
    347395                        }
    348 
    349                         /* collect next component */
     396                       
     397                        /* Collect next component */
    350398                        len = 0;
    351399                        while (next <= last) {
    352400                                if (ops->plb_get_char(next) == '/') {
    353                                         /* more than one component */
     401                                        /* More than one component */
    354402                                        ipc_answer_0(rid, ENOENT);
    355403                                        goto out;
    356404                                }
     405                               
    357406                                if (len + 1 == NAME_MAX) {
    358                                         /* component length overflow */
     407                                        /* Component length overflow */
    359408                                        ipc_answer_0(rid, ENAMETOOLONG);
    360409                                        goto out;
    361410                                }
     411                               
    362412                                component[len++] = ops->plb_get_char(next);
    363                                 next++; /* process next character */
     413                                /* Process next character */
     414                                next++;
    364415                        }
     416                       
    365417                        assert(len);
    366418                        component[len] = '\0';
    367                                
     419                       
    368420                        fs_node_t *fn;
    369421                        if (lflag & L_CREATE)
    370                                 fn = ops->create(dev_handle, lflag);
     422                                rc = ops->create(&fn, dev_handle, lflag);
    371423                        else
    372                                 fn = ops->node_get(dev_handle, index);
     424                                rc = ops->node_get(&fn, dev_handle, index);
     425                        on_error(rc, goto out_with_answer);
     426                       
    373427                        if (fn) {
    374                                 int rc;
    375 
    376428                                rc = ops->link(cur, fn, component);
    377429                                if (rc != EOK) {
    378430                                        if (lflag & L_CREATE)
    379                                                 (void)ops->destroy(fn);
     431                                                (void) ops->destroy(fn);
    380432                                        ipc_answer_0(rid, rc);
    381433                                } else {
     
    385437                                            ops->size_get(fn),
    386438                                            ops->lnkcnt_get(fn));
    387                                         ops->node_put(fn);
     439                                        (void) ops->node_put(fn);
    388440                                }
    389                         } else {
     441                        } else
    390442                                ipc_answer_0(rid, ENOSPC);
    391                         }
     443                       
    392444                        goto out;
    393445                }
     446               
    394447                ipc_answer_0(rid, ENOENT);
    395448                goto out;
    396449        }
    397 
    398         /* handle hit */
     450       
     451skip_miss:
     452       
     453        /* Handle hit */
    399454        if (lflag & L_UNLINK) {
    400                 unsigned old_lnkcnt = ops->lnkcnt_get(cur);
    401                 int res = ops->unlink(par, cur, component);
    402                 ipc_answer_5(rid, (ipcarg_t)res, fs_handle, dev_handle,
     455                unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
     456                rc = ops->unlink(par, cur, component);
     457                ipc_answer_5(rid, (ipcarg_t) rc, fs_handle, dev_handle,
    403458                    ops->index_get(cur), ops->size_get(cur), old_lnkcnt);
    404459                goto out;
    405460        }
     461       
    406462        if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
    407463            (lflag & L_LINK)) {
     
    409465                goto out;
    410466        }
     467       
    411468        if ((lflag & L_FILE) && (ops->is_directory(cur))) {
    412469                ipc_answer_0(rid, EISDIR);
    413470                goto out;
    414471        }
     472       
    415473        if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
    416474                ipc_answer_0(rid, ENOTDIR);
    417475                goto out;
    418476        }
    419 
    420         ipc_answer_5(rid, EOK, fs_handle, dev_handle, ops->index_get(cur),
    421             ops->size_get(cur), ops->lnkcnt_get(cur));
    422 
     477       
     478out_with_answer:
     479       
     480        if (rc == EOK) {
     481                if (lflag & L_OPEN)
     482                        rc = ops->node_open(cur);
     483               
     484                ipc_answer_5(rid, rc, fs_handle, dev_handle,
     485                    ops->index_get(cur), ops->size_get(cur),
     486                    ops->lnkcnt_get(cur));
     487        } else
     488                ipc_answer_0(rid, rc);
     489       
    423490out:
     491       
    424492        if (par)
    425                 ops->node_put(par);
     493                (void) ops->node_put(par);
     494       
    426495        if (cur)
    427                 ops->node_put(cur);
     496                (void) ops->node_put(cur);
     497       
    428498        if (tmp)
    429                 ops->node_put(tmp);
     499                (void) ops->node_put(tmp);
    430500}
    431501
     
    435505        dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
    436506        fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    437         fs_node_t *fn = ops->node_get(dev_handle, index);
    438 
     507       
     508        fs_node_t *fn;
     509        int rc = ops->node_get(&fn, dev_handle, index);
     510        on_error(rc, answer_and_return(rid, rc));
     511       
    439512        ipc_callid_t callid;
    440513        size_t size;
    441         if (!ipc_data_read_receive(&callid, &size) ||
    442             size != sizeof(struct stat)) {
     514        if ((!async_data_read_receive(&callid, &size)) ||
     515            (size != sizeof(struct stat))) {
     516                ops->node_put(fn);
    443517                ipc_answer_0(callid, EINVAL);
    444518                ipc_answer_0(rid, EINVAL);
    445519                return;
    446520        }
    447 
     521       
    448522        struct stat stat;
    449523        memset(&stat, 0, sizeof(struct stat));
     
    452526        stat.dev_handle = dev_handle;
    453527        stat.index = index;
    454         stat.lnkcnt = ops->lnkcnt_get(fn); 
     528        stat.lnkcnt = ops->lnkcnt_get(fn);
    455529        stat.is_file = ops->is_file(fn);
     530        stat.is_directory = ops->is_directory(fn);
    456531        stat.size = ops->size_get(fn);
    457 
    458         ipc_data_read_finalize(callid, &stat, sizeof(struct stat));
     532        stat.device = ops->device_get(fn);
     533       
     534        ops->node_put(fn);
     535       
     536        async_data_read_finalize(callid, &stat, sizeof(struct stat));
    459537        ipc_answer_0(rid, EOK);
    460538}
     
    462540/** Open VFS triplet.
    463541 *
    464  * @param ops       libfs operations structure with function pointers to
    465  *                  file system implementation
    466  * @param rid       Request ID of the VFS_OUT_OPEN_NODE request.
    467  * @param request   VFS_OUT_OPEN_NODE request data itself.
     542 * @param ops     libfs operations structure with function pointers to
     543 *                file system implementation
     544 * @param rid     Request ID of the VFS_OUT_OPEN_NODE request.
     545 * @param request VFS_OUT_OPEN_NODE request data itself.
    468546 *
    469547 */
     
    473551        dev_handle_t dev_handle = IPC_GET_ARG1(*request);
    474552        fs_index_t index = IPC_GET_ARG2(*request);
    475        
    476         fs_node_t *node = ops->node_get(dev_handle, index);
    477        
    478         if (node == NULL) {
     553        fs_node_t *fn;
     554        int rc;
     555       
     556        rc = ops->node_get(&fn, dev_handle, index);
     557        on_error(rc, answer_and_return(rid, rc));
     558       
     559        if (fn == NULL) {
    479560                ipc_answer_0(rid, ENOENT);
    480561                return;
    481562        }
    482563       
    483         ipc_answer_5(rid, EOK, fs_handle, dev_handle, index,
    484             ops->size_get(node), ops->lnkcnt_get(node));
    485        
    486         ops->node_put(node);
     564        rc = ops->node_open(fn);
     565        ipc_answer_3(rid, rc, ops->size_get(fn), ops->lnkcnt_get(fn),
     566            (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
     567       
     568        (void) ops->node_put(fn);
    487569}
    488570
Note: See TracChangeset for help on using the changeset viewer.