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

Last change on this file since 656 was 656, checked in by alain, 4 years ago

Fix several bugs in the FATFS and in the VFS,
related to the creation of big files requiring
more than 4 Kbytes (one cluster) on device.

File size: 135.5 KB
Line 
1/*
2 * vfs.c - Virtual File System implementation.
3 *
4 * Author  Mohamed Lamine Karaoui (2015)
5 *         Alain Greiner (2016,2017,2018,2019)
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#include <kernel_config.h>
26#include <hal_kernel_types.h>
27#include <hal_atomic.h>
28#include <hal_special.h>
29#include <printk.h>
30#include <list.h>
31#include <xlist.h>
32#include <slist.h>
33#include <xhtab.h>
34#include <string.h>
35#include <rpc.h>
36#include <errno.h>
37#include <kmem.h>
38#include <mapper.h>
39#include <thread.h>
40#include <chdev.h>
41#include <process.h>
42#include <cluster.h>
43#include <vfs.h>
44#include <fatfs.h>
45#include <ramfs.h>
46#include <devfs.h>
47#include <syscalls.h>
48
49//////////////////////////////////////////////////////////////////////////////////////////
50//           Extern variables         
51//////////////////////////////////////////////////////////////////////////////////////////
52
53extern vfs_ctx_t          fs_context[FS_TYPES_NR];    // allocated in kernel_init.c
54extern chdev_directory_t  chdev_dir;                  // allocated in kernel_init.c 
55extern char *             lock_type_str[];            // allocated in kernel_init.c
56 
57///////////////////////////////////////////////////////////////////////////////////////////
58//           VFS Context related functions
59//////////////////////////////////////////////////////////////////////////////////////////
60
61////////////////////////////////////////
62void vfs_ctx_init( vfs_fs_type_t   type,
63                   uint32_t        attr,
64                       uint32_t        total_clusters,
65                       uint32_t        cluster_size,
66                       xptr_t          vfs_root_xp,
67                   void          * extend )
68{
69    vfs_ctx_t * vfs_ctx = &fs_context[type];
70
71    vfs_ctx->type           = type;
72    vfs_ctx->attr           = attr;
73    vfs_ctx->total_clusters = total_clusters;
74    vfs_ctx->cluster_size   = cluster_size;
75    vfs_ctx->vfs_root_xp    = vfs_root_xp;
76    vfs_ctx->extend         = extend;
77
78    busylock_init( &vfs_ctx->lock , LOCK_VFS_CTX );
79
80    bitmap_init( vfs_ctx->bitmap , BITMAP_SIZE(CONFIG_VFS_MAX_INODES) ); 
81}
82
83////////////////////////////////////////////
84error_t vfs_ctx_inum_alloc( vfs_ctx_t * ctx,
85                            uint32_t  * inum )
86{
87    // get lock on inum allocator
88    busylock_acquire( &ctx->lock );
89
90    // get lid from local inum allocator
91    uint32_t lid = bitmap_ffc( ctx->bitmap , CONFIG_VFS_MAX_INODES );
92
93    if( lid == 0xFFFFFFFF )   // no more free slot => error
94    {
95        // release lock
96        busylock_release( &ctx->lock );
97
98        // return error
99        return 1;
100    }
101    else              // found => return inum
102    {
103        // set slot allocated
104        bitmap_set( ctx->bitmap , lid );
105
106        // release lock
107        busylock_release( &ctx->lock );
108
109        // return inum
110        *inum = (((uint32_t)local_cxy) << 16) | (lid & 0xFFFF);
111        return 0;
112    }
113}
114
115////////////////////////////////////////////
116void vfs_ctx_inum_release( vfs_ctx_t * ctx,
117                           uint32_t    inum )
118{
119    bitmap_clear( ctx->bitmap , inum & 0xFFFF ); 
120}
121
122//////////////////////////////////////////////////////////////////////////////////////////
123//           VFS inode descriptor related functions
124//////////////////////////////////////////////////////////////////////////////////////////
125
126const char * vfs_inode_type_str( vfs_inode_type_t type )
127{
128    switch ( type ) 
129    {
130        case INODE_TYPE_FILE: return "FILE";
131        case INODE_TYPE_DIR:  return "DIR ";
132        case INODE_TYPE_FIFO: return "FIFO";
133        case INODE_TYPE_PIPE: return "PIPE";
134        case INODE_TYPE_SOCK: return "SOCK";
135        case INODE_TYPE_DEV:  return "DEV ";
136        case INODE_TYPE_BLK:  return "BLK ";
137        case INODE_TYPE_SYML: return "SYML";
138        default:              return "undefined";
139    }
140}
141
142////////////////////////////////////////////////////
143error_t vfs_inode_create( vfs_fs_type_t     fs_type,
144                          uint32_t          attr,
145                          uint32_t          rights,
146                          uid_t             uid,
147                          gid_t             gid,
148                          xptr_t          * inode_xp )
149{
150    mapper_t         * mapper;     // associated mapper( to be allocated)
151    vfs_inode_t      * inode;      // inode descriptor (to be allocated)
152   
153    uint32_t           inum;       // inode identifier (to be allocated)
154    vfs_ctx_t        * ctx;        // file system context
155        kmem_req_t         req;        // request to kernel memory allocator
156    error_t            error;
157
158    // check fs type and get pointer on context
159    if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
160    else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
161    else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS];
162    else
163    {
164        printk("\n[ERROR] in %s : illegal FS type\n", __FUNCTION__ );
165        return -1;
166    }
167
168    // allocate inum
169    error = vfs_ctx_inum_alloc( ctx , &inum );
170
171    if( error )
172    {
173        printk("\n[ERROR] in %s : cannot allocate inum\n", __FUNCTION__ );
174        return -1;
175    }
176
177    // allocate memory for mapper
178    mapper = mapper_create( fs_type );
179
180    if( mapper == NULL )
181    {
182        printk("\n[ERROR] in %s : cannot allocate mapper\n", __FUNCTION__ );
183        vfs_ctx_inum_release( ctx , inum );
184        return ENOMEM;
185    }
186
187// check inode descriptor contained in one page
188assert( (sizeof(vfs_inode_t) <= CONFIG_PPM_PAGE_SIZE),
189"inode descriptor must fit in one page" );
190
191    // allocate one page for VFS inode descriptor
192    // because the embedded "children xhtab footprint
193        req.type  = KMEM_PPM;
194        req.order = 0;
195    req.flags = AF_KERNEL | AF_ZERO;
196        inode     = kmem_alloc( &req );
197
198    if( inode == NULL )
199    {
200        printk("\n[ERROR] in %s : cannot allocate inode descriptor\n", __FUNCTION__ );
201        vfs_ctx_inum_release( ctx , inum );
202        mapper_destroy( mapper );
203        return -1;
204    }
205
206    // initialize inode descriptor
207    inode->type       = INODE_TYPE_FILE;     // default value
208    inode->inum       = inum;
209    inode->attr       = attr;
210    inode->rights     = rights;
211    inode->uid        = uid;
212    inode->gid        = gid;
213    inode->ctx        = ctx;
214    inode->mapper     = mapper;
215    inode->extend     = NULL;
216    inode->links      = 0;
217
218    // initialise inode field in mapper
219    mapper->inode     = inode;
220 
221    // initialize chidren dentries xhtab
222    xhtab_init( &inode->children , XHTAB_DENTRY_TYPE );
223
224    // initialize parents dentries xlist
225    xlist_root_init( XPTR( local_cxy , &inode->parents ) );
226 
227    // initialize lock protecting size
228    remote_rwlock_init( XPTR( local_cxy , &inode->size_lock ), LOCK_VFS_SIZE );
229
230    // initialise lock protecting inode tree traversal
231    remote_rwlock_init( XPTR( local_cxy , &inode->main_lock ), LOCK_VFS_MAIN );
232
233    // return extended pointer on inode
234    *inode_xp = XPTR( local_cxy , inode );
235
236#if DEBUG_VFS_INODE_CREATE
237uint32_t       cycle      = (uint32_t)hal_get_cycles();
238thread_t *     this       = CURRENT_THREAD;
239if( DEBUG_VFS_INODE_CREATE < cycle )
240printk("\n[%s] thread[%x,%x] created inode (%x,%x) / cycle %d\n",
241__FUNCTION__, this->process->pid, this->trdid, local_cxy, inode, cycle );
242#endif
243 
244    return 0;
245
246}  // end vfs_inode_create() 
247
248/////////////////////////////////////////////
249void vfs_inode_destroy( vfs_inode_t * inode )
250{
251    // release memory allocated for mapper
252    mapper_destroy( inode->mapper );
253
254    // release memory allocate for inode descriptor
255        kmem_req_t req;
256        req.type  = KMEM_PPM;
257        req.ptr   = inode;
258        kmem_free( &req );
259
260}  // end vfs_inode_destroy()
261
262//////////////////////////////////////////////
263uint32_t vfs_inode_get_size( xptr_t inode_xp )
264{
265    // get inode cluster and local pointer
266    cxy_t         cxy = GET_CXY( inode_xp );
267    vfs_inode_t * ptr = GET_PTR( inode_xp );
268
269    // build extended pointers on lock & size
270    xptr_t   lock_xp = XPTR( cxy , &ptr->size_lock );
271    xptr_t   size_xp = XPTR( cxy , &ptr->size );
272
273    // take lock in read mode
274    remote_rwlock_rd_acquire( lock_xp );
275
276    // get size
277    uint32_t size = hal_remote_l32( size_xp );
278
279    // release lock from read mode
280    remote_rwlock_rd_release( lock_xp );
281
282    return size;
283}
284
285///////////////////////////////////////////////
286void vfs_inode_update_size( xptr_t    inode_xp,
287                            uint32_t  size )
288{
289    // get inode cluster and local pointer
290    cxy_t         cxy = GET_CXY( inode_xp );
291    vfs_inode_t * ptr = GET_PTR( inode_xp );
292
293    // build extended pointers on lock & size
294    xptr_t   lock_xp = XPTR( cxy , &ptr->size_lock );
295    xptr_t   size_xp = XPTR( cxy , &ptr->size );
296
297    // take lock in write mode
298    remote_rwlock_wr_acquire( lock_xp );
299
300    // get current size
301    uint32_t current_size = hal_remote_l32( size_xp );
302
303    // set size if required
304    if( current_size < size ) hal_remote_s32( size_xp , size );
305
306    // release lock from write mode
307    remote_rwlock_wr_release( lock_xp );
308}
309
310////////////////////////////////////////
311void vfs_inode_unlock( xptr_t inode_xp )
312{
313    // get inode cluster and local pointer
314    cxy_t         cxy = GET_CXY( inode_xp );
315    vfs_inode_t * ptr = GET_PTR( inode_xp );
316
317    // release the main lock
318    remote_busylock_release( XPTR( cxy , &ptr->main_lock ) );
319}
320
321//////////////////////////////////////
322void vfs_inode_lock( xptr_t inode_xp )
323{
324    // get inode cluster and local pointer
325    cxy_t         cxy = GET_CXY( inode_xp );
326    vfs_inode_t * ptr = GET_PTR( inode_xp );
327
328    // get the main lock
329    remote_busylock_acquire( XPTR( cxy , &ptr->main_lock ) );
330}
331
332///////////////////////////////////////////
333void vfs_inode_get_name( xptr_t   inode_xp,
334                         char   * name )
335{
336    cxy_t          inode_cxy;          // inode cluster identifier
337    vfs_inode_t  * inode_ptr;          // local pointer on inode
338    xptr_t         parents_root_xp;    // extended pointer on inode parents root
339   
340    // get inode cluster and local pointer
341    inode_cxy = GET_CXY( inode_xp );
342    inode_ptr = GET_PTR( inode_xp );
343
344    // build extended pointer on parents dentries root
345    parents_root_xp  = XPTR( inode_cxy , &inode_ptr->parents );
346
347    // check VFS root
348    if( xlist_is_empty( parents_root_xp ) )  // inode is the VFS root
349    {
350        strcpy( name , "/" );
351    }
352    else                                     // not the VFS root
353    {
354        xptr_t         dentry_xp;
355        cxy_t          dentry_cxy;
356        vfs_dentry_t * dentry_ptr;
357
358        // get first name in list of parents
359        dentry_xp  = XLIST_FIRST( parents_root_xp , vfs_dentry_t , parents );
360        dentry_cxy = GET_CXY( dentry_xp );
361        dentry_ptr = GET_PTR( dentry_xp );
362
363        hal_remote_strcpy( XPTR( local_cxy  , name ) , 
364                           XPTR( dentry_cxy , dentry_ptr->name ) );
365    }
366
367}  // end vfs_inode_get_name()
368
369///////////////////////////////////////////////////////
370error_t vfs_inode_load_all_pages( vfs_inode_t * inode )
371{
372
373assert( (inode != NULL) , "inode pointer is NULL" );
374
375    uint32_t   page_id;
376    xptr_t     page_xp;
377
378    mapper_t * mapper = inode->mapper;
379    uint32_t   size   = inode->size;
380
381assert( (mapper != NULL) , "mapper pointer is NULL" );
382
383#if DEBUG_VFS_INODE_LOAD_ALL
384uint32_t   cycle = (uint32_t)hal_get_cycles();
385thread_t * this  = CURRENT_THREAD;
386char       name[CONFIG_VFS_MAX_NAME_LENGTH];
387vfs_inode_get_name( XPTR( local_cxy , inode ) , name );
388if( DEBUG_VFS_INODE_LOAD_ALL < cycle )
389printk("\n[%s] thread[%x,%x] enter for <%s> in cluster %x / cycle %d\n",
390__FUNCTION__, this->process->pid, this->trdid, name, local_cxy, cycle );
391#endif
392
393    // compute number of pages
394    uint32_t npages = size >> CONFIG_PPM_PAGE_SHIFT;
395    if( (size & CONFIG_PPM_PAGE_MASK) || (size == 0) ) npages++;
396
397    // loop on pages
398    for( page_id = 0 ; page_id < npages ; page_id ++ )
399    {
400        // If the mage is missing, this function allocates the missing page,
401        // and load the page from IOC device into mapper
402        page_xp = mapper_remote_get_page( XPTR( local_cxy , mapper ), page_id );
403
404        if( page_xp == XPTR_NULL ) return -1;
405    }
406
407#if DEBUG_VFS_INODE_LOAD_ALL
408cycle = (uint32_t)hal_get_cycles();
409if( DEBUG_VFS_INODE_LOAD_ALL < cycle )
410printk("\n[%s] thread[%x,%x] exit for <%x> in cluster %x / cycle %d\n",
411__FUNCTION__, this->process->pid, this->trdid, name, local_cxy, cycle );
412#endif
413
414    return 0;
415
416}  // end vfs_inode_load_all_pages()
417
418/////////////////////////////////////////
419void vfs_inode_display( xptr_t inode_xp )
420{
421    assert( (inode_xp != XPTR_NULL), "inode pointer is NULL");
422
423    char  name[CONFIG_VFS_MAX_NAME_LENGTH];
424
425    vfs_inode_get_name( inode_xp , name );
426
427    cxy_t         inode_cxy = GET_CXY( inode_xp );
428    vfs_inode_t * inode_ptr = GET_PTR( inode_xp );
429
430    vfs_inode_type_t type    = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) );
431    uint32_t         attr    = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->attr ) );
432    uint32_t         size    = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->size ) );
433    uint32_t         parents = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->links ) );
434    mapper_t       * mapper  = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->mapper ) );
435    void           * extend  = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->extend ) );
436
437    printk("\n**** inode <%s>\n"
438           " - type    = %s\n"
439           " - attr    = %x\n"
440           " - size    = %d\n"
441           " - parents = %d\n"
442           " - cxy     = %x\n"
443           " - inode   = %x\n"
444           " - mapper  = %x\n"
445           " - extend  = %x\n",
446           name,
447           vfs_inode_type_str( type ),
448           attr,
449           size,
450           parents,
451           inode_cxy,
452           inode_ptr,
453           mapper,
454           extend );
455
456}  // end vfs_inode_display()
457
458
459////////////////////////////////////////////////////////////////////////////////////////////
460//          VFS dentry descriptor related functions
461//////////////////////////////////////////////////////////////////////////////////////////
462
463///////////////////////////////////////////////////
464error_t vfs_dentry_create( vfs_fs_type_t   fs_type,
465                           char          * name,
466                           xptr_t        * dentry_xp )
467{
468    vfs_ctx_t      * ctx;        // context descriptor
469    vfs_dentry_t   * dentry;     // dentry descriptor (to be allocated)
470        kmem_req_t       req;        // request to kernel memory allocator
471
472    // get pointer on context
473    if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
474    else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
475    else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS];
476    else 
477    {
478        ctx = NULL;
479        return -1;
480    }
481
482    // get name length
483    uint32_t length = strlen( name );
484
485    if( length >= CONFIG_VFS_MAX_NAME_LENGTH ) return EINVAL;
486
487    // allocate memory for dentry descriptor
488        req.type  = KMEM_KCM;
489        req.order = bits_log2( sizeof(vfs_dentry_t) );
490    req.flags = AF_KERNEL | AF_ZERO;
491        dentry    = kmem_alloc( &req );
492
493    if( dentry == NULL ) 
494    {
495        printk("\n[ERROR] in %s : cannot allocate dentry descriptor\n",
496        __FUNCTION__ );
497        return -1;
498    }
499
500    // initialize dentry descriptor
501    dentry->ctx     = ctx;
502    dentry->length  = length;
503    dentry->extend  = NULL;
504    strcpy( dentry->name , name );
505
506    // return extended pointer on dentry
507    *dentry_xp = XPTR( local_cxy , dentry );
508
509#if DEBUG_VFS_DENTRY_CREATE
510thread_t * this  = CURRENT_THREAD;
511uint32_t   cycle = (uint32_t)hal_get_cycles();
512if( DEBUG_VFS_DENTRY_CREATE < cycle )
513printk("\n[%s] thread[%x,%x] created dentry <%s> : (%x,%x) / cycle %d\n",
514__FUNCTION__, this->process->pid, this->trdid, name, local_cxy, dentry, cycle );
515#endif
516
517    return 0;
518
519}  // end vfs_dentry_create()
520
521////////////////////////////////////////////////
522void vfs_dentry_destroy( vfs_dentry_t * dentry )
523{
524    // release memory allocated to dentry
525        kmem_req_t req;
526        req.type  = KMEM_KCM;
527        req.ptr   = dentry;
528        kmem_free( &req );
529
530}  // end vfs_dentry_destroy()
531
532
533//////////////////////////////////////////////////////////////////////////////////////////
534//       VFS file descriptor related functions
535//////////////////////////////////////////////////////////////////////////////////////////
536
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#if DEBUG_VFS_FILE_CREATE
546thread_t * this = CURRENT_THREAD;
547uint32_t cycle = (uint32_t)hal_get_cycles();
548if( DEBUG_VFS_OPEN < cycle )
549printk("\n[%s] thread[%x,%x] enter for inode %x in cluster %x / cycle %d\n",
550__FUNCTION__, this->process->pid, this->trdid, inode, local_cxy, cycle );
551#endif
552
553    // allocate memory for new file descriptor
554        req.type  = KMEM_KCM;
555        req.order = bits_log2( sizeof(vfs_file_t) );
556    req.flags = AF_KERNEL | AF_ZERO;
557        file      = kmem_alloc( &req );
558
559    if( file == NULL ) return ENOMEM;
560
561    // initializes new file descriptor
562    file->gc       = 0;
563    file->type     = inode->type;
564    file->attr     = attr;
565    file->offset   = 0;
566    file->refcount = 1;
567    file->inode    = inode;
568    file->ctx      = inode->ctx;
569    file->mapper   = inode->mapper;
570
571    remote_rwlock_init( XPTR( local_cxy , &file->lock ), LOCK_VFS_FILE );
572
573    *file_xp = XPTR( local_cxy , file );
574
575#if DEBUG_VFS_FILE_CREATE
576cycle = (uint32_t)hal_get_cycles();
577if( DEBUG_VFS_OPEN < cycle )
578printk("\n[%s] thread[%x,%x] created file %x in cluster %x / cycle %d\n",
579__FUNCTION__, this->process->pid, this->trdid, file, local_cxy, cycle );
580#endif
581
582    return 0;
583
584}  // end vfs_file_create()
585
586///////////////////////////////////////////
587void vfs_file_destroy( vfs_file_t *  file )
588{
589        kmem_req_t req;
590        req.type  = KMEM_KCM;
591        req.ptr   = file;
592        kmem_free( &req );
593
594#if DEBUG_VFS_CLOSE
595char name[CONFIG_VFS_MAX_NAME_LENGTH];
596vfs_file_get_name( XPTR( local_cxy , file ) , name );
597thread_t * this = CURRENT_THREAD;
598uint32_t cycle = (uint32_t)hal_get_cycles();
599if( DEBUG_VFS_CLOSE < cycle )
600printk("\n[%s] thread[%x,%x] deleted file <%s> in cluster %x / cycle %d\n",
601__FUNCTION__, this->process->pid, this->trdid, name, local_cxy, cycle );
602#endif
603
604}  // end vfs_file_destroy()
605
606
607////////////////////////////////////////
608void vfs_file_count_up( xptr_t file_xp )
609{
610    // get file cluster and local pointer
611    cxy_t        file_cxy = GET_CXY( file_xp );
612    vfs_file_t * file_ptr = GET_PTR( file_xp ); 
613
614    // atomically increment count
615    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , 1 ); 
616}
617
618//////////////////////////////////////////
619void vfs_file_count_down( xptr_t file_xp )
620{
621    // get file cluster and local pointer
622    cxy_t        file_cxy = GET_CXY( file_xp );
623    vfs_file_t * file_ptr = GET_PTR( file_xp ); 
624
625    // atomically decrement count
626    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , -1 ); 
627}
628
629///////////////////////////////////////
630void vfs_file_get_name( xptr_t file_xp,
631                        char * name )
632{
633    // get cluster and local pointer on remote file
634    vfs_file_t * file_ptr = GET_PTR( file_xp );
635    cxy_t        file_cxy = GET_CXY( file_xp );
636
637    // get pointers on remote inode
638    vfs_inode_t * inode_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) ); 
639    xptr_t        inode_xp  = XPTR( file_cxy , inode_ptr );
640
641    // call the relevant function
642    vfs_inode_get_name( inode_xp , name );
643}
644
645
646//////////////////////////////////////////////////////////////////////////////////////////
647//           "syscalls" API related functions
648//////////////////////////////////////////////////////////////////////////////////////////
649
650//////////////////////////////////////
651error_t vfs_open( xptr_t      root_xp,
652                          char      * path,
653                  xptr_t      process_xp,
654                          uint32_t    flags,
655                  uint32_t    mode, 
656                          xptr_t    * new_file_xp,
657                  uint32_t  * new_file_id )
658{
659    error_t        error;
660    xptr_t         inode_xp;       // extended pointer on target inode
661    cxy_t          inode_cxy;      // inode cluster identifier       
662    vfs_inode_t  * inode_ptr;      // inode local pointer
663    uint32_t       file_attr;      // file descriptor attributes
664    uint32_t       lookup_mode;    // lookup working mode       
665    xptr_t         file_xp;        // extended pointer on created file descriptor
666    uint32_t       file_id;        // created file descriptor index in reference fd_array
667    xptr_t         vfs_root_xp;    // extended pointer on VFS root inode
668    vfs_inode_t  * vfs_root_ptr;   // local pointer on VFS root inode
669    cxy_t          vfs_root_cxy;   // VFS root inode cluster identifier
670    xptr_t         lock_xp;        // extended pointer on Inode Tree lock
671
672    if( mode != 0 )
673    {
674        printk("\n[ERROR] in %s : the mode parameter is not supported yet\n" );
675        return -1;
676    }
677
678    thread_t  * this    = CURRENT_THREAD;
679    process_t * process = this->process;
680
681    // compute lookup working mode
682    lookup_mode = VFS_LOOKUP_OPEN;
683    if( (flags & O_DIR    )      )  lookup_mode |= VFS_LOOKUP_DIR;
684    if( (flags & O_CREAT  )      )  lookup_mode |= VFS_LOOKUP_CREATE;
685    if( (flags & O_EXCL   )      )  lookup_mode |= VFS_LOOKUP_EXCL;
686 
687#if DEBUG_VFS_OPEN
688uint32_t cycle = (uint32_t)hal_get_cycles();
689if( DEBUG_VFS_OPEN < cycle )
690printk("\n[%s] thread[%x,%x] enter for <%s> / root_inode (%x,%x) / cycle %d\n",
691__FUNCTION__, process->pid, this->trdid, path, GET_CXY(root_xp), GET_PTR(root_xp), cycle );
692#endif
693
694    // compute attributes for the created file
695    file_attr = 0;
696    if( (flags & O_RDONLY ) == 0 )  file_attr |= FD_ATTR_WRITE_ENABLE;
697    if( (flags & O_WRONLY ) == 0 )  file_attr |= FD_ATTR_READ_ENABLE;
698    if( (flags & O_SYNC   )      )  file_attr |= FD_ATTR_SYNC;
699    if( (flags & O_APPEND )      )  file_attr |= FD_ATTR_APPEND;
700    if( (flags & O_CLOEXEC)      )  file_attr |= FD_ATTR_CLOSE_EXEC;
701
702    // build extended pointer on lock protecting Inode Tree
703    vfs_root_xp  = process->vfs_root_xp;
704    vfs_root_ptr = GET_PTR( vfs_root_xp );
705    vfs_root_cxy = GET_CXY( vfs_root_xp );
706    lock_xp      = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock );
707
708    // take lock protecting Inode Tree in read mode
709    remote_rwlock_rd_acquire( lock_xp );
710
711    // get extended pointer on target inode
712    error = vfs_lookup( root_xp,
713                        path,
714                        lookup_mode,
715                        &inode_xp,
716                        NULL );
717
718    // release lock protecting Inode Tree
719    remote_rwlock_rd_release( lock_xp );
720
721    if( error )
722    {
723        printk("\n[ERROR] in %s : cannot get inode <%s>\n",
724        __FUNCTION__ , path );
725        return -1;
726    }
727
728    // get target inode cluster and local pointer
729    inode_cxy = GET_CXY( inode_xp );
730    inode_ptr = GET_PTR( inode_xp );
731   
732#if (DEBUG_VFS_OPEN & 1)
733cycle = (uint32_t)hal_get_cycles();
734if( DEBUG_VFS_OPEN < cycle )
735printk("\n[%s] thread[%x,%x] found inode(%x,%x) for <%s>\n",
736__FUNCTION__, process->pid, this->trdid, inode_cxy, inode_ptr, path );
737#endif
738
739    // create a new file descriptor in cluster containing inode
740    if( inode_cxy == local_cxy )      // target cluster is local
741    {
742        error = vfs_file_create( inode_ptr , file_attr , &file_xp );
743    }
744    else                              // target cluster is remote
745    {
746        rpc_vfs_file_create_client( inode_cxy , inode_ptr , file_attr , &file_xp , &error );
747    }
748
749    if( error )  return error;
750
751#if (DEBUG_VFS_OPEN & 1)
752cycle = (uint32_t)hal_get_cycles();
753if( DEBUG_VFS_OPEN < cycle )
754printk("\n[%s] thread[%x,%x] created file descriptor (%x,%x) for <%s>\n",
755__FUNCTION__, process->pid, this->trdid, GET_CXY(file_xp), GET_PTR(file_xp), path );
756#endif
757
758    // allocate and register a new file descriptor index in reference process
759    error = process_fd_register( process_xp , file_xp , &file_id );
760
761    if( error ) return error;
762
763#if DEBUG_VFS_OPEN
764cycle = (uint32_t)hal_get_cycles();
765if( DEBUG_VFS_OPEN < cycle )
766printk("\n[%s] thread[%x,%x] exit for <%s> / fdid %d / cxy %x / cycle %d\n",
767__FUNCTION__, process->pid, this->trdid, path, file_id, GET_CXY( file_xp ), cycle );
768#endif
769
770    // success
771    *new_file_xp = file_xp;
772    *new_file_id = file_id;
773    return 0;
774
775}  // end vfs_open()
776
777///////////////////////////////////////////
778uint32_t vfs_user_move( bool_t   to_buffer,
779                        xptr_t   file_xp,
780                        void   * buffer,
781                        uint32_t count )
782{
783    cxy_t              file_cxy;       // remote file descriptor cluster
784    vfs_file_t       * file_ptr;       // remote file descriptor local pointer
785    mapper_t         * mapper;         // local pointer on file mapper
786    vfs_inode_t      * inode;          // local pointer on file inode
787    vfs_inode_type_t   type;           // inode type
788    uint32_t           offset;         // current offset in file
789    uint32_t           size;           // current file size
790    uint32_t           nbytes;         // number of bytes actually transfered
791    error_t            error;
792
793// check argument
794assert( (file_xp != XPTR_NULL), "file_xp == XPTR_NULL" );
795
796    // get cluster and local pointer on remote file descriptor
797    file_cxy  = GET_CXY( file_xp );
798    file_ptr  = GET_PTR( file_xp );
799
800    // get various infos from remote file descriptor
801    type   = hal_remote_l32( XPTR( file_cxy , &file_ptr->type   ) );
802    offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) );
803    mapper = hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) );
804    inode  = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode  ) );
805    size   = hal_remote_l32( XPTR( file_cxy , &inode->size      ) );
806   
807// check inode type
808assert( (type == INODE_TYPE_FILE), "bad inode type" );
809
810#if DEBUG_VFS_USER_MOVE
811char          name[CONFIG_VFS_MAX_NAME_LENGTH];
812uint32_t      cycle      = (uint32_t)hal_get_cycles();
813thread_t    * this       = CURRENT_THREAD;
814vfs_inode_get_name( XPTR( file_cxy , inode ) , name );
815if( cycle > DEBUG_VFS_USER_MOVE )
816{
817    if( to_buffer ) 
818    printk("\n[%s] thread[%x,%x] enter / %d bytes / map(%s) -> buf(%x) / offset %d / cycle %d\n",
819    __FUNCTION__ , this->process->pid, this->trdid, count, name, buffer, offset, cycle );
820    else           
821    printk("\n[%s] thread[%x,%x] enter / %d bytes / buf(%x) -> map(%s) / offset %d / cycle %d\n",
822    __FUNCTION__ , this->process->pid, this->trdid, count, buffer, name, offset, cycle );
823}
824#endif
825
826    if( to_buffer ) // => compute the number of bytes to move and make the move
827    {
828        // compute number of bytes to move
829        if     ( size <= offset )         nbytes = 0; 
830        else if( size < offset + count )  nbytes = size - offset;
831        else                              nbytes = count;
832
833        // move data from mapper to buffer when required
834        if( nbytes > 0 )
835        {     
836            error = mapper_move_user( XPTR( file_cxy , mapper ),
837                                      to_buffer,
838                                      offset,
839                                      buffer,
840                                      nbytes );
841        }
842        else
843        {
844            error = 0;
845        }
846    }
847    else // to mapper => make the move and update the file size if required
848    {
849        nbytes = count;
850
851        // move data from buffer to mapper
852        error = mapper_move_user( XPTR( file_cxy , mapper ),
853                                  to_buffer,
854                                  offset,
855                                  buffer,
856                                  count );
857
858        // update file size in inode if required
859        if( offset + count > size ) 
860        {
861            vfs_inode_update_size( XPTR( file_cxy , inode ) , offset + count );
862        }
863    }
864       
865    if( error ) 
866    {
867        printk("\n[ERROR] in %s : cannot move data", __FUNCTION__ );
868        return -1;
869    }
870
871    // update file offset in file descriptor
872    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->offset ) , nbytes );
873
874#if DEBUG_VFS_USER_MOVE
875cycle = (uint32_t)hal_get_cycles();
876if( cycle > DEBUG_VFS_USER_MOVE )
877{
878    if( to_buffer ) 
879    printk("\n[%s] thread[%x,%x] exit / %d bytes moved from mapper to buffer\n",
880    __FUNCTION__ , this->process->pid, nbytes );
881    else           
882    printk("\n[%s] thread[%x,%x] exit / %d bytes moved from buffer to mapper / size %d\n",
883    __FUNCTION__ , this->process->pid, nbytes, hal_remote_l32(XPTR(file_cxy,&inode->size)) );
884}
885#endif
886
887    return nbytes;
888
889}  // end vfs_user_move()
890
891////////////////////////////////////////////
892error_t vfs_kernel_move( bool_t   to_buffer,
893                         xptr_t   file_xp,
894                         xptr_t   buffer_xp,
895                         uint32_t size )
896{
897    cxy_t              file_cxy;     // remote file descriptor cluster
898    vfs_file_t       * file_ptr;     // remote file descriptor local pointer
899    vfs_inode_type_t   inode_type;   // remote file type
900    uint32_t           file_offset;  // current offset in file
901    mapper_t         * mapper_ptr;   // remote mapper local pointer
902    xptr_t             mapper_xp;    // remote mapper extended pointer
903    error_t            error;
904
905// check argument
906assert( (file_xp != XPTR_NULL) , "file_xp == XPTR_NULL" );
907
908    // get cluster and local pointer on remote file descriptor
909    file_cxy  = GET_CXY( file_xp );
910    file_ptr  = GET_PTR( file_xp );
911
912    // get inode type from remote file descriptor
913    inode_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type   ) );
914
915// check inode type
916assert( (inode_type == INODE_TYPE_FILE), "bad file type" );
917
918    // get mapper pointers and file offset from file descriptor
919    file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) );
920    mapper_ptr  = hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) );
921    mapper_xp   = XPTR( file_cxy , mapper_ptr );
922
923    // move data between mapper and buffer
924    error = mapper_move_kernel( mapper_xp,
925                                to_buffer,
926                                file_offset,
927                                buffer_xp,
928                                size );
929    if( error ) 
930    {
931        printk("\n[ERROR] in %s : cannot move data", __FUNCTION__ );
932        return -1;
933    }
934
935#if DEBUG_VFS_KERNEL_MOVE
936char          name[CONFIG_VFS_MAX_NAME_LENGTH];
937uint32_t      cycle      = (uint32_t)hal_get_cycles();
938thread_t    * this       = CURRENT_THREAD;
939cxy_t         buffer_cxy = GET_CXY( buffer_xp );
940void        * buffer_ptr = GET_PTR( buffer_xp );
941vfs_inode_t * inode      = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
942vfs_inode_get_name( XPTR( file_cxy , inode ) , name );
943if( cycle > DEBUG_VFS_KERNEL_MOVE )
944{
945    if( to_buffer ) 
946    printk("\n[%s] thread[%x,%x] moves %d bytes from <%s> mapper to buffer(%x,%x) / cycle %d\n",
947    __FUNCTION__ , this->process->pid, this->trdid, size, name, buffer_cxy, buffer_ptr );
948    else           
949    printk("\n[%s] thread[%x,%x] moves %d bytes from buffer(%x,%x) to <%s> mapper / cycle %d\n",
950    __FUNCTION__ , this->process->pid, this->trdid, size, buffer_cxy, buffer_ptr, name );
951}
952#endif
953
954    return 0;
955
956}  // end vfs_kernel_move()
957
958//////////////////////////////////////
959error_t vfs_lseek( xptr_t     file_xp,
960                   uint32_t   offset,
961                   uint32_t   whence, 
962                   uint32_t * new_offset )
963{
964    xptr_t         offset_xp;
965    xptr_t         lock_xp;
966    xptr_t         size_xp;
967    cxy_t          file_cxy;
968    vfs_file_t  *  file_ptr;
969    vfs_inode_t *  inode_ptr;
970    uint32_t       new;
971
972// check arguments
973assert( (file_xp != XPTR_NULL) , "file_xp == XPTR_NULL" );
974assert( (new_offset != NULL )  , "new_offset == NULL" );
975
976    // get cluster and local pointer on remote file descriptor
977    file_cxy = GET_CXY( file_xp );
978    file_ptr = GET_PTR( file_xp );
979
980    // get local pointer on remote inode
981    inode_ptr = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
982
983    // build extended pointers on lock, offset and size
984    offset_xp = XPTR( file_cxy , &file_ptr->offset );
985    lock_xp   = XPTR( file_cxy , &file_ptr->lock );
986    size_xp   = XPTR( file_cxy , &inode_ptr->size );
987
988    // take file descriptor lock
989    remote_rwlock_wr_acquire( lock_xp );
990
991    if      ( whence == SEEK_CUR )   // new = current + offset
992    {
993        new = hal_remote_l32( offset_xp ) + offset;
994    }
995    else if ( whence == SEEK_SET )   // new = offset
996    {
997        new = offset;
998    }
999    else if ( whence == SEEK_END )   // new = size + offset
1000    { 
1001        new = hal_remote_l32( size_xp ) + offset;
1002    }
1003    else
1004    {
1005        printk("\n[ERROR] in %s : illegal whence value\n", __FUNCTION__ );
1006        remote_rwlock_wr_release( lock_xp );
1007        return -1;
1008    }
1009
1010#if DEBUG_VFS_LSEEK
1011uint32_t   cycle = (uint32_t)hal_get_cycles();
1012thread_t * this  = CURRENT_THREAD;
1013char       name[CONFIG_VFS_MAX_NAME_LENGTH];
1014vfs_inode_get_name( XPTR( file_cxy , inode_ptr ) , name );
1015if( cycle > DEBUG_VFS_LSEEK )
1016printk("\n[%s] thread[%x,%x] for <%s> / new offset %d / cycle %d\n",
1017__FUNCTION__ , this->process->pid, this->trdid, name, new, cycle );
1018#endif
1019
1020    // set new offset
1021    hal_remote_s32( offset_xp , new );
1022
1023    // release file descriptor lock
1024    remote_rwlock_wr_release( lock_xp );
1025
1026    // success
1027    *new_offset = new;
1028    return 0;
1029
1030}  // vfs_lseek()
1031
1032////////////////////////////////////
1033error_t vfs_close( xptr_t   file_xp,
1034                   uint32_t file_id )
1035{
1036    cxy_t         file_cxy;         // cluster containing the file descriptor.
1037    vfs_file_t  * file_ptr;         // local ponter on file descriptor
1038    cxy_t         owner_cxy;        // process owner cluster
1039    pid_t         pid;              // process identifier
1040    lpid_t        lpid;             // process local index
1041    xptr_t        root_xp;          // root of xlist (processes , or dentries)
1042    xptr_t        lock_xp;          // lock protecting the xlist
1043    xptr_t        iter_xp;          // iterator on xlist
1044    mapper_t    * mapper_ptr;       // local pointer on associated mapper
1045    vfs_inode_t * inode_ptr;        // local pointer on associated inode
1046    uint32_t      size;             // current file size (from inode descriptor)
1047    error_t       error;
1048
1049    char          name[CONFIG_VFS_MAX_NAME_LENGTH];  // file name
1050
1051// check argument
1052assert( (file_xp != XPTR_NULL) , "file_xp is XPTR_NULL" );
1053
1054    thread_t  * this    = CURRENT_THREAD;
1055    process_t * process = this->process;
1056    cluster_t * cluster = LOCAL_CLUSTER;
1057
1058    // get file name
1059    vfs_file_get_name( file_xp , name );
1060   
1061#if DEBUG_VFS_CLOSE
1062uint32_t cycle = (uint32_t)hal_get_cycles();
1063if( DEBUG_VFS_CLOSE < cycle )
1064printk("\n[%s] thread[%x,%x] enter for <%s> / cycle %d\n",
1065__FUNCTION__, process->pid, this->trdid, name, cycle );
1066#endif
1067
1068    // get cluster and local pointer on remote file descriptor
1069    file_cxy = GET_CXY( file_xp );
1070    file_ptr = GET_PTR( file_xp );
1071
1072    //////// 1) update all dirty pages from mapper to device
1073
1074    // get local pointer on mapper associated to file
1075    mapper_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) );
1076
1077    // copy all dirty pages from mapper to device
1078    if( file_cxy == local_cxy )
1079    {
1080        error = mapper_sync( mapper_ptr );
1081    }
1082    else
1083    {
1084        rpc_mapper_sync_client( file_cxy,
1085                                mapper_ptr,
1086                                &error );
1087    }
1088
1089    if( error )
1090    {
1091        printk("\n[ERROR] in %s : cannot synchronise dirty pages for <%s>\n",
1092        __FUNCTION__, name ); 
1093        return -1;
1094    }
1095
1096#if DEBUG_VFS_CLOSE
1097if( DEBUG_VFS_CLOSE < cycle )
1098printk("\n[%s] thread[%x,%x] synchronised mapper of <%s> to device\n",
1099__FUNCTION__, process->pid, this->trdid, name );
1100#endif
1101
1102    //////// 2) update file size in all parent directory mapper(s) and update device
1103
1104    // get local pointer on remote inode
1105    inode_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
1106
1107    // get file size from remote inode
1108    size = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) );
1109
1110    // get root of list of parents dentry
1111    root_xp = XPTR( file_cxy , &inode_ptr->parents );
1112
1113    // loop on all parents
1114    XLIST_FOREACH( root_xp , iter_xp )
1115    {
1116        // get pointers on parent directory dentry
1117        xptr_t         parent_dentry_xp  = XLIST_ELEMENT( iter_xp , vfs_dentry_t , parents );
1118        cxy_t          parent_cxy        = GET_CXY( parent_dentry_xp );
1119        vfs_dentry_t * parent_dentry_ptr = GET_PTR( parent_dentry_xp );
1120
1121        // get local pointer on parent directory inode
1122        vfs_inode_t * parent_inode_ptr = hal_remote_lpt( XPTR( parent_cxy, 
1123                                                         &parent_dentry_ptr->parent ) );
1124
1125        // get local pointer on parent directory mapper
1126        mapper_t * parent_mapper_ptr = hal_remote_lpt( XPTR( parent_cxy,
1127                                                       &parent_inode_ptr->mapper ) );
1128 
1129        // update dentry size in parent directory mapper
1130        if( parent_cxy == local_cxy )
1131        {
1132            error = vfs_fs_update_dentry( parent_inode_ptr,
1133                                          parent_dentry_ptr,
1134                                          size );
1135        }
1136        else
1137        {
1138            rpc_vfs_fs_update_dentry_client( parent_cxy,
1139                                             parent_inode_ptr,
1140                                             parent_dentry_ptr,
1141                                             size,
1142                                             &error );
1143        }
1144
1145        if( error )
1146        {
1147            printk("\n[ERROR] in %s : cannot update size in parent\n",
1148            __FUNCTION__ ); 
1149            return -1;
1150        }
1151
1152#if DEBUG_VFS_CLOSE
1153char parent_name[CONFIG_VFS_MAX_NAME_LENGTH];
1154vfs_inode_get_name( XPTR( parent_cxy , parent_inode_ptr ) , parent_name );
1155if( DEBUG_VFS_CLOSE < cycle )
1156printk("\n[%s] thread[%x,%x] updated <%s> in <%s> / size = %d bytes\n",
1157__FUNCTION__, process->pid, this->trdid, name, parent_name, size );
1158#endif
1159
1160        // copy all dirty pages from parent mapper to device
1161        if( parent_cxy == local_cxy )
1162        {
1163            error = mapper_sync( parent_mapper_ptr );
1164        }
1165        else
1166        {
1167            rpc_mapper_sync_client( parent_cxy,
1168                                    parent_mapper_ptr,
1169                                    &error );
1170        }
1171
1172        if( error )
1173        {
1174            printk("\n[ERROR] in %s : cannot synchronise parent mapper to device\n",
1175            __FUNCTION__ ); 
1176            return -1;
1177        }
1178
1179#if DEBUG_VFS_CLOSE
1180if( DEBUG_VFS_CLOSE < cycle )
1181printk("\n[%s] thread[%x,%x] synchonized mapper of parent <%s> to device\n",
1182__FUNCTION__, process->pid, this->trdid, parent_name );
1183#endif
1184
1185    }
1186
1187    //////// 3) loop on the process copies to reset all fd_array[file_id] entries
1188
1189    // get owner process cluster and lpid
1190    pid        = process->pid;
1191    owner_cxy  = CXY_FROM_PID( pid );
1192    lpid       = LPID_FROM_PID( pid );
1193
1194    // get extended pointers on copies root and lock
1195    root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] );
1196    lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] );
1197
1198    // take the lock protecting the list of copies
1199    remote_queuelock_acquire( lock_xp );
1200
1201    XLIST_FOREACH( root_xp , iter_xp )
1202    {
1203        xptr_t      process_xp  = XLIST_ELEMENT( iter_xp , process_t , copies_list );
1204        cxy_t       process_cxy = GET_CXY( process_xp );
1205        process_t * process_ptr = GET_PTR( process_xp );
1206
1207        xptr_t entry_xp = XPTR( process_cxy , &process_ptr->fd_array.array[file_id] );
1208        hal_remote_s64( entry_xp , XPTR_NULL );
1209        vfs_file_count_down( file_xp );
1210        hal_fence();
1211    }   
1212
1213    // release the lock protecting the list of copies
1214    remote_queuelock_release( lock_xp );
1215
1216#if DEBUG_VFS_CLOSE
1217if( DEBUG_VFS_CLOSE < cycle )
1218printk("\n[%s] thread[%x,%x] reset all fd-array copies for <%s>\n",
1219__FUNCTION__, process->pid, this->trdid, name );
1220#endif
1221
1222    //////// 4) release memory allocated to file descriptor in remote cluster
1223
1224    if( file_cxy == local_cxy )             // file cluster is local
1225    {
1226        vfs_file_destroy( file_ptr );
1227    }
1228    else                                    // file cluster is local
1229    {
1230        rpc_vfs_file_destroy_client( file_cxy , file_ptr );
1231    }
1232
1233#if DEBUG_VFS_CLOSE
1234cycle = (uint32_t)hal_get_cycles();
1235if( DEBUG_VFS_CLOSE < cycle )
1236printk("\n[%s] thread[%x,%x] exit / closed <%s> in process %x / cycle %d\n",
1237__FUNCTION__, process->pid, this->trdid, name, process->pid, cycle );
1238#endif
1239
1240    return 0;
1241
1242}  // end vfs_close()
1243
1244////////////////////////////////////
1245error_t vfs_mkdir( xptr_t   root_xp,
1246                   char   * path,
1247                   uint32_t rights )
1248{
1249    error_t        error;
1250    xptr_t         vfs_root_xp;        // extended pointer on VFS root inode
1251    vfs_inode_t  * vfs_root_ptr;       // local pointer on VFS root inode
1252    cxy_t          vfs_root_cxy;       // VFS root inode cluster identifier
1253    xptr_t         lock_xp;            // extended pointer on lock protecting Inode Tree
1254    xptr_t         inode_xp;           // extended pointer on new directory inode
1255    vfs_inode_t  * inode_ptr;          // local pointer on new directory inode
1256    cxy_t          inode_cxy;          // new directory inode cluster identifier
1257    xptr_t         dentry_xp;          // extended pointer on new dentry
1258    vfs_dentry_t * dentry_ptr;         // new dentry local pointer
1259    xptr_t         parent_xp;          // extended pointer on parent inode
1260    vfs_inode_t  * parent_ptr;         // local pointer on parent inode 
1261    cxy_t          parent_cxy;         // parent inode cluster identifier
1262    vfs_ctx_t    * parent_ctx_ptr;     // local pointer on parent inode context
1263    uint32_t       parent_fs_type;     // parent inode file system type
1264
1265    xptr_t         parents_root_xp;    // extended pointer on parents field in inode (root)
1266    xptr_t         parents_entry_xp;   // extended pointer on parents field in dentry
1267    xptr_t         children_xhtab_xp;  // extended pointer on children field in inode (root)
1268    xptr_t         children_entry_xp;  // extended pointer on children field in dentry
1269
1270    char           last_name[CONFIG_VFS_MAX_NAME_LENGTH];
1271
1272    thread_t  * this    = CURRENT_THREAD;
1273    process_t * process = this->process;
1274
1275#if DEBUG_VFS_MKDIR
1276char root_name[CONFIG_VFS_MAX_NAME_LENGTH];
1277vfs_inode_get_name( root_xp , root_name );
1278uint32_t   cycle = (uint32_t)hal_get_cycles();
1279if( DEBUG_VFS_MKDIR < cycle )
1280printk("\n[%s] thread[%x,%x] enter / root <%s> / path <%s> / cycle %d\n",
1281__FUNCTION__, process->pid, this->trdid, root_name, path, cycle );
1282#endif
1283
1284    // build extended pointer on lock protecting Inode Tree (in VFS root inode)
1285    vfs_root_xp  = process->vfs_root_xp;
1286    vfs_root_ptr = GET_PTR( vfs_root_xp );
1287    vfs_root_cxy = GET_CXY( vfs_root_xp );
1288    lock_xp      = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock );
1289
1290    // take the lock protecting Inode Tree in write mode
1291    remote_rwlock_wr_acquire( lock_xp );
1292
1293    // 1. get pointers on parent inode
1294    error = vfs_lookup( root_xp,
1295                        path,
1296                        VFS_LOOKUP_DIR | VFS_LOOKUP_PARENT,
1297                        &parent_xp,
1298                        last_name );
1299    if( error )
1300    {
1301        remote_rwlock_wr_release( lock_xp );
1302        printk("\n[ERROR] in %s : cannot get parent inode for <%s>\n",
1303        __FUNCTION__, path );
1304        return -1;
1305    }
1306
1307    // get parent inode cluster and local pointer
1308    parent_cxy = GET_CXY( parent_xp );
1309    parent_ptr = GET_PTR( parent_xp );
1310
1311#if( DEBUG_VFS_MKDIR & 1 )
1312if( DEBUG_VFS_MKDIR < cycle )
1313printk("\n[%s] thread[%x,%x] get parent inode (%x,%x) for <%s>\n",
1314__FUNCTION__, process->pid, this->trdid, parent_cxy, parent_ptr, path );
1315#endif
1316
1317    // get parent inode context, and FS type
1318    parent_ctx_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->ctx ) );
1319    parent_fs_type = hal_remote_l32( XPTR( parent_cxy , &parent_ctx_ptr->type ) );
1320
1321    // 2. create one new dentry in parent cluster
1322    if( parent_cxy == local_cxy ) 
1323    {
1324        error = vfs_dentry_create( parent_fs_type,
1325                                   last_name,
1326                                   &dentry_xp );
1327    }
1328    else
1329    {
1330        rpc_vfs_dentry_create_client( parent_cxy,
1331                                      parent_fs_type,
1332                                      last_name,
1333                                      &dentry_xp,
1334                                      &error );
1335    }
1336
1337    if( error )
1338    {
1339        remote_rwlock_wr_release( lock_xp );
1340        printk("\n[ERROR] in %s : cannot create new dentry in cluster %x for <%s>\n",
1341        __FUNCTION__, parent_cxy, path );
1342        return -1;
1343    }
1344
1345    // get local pointer on dentry
1346    dentry_ptr = GET_PTR( dentry_xp );
1347
1348#if( DEBUG_VFS_MKDIR & 1 )
1349if( DEBUG_VFS_MKDIR < cycle )
1350printk("\n[%s] thread[%x,%x] created new dentry (%x,%x) for <%s>\n",
1351__FUNCTION__, process->pid, this->trdid, parent_cxy, dentry_ptr, path );
1352#endif
1353
1354    // 3. create new directory inode
1355    // TODO : define attr / uid / gid
1356    uint32_t attr = 0;
1357    uint32_t uid  = 0;
1358    uint32_t gid  = 0;
1359
1360    // select a target cluster for new inode
1361    inode_cxy = cluster_random_select();
1362   
1363    if( inode_cxy == local_cxy )      // target cluster is local
1364    {
1365        error = vfs_inode_create( parent_fs_type,
1366                                  attr,
1367                                  rights,
1368                                  uid,
1369                                  gid,
1370                                  &inode_xp );
1371    }
1372    else                              // target cluster is remote
1373    {
1374        rpc_vfs_inode_create_client( inode_cxy,
1375                                     parent_fs_type,
1376                                     attr,
1377                                     rights,
1378                                     uid,
1379                                     gid,
1380                                     &inode_xp,
1381                                     &error );
1382    }
1383                                     
1384    if( error )
1385    {
1386        remote_rwlock_wr_release( lock_xp );
1387        printk("\n[ERROR] in %s : cannot create new inode in cluster %x for <%s>\n",
1388               __FUNCTION__ , inode_cxy , path );
1389        if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry_ptr );
1390        else rpc_vfs_dentry_destroy_client( parent_cxy , dentry_ptr );
1391        return -1;
1392    }
1393
1394    // get new inode local pointer
1395    inode_ptr = GET_PTR( inode_xp );
1396
1397    // update inode "type" field
1398    hal_remote_s32( XPTR( inode_cxy , &inode_ptr->type ) , INODE_TYPE_DIR ); 
1399   
1400#if(DEBUG_VFS_MKDIR & 1)
1401if( DEBUG_VFS_MKDIR < cycle )
1402printk("\n[%s] thread[%x,%x] created new inode (%x,%x) for <%s>\n",
1403__FUNCTION__ , process->pid, this->trdid, inode_cxy, inode_ptr, path );
1404#endif
1405
1406    // 4. register dentry in new inode list of parents
1407    parents_root_xp  = XPTR( inode_cxy  , &inode_ptr->parents );
1408    parents_entry_xp = XPTR( parent_cxy , &dentry_ptr->parents );
1409    xlist_add_first( parents_root_xp , parents_entry_xp );
1410    hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->links ) , 1 );
1411
1412    // 5. register dentry in parent inode
1413    children_xhtab_xp = XPTR( parent_cxy , &parent_ptr->children );
1414    children_entry_xp = XPTR( parent_cxy , &dentry_ptr->children );
1415    xhtab_insert( children_xhtab_xp , last_name , children_entry_xp );
1416
1417    // 6. update "parent" and "child_xp" fields in dentry
1418    hal_remote_s64( XPTR( parent_cxy , &dentry_ptr->child_xp ) , inode_xp );
1419    hal_remote_spt( XPTR( parent_cxy , &dentry_ptr->parent ) , parent_ptr );
1420
1421#if(DEBUG_VFS_MKDIR & 1)
1422if( DEBUG_VFS_MKDIR < cycle )
1423printk("\n[%s] thread[%x,%x] updated Inode Tree for <%s>\n",
1424__FUNCTION__, process->pid, this->trdid, path );
1425#endif
1426
1427    // 7. create the two special dentries <.> and <..> in new directory
1428    // both the new directory mapper, and the Inode Tree are updated
1429    error = vfs_add_special_dentries( inode_xp,
1430                                      parent_xp );
1431
1432    if( error )
1433    {
1434        remote_rwlock_wr_release( lock_xp );
1435        printk("\n[ERROR] in %s : cannot create new inode in cluster %x for <%s>\n",
1436               __FUNCTION__ , inode_cxy , path );
1437        if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry_ptr );
1438        else rpc_vfs_dentry_destroy_client( parent_cxy , dentry_ptr );
1439        return -1;
1440    }
1441
1442    // release the lock protecting Inode Tree
1443    remote_rwlock_wr_release( lock_xp );
1444
1445    // 8. update parent directory mapper
1446    //    and synchronize the parent directory on IOC device
1447    if (parent_cxy == local_cxy)
1448    {
1449        error = vfs_fs_add_dentry( parent_ptr,
1450                                   dentry_ptr );
1451    }
1452    else
1453    {
1454        rpc_vfs_fs_add_dentry_client( parent_cxy,
1455                                      parent_ptr,
1456                                      dentry_ptr,
1457                                      &error );
1458    }
1459
1460    if( error )
1461    {
1462        printk("\n[ERROR] in %s : cannot update parent directory for <%s>\n",
1463        __FUNCTION__, path );
1464        return -1;
1465    }
1466
1467#if(DEBUG_VFS_MKDIR & 1)
1468if( DEBUG_VFS_MKDIR < cycle )
1469printk("\n[%s] thread[%x,%x] updated parent dir (mapper and IOC) for <%s>\n",
1470__FUNCTION__, process->pid, this->trdid, path );
1471#endif
1472
1473    return 0;
1474
1475}  // end vfs_mkdir()
1476
1477///////////////////////////////////////
1478error_t vfs_link( xptr_t   old_root_xp,
1479                  char   * old_path,
1480                  xptr_t   new_root_xp,
1481                  char   * new_path )
1482{
1483    error_t        error;
1484    xptr_t         vfs_root_xp;        // extended pointer on VFS root inode
1485    vfs_inode_t  * vfs_root_ptr;       // local pointer on VFS root inode
1486    cxy_t          vfs_root_cxy;       // VFS root inode cluster identifier
1487    xptr_t         lock_xp;            // extended pointer on lock protecting Inode Tree
1488    xptr_t         inode_xp;           // extended pointer on target inode
1489    vfs_inode_t  * inode_ptr;          // local pointer on target inode
1490    cxy_t          inode_cxy;          // target inode cluster identifier
1491    uint32_t       inode_type;         // target inode type
1492    vfs_ctx_t    * inode_ctx_ptr;      // local pointer on target inode context
1493    uint32_t       inode_fs_type;      // target inode file system type
1494    xptr_t         dentry_xp;          // extended pointer on new dentry
1495    vfs_dentry_t * dentry_ptr;         // target dentry local pointer
1496    xptr_t         new_parent_xp;      // extended pointer on new parent inode
1497    vfs_inode_t  * new_parent_ptr;     // local pointer on new parent inode 
1498    cxy_t          new_parent_cxy;     // new parent inode cluster identifier
1499
1500    xptr_t         parents_root_xp;    // extended pointer on parents field in inode (root)
1501    xptr_t         parents_entry_xp;   // extended pointer on parents field in dentry
1502    xptr_t         children_xhtab_xp;  // extended pointer on children field in inode (root)
1503    xptr_t         children_entry_xp;  // extended pointer on children field in dentry
1504
1505    char           new_name[CONFIG_VFS_MAX_NAME_LENGTH];
1506
1507    thread_t  * this    = CURRENT_THREAD;
1508    process_t * process = this->process;
1509
1510#if DEBUG_VFS_LINK
1511char old_root_name[CONFIG_VFS_MAX_NAME_LENGTH];
1512char new_root_name[CONFIG_VFS_MAX_NAME_LENGTH];
1513vfs_inode_get_name( old_root_xp , old_root_name );
1514vfs_inode_get_name( new_root_xp , new_root_name );
1515uint32_t   cycle = (uint32_t)hal_get_cycles();
1516if( DEBUG_VFS_LINK < cycle )
1517printk("\n[%s] thread[%x,%x] enter / old_root <%s> / old_path <%s> / "
1518"new_root <%s> / new_path <%s> / cycle %d\n",
1519__FUNCTION__, process->pid, this->trdid,
1520old_root_name, old_path, new_root_name, new_path, cycle );
1521#endif
1522
1523    // build extended pointer on lock protecting Inode Tree (in VFS root inode)
1524    vfs_root_xp  = process->vfs_root_xp;
1525    vfs_root_ptr = GET_PTR( vfs_root_xp );
1526    vfs_root_cxy = GET_CXY( vfs_root_xp );
1527    lock_xp      = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock );
1528
1529    // take the lock protecting Inode Tree in write mode
1530    remote_rwlock_wr_acquire( lock_xp );
1531
1532    // get extended pointer on target inode
1533    error = vfs_lookup( old_root_xp,
1534                        old_path,
1535                        0,
1536                        &inode_xp,
1537                        NULL );
1538    if( error )
1539    {
1540        remote_rwlock_wr_release( lock_xp );
1541        printk("\n[ERROR] in %s : cannot get target inode for <%s>\n",
1542        __FUNCTION__, old_path );
1543        return -1;
1544    }
1545
1546#if( DEBUG_VFS_LINK & 1 )
1547if( DEBUG_VFS_LINK < cycle )
1548printk("\n[%s] thread[%x,%x] get child inode (%x,%x) for <%s>\n",
1549__FUNCTION__, process->pid, this->trdid,
1550GET_CXY(inode_xp), GET_PTR(inode_xp), old_path, cycle );
1551#endif
1552
1553    // get extended pointer on parent inode in new path
1554    error = vfs_lookup( new_root_xp,
1555                        new_path,
1556                        VFS_LOOKUP_PARENT,
1557                        &new_parent_xp,
1558                        new_name );
1559    if( error )
1560    {
1561        remote_rwlock_wr_release( lock_xp );
1562        printk("\n[ERROR] in %s : cannot get parent inode for <%s>\n",
1563        __FUNCTION__, new_path );
1564        return -1;
1565    }
1566
1567#if( DEBUG_VFS_LINK & 1 )
1568if( DEBUG_VFS_LINK < cycle )
1569printk("\n[%s] thread[%x,%x] get parent inode (%x,%x) for <%s>\n",
1570__FUNCTION__, process->pid, this->trdid,
1571GET_CXY(new_parent_xp), GET_PTR(new_parent_xp), new_path );
1572#endif
1573
1574    // get target inode cluster and local pointer
1575    inode_cxy = GET_CXY( inode_xp );
1576    inode_ptr = GET_PTR( inode_xp );
1577
1578    // get target inode type, context, and FS type
1579    inode_type        = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) );
1580    inode_ctx_ptr     = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->ctx ) );
1581    inode_fs_type     = hal_remote_l32( XPTR( inode_cxy , &inode_ctx_ptr->type ) );
1582
1583    // get new parent inode cluster an local pointer
1584    new_parent_ptr = GET_PTR( new_parent_xp );
1585    new_parent_cxy = GET_CXY( new_parent_xp );
1586
1587    ///////////////////////////////////////////////////////////////////////
1588    if( (inode_type == INODE_TYPE_FILE) || (inode_type == INODE_TYPE_DIR) )
1589    {
1590        // 1. create one new dentry
1591        if( new_parent_cxy == local_cxy ) 
1592        {
1593            error = vfs_dentry_create( inode_fs_type,
1594                                       new_name,
1595                                       &dentry_xp );
1596        }
1597        else
1598        {
1599            rpc_vfs_dentry_create_client( new_parent_cxy,
1600                                          inode_fs_type,
1601                                          new_name,
1602                                          &dentry_xp,
1603                                          &error );
1604        }
1605
1606        if( error )
1607        {
1608            remote_rwlock_wr_release( lock_xp );
1609            printk("\n[ERROR] in %s : cannot create new dentry for <%s>\n",
1610            __FUNCTION__, new_path );
1611            return -1;
1612        }
1613
1614        // get local pointer on dentry
1615        dentry_ptr = GET_PTR( dentry_xp );
1616
1617        // 2. register dentry in target inode
1618        parents_root_xp  = XPTR( inode_cxy      , &inode_ptr->parents );
1619        parents_entry_xp = XPTR( new_parent_cxy , &dentry_ptr->parents );
1620        xlist_add_first( parents_root_xp , parents_entry_xp );
1621        hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->links ) , 1 );
1622
1623        // 3. register dentry in parent inode
1624        children_xhtab_xp = XPTR( new_parent_cxy , &new_parent_ptr->children );
1625        children_entry_xp = XPTR( new_parent_cxy , &dentry_ptr->children );
1626        xhtab_insert( children_xhtab_xp , new_name , children_entry_xp );
1627
1628        // 4. update "parent" and "child_xp" fields in dentry
1629        hal_remote_s64( XPTR( new_parent_cxy , &dentry_ptr->child_xp ) , inode_xp );
1630        hal_remote_spt( XPTR( new_parent_cxy , &dentry_ptr->parent ) , new_parent_ptr );
1631
1632#if(DEBUG_VFS_LINK & 1)
1633if( DEBUG_VFS_LINK < cycle )
1634printk("\n[%s] thread[%x,%x] updated Inode Tree / old <%s> / new <%s>\n",
1635__FUNCTION__, process->pid, this->trdid, old_path, new_path );
1636vfs_display( new_parent_xp ); 
1637#endif
1638
1639        // release the lock protecting Inode Tree
1640        remote_rwlock_wr_release( lock_xp );
1641
1642        // 5. update new parent directory mapper in Inode Tree
1643        //    and synchronize the parent directory on IOC device
1644        if (new_parent_cxy == local_cxy)
1645        {
1646            error = vfs_fs_add_dentry( new_parent_ptr,
1647                                       dentry_ptr );
1648        }
1649        else
1650        {
1651            rpc_vfs_fs_add_dentry_client( new_parent_cxy,
1652                                          new_parent_ptr,
1653                                          dentry_ptr,
1654                                          &error );
1655        }
1656        if( error )
1657        {
1658            printk("\n[ERROR] in %s : cannot update new parent directory for <%s>\n",
1659            __FUNCTION__, new_path );
1660            return -1;
1661        }
1662
1663#if(DEBUG_VFS_LINK & 1)
1664if( DEBUG_VFS_LINK < cycle )
1665printk("\n[%s] thread[%x,%x] updated new parent dir (mapper and IOC) / old <%s> / new <%s>\n",
1666__FUNCTION__, process->pid, this->trdid, old_path, new_path );
1667#endif
1668        return 0;
1669    }
1670    else
1671    {
1672        // release the lock protecting Inode Tree
1673        remote_rwlock_wr_release( lock_xp );
1674
1675        printk("\n[ERROR] in %s : unsupported inode type %s\n",
1676        __FUNCTION__ , vfs_inode_type_str( inode_type ) );
1677        return -1;
1678    }
1679
1680}  // end vfs_link()
1681
1682/////////////////////////////////////
1683error_t vfs_unlink( xptr_t   root_xp,
1684                    char   * path )
1685{
1686    error_t           error;
1687    xptr_t            vfs_root_xp;        // extended pointer on VFS root inode
1688    vfs_inode_t     * vfs_root_ptr;       // local_pointer on VFS root inode
1689    cxy_t             vfs_root_cxy;       // VFS root inode cluster identifier
1690    xptr_t            lock_xp;            // extended pointer on lock protecting Inode Tree
1691    xptr_t            parent_xp;          // extended pointer on target inode
1692    cxy_t             parent_cxy;         // target inode cluster identifier       
1693    vfs_inode_t     * parent_ptr;         // target inode local pointer
1694    xptr_t            inode_xp;           // extended pointer on target inode
1695    cxy_t             inode_cxy;          // target inode cluster identifier       
1696    vfs_inode_t     * inode_ptr;          // target inode local pointer
1697    uint32_t          inode_links;        // target inode links count
1698    vfs_inode_type_t  inode_type;         // target inode type
1699    uint32_t          inode_children;     // target inode number of children
1700    xptr_t            dentry_xp;          // extended pointer on dentry to unlink
1701    vfs_dentry_t    * dentry_ptr;         // local pointer on dentry to unlink
1702    vfs_ctx_t       * ctx_ptr;            // local pointer on FS context
1703    vfs_fs_type_t     fs_type;            // File system type
1704
1705    char              name[CONFIG_VFS_MAX_NAME_LENGTH];  // name of link to remove
1706
1707    thread_t  * this    = CURRENT_THREAD;
1708    process_t * process = this->process;
1709
1710#if DEBUG_VFS_UNLINK
1711uint32_t   cycle = (uint32_t)hal_get_cycles();
1712char root_name[CONFIG_VFS_MAX_NAME_LENGTH];
1713vfs_inode_get_name( root_xp , root_name );
1714if( DEBUG_VFS_UNLINK < cycle )
1715printk("\n[%s] thread[%x,%x] : enter for root <%s> / path <%s> / cycle %d\n",
1716__FUNCTION__, process->pid, this->trdid, root_name, path, cycle );
1717#endif
1718
1719    // build extended pointer on lock protecting Inode Tree (in VFS root inode)
1720    vfs_root_xp  = process->vfs_root_xp;
1721    vfs_root_ptr = GET_PTR( vfs_root_xp );
1722    vfs_root_cxy = GET_CXY( vfs_root_xp );
1723    lock_xp      = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock );
1724
1725    // take the lock protecting Inode Tree
1726    remote_rwlock_wr_acquire( lock_xp );
1727
1728    // get extended pointer on parent inode
1729    error = vfs_lookup( root_xp,
1730                        path,
1731                        VFS_LOOKUP_PARENT,
1732                        &parent_xp,
1733                        name );
1734    if( error ) 
1735    {
1736        remote_rwlock_wr_release( lock_xp );
1737        printk("\n[ERROR] in %s : cannot get parent inode for <%s> in <%s>\n",
1738        __FUNCTION__, name, path );
1739        return -1;
1740    }
1741
1742    // get parent inode cluster and local pointer
1743    parent_cxy = GET_CXY( parent_xp );
1744    parent_ptr = GET_PTR( parent_xp );
1745 
1746#if( DEBUG_VFS_UNLINK & 1 )
1747char parent_name[CONFIG_VFS_MAX_NAME_LENGTH];
1748vfs_inode_get_name( parent_xp , parent_name );
1749if( DEBUG_VFS_UNLINK < cycle )
1750printk("\n[%s] thread[%x,%x] : parent inode <%s> is (%x,%x)\n",
1751__FUNCTION__, process->pid, this->trdid, parent_name, parent_cxy, parent_ptr );
1752#endif
1753
1754    // build extended pointer on parent inode "children" xhtab
1755    xptr_t children_xp = XPTR( parent_cxy , &parent_ptr->children );
1756
1757    // try to get extended pointer on dentry from Inode Tree
1758    dentry_xp = xhtab_lookup( children_xp , name );
1759   
1760    // when dentry not found in Inode Tree, try to get it from inode tree
1761
1762    if( dentry_xp == XPTR_NULL )           // miss target dentry in Inode Tree
1763    {
1764
1765#if( DEBUG_VFS_UNLINK & 1 )
1766if( DEBUG_VFS_UNLINK < cycle )
1767printk("\n[%s] thread[%x,%x] : inode <%s> not found => scan parent mapper\n",
1768__FUNCTION__, process->pid, this->trdid, name );
1769#endif
1770        // get parent inode FS type
1771        ctx_ptr    = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->ctx ) );
1772        fs_type    = hal_remote_l32( XPTR( parent_cxy , &ctx_ptr->type ) );
1773
1774        // select a cluster for new inode
1775        inode_cxy = cluster_random_select();
1776
1777        // speculatively insert a new child dentry/inode couple in inode tree
1778        error = vfs_add_child_in_parent( inode_cxy,
1779                                         fs_type, 
1780                                         parent_xp, 
1781                                         name, 
1782                                         &dentry_xp,
1783                                         &inode_xp );
1784        if( error )
1785        {
1786            printk("\n[ERROR] in %s : cannot create inode <%s> in path <%s>\n",
1787            __FUNCTION__ , name, path );
1788
1789            vfs_remove_child_from_parent( dentry_xp );
1790            return -1;
1791        }
1792
1793        // get local pointers on new dentry and new inode descriptors
1794        inode_ptr  = GET_PTR( inode_xp );
1795        dentry_ptr = GET_PTR( dentry_xp );
1796
1797        // scan parent mapper to find the missing dentry, and complete
1798        // initialisation of new dentry and new inode descriptors In Inode Tree
1799        if( parent_cxy == local_cxy )
1800        {
1801            error = vfs_fs_new_dentry( parent_ptr,
1802                                       name,
1803                                       inode_xp );
1804        }
1805        else
1806        {
1807            rpc_vfs_fs_new_dentry_client( parent_cxy,
1808                                          parent_ptr,
1809                                          name,
1810                                          inode_xp,
1811                                          &error );
1812        }
1813
1814        if ( error )   // dentry not found in parent mapper
1815        {
1816            printk("\n[ERROR] in %s : cannot get dentry <%s> in path <%s>\n",
1817            __FUNCTION__ , name, path );
1818            return -1;
1819        }
1820
1821#if (DEBUG_VFS_UNLINK & 1)
1822if( DEBUG_VFS_UNLINK < cycle )
1823printk("\n[%s] thread[%x,%x] : created missing inode & dentry <%s> in cluster %x\n",
1824__FUNCTION__, process->pid, this->trdid, name, inode_cxy );
1825#endif
1826
1827    }
1828    else                                  // found target dentry in Inode Tree
1829    {
1830        dentry_ptr = GET_PTR( dentry_xp );
1831       
1832        // get pointer on target inode from dentry
1833        inode_xp  = hal_remote_l64( XPTR( parent_cxy , &dentry_ptr->child_xp ) );
1834        inode_cxy = GET_CXY( inode_xp );
1835        inode_ptr = GET_PTR( inode_xp );
1836    }
1837
1838    // At this point the Inode Tree contains the target dentry and child inode
1839    // we can safely remove this dentry from both the parent mapper, and the Inode Tree.
1840
1841#if( DEBUG_VFS_UNLINK & 1 )
1842if( DEBUG_VFS_UNLINK < cycle )
1843printk("\n[%s] thread[%x,%x] : dentry (%x,%x) / inode (%x,%x)\n",
1844__FUNCTION__, process->pid, this->trdid, parent_cxy, dentry_ptr, inode_cxy, inode_ptr );
1845#endif
1846
1847    // get target inode "type" and "links"
1848    inode_type   = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) );
1849    inode_links  = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->links ) );
1850
1851    ///////////////////////////////////////////////////////////////////////
1852    if( (inode_type == INODE_TYPE_FILE) || (inode_type == INODE_TYPE_DIR) )
1853    {
1854
1855#if( DEBUG_VFS_UNLINK & 1 )
1856if( DEBUG_VFS_UNLINK < cycle )
1857printk("\n[%s] thread[%x,%x] : unlink inode <%s> / type %s / %d links\n",
1858__FUNCTION__, process->pid, this->trdid, name, vfs_inode_type_str(inode_type), inode_links );
1859#endif
1860
1861        // 1. Release clusters allocated to target inode
1862        //    and synchronize the FAT on IOC device if last link.
1863        if( inode_links == 1 ) 
1864        {
1865            // build extended pointer on target inode "children" number
1866            xptr_t inode_children_xp = XPTR( inode_cxy , &inode_ptr->children.items );
1867
1868            // get target inode number of children
1869            inode_children = hal_remote_l32( inode_children_xp );
1870
1871            // check no children
1872            if( inode_children != 0 )
1873            {
1874                remote_rwlock_wr_release( lock_xp );
1875                printk("\n[ERROR] in %s : cannot remove <%s> inode that has children\n",
1876                __FUNCTION__, path );
1877                return -1;
1878            }
1879
1880            // release clusters on IOC device
1881            error = vfs_fs_release_inode( inode_xp ); 
1882
1883            if( error )
1884            {
1885                remote_rwlock_wr_release( lock_xp );
1886                printk("\n[ERROR] in %s : cannot update FAT mapper to remove <%s> inode\n",
1887                __FUNCTION__ , path );
1888                return -1;
1889            }
1890
1891#if(DEBUG_VFS_UNLINK & 1)
1892if( DEBUG_VFS_UNLINK < cycle )
1893printk("\n[%s] thread[%x,%x] removed <%s> inode from FAT (mapper and IOC device)\n",
1894__FUNCTION__, process->pid, this->trdid, path );
1895#endif
1896        }
1897
1898        // 2. update parent directory mapper
1899        //    and synchronize the parent directory on IOC device
1900        if (parent_cxy == local_cxy)
1901        {
1902            error = vfs_fs_remove_dentry( parent_ptr,
1903                                          dentry_ptr );
1904        }
1905        else           
1906        {
1907            rpc_vfs_fs_remove_dentry_client( parent_cxy,
1908                                             parent_ptr,
1909                                             dentry_ptr,
1910                                             &error );
1911        }
1912
1913        if( error )
1914        {
1915            remote_rwlock_wr_release( lock_xp );
1916            printk("\n[ERROR] in %s : cannot update dentry on device for <%s>\n",
1917            __FUNCTION__ , path );
1918            return -1;
1919        }
1920
1921#if(DEBUG_VFS_UNLINK & 1)
1922if( DEBUG_VFS_UNLINK < cycle )
1923printk("\n[%s] thread[%x,%x] removed <%s> inode from parent dir (mapper and IOC device)\n",
1924__FUNCTION__, process->pid, this->trdid, path );
1925#endif
1926        // 3. remove dentry from Inode Tree (and associated chils inode when last link)
1927        vfs_remove_child_from_parent( dentry_xp );
1928
1929        // release the lock protecting Inode Tree
1930        remote_rwlock_wr_release( lock_xp );
1931
1932#if DEBUG_VFS_UNLINK
1933if( DEBUG_VFS_UNLINK < cycle )
1934printk("\n[%s] thread[%x,%x] exit / removed <%s> inode from Inode Tree / cycle %d\n",
1935__FUNCTION__, process->pid, this->trdid, path, cycle );
1936#endif
1937        return 0;
1938    }
1939    else
1940    {
1941        remote_rwlock_wr_release( lock_xp );
1942        printk("\n[ERROR] in %s : unsupported inode type %s\n",
1943        __FUNCTION__ , vfs_inode_type_str( inode_type ) );
1944        return -1;
1945    }
1946
1947}  // end vfs_unlink()
1948
1949////////////////////////////////////////////////
1950error_t vfs_stat( xptr_t         root_inode_xp,
1951                  char         * path,
1952                  struct stat  * st )
1953{
1954    error_t       error;
1955    xptr_t        inode_xp;           // extended pointer on target inode
1956    vfs_inode_t * inode_ptr;          // local pointer on target inode
1957    cxy_t         inode_cxy;          // target inode cluster identifier
1958    xptr_t        vfs_root_xp;        // extended pointer on VFS root inode
1959    vfs_inode_t * vfs_root_ptr;       // local_pointer on VFS root inode
1960    cxy_t         vfs_root_cxy;       // VFS root inode cluster identifier
1961    xptr_t        lock_xp;            // extended pointer on lock protecting Inode Tree
1962
1963    thread_t  * this    = CURRENT_THREAD;
1964    process_t * process = this->process;
1965
1966    // build extended pointer on lock protecting Inode Tree (in VFS root inode)
1967    vfs_root_xp  = process->vfs_root_xp;
1968    vfs_root_ptr = GET_PTR( vfs_root_xp );
1969    vfs_root_cxy = GET_CXY( vfs_root_xp );
1970    lock_xp      = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock );
1971
1972    // get the lock protecting Inode Tree in read mode
1973    remote_rwlock_rd_acquire( lock_xp );
1974
1975    // get extended pointer on target inode
1976    error = vfs_lookup( root_inode_xp,
1977                        path,
1978                        0,
1979                        &inode_xp,
1980                        NULL );
1981
1982    // release the lock protecting Inode Tree
1983    remote_rwlock_rd_release( lock_xp );
1984
1985    if( error )
1986    {
1987        printk("\n[ERROR] in %s : cannot found inode <%s>\n",
1988        __FUNCTION__ , path );
1989        return -1;
1990    }
1991
1992    // get cluster and local pointer on inode descriptor
1993    inode_ptr = GET_PTR( inode_xp );
1994    inode_cxy = GET_CXY( inode_xp );
1995
1996    // get relevant infos from inode descriptor
1997    uint32_t inum   = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->inum   ) );
1998    uint32_t size   = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->size   ) );
1999    uint32_t uid    = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->uid    ) );
2000    uint32_t gid    = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->gid    ) );
2001    uint32_t type   = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type   ) );
2002    uint32_t rights = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->rights ) );
2003
2004    // set stat structure fields
2005    st->st_ino  = inum;
2006    st->st_gid  = gid;
2007    st->st_uid  = uid;
2008    st->st_size = size;
2009    st->st_mode = (type << 16) | rights;
2010
2011#if DEBUG_VFS_STAT
2012uint32_t cycle  = (uint32_t)hal_get_cycles();
2013if( DEBUG_VFS_STAT < cycle )
2014printk("\n[%s] thread[%x,%x] set stat %x for inode %x in cluster %x / cycle %d\n"
2015       " %s / inum %d / size %d\n",
2016__FUNCTION__, process->pid, this->trdid, st, inode_ptr, inode_cxy, cycle,
2017vfs_inode_type_str( type ), inum, size );
2018#endif
2019
2020    return 0;
2021
2022}  // end vfs_stat()
2023
2024////////////////////////////////////
2025error_t vfs_chdir( xptr_t   root_xp,
2026                   char   * path )
2027{
2028    error_t           error;
2029    xptr_t            inode_xp;           // extended pointer on target inode
2030    cxy_t             inode_cxy;          // target inode cluster identifier       
2031    vfs_inode_t     * inode_ptr;          // target inode local pointer
2032    vfs_inode_type_t  inode_type;         // target inode type
2033    xptr_t            vfs_root_xp;        // extended pointer on VFS root inode
2034    vfs_inode_t     * vfs_root_ptr;       // local_pointer on VFS root inode
2035    cxy_t             vfs_root_cxy;       // VFS root inode cluster identifier
2036    xptr_t            main_lock_xp;       // extended pointer on lock protecting Inode Tree
2037    xptr_t            ref_xp;             // extended pointer on reference process
2038    process_t       * ref_ptr;            // local pointer on reference process
2039    cxy_t             ref_cxy;            // reference process cluster
2040    xptr_t            cwd_lock_xp;        // extended pointer on lock protecting CWD change
2041    xptr_t            cwd_xp_xp;          // extended pointer on cwd_xp in reference process
2042
2043    thread_t  * this    = CURRENT_THREAD;
2044    process_t * process = this->process;
2045
2046#if DEBUG_VFS_CHDIR
2047uint32_t cycle = (uint32_t)hal_get_cycles();
2048if( DEBUG_VFS_CHDIR < cycle )
2049printk("\n[%s] thread[%x,%x] enter for path <%s> / cycle %d\n",
2050__FUNCTION__, process->pid, this->trdid, path, cycle );
2051#endif
2052
2053    // build extended pointer on lock protecting Inode Tree (in VFS root inode)
2054    vfs_root_xp  = process->vfs_root_xp;
2055    vfs_root_ptr = GET_PTR( vfs_root_xp );
2056    vfs_root_cxy = GET_CXY( vfs_root_xp );
2057    main_lock_xp = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock );
2058
2059    // take lock protecting Inode Tree in read mode
2060    remote_rwlock_rd_acquire( main_lock_xp );
2061
2062    // get extended pointer on target inode
2063    error = vfs_lookup( root_xp,
2064                        path,
2065                        VFS_LOOKUP_DIR,
2066                        &inode_xp,
2067                        NULL );
2068
2069    // release lock protecting Inode Tree in read mode
2070    remote_rwlock_rd_release( main_lock_xp );
2071
2072    if( error ) 
2073    {
2074        printk("\n[ERROR] in %s : <%s> not found\n",
2075        __FUNCTION__, path );
2076        return -1;
2077    }
2078
2079    // get inode type from remote file
2080    inode_cxy = GET_CXY( inode_xp );
2081    inode_ptr = GET_PTR( inode_xp );
2082    inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) );
2083
2084    if( inode_type != INODE_TYPE_DIR )
2085    {
2086        printk("\n[ERROR] in %s : <%s> is not a directory\n",
2087        __FUNCTION__, path );
2088        return -1;
2089    }
2090
2091    // build extended pointer on cwd_lock and cwd_xp
2092    ref_xp       = process->ref_xp;
2093    ref_ptr      = GET_PTR( ref_xp );
2094    ref_cxy      = GET_CXY( ref_xp );
2095    cwd_lock_xp  = XPTR( ref_cxy , &ref_ptr->cwd_lock );
2096    cwd_xp_xp    = XPTR( ref_cxy , &ref_ptr->cwd_xp );
2097
2098    // take lock protecting CWD changes
2099    remote_busylock_acquire( cwd_lock_xp );
2100
2101    // update cwd_xp field in reference process descriptor
2102    hal_remote_s64( cwd_xp_xp , inode_xp );
2103
2104    // release lock protecting CWD changes
2105    remote_busylock_release( cwd_lock_xp );
2106
2107#if DEBUG_VFS_CHDIR
2108cycle = (uint32_t)hal_get_cycles();
2109if( DEBUG_VFS_CHDIR < cycle )
2110printk("\n[%s] thread[%x,%x] exit : inode (%x,%x) / &cwd_xp (%x,%x) / cycle %d\n",
2111__FUNCTION__, process->pid, this->trdid, inode_cxy, inode_ptr, 
2112GET_CXY(cwd_xp_xp), GET_PTR(cwd_xp_xp), cycle );
2113#endif
2114
2115    return 0;
2116
2117}  // end vfs_chdir()
2118
2119///////////////////////////////////
2120error_t vfs_chmod( xptr_t   cwd_xp,
2121                   char   * path,
2122                   uint32_t rights )
2123{
2124    error_t           error;
2125    xptr_t            inode_xp;     // extended pointer on target inode
2126    cxy_t             inode_cxy;    // inode cluster identifier       
2127    vfs_inode_t     * inode_ptr;    // inode local pointer
2128
2129// check lookup working mode
2130assert( (rights == 0), "access rights non implemented yet" );
2131 
2132    // get extended pointer on target inode
2133    error = vfs_lookup( cwd_xp,
2134                        path,
2135                        0,
2136                        &inode_xp,
2137                        NULL );
2138
2139    if( error ) return error;
2140
2141    // get inode cluster and local pointer
2142    inode_cxy = GET_CXY( inode_xp );
2143    inode_ptr = GET_PTR( inode_xp );
2144   
2145    // get inode type from remote inode
2146    // inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) );
2147
2148    // TODO finalize implementation
2149
2150assert( false , "not implemented" );
2151
2152    // set inode rights in remote inode
2153    hal_remote_s32( XPTR( inode_cxy , &inode_ptr->rights ) , rights );
2154
2155    return 0;
2156}
2157
2158///////////////////////////////////
2159error_t vfs_mkfifo( xptr_t   cwd_xp,
2160                    char   * path,
2161                    uint32_t rights )
2162{
2163    assert( false , "not implemented %l %x %x", cwd_xp, path, rights );
2164    return 0;
2165}
2166
2167
2168
2169//////////////////////////////////////////////////////////////////////////////////////////
2170//       Distributed Inode Tree access related functions
2171//////////////////////////////////////////////////////////////////////////////////////////
2172
2173//////////////////////////////////////////////////////////////////////////
2174// This static function is called by the vfs_display() function.
2175// that is supposed to take the TXT0 lock.
2176//////////////////////////////////////////////////////////////////////////
2177static void vfs_recursive_display( xptr_t   inode_xp,
2178                                   xptr_t   name_xp,
2179                                   uint32_t indent )
2180{
2181    cxy_t              inode_cxy;
2182    vfs_inode_t      * inode_ptr;
2183    vfs_inode_type_t   inode_type;
2184    uint32_t           inode_size;
2185    uint32_t           inode_attr;
2186    uint32_t           inode_dirty;
2187    void             * inode_extd;
2188
2189    xptr_t             children_xp;    // extended pointer on children xhtab
2190
2191    xptr_t             child_dentry_xp;
2192    cxy_t              child_dentry_cxy;
2193    vfs_dentry_t     * child_dentry_ptr;
2194    xptr_t             child_inode_xp;
2195    xptr_t             child_dentry_name_xp;
2196    mapper_t         * mapper_ptr;
2197
2198    char               name[CONFIG_VFS_MAX_NAME_LENGTH];
2199
2200    char *             indent_str[] = { "",                                  // level 0
2201                                        "  ",                                // level 1
2202                                        "    ",                              // level 2
2203                                        "      ",                            // level 3
2204                                        "        ",                          // level 4
2205                                        "          ",                        // level 5
2206                                        "            ",                      // level 6
2207                                        "              ",                    // level 7
2208                                        "                ",                  // level 8
2209                                        "                  ",                // level 9
2210                                        "                    ",              // level 10
2211                                        "                      ",            // level 11
2212                                        "                        ",          // level 12
2213                                        "                          ",        // level 13
2214                                        "                            ",      // level 14
2215                                        "                              " };  // level 15
2216
2217assert( (inode_xp != XPTR_NULL) , "inode_xp cannot be NULL" );
2218assert( (name_xp  != XPTR_NULL) , "name_xp cannot be NULL" );
2219assert( (indent < 16)           , "depth cannot be larger than 15" );
2220   
2221    // get current inode cluster and local pointer
2222    inode_cxy = GET_CXY( inode_xp );
2223    inode_ptr = GET_PTR( inode_xp );
2224
2225    // get inode type, size, attr, mapper, and inum
2226    inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type   ) );
2227    inode_size = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->size   ) );
2228    inode_attr = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->attr   ) );
2229    inode_extd = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->extend ) );
2230    mapper_ptr = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->mapper ) );
2231
2232    // make a local copy of node name
2233    hal_remote_strcpy( XPTR( local_cxy , name ) , name_xp );
2234
2235    // compute dirty
2236    inode_dirty = ((inode_attr & INODE_ATTR_DIRTY) != 0);
2237
2238    // display inode
2239    nolock_printk("%s<%s> : %s / extd %x / %d bytes / dirty %d / cxy %x / inode %x / mapper %x\n",
2240    indent_str[indent], name, vfs_inode_type_str( inode_type ), (uint32_t)inode_extd,
2241    inode_size, inode_dirty, inode_cxy, inode_ptr, mapper_ptr );
2242
2243    // scan directory entries when current inode is a directory
2244    // don't scan the the "." and ".." directories to break loops
2245    if( (inode_type == INODE_TYPE_DIR) && 
2246        (strcmp( name , "." ) != 0)    &&
2247        (strcmp( name , ".." ) != 0) )
2248    {
2249        // get extended pointer on directory entries xhtab
2250        children_xp =  XPTR( inode_cxy , &inode_ptr->children );
2251
2252        // get xhtab lock
2253        xhtab_lock( children_xp );
2254
2255        // get first dentry from xhtab
2256        child_dentry_xp = xhtab_get_first( children_xp );
2257
2258        while( child_dentry_xp != XPTR_NULL )
2259        {
2260            // get dentry cluster and local pointer
2261            child_dentry_cxy = GET_CXY( child_dentry_xp );
2262            child_dentry_ptr = GET_PTR( child_dentry_xp );
2263
2264            // get extended pointer on child inode
2265            child_inode_xp = hal_remote_l64( XPTR( child_dentry_cxy,
2266                                                   &child_dentry_ptr->child_xp ) );
2267
2268            // get extended pointer on dentry name
2269            child_dentry_name_xp = XPTR( child_dentry_cxy , &child_dentry_ptr->name );
2270
2271            // recursive call on inode display
2272            vfs_recursive_display( child_inode_xp,
2273                                   child_dentry_name_xp,
2274                                   indent+1 );
2275
2276            // get next dentry
2277            child_dentry_xp = xhtab_get_next( children_xp );
2278        }
2279
2280        // release xhtab lock
2281        xhtab_unlock( children_xp );
2282    }
2283}  // end vfs_recursive_display()
2284
2285///////////////////////////////////
2286void vfs_display( xptr_t inode_xp )
2287{
2288    xptr_t         name_xp;
2289    xptr_t         dentry_xp; 
2290    cxy_t          dentry_cxy;
2291    vfs_dentry_t * dentry_ptr;
2292    xptr_t         parents_root_xp;   // root of parent dentries xlist
2293
2294    // get target inode cluster and local pointer
2295    cxy_t         inode_cxy = GET_CXY( inode_xp );
2296    vfs_inode_t * inode_ptr = GET_PTR( inode_xp );
2297
2298    // build extended pointer on parents dentries root
2299    parents_root_xp = XPTR( inode_cxy , &inode_ptr->parents );
2300
2301    // check VFS root     
2302    if( xlist_is_empty( parents_root_xp ) )  // inode is the VFS root
2303    {
2304        // build extended pointer on root name
2305        name_xp = XPTR( local_cxy , "/" );
2306    }
2307    else
2308    {
2309        // get first parent dentry cluster and pointers
2310        dentry_xp  = XLIST_FIRST( parents_root_xp , vfs_dentry_t , parents );
2311        dentry_cxy = GET_CXY( dentry_xp );
2312        dentry_ptr = GET_PTR( dentry_xp );
2313
2314        // get extended pointer on dentry name
2315        name_xp = XPTR( dentry_cxy , &dentry_ptr->name );
2316    }
2317
2318    // get pointers on TXT0 chdev
2319    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
2320    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
2321    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
2322
2323    // get extended pointer on remote TXT0 chdev lock
2324    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
2325
2326    // get TXT0 lock in busy waiting mode
2327    remote_busylock_acquire( lock_xp );
2328
2329    // print header
2330    nolock_printk("\n***** file system state\n\n");
2331
2332    // call recursive function
2333    vfs_recursive_display( inode_xp , name_xp , 0 );
2334
2335    // release lock
2336    remote_busylock_release( lock_xp );
2337
2338}  // end vfs_display()
2339
2340/*
2341//////////////////////////////////////////////////////////////////////////////////////////
2342// This static function is used by the vfs_lookup() function.
2343// It takes an extended pointer on a remote inode (parent directory inode),
2344// and check access_rights violation for the calling thread.
2345// It can be used by any thread running in any cluster.
2346//////////////////////////////////////////////////////////////////////////////////////////
2347// @ inode_xp    : extended pointer on inode.
2348// @ client_uid  : client thread user ID
2349// @ client_gid  : client thread group ID
2350// @ return true if access rights are violated.
2351//////////////////////////////////////////////////////////////////////////////////////////
2352static bool_t vfs_access_denied( xptr_t   inode_xp,
2353                          uint32_t client_uid,
2354                          uint32_t client_gid )
2355{
2356    // get found inode cluster and local pointer
2357    cxy_t         inode_cxy = GET_CXY( inode_xp );
2358    vfs_inode_t * inode_ptr = GET_PTR( inode_xp );
2359
2360    // get inode access mode, UID, and GID
2361    // TODO uint32_t  mode = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->mode ) );
2362    uid_t     uid  = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->uid  ) );
2363    gid_t     gid  = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->gid  ) );
2364
2365    // TODO : me must use mode
2366    if( (uid == client_uid) || (gid == client_gid) ) return false;
2367    else                                             return true;
2368}
2369*/
2370
2371//////////////////////////////////////////////////////////////////////////////////////////
2372// This static function is used by the vfs_lookup() function.
2373// It takes an extended pointer on a remote parent directory inode, a directory
2374// entry name, and and scan the XHTAB associated to the parent inode to find the
2375// searched dentry. It does NOT modify the inode tree in case of miss.
2376// It can be used by any thread running in any cluster.
2377//////////////////////////////////////////////////////////////////////////////////////////
2378// @ parent_xp   : extended pointer on parent inode in remote cluster.
2379// @ name        : dentry name
2380// @ child_xp    : [out] buffer for extended pointer on child inode.
2381// @ return true if success / return false if not found.
2382//////////////////////////////////////////////////////////////////////////////////////////
2383static bool_t vfs_get_child( xptr_t   parent_xp,
2384                             char   * name,
2385                             xptr_t * child_xp )
2386{
2387    xptr_t         xhtab_xp;    // extended pointer on hash table for children dentries
2388    xptr_t         dentry_xp;   // extended pointer on children dentry
2389    cxy_t          dentry_cxy;
2390    vfs_dentry_t * dentry_ptr;
2391
2392    // get parent inode cluster and local pointer
2393    cxy_t         parent_cxy = GET_CXY( parent_xp );
2394    vfs_inode_t * parent_ptr = GET_PTR( parent_xp );
2395
2396    // get extended pointer on hash table of children directory entries
2397    xhtab_xp = XPTR( parent_cxy , &parent_ptr->children );
2398
2399    // get pointers on matching dentry
2400    dentry_xp  = xhtab_lookup( xhtab_xp , name );
2401    dentry_cxy = GET_CXY( dentry_xp );
2402    dentry_ptr = GET_PTR( dentry_xp );
2403
2404    if( dentry_xp == XPTR_NULL ) 
2405    {
2406        return false;
2407    }
2408    else
2409    {
2410        *child_xp = (xptr_t)hal_remote_l64( XPTR( dentry_cxy , &dentry_ptr->child_xp ) );
2411        return true;
2412    }
2413
2414}  // end vfs_get_child()
2415
2416//////////////////////////////////////////////////////////////////////////////////////////
2417// This static function is used by the vfs_lookup() function.
2418// It takes the <current> pointer on a buffer containing a complete pathname, and return
2419// in the <name> buffer, allocated by the caller, a single name in the path.
2420// It return also in the <next> pointer the next character to analyse in the path.
2421// Finally it returns a <last> boolean, that is true when the returned <name> is the
2422// last name in the path. The names are supposed to be separated by one or several '/'
2423// characters, that are not written in  the <name> buffer.
2424//
2425// WARNING: the leading characters '/' in the path are skiped before analysis.
2426//          The path "/" identifies the VFS root, and is therefore anaysed as an empty
2427//          string. This empty string is dignaled by the (-1) return value. 
2428//////////////////////////////////////////////////////////////////////////////////////////
2429// @ current   : pointer on first character to analyse in buffer containing the path.
2430// @ name      : [out] pointer on buffer allocated by the caller for the returned name.
2431// @ next      : [out] pointer on next character to analyse in buffer containing the path.
2432// @ last      : [out] true if the returned name is the last (NUL character found).
2433// @ return 0 if success / return -1 if string empty (first chracter is NUL).
2434//////////////////////////////////////////////////////////////////////////////////////////
2435static error_t vfs_get_name_from_path( char     * current,
2436                                       char     * name,
2437                                       char    ** next,
2438                                       bool_t   * last )
2439{
2440    char * ptr = current;
2441
2442    // skip leading '/' characters
2443    while( *ptr == '/' ) ptr++;
2444
2445    // signal empty string
2446    if( *ptr == 0 )
2447    {
2448        *last = true;
2449        return -1;
2450    }
2451
2452    // copy all characters in name until NUL or '/'
2453    while( (*ptr != 0) && (*ptr !='/') )  *(name++) = *(ptr++);
2454
2455    // set NUL terminating character in name buffer
2456    *(name++) = 0;
2457
2458    // return last an next
2459    if( *ptr == 0 )             // last found character is NUL => last name in path
2460    {
2461        *last = true;
2462    }
2463    else                        // last found character is '/' => skip it
2464    {
2465        *last = false;
2466        *next = ptr + 1;
2467    }
2468
2469    return 0;
2470
2471}  // end vfs_get name_from_path()
2472   
2473///////////////////////////////////////////////
2474error_t vfs_lookup( xptr_t             root_xp,
2475                    char             * pathname,
2476                    uint32_t           lookup_mode,
2477                                        xptr_t           * inode_xp,
2478                                        char             * last_name )
2479{
2480    char               name[CONFIG_VFS_MAX_NAME_LENGTH];   // one name in path
2481
2482    xptr_t             parent_xp;    // extended pointer on parent inode
2483    cxy_t              parent_cxy;   // cluster for parent inode
2484    vfs_inode_t      * parent_ptr;   // local pointer on parent inode 
2485    xptr_t             dentry_xp;    // extended pointer on dentry       
2486    xptr_t             child_xp;     // extended pointer on child inode
2487    cxy_t              child_cxy;    // cluster for child inode
2488    vfs_inode_t      * child_ptr;    // local pointer on child inode
2489    vfs_fs_type_t      fs_type;      // File system type
2490    vfs_ctx_t        * ctx_ptr;      // local pointer on FS context
2491    char             * current;      // current pointer on path
2492    char             * next;         // next value for current pointer   
2493    bool_t             last;         // true when the name is the last in path
2494    bool_t             found;        // true when a child has been found
2495    bool_t             create;       // searched inode must be created if not found
2496    bool_t             excl;         // searched inode must not exist
2497    bool_t             parent;       // searched inode is the parent
2498    thread_t         * this;         // pointer on calling thread descriptor
2499    process_t        * process;      // pointer on calling process descriptor
2500    error_t            error;
2501
2502    this    = CURRENT_THREAD;
2503    process = this->process;
2504
2505// check pathname / root_xp consistency
2506assert( ((pathname[0] != '/') || (root_xp == process->vfs_root_xp)), 
2507"root inode must be VFS root for path <%s>", pathname );
2508
2509#if DEBUG_VFS_LOOKUP
2510uint32_t cycle = (uint32_t)hal_get_cycles();
2511char     root_name[CONFIG_VFS_MAX_NAME_LENGTH];
2512vfs_inode_get_name( root_xp , root_name );
2513if( DEBUG_VFS_LOOKUP < cycle )
2514printk("\n[%s] thread[%x,%x] enter / root <%s> / path <%s> / mode %x / cycle %d\n",
2515__FUNCTION__, process->pid, this->trdid, root_name, pathname, lookup_mode, cycle );
2516#endif
2517
2518#if ( DEBUG_VFS_LOOKUP & 1 )
2519if( DEBUG_VFS_LOOKUP < cycle )
2520vfs_display( root_xp );
2521#endif
2522
2523    // compute lookup flags
2524    create = (lookup_mode & VFS_LOOKUP_CREATE) == VFS_LOOKUP_CREATE;
2525    excl   = (lookup_mode & VFS_LOOKUP_EXCL)   == VFS_LOOKUP_EXCL;
2526    parent = (lookup_mode & VFS_LOOKUP_PARENT) == VFS_LOOKUP_PARENT;
2527
2528    // initialise loop variables
2529    parent_xp = root_xp;
2530    current   = pathname;
2531    next      = NULL;
2532    last      = false;
2533    child_xp  = XPTR_NULL;
2534
2535    // loop on nodes in pathname
2536    // load from device if one node in path not found in Inode Tree
2537    // exit loop when last name found (i.e. last == true)
2538    while( 1 )
2539    {
2540        // get parent inode cluster and local pointer
2541        parent_cxy = GET_CXY( parent_xp );
2542        parent_ptr = GET_PTR( parent_xp );
2543
2544        // get one "name" from path, and "last" flag
2545        error = vfs_get_name_from_path( current , name , &next , &last );
2546
2547        // handle VFS root case
2548        if ( error )
2549        {
2550
2551#if DEBUG_VFS_LOOKUP
2552cycle = (uint32_t)hal_get_cycles();
2553if( DEBUG_VFS_LOOKUP < cycle )
2554printk("\n[%s] thread[%x,%x] exit / parent inode(%x,%x) / <%s> / cycle %d\n",
2555__FUNCTION__ , process->pid, this->trdid, parent_cxy, parent_ptr, pathname, cycle );
2556#endif
2557            *inode_xp = process->vfs_root_xp;
2558            break;
2559        }
2560
2561#if (DEBUG_VFS_LOOKUP & 1)
2562if( DEBUG_VFS_LOOKUP < cycle )
2563printk("\n[%s] thread[%x,%x] search <%s> in <%s> / last = %d\n",
2564__FUNCTION__, process->pid, this->trdid, name, pathname, last );
2565#endif
2566
2567        // search the child dentry matching name in parent inode XHTAB
2568        found = vfs_get_child( parent_xp,
2569                               name,
2570                               &child_xp );
2571
2572        // get child inode local pointer and cluster
2573        child_ptr  = GET_PTR( child_xp );
2574        child_cxy  = GET_CXY( child_xp );
2575
2576        if( found == false )                              // not found in Inode Tree
2577        {
2578            // when a inode is not found in the Inode Tree:
2579            // - if (last and parent) the Inode Tree is not modified
2580            // - else we speculatively introduce a new (dentry/inode) in inode tree,
2581            //        and scan the parent directory mapper to initialise it.
2582            //     . if it is not found in the parent mapper:
2583            //         - if(last and create), a brand new file or directory is created
2584            //         - else, an error is reported
2585            //     . if it is found in parent mapper:
2586            //         - if( last and excl ), an error is reported
2587            //         - else the new child (inode & dentry) is initialised in Inode Tree
2588            //         - if the child is a directory, the child mapper is loaded from device
2589
2590            if( last && parent )   //  does nothing
2591            {
2592
2593#if (DEBUG_VFS_LOOKUP & 1)
2594if( DEBUG_VFS_LOOKUP < cycle )
2595printk("\n[%s] thread[%x,%x] child not found but only parent requested in <%s>\n",
2596__FUNCTION__, process->pid, this->trdid, pathname );
2597#endif
2598            }
2599            else                                    // try to get it from parent mapper
2600            {
2601
2602#if (DEBUG_VFS_LOOKUP & 1)
2603if( DEBUG_VFS_LOOKUP < cycle )
2604printk("\n[%s] thread[%x,%x] miss <%s> inode in Inode Tree => build from parent mapper\n",
2605__FUNCTION__, process->pid, this->trdid, name );
2606#endif
2607                // get parent inode FS type
2608                ctx_ptr    = hal_remote_lpt( XPTR( parent_cxy,&parent_ptr->ctx ) );
2609                fs_type    = hal_remote_l32( XPTR( parent_cxy , &ctx_ptr->type ) );
2610
2611                // select a cluster for new inode
2612                child_cxy = cluster_random_select();
2613
2614                // insert a new child dentry/inode couple in inode tree
2615                error = vfs_add_child_in_parent( child_cxy,
2616                                                 fs_type, 
2617                                                 parent_xp, 
2618                                                 name, 
2619                                                 &dentry_xp,
2620                                                 &child_xp );
2621                if( error )
2622                {
2623                    printk("\n[ERROR] in %s : cannot create inode <%s> in path <%s>\n",
2624                    __FUNCTION__ , name, pathname );
2625                    return -1;
2626                }
2627
2628                // get child inode local pointer
2629                child_ptr = GET_PTR( child_xp );
2630
2631#if (DEBUG_VFS_LOOKUP & 1)
2632if( DEBUG_VFS_LOOKUP < cycle )
2633printk("\n[%s] thread[%x,%x] created missing inode <%s> in cluster %x\n",
2634__FUNCTION__, process->pid, this->trdid, name, child_cxy );
2635#endif
2636                // scan parent mapper to find the missing dentry, and complete
2637                // the initialisation of dentry and child inode descriptors
2638                if( parent_cxy == local_cxy )
2639                {
2640                    error = vfs_fs_new_dentry( parent_ptr,
2641                                               name,
2642                                               child_xp );
2643                }
2644                else
2645                {
2646                    rpc_vfs_fs_new_dentry_client( parent_cxy,
2647                                                  parent_ptr,
2648                                                  name,
2649                                                  child_xp,
2650                                                  &error );
2651                }
2652
2653                // when the missing dentry is not in the parent mapper,
2654                // a new dentry must be registered in parent directory mapper
2655                if ( error )
2656                {
2657                    if ( last && create )  // add a brand new dentry in parent directory
2658                    {
2659                        error = vfs_new_dentry_init( parent_xp,               
2660                                                     dentry_xp,
2661                                                     child_xp );
2662                        if ( error )
2663                        {
2664                            printk("\n[ERROR] in %s : cannot init inode <%s> in path <%s>\n",
2665                            __FUNCTION__, name, pathname );
2666                            vfs_remove_child_from_parent( dentry_xp );
2667                            return -1;
2668                        }
2669
2670#if (DEBUG_VFS_LOOKUP & 1)
2671if( DEBUG_VFS_LOOKUP < cycle )
2672printk("\n[%s] thread[%x,%x] child <%s> not found in parent mapper => created it\n",
2673__FUNCTION__, process->pid, this->trdid, name );
2674vfs_inode_display( child_xp );
2675#endif
2676
2677
2678                    }
2679                    else                   // not last or not create => error
2680                    {                       
2681                        printk("\n[ERROR] in %s : <%s> node not found in parent for <%s>\n",
2682                        __FUNCTION__ , name , pathname );
2683                        vfs_remove_child_from_parent( dentry_xp );
2684                        return -1;
2685                    }
2686                }
2687                else          // child has been found in parent mapper
2688                {
2689                    // check the excl
2690                    if( last && create && excl )
2691                    {
2692                        printk("\n[ERROR] in %s : node already exist <%s>\n",
2693                        __FUNCTION__, name );
2694                       return -1;
2695                    }
2696
2697#if (DEBUG_VFS_LOOKUP & 1)
2698if( DEBUG_VFS_LOOKUP < cycle )
2699printk("\n[%s] thread[%x,%x] initialised inode <%s> from parent mapper\n",
2700__FUNCTION__, process->pid, this->trdid, name );
2701#endif
2702                    // load child mapper from device if child is a directory (prefetch)
2703                    uint32_t type = hal_remote_l32( XPTR( child_cxy , &child_ptr->type ) );
2704                    if( type == INODE_TYPE_DIR ) 
2705                    {
2706                        if( child_cxy == local_cxy )
2707                        {
2708                            error = vfs_inode_load_all_pages( child_ptr );
2709                        }
2710                        else
2711                        {
2712                            rpc_vfs_inode_load_all_pages_client( child_cxy,
2713                                                                 child_ptr,
2714                                                                 &error );
2715                        }
2716                        if ( error )
2717                        {
2718                            printk("\n[ERROR] in %s : cannot load <%s> from device\n",
2719                            __FUNCTION__ , name );
2720                            vfs_remove_child_from_parent( dentry_xp );
2721                            return -1;
2722                        }
2723
2724#if (DEBUG_VFS_LOOKUP & 1)
2725if( DEBUG_VFS_LOOKUP < cycle )
2726printk("\n[%s] thread[%x,%x] loaded directory mapper for <%s> from IOC\n",
2727__FUNCTION__ , process->pid, this->trdid, name );
2728#endif
2729                    }
2730                }
2731            }
2732        }
2733        else                                    // child directly found in inode tree
2734        {
2735       
2736#if (DEBUG_VFS_LOOKUP & 1)
2737if( DEBUG_VFS_LOOKUP < cycle )
2738printk("\n[%s] thread[%x,%x] found <%s> in Inode Tree / inode (%x,%x)\n",
2739__FUNCTION__, process->pid, this->trdid, name, child_cxy, child_ptr );
2740#endif
2741            // check the excl flag
2742            if( last && create && excl )
2743            {
2744                printk("\n[ERROR] in %s : node <%s> already exist\n",
2745                __FUNCTION__, name );
2746                return -1;
2747            }
2748        }
2749
2750        // TODO check access rights here [AG]
2751        // error = vfs_access_denied( child_xp,
2752        //                            client_uid,
2753        //                            client_gid );
2754        // if( error )
2755        // {
2756        //     printk("\n[ERROR] in %s : thread %x / permission denied for %s\n",
2757        //     __FUNCTION__ , this , name );
2758        //     return EACCES;
2759        // }
2760
2761        // take lock on child inode and release lock on parent
2762        // vfs_inode_lock( child_xp );
2763        // vfs_inode_unlock( parent_xp );
2764
2765        // exit when last
2766        if ( last )           // last inode in path  => return relevant info
2767        {
2768            if ( parent )  // return parent inode and child name
2769            {
2770
2771#if DEBUG_VFS_LOOKUP
2772cycle = (uint32_t)hal_get_cycles();
2773if( DEBUG_VFS_LOOKUP < cycle )
2774printk("\n[%s] thread[%x,%x] exit / parent inode(%x,%x) / <%s> / cycle %d\n",
2775__FUNCTION__ , process->pid, this->trdid, parent_cxy, parent_ptr, pathname, cycle );
2776#endif
2777                *inode_xp = parent_xp;
2778                strcpy( last_name , name );
2779                break; 
2780            }
2781            else        // return child inode name     
2782            {
2783
2784#if DEBUG_VFS_LOOKUP
2785cycle = (uint32_t)hal_get_cycles();
2786if( DEBUG_VFS_LOOKUP < cycle )
2787printk("\n[%s] thread[%x,%x] exit / child inode (%x,%x) / <%s> / cycle %d\n",
2788__FUNCTION__ , process->pid, this->trdid, child_cxy, child_ptr, pathname, cycle );
2789#endif
2790                *inode_xp = child_xp;
2791                break;
2792            }
2793        }
2794        else                     // not the last inode in path => update loop variables
2795        {
2796            parent_xp = child_xp;
2797            current   = next;
2798        }
2799    }
2800
2801    return 0;
2802
2803}  // end vfs_lookup()
2804
2805////////////////////////////////////////////////
2806error_t vfs_new_dentry_init( xptr_t   parent_xp,
2807                             xptr_t   dentry_xp,
2808                             xptr_t   child_xp )
2809{
2810    error_t     error;
2811    uint32_t    cluster_id;
2812    uint32_t    child_type;
2813    uint32_t    child_size;
2814
2815#if DEBUG_VFS_NEW_DENTRY_INIT
2816char parent_name[CONFIG_VFS_MAX_NAME_LENGTH];
2817char child_name[CONFIG_VFS_MAX_NAME_LENGTH];
2818vfs_inode_get_name( parent_xp , parent_name );
2819vfs_inode_get_name( child_xp  , child_name );
2820uint32_t   cycle = (uint32_t)hal_get_cycles();
2821thread_t * this  = CURRENT_THREAD;
2822if( DEBUG_VFS_NEW_DENTRY_INIT < cycle )
2823printk("\n[%s] thread[%x,%x] enter / parent <%s> / child <%s> / cycle %d\n",
2824__FUNCTION__ , this->process->pid, this->trdid, parent_name, child_name, cycle );
2825#endif
2826
2827    // get parent inode cluster and local pointer
2828    cxy_t          parent_cxy = GET_CXY( parent_xp );
2829    vfs_inode_t  * parent_ptr = GET_PTR( parent_xp );
2830
2831    // get dentry local pointer
2832    vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp );
2833
2834    // get child inode cluster and local pointer
2835    cxy_t          child_cxy  = GET_CXY( child_xp );
2836    vfs_inode_t  * child_ptr  = GET_PTR( child_xp );
2837
2838    // 1. allocate one free cluster_id in file system to child inode,
2839    // and update the File Allocation Table in both the FAT mapper and IOC device.
2840    // It depends on the child inode FS type.
2841    vfs_ctx_t * ctx = hal_remote_lpt( XPTR( child_cxy , &child_ptr->ctx ) );
2842
2843    error = vfs_fs_cluster_alloc( ctx->type,
2844                                  &cluster_id );
2845    if ( error )
2846    {
2847        printk("\n[ERROR] in %s : cannot find a free VFS cluster_id\n",
2848        __FUNCTION__ );
2849        return -1;
2850    }
2851
2852#if( DEBUG_VFS_NEW_DENTRY_INIT & 1)
2853if( DEBUG_VFS_NEW_DENTRY_INIT < cycle )
2854printk("\n[%s] thread[%x,%x] allocated FS cluster_id %x to <%s>\n",
2855__FUNCTION__ , this->process->pid, this->trdid, cluster_id, child_name );
2856#endif
2857
2858    // 2. update the child inode descriptor size and extend
2859    child_type = hal_remote_l32( XPTR( child_cxy , &child_ptr->type ) );
2860    child_size = 0;
2861   
2862    hal_remote_s32( XPTR( child_cxy , &child_ptr->size )   , child_size );
2863    hal_remote_spt( XPTR( child_cxy , &child_ptr->extend ) , (void*)(intptr_t)cluster_id );
2864
2865    // 3. update the parent inode mapper, and
2866    // update the dentry extension if required
2867    if( local_cxy == parent_cxy )
2868    {
2869        error = vfs_fs_add_dentry( parent_ptr,
2870                                   dentry_ptr );
2871    }
2872    else
2873    {
2874        rpc_vfs_fs_add_dentry_client( parent_cxy,
2875                                      parent_ptr,
2876                                      dentry_ptr,
2877                                      &error );
2878    }
2879    if ( error )
2880    {
2881        printk("\n[ERROR] in %s : cannot register child in parent directory\n",
2882        __FUNCTION__ );
2883        return -1;
2884    }
2885
2886#if DEBUG_VFS_NEW_DENTRY_INIT
2887cycle = (uint32_t)hal_get_cycles();
2888if( DEBUG_VFS_NEW_DENTRY_INIT < cycle )
2889printk("\n[%s] thread[%x,%x] exit / parent <%s> / child <%s> / cycle %d\n",
2890__FUNCTION__ , this->process->pid, this->trdid, parent_name, child_name, cycle );
2891#endif
2892
2893    return 0;
2894
2895}  // end vfs_new_dentry_init()
2896
2897///////////////////////////////////////////////////
2898error_t vfs_add_special_dentries( xptr_t  child_xp,
2899                                  xptr_t  parent_xp )
2900{
2901    error_t         error;
2902    vfs_inode_t   * child_ptr;         // local pointer on child inode directory
2903    cxy_t           child_cxy;         // child inode directory cluster identifier
2904    vfs_ctx_t     * ctx_ptr;           // local pointer on child inode FS context
2905    vfs_fs_type_t   fs_type;           // FS type of child inode
2906    xptr_t          dentry_xp;         // extended pointer on dentry (used for . and ..)
2907    vfs_dentry_t  * dentry_ptr;        // local pointer on dentry (used for . and ..)
2908
2909    // xptr_t          parents_root_xp;   // extended pointer on inode "parents" field
2910    // xptr_t          parents_entry_xp;  // extended pointer on dentry "parents" field
2911    xptr_t          children_xhtab_xp; // extended pointer on inode "children" field
2912    xptr_t          children_entry_xp; // extended pointer on dentry "children" field
2913
2914#if DEBUG_VFS_ADD_SPECIAL
2915uint32_t   cycle = (uint32_t)hal_get_cycles();
2916thread_t * this  = CURRENT_THREAD;
2917char child_name[CONFIG_VFS_MAX_NAME_LENGTH];
2918char parent_name[CONFIG_VFS_MAX_NAME_LENGTH];
2919vfs_inode_get_name( child_xp  , child_name );
2920vfs_inode_get_name( parent_xp , parent_name );
2921if( DEBUG_VFS_ADD_SPECIAL < cycle )
2922printk("\n[%s] thread[%x,%x] enter for child <%s> in parent <%s> / cycle %d\n",
2923__FUNCTION__, this->process->pid, this->trdid, child_name, parent_name, cycle );
2924#endif
2925
2926    // get new directory cluster and local pointer
2927    child_cxy  = GET_CXY( child_xp );
2928    child_ptr  = GET_PTR( child_xp );
2929
2930    // get child inode FS type
2931    ctx_ptr    = hal_remote_lpt( XPTR( child_cxy , &child_ptr->ctx ) );
2932    fs_type    = hal_remote_l32( XPTR( child_cxy , &ctx_ptr->type ) );
2933
2934    //////////////////////////// create <.> dentry //////////////////////
2935    if( child_cxy == local_cxy )     
2936    {
2937        error = vfs_dentry_create( fs_type,
2938                                   ".",
2939                                   &dentry_xp );
2940    }
2941    else
2942    {
2943        rpc_vfs_dentry_create_client( child_cxy,
2944                                      fs_type,
2945                                      ".",
2946                                      &dentry_xp,
2947                                      &error );
2948    }
2949    if( error )
2950    {
2951        printk("\n[ERROR] in %s : cannot create dentry <.> in cluster %x\n",
2952        __FUNCTION__ , child_cxy );
2953        return -1;
2954    }
2955
2956    // get <.> dentry local pointer
2957    dentry_ptr = GET_PTR( dentry_xp );
2958
2959#if(DEBUG_VFS_ADD_SPECIAL & 1)
2960cycle = (uint32_t)hal_get_cycles();
2961if( DEBUG_VFS_ADD_SPECIAL < cycle )
2962printk("\n[%s] thread[%x,%x] created dentry <.> (%x,%x) / cycle %d\n",
2963__FUNCTION__, this->process->pid, this->trdid, child_cxy, dentry_ptr, cycle );
2964#endif
2965
2966    // register <.> dentry in child inode xhtab of children
2967    children_xhtab_xp = XPTR( child_cxy , &child_ptr->children );
2968    children_entry_xp = XPTR( child_cxy , &dentry_ptr->children );
2969    error = xhtab_insert( children_xhtab_xp , "." , children_entry_xp );
2970    if( error )
2971    {
2972        printk("\n[ERROR] in %s : cannot register dentry <.> in xhtab\n",
2973        __FUNCTION__ );
2974        return -1;
2975    }
2976
2977   
2978    // don't register <.> dentry in child_inode xlist of parents
2979    // parents_root_xp  = XPTR( child_cxy , &child_ptr->parents );
2980    // parents_entry_xp = XPTR( child_cxy , &dentry_ptr->parents );
2981    // xlist_add_first( parents_root_xp , parents_entry_xp );
2982    // hal_remote_atomic_add( XPTR( child_cxy , &child_ptr->links ) , 1 );
2983
2984    // update "parent" and "child_xp" fields in <.> dentry
2985    hal_remote_s64( XPTR( child_cxy , &dentry_ptr->child_xp ) , child_xp );
2986    hal_remote_spt( XPTR( child_cxy , &dentry_ptr->parent ) , child_ptr );
2987
2988#if(DEBUG_VFS_ADD_SPECIAL & 1)
2989cycle = (uint32_t)hal_get_cycles();
2990if( DEBUG_VFS_ADD_SPECIAL < cycle )
2991printk("\n[%s] thread[%x,%x] linked dentry <.> to parent and child inodes / cycle %d\n", 
2992__FUNCTION__, this->process->pid, this->trdid, cycle ); 
2993#endif
2994
2995    // introduce <.> dentry into child directory mapper
2996    // only if the target directory is not the root VFS
2997    if( child_xp != parent_xp )
2998    {
2999        if( child_cxy == local_cxy )
3000        { 
3001            error = vfs_fs_add_dentry( child_ptr,
3002                                       dentry_ptr );
3003        }
3004        else
3005        {
3006            rpc_vfs_fs_add_dentry_client( child_cxy,
3007                                          child_ptr,
3008                                          dentry_ptr,
3009                                          &error );
3010        }
3011        if( error )
3012        {
3013            printk("\n[ERROR] in %s : cannot introduce dentry <..> in mapper %x\n",
3014            __FUNCTION__ );
3015            return -1;
3016        }
3017
3018#if(DEBUG_VFS_ADD_SPECIAL & 1)
3019cycle = (uint32_t)hal_get_cycles();
3020if( DEBUG_VFS_ADD_SPECIAL < cycle )
3021printk("\n[%s] thread[%x,%x] registered dentry <.> in child mapper / cycle %d\n", 
3022__FUNCTION__, this->process->pid, this->trdid, cycle ); 
3023#endif
3024
3025    }
3026
3027    ///////////////////////////// create <..> dentry ///////////////////////
3028    if( child_cxy == local_cxy )     
3029    {
3030        error = vfs_dentry_create( fs_type,
3031                                   "..",
3032                                   &dentry_xp );
3033    }
3034    else
3035    {
3036        rpc_vfs_dentry_create_client( child_cxy,
3037                                      fs_type,
3038                                      "..",
3039                                      &dentry_xp,
3040                                      &error );
3041    }
3042    if( error )
3043    {
3044        printk("\n[ERROR] in %s : cannot create dentry <..> in cluster %x\n",
3045        __FUNCTION__ , child_cxy );
3046        return -1;
3047    }
3048
3049    // get <..> dentry local pointer
3050    dentry_ptr = GET_PTR( dentry_xp );
3051
3052#if(DEBUG_VFS_ADD_SPECIAL & 1)
3053cycle = (uint32_t)hal_get_cycles();
3054if( DEBUG_VFS_ADD_SPECIAL < cycle )
3055printk("\n[%s] thread[%x,%x] created dentry <..> (%x,%x) / cycle %d\n",
3056__FUNCTION__, this->process->pid, this->trdid, child_cxy, dentry_ptr, cycle );
3057#endif
3058
3059    // register <..> dentry in child_inode xhtab of children
3060    children_xhtab_xp = XPTR( child_cxy , &child_ptr->children );
3061    children_entry_xp = XPTR( child_cxy , &dentry_ptr->children );
3062    error = xhtab_insert( children_xhtab_xp , ".." , children_entry_xp );
3063    if( error )
3064    {
3065        printk("\n[ERROR] in %s : cannot register dentry <..> in xhtab\n",
3066        __FUNCTION__ );
3067        return -1;
3068    }
3069
3070    // don't register <..> dentry in parent_inode xlist of parents
3071
3072    // update "parent" and "child_xp" fields in <..> dentry
3073    hal_remote_s64( XPTR( child_cxy , &dentry_ptr->child_xp ) , parent_xp );
3074    hal_remote_spt( XPTR( child_cxy , &dentry_ptr->parent ) , child_ptr );
3075
3076#if(DEBUG_VFS_ADD_SPECIAL & 1)
3077cycle = (uint32_t)hal_get_cycles();
3078if( DEBUG_VFS_ADD_SPECIAL < cycle )
3079printk("\n[%s] thread[%x,%x] linked dentry <..> to parent and child inodes / cycle %d\n", 
3080__FUNCTION__, this->process->pid, this->trdid, cycle ); 
3081#endif
3082
3083    // introduce <..> dentry into child directory mapper
3084    // only if the target directory is not the root VFS
3085    if( child_xp != parent_xp )
3086    {
3087        if( child_cxy == local_cxy )
3088        { 
3089            error = vfs_fs_add_dentry( child_ptr,
3090                                       dentry_ptr );
3091        }
3092        else
3093        {
3094            rpc_vfs_fs_add_dentry_client( child_cxy,
3095                                          child_ptr,
3096                                          dentry_ptr,
3097                                          &error );
3098        }
3099        if( error )
3100        {
3101            printk("\n[ERROR] in %s : cannot introduce dentry <..> in mapper %x\n",
3102            __FUNCTION__ );
3103            return -1;
3104        }
3105
3106#if(DEBUG_VFS_ADD_SPECIAL & 1)
3107cycle = (uint32_t)hal_get_cycles();
3108if( DEBUG_VFS_ADD_SPECIAL < cycle )
3109printk("\n[%s] thread[%x,%x] registered dentry <..> in child mapper / cycle %d\n", 
3110__FUNCTION__, this->process->pid, this->trdid, cycle ); 
3111#endif
3112
3113    }
3114
3115#if DEBUG_VFS_ADD_SPECIAL
3116cycle = (uint32_t)hal_get_cycles();
3117if( DEBUG_VFS_ADD_SPECIAL < cycle )
3118printk("\n[%s] thread[%x,%x] exit for child <%s> in parent <%s> / cycle %d\n",
3119__FUNCTION__, this->process->pid, this->trdid, child_name, parent_name, cycle );
3120#endif
3121
3122    return 0;
3123
3124}  // end vfs_add_special_dentries()
3125
3126//////////////////////////////////////////
3127error_t vfs_get_path( xptr_t     inode_xp,
3128                      char     * buffer,
3129                      char    ** first,
3130                      uint32_t   max_size )
3131{
3132        xptr_t         dentry_xp;        // extended pointer on current dentry
3133    vfs_dentry_t * dentry_ptr;       // local pointer on current dentry
3134    cxy_t          dentry_cxy;       // current dentry cluster identifier
3135    xptr_t         name_xp;          // extended pointer on current dentry name
3136        uint32_t       length;           // length of current dentry name
3137        int32_t        index;            // slot index in buffer
3138    xptr_t         current_xp;       // extended pointer on current inode
3139    vfs_inode_t  * current_ptr;      // local pointer on current inode
3140    cxy_t          current_cxy;      // current inode cluster identifier
3141    xptr_t         vfs_root_xp;      // extended pointer on VFS root inode
3142    vfs_inode_t  * vfs_root_ptr;     // local pointer on VFS root inode
3143    cxy_t          vfs_root_cxy;     // VFS root inode cluster identifier
3144    xptr_t         lock_xp;          // extended pointer on Inode Tree lock
3145    xptr_t         parents_root_xp;  // extended pointer on current inode parents root
3146    bool_t         found;            // condition to exit the while loop
3147
3148    thread_t  * this    = CURRENT_THREAD;
3149    process_t * process = this->process;
3150
3151#if DEBUG_VFS_GET_PATH
3152uint32_t cycle = (uint32_t)hal_get_cycles();
3153if( DEBUG_VFS_GET_PATH < cycle )
3154printk("\n[%s] thread[%x,%x] enter : inode (%x,%x) / cycle %d\n",
3155__FUNCTION__ , process->pid, this->trdid,
3156GET_CXY( inode_xp ), GET_PTR( inode_xp ), cycle );
3157#endif
3158
3159        // set the NUL character in buffer / initialise buffer index
3160        buffer[max_size - 1] = 0;
3161    index    = (int32_t)(max_size - 1);
3162
3163    // initialize current inode
3164    current_xp  = inode_xp;
3165
3166    // build extended pointer on lock protecting Inode Tree
3167    vfs_root_xp  = process->vfs_root_xp;
3168    vfs_root_ptr = GET_PTR( vfs_root_xp );
3169    vfs_root_cxy = GET_CXY( vfs_root_xp );
3170    lock_xp      = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock );
3171
3172    // take lock protecting Inode Tree in read mode
3173    remote_rwlock_rd_acquire( lock_xp );
3174
3175    // traverse Inode Tree from target inode to VFS root
3176    // selecting always the first parent dentry
3177    // the buffer is written in "reverse order" (from target inode to root)
3178    // exit the while loop when the VFS root has been found
3179        do
3180    {
3181        // get current inode cluster and local pointer
3182        current_cxy = GET_CXY( current_xp );
3183        current_ptr = GET_PTR( current_xp );
3184
3185        // build extended pointer on parents dentries root
3186        parents_root_xp = XPTR( current_cxy , &current_ptr->parents );
3187
3188        // compute exit condition <=> current inode is VFS root   
3189        found = xlist_is_empty( parents_root_xp );
3190
3191        if( found )                              // parent is the VFS root
3192        {
3193            if( index == (int32_t)(max_size - 1) )
3194            {
3195                // update index
3196                index--;
3197                 
3198                // set separator 
3199                        buffer[index] = '/';
3200
3201// check buffer overflow
3202assert( (index >= 0) , "kernel buffer too small" );
3203
3204            }
3205        }
3206        else                                     // not the VFS root
3207        {
3208            // get first parent dentry cluster and pointers
3209            dentry_xp  = XLIST_FIRST( parents_root_xp , vfs_dentry_t , parents );
3210            dentry_cxy = GET_CXY( dentry_xp );
3211            dentry_ptr = GET_PTR( dentry_xp );
3212
3213            // get extended pointer on dentry name and name length
3214            name_xp = XPTR( dentry_cxy , dentry_ptr->name );
3215            length  = hal_remote_l32( XPTR( dentry_cxy , &dentry_ptr->length ) );
3216
3217#if (DEBUG_VFS_GET_PATH & 1)
3218char debug_name[CONFIG_VFS_MAX_NAME_LENGTH];
3219hal_remote_strcpy( XPTR( local_cxy , debug_name ) , name_xp );
3220if( DEBUG_VFS_GET_PATH < cycle )
3221printk("\n[%s] thread(%x,%s) get current dentry <%s> in cluster %x\n",
3222__FUNCTION__ , process->pid, this->trdid, debug_name, current_cxy );
3223#endif
3224            // update index
3225            index -= (length + 1); 
3226
3227// check buffer overflow
3228assert( (index >= 0) , "kernel buffer too small" );
3229
3230            // update pathname
3231            hal_remote_memcpy( XPTR( local_cxy , &buffer[index + 1] ) ,
3232                               name_xp , length );
3233
3234            // set separator 
3235                    buffer[index] = '/';
3236
3237            // get extended pointer on parent inode
3238            current_xp = XPTR( dentry_cxy , 
3239                               hal_remote_lpt( XPTR( dentry_cxy , &dentry_ptr->parent ) ) );
3240        }
3241    }
3242    while( found == false );
3243
3244    // release lock protecting Inode Tree in read mode
3245    remote_rwlock_rd_release( lock_xp );
3246
3247#if DEBUG_VFS_GET_PATH
3248cycle = (uint32_t)hal_get_cycles();
3249if( DEBUG_VFS_GET_PATH < cycle )
3250printk("\n[%s] thread[%x,%x] exit : path <%s> / cycle %d\n",
3251__FUNCTION__ , process->pid, this->trdid, &buffer[index], cycle );
3252#endif
3253
3254    // return pointer on first character in buffer
3255    *first = &buffer[index];
3256        return 0;
3257
3258}  // end vfs_get_path()
3259
3260     
3261////////////////////////////////////////////////////////////////////
3262error_t vfs_add_child_in_parent( cxy_t              child_cxy,
3263                                 vfs_fs_type_t      fs_type,
3264                                 xptr_t             parent_inode_xp,
3265                                 char             * name,
3266                                 xptr_t           * child_dentry_xp,
3267                                 xptr_t           * child_inode_xp )
3268{
3269    error_t        error;
3270    cxy_t          parent_cxy;          // parent inode cluster identifier
3271    vfs_inode_t  * parent_inode_ptr;    // parent inode local pointer
3272    xptr_t         new_dentry_xp;       // extended pointer on created dentry
3273    vfs_dentry_t * new_dentry_ptr;      // created dentry local pointer
3274    xptr_t         new_inode_xp;        // extended pointer on created child inode
3275    vfs_inode_t  * new_inode_ptr;       // local pointer on created child inode
3276
3277    xptr_t         parents_root_xp;     // extended pointer on child inode  "parents" field
3278    xptr_t         parents_entry_xp;    // extended pointer on child dentry "parents" field
3279    xptr_t         children_xhtab_xp;   // extended pointer on parent inode "children" field
3280    xptr_t         children_entry_xp;   // extended pointer on child dentry "children" field
3281   
3282    // get parent inode cluster and pointer
3283    parent_cxy       = GET_CXY( parent_inode_xp );
3284    parent_inode_ptr = GET_PTR( parent_inode_xp );
3285
3286#if DEBUG_VFS_ADD_CHILD
3287char parent_name[CONFIG_VFS_MAX_NAME_LENGTH];
3288vfs_inode_get_name( parent_inode_xp , parent_name );
3289uint32_t cycle = (uint32_t)hal_get_cycles();
3290thread_t * this = CURRENT_THREAD; 
3291if( DEBUG_VFS_ADD_CHILD < cycle )
3292printk("\n[%s] thread[%x,%x] enter / child <%s> / parent <%s> / cycle %d\n",
3293__FUNCTION__, this->process->pid, this->trdid, name,
3294parent_name, (uint32_t)hal_get_cycles() );
3295#endif
3296
3297    // 1. create dentry in parent cluster
3298    if( parent_cxy == local_cxy )           // parent cluster is local
3299    {
3300        error = vfs_dentry_create( fs_type,
3301                                   name,
3302                                   &new_dentry_xp );
3303    }
3304    else                                    // parent cluster is remote
3305    {
3306        rpc_vfs_dentry_create_client( parent_cxy,
3307                                      fs_type,
3308                                      name,
3309                                      &new_dentry_xp,
3310                                      &error );
3311    }
3312                                     
3313    if( error )
3314    {
3315        printk("\n[ERROR] in %s : cannot create dentry <%s> in cluster %x\n",
3316        __FUNCTION__ , name , parent_cxy );
3317        return -1;
3318    }
3319
3320    // get dentry local pointer
3321    new_dentry_ptr = GET_PTR( new_dentry_xp );
3322
3323#if(DEBUG_VFS_ADD_CHILD & 1)
3324if( DEBUG_VFS_ADD_CHILD < cycle )
3325printk("\n[%s] thread[%x,%x] created dentry <%s> : (%x,%x)\n",
3326__FUNCTION__, this->process->pid, this->trdid, name, parent_cxy, new_dentry_ptr );
3327#endif
3328
3329    // 2. create child inode in child cluster
3330    // TODO : define attr / mode / uid / gid
3331    uint32_t attr = 0;
3332    uint32_t mode = 0;
3333    uint32_t uid  = 0;
3334    uint32_t gid  = 0;
3335   
3336    if( child_cxy == local_cxy )      // child cluster is local
3337    {
3338        error = vfs_inode_create( fs_type,
3339                                  attr,
3340                                  mode,
3341                                  uid,
3342                                  gid,
3343                                  &new_inode_xp );
3344    }
3345    else                              // child cluster is remote
3346    {
3347        rpc_vfs_inode_create_client( child_cxy,
3348                                     fs_type,
3349                                     attr,
3350                                     mode,
3351                                     uid,
3352                                     gid,
3353                                     &new_inode_xp,
3354                                     &error );
3355    }
3356                                     
3357    if( error )
3358    {
3359        printk("\n[ERROR] in %s : cannot create inode in cluster %x\n",
3360               __FUNCTION__ , child_cxy );
3361 
3362        if( parent_cxy == local_cxy ) vfs_dentry_destroy( new_dentry_ptr );
3363        else rpc_vfs_dentry_destroy_client( parent_cxy , new_dentry_ptr );
3364        return -1;
3365    }
3366
3367    // get new inode local pointer
3368    new_inode_ptr = GET_PTR( new_inode_xp );
3369   
3370#if(DEBUG_VFS_ADD_CHILD & 1)
3371if( DEBUG_VFS_ADD_CHILD < cycle )
3372printk("\n[%s] thread[%x,%x] created inode <%s> : (%x,%x)\n",
3373__FUNCTION__ , this->process->pid, this->trdid, name , child_cxy, new_inode_ptr );
3374#endif
3375
3376
3377    // 3. register new_dentry in new_inode xlist of parents
3378    parents_root_xp  = XPTR( child_cxy , &new_inode_ptr->parents );
3379    parents_entry_xp = XPTR( parent_cxy, &new_dentry_ptr->parents );
3380    xlist_add_first( parents_root_xp , parents_entry_xp );
3381    hal_remote_atomic_add( XPTR( child_cxy , &new_inode_ptr->links ) , 1 );
3382
3383#if(DEBUG_VFS_ADD_CHILD & 1)
3384if( DEBUG_VFS_ADD_CHILD < cycle )
3385printk("\n[%s] thread[%x,%x] linked dentry(%x,%x) to child inode(%x,%x)\n",
3386__FUNCTION__, this->process->pid, this->trdid, 
3387parent_cxy, new_dentry_ptr, child_cxy, new_inode_ptr );
3388#endif
3389
3390    // 4. register new_dentry in parent_inode xhtab of children
3391    children_xhtab_xp = XPTR( parent_cxy , &parent_inode_ptr->children );
3392    children_entry_xp = XPTR( parent_cxy , &new_dentry_ptr->children );
3393    xhtab_insert( children_xhtab_xp , name , children_entry_xp );
3394
3395#if(DEBUG_VFS_ADD_CHILD & 1)
3396if( DEBUG_VFS_ADD_CHILD < cycle )
3397printk("\n[%s] thread[%x,%x] linked dentry(%x,%x) to parent inode(%x,%x)\n",
3398__FUNCTION__, this->process->pid, this->trdid, 
3399parent_cxy, new_dentry_ptr, parent_cxy, parent_inode_ptr );
3400#endif
3401
3402    // 5. update "parent" and "child_xp" fields in new_dentry
3403    hal_remote_s64( XPTR( parent_cxy , &new_dentry_ptr->child_xp ) , new_inode_xp );
3404    hal_remote_spt( XPTR( parent_cxy , &new_dentry_ptr->parent ) , parent_inode_ptr );
3405
3406#if DEBUG_VFS_ADD_CHILD
3407cycle = (uint32_t)hal_get_cycles();
3408if( DEBUG_VFS_ADD_CHILD < cycle )
3409printk("\n[%s] thread[%x,%x] exit for <%s> / cycle %d\n",
3410__FUNCTION__, this->process->pid, this->trdid, name, cycle );
3411#endif
3412
3413    // return extended pointer on dentry & child inode
3414    *child_dentry_xp = new_dentry_xp;
3415    *child_inode_xp  = new_inode_xp;
3416    return 0;
3417
3418}  // end vfs_add_child_in_parent()
3419
3420/////////////////////////////////////////////////////
3421void vfs_remove_child_from_parent( xptr_t dentry_xp )
3422{
3423    cxy_t          parent_cxy;         // parent inode cluster identifier
3424    cxy_t          child_cxy;          // child inode cluster identifier
3425    vfs_dentry_t * dentry_ptr;         // local pointer on dentry
3426    xptr_t         child_inode_xp;     // extended pointer on child inode
3427    vfs_inode_t  * child_inode_ptr;    // local pointer on child inode
3428    vfs_inode_t  * parent_inode_ptr;   // local pointer on parent inode
3429    uint32_t       links;              // number of child inode parents
3430
3431    char dentry_name[CONFIG_VFS_MAX_NAME_LENGTH];
3432   
3433    // get parent cluster and dentry local pointer
3434    parent_cxy = GET_CXY( dentry_xp );
3435    dentry_ptr = GET_PTR( dentry_xp );
3436
3437    // get a local copy of dentry name
3438    hal_remote_strcpy( XPTR( local_cxy  , dentry_name ),
3439                       XPTR( parent_cxy , &dentry_ptr->name ) );
3440
3441    // get parent_inode local pointer
3442    parent_inode_ptr = hal_remote_lpt( XPTR( parent_cxy , &dentry_ptr->parent ) );
3443 
3444    // get child cluster and child_inode pointers
3445    child_inode_xp   = hal_remote_l64( XPTR( parent_cxy , &dentry_ptr->child_xp ) );
3446    child_cxy        = GET_CXY( child_inode_xp ); 
3447    child_inode_ptr  = GET_PTR( child_inode_xp );
3448
3449    // remove dentry from parent_inode
3450    xhtab_remove( XPTR( parent_cxy , &parent_inode_ptr->children ),
3451                  dentry_name,
3452                  XPTR( parent_cxy , &dentry_ptr->children ) );
3453
3454    // remove dentry from child_inode
3455    xlist_unlink( XPTR( parent_cxy , &dentry_ptr->parents ) );
3456    links = hal_remote_atomic_add( XPTR( child_cxy , &child_inode_ptr->links ) , -1 );
3457
3458    // delete dentry descriptor
3459    if( parent_cxy == local_cxy )
3460    {
3461         vfs_dentry_destroy( dentry_ptr );
3462    }
3463    else
3464    {
3465         rpc_vfs_dentry_destroy_client( parent_cxy,
3466                                        dentry_ptr );
3467    }
3468
3469    // delete child_inode descriptor if last link
3470    if( links == 1 )
3471    {
3472        if( child_cxy == local_cxy )
3473        {
3474            vfs_inode_destroy( child_inode_ptr );
3475        }
3476        else
3477        {
3478            rpc_vfs_inode_destroy_client( child_cxy , child_inode_ptr );
3479        }
3480    }
3481
3482}  // end vfs_remove_child_from_parent()
3483
3484
3485
3486
3487//////////////////////////////////////////////////////////////////////////////////////////
3488//    API used by VFS to access a specific FS 
3489//////////////////////////////////////////////////////////////////////////////////////////
3490
3491//////////////////////////////////////////////
3492error_t vfs_fs_move_page( xptr_t      page_xp,
3493                          cmd_type_t  cmd_type )
3494{
3495    error_t error = 0;
3496
3497assert( (page_xp != XPTR_NULL) , "page pointer is NULL" );
3498
3499    page_t * page_ptr = GET_PTR( page_xp );
3500    cxy_t    page_cxy = GET_CXY( page_xp );
3501
3502    // get local pointer on page mapper
3503    mapper_t * mapper = hal_remote_lpt( XPTR( page_cxy , &page_ptr->mapper ) );
3504
3505assert( (mapper != NULL) , "no mapper for page" );
3506
3507    // get FS type
3508    vfs_fs_type_t fs_type = hal_remote_l32( XPTR( page_cxy , &mapper->type ) );
3509
3510    // call relevant FS function
3511    if( fs_type == FS_TYPE_FATFS )
3512    {
3513        error = fatfs_move_page( page_xp , cmd_type ); 
3514    }
3515    else if( fs_type == FS_TYPE_RAMFS )
3516    {
3517        assert( false , "should not be called for RAMFS\n" );
3518    }
3519    else if( fs_type == FS_TYPE_DEVFS )
3520    {
3521        assert( false , "should not be called for DEVFS\n" );
3522    }
3523    else
3524    {
3525        assert( false , "undefined file system type" );
3526    }
3527
3528    return error;
3529
3530}  // end vfs_fs_move_page()
3531
3532////////////////////////////////////////////////
3533error_t vfs_fs_add_dentry( vfs_inode_t  * inode,
3534                           vfs_dentry_t * dentry )
3535{
3536    error_t error = 0;
3537
3538assert( (inode  != NULL) , "inode  pointer is NULL" );
3539assert( (dentry != NULL) , "dentry pointer is NULL" );
3540
3541    mapper_t * mapper = inode->mapper;
3542
3543assert( (mapper != NULL) , "mapper pointer is NULL" );
3544
3545    // get FS type
3546    vfs_fs_type_t fs_type = mapper->type;
3547
3548    // call relevant FS function
3549    if( fs_type == FS_TYPE_FATFS )
3550    {
3551        error = fatfs_add_dentry( inode , dentry ); 
3552    }
3553    else if( fs_type == FS_TYPE_RAMFS )
3554    {
3555        error = 0;     // does nothing for RAMFS
3556    }
3557    else if( fs_type == FS_TYPE_DEVFS )
3558    {
3559        error = 0;     // does nothing for DEVFS
3560    }
3561    else
3562    {
3563        assert( false , "undefined file system type" );
3564    }
3565
3566    return error;
3567
3568}  // end vfs_fs_add_dentry()
3569
3570///////////////////////////////////////////////////
3571error_t vfs_fs_remove_dentry( vfs_inode_t  * inode,
3572                              vfs_dentry_t * dentry )
3573{
3574    error_t error = 0;
3575
3576assert( (inode  != NULL) , "inode  pointer is NULL" );
3577assert( (dentry != NULL) , "dentry pointer is NULL" );
3578
3579    mapper_t * mapper = inode->mapper;
3580
3581assert( (mapper != NULL) , "mapper pointer is NULL" );
3582
3583    // get FS type
3584    vfs_fs_type_t fs_type = mapper->type;
3585
3586    // call relevant FS function
3587    if( fs_type == FS_TYPE_FATFS )
3588    {
3589        error = fatfs_remove_dentry( inode , dentry ); 
3590    }
3591    else if( fs_type == FS_TYPE_RAMFS )
3592    {
3593        error = 0;     // does nothing for RAMFS
3594    }
3595    else if( fs_type == FS_TYPE_DEVFS )
3596    {
3597        error = 0;     // does nothing for DEVFS
3598    }
3599    else
3600    {
3601        assert( false , "undefined file system type" );
3602    }
3603
3604    return error;
3605
3606}  // end vfs_fs_remove_dentry()
3607
3608////////////////////////////////////////////////
3609error_t vfs_fs_new_dentry( vfs_inode_t * parent,
3610                           char        * name,
3611                           xptr_t        child_xp )
3612{
3613    error_t error = 0;
3614
3615// check arguments
3616assert( (parent != NULL) , "parent pointer is NULL");
3617assert( (child_xp != XPTR_NULL) , "child pointer is NULL");
3618
3619    // get parent inode FS type
3620    vfs_fs_type_t fs_type = parent->ctx->type;
3621
3622    // call relevant FS function
3623    if( fs_type == FS_TYPE_FATFS )
3624    {
3625        error = fatfs_new_dentry( parent , name , child_xp );
3626    }
3627    else if( fs_type == FS_TYPE_RAMFS )
3628    {
3629        assert( false , "should not be called for RAMFS" );
3630    }
3631    else if( fs_type == FS_TYPE_DEVFS )
3632    {
3633        assert( false , "should not be called for DEVFS" );
3634    }
3635    else
3636    {
3637        assert( false , "undefined file system type" );
3638    }
3639
3640    return error;
3641
3642} // end vfs_fs_new_dentry()
3643
3644///////////////////////////////////////////////////
3645error_t vfs_fs_update_dentry( vfs_inode_t  * inode,
3646                              vfs_dentry_t * dentry,
3647                              uint32_t       size )
3648{
3649    error_t error = 0;
3650
3651// check arguments
3652assert( (inode  != NULL) , "inode  pointer is NULL");
3653assert( (dentry != NULL) , "dentry pointer is NULL");
3654
3655    // get parent inode FS type
3656    vfs_fs_type_t fs_type = inode->ctx->type;
3657
3658    // call relevant FS function
3659    if( fs_type == FS_TYPE_FATFS )
3660    {
3661        error = fatfs_update_dentry( inode , dentry , size );
3662    }
3663    else if( fs_type == FS_TYPE_RAMFS )
3664    {
3665        assert( false , "should not be called for RAMFS" );
3666    }
3667    else if( fs_type == FS_TYPE_DEVFS )
3668    {
3669        assert( false , "should not be called for DEVFS" );
3670    }
3671    else
3672    {
3673        assert( false , "undefined file system type" );
3674    }
3675
3676    return error;
3677
3678} // end vfs_fs_update_dentry()
3679
3680///////////////////////////////////////////////////
3681error_t vfs_fs_get_user_dir( vfs_inode_t   * inode,
3682                             struct dirent * array,
3683                             uint32_t        max_dirent,
3684                             uint32_t        min_dentry,
3685                             bool_t          detailed,
3686                             uint32_t      * entries,
3687                             bool_t        * done )
3688{
3689    error_t error = 0;
3690
3691// check arguments
3692assert( (inode != NULL) , "parent pointer is NULL");
3693assert( (array != NULL) , "child pointer is NULL");
3694assert( (detailed == false) , "detailed argument not supported\n");
3695
3696    // check inode type
3697    if( inode->type != INODE_TYPE_DIR )
3698    {
3699        printk("\n[ERROR] in %s : target inode is not a directory\n",
3700        __FUNCTION__ );
3701        return -1;
3702    }
3703
3704    // get parent inode FS type
3705    vfs_fs_type_t fs_type = inode->ctx->type;
3706
3707    // call relevant FS function
3708    if( fs_type == FS_TYPE_FATFS )
3709    {
3710        error = fatfs_get_user_dir( inode, 
3711                                    array,
3712                                    max_dirent,
3713                                    min_dentry,
3714                                    detailed,
3715                                    entries,
3716                                    done );
3717    }
3718    else if( fs_type == FS_TYPE_RAMFS )
3719    {
3720        assert( false , "should not be called for RAMFS" );
3721    }
3722    else if( fs_type == FS_TYPE_DEVFS )
3723    {
3724        error = devfs_get_user_dir( inode,
3725                                    array,
3726                                    max_dirent,
3727                                    min_dentry,
3728                                    detailed,
3729                                    entries,
3730                                    done );
3731    }
3732    else
3733    {
3734        assert( false , "undefined file system type" );
3735    }
3736
3737    return error;
3738
3739}  // end vfs_fs_get_user_dir()
3740 
3741////////////////////////////////////////////////
3742error_t vfs_fs_sync_inode( vfs_inode_t * inode )
3743{
3744    error_t error = 0;
3745
3746// check arguments
3747assert( (inode != NULL) , "inode pointer is NULL");
3748
3749    // get inode FS type
3750    vfs_fs_type_t fs_type = inode->ctx->type;
3751
3752    // call relevant FS function
3753    if( fs_type == FS_TYPE_FATFS )
3754    {
3755        error = fatfs_sync_inode( inode );
3756    }
3757    else if( fs_type == FS_TYPE_RAMFS )
3758    {
3759        assert( false , "should not be called for RAMFS" );
3760    }
3761    else if( fs_type == FS_TYPE_DEVFS )
3762    {
3763        assert( false , "should not be called for DEVFS" );
3764    }
3765    else
3766    {
3767        assert( false , "undefined file system type" );
3768    }
3769
3770    return error;
3771
3772}  // end vfs_fs_sync_inode()
3773
3774////////////////////////////////////////////////
3775error_t vfs_fs_sync_fat( vfs_fs_type_t fs_type )
3776{
3777    error_t error = 0;
3778
3779    // call relevant FS function
3780    if( fs_type == FS_TYPE_FATFS )
3781    {
3782        error = fatfs_sync_fat();
3783    }
3784    else if( fs_type == FS_TYPE_RAMFS )
3785    {
3786        assert( false , "should not be called for RAMFS" );
3787    }
3788    else if( fs_type == FS_TYPE_DEVFS )
3789    {
3790        assert( false , "should not be called for DEVFS" );
3791    }
3792    else
3793    {
3794        assert( false , "undefined file system type" );
3795    }
3796
3797    return error;
3798
3799}  // end vfs_fs_sync_fat()
3800
3801//////////////////////////////////////////////////////
3802error_t vfs_fs_sync_free_info( vfs_fs_type_t fs_type )
3803{
3804    error_t error = 0;
3805
3806    // call relevant FS function
3807    if( fs_type == FS_TYPE_FATFS )
3808    {
3809        error = fatfs_sync_free_info();
3810    }
3811    else if( fs_type == FS_TYPE_RAMFS )
3812    {
3813        assert( false , "should not be called for RAMFS" );
3814    }
3815    else if( fs_type == FS_TYPE_DEVFS )
3816    {
3817        assert( false , "should not be called for DEVFS" );
3818    }
3819    else
3820    {
3821        assert( false , "undefined file system type" );
3822    }
3823
3824    return error;
3825
3826}  // end vfs_fs_sync_fat()
3827
3828/////////////////////////////////////////////////
3829error_t vfs_fs_cluster_alloc( uint32_t   fs_type,
3830                              uint32_t * cluster )
3831{
3832    error_t error = 0;
3833
3834    // call relevant FS function
3835    if( fs_type == FS_TYPE_FATFS )
3836    {
3837        error = fatfs_cluster_alloc( cluster );
3838    }
3839    else if( fs_type == FS_TYPE_RAMFS )
3840    {
3841        assert( false , "should not be called for RAMFS" );
3842    }
3843    else if( fs_type == FS_TYPE_DEVFS )
3844    {
3845        assert( false , "should not be called for DEVFS" );
3846    }
3847    else
3848    {
3849        assert( false , "undefined file system type" );
3850    }
3851
3852    return error;
3853
3854} // end vfs_fs_cluster_alloc()
3855
3856////////////////////////////////////////////////
3857error_t vfs_fs_release_inode( xptr_t  inode_xp )
3858{
3859    error_t error = 0;
3860
3861assert( (inode_xp  != XPTR_NULL) , "inode pointer is NULL")       
3862
3863    vfs_inode_t * inode_ptr = GET_PTR( inode_xp );
3864    cxy_t         inode_cxy = GET_CXY( inode_xp );
3865
3866    // get local pointer on page mapper
3867    mapper_t * mapper = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->mapper ) );
3868
3869assert( (mapper != NULL) , "mapper pointer is NULL")       
3870
3871    // get FS type from mapper
3872    vfs_fs_type_t fs_type = hal_remote_l32( XPTR( inode_cxy , &mapper->type ) );
3873
3874    // call relevant FS function
3875    if( fs_type == FS_TYPE_FATFS )
3876    {
3877        error = fatfs_release_inode( inode_xp ); 
3878    }
3879    else if( fs_type == FS_TYPE_RAMFS )
3880    {
3881        assert( false , "should not be called for RAMFS" );
3882    }
3883    else if( fs_type == FS_TYPE_DEVFS )
3884    {
3885        assert( false , "should not be called for DEVFS" );
3886    }
3887    else
3888    {
3889        assert( false , "undefined file system type" );
3890    }
3891
3892    return error;
3893   
3894}  // end vfs_fs_release_inode()
3895
3896
Note: See TracBrowser for help on using the repository browser.