Changeset 3b3e776 in mainline for uspace/lib/libfs/libfs.c


Ignore:
Timestamp:
2010-02-05T10:57:50Z (14 years ago)
Author:
Lenka Trochtova <trochtova.lenka@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0358da0
Parents:
3f085132 (diff), b4cbef1 (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:

merged with head

File:
1 edited

Legend:

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

    r3f085132 r3b3e776  
    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.
     34 * Glue code which is common to all FS implementations.
    3535 */
    3636
    37 #include "libfs.h" 
     37#include "libfs.h"
    3838#include "../../srv/vfs/vfs.h"
    3939#include <errno.h>
     
    6767 * code.
    6868 *
    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.
     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 *
    7879 */
    7980int fs_register(int vfs_phone, fs_reg_t *reg, vfs_info_t *info,
     
    8788        ipc_call_t answer;
    8889        aid_t req = async_send_0(vfs_phone, VFS_IN_REGISTER, &answer);
    89 
     90       
    9091        /*
    9192         * Send our VFS info structure to VFS.
     
    9697                return rc;
    9798        }
    98 
     99       
    99100        /*
    100101         * Ask VFS for callback connection.
    101102         */
    102103        ipc_connect_to_me(vfs_phone, 0, 0, 0, &reg->vfs_phonehash);
    103 
     104       
    104105        /*
    105106         * Allocate piece of address space for PLB.
     
    110111                return ENOMEM;
    111112        }
    112 
     113       
    113114        /*
    114115         * Request sharing the Path Lookup Buffer with VFS.
     
    136137         */
    137138        async_set_client_connection(conn);
    138 
     139       
    139140        return IPC_GET_RETVAL(answer);
    140141}
     
    154155        int res;
    155156        ipcarg_t rc;
    156 
     157       
    157158        ipc_call_t call;
    158159        ipc_callid_t callid;
    159 
    160         /* accept the phone */
     160       
     161        /* Accept the phone */
    161162        callid = async_get_call(&call);
    162         int mountee_phone = (int)IPC_GET_ARG1(call);
     163        int mountee_phone = (int) IPC_GET_ARG1(call);
    163164        if ((IPC_GET_METHOD(call) != IPC_M_CONNECTION_CLONE) ||
    164             mountee_phone < 0) {
     165            (mountee_phone < 0)) {
    165166                ipc_answer_0(callid, EINVAL);
    166167                ipc_answer_0(rid, EINVAL);
    167168                return;
    168169        }
    169         ipc_answer_0(callid, EOK);      /* acknowledge the mountee_phone */
    170        
    171         res = async_data_write_receive(&callid, NULL);
    172         if (!res) {
    173                 ipc_hangup(mountee_phone);
    174                 ipc_answer_0(callid, EINVAL);
    175                 ipc_answer_0(rid, EINVAL);
    176                 return;
    177         }
    178 
     170       
     171        /* Acknowledge the mountee_phone */
     172        ipc_answer_0(callid, EOK);
     173       
    179174        fs_node_t *fn;
    180175        res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
    181         if (res != EOK || !fn) {
     176        if ((res != EOK) || (!fn)) {
    182177                ipc_hangup(mountee_phone);
    183                 ipc_answer_0(callid, combine_rc(res, ENOENT));
     178                async_data_void(combine_rc(res, ENOENT));
    184179                ipc_answer_0(rid, combine_rc(res, ENOENT));
    185180                return;
    186181        }
    187 
     182       
    188183        if (fn->mp_data.mp_active) {
    189184                ipc_hangup(mountee_phone);
    190185                (void) ops->node_put(fn);
    191                 ipc_answer_0(callid, EBUSY);
     186                async_data_void(EBUSY);
    192187                ipc_answer_0(rid, EBUSY);
    193188                return;
    194189        }
    195 
     190       
    196191        rc = async_req_0_0(mountee_phone, IPC_M_CONNECT_ME);
    197192        if (rc != EOK) {
    198193                ipc_hangup(mountee_phone);
    199194                (void) ops->node_put(fn);
    200                 ipc_answer_0(callid, rc);
     195                async_data_void(rc);
    201196                ipc_answer_0(rid, rc);
    202197                return;
     
    204199       
    205200        ipc_call_t answer;
    206         aid_t msg = async_send_1(mountee_phone, VFS_OUT_MOUNTED, mr_dev_handle,
     201        rc = async_data_forward_1_1(mountee_phone, VFS_OUT_MOUNTED, mr_dev_handle,
    207202            &answer);
    208         ipc_forward_fast(callid, mountee_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
    209         async_wait_for(msg, &rc);
    210203       
    211204        if (rc == EOK) {
     
    215208                fn->mp_data.phone = mountee_phone;
    216209        }
     210       
    217211        /*
    218212         * Do not release the FS node so that it stays in memory.
     
    220214        ipc_answer_3(rid, rc, IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
    221215            IPC_GET_ARG3(answer));
     216}
     217
     218void libfs_unmount(libfs_ops_t *ops, ipc_callid_t rid, ipc_call_t *request)
     219{
     220        dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
     221        fs_index_t mp_fs_index = (fs_index_t) IPC_GET_ARG2(*request);
     222        fs_node_t *fn;
     223        int res;
     224
     225        res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
     226        if ((res != EOK) || (!fn)) {
     227                ipc_answer_0(rid, combine_rc(res, ENOENT));
     228                return;
     229        }
     230
     231        /*
     232         * We are clearly expecting to find the mount point active.
     233         */
     234        if (!fn->mp_data.mp_active) {
     235                (void) ops->node_put(fn);
     236                ipc_answer_0(rid, EINVAL);
     237                return;
     238        }
     239
     240        /*
     241         * Tell the mounted file system to unmount.
     242         */
     243        res = async_req_1_0(fn->mp_data.phone, VFS_OUT_UNMOUNTED,
     244            fn->mp_data.dev_handle);
     245
     246        /*
     247         * If everything went well, perform the clean-up on our side.
     248         */
     249        if (res == EOK) {
     250                ipc_hangup(fn->mp_data.phone);
     251                fn->mp_data.mp_active = false;
     252                fn->mp_data.fs_handle = 0;
     253                fn->mp_data.dev_handle = 0;
     254                fn->mp_data.phone = 0;
     255                /* Drop the reference created in libfs_mount(). */
     256                (void) ops->node_put(fn);
     257        }
     258
     259        (void) ops->node_put(fn);
     260        ipc_answer_0(rid, res);
    222261}
    223262
     
    238277    ipc_call_t *request)
    239278{
    240         unsigned first = IPC_GET_ARG1(*request);
    241         unsigned last = IPC_GET_ARG2(*request);
    242         unsigned next = first;
     279        unsigned int first = IPC_GET_ARG1(*request);
     280        unsigned int last = IPC_GET_ARG2(*request);
     281        unsigned int next = first;
    243282        dev_handle_t dev_handle = IPC_GET_ARG3(*request);
    244283        int lflag = IPC_GET_ARG4(*request);
    245         fs_index_t index = IPC_GET_ARG5(*request); /* when L_LINK specified */
     284        fs_index_t index = IPC_GET_ARG5(*request);
    246285        char component[NAME_MAX + 1];
    247286        int len;
    248287        int rc;
    249 
     288       
    250289        if (last < next)
    251290                last += PLB_SIZE;
    252 
     291       
    253292        fs_node_t *par = NULL;
    254293        fs_node_t *cur = NULL;
    255294        fs_node_t *tmp = NULL;
    256 
     295       
    257296        rc = ops->root_get(&cur, dev_handle);
    258297        on_error(rc, goto out_with_answer);
    259 
     298       
    260299        if (cur->mp_data.mp_active) {
    261300                ipc_forward_slow(rid, cur->mp_data.phone, VFS_OUT_LOOKUP,
     
    265304                return;
    266305        }
    267 
     306       
     307        /* Eat slash */
    268308        if (ops->plb_get_char(next) == '/')
    269                 next++;         /* eat slash */
     309                next++;
    270310       
    271311        while (next <= last) {
    272312                bool has_children;
    273 
     313               
    274314                rc = ops->has_children(&has_children, cur);
    275315                on_error(rc, goto out_with_answer);
    276316                if (!has_children)
    277317                        break;
    278 
    279                 /* collect the component */
     318               
     319                /* Collect the component */
    280320                len = 0;
    281                 while ((next <= last) &&  (ops->plb_get_char(next) != '/')) {
     321                while ((next <= last) && (ops->plb_get_char(next) != '/')) {
    282322                        if (len + 1 == NAME_MAX) {
    283                                 /* component length overflow */
     323                                /* Component length overflow */
    284324                                ipc_answer_0(rid, ENAMETOOLONG);
    285325                                goto out;
    286326                        }
    287327                        component[len++] = ops->plb_get_char(next);
    288                         next++; /* process next character */
     328                        /* Process next character */
     329                        next++;
    289330                }
    290 
     331               
    291332                assert(len);
    292333                component[len] = '\0';
    293                 next++;         /* eat slash */
    294 
    295                 /* match the component */
     334                /* Eat slash */
     335                next++;
     336               
     337                /* Match the component */
    296338                rc = ops->match(&tmp, cur, component);
    297339                on_error(rc, goto out_with_answer);
    298 
    299                 if (tmp && tmp->mp_data.mp_active) {
     340               
     341                /*
     342                 * If the matching component is a mount point, there are two
     343                 * legitimate semantics of the lookup operation. The first is
     344                 * the commonly used one in which the lookup crosses each mount
     345                 * point into the mounted file system. The second semantics is
     346                 * used mostly during unmount() and differs from the first one
     347                 * only in that the last mount point in the looked up path,
     348                 * which is also its last component, is not crossed.
     349                 */
     350
     351                if ((tmp) && (tmp->mp_data.mp_active) &&
     352                    (!(lflag & L_MP) || (next <= last))) {
    300353                        if (next > last)
    301354                                next = last = first;
    302355                        else
    303356                                next--;
    304                                
     357                       
    305358                        ipc_forward_slow(rid, tmp->mp_data.phone,
    306359                            VFS_OUT_LOOKUP, next, last, tmp->mp_data.dev_handle,
     
    312365                        return;
    313366                }
    314 
    315                 /* handle miss: match amongst siblings */
     367               
     368                /* Handle miss: match amongst siblings */
    316369                if (!tmp) {
    317370                        if (next <= last) {
    318                                 /* there are unprocessed components */
     371                                /* There are unprocessed components */
    319372                                ipc_answer_0(rid, ENOENT);
    320373                                goto out;
    321374                        }
    322                         /* miss in the last component */
    323                         if (lflag & (L_CREATE | L_LINK)) {
    324                                 /* request to create a new link */
     375                       
     376                        /* Miss in the last component */
     377                        if (lflag & (L_CREATE | L_LINK)) {
     378                                /* Request to create a new link */
    325379                                if (!ops->is_directory(cur)) {
    326380                                        ipc_answer_0(rid, ENOTDIR);
    327381                                        goto out;
    328382                                }
     383                               
    329384                                fs_node_t *fn;
    330385                                if (lflag & L_CREATE)
     
    335390                                            index);
    336391                                on_error(rc, goto out_with_answer);
     392                               
    337393                                if (fn) {
    338394                                        rc = ops->link(cur, fn, component);
     
    349405                                                (void) ops->node_put(fn);
    350406                                        }
    351                                 } else {
     407                                } else
    352408                                        ipc_answer_0(rid, ENOSPC);
    353                                 }
     409                               
    354410                                goto out;
    355                         }
     411                        }
     412                       
    356413                        ipc_answer_0(rid, ENOENT);
    357414                        goto out;
    358415                }
    359 
     416               
    360417                if (par) {
    361418                        rc = ops->node_put(par);
    362419                        on_error(rc, goto out_with_answer);
    363420                }
    364 
    365                 /* descend one level */
     421               
     422                /* Descend one level */
    366423                par = cur;
    367424                cur = tmp;
    368425                tmp = NULL;
    369426        }
    370 
    371         /* handle miss: excessive components */
     427       
     428        /* Handle miss: excessive components */
    372429        if (next <= last) {
    373430                bool has_children;
    374 
    375431                rc = ops->has_children(&has_children, cur);
    376432                on_error(rc, goto out_with_answer);
     433               
    377434                if (has_children)
    378435                        goto skip_miss;
    379 
     436               
    380437                if (lflag & (L_CREATE | L_LINK)) {
    381438                        if (!ops->is_directory(cur)) {
     
    383440                                goto out;
    384441                        }
    385 
    386                         /* collect next component */
     442                       
     443                        /* Collect next component */
    387444                        len = 0;
    388445                        while (next <= last) {
    389446                                if (ops->plb_get_char(next) == '/') {
    390                                         /* more than one component */
     447                                        /* More than one component */
    391448                                        ipc_answer_0(rid, ENOENT);
    392449                                        goto out;
    393450                                }
     451                               
    394452                                if (len + 1 == NAME_MAX) {
    395                                         /* component length overflow */
     453                                        /* Component length overflow */
    396454                                        ipc_answer_0(rid, ENAMETOOLONG);
    397455                                        goto out;
    398456                                }
     457                               
    399458                                component[len++] = ops->plb_get_char(next);
    400                                 next++; /* process next character */
     459                                /* Process next character */
     460                                next++;
    401461                        }
     462                       
    402463                        assert(len);
    403464                        component[len] = '\0';
    404                                
     465                       
    405466                        fs_node_t *fn;
    406467                        if (lflag & L_CREATE)
     
    409470                                rc = ops->node_get(&fn, dev_handle, index);
    410471                        on_error(rc, goto out_with_answer);
     472                       
    411473                        if (fn) {
    412474                                rc = ops->link(cur, fn, component);
     
    423485                                        (void) ops->node_put(fn);
    424486                                }
    425                         } else {
     487                        } else
    426488                                ipc_answer_0(rid, ENOSPC);
    427                         }
     489                       
    428490                        goto out;
    429491                }
     492               
    430493                ipc_answer_0(rid, ENOENT);
    431494                goto out;
    432495        }
     496       
    433497skip_miss:
    434 
    435         /* handle hit */
     498       
     499        /* Handle hit */
    436500        if (lflag & L_UNLINK) {
    437                 unsigned old_lnkcnt = ops->lnkcnt_get(cur);
     501                unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
    438502                rc = ops->unlink(par, cur, component);
    439                 ipc_answer_5(rid, (ipcarg_t)rc, fs_handle, dev_handle,
     503                ipc_answer_5(rid, (ipcarg_t) rc, fs_handle, dev_handle,
    440504                    ops->index_get(cur), ops->size_get(cur), old_lnkcnt);
    441505                goto out;
    442506        }
     507       
    443508        if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
    444509            (lflag & L_LINK)) {
     
    446511                goto out;
    447512        }
     513       
    448514        if ((lflag & L_FILE) && (ops->is_directory(cur))) {
    449515                ipc_answer_0(rid, EISDIR);
    450516                goto out;
    451517        }
     518       
    452519        if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
    453520                ipc_answer_0(rid, ENOTDIR);
     
    455522        }
    456523
     524        if ((lflag & L_ROOT) && par) {
     525                ipc_answer_0(rid, EINVAL);
     526                goto out;
     527        }
     528       
    457529out_with_answer:
     530       
    458531        if (rc == EOK) {
    459                 ipc_answer_5(rid, EOK, fs_handle, dev_handle,
     532                if (lflag & L_OPEN)
     533                        rc = ops->node_open(cur);
     534               
     535                ipc_answer_5(rid, rc, fs_handle, dev_handle,
    460536                    ops->index_get(cur), ops->size_get(cur),
    461537                    ops->lnkcnt_get(cur));
    462         } else {
     538        } else
    463539                ipc_answer_0(rid, rc);
    464         }
    465 
     540       
    466541out:
     542       
    467543        if (par)
    468544                (void) ops->node_put(par);
     545       
    469546        if (cur)
    470547                (void) ops->node_put(cur);
     548       
    471549        if (tmp)
    472550                (void) ops->node_put(tmp);
     
    478556        dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
    479557        fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
     558       
    480559        fs_node_t *fn;
    481         int rc;
    482 
    483         rc = ops->node_get(&fn, dev_handle, index);
     560        int rc = ops->node_get(&fn, dev_handle, index);
    484561        on_error(rc, answer_and_return(rid, rc));
    485 
     562       
    486563        ipc_callid_t callid;
    487564        size_t size;
    488         if (!async_data_read_receive(&callid, &size) ||
    489             size != sizeof(struct stat)) {
     565        if ((!async_data_read_receive(&callid, &size)) ||
     566            (size != sizeof(struct stat))) {
     567                ops->node_put(fn);
    490568                ipc_answer_0(callid, EINVAL);
    491569                ipc_answer_0(rid, EINVAL);
    492570                return;
    493571        }
    494 
     572       
    495573        struct stat stat;
    496574        memset(&stat, 0, sizeof(struct stat));
     
    499577        stat.dev_handle = dev_handle;
    500578        stat.index = index;
    501         stat.lnkcnt = ops->lnkcnt_get(fn); 
     579        stat.lnkcnt = ops->lnkcnt_get(fn);
    502580        stat.is_file = ops->is_file(fn);
     581        stat.is_directory = ops->is_directory(fn);
    503582        stat.size = ops->size_get(fn);
    504 
     583        stat.device = ops->device_get(fn);
     584       
     585        ops->node_put(fn);
     586       
    505587        async_data_read_finalize(callid, &stat, sizeof(struct stat));
    506588        ipc_answer_0(rid, EOK);
     
    509591/** Open VFS triplet.
    510592 *
    511  * @param ops       libfs operations structure with function pointers to
    512  *                  file system implementation
    513  * @param rid       Request ID of the VFS_OUT_OPEN_NODE request.
    514  * @param request   VFS_OUT_OPEN_NODE request data itself.
     593 * @param ops     libfs operations structure with function pointers to
     594 *                file system implementation
     595 * @param rid     Request ID of the VFS_OUT_OPEN_NODE request.
     596 * @param request VFS_OUT_OPEN_NODE request data itself.
    515597 *
    516598 */
     
    531613        }
    532614       
    533         ipc_answer_3(rid, EOK, ops->size_get(fn), ops->lnkcnt_get(fn),
     615        rc = ops->node_open(fn);
     616        ipc_answer_3(rid, rc, ops->size_get(fn), ops->lnkcnt_get(fn),
    534617            (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
    535618       
Note: See TracChangeset for help on using the changeset viewer.