source: trunk/kernel/fs/vfs.c @ 473

Last change on this file since 473 was 473, checked in by alain, 6 years ago

Fix several GCC warning related to the -Wextra compilation option.

File size: 69.1 KB
RevLine 
[1]1/*
2 * vfs.c - Virtual File System implementation.
3 *
4 * Author  Mohamed Lamine Karaoui (2015)
[437]5 *         Alain Greiner (2016,2017,2018)
[1]6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25
[14]26#include <kernel_config.h>
[457]27#include <hal_kernel_types.h>
[1]28#include <hal_atomic.h>
29#include <hal_special.h>
30#include <readlock.h>
31#include <spinlock.h>
32#include <printk.h>
33#include <list.h>
34#include <xlist.h>
35#include <slist.h>
36#include <xhtab.h>
[430]37#include <string.h>
[23]38#include <rpc.h>
[1]39#include <errno.h>
40#include <kmem.h>
41#include <mapper.h>
42#include <thread.h>
[337]43#include <chdev.h>
[1]44#include <process.h>
[23]45#include <vfs.h>
[1]46#include <fatfs.h>
47#include <ramfs.h>
[23]48#include <devfs.h>
49#include <syscalls.h>
[1]50
51
52//////////////////////////////////////////////////////////////////////////////////////////
[50]53//           Extern variables         
[1]54//////////////////////////////////////////////////////////////////////////////////////////
55
[337]56extern vfs_ctx_t          fs_context[FS_TYPES_NR];    // allocated in kernel_init.c
[188]57
[337]58extern chdev_directory_t  chdev_dir;                  // allocated in kernel_init.c 
[50]59 
[1]60//////////////////////////////////////////////////////////////////////////////////////////
61//           Context related functions
62//////////////////////////////////////////////////////////////////////////////////////////
63
[188]64////////////////////////////////////////
65void vfs_ctx_init( vfs_fs_type_t   type,
66                   uint32_t        attr,
67                       uint32_t        total_clusters,
68                       uint32_t        cluster_size,
69                       xptr_t          vfs_root_xp,
70                   void          * extend )
71{
72    vfs_ctx_t * vfs_ctx = &fs_context[type];
73
74    vfs_ctx->type           = type;
75    vfs_ctx->attr           = attr;
76    vfs_ctx->total_clusters = total_clusters;
77    vfs_ctx->cluster_size   = cluster_size;
78    vfs_ctx->vfs_root_xp    = vfs_root_xp;
79    vfs_ctx->extend         = extend;
80
81    spinlock_init( &vfs_ctx->lock );
82
83    bitmap_init( vfs_ctx->bitmap , BITMAP_SIZE(CONFIG_VFS_MAX_INODES) ); 
84}
85
[23]86////////////////////////////////////////////
[1]87error_t vfs_ctx_inum_alloc( vfs_ctx_t * ctx,
88                            uint32_t  * inum )
89{
90    // get lock on inum allocator
91    spinlock_lock( &ctx->lock );
92
93    // get lid from local inum allocator
[23]94    uint32_t lid = bitmap_ffc( ctx->bitmap , CONFIG_VFS_MAX_INODES );
[1]95
[473]96    if( lid == 0xFFFFFFFF )   // no more free slot => error
[1]97    {
98        // release lock
99        spinlock_unlock( &ctx->lock );
100
101        // return error
102        return 1;
103    }
104    else              // found => return inum
105    {
106        // set slot allocated
[23]107        bitmap_set( ctx->bitmap , lid );
[1]108
109        // release lock
110        spinlock_unlock( &ctx->lock );
111
112        // return inum
113        *inum = (((uint32_t)local_cxy) << 16) | (lid & 0xFFFF);
114        return 0;
115    }
116}
117
118////////////////////////////////////////////
119void vfs_ctx_inum_release( vfs_ctx_t * ctx,
120                           uint32_t    inum )
121{
[23]122    bitmap_clear( ctx->bitmap , inum & 0xFFFF ); 
[1]123}
124
125//////////////////////////////////////////////////////////////////////////////////////////
126//           Inode related functions
127//////////////////////////////////////////////////////////////////////////////////////////
128
[188]129char * vfs_inode_type_str( uint32_t type )
130{
131    if     ( type == INODE_TYPE_FILE ) return "FILE";
132    else if( type == INODE_TYPE_DIR  ) return "DIR ";
133    else if( type == INODE_TYPE_FIFO ) return "FIFO";
134    else if( type == INODE_TYPE_PIPE ) return "PIPE";
135    else if( type == INODE_TYPE_SOCK ) return "SOCK";
136    else if( type == INODE_TYPE_DEV  ) return "DEV ";
137    else if( type == INODE_TYPE_SYML ) return "SYML";
138    else                               return "undefined";
139}
140
[23]141//////////////////////////////////////////////////////
142error_t vfs_inode_create( xptr_t            dentry_xp,
143                          vfs_fs_type_t     fs_type,
144                          vfs_inode_type_t  inode_type,
[188]145                          void            * extend,
[23]146                          uint32_t          attr,
147                          uint32_t          rights,
148                          uid_t             uid,
149                          gid_t             gid,
150                          xptr_t          * inode_xp )
[1]151{
152    mapper_t         * mapper;     // associated mapper( to be allocated)
153    vfs_inode_t      * inode;      // inode descriptor (to be allocated)
154    uint32_t           inum;       // inode identifier (to be allocated)
155    vfs_ctx_t        * ctx;        // file system context
156        kmem_req_t         req;        // request to kernel memory allocator
157    error_t            error;
158
[438]159#if DEBUG_VFS_INODE_CREATE
[433]160uint32_t cycle = (uint32_t)hal_get_cycles();
[438]161if( DEBUG_VFS_INODE_CREATE < cycle )
[433]162printk("\n[DBG] %s : thread %x enter / dentry = %x in cluster %x / cycle %d\n",
163__FUNCTION__, CURRENT_THREAD, GET_PTR(dentry_xp), GET_CXY(dentry_xp), cycle );
164#endif
[279]165 
[23]166    // check fs type and get pointer on context
167    if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
168    else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
169    else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS];
[1]170    else
171    {
172        ctx = NULL;
[430]173                assert( false , __FUNCTION__ , "illegal file system type = %d\n" , fs_type );
[1]174    }
175
176    // allocate inum
177    error = vfs_ctx_inum_alloc( ctx , &inum );
178
179    if( error )
180    {
181        printk("\n[ERROR] in %s : cannot allocate inum\n", __FUNCTION__ );
182        return ENOMEM;
183    }
184
185    // allocate memory for mapper
[246]186    mapper = mapper_create( fs_type );
[1]187
188    if( mapper == NULL )
189    {
190        printk("\n[ERROR] in %s : cannot allocate mapper\n", __FUNCTION__ );
191        vfs_ctx_inum_release( ctx , inum );
192        return ENOMEM;
193    }
194
[23]195    // allocate memory for VFS inode descriptor
[1]196        req.type  = KMEM_VFS_INODE;
197        req.size  = sizeof(vfs_inode_t);
198    req.flags = AF_KERNEL | AF_ZERO;
199        inode     = (vfs_inode_t *)kmem_alloc( &req );
200
201    if( inode == NULL )
202    {
203        printk("\n[ERROR] in %s : cannot allocate inode descriptor\n", __FUNCTION__ );
204        vfs_ctx_inum_release( ctx , inum );
205        mapper_destroy( mapper );
206        return ENOMEM;
207    }
208
209    // initialize inode descriptor
210    inode->gc         = 0;
[23]211    inode->type       = inode_type;
[1]212    inode->inum       = inum;
213    inode->attr       = attr;
[23]214    inode->rights     = rights;
[1]215    inode->uid        = uid;
216    inode->gid        = gid;
217    inode->refcount   = 0;
218    inode->parent_xp  = dentry_xp;
219    inode->ctx        = ctx;
[246]220    inode->mapper     = mapper;
[188]221    inode->extend     = extend;
[1]222
[246]223    // initialise inode field in mapper
224    mapper->inode     = inode;
225 
[1]226    // initialise threads waiting queue
227    xlist_root_init( XPTR( local_cxy , &inode->wait_root ) );
228
[204]229    // initialize dentries hash table
230    xhtab_init( &inode->children , XHTAB_DENTRY_TYPE );
[1]231
232    // initialize inode locks
[10]233    remote_rwlock_init( XPTR( local_cxy , &inode->data_lock ) );
[1]234    remote_spinlock_init( XPTR( local_cxy , &inode->main_lock ) );
235
[438]236#if DEBUG_VFS_INODE_CREATE
[436]237cycle = (uint32_t)hal_get_cycles();
[438]238if( DEBUG_VFS_INODE_CREATE < cycle )
[433]239printk("\n[DBG] %s : thread %x exit / inode = %x in cluster %x / cycle %d\n",
240__FUNCTION__, CURRENT_THREAD, inode, local_cxy, cycle );
241#endif
[401]242 
[1]243    // return extended pointer on inode
244    *inode_xp = XPTR( local_cxy , inode );
245    return 0;
246
247}  // end vfs_inode_create() 
248
[459]249////////////////////////////////////////////////
250error_t vfs_inode_destroy( vfs_inode_t * inode )
[1]251{
[459]252    assert( (inode->refcount == 0), __FUNCTION__ , "inode refcount non zero\n" );
[1]253
254    // release memory allocated for mapper
255    mapper_destroy( inode->mapper );
256
257    // release memory allocate for inode descriptor
258        kmem_req_t req;
259        req.ptr   = inode;
260        req.type  = KMEM_VFS_INODE;
261        kmem_free( &req );
262
[459]263    return 0;
264
[1]265}  // end vfs_inode_destroy()
266
[238]267/////////////////////////////////////////////
268error_t vfs_inode_load( vfs_inode_t * parent,
269                        char        * name,
270                        xptr_t        child_xp )
271{
[246]272
[438]273#if DEBUG_VFS_INODE_LOAD
[433]274uint32_t cycle = (uint32_t)hal_get_cycles();
[438]275if( DEBUG_VFS_INODE_LOAD < cycle )
[433]276printk("\n[DBG] %s : thread %x enter for <%s> / cycle %d\n",
277__FUNCTION__, CURRENT_THREAD , name , cycle );
278#endif
279
[238]280    error_t error = 0;
281
282    assert( (parent != NULL) , __FUNCTION__ , "parent pointer is NULL\n");
283
284    assert( (child_xp != XPTR_NULL) , __FUNCTION__ , "child pointer is NULL\n");
285
286    // get parent inode FS type
287    vfs_fs_type_t fs_type = parent->ctx->type;
288
289    // call relevant FS function
290    if( fs_type == FS_TYPE_FATFS )
291    {
292        error = fatfs_inode_load( parent , name , child_xp );
293    }
294    else if( fs_type == FS_TYPE_RAMFS )
295    {
296        assert( false , __FUNCTION__ , "should not be called for RAMFS\n" );
297    }
298    else if( fs_type == FS_TYPE_DEVFS )
299    {
300        assert( false , __FUNCTION__ , "should not be called for DEVFS\n" );
301    }
302    else
303    {
304        assert( false , __FUNCTION__ , "undefined file system type\n" );
305    }
306
[438]307#if DEBUG_VFS_INODE_LOAD
[433]308cycle = (uint32_t)hal_get_cycles();
[438]309if( DEBUG_VFS_INODE_LOAD < cycle )
[433]310printk("\n[DBG] %s : thread %x exit for <%s> / cycle %d\n",
311__FUNCTION__, CURRENT_THREAD , name , cycle );
312#endif
[246]313
[238]314    return error;
315
[459]316} // end vfs_inode_load()
[238]317
[1]318////////////////////////////////////////////
319void vfs_inode_remote_up( xptr_t  inode_xp )
320{
321    // get inode cluster and local pointer
322    cxy_t         inode_cxy = GET_CXY( inode_xp );
[473]323    vfs_inode_t * inode_ptr = GET_PTR( inode_xp );
[1]324
325    hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->refcount ) , 1 );   
326}
327
328//////////////////////////////////////////////
329void vfs_inode_remote_down( xptr_t  inode_xp )
330{
331    // get inode cluster and local pointer
332    cxy_t         inode_cxy = GET_CXY( inode_xp );
[473]333    vfs_inode_t * inode_ptr = GET_PTR( inode_xp );
[1]334
335    hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->refcount ) , -1 );   
336}
337
338//////////////////////////////////////////////
339uint32_t vfs_inode_get_size( xptr_t inode_xp )
340{
341    // get inode cluster and local pointer
342    cxy_t         cxy = GET_CXY( inode_xp );
[473]343    vfs_inode_t * ptr = GET_PTR( inode_xp );
[1]344
345    // get size
[10]346    remote_rwlock_rd_lock( XPTR( cxy , &ptr->data_lock ) );
[1]347    uint32_t size = hal_remote_lw( XPTR( cxy , &ptr->size ) );
[10]348    remote_rwlock_rd_unlock( XPTR( cxy , &ptr->data_lock ) );
[1]349    return size;
350}
351
[101]352////////////////////////////////////////////
353void vfs_inode_set_size( xptr_t    inode_xp,
[409]354                         uint32_t  size )
[1]355{
356    // get inode cluster and local pointer
357    cxy_t         cxy = GET_CXY( inode_xp );
[473]358    vfs_inode_t * ptr = GET_PTR( inode_xp );
[1]359
360    // set size
[10]361    remote_rwlock_wr_unlock( XPTR( cxy , &ptr->data_lock ) );
[1]362    hal_remote_sw( XPTR( cxy , &ptr->size ) , size );
[10]363    remote_rwlock_wr_unlock( XPTR( cxy , &ptr->data_lock ) );
[1]364}
365
[101]366////////////////////////////////////////
367void vfs_inode_unlock( xptr_t inode_xp )
[1]368{
369    // get inode cluster and local pointer
370    cxy_t         cxy = GET_CXY( inode_xp );
[473]371    vfs_inode_t * ptr = GET_PTR( inode_xp );
[1]372
373    // release the main lock
374    remote_spinlock_unlock( XPTR( cxy , &ptr->main_lock ) );
375}
376
[101]377//////////////////////////////////////
378void vfs_inode_lock( xptr_t inode_xp )
[1]379{
380    // get inode cluster and local pointer
381    cxy_t         cxy = GET_CXY( inode_xp );
[473]382    vfs_inode_t * ptr = GET_PTR( inode_xp );
[1]383
384    // get the main lock
385    remote_spinlock_lock( XPTR( cxy , &ptr->main_lock ) );
386}
387
[101]388/////////////////////////////////////////
[409]389void vfs_inode_get_name( xptr_t inode_xp,
390                         char * name )
[101]391{
[204]392    cxy_t          inode_cxy;
393    vfs_inode_t  * inode_ptr;
394    xptr_t         dentry_xp;
395    cxy_t          dentry_cxy;
396    vfs_dentry_t * dentry_ptr;
397   
398    // get inode cluster and local pointer
399    inode_cxy = GET_CXY( inode_xp );
[473]400    inode_ptr = GET_PTR( inode_xp );
[204]401
402    // get parent dentry
403    dentry_xp  = hal_remote_lwd( XPTR( inode_cxy , &inode_ptr->parent_xp ) );
404
405    // get local copy of name
406    if( dentry_xp == XPTR_NULL )  // it is the VFS root
407    {
408        strcpy( name , "/" );
409    }
410    else                          // not the VFS root
411    {
412        dentry_cxy = GET_CXY( dentry_xp );
[473]413        dentry_ptr = GET_PTR( dentry_xp );
[204]414
415        hal_remote_strcpy( XPTR( local_cxy  , name ) , 
416                           XPTR( dentry_cxy , &dentry_ptr->name ) );
417    }
[409]418}  // end vfs_inode_get_name()
[204]419
420////////////////////////////////////////////////////////////////////////////////////////////
[1]421//           Dentry related functions
422//////////////////////////////////////////////////////////////////////////////////////////
423
[23]424///////////////////////////////////////////////////
425error_t vfs_dentry_create( vfs_fs_type_t   fs_type,
426                           char          * name,
427                           vfs_inode_t   * parent,
428                           xptr_t        * dentry_xp )
[1]429{
430    vfs_ctx_t      * ctx;        // context descriptor
431    vfs_dentry_t   * dentry;     // dentry descriptor (to be allocated)
432        kmem_req_t       req;        // request to kernel memory allocator
[459]433    error_t          error;
[1]434
[438]435#if DEBUG_VFS_DENTRY_CREATE
[433]436uint32_t cycle = (uint32_t)hal_get_cycles();
[438]437if( DEBUG_VFS_DENTRY_CREATE < cycle )
[459]438printk("\n[DBG] %s : thread %x in process %x enter for <%s> / parent_inode %x / cycle %d\n",
439__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, name, parent, cycle );
[433]440#endif
[296]441
[188]442    // get pointer on context
[23]443    if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
444    else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
445    else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS];
[459]446    else 
[1]447    {
448        ctx = NULL;
[459]449        return EINVAL;
[1]450    }
451
452    // get name length
453    uint32_t length = strlen( name );
454
[459]455    if( length >= CONFIG_VFS_MAX_NAME_LENGTH ) return EINVAL;
[437]456
[1]457    // allocate memory for dentry descriptor
458        req.type  = KMEM_VFS_DENTRY;
459        req.size  = sizeof(vfs_dentry_t);
460    req.flags = AF_KERNEL | AF_ZERO;
461        dentry     = (vfs_dentry_t *)kmem_alloc( &req );
462
[459]463    if( dentry == NULL ) return ENOMEM;
[437]464
[1]465    // initialize dentry descriptor
[23]466
[1]467    dentry->ctx     = ctx;
468    dentry->length  = length;
469    dentry->parent  = parent;
470    strcpy( dentry->name , name );
471
[438]472#if( DEBUG_VFS_DENTRY_CREATE & 1 )
[437]473cycle = (uint32_t)hal_get_cycles();
[438]474if( DEBUG_VFS_DENTRY_CREATE < cycle )
[437]475printk("\n[DBG] %s : dentry initialised\n", __FUNCTION__ );
476#endif
477
[23]478    // register dentry in hash table rooted in parent inode
[459]479    error = xhtab_insert( XPTR( local_cxy , &parent->children ),
480                          name, 
481                          XPTR( local_cxy , &dentry->list ) );
[23]482
[459]483    if( error ) return EINVAL;
484
[438]485#if( DEBUG_VFS_DENTRY_CREATE & 1 )
[437]486cycle = (uint32_t)hal_get_cycles();
[438]487if( DEBUG_VFS_DENTRY_CREATE < cycle )
[437]488printk("\n[DBG] %s : dentry registerd in htab\n", __FUNCTION__ );
489#endif
490
[23]491    // return extended pointer on dentry
[1]492    *dentry_xp = XPTR( local_cxy , dentry );
493
[438]494#if DEBUG_VFS_DENTRY_CREATE
[433]495cycle = (uint32_t)hal_get_cycles();
[438]496if( DEBUG_VFS_DENTRY_CREATE < cycle )
[459]497printk("\n[DBG] %s : thread %x in process %x exit for <%s> / dentry %x / cycle %d\n",
498__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, name, dentry, cycle );
[433]499#endif
[296]500
[1]501    return 0;
502
503}  // end vfs_dentry_create()
504
[459]505///////////////////////////////////////////////////
506error_t vfs_dentry_destroy( vfs_dentry_t * dentry )
[1]507{
[459]508    error_t error;
[1]509
[459]510    assert( (dentry->refcount == 0) , __FUNCTION__ , "dentry refcount non zero\n" );
511
512    // get pointer on parent inode
513    vfs_inode_t * parent = dentry->parent;
514
515    // remove this dentry from parent inode htab
516    error = xhtab_remove( XPTR( local_cxy , &parent->children ),
517                          dentry->name,
518                          XPTR( local_cxy , &dentry->list ) ); 
519
520    if( error ) return EINVAL;     
521
522    // release memory allocated to dentry
[1]523        kmem_req_t req;
524        req.ptr   = dentry;
525        req.type  = KMEM_VFS_DENTRY;
526        kmem_free( &req );
[459]527
528    return 0;
[1]529}
530
531
[188]532
[1]533//////////////////////////////////////////////////////////////////////////////////////////
534//           File descriptor related functions
535//////////////////////////////////////////////////////////////////////////////////////////
536
[23]537/////////////////////////////////////////////
538error_t vfs_file_create( vfs_inode_t * inode,
539                         uint32_t      attr,
540                         xptr_t      * file_xp )
541{
542    vfs_file_t  * file;
543        kmem_req_t    req;
544
545    // allocate memory for new file descriptor
546        req.type  = KMEM_VFS_FILE;
547        req.size  = sizeof(vfs_file_t);
548    req.flags = AF_KERNEL | AF_ZERO;
549        file      = (vfs_file_t *)kmem_alloc( &req );
550
551    if( file == NULL ) return ENOMEM;
552
553    // initializes new file descriptor
554    file->gc       = 0;
555    file->type     = inode->type;
556    file->attr     = attr;
557    file->offset   = 0;
[337]558    file->refcount = 1;
[23]559    file->inode    = inode;
560    file->ctx      = inode->ctx;
561    file->mapper   = inode->mapper;
562
563    remote_rwlock_init( XPTR( local_cxy , &file->lock ) );
564
565    *file_xp = XPTR( local_cxy , file );
[459]566
567#if DEBUG_VFS_OPEN
568uint32_t cycle = (uint32_t)hal_get_cycles();
569if( DEBUG_VFS_OPEN < cycle )
570printk("\n[DBG] %s : thread %x in process %x created file %x in cluster %x / cycle %d\n",
571__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, file, local_cxy, cycle );
572#endif
573
[23]574    return 0;
575
576}  // end vfs_file_create()
577
578///////////////////////////////////////////
579void vfs_file_destroy( vfs_file_t *  file )
580{
581    if( file->refcount )
582    {
[430]583        assert( false , __FUNCTION__ , "refcount non zero\n" );
[23]584    }       
585
586        kmem_req_t req;
587        req.ptr   = file;
588        req.type  = KMEM_VFS_FILE;
589        kmem_free( &req );
590
[459]591#if DEBUG_VFS_CLOSE
592uint32_t cycle = (uint32_t)hal_get_cycles();
593if( DEBUG_VFS_CLOSE < cycle )
594printk("\n[DBG] %s : thread %x in process %x deleted file %x in cluster %x / cycle %d\n",
595__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, file, local_cxy, cycle );
596#endif
597
[23]598}  // end vfs_file_destroy()
599
600
[1]601////////////////////////////////////////
602void vfs_file_count_up( xptr_t file_xp )
603{
604    // get file cluster and local pointer
605    cxy_t        file_cxy = GET_CXY( file_xp );
[459]606    vfs_file_t * file_ptr = GET_PTR( file_xp ); 
[1]607
608    // atomically increment count
609    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , 1 ); 
610}
611
612//////////////////////////////////////////
613void vfs_file_count_down( xptr_t file_xp )
614{
615    // get file cluster and local pointer
616    cxy_t        file_cxy = GET_CXY( file_xp );
[459]617    vfs_file_t * file_ptr = GET_PTR( file_xp ); 
[1]618
619    // atomically decrement count
620    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , -1 ); 
621}
622
[23]623//////////////////////////////////////////////////////////////////////////////////////////
624//           File access related functions
625//////////////////////////////////////////////////////////////////////////////////////////
626
[407]627//////////////////////////////////////
628error_t vfs_open( process_t * process,
629                          char      * path,
630                          uint32_t    flags,
631                  uint32_t    mode, 
632                          xptr_t    * new_file_xp,
633                  uint32_t  * new_file_id )
[1]634{
[23]635    error_t       error;
636    xptr_t        inode_xp;     // extended pointer on target inode
637    cxy_t         inode_cxy;    // inode cluster identifier       
638    vfs_inode_t * inode_ptr;    // inode local pointer
639    uint32_t      file_attr;    // file descriptor attributes
640    uint32_t      lookup_mode;  // lookup working mode       
641    xptr_t        file_xp;      // extended pointer on created file descriptor
642    uint32_t      file_id;      // created file descriptor index in reference fd_array
[1]643
[473]644    assert( (mode == 0), __FUNCTION__,
645    "the mode parameter is not supported yet\n" );
646
[438]647#if DEBUG_VFS_OPEN
[433]648uint32_t cycle = (uint32_t)hal_get_cycles();
[438]649if( DEBUG_VFS_OPEN < cycle )
[459]650printk("\n[DBG] %s :  thread %x in process %x enter for <%s> / cycle %d\n",
651__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, path, cycle );
[433]652#endif
[101]653
[23]654    // compute lookup working mode
655    lookup_mode = VFS_LOOKUP_OPEN;
656    if( (flags & O_DIR    )      )  lookup_mode |= VFS_LOOKUP_DIR;
657    if( (flags & O_CREAT  )      )  lookup_mode |= VFS_LOOKUP_CREATE;
658    if( (flags & O_EXCL   )      )  lookup_mode |= VFS_LOOKUP_EXCL;
659 
660    // compute attributes for the created file
661    file_attr = 0;
[407]662    if( (flags & O_RDONLY ) == 0 )  file_attr |= FD_ATTR_WRITE_ENABLE;
663    if( (flags & O_WRONLY ) == 0 )  file_attr |= FD_ATTR_READ_ENABLE;
[23]664    if( (flags & O_SYNC   )      )  file_attr |= FD_ATTR_SYNC;
665    if( (flags & O_APPEND )      )  file_attr |= FD_ATTR_APPEND;
666    if( (flags & O_CLOEXEC)      )  file_attr |= FD_ATTR_CLOSE_EXEC;
[1]667
[23]668    // get extended pointer on target inode
[407]669    error = vfs_lookup( process->vfs_cwd_xp , path , lookup_mode , &inode_xp );
[23]670
671    if( error ) return error;
672
673    // get target inode cluster and local pointer
674    inode_cxy = GET_CXY( inode_xp );
[473]675    inode_ptr = GET_PTR( inode_xp );
[23]676   
677    // create a new file descriptor in cluster containing inode
678    if( inode_cxy == local_cxy )      // target cluster is local
[1]679    {
[23]680        error = vfs_file_create( inode_ptr , file_attr , &file_xp );
[1]681    }
[23]682    else                              // target cluster is remote
683    {
684        rpc_vfs_file_create_client( inode_cxy , inode_ptr , file_attr , &file_xp , &error );
685    }
[1]686
[23]687    if( error )  return error;
[1]688
[407]689    // allocate and register a new file descriptor index in reference process
690    error = process_fd_register( process , file_xp , &file_id );
[1]691
[23]692    if( error ) return error;
[1]693
[438]694#if DEBUG_VFS_OPEN
[433]695cycle = (uint32_t)hal_get_cycles();
[438]696if( DEBUG_VFS_OPEN < cycle )
[459]697printk("\n[DBG] %s :  thread %x in process %x exit for <%s> / fdid %d / cluster %x / cycle %d\n",
698__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, path,
699file_id, GET_CXY( file_xp ), cycle );
[433]700#endif
[238]701
[23]702    // success
703    *new_file_xp = file_xp;
704    *new_file_id = file_id;
705    return 0;
[1]706
[23]707}  // end vfs_open()
708
[407]709//////////////////////////////////////
710int vfs_user_move( bool_t   to_buffer,
711                   xptr_t   file_xp,
712                   void   * buffer,
713                   uint32_t size )
[23]714{
[433]715    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
[23]716
717    cxy_t              file_cxy;     // remote file descriptor cluster
718    vfs_file_t       * file_ptr;     // remote file descriptor local pointer
719    vfs_inode_type_t   inode_type;
720    uint32_t           file_offset;  // current offset in file
721    mapper_t         * mapper;
722    error_t            error;
723
724    // get cluster and local pointer on remote file descriptor
725    file_cxy  = GET_CXY( file_xp );
[473]726    file_ptr  = GET_PTR( file_xp );
[23]727
728    // get inode type from remote file descriptor
729    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type   ) );
730   
[407]731    assert( (inode_type == INODE_TYPE_FILE) , __FUNCTION__ ,
732    "inode type is not INODE_TYPE_FILE" );
[23]733
[407]734    // get mapper pointer and file offset from file descriptor
735    file_offset = hal_remote_lw( XPTR( file_cxy , &file_ptr->offset ) );
736    mapper = (mapper_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) );
[23]737
[407]738    // move data between mapper and buffer
739    if( file_cxy == local_cxy )
740    {
741        error = mapper_move_user( mapper,
742                                  to_buffer,
743                                  file_offset,
744                                  buffer,
745                                  size );
[23]746    }
[407]747    else
[23]748    {
[407]749        rpc_mapper_move_buffer_client( file_cxy,
750                                       mapper,
751                                       to_buffer,
752                                       true,          // user buffer
753                                       file_offset,
754                                       (uint64_t)(intptr_t)buffer,
755                                       size,
756                                       &error );
757    } 
758
759    if( error ) return -1;
760    else        return size;
761
[313]762}  // end vfs_user_move()
[23]763
[317]764////////////////////////////////////////////
765error_t vfs_kernel_move( bool_t   to_buffer,
766                         xptr_t   file_xp,
767                         xptr_t   buffer_xp,
768                         uint32_t size )
769{
770    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
771
772    cxy_t              file_cxy;     // remote file descriptor cluster
773    vfs_file_t       * file_ptr;     // remote file descriptor local pointer
774    vfs_inode_type_t   inode_type;
775    uint32_t           file_offset;  // current offset in file
776    mapper_t         * mapper;
777    error_t            error;
778
779    // get cluster and local pointer on remote file descriptor
780    file_cxy  = GET_CXY( file_xp );
[473]781    file_ptr  = GET_PTR( file_xp );
[317]782
783    // get inode type from remote file descriptor
784    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type   ) );
785   
786    // action depends on inode type
787    if( inode_type == INODE_TYPE_FILE )
788    {
789        // get mapper pointer and file offset from file descriptor
790        file_offset = hal_remote_lw( XPTR( file_cxy , &file_ptr->offset ) );
791        mapper = (mapper_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) );
792
793        // move data between mapper and buffer
794        if( file_cxy == local_cxy )
795        {
796            error = mapper_move_kernel( mapper,
797                                        to_buffer,
798                                        file_offset,
799                                        buffer_xp,
800                                        size );
801        }
802        else
803        {
804            rpc_mapper_move_buffer_client( file_cxy,
805                                           mapper,
806                                           to_buffer,
807                                           false,          // kernel buffer
808                                           file_offset,
809                                           buffer_xp,
810                                           size,
811                                           &error );
812        } 
813
814        if( error ) return -1;
815        else        return 0;
816    }
817    else 
818    {
819        printk("\n[ERROR] in %s : inode is not a file", __FUNCTION__ );
820        return -1;
821    }
822}  // end vfs_kernel_move()
823
[23]824//////////////////////////////////////
825error_t vfs_lseek( xptr_t     file_xp,
826                   uint32_t   offset,
827                   uint32_t   whence, 
828                   uint32_t * new_offset )
829{
[266]830    xptr_t         offset_xp;
831    xptr_t         lock_xp;
832    cxy_t          file_cxy;
833    vfs_file_t  *  file_ptr;
834    vfs_inode_t *  inode_ptr;
835    uint32_t       new;
836
837    assert( (file_xp != XPTR_NULL) , __FUNCTION__ , "file_xp == XPTR_NULL" );
838
839    // get cluster and local pointer on remote file descriptor
840    file_cxy = GET_CXY( file_xp );
[473]841    file_ptr = GET_PTR( file_xp );
[266]842
843    // build extended pointers on lock and offset
844    offset_xp = XPTR( file_cxy , &file_ptr->offset );
845    lock_xp   = XPTR( file_cxy , &file_ptr->lock );
846
847    // take file descriptor lock
848    remote_rwlock_wr_lock( lock_xp );
849
850    if      ( whence == SEEK_CUR )   // new = current + offset
851    {
852        new = hal_remote_lw( offset_xp ) + offset;
853    }
854    else if ( whence == SEEK_SET )   // new = offset
855    {
856        new = offset;
857    }
858    else if ( whence == SEEK_END )   // new = size + offset
859    { 
860        // get local pointer on remote inode
861        inode_ptr = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
862
863        new = hal_remote_lw( XPTR( file_cxy , &inode_ptr->size ) ) + offset;
864    }
865    else
866    {
867        printk("\n[ERROR] in %s : illegal whence value\n", __FUNCTION__ );
868        remote_rwlock_wr_unlock( lock_xp );
869        return -1;
870    }
871
872    // set new offset
873    hal_remote_sw( offset_xp , new );
874
875    // release file descriptor lock
876    remote_rwlock_wr_unlock( lock_xp );
877
878    // success
[271]879    if ( new_offset != NULL )
880        *new_offset = new;
[1]881    return 0;
882
[23]883}  // vfs_lseek()
884
885///////////////////////////////////
886error_t vfs_close( xptr_t   file_xp,
887                   uint32_t file_id )
[1]888{
[459]889    cluster_t  * cluster;          // local pointer on local cluster
890    cxy_t        file_cxy;         // cluster containing the file descriptor.
891    vfs_file_t * file_ptr;         // local ponter on file descriptor
892    cxy_t        owner_cxy;        // process owner cluster
893    lpid_t       lpid;             // process local index
894    xptr_t       root_xp;          // root of list of process copies
895    xptr_t       lock_xp;          // lock protecting the list of copies
896    xptr_t       iter_xp;          // iterator on list of process copies
897    xptr_t       process_xp;       // extended pointer on one process copy
898    cxy_t        process_cxy;      // process copy cluster
899    process_t  * process_ptr;      // process copy local pointer
900
[23]901    assert( (file_xp != XPTR_NULL) , __FUNCTION__ , "file_xp == XPTR_NULL" );
902
903    assert( (file_id < CONFIG_PROCESS_FILE_MAX_NR) , __FUNCTION__ , "illegal file_id" );
904
905    thread_t  * this    = CURRENT_THREAD;
906    process_t * process = this->process;
907
[459]908#if DEBUG_VFS_CLOSE
909uint32_t cycle = (uint32_t)hal_get_cycles();
910if( DEBUG_VFS_CLOSE < cycle )
911printk("\n[DBG] %s : thread %x in process %x enter / fdid %d / cycle %d\n",
912__FUNCTION__, this->trdid, process->pid, file_id, cycle );
913#endif
[1]914
[23]915    // get local pointer on local cluster manager
[459]916    cluster = LOCAL_CLUSTER;
[23]917
918    // get owner process cluster and lpid
[459]919    owner_cxy  = CXY_FROM_PID( process->pid );
920    lpid       = LPID_FROM_PID( process->pid );
[23]921
922    // get extended pointers on copies root and lock
[459]923    root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] );
924    lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] );
[23]925
[459]926    // 1) loop on the process descriptor copies to reset all fd_array[file_id] entries
927
928    // take the lock protecting the list of copies
[23]929    remote_spinlock_lock( lock_xp );
930
931    XLIST_FOREACH( root_xp , iter_xp )
[1]932    {
[459]933        process_xp  = XLIST_ELEMENT( iter_xp , process_t , copies_list );
934        process_cxy = GET_CXY( process_xp );
935        process_ptr = GET_PTR( process_xp );
[1]936
[459]937#if (DEBUG_VFS_CLOSE & 1 )
938if( DEBUG_VFS_CLOSE < cycle )
939printk("\n[DBG] %s : reset fd_array[%d] for process %x in cluster %x\n",
940__FUNCTION__, file_id, process_ptr, process_cxy );
941#endif
[23]942
[459]943// fd_array lock is required for atomic write of a 64 bits word
944// xptr_t fd_array_lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock );
945
946        xptr_t entry_xp         = XPTR( process_cxy , &process_ptr->fd_array.array[file_id] );
947
948// remote_rwlock_wr_lock( fd_array_lock_xp );
949
[23]950        hal_remote_swd( entry_xp , XPTR_NULL );
[459]951       
952// remote_rwlock_wr_unlock( fd_array_lock_xp );
[23]953
[459]954        vfs_file_count_down( file_xp );
955
[124]956        hal_fence();
[23]957    }   
958
[459]959    // release the lock protecting the list of copies
960    remote_spinlock_unlock( lock_xp );
961
962#if (DEBUG_VFS_CLOSE & 1)
963if( DEBUG_VFS_CLOSE < cycle )
964printk("\n[DBG] %s : thread %x in process %x reset all fd-array copies\n",
965__FUNCTION__, this->trdid, process->pid );
966#endif
967
[23]968    // 2) release memory allocated to file descriptor in remote cluster
[459]969
970    // get cluster and local pointer on remote file descriptor
971    file_cxy = GET_CXY( file_xp );
972    file_ptr = GET_PTR( file_xp );
973
[23]974    if( file_cxy == local_cxy )             // file cluster is local
[1]975    {
[23]976        vfs_file_destroy( file_ptr );
977    }
978    else                                    // file cluster is local
979    {
980        rpc_vfs_file_destroy_client( file_cxy , file_ptr );
981    }
[1]982
[459]983#if DEBUG_VFS_CLOSE
984cycle = (uint32_t)hal_get_cycles();
985if( DEBUG_VFS_CLOSE < cycle )
986printk("\n[DBG] %s : thread %x in process %x exit / fdid %d closed / cycle %d\n",
987__FUNCTION__, this->trdid, process->pid, file_id, cycle );
988#endif
989
[23]990    return 0;
[1]991
[23]992}  // end vfs_close()
[1]993
994////////////////////////////////////
[23]995error_t vfs_unlink( xptr_t   cwd_xp,
996                    char   * path )
[1]997{
[473]998    assert( false , __FUNCTION__ , "not implemented\n", cwd_xp, path );
[1]999    return 0;
[407]1000} 
[1]1001
[407]1002////////////////////////////////////////
1003error_t vfs_stat( xptr_t        file_xp,
1004                  struct stat * k_stat )
[1]1005{
[473]1006    assert( false , __FUNCTION__ , "not implemented\n", file_xp, k_stat );
[1]1007    return 0;
1008}
1009
[407]1010/////////////////////////////////////////////
1011error_t vfs_readdir( xptr_t          file_xp,
1012                     struct dirent * k_dirent )
[1]1013{
[473]1014    assert( false , __FUNCTION__ , "not implemented\n", file_xp, k_dirent );
[1]1015    return 0;
1016}
1017
1018//////////////////////////////////////
[23]1019error_t vfs_mkdir( xptr_t     file_xp,
1020                   char     * path,
1021                   uint32_t   mode )
[1]1022{
[473]1023    assert( false , __FUNCTION__ , "not implemented\n", file_xp, path, mode );
[1]1024    return 0;
1025}
1026
[23]1027////////////////////////////////////
1028error_t vfs_rmdir( xptr_t   file_xp,
1029                   char   * path )
[1]1030{
[473]1031    assert( false , __FUNCTION__ , "not implemented\n", file_xp, path );
[1]1032    return 0;
1033}
1034
[23]1035///////////////////////////////////
1036error_t vfs_chdir( xptr_t   cwd_xp,
1037                   char   * path )
[1]1038{
[23]1039    error_t           error;
1040    xptr_t            inode_xp;     // extended pointer on target inode
1041    cxy_t             inode_cxy;    // target inode cluster identifier       
1042    vfs_inode_t     * inode_ptr;    // target inode local pointer
1043    uint32_t          mode;         // lookup working mode       
1044    vfs_inode_type_t  inode_type;   // target inode type
1045
1046    // set lookup working mode
1047    mode = 0;
1048
1049    // get extended pointer on target inode
1050    error = vfs_lookup( cwd_xp , path , mode , &inode_xp );
1051
1052    if( error ) return error;
1053
1054    // get inode cluster and local pointer
1055    inode_cxy = GET_CXY( inode_xp );
[473]1056    inode_ptr = GET_PTR( inode_xp );
[23]1057
1058    // get inode type from remote file
1059    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
1060
1061    if( inode_type != INODE_TYPE_DIR )
1062    {
1063        CURRENT_THREAD->errno = ENOTDIR;
1064        return -1;
1065    }
1066
[430]1067    assert( false , __FUNCTION__ , "not implemented\n" );
[1]1068    return 0;
1069}
1070
[23]1071///////////////////////////////////
1072error_t vfs_chmod( xptr_t   cwd_xp,
1073                   char   * path,
1074                   uint32_t rights )
[1]1075{
[23]1076    error_t           error;
1077    xptr_t            inode_xp;     // extended pointer on target inode
1078    cxy_t             inode_cxy;    // inode cluster identifier       
1079    vfs_inode_t     * inode_ptr;    // inode local pointer
1080    vfs_inode_type_t  inode_type;   // target inode type
1081
1082    // set lookup working mode
[473]1083    assert( (rights == 0), __FUNCTION__,
1084    "access rights non implemented yet\n" );
[23]1085 
1086    // get extended pointer on target inode
[473]1087    error = vfs_lookup( cwd_xp , path , 0 , &inode_xp );
[23]1088
1089    if( error ) return error;
1090
1091    // get inode cluster and local pointer
1092    inode_cxy = GET_CXY( inode_xp );
[473]1093    inode_ptr = GET_PTR( inode_xp );
[23]1094   
1095    // get inode type from remote inode
1096    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
1097
1098   
[430]1099    assert( false , __FUNCTION__ , "not implemented\n" );
[1]1100    return 0;
1101}
1102
[23]1103///////////////////////////////////
1104error_t vfs_mkfifo( xptr_t   cwd_xp,
1105                    char   * path,
1106                    uint32_t rights )
1107{
[473]1108    assert( false , __FUNCTION__ , "not implemented\n", cwd_xp, path, rights );
[23]1109    return 0;
1110}
[1]1111
1112
1113
[188]1114//////////////////////////////////////////////////////////////////////////////////////////
[1]1115//            Inode Tree functions
1116//////////////////////////////////////////////////////////////////////////////////////////
1117
[188]1118/////////////////////////////////
1119cxy_t vfs_cluster_random_select()
1120{
1121    uint32_t  x_size    = LOCAL_CLUSTER->x_size;
1122    uint32_t  y_size    = LOCAL_CLUSTER->y_size;
1123    uint32_t  y_width   = LOCAL_CLUSTER->y_width;
1124    uint32_t  index     = ( hal_get_cycles() + hal_get_gid() ) % (x_size * y_size);
1125    uint32_t  x         = index / y_size;   
1126    uint32_t  y         = index % y_size;
1127
1128    return (x<<y_width) + y;
1129}
1130
1131
1132//////////////////////////////////////////////////////////////////////////
1133// This static function is called by the vfs_display() function.
[337]1134// that is supposed to take the TXT0 lock.
[188]1135//////////////////////////////////////////////////////////////////////////
1136static void vfs_recursive_display( xptr_t   inode_xp,
1137                                   xptr_t   name_xp,
1138                                   uint32_t indent )
1139{
1140    cxy_t              inode_cxy;
1141    vfs_inode_t      * inode_ptr;
1142    vfs_inode_type_t   inode_type;
[204]1143    xptr_t             children_xp;    // extended pointer on children xhtab
[188]1144
[204]1145    xptr_t             child_dentry_xp;
1146    cxy_t              child_dentry_cxy;
1147    vfs_dentry_t     * child_dentry_ptr;
1148    xptr_t             child_inode_xp;
1149    xptr_t             child_dentry_name_xp;
[188]1150
1151    char               name[CONFIG_VFS_MAX_NAME_LENGTH];
1152
1153    char *             indent_str[] = { "",                                  // level 0
1154                                        "  ",                                // level 1
1155                                        "    ",                              // level 2
1156                                        "      ",                            // level 3
1157                                        "        ",                          // level 4
1158                                        "          ",                        // level 5
1159                                        "            ",                      // level 6
1160                                        "              ",                    // level 7
1161                                        "                ",                  // level 8
1162                                        "                  ",                // level 9
1163                                        "                    ",              // level 10
1164                                        "                      ",            // level 11
1165                                        "                        ",          // level 12
1166                                        "                          ",        // level 13
1167                                        "                            ",      // level 14
1168                                        "                              " };  // level 15
1169
1170    assert( (inode_xp != XPTR_NULL) , __FUNCTION__ , "inode_xp cannot be NULL\n" );
1171    assert( (name_xp  != XPTR_NULL) , __FUNCTION__ , "name_xp cannot be NULL\n" );
1172    assert( (indent < 16)           , __FUNCTION__ , "depth cannot be larger than 15\n" );
1173   
1174    // get inode cluster and local pointer
1175    inode_cxy = GET_CXY( inode_xp );
[473]1176    inode_ptr = GET_PTR( inode_xp );
[188]1177
1178    // get inode type
1179    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
1180
[367]1181    // get local pointer on associated mapper
1182    mapper_t * mapper_ptr = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->mapper ) );
1183
[188]1184    // make a local copy of node name
1185    hal_remote_strcpy( XPTR( local_cxy , name ) , name_xp );
1186
1187    // display inode
[407]1188    nolock_printk("%s%s <%s> : inode = %x / mapper = %x / cluster %x\n",
[367]1189                  indent_str[indent], vfs_inode_type_str( inode_type ), name,
[407]1190                  inode_ptr , mapper_ptr , inode_cxy );
[188]1191
1192    // scan directory entries 
1193    if( inode_type == INODE_TYPE_DIR )
1194    {
1195        // get extended pointer on directory entries xhtab
[204]1196        children_xp =  XPTR( inode_cxy , &inode_ptr->children );
[188]1197
1198        // get xhtab lock
[204]1199        xhtab_read_lock( children_xp );
[188]1200
1201        // get first dentry from xhtab
[204]1202        child_dentry_xp = xhtab_get_first( children_xp );
[188]1203
[204]1204        while( child_dentry_xp != XPTR_NULL )
[188]1205        {
1206            // get dentry cluster and local pointer
[204]1207            child_dentry_cxy = GET_CXY( child_dentry_xp );
[473]1208            child_dentry_ptr = GET_PTR( child_dentry_xp );
[188]1209
1210            // get extended pointer on child inode
[204]1211            child_inode_xp = hal_remote_lwd( XPTR( child_dentry_cxy,
1212                                                   &child_dentry_ptr->child_xp ) );
[188]1213
1214            // get extended pointer on dentry name
[204]1215            child_dentry_name_xp = XPTR( child_dentry_cxy , &child_dentry_ptr->name );
[188]1216
1217            // recursive call on child inode
[204]1218            vfs_recursive_display( child_inode_xp,
1219                                   child_dentry_name_xp,
1220                                   indent+1 );
[188]1221
1222            // get next dentry
[204]1223            child_dentry_xp = xhtab_get_next( children_xp );
[188]1224        }
1225
1226        // release xhtab lock
[204]1227        xhtab_read_unlock( children_xp );
[188]1228    }
1229}  // end vfs_recursive_display()
1230
1231///////////////////////////////////
1232void vfs_display( xptr_t inode_xp )
1233{
[204]1234    xptr_t         name_xp;
[188]1235    xptr_t         dentry_xp; 
1236    cxy_t          dentry_cxy;
1237    vfs_dentry_t * dentry_ptr;
[337]1238    uint32_t       save_sr;
[188]1239
1240    // get target inode cluster and local pointer
1241    cxy_t         inode_cxy = GET_CXY( inode_xp );
[473]1242    vfs_inode_t * inode_ptr = GET_PTR( inode_xp );
[188]1243
1244    // get extended pointer on associated dentry
[367]1245    dentry_xp   = hal_remote_lwd( XPTR( inode_cxy , &inode_ptr->parent_xp ) );
[188]1246
1247    // check if target inode is the File System root
1248    if( dentry_xp == XPTR_NULL )
1249    {
1250        // build extended pointer on root name
1251        name_xp = XPTR( local_cxy , "/" );
1252    }
1253    else
1254    {
1255        // get dentry cluster and local pointer
1256        dentry_cxy = GET_CXY( dentry_xp );
[473]1257        dentry_ptr = GET_PTR( dentry_xp );
[188]1258
1259        // get extended pointer on dentry name
1260        name_xp = XPTR( dentry_cxy , &dentry_ptr->name );
1261    }
1262
[337]1263    // get pointers on TXT0 chdev
[407]1264    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
[337]1265    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
1266    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
1267
1268    // get extended pointer on remote TXT0 chdev lock
1269    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
1270
1271    // get TXT0 lock in busy waiting mode
1272    remote_spinlock_lock_busy( lock_xp , &save_sr );
1273
[188]1274    // print header
[401]1275    nolock_printk("\n***** file system state\n\n");
[188]1276
1277    // call recursive function
[473]1278    vfs_recursive_display( inode_xp , name_xp , 0 );
[188]1279
[337]1280    // release lock
1281    remote_spinlock_unlock_busy( lock_xp , save_sr );
1282
[204]1283}  // end vfs_display()
[188]1284
[1]1285//////////////////////////////////////////////////////////////////////////////////////////
[23]1286// This function is used by the vfs_lookup() function.
[1]1287// It takes an extended pointer on a remote inode (parent directory inode),
1288// and check access_rights violation for the calling thread.
1289// It can be used by any thread running in any cluster.
1290//////////////////////////////////////////////////////////////////////////////////////////
1291// @ inode_xp    : extended pointer on inode.
1292// @ client_uid  : client thread user ID
1293// @ client_gid  : client thread group ID
1294// @ return true if access rights are violated.
1295//////////////////////////////////////////////////////////////////////////////////////////
1296bool_t vfs_access_denied( xptr_t   inode_xp,
1297                          uint32_t client_uid,
1298                          uint32_t client_gid )
1299{
1300    // get found inode cluster and local pointer
1301    cxy_t         inode_cxy = GET_CXY( inode_xp );
[473]1302    vfs_inode_t * inode_ptr = GET_PTR( inode_xp );
[1]1303
1304    // get inode access mode, UID, and GID
1305    // TODO uint32_t  mode = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->mode ) );
1306    uid_t     uid  = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->uid  ) );
1307    gid_t     gid  = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->gid  ) );
1308
1309    // FIXME : me must use mode
1310    if( (uid == client_uid) || (gid == client_gid) ) return false;
1311    else                                             return true;
1312}
1313
1314//////////////////////////////////////////////////////////////////////////////////////////
1315// This static function is used by the vfs_lookup() function.
[204]1316// It takes an extended pointer on a remote parent directory inode, a directory
[1]1317// entry name, and returns an extended pointer on the child inode.
1318// It can be used by any thread running in any cluster.
1319//////////////////////////////////////////////////////////////////////////////////////////
1320// @ parent_xp   : extended pointer on parent inode in remote cluster.
1321// @ name        : dentry name
1322// @ child_xp    : [out] buffer for extended pointer on child inode.
1323// @ return true if success / return false if not found.
1324//////////////////////////////////////////////////////////////////////////////////////////
1325static bool_t vfs_get_child( xptr_t   parent_xp,
1326                             char   * name,
1327                             xptr_t * child_xp )
1328{
1329    xptr_t  xhtab_xp;    // extended pointer on hash table containing children dentries
1330    xptr_t  dentry_xp;   // extended pointer on children dentry
1331
1332    // get parent inode cluster and local pointer
1333    cxy_t         parent_cxy = GET_CXY( parent_xp );
[473]1334    vfs_inode_t * parent_ptr = GET_PTR( parent_xp );
[1]1335
1336    // get extended pointer on hash table of children directory entries
1337    xhtab_xp = XPTR( parent_cxy , &parent_ptr->children );
1338
1339    // search extended pointer on matching dentry
1340    dentry_xp = xhtab_lookup( xhtab_xp , name );
1341
1342    if( dentry_xp == XPTR_NULL ) return false;
1343
1344    // get dentry cluster and local pointer
1345    cxy_t          dentry_cxy = GET_CXY( dentry_xp );
[473]1346    vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp );
[1]1347
1348    // return child inode
[101]1349    *child_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , &dentry_ptr->child_xp ) );
[1]1350    return true;
1351
[204]1352}  // end vfs_get_child()
1353
[1]1354//////////////////////////////////////////////////////////////////////////////////////////
1355// This static function is used by the vfs_lookup() function.
1356// It takes the <current> pointer on a buffer containing a complete pathname, and return
1357// in the <name> buffer, allocated by the caller, a single name in the path.
1358// It return also in the <next> pointer the next character to analyse in the path.
1359// Finally it returns a <last> boolean, that is true when the returned <name> is the
1360// last name in the path. The names are supposed to be separated by one or several '/'
1361// characters, that are not written in  the <name> buffer.
1362//////////////////////////////////////////////////////////////////////////////////////////
1363// @ current   : pointer on first character to analyse in buffer containing the path.
1364// @ name      : [out] pointer on buffer allocated by the caller for the returned name.
1365// @ next      : [out] pointer on next character to analyse in buffer containing the path.
1366// @ last      : [out] true if the returned name is the last (NUL character found).
1367// @ return 0 if success / return EINVAL if string empty (first chracter is NUL).
1368//////////////////////////////////////////////////////////////////////////////////////////
1369static error_t vfs_get_name_from_path( char     * current,
1370                                       char     * name,
1371                                       char    ** next,
1372                                       bool_t   * last )
1373{
1374    char * ptr = current;
1375
1376    // skip leading '/' characters
1377    while( *ptr == '/' ) ptr++;
1378
1379    // return EINVAL if string empty
1380    if( *ptr == 0 ) return EINVAL;
1381
1382    // copy all characters in name until NUL or '/'
1383    while( (*ptr != 0) && (*ptr !='/') )  *(name++) = *(ptr++);
1384
[204]1385    // set NUL terminating character in name buffer
1386    *(name++) = 0;
1387
[1]1388    // return last an next
1389    if( *ptr == 0 )             // last found character is NUL => last name in path
1390    {
1391        *last = true;
1392    }
1393    else                        // last found character is '/' => skip it
1394    {
1395        *last = false;
1396        *next = ptr + 1;
1397    }
1398
1399    return 0;
[204]1400
1401}  // end vfs_get name_from_path()
[188]1402   
[23]1403//////////////////////////////////////////////
1404error_t vfs_lookup( xptr_t             cwd_xp,
1405                    char             * pathname,
1406                    uint32_t           mode,
1407                                        xptr_t           * inode_xp )
[1]1408{
[101]1409    char               name[CONFIG_VFS_MAX_NAME_LENGTH];   // one name in path
[1]1410
[23]1411    xptr_t             parent_xp;    // extended pointer on parent inode
1412    cxy_t              parent_cxy;   // cluster for parent inode
1413    vfs_inode_t      * parent_ptr;   // local pointer on parent inode 
1414    xptr_t             child_xp;     // extended pointer on child inode
1415    cxy_t              child_cxy;    // cluster for child inode
1416    vfs_inode_t      * child_ptr;    // local pointer on child inode 
1417    vfs_fs_type_t      fs_type;      // File system type
1418    vfs_ctx_t        * ctx_ptr;      // local pointer on FS context
1419    char             * current;      // current pointer on path
1420    char             * next;         // next value for current pointer   
1421    bool_t             last;         // true when the name is the last in path
1422    bool_t             found;        // true when a child has been found
[459]1423    bool_t             dir;          // searched inode is a directory
1424    bool_t             create;       // searched inode must be created if not found
1425    bool_t             excl;         // searched inode must not exist
[23]1426    thread_t         * this;         // pointer on calling thread descriptor
1427    process_t        * process;      // pointer on calling process descriptor
1428    error_t            error;
[1]1429
1430    this    = CURRENT_THREAD;
1431    process = this->process;
1432
[438]1433#if DEBUG_VFS_LOOKUP
[433]1434uint32_t cycle = (uint32_t)hal_get_cycles();
[438]1435if( DEBUG_VFS_LOOKUP < cycle )
[459]1436printk("\n[DBG] %s : thread %x in process %x enter for <%s> / cycle %d\n",
1437__FUNCTION__, this->trdid, process->pid, pathname, cycle );
[433]1438#endif
[380]1439
[459]1440    // compute lookup flags
1441    dir    = mode & VFS_LOOKUP_DIR;
1442    create = mode & VFS_LOOKUP_CREATE;
1443    excl   = mode & VFS_LOOKUP_EXCL;
1444   
[1]1445    // get extended pointer on first inode to search
1446    if( pathname[0] == '/' ) parent_xp = process->vfs_root_xp;
1447    else                     parent_xp = cwd_xp;
1448
[101]1449    // initialise other loop variables
[1]1450    current  = pathname;
1451    next     = NULL;
1452    last     = false;
1453    child_xp = XPTR_NULL;
1454
1455    // take lock on parent inode
[101]1456    vfs_inode_lock( parent_xp );
[1]1457
[401]1458    // sequencially loop on nodes in pathname
[459]1459    // load from device if one node in path not found in inode tree
[401]1460    // exit loop when last name found (i.e. last == true)
[1]1461    do
1462    {
[401]1463        // get one name from path, and "last" flag
[1]1464        vfs_get_name_from_path( current , name , &next , &last );
1465
[438]1466#if (DEBUG_VFS_LOOKUP & 1)
1467if( DEBUG_VFS_LOOKUP < cycle )
[459]1468printk("\n[DBG] %s : look for <%s> / last = %d\n",
1469__FUNCTION__ , name , last );
[433]1470#endif
[101]1471
[204]1472        // search a child dentry matching name in parent inode
[1]1473        found = vfs_get_child( parent_xp,
1474                               name,
1475                               &child_xp );
1476
[459]1477        if (found == false )  // child not found in inode tree
[1]1478        {
[101]1479
[438]1480#if (DEBUG_VFS_LOOKUP & 1)
1481if( DEBUG_VFS_LOOKUP < cycle )
[459]1482printk("\n[DBG] %s : miss <%s> node => try to create it\n",
1483__FUNCTION__ , name );
[433]1484#endif
[459]1485            // if a child node is not found in the inode tree,
1486            // we introduce a new (dentry/inode) in inode tree,
1487            // and try to find it by scanning the parent directory mapper.
1488            // . if it is found in parent mapper:
1489            //   - if the child is a directory, the child mapper is loaded from device
1490            //   - if the child is not a directory, the search is completed
1491            // . if it is not found in the parent mapper:
1492            //   - if ( not last or not create ) an error is reported
1493            //   - if (last and create and dir) a new directory is created
1494            //   - if (last and create and not dir) a new file is created
[407]1495
[1]1496            // release lock on parent inode
[101]1497            vfs_inode_unlock( parent_xp );
[459]1498 
[238]1499            // get parent inode FS type
[23]1500            parent_cxy = GET_CXY( parent_xp );
[459]1501            parent_ptr = GET_PTR( parent_xp );
1502            ctx_ptr    = (vfs_ctx_t *)hal_remote_lpt( XPTR( parent_cxy,&parent_ptr->ctx ) );
[238]1503            fs_type    = hal_remote_lw( XPTR( parent_cxy , &ctx_ptr->type ) );
[23]1504
[238]1505            // select a cluster for missing inode
1506            child_cxy = vfs_cluster_random_select();
[401]1507 
[459]1508            // insert a new child dentry/inode in inode tree
[188]1509            error = vfs_add_child_in_parent( child_cxy,
[459]1510                                             0,           // type will be updated later
[23]1511                                             fs_type, 
1512                                             parent_xp, 
[222]1513                                             name, 
[459]1514                                             NULL,        // fs_type_specific inode extend
[23]1515                                             &child_xp );
[1]1516            if( error )
1517            {
[459]1518                printk("\n[ERROR] in %s : cannot create node %s for path <%s>\n",
1519                __FUNCTION__ , name, pathname );
[238]1520                return ENOMEM;
1521            }
1522
[459]1523            // get child inode cluster and local pointer
1524            child_cxy = GET_CXY( child_xp );
1525            child_ptr = GET_PTR( child_xp );
1526
1527#if (DEBUG_VFS_LOOKUP & 1)
1528if( DEBUG_VFS_LOOKUP < cycle )
1529printk("\n[DBG] %s : missing <%s> inode speculatively created / cxy %x / ptr %x\n",
1530__FUNCTION__ , name , child_cxy, child_ptr );
1531#endif
1532             // scan parent mapper to complete the missing inode
[238]1533            if( parent_cxy == local_cxy )
1534            {
1535                error = vfs_inode_load( parent_ptr,
1536                                        name,
1537                                        child_xp );
1538            }
1539            else
1540            {
1541                rpc_vfs_inode_load_client( parent_cxy,
1542                                           parent_ptr,
1543                                           name,
1544                                           child_xp,
1545                                           &error );
1546            }
1547
[459]1548            if ( error )   // child not found in parent mapper
[238]1549            {
[459]1550                if( last && create && dir )  // new directory  => update inode type
1551                {
1552                     hal_remote_sw( XPTR( child_cxy, &child_ptr->type ), INODE_TYPE_DIR );
[1]1553
[459]1554#if (DEBUG_VFS_LOOKUP & 1)
1555if( DEBUG_VFS_LOOKUP < cycle )
1556printk("\n[DBG] %s : created node <%s> in path %s / type DIR\n",
1557__FUNCTION__ , name, pathname );
1558#endif
[238]1559                }
[459]1560                else if ( last && create )   // new file => update inode type
[238]1561                {
[459]1562                     hal_remote_sw( XPTR( child_cxy, &child_ptr->type ), INODE_TYPE_FILE );
[238]1563
[459]1564#if (DEBUG_VFS_LOOKUP & 1)
1565if( DEBUG_VFS_LOOKUP < cycle )
1566printk("\n[DBG] %s : created node <%s> in path %s / type FILE\n",
1567__FUNCTION__ , name, pathname );
1568#endif
[238]1569                }
[459]1570                else                         // not last or not create => remove created node
1571                {                       
1572                     printk("\n[ERROR] in %s : <%s> node not found in parent for <%s>\n",
1573                     __FUNCTION__ , name , pathname );
1574                     vfs_remove_child_from_parent( child_xp );
1575                     return ENOENT;
1576                }
[238]1577            }
[459]1578            else          // child found in parent
1579            {
1580                // load child mapper from device if child is a directory (prefetch)
1581                if( hal_remote_lw( XPTR( child_cxy , &child_ptr->type ) ) == INODE_TYPE_DIR ) 
1582                {
1583                    if( child_cxy == local_cxy )
1584                    {
1585                        error = vfs_mapper_load_all( child_ptr );
1586                    }
1587                    else
1588                    {
1589                        rpc_vfs_mapper_load_all_client( child_cxy,
1590                                                        child_ptr,
1591                                                        &error );
1592                    }
1593                    if ( error )
1594                    {
1595                        printk("\n[ERROR] in %s : cannot load <%s> from device\n",
1596                        __FUNCTION__ , name );
1597                        vfs_remove_child_from_parent( child_xp );
1598                        return EIO;
1599                    }
[238]1600
[438]1601#if (DEBUG_VFS_LOOKUP & 1)
1602if( DEBUG_VFS_LOOKUP < cycle )
[459]1603printk("\n[DBG] %s : load mapper from device for node <%s> in path %s\n",
1604__FUNCTION__ , name, pathname );
[433]1605#endif
[459]1606                }
1607            }
[407]1608
[459]1609            // take lock on parent inode
1610            vfs_inode_lock( parent_xp );
[1]1611        }
[459]1612        else   // child found in inode tree
1613        {
1614       
[438]1615#if (DEBUG_VFS_LOOKUP & 1)
1616if( DEBUG_VFS_LOOKUP < cycle )
[433]1617printk("\n[DBG] %s : found <%s> / inode %x in cluster %x\n",
1618__FUNCTION__ , name , GET_PTR(child_xp) , GET_CXY(child_xp) );
1619#endif
[459]1620            child_ptr  = GET_PTR( child_xp );
1621            child_cxy  = GET_CXY( child_xp );
1622            parent_cxy = GET_CXY( parent_xp );
1623            parent_ptr = GET_PTR( parent_xp );
[101]1624
[459]1625            if( last && (mode & VFS_LOOKUP_CREATE) && (mode & VFS_LOOKUP_EXCL) )
1626            {
1627                printk("\n[ERROR] in %s : node already exist <%s>\n", __FUNCTION__, name );
1628                return EINVAL;
1629            }
1630        }
1631
[380]1632        // TODO check access rights here [AG]
[23]1633        // error = vfs_access_denied( child_xp,
1634        //                            client_uid,
1635        //                            client_gid );
1636        // if( error )
1637        // {
[441]1638        //     printk("\n[ERROR] in %s : thread %x / permission denied for %s\n",
1639        //     __FUNCTION__ , this , name );
[23]1640        //     return EACCES;
1641        // }
[1]1642
[238]1643        // take lock on child inode and release lock on parent
1644        vfs_inode_lock( child_xp );
[101]1645        vfs_inode_unlock( parent_xp );
[1]1646
1647        // update loop variables
1648        parent_xp = child_xp;
1649        current   = next;
1650    }
1651    while( last == false );
1652
[238]1653    // release lock
1654    vfs_inode_unlock( parent_xp );
[1]1655
[438]1656#if DEBUG_VFS_LOOKUP
[433]1657cycle = (uint32_t)hal_get_cycles();
[438]1658if( DEBUG_VFS_LOOKUP < cycle )
[459]1659printk("\n[DBG] %s : thread %x in process %x exit for <%s>\n" 
1660"     parent %x in cluster %x / child %x in cluster %x / cycle %d\n",
1661__FUNCTION__ , this->trdid, process->pid, pathname, 
1662parent_ptr, parent_cxy, child_ptr, child_cxy, cycle );
[433]1663#endif
[1]1664
[238]1665    // return searched pointer
[459]1666    if( mode & VFS_LOOKUP_PARENT ) *inode_xp = parent_xp;
1667    else                           *inode_xp = child_xp;
[1]1668
1669    return 0;
1670
1671}  // end vfs_lookup()
1672
1673////////////////////////////////////////////
1674error_t vfs_get_path( xptr_t    searched_xp,
1675                      char    * buffer,
1676                      uint32_t  max_size )
1677{
1678        xptr_t       dentry_xp;   // extended pointer on current dentry
1679    char       * name;        // local pointer on current dentry name
1680        uint32_t     length;      // length of current dentry name
1681        uint32_t     count;       // number of characters written in buffer
1682        uint32_t     index;       // slot index in buffer
[23]1683    xptr_t       inode_xp;    // extended pointer on   
[1]1684
1685    // implementation note:
1686    // we use two variables "index" and "count" because the buffer
[401]1687    // is written in decreasing index order (from leaf to root)
[433]1688    // TODO  : handle conflict with a concurrent rename [AG]
1689    // FIXME : handle synchro in the loop  [AG]
[1]1690
1691        // set the NUL character in buffer / initialise buffer index and count
1692        buffer[max_size - 1] = 0;
1693        count    = 1;
1694    index    = max_size - 2;
1695
1696    // initialize current inode
1697    inode_xp  = searched_xp;
1698
1699    // exit when root inode found (i.e. dentry_xp == XPTR_NULL)
1700        do
1701    {
1702        // get inode cluster and local pointer
1703        cxy_t         inode_cxy = GET_CXY( inode_xp );
[473]1704        vfs_inode_t * inode_ptr = GET_PTR( inode_xp );
[1]1705
1706        // get extended pointer on parent dentry               
1707        dentry_xp = (xptr_t)hal_remote_lwd( XPTR( inode_cxy , inode_ptr->parent_xp ) );
1708
1709        // get dentry cluster and local pointer
1710        cxy_t          dentry_cxy = GET_CXY( dentry_xp );
[473]1711        vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp );
[1]1712
1713        // get dentry name length and pointer
1714        length =  hal_remote_lw( XPTR( dentry_cxy , &dentry_ptr->length ) );
1715        name   = (char *)hal_remote_lpt( XPTR( dentry_cxy , &dentry_ptr->name ) );
1716
1717        // update index and count
1718        index -= (length + 1); 
1719        count += (length + 1);
1720
1721        // check buffer overflow
1722        if( count >= max_size )
1723        {
1724            printk("\n[ERROR] in %s : kernel buffer too small\n", __FUNCTION__ );
1725            return EINVAL;
1726        }
1727
1728        // update pathname
1729        hal_remote_memcpy( XPTR( local_cxy , &buffer[index + 1] ) ,
1730                           XPTR( dentry_cxy , name ) , length );
1731                buffer[index] = '/';
1732
1733                // get extended pointer on next inode
1734        inode_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , dentry_ptr->parent ) );
1735    }
1736    while( (dentry_xp != XPTR_NULL) );
1737
1738        return 0;
1739
1740}  // end vfs_get_path()
1741
[188]1742     
1743//////////////////////////////////////////////////////////////
1744error_t vfs_add_child_in_parent( cxy_t              child_cxy,
1745                                 vfs_inode_type_t   inode_type,
[23]1746                                 vfs_fs_type_t      fs_type,
1747                                 xptr_t             parent_xp,
1748                                 char             * name,
[188]1749                                 void             * extend,
[23]1750                                 xptr_t           * child_xp )
[1]1751{
[23]1752    error_t         error;
1753    xptr_t          dentry_xp;   // extended pointer on created dentry
1754    xptr_t          inode_xp;    // extended pointer on created inode
1755    cxy_t           parent_cxy;  // parent inode cluster identifier
1756    vfs_inode_t   * parent_ptr;  // parent inode local pointer
[1]1757
1758    // get parent inode cluster and local pointer
[23]1759    parent_cxy = GET_CXY( parent_xp );
[459]1760    parent_ptr = GET_PTR( parent_xp );
[1]1761
[438]1762#if DEBUG_VFS_ADD_CHILD
[433]1763uint32_t cycle = (uint32_t)hal_get_cycles();
[438]1764if( DEBUG_VFS_ADD_CHILD < cycle )
[459]1765printk("\n[DBG] %s : thread %x enter for <%s> / child_cxy = %x / parent_cxy = %x / cycle %d\n",
1766__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, name,
1767child_cxy, parent_cxy, (uint32_t)hal_get_cycles() );
[433]1768#endif
[279]1769
[204]1770    // 1. create dentry
[1]1771    if( parent_cxy == local_cxy )      // parent cluster is the local cluster
1772    {
[23]1773        error = vfs_dentry_create( fs_type,
[1]1774                                   name,
1775                                   parent_ptr,
1776                                   &dentry_xp );
[279]1777
[438]1778#if (DEBUG_VFS_ADD_CHILD & 1)
1779if( (DEBUG_VFS_ADD_CHILD < cycle) && (error == 0) )
[433]1780printk("\n[DBG] %s : dentry <%s> created in cluster %x\n", __FUNCTION__, name, local_cxy );
1781#endif
[407]1782
[1]1783    }
1784    else                               // parent cluster is remote
1785    {
1786        rpc_vfs_dentry_create_client( parent_cxy,
[23]1787                                      fs_type,
[1]1788                                      name,
1789                                      parent_ptr,
1790                                      &dentry_xp,
1791                                      &error );
[279]1792
[438]1793#if (DEBUG_VFS_ADD_CHILD & 1)
1794if( (DEBUG_VFS_ADD_CHILD < cycle) && (error == 0) )
[433]1795printk("\n[DBG] %s : dentry <%s> created in cluster %x\n", __FUNCTION__, name, parent_cxy );
1796#endif
[407]1797
[1]1798    }
1799                                     
1800    if( error )
1801    {
[437]1802        printk("\n[ERROR] in %s : cannot create dentry <%s> in cluster %x\n",
1803        __FUNCTION__ , name , parent_cxy );
[204]1804        return ENOMEM;
[1]1805    }
1806
[204]1807    // 2. create child inode TODO : define attr / mode / uid / gid
[1]1808    uint32_t attr = 0;
1809    uint32_t mode = 0;
1810    uint32_t uid  = 0;
1811    uint32_t gid  = 0;
1812   
1813    if( child_cxy == local_cxy )      // child cluster is the local cluster
1814    {
1815        error = vfs_inode_create( dentry_xp,
[23]1816                                  fs_type,
1817                                  inode_type,
[188]1818                                  extend,
[1]1819                                  attr,
1820                                  mode,
1821                                  uid,
1822                                  gid,
1823                                  &inode_xp );
[279]1824
[438]1825#if (DEBUG_VFS_ADD_CHILD & 1)
1826if( DEBUG_VFS_ADD_CHILD < cycle )
[433]1827printk("\n[DBG] %s : inode <%x> created in cluster %x\n",
1828__FUNCTION__ , GET_PTR(inode_xp) , local_cxy );
1829#endif
1830
[1]1831    }
1832    else                              // child cluster is remote
1833    {
1834        rpc_vfs_inode_create_client( child_cxy,
1835                                     dentry_xp,
[23]1836                                     fs_type,
1837                                     inode_type,
[188]1838                                     extend,
[1]1839                                     attr,
1840                                     mode,
1841                                     uid,
1842                                     gid,
1843                                     &inode_xp,
1844                                     &error );
[279]1845
[438]1846#if (DEBUG_VFS_ADD_CHILD & 1)
1847if( DEBUG_VFS_ADD_CHILD < cycle )
[433]1848printk("\n[DBG] %s : inode <%s> created in cluster %x\n",
[407]1849__FUNCTION__ , GET_PTR(inode_xp) , child_cxy );
[433]1850#endif
[407]1851
[1]1852    }
1853                                     
1854    if( error )
1855    {
1856        printk("\n[ERROR] in %s : cannot create inode in cluster %x\n",
1857               __FUNCTION__ , child_cxy );
1858 
[459]1859        vfs_dentry_t * dentry = GET_PTR( dentry_xp );
[1]1860        if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry );
[459]1861        else rpc_vfs_dentry_destroy_client( parent_cxy , dentry , &error );
[204]1862        return ENOMEM;
[1]1863    }
1864
[204]1865    // 3. update extended pointer on inode in dentry
1866    cxy_t          dentry_cxy = GET_CXY( dentry_xp );
[473]1867    vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp );
[204]1868    hal_remote_swd( XPTR( dentry_cxy , &dentry_ptr->child_xp ) , inode_xp );
1869
[438]1870#if DEBUG_VFS_ADD_CHILD
[433]1871cycle = (uint32_t)hal_get_cycles();
[438]1872if( DEBUG_VFS_ADD_CHILD < cycle )
[459]1873printk("\n[DBG] %s : thread %x in process %x exit for <%s>\n",
1874__FUNCTION__, CURRENT_THREAD, CURRENT_THREAD->process->pid, name );
[433]1875#endif
[296]1876
[1]1877    // success : return extended pointer on child inode
1878    *child_xp = inode_xp;
1879    return 0;
1880
[459]1881    // FIXME update the refcount fields in both inode and dentry
1882
[1]1883}  // end vfs_add_child_in_parent()
1884
[459]1885///////////////////////////////////////////////////////
1886error_t vfs_remove_child_from_parent( xptr_t inode_xp )
1887{
1888    cxy_t          inode_cxy;
1889    vfs_inode_t  * inode_ptr;
1890    xptr_t         dentry_xp;
1891    cxy_t          dentry_cxy;
1892    vfs_dentry_t * dentry_ptr;
1893    error_t        error;
1894   
1895    // get inode cluster and local pointer
1896    inode_cxy = GET_CXY( inode_xp );
1897    inode_ptr = GET_PTR( inode_xp );
1898
1899    // get cluster and pointers of associated dentry
1900    dentry_xp  = hal_remote_lwd( XPTR( inode_cxy , &inode_ptr->parent_xp ) );
1901    dentry_cxy = GET_CXY( dentry_xp ); 
1902    dentry_ptr = GET_PTR( dentry_xp );
1903
1904    // FIXME update the refcount fields in both inode and dentry
1905
1906    // delete dentry
1907    if( dentry_cxy == local_cxy )
1908    {
1909         error = vfs_dentry_destroy( dentry_ptr );
1910    }
1911    else
1912    {
1913         rpc_vfs_dentry_destroy_client( dentry_cxy,
1914                                        dentry_ptr,
1915                                        &error );
1916    }
1917    if( error ) return EINVAL;
1918
1919    // delete inode
1920    if( inode_cxy == local_cxy )
1921    {
1922         vfs_inode_destroy( inode_ptr );
1923    }
1924    else
1925    {
1926         rpc_vfs_inode_destroy_client( inode_cxy,
1927                                       inode_ptr,
1928                                       &error );
1929    }
1930    if( error ) return EINVAL;
1931
1932    return 0;
1933
1934}  // end vfs_remove_child_from_parent()
1935
[23]1936//////////////////////////////////////////////////////////////////////////////////////////
1937//            Mapper related functions
1938//////////////////////////////////////////////////////////////////////////////////////////
1939
[238]1940////////////////////////////////////////////
1941error_t vfs_mapper_move_page( page_t * page,
1942                              bool_t   to_mapper )
[23]1943{
[204]1944    error_t error = 0;
[23]1945
1946    assert( (page != NULL) , __FUNCTION__ , "page pointer is NULL\n" );
1947
[246]1948    mapper_t    * mapper = page->mapper;
[23]1949
1950    assert( (mapper != NULL) , __FUNCTION__ , "no mapper for page\n" );
1951
[438]1952#if DEBUG_VFS_MAPPER_MOVE
[433]1953uint32_t cycle = (uint32_t)hal_get_cycles();
[438]1954if( DEBUG_VFS_MAPPER_MOVE < cycle )
[433]1955printk("\n[DBG] %s : thread %x enter for page %d / mapper %x / inode %x / cycle %d\n",
1956__FUNCTION__, CURRENT_THREAD, page->index, mapper, mapper->inode, cycle );
1957#endif
[246]1958
[23]1959    // get FS type
[246]1960    vfs_fs_type_t fs_type = mapper->type;
[23]1961
[238]1962    // call relevant FS function
[23]1963    if( fs_type == FS_TYPE_FATFS )
1964    {
1965        rwlock_wr_lock( &mapper->lock );
[246]1966        error = fatfs_mapper_move_page( page , to_mapper ); 
[23]1967        rwlock_wr_unlock( &mapper->lock );
1968    }
1969    else if( fs_type == FS_TYPE_RAMFS )
1970    {
1971        assert( false , __FUNCTION__ , "should not be called for RAMFS\n" );
1972    }
1973    else if( fs_type == FS_TYPE_DEVFS )
1974    {
1975        assert( false , __FUNCTION__ , "should not be called for DEVFS\n" );
1976    }
1977    else
1978    {
1979        assert( false , __FUNCTION__ , "undefined file system type\n" );
1980    }
1981
[438]1982#if DEBUG_VFS_MAPPER_MOVE
[433]1983cycle = (uint32_t)hal_get_cycles();
[438]1984if( DEBUG_VFS_MAPPER_MOVE < cycle )
[433]1985printk("\n[DBG] %s : thread %x exit for page %d / mapper %x / inode %x / cycle %d\n",
1986__FUNCTION__, CURRENT_THREAD, page->index, mapper, mapper->inode, cycle );
1987#endif
[246]1988
[23]1989    return error;
1990
[238]1991}  // end vfs_move_page()
[23]1992
1993//////////////////////////////////////////////////
[238]1994error_t vfs_mapper_load_all( vfs_inode_t * inode )
[23]1995{
[265]1996    assert( (inode != NULL) , __FUNCTION__ , "inode pointer is NULL\n" );
[23]1997
[238]1998    uint32_t   index;
1999    page_t   * page;
[23]2000
[238]2001    mapper_t * mapper = inode->mapper;
2002    uint32_t   size   = inode->size;
[23]2003
[265]2004    assert( (mapper != NULL) , __FUNCTION__ , "mapper pointer is NULL\n" );
[23]2005
[438]2006#if DEBUG_VFS_MAPPER_LOAD
[433]2007uint32_t cycle = (uint32_t)hal_get_cycles();
[438]2008if( DEBUG_VFS_MAPPER_MOVE < cycle )
[433]2009printk("\n[DBG] %s : thread %x enter for inode %x in cluster %x / cycle %d\n",
2010__FUNCTION__, CURRENT_THREAD, inode, local_cxy, cycle );
2011#endif
[401]2012
2013    // compute number of pages
[238]2014    uint32_t npages = size >> CONFIG_PPM_PAGE_SHIFT;
[265]2015    if( (size & CONFIG_PPM_PAGE_MASK) || (size == 0) ) npages++;
[238]2016
[265]2017    // loop on pages
[238]2018    for( index = 0 ; index < npages ; index ++ )
[23]2019    {
[238]2020        // this function allocates the missing page in mapper,
2021        // and call the vfs_mapper_move_page() to load the page from device
2022        page = mapper_get_page( mapper , index );
[23]2023
[238]2024        if( page == NULL ) return EIO;
[23]2025    }
2026
[438]2027#if DEBUG_VFS_MAPPER_LOAD
[433]2028cycle = (uint32_t)hal_get_cycles();
[438]2029if( DEBUG_VFS_MAPPER_MOVE < cycle )
[433]2030printk("\n[DBG] %s : thread %x exit for inode %x in cluster %x / cycle %d\n",
2031__FUNCTION__, CURRENT_THREAD, inode, local_cxy, cycle );
2032#endif
[401]2033
[238]2034    return 0;
[23]2035
[238]2036}  // end vfs_mapper_load_all()
[23]2037
Note: See TracBrowser for help on using the repository browser.