Changeset 23 for trunk/kernel/vfs/vfs.c


Ignore:
Timestamp:
Jun 18, 2017, 10:06:41 PM (7 years ago)
Author:
alain
Message:

Introduce syscalls.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/vfs/vfs.c

    r14 r23  
    3535#include <slist.h>
    3636#include <xhtab.h>
     37#include <rpc.h>
    3738#include <errno.h>
    3839#include <kmem.h>
     
    4041#include <thread.h>
    4142#include <process.h>
     43#include <vfs.h>
    4244#include <fatfs.h>
    4345#include <ramfs.h>
    44 #include <vfs.h>
     46#include <devfs.h>
     47#include <syscalls.h>
    4548
    4649
     
    4952//////////////////////////////////////////////////////////////////////////////////////////
    5053
    51 // array of supported FS contexts (indexed by the FS type)
     54// array of supported FS contexts
    5255vfs_ctx_t   fs_context[FS_TYPES_NR];
    5356
     
    5659//////////////////////////////////////////////////////////////////////////////////////////
    5760
    58 /////////////////////////////////////////////////
     61////////////////////////////////////////////
    5962error_t vfs_ctx_inum_alloc( vfs_ctx_t * ctx,
    6063                            uint32_t  * inum )
     
    6467
    6568    // get lid from local inum allocator
    66     uint32_t lid = bitmap_ffc( ctx->inum , CONFIG_VFS_MAX_INODES );
     69    uint32_t lid = bitmap_ffc( ctx->bitmap , CONFIG_VFS_MAX_INODES );
    6770
    6871    if( lid == -1 )   // no more free slot => error
     
    7780    {
    7881        // set slot allocated
    79         bitmap_set( ctx->inum , lid );
     82        bitmap_set( ctx->bitmap , lid );
    8083
    8184        // release lock
     
    9295                           uint32_t    inum )
    9396{
    94     bitmap_clear( ctx->inum , inum & 0xFFFF );
     97    bitmap_clear( ctx->bitmap , inum & 0xFFFF );
    9598}
    9699
     
    99102//////////////////////////////////////////////////////////////////////////////////////////
    100103
    101 ////////////////////////////////////////////////
    102 error_t vfs_inode_create( xptr_t      dentry_xp,
    103                           uint32_t    type,
    104                           uint32_t    attr,
    105                           uint32_t    mode,
    106                           uid_t       uid,
    107                           gid_t       gid,
    108                           xptr_t    * inode_xp )
     104//////////////////////////////////////////////////////
     105
     106error_t vfs_inode_create( xptr_t            dentry_xp,
     107                          vfs_fs_type_t     fs_type,
     108                          vfs_inode_type_t  inode_type,
     109                          uint32_t          attr,
     110                          uint32_t          rights,
     111                          uid_t             uid,
     112                          gid_t             gid,
     113                          xptr_t          * inode_xp )
    109114{
    110115    mapper_t         * mapper;     // associated mapper( to be allocated)
     
    115120    error_t            error;
    116121
    117     // check type and get pointer on context
    118     if     ( type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
    119     else if( type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
     122    // check fs type and get pointer on context
     123    if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
     124    else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
     125    else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS];
    120126    else
    121127    {
     
    144150    }
    145151
    146     // allocate memory for inode descriptor
     152    // allocate memory for VFS inode descriptor
    147153        req.type  = KMEM_VFS_INODE;
    148154        req.size  = sizeof(vfs_inode_t);
     
    160166    // initialize inode descriptor
    161167    inode->gc         = 0;
     168    inode->type       = inode_type;
    162169    inode->inum       = inum;
    163170    inode->attr       = attr;
    164     inode->mode       = mode;
     171    inode->rights     = rights;
    165172    inode->uid        = uid;
    166173    inode->gid        = gid;
     
    174181
    175182    // initialize dentries hash table, if new inode is a directory
    176     if( attr & INODE_ATTR_DIR ) xhtab_init( &inode->children , XHTAB_DENTRY_TYPE );
     183    if( inode_type == INODE_TYPE_DIR ) xhtab_init( &inode->children , XHTAB_DENTRY_TYPE );
    177184
    178185    // initialize inode locks
    179186    remote_rwlock_init( XPTR( local_cxy , &inode->data_lock ) );
    180187    remote_spinlock_init( XPTR( local_cxy , &inode->main_lock ) );
    181 
    182     // create FS specific inode
    183     if     ( ctx->type == FS_TYPE_FATFS )  fatfs_inode_create( inode );
    184     else if( ctx->type == FS_TYPE_RAMFS )  ramfs_inode_create( inode );
    185188
    186189    // return extended pointer on inode
     
    284287//////////////////////////////////////////////////////////////////////////////////////////
    285288
    286 //////////////////////////////////////////////
    287 error_t vfs_dentry_create( uint32_t      type,
    288                            char        * name,
    289                            vfs_inode_t * parent,
    290                            xptr_t      * dentry_xp )
     289///////////////////////////////////////////////////
     290error_t vfs_dentry_create( vfs_fs_type_t   fs_type,
     291                           char          * name,
     292                           vfs_inode_t   * parent,
     293                           xptr_t        * dentry_xp )
    291294{
    292295    vfs_ctx_t      * ctx;        // context descriptor
    293296    vfs_dentry_t   * dentry;     // dentry descriptor (to be allocated)
    294297        kmem_req_t       req;        // request to kernel memory allocator
    295     xptr_t           xhtab_xp;   // extended pointer on xhtab_t embedded in inode
    296     xptr_t           xlist_xp;   // extended pointer on xlist_entry_t in dentry
     298
     299printk("\n            @@@ dentry_create : 0 / name = %s\n", name );
    297300
    298301    // check type and get pointer on context
    299     if     ( type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
    300     else if( type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
     302    if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
     303    else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
     304    else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS];
    301305    else
    302306    {
     
    309313    uint32_t length = strlen( name );
    310314
    311     if( length > (CONFIG_VFS_MAX_NAME_LENGTH - 1) )
     315    if( length >= CONFIG_VFS_MAX_NAME_LENGTH )
    312316    {
    313317        printk("\n[ERROR] in %s : name too long\n", __FUNCTION__ );
    314318        return EINVAL;
    315319    }
     320
     321printk("\n            @@@ dentry_create : 1 / name = %s\n", name );
    316322
    317323    // allocate memory for dentry descriptor
     
    328334
    329335    // initialize dentry descriptor
     336
    330337    dentry->ctx     = ctx;
    331338    dentry->length  = length;
     
    333340    strcpy( dentry->name , name );
    334341
    335     // return extended pointer on dentry to caller
     342printk("\n            @@@ dentry_create : 2 / name = %s\n", name );
     343
     344    // register dentry in hash table rooted in parent inode
     345    xhtab_insert( XPTR( local_cxy , &parent->children ),
     346                  name,
     347                  XPTR( local_cxy , &dentry->xlist ) );
     348
     349printk("\n            @@@ dentry_create : 3 / name = %s\n", name );
     350
     351    // return extended pointer on dentry
    336352    *dentry_xp = XPTR( local_cxy , dentry );
    337353
    338     // register dentry in hash table rooted in parent inode
    339     xhtab_xp    = XPTR( local_cxy , &parent->children );
    340     xlist_xp    = XPTR( local_cxy , &dentry->xlist );
    341     xhtab_register( xhtab_xp  , name , xlist_xp );
    342    
    343354    return 0;
    344355
     
    365376//////////////////////////////////////////////////////////////////////////////////////////
    366377
    367 ////////////////////////////////////////
    368 void vfs_file_count_up( xptr_t file_xp )
    369 {
    370     // get file cluster and local pointer
    371     cxy_t        file_cxy = GET_CXY( file_xp );
    372     vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
    373 
    374     // atomically increment count
    375     hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , 1 );
    376 }
    377 
    378 //////////////////////////////////////////
    379 void vfs_file_count_down( xptr_t file_xp )
    380 {
    381     // get file cluster and local pointer
    382     cxy_t        file_cxy = GET_CXY( file_xp );
    383     vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
    384 
    385     // atomically decrement count
    386     hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , -1 );
    387 }
    388 
    389 ////////////////////////////////////////////////
    390 error_t vfs_file_create( xptr_t        inode_xp,
    391                          uint32_t      type,
     378/////////////////////////////////////////////
     379error_t vfs_file_create( vfs_inode_t * inode,
    392380                         uint32_t      attr,
    393                          xptr_t      * file_xp ) 
    394 {
    395     vfs_file_t  * file_ptr;
     381                         xptr_t      * file_xp )
     382{
     383    vfs_file_t  * file;
    396384        kmem_req_t    req;
    397 
    398     // get inode cluster and local pointer
    399     cxy_t         inode_cxy = GET_CXY( inode_xp );
    400     vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
    401 
    402     // check cluster identifier
    403     if( inode_cxy != local_cxy )
    404     {
    405         printk("\n[PANIC] in %s : local cluster is not the inode owner\n", __FUNCTION__ );
    406         hal_core_sleep();
    407     }
    408385
    409386    // allocate memory for new file descriptor
     
    411388        req.size  = sizeof(vfs_file_t);
    412389    req.flags = AF_KERNEL | AF_ZERO;
    413         file_ptr  = (vfs_file_t *)kmem_alloc( &req );
    414 
    415     if( file_ptr == NULL ) return ENOMEM;
    416 
    417     // get inode local pointer
     390        file      = (vfs_file_t *)kmem_alloc( &req );
     391
     392    if( file == NULL ) return ENOMEM;
     393
    418394    // initializes new file descriptor
    419     file_ptr->gc       = 0;
    420     file_ptr->type     = type;
    421     file_ptr->attr     = attr;
    422     file_ptr->offset   = 0;
    423     file_ptr->refcount = 0;
    424     file_ptr->inode    = inode_ptr;
    425     file_ptr->ctx      = inode_ptr->ctx;
    426     file_ptr->mapper   = inode_ptr->mapper;
    427 
    428     remote_rwlock_init( XPTR( local_cxy , &file_ptr->lock ) );
    429 
    430     *file_xp = XPTR( local_cxy , file_ptr );
    431     return 0;
    432 }
    433 
    434 ////////////////////////////////////////
    435 void vfs_file_destroy( xptr_t  file_xp )
    436 {
    437     // get file cluster and local pointer
    438     cxy_t        file_cxy = GET_CXY( file_xp );
    439     vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
    440 
    441     if( file_cxy != local_cxy )
    442     {
    443         printk("\n[PANIC] in %s : file descriptor not in local cluster\n", __FUNCTION__ );
    444         hal_core_sleep();
    445     }
    446 
    447     if( file_ptr->refcount )
     395    file->gc       = 0;
     396    file->type     = inode->type;
     397    file->attr     = attr;
     398    file->offset   = 0;
     399    file->refcount = 0;
     400    file->inode    = inode;
     401    file->ctx      = inode->ctx;
     402    file->mapper   = inode->mapper;
     403
     404    remote_rwlock_init( XPTR( local_cxy , &file->lock ) );
     405
     406    *file_xp = XPTR( local_cxy , file );
     407    return 0;
     408
     409}  // end vfs_file_create()
     410
     411///////////////////////////////////////////
     412void vfs_file_destroy( vfs_file_t *  file )
     413{
     414    if( file->refcount )
    448415    {
    449416        printk("\n[PANIC] in %s : file refcount non zero\n", __FUNCTION__ );
     
    452419
    453420        kmem_req_t req;
    454         req.ptr   = file_ptr;
     421        req.ptr   = file;
    455422        req.type  = KMEM_VFS_FILE;
    456423        kmem_free( &req );
    457 }
    458 
    459 //////////////////////////////////////////////////////////////////////////////////////////
    460 //           File related functions
     424
     425}  // end vfs_file_destroy()
     426
     427
     428////////////////////////////////////////
     429void vfs_file_count_up( xptr_t file_xp )
     430{
     431    // get file cluster and local pointer
     432    cxy_t        file_cxy = GET_CXY( file_xp );
     433    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
     434
     435    // atomically increment count
     436    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , 1 );
     437}
     438
     439//////////////////////////////////////////
     440void vfs_file_count_down( xptr_t file_xp )
     441{
     442    // get file cluster and local pointer
     443    cxy_t        file_cxy = GET_CXY( file_xp );
     444    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
     445
     446    // atomically decrement count
     447    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , -1 );
     448}
     449
     450//////////////////////////////////////////////////////////////////////////////////////////
     451//           File access related functions
    461452//////////////////////////////////////////////////////////////////////////////////////////
    462453
     
    465456                          char     * path,
    466457                          uint32_t   flags,
    467                           xptr_t   * file_xp )
    468 {
    469     return 0;
    470 }
    471 
    472 ///////////////////////////////////
    473 uint32_t vfs_read( xptr_t   file_xp,
    474                    void   * buffer,
    475                    uint32_t size )
    476 {
    477     return 0;
    478 }
    479 
    480 ////////////////////////////////////
    481 uint32_t vfs_write( xptr_t   file_xp,
    482                     void   * buffer,
    483                     uint32_t size )
    484 {
    485     return 0;
    486 }
     458                  uint32_t   mode,
     459                          xptr_t   * new_file_xp,
     460                  uint32_t * new_file_id )
     461{
     462    error_t       error;
     463    xptr_t        inode_xp;     // extended pointer on target inode
     464    cxy_t         inode_cxy;    // inode cluster identifier       
     465    vfs_inode_t * inode_ptr;    // inode local pointer
     466    uint32_t      file_attr;    // file descriptor attributes
     467    uint32_t      lookup_mode;  // lookup working mode       
     468    xptr_t        file_xp;      // extended pointer on created file descriptor
     469    uint32_t      file_id;      // created file descriptor index in reference fd_array
     470
     471    // compute lookup working mode
     472    lookup_mode = VFS_LOOKUP_OPEN;
     473    if( (flags & O_DIR    )      )  lookup_mode |= VFS_LOOKUP_DIR;
     474    if( (flags & O_CREAT  )      )  lookup_mode |= VFS_LOOKUP_CREATE;
     475    if( (flags & O_EXCL   )      )  lookup_mode |= VFS_LOOKUP_EXCL;
     476 
     477    // compute attributes for the created file
     478    file_attr = 0;
     479    if( (flags & O_RDONLY ) == 0 )  file_attr |= FD_ATTR_READ_ENABLE;
     480    if( (flags & O_WRONLY ) == 0 )  file_attr |= FD_ATTR_WRITE_ENABLE;
     481    if( (flags & O_SYNC   )      )  file_attr |= FD_ATTR_SYNC;
     482    if( (flags & O_APPEND )      )  file_attr |= FD_ATTR_APPEND;
     483    if( (flags & O_CLOEXEC)      )  file_attr |= FD_ATTR_CLOSE_EXEC;
     484
     485    // get extended pointer on target inode
     486    error = vfs_lookup( cwd_xp , path , lookup_mode , &inode_xp );
     487
     488    if( error ) return error;
     489
     490    // get target inode cluster and local pointer
     491    inode_cxy = GET_CXY( inode_xp );
     492    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
     493   
     494    // create a new file descriptor in cluster containing inode
     495    if( inode_cxy == local_cxy )      // target cluster is local
     496    {
     497        error = vfs_file_create( inode_ptr , file_attr , &file_xp );
     498    }
     499    else                              // target cluster is remote
     500    {
     501        rpc_vfs_file_create_client( inode_cxy , inode_ptr , file_attr , &file_xp , &error );
     502    }
     503
     504    if( error )  return error;
     505
     506    // allocate and register a new file descriptor index in reference cluster fd_array
     507    error = process_fd_register( file_xp , &file_id );
     508
     509    if( error ) return error;
     510
     511    // success
     512    *new_file_xp = file_xp;
     513    *new_file_id = file_id;
     514    return 0;
     515
     516}  // end vfs_open()
     517
     518/////////////////////////////////////
     519error_t vfs_move( bool_t   to_buffer,
     520                  xptr_t   file_xp,
     521                  void   * buffer,
     522                  uint32_t size )
     523{
     524    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
     525
     526    cxy_t              file_cxy;     // remote file descriptor cluster
     527    vfs_file_t       * file_ptr;     // remote file descriptor local pointer
     528    vfs_inode_type_t   inode_type;
     529    uint32_t           file_offset;  // current offset in file
     530    mapper_t         * mapper;
     531    error_t            error;
     532
     533    // get cluster and local pointer on remote file descriptor
     534    file_cxy  = GET_CXY( file_xp );
     535    file_ptr  = (vfs_file_t *)GET_PTR( file_xp );
     536
     537    // get inode type from remote file descriptor
     538    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type   ) );
     539   
     540    // action depends on inode type
     541    if( inode_type == INODE_TYPE_FILE )
     542    {
     543        // get mapper pointer and file offset from file descriptor
     544        file_offset = hal_remote_lw( XPTR( file_cxy , &file_ptr->offset ) );
     545        mapper = (mapper_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) );
     546
     547        // move data between mapper and buffer
     548        if( file_cxy == local_cxy )
     549        {
     550            error = mapper_move( mapper,
     551                                 to_buffer,
     552                                 file_offset,
     553                                 buffer,
     554                                 size );
     555        }
     556        else
     557        {
     558            rpc_mapper_move_client( file_cxy,
     559                                    mapper,
     560                                    to_buffer,
     561                                    file_offset,
     562                                    buffer,
     563                                    size,
     564                                    &error );
     565        }
     566
     567        return error;
     568    }
     569    else if (inode_type == INODE_TYPE_DIR )
     570    {
     571        printk("\n[ERROR] in %s : inode is a directory", __FUNCTION__ );
     572        return EINVAL;
     573    }
     574    else if (inode_type == INODE_TYPE_DEV )
     575    {
     576        // TODO
     577        return 0;
     578    }
     579    else
     580    {
     581        printk("\n[PANIC] in %s : illegal inode type\n", __FUNCTION__ );
     582        hal_core_sleep();
     583        return -1;
     584    }
     585}  // end vfs_access()
    487586
    488587//////////////////////////////////////
     
    492591                   uint32_t * new_offset )
    493592{
    494     return 0;
    495 }
    496 
    497 //////////////////////////////////////
    498 error_t vfs_close( xptr_t     file_xp,
    499                    uint32_t * refcount )
    500 {
    501     return 0;
    502 }
     593    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
     594    hal_core_sleep();
     595    return 0;
     596
     597    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
     598
     599}  // vfs_lseek()
     600
     601///////////////////////////////////
     602error_t vfs_close( xptr_t   file_xp,
     603                   uint32_t file_id )
     604{
     605    assert( (file_xp != XPTR_NULL) , __FUNCTION__ , "file_xp == XPTR_NULL" );
     606
     607    assert( (file_id < CONFIG_PROCESS_FILE_MAX_NR) , __FUNCTION__ , "illegal file_id" );
     608
     609    thread_t  * this    = CURRENT_THREAD;
     610    process_t * process = this->process;
     611
     612    // get cluster and local pointer on remote file descriptor
     613    cxy_t        file_cxy = GET_CXY( file_xp );
     614    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
     615
     616    // get local pointer on local cluster manager
     617    cluster_t * cluster = LOCAL_CLUSTER;
     618
     619    // get owner process cluster and lpid
     620    cxy_t   owner_cxy  = CXY_FROM_PID( process->pid );
     621    lpid_t  lpid       = LPID_FROM_PID( process->pid );
     622
     623    // get extended pointers on copies root and lock
     624    xptr_t root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] );
     625    xptr_t lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] );
     626
     627    // take the lock protecting the copies
     628    remote_spinlock_lock( lock_xp );
     629
     630    // 1) loop on the process descriptor copies to cancel all fd_array[file_id] entries
     631    xptr_t  iter_xp;
     632    XLIST_FOREACH( root_xp , iter_xp )
     633    {
     634        xptr_t      process_xp  = XLIST_ELEMENT( iter_xp , process_t , copies_list );
     635        cxy_t       process_cxy = GET_CXY( process_xp );
     636        process_t * process_ptr = (process_t *)GET_PTR( process_xp );
     637
     638        xptr_t lock_xp  = XPTR( process_cxy , &process_ptr->fd_array.lock );
     639        xptr_t entry_xp = XPTR( process_cxy , &process_ptr->fd_array.array[file_id] );
     640
     641        // lock is required for atomic write of a 64 bits word
     642        remote_rwlock_wr_lock( lock_xp );
     643        hal_remote_swd( entry_xp , XPTR_NULL );
     644        remote_rwlock_wr_unlock( lock_xp );
     645
     646        hal_wbflush();
     647    }   
     648
     649    // 2) release memory allocated to file descriptor in remote cluster
     650    if( file_cxy == local_cxy )             // file cluster is local
     651    {
     652        vfs_file_destroy( file_ptr );
     653    }
     654    else                                    // file cluster is local
     655    {
     656        rpc_vfs_file_destroy_client( file_cxy , file_ptr );
     657    }
     658
     659    return 0;
     660
     661}  // end vfs_close()
    503662
    504663////////////////////////////////////
     
    506665                    char   * path )
    507666{
     667    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
     668    hal_core_sleep();
     669    return 0;
     670}  // vfs_unlink()
     671
     672///////////////////////////////////////
     673error_t vfs_stat( xptr_t       file_xp,
     674                  vfs_stat_t * k_stat )
     675{
     676    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
     677    hal_core_sleep();
     678    return 0;
     679}
     680
     681////////////////////////////////////////////
     682error_t vfs_readdir( xptr_t         file_xp,
     683                     vfs_dirent_t * k_dirent )
     684{
     685    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
     686    hal_core_sleep();
    508687    return 0;
    509688}
    510689
    511690//////////////////////////////////////
    512 error_t vfs_stat( xptr_t       file_xp,
    513                   vfs_stat_t * stat )
    514 {
    515     return 0;
    516 }
    517 
    518 //////////////////////////////////////////////////////////////////////////////////////////
    519 //           Directory related functions
    520 //////////////////////////////////////////////////////////////////////////////////////////
    521 
    522 
    523 
    524 
    525 
    526 //////////////////////////////////////////////////////////////////////////////////////////
     691error_t vfs_mkdir( xptr_t     file_xp,
     692                   char     * path,
     693                   uint32_t   mode )
     694{
     695    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
     696    hal_core_sleep();
     697    return 0;
     698}
     699
     700////////////////////////////////////
     701error_t vfs_rmdir( xptr_t   file_xp,
     702                   char   * path )
     703{
     704    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
     705    hal_core_sleep();
     706    return 0;
     707}
     708
     709///////////////////////////////////
     710error_t vfs_chdir( xptr_t   cwd_xp,
     711                   char   * path )
     712{
     713    error_t           error;
     714    xptr_t            inode_xp;     // extended pointer on target inode
     715    cxy_t             inode_cxy;    // target inode cluster identifier       
     716    vfs_inode_t     * inode_ptr;    // target inode local pointer
     717    uint32_t          mode;         // lookup working mode       
     718    vfs_inode_type_t  inode_type;   // target inode type
     719
     720    // set lookup working mode
     721    mode = 0;
     722
     723    // get extended pointer on target inode
     724    error = vfs_lookup( cwd_xp , path , mode , &inode_xp );
     725
     726    if( error ) return error;
     727
     728    // get inode cluster and local pointer
     729    inode_cxy = GET_CXY( inode_xp );
     730    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
     731
     732    // get inode type from remote file
     733    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
     734
     735    if( inode_type != INODE_TYPE_DIR )
     736    {
     737        CURRENT_THREAD->errno = ENOTDIR;
     738        return -1;
     739    }
     740
     741    printk("\n[PANIC] %s non fully implemented\n", __FUNCTION__ );
     742    hal_core_sleep();
     743    return 0;
     744}
     745
     746///////////////////////////////////
     747error_t vfs_chmod( xptr_t   cwd_xp,
     748                   char   * path,
     749                   uint32_t rights )
     750{
     751    error_t           error;
     752    xptr_t            inode_xp;     // extended pointer on target inode
     753    cxy_t             inode_cxy;    // inode cluster identifier       
     754    vfs_inode_t     * inode_ptr;    // inode local pointer
     755    uint32_t          mode;         // lookup working mode
     756    vfs_inode_type_t  inode_type;   // target inode type
     757
     758    // set lookup working mode
     759    mode = 0;
     760 
     761    // get extended pointer on target inode
     762    error = vfs_lookup( cwd_xp , path , mode , &inode_xp );
     763
     764    if( error ) return error;
     765
     766    // get inode cluster and local pointer
     767    inode_cxy = GET_CXY( inode_xp );
     768    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
     769   
     770    // get inode type from remote inode
     771    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
     772
     773   
     774    printk("\n[PANIC] %s non fully implemented\n", __FUNCTION__ );
     775    hal_core_sleep();
     776    return 0;
     777}
     778
     779///////////////////////////////////
     780error_t vfs_mkfifo( xptr_t   cwd_xp,
     781                    char   * path,
     782                    uint32_t rights )
     783{
     784    printk("\n[PANIC] in %s : not implemented yet\n", __FUNCTION__ );
     785    hal_core_sleep();
     786    return 0;
     787}
     788
     789
     790
     791/////////////////////////////////////////////////////////////////////////////////////////r
    527792//            Inode Tree functions
    528793//////////////////////////////////////////////////////////////////////////////////////////
    529794
    530795//////////////////////////////////////////////////////////////////////////////////////////
    531 // This static function is used by the vfs_lookup() function.
     796// This function is used by the vfs_lookup() function.
    532797// It takes an extended pointer on a remote inode (parent directory inode),
    533798// and check access_rights violation for the calling thread.
     
    641906}
    642907
    643 ////////////////////////////////////////
    644 error_t vfs_lookup( xptr_t       cwd_xp,
    645                     char       * pathname,
    646                     uint32_t     client_uid,
    647                     uint32_t     client_gid,
    648                                         xptr_t     * inode_xp,
    649                     xptr_t     * ctx_xp )
     908//////////////////////////////////////////////
     909error_t vfs_lookup( xptr_t             cwd_xp,
     910                    char             * pathname,
     911                    uint32_t           mode,
     912                                        xptr_t           * inode_xp )
    650913{
    651914    char          name[CONFIG_VFS_MAX_NAME_LENGTH];   // one name in path
    652915
    653     xptr_t        parent_xp;    // extended pointer on parent inode
    654     cxy_t         parent_cxy;   // cluster for parentc inode
    655     vfs_inode_t * parent_ptr;   // local pointer on parent inode 
    656     xptr_t        child_xp;     // extended pointer on child inode
    657     cxy_t         child_cxy;    // cluster for child inode
    658     vfs_inode_t * child_ptr;    // local pointer on child inode 
    659     char        * current;      // current pointer on path
    660     char        * next;         // next value for current pointer   
    661     bool_t        last;         // true when the name is the last in path
    662     bool_t        found;        // true when a child has been found
    663     thread_t    * this;         // pointer on calling thread descriptor
    664     process_t   * process;      // pointer on calling process descriptor
    665     vfs_ctx_t   * ctx;          // parent inode context
    666     uint32_t      type;         // file system type of parent inode
    667     error_t       error;
     916    xptr_t             parent_xp;    // extended pointer on parent inode
     917    cxy_t              parent_cxy;   // cluster for parent inode
     918    vfs_inode_t      * parent_ptr;   // local pointer on parent inode 
     919    xptr_t             child_xp;     // extended pointer on child inode
     920    cxy_t              child_cxy;    // cluster for child inode
     921    vfs_inode_t      * child_ptr;    // local pointer on child inode 
     922    vfs_inode_type_t   inode_type;   // child inode type
     923    vfs_fs_type_t      fs_type;      // File system type
     924    vfs_ctx_t        * ctx_ptr;      // local pointer on FS context
     925    char             * current;      // current pointer on path
     926    char             * next;         // next value for current pointer   
     927    bool_t             last;         // true when the name is the last in path
     928    bool_t             found;        // true when a child has been found
     929    thread_t         * this;         // pointer on calling thread descriptor
     930    process_t        * process;      // pointer on calling process descriptor
     931    error_t            error;
    668932
    669933    this    = CURRENT_THREAD;
     
    687951    do
    688952    {
    689         // get cluster and local pointer for parent inode
    690         parent_cxy = GET_CXY( parent_xp );
    691         parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
    692        
    693         // get parent inode FS type
    694         ctx = (vfs_ctx_t *)(intptr_t)hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->ctx ) );
    695         type = ctx->type;
    696 
    697         // get one name from path
     953        // get one name from path and the last flag
    698954        vfs_get_name_from_path( current , name , &next , &last );
    699955
     
    708964            vfs_inode_remote_unlock( parent_xp );
    709965
    710             // insert a new dentry/inode in parent inode
    711             error = vfs_add_child_in_parent( type , parent_xp , name , &child_xp );
     966            // get cluster and local pointer on parent inode
     967            parent_cxy = GET_CXY( parent_xp );
     968            parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
     969
     970            // get parent inode FS type
     971            ctx_ptr = (vfs_ctx_t *)hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->ctx ) );
     972            fs_type = ctx_ptr->type;
     973
     974            // get child inode type
     975            if( (last == false) || (mode & VFS_LOOKUP_DIR) ) inode_type = INODE_TYPE_DIR;
     976            else                                             inode_type = INODE_TYPE_FILE;
     977
     978            // insert a child dentry/inode in parent inode
     979            error = vfs_add_child_in_parent( inode_type,
     980                                             fs_type,
     981                                             parent_xp,
     982                                             name,
     983                                             &child_xp );
    712984
    713985            if( error )
     
    723995
    724996        // check access rights
    725         error = vfs_access_denied( child_xp,
    726                                    client_uid,
    727                                    client_gid );
    728         if( error )
    729         {
    730             printk("\n[ERROR] in %s : permission denied for %s\n", __FUNCTION__ , name );
    731             return EACCES;
    732         }
     997        // error = vfs_access_denied( child_xp,
     998        //                            client_uid,
     999        //                            client_gid );
     1000        // if( error )
     1001        // {
     1002        //     printk("\n[ERROR] in %s : permission denied for %s\n", __FUNCTION__ , name );
     1003        //     return EACCES;
     1004        // }
    7331005
    7341006        // take lock on child inode if not last
     
    7531025    // return searched pointers
    7541026    *inode_xp = child_xp;
    755     *ctx_xp   = (xptr_t)hal_remote_lwd( XPTR( child_cxy , &child_ptr->ctx ) );
    7561027
    7571028    return 0;
     
    7701041        uint32_t     count;       // number of characters written in buffer
    7711042        uint32_t     index;       // slot index in buffer
    772     xptr_t       inode_xp;  // extended pointer on   
     1043    xptr_t       inode_xp;    // extended pointer on   
    7731044
    7741045    // implementation note:
     
    8291100}  // end vfs_get_path()
    8301101
    831 ///////////////////////////////////////////////
    832 error_t vfs_add_child_in_parent( uint32_t type,
    833                                  xptr_t   parent_xp,
    834                                  char   * name,   
    835                                  xptr_t * child_xp )
    836 {
    837     xptr_t     dentry_xp;  // extended pointer on created dentry
    838     xptr_t     inode_xp;   // extended pointer on created inode
    839     error_t    error;
     1102///////////////////////////////////////////////////////////////
     1103error_t vfs_add_child_in_parent( vfs_inode_type_t   inode_type,
     1104                                 vfs_fs_type_t      fs_type,
     1105                                 xptr_t             parent_xp,
     1106                                 char             * name,
     1107                                 xptr_t           * child_xp )
     1108{
     1109    error_t         error;
     1110    xptr_t          dentry_xp;   // extended pointer on created dentry
     1111    xptr_t          inode_xp;    // extended pointer on created inode
     1112    cxy_t           parent_cxy;  // parent inode cluster identifier
     1113    vfs_inode_t   * parent_ptr;  // parent inode local pointer
     1114    vfs_ctx_t     * parent_ctx;  // parent inode context local pointer
    8401115
    8411116    // get parent inode cluster and local pointer
    842     cxy_t         parent_cxy = GET_CXY( parent_xp );
    843     vfs_inode_t * parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
     1117    parent_cxy = GET_CXY( parent_xp );
     1118    parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
     1119
     1120    // get parent inode context local pointer
     1121    parent_ctx = (vfs_ctx_t *)hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->ctx ) );
    8441122
    8451123    // create dentry
    8461124    if( parent_cxy == local_cxy )      // parent cluster is the local cluster
    8471125    {
    848         error = vfs_dentry_create( type,
     1126        error = vfs_dentry_create( fs_type,
    8491127                                   name,
    8501128                                   parent_ptr,
     
    8541132    {
    8551133        rpc_vfs_dentry_create_client( parent_cxy,
    856                                       type,
     1134                                      fs_type,
    8571135                                      name,
    8581136                                      parent_ptr,
     
    8871165    {
    8881166        error = vfs_inode_create( dentry_xp,
    889                                   type,
     1167                                  fs_type,
     1168                                  inode_type,
    8901169                                  attr,
    8911170                                  mode,
     
    8981177        rpc_vfs_inode_create_client( child_cxy,
    8991178                                     dentry_xp,
    900                                      type,
     1179                                     fs_type,
     1180                                     inode_type,
    9011181                                     attr,
    9021182                                     mode,
     
    9251205
    9261206
     1207
     1208
     1209//////////////////////////////////////////////////////////////////////////////////////////
     1210//            Mapper related functions
     1211//////////////////////////////////////////////////////////////////////////////////////////
     1212
     1213////////////////////////////////////////////////
     1214error_t vfs_move_page_to_mapper( page_t * page )
     1215{
     1216    error_t         error = 0;
     1217
     1218    assert( (page != NULL) , __FUNCTION__ , "page pointer is NULL\n" );
     1219
     1220    mapper_t * mapper = page->mapper;
     1221
     1222    assert( (mapper != NULL) , __FUNCTION__ , "no mapper for page\n" );
     1223
     1224    // get FS type
     1225    vfs_fs_type_t fs_type = mapper->inode->ctx->type;
     1226
     1227    // update mapper if permitted by file system type
     1228    if( fs_type == FS_TYPE_FATFS )
     1229    {
     1230        // get mapper lock in WRITE_MODE
     1231        rwlock_wr_lock( &mapper->lock );
     1232
     1233        error = fatfs_read_page( page );
     1234
     1235        // release mapper lock
     1236        rwlock_wr_unlock( &mapper->lock );
     1237    }
     1238    else if( fs_type == FS_TYPE_RAMFS )
     1239    {
     1240        assert( false , __FUNCTION__ , "should not be called for RAMFS\n" );
     1241    }
     1242    else if( fs_type == FS_TYPE_DEVFS )
     1243    {
     1244        assert( false , __FUNCTION__ , "should not be called for DEVFS\n" );
     1245    }
     1246    else
     1247    {
     1248        assert( false , __FUNCTION__ , "undefined file system type\n" );
     1249    }
     1250
     1251    return error;
     1252
     1253}  // end vfs_move_page_to_mapper()
     1254
     1255//////////////////////////////////////////////////
     1256error_t vfs_move_page_from_mapper( page_t * page )
     1257{
     1258    error_t         error = 0;
     1259
     1260    assert( (page != NULL) , __FUNCTION__ , "page pointer is NULL\n" );
     1261
     1262    mapper_t * mapper = page->mapper;
     1263
     1264    assert( (mapper != NULL) , __FUNCTION__ , "no mapper for page\n" );
     1265
     1266    // get FS type
     1267    vfs_fs_type_t  fs_type = mapper->inode->ctx->type;
     1268
     1269    // update file system if permitted by file system type
     1270    if( fs_type == FS_TYPE_FATFS )
     1271    {
     1272            if( page_is_flag( page , PG_DIRTY ) )
     1273            {
     1274            // get mapper lock in READ_MODE
     1275            rwlock_rd_lock( &mapper->lock );
     1276
     1277            error = fatfs_write_page( page );
     1278
     1279            // release mapper lock from READ_MODE
     1280            rwlock_rd_unlock( &mapper->lock );
     1281
     1282            // clear dirty bit if success
     1283                    if( error == 0 ) page_undo_dirty( page );
     1284         }
     1285    }
     1286    else if( fs_type == FS_TYPE_RAMFS )
     1287    {
     1288        assert( false , __FUNCTION__ , "should not be called for RAMFS\n" );
     1289    }
     1290    else if( fs_type == FS_TYPE_DEVFS )
     1291    {
     1292        assert( false , __FUNCTION__ , "should not be called for DEVFS\n" );
     1293    }
     1294    else
     1295    {
     1296        assert( false , __FUNCTION__ , "undefined file system type\n" );
     1297    }
     1298       
     1299    return error;
     1300
     1301}  // end vfs_move_page_from_mapper()
     1302
     1303
Note: See TracChangeset for help on using the changeset viewer.