Changeset 41811af in mainline for uspace/lib/c/generic/vfs/vfs.c


Ignore:
Timestamp:
2011-06-10T10:14:26Z (14 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ab547063
Parents:
9536e6e (diff), 390d80d (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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/vfs/vfs.c

    r9536e6e r41811af  
    3333 */
    3434
     35#include <vfs/canonify.h>
    3536#include <vfs/vfs.h>
    36 #include <vfs/canonify.h>
     37#include <vfs/vfs_sess.h>
    3738#include <macros.h>
    3839#include <stdlib.h>
     
    4445#include <sys/types.h>
    4546#include <ipc/services.h>
    46 #include <ipc/ns.h>
     47#include <ns.h>
    4748#include <async.h>
    4849#include <fibril_synch.h>
     
    5455#include <ipc/devmap.h>
    5556
    56 static async_sess_t vfs_session;
    57 
    58 static FIBRIL_MUTEX_INITIALIZE(vfs_phone_mutex);
    59 static int vfs_phone = -1;
     57static FIBRIL_MUTEX_INITIALIZE(vfs_mutex);
     58static async_sess_t *vfs_sess = NULL;
    6059
    6160static FIBRIL_MUTEX_INITIALIZE(cwd_mutex);
     
    6564static size_t cwd_size = 0;
    6665
     66/** Start an async exchange on the VFS session.
     67 *
     68 * @return New exchange.
     69 *
     70 */
     71static async_exch_t *vfs_exchange_begin(void)
     72{
     73        fibril_mutex_lock(&vfs_mutex);
     74       
     75        while (vfs_sess == NULL)
     76                vfs_sess = service_connect_blocking(EXCHANGE_PARALLEL, SERVICE_VFS,
     77                    0, 0);
     78       
     79        fibril_mutex_unlock(&vfs_mutex);
     80       
     81        return async_exchange_begin(vfs_sess);
     82}
     83
     84/** Finish an async exchange on the VFS session.
     85 *
     86 * @param exch Exchange to be finished.
     87 *
     88 */
     89static void vfs_exchange_end(async_exch_t *exch)
     90{
     91        async_exchange_end(exch);
     92}
     93
    6794char *absolutize(const char *path, size_t *retlen)
    6895{
    6996        char *ncwd_path;
    7097        char *ncwd_path_nc;
    71         size_t total_size;
    7298
    7399        fibril_mutex_lock(&cwd_mutex);
     
    78104                        return NULL;
    79105                }
    80                 total_size = cwd_size + 1 + size + 1;
    81                 ncwd_path_nc = malloc(total_size);
     106                ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
    82107                if (!ncwd_path_nc) {
    83108                        fibril_mutex_unlock(&cwd_mutex);
    84109                        return NULL;
    85110                }
    86                 str_cpy(ncwd_path_nc, total_size, cwd_path);
     111                str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
    87112                ncwd_path_nc[cwd_size] = '/';
    88113                ncwd_path_nc[cwd_size + 1] = '\0';
    89114        } else {
    90                 total_size = size + 1;
    91                 ncwd_path_nc = malloc(total_size);
     115                ncwd_path_nc = malloc(size + 1);
    92116                if (!ncwd_path_nc) {
    93117                        fibril_mutex_unlock(&cwd_mutex);
     
    96120                ncwd_path_nc[0] = '\0';
    97121        }
    98         str_append(ncwd_path_nc, total_size, path);
     122        str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
    99123        ncwd_path = canonify(ncwd_path_nc, retlen);
    100124        if (!ncwd_path) {
     
    118142}
    119143
    120 /** Connect to VFS service and create session. */
    121 static void vfs_connect(void)
    122 {
    123         while (vfs_phone < 0)
    124                 vfs_phone = service_connect_blocking(SERVICE_VFS, 0, 0);
    125        
    126         async_session_create(&vfs_session, vfs_phone, 0);
    127 }
    128 
    129 /** Start an async exchange on the VFS session.
    130  *
    131  * @return              New phone to be used during the exchange.
    132  */
    133 static int vfs_exchange_begin(void)
    134 {
    135         fibril_mutex_lock(&vfs_phone_mutex);
    136         if (vfs_phone < 0)
    137                 vfs_connect();
    138         fibril_mutex_unlock(&vfs_phone_mutex);
    139 
    140         return async_exchange_begin(&vfs_session);
    141 }
    142 
    143 /** End an async exchange on the VFS session.
    144  *
    145  * @param phone         Phone used during the exchange.
    146  */
    147 static void vfs_exchange_end(int phone)
    148 {
    149         async_exchange_end(&vfs_session, phone);
    150 }
    151 
    152144int mount(const char *fs_name, const char *mp, const char *fqdn,
    153145    const char *opts, unsigned int flags)
     
    186178        }
    187179       
    188         int vfs_phone = vfs_exchange_begin();
     180        async_exch_t *exch = vfs_exchange_begin();
    189181
    190182        sysarg_t rc_orig;
    191         aid_t req = async_send_2(vfs_phone, VFS_IN_MOUNT, devmap_handle, flags, NULL);
    192         sysarg_t rc = async_data_write_start(vfs_phone, (void *) mpa, mpa_size);
    193         if (rc != EOK) {
    194                 vfs_exchange_end(vfs_phone);
     183        aid_t req = async_send_2(exch, VFS_IN_MOUNT, devmap_handle, flags, NULL);
     184        sysarg_t rc = async_data_write_start(exch, (void *) mpa, mpa_size);
     185        if (rc != EOK) {
     186                vfs_exchange_end(exch);
    195187                free(mpa);
    196188                async_wait_for(req, &rc_orig);
     
    205197        }
    206198       
    207         rc = async_data_write_start(vfs_phone, (void *) opts, str_size(opts));
    208         if (rc != EOK) {
    209                 vfs_exchange_end(vfs_phone);
     199        rc = async_data_write_start(exch, (void *) opts, str_size(opts));
     200        if (rc != EOK) {
     201                vfs_exchange_end(exch);
    210202                free(mpa);
    211203                async_wait_for(req, &rc_orig);
     
    220212        }
    221213       
    222         rc = async_data_write_start(vfs_phone, (void *) fs_name, str_size(fs_name));
    223         if (rc != EOK) {
    224                 vfs_exchange_end(vfs_phone);
     214        rc = async_data_write_start(exch, (void *) fs_name, str_size(fs_name));
     215        if (rc != EOK) {
     216                vfs_exchange_end(exch);
    225217                free(mpa);
    226218                async_wait_for(req, &rc_orig);
     
    236228       
    237229        /* Ask VFS whether it likes fs_name. */
    238         rc = async_req_0_0(vfs_phone, IPC_M_PING);
    239         if (rc != EOK) {
    240                 vfs_exchange_end(vfs_phone);
     230        rc = async_req_0_0(exch, VFS_IN_PING);
     231        if (rc != EOK) {
     232                vfs_exchange_end(exch);
    241233                free(mpa);
    242234                async_wait_for(req, &rc_orig);
     
    251243        }
    252244       
    253         vfs_exchange_end(vfs_phone);
     245        vfs_exchange_end(exch);
    254246        free(mpa);
    255247        async_wait_for(req, &rc);
     
    273265                return ENOMEM;
    274266       
    275         int vfs_phone = vfs_exchange_begin();
    276        
    277         req = async_send_0(vfs_phone, VFS_IN_UNMOUNT, NULL);
    278         rc = async_data_write_start(vfs_phone, (void *) mpa, mpa_size);
    279         if (rc != EOK) {
    280                 vfs_exchange_end(vfs_phone);
     267        async_exch_t *exch = vfs_exchange_begin();
     268       
     269        req = async_send_0(exch, VFS_IN_UNMOUNT, NULL);
     270        rc = async_data_write_start(exch, (void *) mpa, mpa_size);
     271        if (rc != EOK) {
     272                vfs_exchange_end(exch);
    281273                free(mpa);
    282274                async_wait_for(req, &rc_orig);
     
    288280       
    289281
    290         vfs_exchange_end(vfs_phone);
     282        vfs_exchange_end(exch);
    291283        free(mpa);
    292284        async_wait_for(req, &rc);
     
    297289static int open_internal(const char *abs, size_t abs_size, int lflag, int oflag)
    298290{
    299         int vfs_phone = vfs_exchange_begin();
     291        async_exch_t *exch = vfs_exchange_begin();
    300292       
    301293        ipc_call_t answer;
    302         aid_t req = async_send_3(vfs_phone, VFS_IN_OPEN, lflag, oflag, 0, &answer);
    303         sysarg_t rc = async_data_write_start(vfs_phone, abs, abs_size);
    304        
    305         if (rc != EOK) {
    306                 vfs_exchange_end(vfs_phone);
     294        aid_t req = async_send_3(exch, VFS_IN_OPEN, lflag, oflag, 0, &answer);
     295        sysarg_t rc = async_data_write_start(exch, abs, abs_size);
     296       
     297        if (rc != EOK) {
     298                vfs_exchange_end(exch);
    307299
    308300                sysarg_t rc_orig;
     
    315307        }
    316308       
    317         vfs_exchange_end(vfs_phone);
     309        vfs_exchange_end(exch);
    318310        async_wait_for(req, &rc);
    319311       
     
    339331int open_node(fdi_node_t *node, int oflag)
    340332{
    341         int vfs_phone = vfs_exchange_begin();
     333        async_exch_t *exch = vfs_exchange_begin();
    342334       
    343335        ipc_call_t answer;
    344         aid_t req = async_send_4(vfs_phone, VFS_IN_OPEN_NODE, node->fs_handle,
     336        aid_t req = async_send_4(exch, VFS_IN_OPEN_NODE, node->fs_handle,
    345337            node->devmap_handle, node->index, oflag, &answer);
    346338       
    347         vfs_exchange_end(vfs_phone);
     339        vfs_exchange_end(exch);
    348340
    349341        sysarg_t rc;
     
    360352        sysarg_t rc;
    361353       
    362         int vfs_phone = vfs_exchange_begin();
    363        
    364         rc = async_req_1_0(vfs_phone, VFS_IN_CLOSE, fildes);
    365        
    366         vfs_exchange_end(vfs_phone);
    367        
    368         return (int)rc;
     354        async_exch_t *exch = vfs_exchange_begin();
     355        rc = async_req_1_0(exch, VFS_IN_CLOSE, fildes);
     356        vfs_exchange_end(exch);
     357       
     358        return (int) rc;
    369359}
    370360
     
    374364        ipc_call_t answer;
    375365        aid_t req;
    376 
    377         int vfs_phone = vfs_exchange_begin();
    378        
    379         req = async_send_1(vfs_phone, VFS_IN_READ, fildes, &answer);
    380         rc = async_data_read_start_generic(vfs_phone, (void *) buf, nbyte,
    381             IPC_XF_RESTRICT);
    382         if (rc != EOK) {
    383                 vfs_exchange_end(vfs_phone);
     366       
     367        async_exch_t *exch = vfs_exchange_begin();
     368       
     369        req = async_send_1(exch, VFS_IN_READ, fildes, &answer);
     370        rc = async_data_read_start(exch, (void *)buf, nbyte);
     371        if (rc != EOK) {
     372                vfs_exchange_end(exch);
    384373
    385374                sysarg_t rc_orig;
     
    391380                        return (ssize_t) rc_orig;
    392381        }
    393         vfs_exchange_end(vfs_phone);
     382        vfs_exchange_end(exch);
    394383        async_wait_for(req, &rc);
    395384        if (rc == EOK)
     
    404393        ipc_call_t answer;
    405394        aid_t req;
    406 
    407         int vfs_phone = vfs_exchange_begin();
    408        
    409         req = async_send_1(vfs_phone, VFS_IN_WRITE, fildes, &answer);
    410         rc = async_data_write_start_generic(vfs_phone, (void *) buf, nbyte,
    411             IPC_XF_RESTRICT);
    412         if (rc != EOK) {
    413                 vfs_exchange_end(vfs_phone);
     395       
     396        async_exch_t *exch = vfs_exchange_begin();
     397       
     398        req = async_send_1(exch, VFS_IN_WRITE, fildes, &answer);
     399        rc = async_data_write_start(exch, (void *)buf, nbyte);
     400        if (rc != EOK) {
     401                vfs_exchange_end(exch);
    414402
    415403                sysarg_t rc_orig;
     
    421409                        return (ssize_t) rc_orig;
    422410        }
    423         vfs_exchange_end(vfs_phone);
     411        vfs_exchange_end(exch);
    424412        async_wait_for(req, &rc);
    425413        if (rc == EOK)
     
    431419int fsync(int fildes)
    432420{
    433         int vfs_phone = vfs_exchange_begin();
    434        
    435         sysarg_t rc = async_req_1_0(vfs_phone, VFS_IN_SYNC, fildes);
    436        
    437         vfs_exchange_end(vfs_phone);
     421        async_exch_t *exch = vfs_exchange_begin();
     422        sysarg_t rc = async_req_1_0(exch, VFS_IN_SYNC, fildes);
     423        vfs_exchange_end(exch);
    438424       
    439425        return (int) rc;
     
    442428off64_t lseek(int fildes, off64_t offset, int whence)
    443429{
    444         int vfs_phone = vfs_exchange_begin();
     430        async_exch_t *exch = vfs_exchange_begin();
    445431       
    446432        sysarg_t newoff_lo;
    447433        sysarg_t newoff_hi;
    448         sysarg_t rc = async_req_4_2(vfs_phone, VFS_IN_SEEK, fildes,
     434        sysarg_t rc = async_req_4_2(exch, VFS_IN_SEEK, fildes,
    449435            LOWER32(offset), UPPER32(offset), whence,
    450436            &newoff_lo, &newoff_hi);
    451437       
    452         vfs_exchange_end(vfs_phone);
     438        vfs_exchange_end(exch);
    453439       
    454440        if (rc != EOK)
     
    462448        sysarg_t rc;
    463449       
    464         int vfs_phone = vfs_exchange_begin();
    465        
    466         rc = async_req_3_0(vfs_phone, VFS_IN_TRUNCATE, fildes,
     450        async_exch_t *exch = vfs_exchange_begin();
     451        rc = async_req_3_0(exch, VFS_IN_TRUNCATE, fildes,
    467452            LOWER32(length), UPPER32(length));
    468         vfs_exchange_end(vfs_phone);
     453        vfs_exchange_end(exch);
    469454       
    470455        return (int) rc;
     
    475460        sysarg_t rc;
    476461        aid_t req;
    477 
    478         int vfs_phone = vfs_exchange_begin();
    479        
    480         req = async_send_1(vfs_phone, VFS_IN_FSTAT, fildes, NULL);
    481         rc = async_data_read_start(vfs_phone, (void *) stat, sizeof(struct stat));
    482         if (rc != EOK) {
    483                 vfs_exchange_end(vfs_phone);
     462       
     463        async_exch_t *exch = vfs_exchange_begin();
     464       
     465        req = async_send_1(exch, VFS_IN_FSTAT, fildes, NULL);
     466        rc = async_data_read_start(exch, (void *) stat, sizeof(struct stat));
     467        if (rc != EOK) {
     468                vfs_exchange_end(exch);
    484469
    485470                sysarg_t rc_orig;
     
    491476                        return (ssize_t) rc_orig;
    492477        }
    493         vfs_exchange_end(vfs_phone);
     478        vfs_exchange_end(exch);
    494479        async_wait_for(req, &rc);
    495480
     
    508493                return ENOMEM;
    509494       
    510         int vfs_phone = vfs_exchange_begin();
    511        
    512         req = async_send_0(vfs_phone, VFS_IN_STAT, NULL);
    513         rc = async_data_write_start(vfs_phone, pa, pa_size);
    514         if (rc != EOK) {
    515                 vfs_exchange_end(vfs_phone);
     495        async_exch_t *exch = vfs_exchange_begin();
     496       
     497        req = async_send_0(exch, VFS_IN_STAT, NULL);
     498        rc = async_data_write_start(exch, pa, pa_size);
     499        if (rc != EOK) {
     500                vfs_exchange_end(exch);
    516501                free(pa);
    517502                async_wait_for(req, &rc_orig);
     
    521506                        return (int) rc_orig;
    522507        }
    523         rc = async_data_read_start(vfs_phone, stat, sizeof(struct stat));
    524         if (rc != EOK) {
    525                 vfs_exchange_end(vfs_phone);
     508        rc = async_data_read_start(exch, stat, sizeof(struct stat));
     509        if (rc != EOK) {
     510                vfs_exchange_end(exch);
    526511                free(pa);
    527512                async_wait_for(req, &rc_orig);
     
    531516                        return (int) rc_orig;
    532517        }
    533         vfs_exchange_end(vfs_phone);
     518        vfs_exchange_end(exch);
    534519        free(pa);
    535520        async_wait_for(req, &rc);
     
    592577                return ENOMEM;
    593578       
    594         int vfs_phone = vfs_exchange_begin();
    595        
    596         req = async_send_1(vfs_phone, VFS_IN_MKDIR, mode, NULL);
    597         rc = async_data_write_start(vfs_phone, pa, pa_size);
    598         if (rc != EOK) {
    599                 vfs_exchange_end(vfs_phone);
     579        async_exch_t *exch = vfs_exchange_begin();
     580       
     581        req = async_send_1(exch, VFS_IN_MKDIR, mode, NULL);
     582        rc = async_data_write_start(exch, pa, pa_size);
     583        if (rc != EOK) {
     584                vfs_exchange_end(exch);
    600585                free(pa);
    601586
     
    608593                        return (int) rc_orig;
    609594        }
    610         vfs_exchange_end(vfs_phone);
     595        vfs_exchange_end(exch);
    611596        free(pa);
    612597        async_wait_for(req, &rc);
     
    623608        if (!pa)
    624609                return ENOMEM;
    625 
    626         int vfs_phone = vfs_exchange_begin();
    627        
    628         req = async_send_0(vfs_phone, VFS_IN_UNLINK, NULL);
    629         rc = async_data_write_start(vfs_phone, pa, pa_size);
    630         if (rc != EOK) {
    631                 vfs_exchange_end(vfs_phone);
     610       
     611        async_exch_t *exch = vfs_exchange_begin();
     612       
     613        req = async_send_0(exch, VFS_IN_UNLINK, NULL);
     614        rc = async_data_write_start(exch, pa, pa_size);
     615        if (rc != EOK) {
     616                vfs_exchange_end(exch);
    632617                free(pa);
    633618
     
    640625                        return (int) rc_orig;
    641626        }
    642         vfs_exchange_end(vfs_phone);
     627        vfs_exchange_end(exch);
    643628        free(pa);
    644629        async_wait_for(req, &rc);
     
    673658                return ENOMEM;
    674659        }
    675 
    676         int vfs_phone = vfs_exchange_begin();
    677        
    678         req = async_send_0(vfs_phone, VFS_IN_RENAME, NULL);
    679         rc = async_data_write_start(vfs_phone, olda, olda_size);
    680         if (rc != EOK) {
    681                 vfs_exchange_end(vfs_phone);
     660       
     661        async_exch_t *exch = vfs_exchange_begin();
     662       
     663        req = async_send_0(exch, VFS_IN_RENAME, NULL);
     664        rc = async_data_write_start(exch, olda, olda_size);
     665        if (rc != EOK) {
     666                vfs_exchange_end(exch);
    682667                free(olda);
    683668                free(newa);
     
    688673                        return (int) rc_orig;
    689674        }
    690         rc = async_data_write_start(vfs_phone, newa, newa_size);
    691         if (rc != EOK) {
    692                 vfs_exchange_end(vfs_phone);
     675        rc = async_data_write_start(exch, newa, newa_size);
     676        if (rc != EOK) {
     677                vfs_exchange_end(exch);
    693678                free(olda);
    694679                free(newa);
     
    699684                        return (int) rc_orig;
    700685        }
    701         vfs_exchange_end(vfs_phone);
     686        vfs_exchange_end(exch);
    702687        free(olda);
    703688        free(newa);
     
    755740}
    756741
    757 int fd_phone(int fildes)
     742async_sess_t *fd_session(exch_mgmt_t mgmt, int fildes)
    758743{
    759744        struct stat stat;
    760        
    761745        int rc = fstat(fildes, &stat);
    762         if (rc != 0)
    763                 return rc;
    764        
    765         if (!stat.device)
    766                 return -1;
    767        
    768         return devmap_device_connect(stat.device, 0);
     746        if (rc != 0) {
     747                errno = rc;
     748                return NULL;
     749        }
     750       
     751        if (!stat.device) {
     752                errno = ENOENT;
     753                return NULL;
     754        }
     755       
     756        return devmap_device_connect(mgmt, stat.device, 0);
    769757}
    770758
     
    772760{
    773761        struct stat stat;
    774         int rc;
    775 
    776         rc = fstat(fildes, &stat);
     762        int rc = fstat(fildes, &stat);
    777763       
    778764        if (rc == EOK) {
     
    787773int dup2(int oldfd, int newfd)
    788774{
    789         int vfs_phone = vfs_exchange_begin();
     775        async_exch_t *exch = vfs_exchange_begin();
    790776       
    791777        sysarg_t ret;
    792         sysarg_t rc = async_req_2_1(vfs_phone, VFS_IN_DUP, oldfd, newfd, &ret);
    793        
    794         vfs_exchange_end(vfs_phone);
     778        sysarg_t rc = async_req_2_1(exch, VFS_IN_DUP, oldfd, newfd, &ret);
     779       
     780        vfs_exchange_end(exch);
    795781       
    796782        if (rc == EOK)
Note: See TracChangeset for help on using the changeset viewer.