Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/vfs/vfs_file.c

    rfcab7ef r3e6a98c5  
    4545#include <adt/list.h>
    4646#include <task.h>
    47 #include <vfs/vfs.h>
    4847#include "vfs.h"
    4948
     
    6059typedef struct {
    6160        link_t link;
    62         vfs_node_t *node;
    63         int permissions;
     61        int handle;
    6462} vfs_boxed_handle_t;
    6563
     
    179177                 * endpoint FS and drop our reference to the underlying VFS node.
    180178                 */
    181                
    182                 if (file->node != NULL) {
    183                         if (file->open_read || file->open_write) {
    184                                 rc = vfs_file_close_remote(file);
    185                         }
    186                         vfs_node_delref(file->node);
    187                 }
     179                rc = vfs_file_close_remote(file);
     180                vfs_node_delref(file->node);
    188181                free(file);
    189182        }
     
    192185}
    193186
    194 static int _vfs_fd_alloc(vfs_client_data_t *vfs_data, vfs_file_t **file, bool desc)
     187static int _vfs_fd_alloc(vfs_client_data_t *vfs_data, bool desc)
    195188{
    196189        if (!vfs_files_init(vfs_data))
     
    212205                        }
    213206                       
    214                        
    215207                        memset(vfs_data->files[i], 0, sizeof(vfs_file_t));
    216                        
    217                         fibril_mutex_initialize(&vfs_data->files[i]->_lock);
    218                         fibril_mutex_lock(&vfs_data->files[i]->_lock);
     208                        fibril_mutex_initialize(&vfs_data->files[i]->lock);
    219209                        vfs_file_addref(vfs_data, vfs_data->files[i]);
    220                        
    221                         *file = vfs_data->files[i];
    222                         vfs_file_addref(vfs_data, *file);
    223                        
    224210                        fibril_mutex_unlock(&vfs_data->lock);
    225211                        return (int) i;
     
    245231/** Allocate a file descriptor.
    246232 *
    247  * @param file Is set to point to the newly created file structure. Must be put afterwards.
    248233 * @param desc If true, look for an available file descriptor
    249234 *             in a descending order.
     
    252237 *         code.
    253238 */
    254 int vfs_fd_alloc(vfs_file_t **file, bool desc)
    255 {
    256         return _vfs_fd_alloc(VFS_DATA, file, desc);
     239int vfs_fd_alloc(bool desc)
     240{
     241        return _vfs_fd_alloc(VFS_DATA, desc);
    257242}
    258243
     
    304289
    305290        fibril_mutex_lock(&VFS_DATA->lock);     
    306         if ((fd < 0) || (fd >= MAX_OPEN_FILES)) {
     291        if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (FILES[fd] != NULL)) {
    307292                fibril_mutex_unlock(&VFS_DATA->lock);
    308                 return EBADF;
    309         }
    310         if (FILES[fd] != NULL) {
    311                 fibril_mutex_unlock(&VFS_DATA->lock);
    312                 return EEXIST;
     293                return EINVAL;
    313294        }
    314295       
     
    318299       
    319300        return EOK;
    320 }
    321 
    322 static void _vfs_file_put(vfs_client_data_t *vfs_data, vfs_file_t *file)
    323 {
    324         fibril_mutex_unlock(&file->_lock);
    325        
    326         fibril_mutex_lock(&vfs_data->lock);
    327         vfs_file_delref(vfs_data, file);
    328         fibril_mutex_unlock(&vfs_data->lock);
    329301}
    330302
     
    340312                        vfs_file_addref(vfs_data, file);
    341313                        fibril_mutex_unlock(&vfs_data->lock);
    342                        
    343                         fibril_mutex_lock(&file->_lock);
    344                         if (file->node == NULL) {
    345                                 _vfs_file_put(vfs_data, file);
    346                                 return NULL;
    347                         }
    348                         assert(file != NULL);
    349                         assert(file->node != NULL);
    350314                        return file;
    351315                }
     
    367331}
    368332
     333static void _vfs_file_put(vfs_client_data_t *vfs_data, vfs_file_t *file)
     334{
     335        fibril_mutex_lock(&vfs_data->lock);
     336        vfs_file_delref(vfs_data, file);
     337        fibril_mutex_unlock(&vfs_data->lock);
     338}
     339
    369340/** Stop using a file structure.
    370341 *
     
    376347}
    377348
    378 void vfs_op_pass_handle(task_id_t donor_id, task_id_t acceptor_id, int donor_fd)
     349void vfs_pass_handle(task_id_t donor_id, task_id_t acceptor_id, int donor_fd)
    379350{
    380351        vfs_client_data_t *donor_data = NULL;
    381352        vfs_client_data_t *acceptor_data = NULL;
    382353        vfs_file_t *donor_file = NULL;
     354        vfs_file_t *acceptor_file = NULL;
    383355        vfs_boxed_handle_t *bh;
     356        int acceptor_fd;
    384357
    385358        acceptor_data = async_get_client_data_by_id(acceptor_id);
     
    391364
    392365        link_initialize(&bh->link);
    393         bh->node = NULL;
     366        bh->handle = -1;
    394367
    395368        donor_data = async_get_client_data_by_id(donor_id);
     
    400373        if (!donor_file)
    401374                goto out;
     375
     376        acceptor_fd = _vfs_fd_alloc(acceptor_data, false);
     377        if (acceptor_fd < 0)
     378                goto out;
     379
     380        bh->handle = acceptor_fd;
    402381
    403382        /*
     
    405384         */
    406385        vfs_node_addref(donor_file->node);
    407         bh->node = donor_file->node;
    408         bh->permissions = donor_file->permissions;
     386        (void) vfs_open_node_remote(donor_file->node);
     387
     388        acceptor_file = _vfs_file_get(acceptor_data, acceptor_fd);
     389        assert(acceptor_file);
     390
     391        /*
     392         * Inherit attributes from the donor.
     393         */
     394        acceptor_file->node = donor_file->node;
     395        acceptor_file->append = donor_file->append;
     396        acceptor_file->pos = donor_file->pos;
    409397
    410398out:
     
    420408        if (donor_file)
    421409                _vfs_file_put(donor_data, donor_file);
    422 }
    423 
    424 int vfs_wait_handle_internal(bool high_fd)
     410        if (acceptor_file)
     411                _vfs_file_put(acceptor_data, acceptor_file);
     412
     413}
     414
     415int vfs_wait_handle_internal(void)
    425416{
    426417        vfs_client_data_t *vfs_data = VFS_DATA;
     418        int fd;
    427419       
    428420        fibril_mutex_lock(&vfs_data->lock);
     
    434426
    435427        vfs_boxed_handle_t *bh = list_get_instance(lnk, vfs_boxed_handle_t, link);
    436 
    437         vfs_file_t *file;
    438         int fd = _vfs_fd_alloc(vfs_data, &file, high_fd);
    439         if (fd < 0) {
    440                 vfs_node_delref(bh->node);
    441                 free(bh);
    442                 return fd;
    443         }
    444        
    445         file->node = bh->node;
    446         file->permissions = bh->permissions;
    447         vfs_file_put(file);
     428        fd = bh->handle;
    448429        free(bh);
     430
    449431        return fd;
    450432}
Note: See TracChangeset for help on using the changeset viewer.