source: trunk/kernel/vfs/vfs.c @ 265

Last change on this file since 265 was 265, checked in by alain, 7 years ago

Fix several bugs in VFS.

File size: 55.6 KB
Line 
1/*
2 * vfs.c - Virtual File System implementation.
3 *
4 * Author  Mohamed Lamine Karaoui (2015)
5 *         Alain Greiner (2016)
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
26#include <kernel_config.h>
27#include <hal_types.h>
28#include <hal_atomic.h>
29#include <hal_special.h>
30#include <readlock.h>
31#include <spinlock.h>
32#include <printk.h>
33#include <list.h>
34#include <xlist.h>
35#include <slist.h>
36#include <xhtab.h>
37#include <rpc.h>
38#include <errno.h>
39#include <kmem.h>
40#include <mapper.h>
41#include <thread.h>
42#include <process.h>
43#include <vfs.h>
44#include <fatfs.h>
45#include <ramfs.h>
46#include <devfs.h>
47#include <syscalls.h>
48
49
50//////////////////////////////////////////////////////////////////////////////////////////
51//           Extern variables         
52//////////////////////////////////////////////////////////////////////////////////////////
53
54extern vfs_ctx_t   fs_context[FS_TYPES_NR];    // allocated in kernel_init.c
55
56 
57//////////////////////////////////////////////////////////////////////////////////////////
58//           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    spinlock_init( &vfs_ctx->lock );
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    spinlock_lock( &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 == -1 )   // no more free slot => error
94    {
95        // release lock
96        spinlock_unlock( &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        spinlock_unlock( &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//           Inode related functions
124//////////////////////////////////////////////////////////////////////////////////////////
125
126char * vfs_inode_type_str( uint32_t type )
127{
128    if     ( type == INODE_TYPE_FILE ) return "FILE";
129    else if( type == INODE_TYPE_DIR  ) return "DIR ";
130    else if( type == INODE_TYPE_FIFO ) return "FIFO";
131    else if( type == INODE_TYPE_PIPE ) return "PIPE";
132    else if( type == INODE_TYPE_SOCK ) return "SOCK";
133    else if( type == INODE_TYPE_DEV  ) return "DEV ";
134    else if( type == INODE_TYPE_SYML ) return "SYML";
135    else                               return "undefined";
136}
137
138//////////////////////////////////////////////////////
139error_t vfs_inode_create( xptr_t            dentry_xp,
140                          vfs_fs_type_t     fs_type,
141                          vfs_inode_type_t  inode_type,
142                          void            * extend,
143                          uint32_t          attr,
144                          uint32_t          rights,
145                          uid_t             uid,
146                          gid_t             gid,
147                          xptr_t          * inode_xp )
148{
149    mapper_t         * mapper;     // associated mapper( to be allocated)
150    vfs_inode_t      * inode;      // inode descriptor (to be allocated)
151    uint32_t           inum;       // inode identifier (to be allocated)
152    vfs_ctx_t        * ctx;        // file system context
153        kmem_req_t         req;        // request to kernel memory allocator
154    error_t            error;
155
156    // check fs type and get pointer on context
157    if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
158    else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
159    else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS];
160    else
161    {
162        ctx = NULL;
163                printk("\n[PANIC] in %s : illegal file system type = %d\n", __FUNCTION__ , fs_type );
164        hal_core_sleep();
165    }
166
167    // allocate inum
168    error = vfs_ctx_inum_alloc( ctx , &inum );
169
170    if( error )
171    {
172        printk("\n[ERROR] in %s : cannot allocate inum\n", __FUNCTION__ );
173        return ENOMEM;
174    }
175
176    // allocate memory for mapper
177    mapper = mapper_create( fs_type );
178
179    if( mapper == NULL )
180    {
181        printk("\n[ERROR] in %s : cannot allocate mapper\n", __FUNCTION__ );
182        vfs_ctx_inum_release( ctx , inum );
183        return ENOMEM;
184    }
185
186    // allocate memory for VFS inode descriptor
187        req.type  = KMEM_VFS_INODE;
188        req.size  = sizeof(vfs_inode_t);
189    req.flags = AF_KERNEL | AF_ZERO;
190        inode     = (vfs_inode_t *)kmem_alloc( &req );
191
192    if( inode == NULL )
193    {
194        printk("\n[ERROR] in %s : cannot allocate inode descriptor\n", __FUNCTION__ );
195        vfs_ctx_inum_release( ctx , inum );
196        mapper_destroy( mapper );
197        return ENOMEM;
198    }
199
200    // initialize inode descriptor
201    inode->gc         = 0;
202    inode->type       = inode_type;
203    inode->inum       = inum;
204    inode->attr       = attr;
205    inode->rights     = rights;
206    inode->uid        = uid;
207    inode->gid        = gid;
208    inode->refcount   = 0;
209    inode->parent_xp  = dentry_xp;
210    inode->ctx        = ctx;
211    inode->mapper     = mapper;
212    inode->extend     = extend;
213
214    // initialise inode field in mapper
215    mapper->inode     = inode;
216 
217    // initialise threads waiting queue
218    xlist_root_init( XPTR( local_cxy , &inode->wait_root ) );
219
220    // initialize dentries hash table
221    xhtab_init( &inode->children , XHTAB_DENTRY_TYPE );
222
223    // initialize inode locks
224    remote_rwlock_init( XPTR( local_cxy , &inode->data_lock ) );
225    remote_spinlock_init( XPTR( local_cxy , &inode->main_lock ) );
226
227    // return extended pointer on inode
228    *inode_xp = XPTR( local_cxy , inode );
229    return 0;
230
231}  // end vfs_inode_create() 
232
233/////////////////////////////////////////////
234void vfs_inode_destroy( vfs_inode_t * inode )
235{
236    if( inode->refcount )
237    {
238        printk("\n[PANIC] in %s : inode refcount non zero\n", __FUNCTION__ );
239        hal_core_sleep(); 
240    }       
241
242    // release memory allocated for mapper
243    mapper_destroy( inode->mapper );
244
245    // release memory allocate for inode descriptor
246        kmem_req_t req;
247        req.ptr   = inode;
248        req.type  = KMEM_VFS_INODE;
249        kmem_free( &req );
250
251}  // end vfs_inode_destroy()
252
253/////////////////////////////////////////////
254error_t vfs_inode_load( vfs_inode_t * parent,
255                        char        * name,
256                        xptr_t        child_xp )
257{
258    vfs_dmsg("\n[INFO] %s : enter for child <%s>\n",
259             __FUNCTION__ , name );
260
261    error_t error = 0;
262
263    assert( (parent != NULL) , __FUNCTION__ , "parent pointer is NULL\n");
264
265    assert( (child_xp != XPTR_NULL) , __FUNCTION__ , "child pointer is NULL\n");
266
267    // get parent inode FS type
268    vfs_fs_type_t fs_type = parent->ctx->type;
269
270    // call relevant FS function
271    if( fs_type == FS_TYPE_FATFS )
272    {
273        error = fatfs_inode_load( parent , name , child_xp );
274    }
275    else if( fs_type == FS_TYPE_RAMFS )
276    {
277        assert( false , __FUNCTION__ , "should not be called for RAMFS\n" );
278    }
279    else if( fs_type == FS_TYPE_DEVFS )
280    {
281        assert( false , __FUNCTION__ , "should not be called for DEVFS\n" );
282    }
283    else
284    {
285        assert( false , __FUNCTION__ , "undefined file system type\n" );
286    }
287
288    vfs_dmsg("\n[INFO] %s : exit for child <%s>\n",
289             __FUNCTION__ , name );
290
291    return error;
292
293} // end vfs_load_inode()
294
295////////////////////////////////////////////
296void vfs_inode_remote_up( xptr_t  inode_xp )
297{
298    // get inode cluster and local pointer
299    cxy_t         inode_cxy = GET_CXY( inode_xp );
300    vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
301
302    hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->refcount ) , 1 );   
303}
304
305//////////////////////////////////////////////
306void vfs_inode_remote_down( xptr_t  inode_xp )
307{
308    // get inode cluster and local pointer
309    cxy_t         inode_cxy = GET_CXY( inode_xp );
310    vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
311
312    hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->refcount ) , -1 );   
313}
314
315//////////////////////////////////////////////
316uint32_t vfs_inode_get_size( xptr_t inode_xp )
317{
318    // get inode cluster and local pointer
319    cxy_t         cxy = GET_CXY( inode_xp );
320    vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp );
321
322    // get size
323    remote_rwlock_rd_lock( XPTR( cxy , &ptr->data_lock ) );
324    uint32_t size = hal_remote_lw( XPTR( cxy , &ptr->size ) );
325    remote_rwlock_rd_unlock( XPTR( cxy , &ptr->data_lock ) );
326    return size;
327}
328
329////////////////////////////////////////////
330void vfs_inode_set_size( xptr_t    inode_xp,
331                              uint32_t  size )
332{
333    // get inode cluster and local pointer
334    cxy_t         cxy = GET_CXY( inode_xp );
335    vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp );
336
337    // set size
338    remote_rwlock_wr_unlock( XPTR( cxy , &ptr->data_lock ) );
339    hal_remote_sw( XPTR( cxy , &ptr->size ) , size );
340    remote_rwlock_wr_unlock( XPTR( cxy , &ptr->data_lock ) );
341}
342
343////////////////////////////////////////
344void vfs_inode_unlock( xptr_t inode_xp )
345{
346    // get inode cluster and local pointer
347    cxy_t         cxy = GET_CXY( inode_xp );
348    vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp );
349
350    // release the main lock
351    remote_spinlock_unlock( XPTR( cxy , &ptr->main_lock ) );
352}
353
354//////////////////////////////////////
355void vfs_inode_lock( xptr_t inode_xp )
356{
357    // get inode cluster and local pointer
358    cxy_t         cxy = GET_CXY( inode_xp );
359    vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp );
360
361    // get the main lock
362    remote_spinlock_lock( XPTR( cxy , &ptr->main_lock ) );
363}
364
365/////////////////////////////////////////
366xptr_t vfs_inode_owner( xptr_t inode_xp )
367{
368    // get inode cluster and local pointer
369    cxy_t         cxy = GET_CXY( inode_xp );
370    vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp );
371
372    // get the main lock
373    return remote_spinlock_owner( XPTR( cxy , &ptr->main_lock ) );
374}
375
376/////////////////////////////////////////
377void vfs_inode_display( xptr_t inode_xp )
378{
379    cxy_t          inode_cxy;
380    vfs_inode_t  * inode_ptr;
381    xptr_t         dentry_xp;
382    cxy_t          dentry_cxy;
383    vfs_dentry_t * dentry_ptr;
384   
385    char           name[CONFIG_VFS_MAX_NAME_LENGTH];
386
387    // get inode cluster and local pointer
388    inode_cxy = GET_CXY( inode_xp );
389    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
390
391    // get parent dentry
392    dentry_xp  = hal_remote_lwd( XPTR( inode_cxy , &inode_ptr->parent_xp ) );
393
394    // get local copy of name
395    if( dentry_xp == XPTR_NULL )  // it is the VFS root
396    {
397        strcpy( name , "/" );
398    }
399    else                          // not the VFS root
400    {
401        dentry_cxy = GET_CXY( dentry_xp );
402        dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
403
404        hal_remote_strcpy( XPTR( local_cxy  , name ) , 
405                           XPTR( dentry_cxy , &dentry_ptr->name ) );
406    }
407
408    // display inode header
409    printk("\n*** inode <%s> / inode_xp = %l / dentry_xp = %l ***\n",
410           name , inode_xp , dentry_xp );
411
412    // display children from xhtab
413    xhtab_display( XPTR( inode_cxy , &inode_ptr->children ) );
414
415}  // end vfs_inode_display()
416
417////////////////////////////////////////////////////////////////////////////////////////////
418//           Dentry related functions
419//////////////////////////////////////////////////////////////////////////////////////////
420
421///////////////////////////////////////////////////
422error_t vfs_dentry_create( vfs_fs_type_t   fs_type,
423                           char          * name,
424                           vfs_inode_t   * parent,
425                           xptr_t        * dentry_xp )
426{
427    vfs_ctx_t      * ctx;        // context descriptor
428    vfs_dentry_t   * dentry;     // dentry descriptor (to be allocated)
429        kmem_req_t       req;        // request to kernel memory allocator
430
431    // get pointer on context
432    if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
433    else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
434    else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS];
435    else
436    {
437        ctx = NULL;
438        printk("\n[PANIC] in %s : undefined file system type\n", __FUNCTION__ );
439        hal_core_sleep();
440    }
441
442    // get name length
443    uint32_t length = strlen( name );
444
445    if( length >= CONFIG_VFS_MAX_NAME_LENGTH )
446    {
447        printk("\n[ERROR] in %s : name too long\n", __FUNCTION__ );
448        return EINVAL;
449    }
450
451    // allocate memory for dentry descriptor
452        req.type  = KMEM_VFS_DENTRY;
453        req.size  = sizeof(vfs_dentry_t);
454    req.flags = AF_KERNEL | AF_ZERO;
455        dentry     = (vfs_dentry_t *)kmem_alloc( &req );
456
457    if( dentry == NULL )
458    {
459        printk("\n[ERROR] in %s : cannot allocate dentry descriptor\n", __FUNCTION__ );
460        return ENOMEM;
461    }
462
463    // initialize dentry descriptor
464
465    dentry->ctx     = ctx;
466    dentry->length  = length;
467    dentry->parent  = parent;
468    strcpy( dentry->name , name );
469
470    // register dentry in hash table rooted in parent inode
471    xhtab_insert( XPTR( local_cxy , &parent->children ),
472                  name, 
473                  XPTR( local_cxy , &dentry->list ) );
474
475    // return extended pointer on dentry
476    *dentry_xp = XPTR( local_cxy , dentry );
477
478    return 0;
479
480}  // end vfs_dentry_create()
481
482////////////////////////////////////////////////
483void vfs_dentry_destroy( vfs_dentry_t * dentry )
484{
485    if( dentry->refcount )
486    {
487        printk("\n[PANIC] in %s : dentry refcount non zero\n", __FUNCTION__ );
488        hal_core_sleep(); 
489    }       
490
491        kmem_req_t req;
492        req.ptr   = dentry;
493        req.type  = KMEM_VFS_DENTRY;
494        kmem_free( &req );
495}
496
497
498
499//////////////////////////////////////////////////////////////////////////////////////////
500//           File descriptor related functions
501//////////////////////////////////////////////////////////////////////////////////////////
502
503/////////////////////////////////////////////
504error_t vfs_file_create( vfs_inode_t * inode,
505                         uint32_t      attr,
506                         xptr_t      * file_xp )
507{
508    vfs_file_t  * file;
509        kmem_req_t    req;
510
511    // allocate memory for new file descriptor
512        req.type  = KMEM_VFS_FILE;
513        req.size  = sizeof(vfs_file_t);
514    req.flags = AF_KERNEL | AF_ZERO;
515        file      = (vfs_file_t *)kmem_alloc( &req );
516
517    if( file == NULL ) return ENOMEM;
518
519    // initializes new file descriptor
520    file->gc       = 0;
521    file->type     = inode->type;
522    file->attr     = attr;
523    file->offset   = 0;
524    file->refcount = 0;
525    file->inode    = inode;
526    file->ctx      = inode->ctx;
527    file->mapper   = inode->mapper;
528
529    remote_rwlock_init( XPTR( local_cxy , &file->lock ) );
530
531    *file_xp = XPTR( local_cxy , file );
532    return 0;
533
534}  // end vfs_file_create()
535
536///////////////////////////////////////////
537void vfs_file_destroy( vfs_file_t *  file )
538{
539    if( file->refcount )
540    {
541        printk("\n[PANIC] in %s : file refcount non zero\n", __FUNCTION__ );
542        hal_core_sleep(); 
543    }       
544
545        kmem_req_t req;
546        req.ptr   = file;
547        req.type  = KMEM_VFS_FILE;
548        kmem_free( &req );
549
550}  // end vfs_file_destroy()
551
552
553////////////////////////////////////////
554void vfs_file_count_up( xptr_t file_xp )
555{
556    // get file cluster and local pointer
557    cxy_t        file_cxy = GET_CXY( file_xp );
558    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); 
559
560    // atomically increment count
561    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , 1 ); 
562}
563
564//////////////////////////////////////////
565void vfs_file_count_down( xptr_t file_xp )
566{
567    // get file cluster and local pointer
568    cxy_t        file_cxy = GET_CXY( file_xp );
569    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); 
570
571    // atomically decrement count
572    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , -1 ); 
573}
574
575//////////////////////////////////////////////////////////////////////////////////////////
576//           File access related functions
577//////////////////////////////////////////////////////////////////////////////////////////
578
579////////////////////////////////////
580error_t vfs_open( xptr_t     cwd_xp,
581                          char     * path,
582                          uint32_t   flags,
583                  uint32_t   mode, 
584                          xptr_t   * new_file_xp,
585                  uint32_t * new_file_id )
586{
587    error_t       error;
588    xptr_t        inode_xp;     // extended pointer on target inode
589    cxy_t         inode_cxy;    // inode cluster identifier       
590    vfs_inode_t * inode_ptr;    // inode local pointer
591    uint32_t      file_attr;    // file descriptor attributes
592    uint32_t      lookup_mode;  // lookup working mode       
593    xptr_t        file_xp;      // extended pointer on created file descriptor
594    uint32_t      file_id;      // created file descriptor index in reference fd_array
595
596    vfs_dmsg("\n[INFO] %s : enters for <%s> at cycle %d\n",
597             __FUNCTION__ , path , (uint32_t)hal_time_stamp() );
598
599    // compute lookup working mode
600    lookup_mode = VFS_LOOKUP_OPEN;
601    if( (flags & O_DIR    )      )  lookup_mode |= VFS_LOOKUP_DIR;
602    if( (flags & O_CREAT  )      )  lookup_mode |= VFS_LOOKUP_CREATE;
603    if( (flags & O_EXCL   )      )  lookup_mode |= VFS_LOOKUP_EXCL;
604 
605    // compute attributes for the created file
606    file_attr = 0;
607    if( (flags & O_RDONLY ) == 0 )  file_attr |= FD_ATTR_READ_ENABLE;
608    if( (flags & O_WRONLY ) == 0 )  file_attr |= FD_ATTR_WRITE_ENABLE;
609    if( (flags & O_SYNC   )      )  file_attr |= FD_ATTR_SYNC;
610    if( (flags & O_APPEND )      )  file_attr |= FD_ATTR_APPEND;
611    if( (flags & O_CLOEXEC)      )  file_attr |= FD_ATTR_CLOSE_EXEC;
612
613    // get extended pointer on target inode
614    error = vfs_lookup( cwd_xp , path , lookup_mode , &inode_xp );
615
616    vfs_dmsg("\n[INFO] %s : get inode_xp = %l for <%s> at cycle %d\n",
617             __FUNCTION__ , inode_xp , path , hal_get_cycles() );
618
619    if( error ) return error;
620
621    // get target inode cluster and local pointer
622    inode_cxy = GET_CXY( inode_xp );
623    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
624   
625    // create a new file descriptor in cluster containing inode
626    if( inode_cxy == local_cxy )      // target cluster is local
627    {
628        error = vfs_file_create( inode_ptr , file_attr , &file_xp );
629    }
630    else                              // target cluster is remote
631    {
632        rpc_vfs_file_create_client( inode_cxy , inode_ptr , file_attr , &file_xp , &error );
633    }
634
635    if( error )  return error;
636
637    // allocate and register a new file descriptor index in reference cluster fd_array
638    error = process_fd_register( file_xp , &file_id );
639
640    if( error ) return error;
641
642    vfs_dmsg("\n[INFO] %s : exit for <%s> / file_id = %d / file_xp = %l / at cycle %d\n",
643             __FUNCTION__ , path , file_id , file_xp , hal_get_cycles() );
644
645    // success
646    *new_file_xp = file_xp;
647    *new_file_id = file_id;
648    return 0;
649
650}  // end vfs_open()
651
652/////////////////////////////////////
653error_t vfs_move( bool_t   to_buffer,
654                  bool_t   is_user,
655                  xptr_t   file_xp,
656                  void   * buffer,
657                  uint32_t size )
658{
659    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
660
661    cxy_t              file_cxy;     // remote file descriptor cluster
662    vfs_file_t       * file_ptr;     // remote file descriptor local pointer
663    vfs_inode_type_t   inode_type;
664    uint32_t           file_offset;  // current offset in file
665    mapper_t         * mapper;
666    error_t            error;
667
668    // get cluster and local pointer on remote file descriptor
669    file_cxy  = GET_CXY( file_xp );
670    file_ptr  = (vfs_file_t *)GET_PTR( file_xp );
671
672    // get inode type from remote file descriptor
673    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type   ) );
674   
675    // action depends on inode type
676    if( inode_type == INODE_TYPE_FILE )
677    {
678        // get mapper pointer and file offset from file descriptor
679        file_offset = hal_remote_lw( XPTR( file_cxy , &file_ptr->offset ) );
680        mapper = (mapper_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) );
681
682        // move data between mapper and buffer
683        if( file_cxy == local_cxy )
684        {
685            error = mapper_move_buffer( mapper,
686                                        to_buffer,
687                                        is_user,
688                                        file_offset,
689                                        buffer,
690                                        size );
691        }
692        else
693        {
694            rpc_mapper_move_buffer_client( file_cxy,
695                                           mapper,
696                                           to_buffer,
697                                           is_user,
698                                           file_offset,
699                                           buffer,
700                                           size,
701                                           &error );
702        } 
703
704        if( error ) return -1;
705        else        return size;
706    }
707    else 
708    {
709        printk("\n[ERROR] in %s : inode is not a file", __FUNCTION__ );
710        return -1;
711    }
712}  // end vfs_move()
713
714//////////////////////////////////////
715error_t vfs_lseek( xptr_t     file_xp,
716                   uint32_t   offset,
717                   uint32_t   whence, 
718                   uint32_t * new_offset )
719{
720    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
721    hal_core_sleep();
722    return 0;
723
724    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
725
726}  // vfs_lseek()
727
728///////////////////////////////////
729error_t vfs_close( xptr_t   file_xp,
730                   uint32_t file_id )
731{
732    assert( (file_xp != XPTR_NULL) , __FUNCTION__ , "file_xp == XPTR_NULL" );
733
734    assert( (file_id < CONFIG_PROCESS_FILE_MAX_NR) , __FUNCTION__ , "illegal file_id" );
735
736    thread_t  * this    = CURRENT_THREAD;
737    process_t * process = this->process;
738
739    // get cluster and local pointer on remote file descriptor
740    cxy_t        file_cxy = GET_CXY( file_xp );
741    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
742
743    // get local pointer on local cluster manager
744    cluster_t * cluster = LOCAL_CLUSTER;
745
746    // get owner process cluster and lpid
747    cxy_t   owner_cxy  = CXY_FROM_PID( process->pid );
748    lpid_t  lpid       = LPID_FROM_PID( process->pid );
749
750    // get extended pointers on copies root and lock
751    xptr_t root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] );
752    xptr_t lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] );
753
754    // take the lock protecting the copies
755    remote_spinlock_lock( lock_xp );
756
757    // 1) loop on the process descriptor copies to cancel all fd_array[file_id] entries
758    xptr_t  iter_xp;
759    XLIST_FOREACH( root_xp , iter_xp )
760    {
761        xptr_t      process_xp  = XLIST_ELEMENT( iter_xp , process_t , copies_list );
762        cxy_t       process_cxy = GET_CXY( process_xp );
763        process_t * process_ptr = (process_t *)GET_PTR( process_xp );
764
765        xptr_t lock_xp  = XPTR( process_cxy , &process_ptr->fd_array.lock );
766        xptr_t entry_xp = XPTR( process_cxy , &process_ptr->fd_array.array[file_id] );
767
768        // lock is required for atomic write of a 64 bits word
769        remote_rwlock_wr_lock( lock_xp );
770        hal_remote_swd( entry_xp , XPTR_NULL );
771        remote_rwlock_wr_unlock( lock_xp );
772
773        hal_fence();
774    }   
775
776    // 2) release memory allocated to file descriptor in remote cluster
777    if( file_cxy == local_cxy )             // file cluster is local
778    {
779        vfs_file_destroy( file_ptr );
780    }
781    else                                    // file cluster is local
782    {
783        rpc_vfs_file_destroy_client( file_cxy , file_ptr );
784    }
785
786    return 0;
787
788}  // end vfs_close()
789
790////////////////////////////////////
791error_t vfs_unlink( xptr_t   cwd_xp,
792                    char   * path )
793{
794    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
795    hal_core_sleep();
796    return 0;
797}  // vfs_unlink()
798
799///////////////////////////////////////
800error_t vfs_stat( xptr_t       file_xp,
801                  vfs_stat_t * k_stat )
802{
803    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
804    hal_core_sleep();
805    return 0;
806}
807
808////////////////////////////////////////////
809error_t vfs_readdir( xptr_t         file_xp,
810                     vfs_dirent_t * k_dirent )
811{
812    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
813    hal_core_sleep();
814    return 0;
815}
816
817//////////////////////////////////////
818error_t vfs_mkdir( xptr_t     file_xp,
819                   char     * path,
820                   uint32_t   mode )
821{
822    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
823    hal_core_sleep();
824    return 0;
825}
826
827////////////////////////////////////
828error_t vfs_rmdir( xptr_t   file_xp,
829                   char   * path )
830{
831    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
832    hal_core_sleep();
833    return 0;
834}
835
836///////////////////////////////////
837error_t vfs_chdir( xptr_t   cwd_xp,
838                   char   * path )
839{
840    error_t           error;
841    xptr_t            inode_xp;     // extended pointer on target inode
842    cxy_t             inode_cxy;    // target inode cluster identifier       
843    vfs_inode_t     * inode_ptr;    // target inode local pointer
844    uint32_t          mode;         // lookup working mode       
845    vfs_inode_type_t  inode_type;   // target inode type
846
847    // set lookup working mode
848    mode = 0;
849
850    // get extended pointer on target inode
851    error = vfs_lookup( cwd_xp , path , mode , &inode_xp );
852
853    if( error ) return error;
854
855    // get inode cluster and local pointer
856    inode_cxy = GET_CXY( inode_xp );
857    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
858
859    // get inode type from remote file
860    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
861
862    if( inode_type != INODE_TYPE_DIR )
863    {
864        CURRENT_THREAD->errno = ENOTDIR;
865        return -1;
866    }
867
868    printk("\n[PANIC] %s non fully implemented\n", __FUNCTION__ );
869    hal_core_sleep();
870    return 0;
871}
872
873///////////////////////////////////
874error_t vfs_chmod( xptr_t   cwd_xp,
875                   char   * path,
876                   uint32_t rights )
877{
878    error_t           error;
879    xptr_t            inode_xp;     // extended pointer on target inode
880    cxy_t             inode_cxy;    // inode cluster identifier       
881    vfs_inode_t     * inode_ptr;    // inode local pointer
882    uint32_t          mode;         // lookup working mode
883    vfs_inode_type_t  inode_type;   // target inode type
884
885    // set lookup working mode
886    mode = 0;
887 
888    // get extended pointer on target inode
889    error = vfs_lookup( cwd_xp , path , mode , &inode_xp );
890
891    if( error ) return error;
892
893    // get inode cluster and local pointer
894    inode_cxy = GET_CXY( inode_xp );
895    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
896   
897    // get inode type from remote inode
898    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
899
900   
901    printk("\n[PANIC] %s non fully implemented\n", __FUNCTION__ );
902    hal_core_sleep();
903    return 0;
904}
905
906///////////////////////////////////
907error_t vfs_mkfifo( xptr_t   cwd_xp,
908                    char   * path,
909                    uint32_t rights )
910{
911    printk("\n[PANIC] in %s : not implemented yet\n", __FUNCTION__ );
912    hal_core_sleep(); 
913    return 0;
914}
915
916
917
918//////////////////////////////////////////////////////////////////////////////////////////
919//            Inode Tree functions
920//////////////////////////////////////////////////////////////////////////////////////////
921
922/////////////////////////////////
923cxy_t vfs_cluster_random_select()
924{
925    uint32_t  x_size    = LOCAL_CLUSTER->x_size;
926    uint32_t  y_size    = LOCAL_CLUSTER->y_size;
927    uint32_t  y_width   = LOCAL_CLUSTER->y_width;
928    uint32_t  index     = ( hal_get_cycles() + hal_get_gid() ) % (x_size * y_size);
929    uint32_t  x         = index / y_size;   
930    uint32_t  y         = index % y_size;
931
932    return (x<<y_width) + y;
933}
934
935
936//////////////////////////////////////////////////////////////////////////
937// This static function is called by the vfs_display() function.
938//////////////////////////////////////////////////////////////////////////
939static void vfs_recursive_display( xptr_t   inode_xp,
940                                   xptr_t   name_xp,
941                                   xptr_t   dentry_xp,
942                                   uint32_t indent )
943{
944    cxy_t              inode_cxy;
945    vfs_inode_t      * inode_ptr;
946    vfs_inode_type_t   inode_type;
947    xptr_t             children_xp;    // extended pointer on children xhtab
948
949    xptr_t             child_dentry_xp;
950    cxy_t              child_dentry_cxy;
951    vfs_dentry_t     * child_dentry_ptr;
952    xptr_t             child_inode_xp;
953    xptr_t             child_dentry_name_xp;
954
955    char               name[CONFIG_VFS_MAX_NAME_LENGTH];
956
957    char *             indent_str[] = { "",                                  // level 0
958                                        "  ",                                // level 1
959                                        "    ",                              // level 2
960                                        "      ",                            // level 3
961                                        "        ",                          // level 4
962                                        "          ",                        // level 5
963                                        "            ",                      // level 6
964                                        "              ",                    // level 7
965                                        "                ",                  // level 8
966                                        "                  ",                // level 9
967                                        "                    ",              // level 10
968                                        "                      ",            // level 11
969                                        "                        ",          // level 12
970                                        "                          ",        // level 13
971                                        "                            ",      // level 14
972                                        "                              " };  // level 15
973
974    assert( (inode_xp != XPTR_NULL) , __FUNCTION__ , "inode_xp cannot be NULL\n" );
975    assert( (name_xp  != XPTR_NULL) , __FUNCTION__ , "name_xp cannot be NULL\n" );
976    assert( (indent < 16)           , __FUNCTION__ , "depth cannot be larger than 15\n" );
977   
978    // get inode cluster and local pointer
979    inode_cxy = GET_CXY( inode_xp );
980    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
981
982    // get inode type
983    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
984
985    // make a local copy of node name
986    hal_remote_strcpy( XPTR( local_cxy , name ) , name_xp );
987
988    // display inode
989    printk("%s%s <%s> inode_xp = %l / dentry_xp = %l\n",
990           indent_str[indent], vfs_inode_type_str( inode_type ), 
991           name , inode_xp , dentry_xp );
992
993    // scan directory entries 
994    if( inode_type == INODE_TYPE_DIR )
995    {
996        // get extended pointer on directory entries xhtab
997        children_xp =  XPTR( inode_cxy , &inode_ptr->children );
998
999        // get xhtab lock
1000        xhtab_read_lock( children_xp );
1001
1002        // get first dentry from xhtab
1003        child_dentry_xp = xhtab_get_first( children_xp );
1004
1005        while( child_dentry_xp != XPTR_NULL )
1006        {
1007            // get dentry cluster and local pointer
1008            child_dentry_cxy = GET_CXY( child_dentry_xp );
1009            child_dentry_ptr = (vfs_dentry_t *)GET_PTR( child_dentry_xp );
1010
1011            // get extended pointer on child inode
1012            child_inode_xp = hal_remote_lwd( XPTR( child_dentry_cxy,
1013                                                   &child_dentry_ptr->child_xp ) );
1014
1015            // get extended pointer on dentry name
1016            child_dentry_name_xp = XPTR( child_dentry_cxy , &child_dentry_ptr->name );
1017
1018            // recursive call on child inode
1019            vfs_recursive_display( child_inode_xp,
1020                                   child_dentry_name_xp,
1021                                   child_dentry_xp,
1022                                   indent+1 );
1023
1024            // get next dentry
1025            child_dentry_xp = xhtab_get_next( children_xp );
1026        }
1027
1028        // release xhtab lock
1029        xhtab_read_unlock( children_xp );
1030    }
1031}  // end vfs_recursive_display()
1032
1033///////////////////////////////////
1034void vfs_display( xptr_t inode_xp )
1035{
1036    xptr_t         name_xp;
1037    xptr_t         dentry_xp; 
1038    cxy_t          dentry_cxy;
1039    vfs_dentry_t * dentry_ptr;
1040
1041    // get target inode cluster and local pointer
1042    cxy_t         inode_cxy = GET_CXY( inode_xp );
1043    vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
1044
1045    // get extended pointer on associated dentry
1046    dentry_xp = hal_remote_lwd( XPTR( inode_cxy , &inode_ptr->parent_xp ) );
1047
1048    // check if target inode is the File System root
1049    if( dentry_xp == XPTR_NULL )
1050    {
1051        // build extended pointer on root name
1052        name_xp = XPTR( local_cxy , "/" );
1053    }
1054    else
1055    {
1056        // get dentry cluster and local pointer
1057        dentry_cxy = GET_CXY( dentry_xp );
1058        dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
1059
1060        // get extended pointer on dentry name
1061        name_xp = XPTR( dentry_cxy , &dentry_ptr->name );
1062    }
1063
1064    // print header
1065    printk("\n*** VFS ***\n");
1066
1067    // call recursive function
1068    vfs_recursive_display( inode_xp , name_xp , dentry_xp , 0 );
1069
1070}  // end vfs_display()
1071
1072//////////////////////////////////////////////////////////////////////////////////////////
1073// This function is used by the vfs_lookup() function.
1074// It takes an extended pointer on a remote inode (parent directory inode),
1075// and check access_rights violation for the calling thread.
1076// It can be used by any thread running in any cluster.
1077//////////////////////////////////////////////////////////////////////////////////////////
1078// @ inode_xp    : extended pointer on inode.
1079// @ client_uid  : client thread user ID
1080// @ client_gid  : client thread group ID
1081// @ return true if access rights are violated.
1082//////////////////////////////////////////////////////////////////////////////////////////
1083bool_t vfs_access_denied( xptr_t   inode_xp,
1084                          uint32_t client_uid,
1085                          uint32_t client_gid )
1086{
1087    // get found inode cluster and local pointer
1088    cxy_t         inode_cxy = GET_CXY( inode_xp );
1089    vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
1090
1091    // get inode access mode, UID, and GID
1092    // TODO uint32_t  mode = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->mode ) );
1093    uid_t     uid  = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->uid  ) );
1094    gid_t     gid  = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->gid  ) );
1095
1096    // FIXME : me must use mode
1097    if( (uid == client_uid) || (gid == client_gid) ) return false;
1098    else                                             return true;
1099}
1100
1101//////////////////////////////////////////////////////////////////////////////////////////
1102// This static function is used by the vfs_lookup() function.
1103// It takes an extended pointer on a remote parent directory inode, a directory
1104// entry name, and returns an extended pointer on the child inode.
1105// It can be used by any thread running in any cluster.
1106//////////////////////////////////////////////////////////////////////////////////////////
1107// @ parent_xp   : extended pointer on parent inode in remote cluster.
1108// @ name        : dentry name
1109// @ child_xp    : [out] buffer for extended pointer on child inode.
1110// @ return true if success / return false if not found.
1111//////////////////////////////////////////////////////////////////////////////////////////
1112static bool_t vfs_get_child( xptr_t   parent_xp,
1113                             char   * name,
1114                             xptr_t * child_xp )
1115{
1116    xptr_t  xhtab_xp;    // extended pointer on hash table containing children dentries
1117    xptr_t  dentry_xp;   // extended pointer on children dentry
1118
1119    // get parent inode cluster and local pointer
1120    cxy_t         parent_cxy = GET_CXY( parent_xp );
1121    vfs_inode_t * parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
1122
1123    // get extended pointer on hash table of children directory entries
1124    xhtab_xp = XPTR( parent_cxy , &parent_ptr->children );
1125
1126    // search extended pointer on matching dentry
1127    dentry_xp = xhtab_lookup( xhtab_xp , name );
1128
1129    if( dentry_xp == XPTR_NULL ) return false;
1130
1131    // get dentry cluster and local pointer
1132    cxy_t          dentry_cxy = GET_CXY( dentry_xp );
1133    vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
1134
1135    // return child inode
1136    *child_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , &dentry_ptr->child_xp ) );
1137    return true;
1138
1139}  // end vfs_get_child()
1140
1141//////////////////////////////////////////////////////////////////////////////////////////
1142// This static function is used by the vfs_lookup() function.
1143// It takes the <current> pointer on a buffer containing a complete pathname, and return
1144// in the <name> buffer, allocated by the caller, a single name in the path.
1145// It return also in the <next> pointer the next character to analyse in the path.
1146// Finally it returns a <last> boolean, that is true when the returned <name> is the
1147// last name in the path. The names are supposed to be separated by one or several '/'
1148// characters, that are not written in  the <name> buffer.
1149//////////////////////////////////////////////////////////////////////////////////////////
1150// @ current   : pointer on first character to analyse in buffer containing the path.
1151// @ name      : [out] pointer on buffer allocated by the caller for the returned name.
1152// @ next      : [out] pointer on next character to analyse in buffer containing the path.
1153// @ last      : [out] true if the returned name is the last (NUL character found).
1154// @ return 0 if success / return EINVAL if string empty (first chracter is NUL).
1155//////////////////////////////////////////////////////////////////////////////////////////
1156static error_t vfs_get_name_from_path( char     * current,
1157                                       char     * name,
1158                                       char    ** next,
1159                                       bool_t   * last )
1160{
1161    char * ptr = current;
1162
1163    // skip leading '/' characters
1164    while( *ptr == '/' ) ptr++;
1165
1166    // return EINVAL if string empty
1167    if( *ptr == 0 ) return EINVAL;
1168
1169    // copy all characters in name until NUL or '/'
1170    while( (*ptr != 0) && (*ptr !='/') )  *(name++) = *(ptr++);
1171
1172    // set NUL terminating character in name buffer
1173    *(name++) = 0;
1174
1175    // return last an next
1176    if( *ptr == 0 )             // last found character is NUL => last name in path
1177    {
1178        *last = true;
1179    }
1180    else                        // last found character is '/' => skip it
1181    {
1182        *last = false;
1183        *next = ptr + 1;
1184    }
1185
1186    return 0;
1187
1188}  // end vfs_get name_from_path()
1189   
1190//////////////////////////////////////////////
1191error_t vfs_lookup( xptr_t             cwd_xp,
1192                    char             * pathname,
1193                    uint32_t           mode,
1194                                        xptr_t           * inode_xp )
1195{
1196    char               name[CONFIG_VFS_MAX_NAME_LENGTH];   // one name in path
1197
1198    xptr_t             parent_xp;    // extended pointer on parent inode
1199    cxy_t              parent_cxy;   // cluster for parent inode
1200    vfs_inode_t      * parent_ptr;   // local pointer on parent inode 
1201    xptr_t             child_xp;     // extended pointer on child inode
1202    cxy_t              child_cxy;    // cluster for child inode
1203    vfs_inode_t      * child_ptr;    // local pointer on child inode 
1204    vfs_inode_type_t   child_type;   // child inode type
1205    vfs_fs_type_t      fs_type;      // File system type
1206    vfs_ctx_t        * ctx_ptr;      // local pointer on FS context
1207    char             * current;      // current pointer on path
1208    char             * next;         // next value for current pointer   
1209    bool_t             last;         // true when the name is the last in path
1210    bool_t             found;        // true when a child has been found
1211    thread_t         * this;         // pointer on calling thread descriptor
1212    process_t        * process;      // pointer on calling process descriptor
1213    error_t            error;
1214
1215    vfs_dmsg("\n[INFO] %s : enters for <%s> at cycle %d\n",
1216             __FUNCTION__ , pathname , (uint32_t)hal_time_stamp() );
1217
1218    this    = CURRENT_THREAD;
1219    process = this->process;
1220
1221    // get extended pointer on first inode to search
1222    if( pathname[0] == '/' ) parent_xp = process->vfs_root_xp;
1223    else                     parent_xp = cwd_xp;
1224
1225    // initialise other loop variables
1226    current  = pathname;
1227    next     = NULL;
1228    last     = false;
1229    child_xp = XPTR_NULL;
1230
1231    // take lock on parent inode
1232    vfs_inode_lock( parent_xp );
1233
1234    // load from device if one intermediate node not found
1235    // exit while loop when last name found (i.e. last == true)
1236    do
1237    {
1238        // get one name from path, and the "last" flag
1239        vfs_get_name_from_path( current , name , &next , &last );
1240
1241        vfs_dmsg("\n[INFO] %s : looking for <%s> / last = %d\n",
1242                 __FUNCTION__ , name , last );
1243
1244        // search a child dentry matching name in parent inode
1245        found = vfs_get_child( parent_xp,
1246                               name,
1247                               &child_xp );
1248
1249        // if a child inode is not found in the inode tree:
1250        // - we create the missing inode/dentry couple in the inode tree,
1251        // - we scan the parent mapper to complete the child inode (type and extension),
1252        // - we return an error if child not found on device.
1253        // - if the missing child is a directory, we load the child mapper from device
1254
1255        // for the last name, the behaviour depends on the "mode" argument:
1256
1257        if (found == false ) // child node not found in inode tree
1258        {
1259            vfs_dmsg("\n[INFO] %s : <%s> not found, try to load it\n",
1260                     __FUNCTION__ , name );
1261
1262            // release lock on parent inode
1263            vfs_inode_unlock( parent_xp );
1264
1265            // get parent inode FS type
1266            parent_cxy = GET_CXY( parent_xp );
1267            parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
1268
1269            ctx_ptr    = (vfs_ctx_t *)hal_remote_lpt( XPTR( parent_cxy ,
1270                                                            &parent_ptr->ctx ) );
1271            fs_type    = hal_remote_lw( XPTR( parent_cxy , &ctx_ptr->type ) );
1272
1273            // select a cluster for missing inode
1274            child_cxy = vfs_cluster_random_select();
1275                     
1276            // insert a new child dentry/inode in parent inode
1277            error = vfs_add_child_in_parent( child_cxy,
1278                                             INODE_TYPE_DIR,
1279                                             fs_type, 
1280                                             parent_xp, 
1281                                             name, 
1282                                             NULL,     // fs_type_specific inode extend
1283                                             &child_xp );
1284            if( error )
1285            {
1286                printk("\n[ERROR] in %s : no memory for inode %s in path %s\n",
1287                       __FUNCTION__ , name , pathname );
1288                return ENOMEM;
1289            }
1290
1291            // scan parent mapper to complete the missing inode
1292            if( parent_cxy == local_cxy )
1293            {
1294                error = vfs_inode_load( parent_ptr,
1295                                        name,
1296                                        child_xp );
1297            }
1298            else
1299            {
1300                rpc_vfs_inode_load_client( parent_cxy,
1301                                           parent_ptr,
1302                                           name,
1303                                           child_xp,
1304                                           &error );
1305            }
1306
1307            if ( error )
1308            {
1309                printk("\n[ERROR] in %s : node %s not found in path %s\n",
1310                       __FUNCTION__ , name , pathname );
1311                return ENOENT;
1312            }
1313
1314            // get child inode type
1315            child_ptr  = (vfs_inode_t *)GET_PTR( child_xp );
1316            child_type = hal_remote_lw( XPTR( child_cxy , &child_ptr->type ) );
1317
1318            // load child mapper from device if it is a directory
1319            if( child_type == INODE_TYPE_DIR )
1320            {
1321                if( child_cxy == local_cxy )
1322                {
1323                    error = vfs_mapper_load_all( child_ptr );
1324                }
1325                else
1326                {
1327                    rpc_vfs_mapper_load_all_client( child_cxy,
1328                                                    child_ptr,
1329                                                    &error );
1330                }
1331
1332                if ( error )
1333                {
1334                    printk("\n[ERROR] in %s : cannot access device for node %s in path %s\n",
1335                           __FUNCTION__ , name , pathname );
1336                    return EIO;
1337                }
1338            }
1339
1340            // TODO handle lookup mode here [AG]
1341
1342            // take lock on parent inode
1343            vfs_inode_lock( parent_xp );
1344        }
1345
1346        vfs_dmsg("\n[INFO] %s : found <%s> / parent = %l / child = %l / last = %d\n",
1347                     __FUNCTION__ , name , parent_xp , child_xp , last );
1348
1349        // TODO check access rights
1350        // error = vfs_access_denied( child_xp,
1351        //                            client_uid,
1352        //                            client_gid );
1353        // if( error )
1354        // {
1355        //     printk("\n[ERROR] in %s : permission denied for %s\n", __FUNCTION__ , name );
1356        //     return EACCES;
1357        // }
1358
1359        // take lock on child inode and release lock on parent
1360        vfs_inode_lock( child_xp );
1361        vfs_inode_unlock( parent_xp );
1362
1363        // update loop variables
1364        parent_xp = child_xp;
1365        current   = next;
1366    }
1367    while( last == false );
1368
1369    // release lock
1370    vfs_inode_unlock( parent_xp );
1371
1372    vfs_dmsg("\n[INFO] in %s : exit <%s> found / inode = %l\n",
1373                 __FUNCTION__ , pathname , child_xp );
1374
1375    // return searched pointer
1376    *inode_xp = child_xp;
1377
1378    return 0;
1379
1380}  // end vfs_lookup()
1381
1382////////////////////////////////////////////
1383error_t vfs_get_path( xptr_t    searched_xp,
1384                      char    * buffer,
1385                      uint32_t  max_size )
1386{
1387        xptr_t       dentry_xp;   // extended pointer on current dentry
1388    char       * name;        // local pointer on current dentry name
1389        uint32_t     length;      // length of current dentry name
1390        uint32_t     count;       // number of characters written in buffer
1391        uint32_t     index;       // slot index in buffer
1392    xptr_t       inode_xp;    // extended pointer on   
1393
1394    // implementation note:
1395    // we use two variables "index" and "count" because the buffer
1396    // is actually written in decreasing index order (from leaf to root)
1397    // TODO : handle conflict with a concurrent rename
1398    // FIXME : handle synchro in the loop ... [AG]
1399
1400        // set the NUL character in buffer / initialise buffer index and count
1401        buffer[max_size - 1] = 0;
1402        count    = 1;
1403    index    = max_size - 2;
1404
1405    // initialize current inode
1406    inode_xp  = searched_xp;
1407
1408    // exit when root inode found (i.e. dentry_xp == XPTR_NULL)
1409        do
1410    {
1411        // get inode cluster and local pointer
1412        cxy_t         inode_cxy = GET_CXY( inode_xp );
1413        vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
1414
1415        // get extended pointer on parent dentry               
1416        dentry_xp = (xptr_t)hal_remote_lwd( XPTR( inode_cxy , inode_ptr->parent_xp ) );
1417
1418        // get dentry cluster and local pointer
1419        cxy_t          dentry_cxy = GET_CXY( dentry_xp );
1420        vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
1421
1422        // get dentry name length and pointer
1423        length =  hal_remote_lw( XPTR( dentry_cxy , &dentry_ptr->length ) );
1424        name   = (char *)hal_remote_lpt( XPTR( dentry_cxy , &dentry_ptr->name ) );
1425
1426        // update index and count
1427        index -= (length + 1); 
1428        count += (length + 1);
1429
1430        // check buffer overflow
1431        if( count >= max_size )
1432        {
1433            printk("\n[ERROR] in %s : kernel buffer too small\n", __FUNCTION__ );
1434            return EINVAL;
1435        }
1436
1437        // update pathname
1438        hal_remote_memcpy( XPTR( local_cxy , &buffer[index + 1] ) ,
1439                           XPTR( dentry_cxy , name ) , length );
1440                buffer[index] = '/';
1441
1442                // get extended pointer on next inode
1443        inode_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , dentry_ptr->parent ) );
1444    }
1445    while( (dentry_xp != XPTR_NULL) );
1446
1447        return 0;
1448
1449}  // end vfs_get_path()
1450
1451     
1452//////////////////////////////////////////////////////////////
1453error_t vfs_add_child_in_parent( cxy_t              child_cxy,
1454                                 vfs_inode_type_t   inode_type,
1455                                 vfs_fs_type_t      fs_type,
1456                                 xptr_t             parent_xp,
1457                                 char             * name,
1458                                 void             * extend,
1459                                 xptr_t           * child_xp )
1460{
1461    error_t         error;
1462    xptr_t          dentry_xp;   // extended pointer on created dentry
1463    xptr_t          inode_xp;    // extended pointer on created inode
1464    cxy_t           parent_cxy;  // parent inode cluster identifier
1465    vfs_inode_t   * parent_ptr;  // parent inode local pointer
1466
1467    // get parent inode cluster and local pointer
1468    parent_cxy = GET_CXY( parent_xp );
1469    parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
1470
1471    // 1. create dentry
1472    if( parent_cxy == local_cxy )      // parent cluster is the local cluster
1473    {
1474        error = vfs_dentry_create( fs_type,
1475                                   name,
1476                                   parent_ptr,
1477                                   &dentry_xp );
1478    }
1479    else                               // parent cluster is remote
1480    {
1481        rpc_vfs_dentry_create_client( parent_cxy,
1482                                      fs_type,
1483                                      name,
1484                                      parent_ptr,
1485                                      &dentry_xp,
1486                                      &error );
1487    }
1488                                     
1489    if( error )
1490    {
1491        printk("\n[ERROR] in %s : cannot create dentry in cluster %x\n",
1492               __FUNCTION__ , parent_cxy );
1493        return ENOMEM;
1494    }
1495
1496    // 2. create child inode TODO : define attr / mode / uid / gid
1497    uint32_t attr = 0;
1498    uint32_t mode = 0;
1499    uint32_t uid  = 0;
1500    uint32_t gid  = 0;
1501   
1502    if( child_cxy == local_cxy )      // child cluster is the local cluster
1503    {
1504        error = vfs_inode_create( dentry_xp,
1505                                  fs_type,
1506                                  inode_type,
1507                                  extend,
1508                                  attr,
1509                                  mode,
1510                                  uid,
1511                                  gid,
1512                                  &inode_xp );
1513    }
1514    else                              // child cluster is remote
1515    {
1516        rpc_vfs_inode_create_client( child_cxy,
1517                                     dentry_xp,
1518                                     fs_type,
1519                                     inode_type,
1520                                     extend,
1521                                     attr,
1522                                     mode,
1523                                     uid,
1524                                     gid,
1525                                     &inode_xp,
1526                                     &error );
1527    }
1528                                     
1529    if( error )
1530    {
1531        printk("\n[ERROR] in %s : cannot create inode in cluster %x\n",
1532               __FUNCTION__ , child_cxy );
1533 
1534        vfs_dentry_t * dentry = (vfs_dentry_t *)GET_PTR( dentry_xp );
1535        if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry );
1536        else rpc_vfs_dentry_destroy_client( parent_cxy , dentry );
1537        return ENOMEM;
1538    }
1539
1540    // 3. update extended pointer on inode in dentry
1541    cxy_t          dentry_cxy = GET_CXY( dentry_xp );
1542    vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
1543    hal_remote_swd( XPTR( dentry_cxy , &dentry_ptr->child_xp ) , inode_xp );
1544
1545    // success : return extended pointer on child inode
1546    *child_xp = inode_xp;
1547    return 0;
1548
1549}  // end vfs_add_child_in_parent()
1550
1551//////////////////////////////////////////////////////////////////////////////////////////
1552//            Mapper related functions
1553//////////////////////////////////////////////////////////////////////////////////////////
1554
1555////////////////////////////////////////////
1556error_t vfs_mapper_move_page( page_t * page,
1557                              bool_t   to_mapper )
1558{
1559    error_t error = 0;
1560
1561    assert( (page != NULL) , __FUNCTION__ , "page pointer is NULL\n" );
1562
1563    mapper_t    * mapper = page->mapper;
1564
1565
1566    assert( (mapper != NULL) , __FUNCTION__ , "no mapper for page\n" );
1567
1568    vfs_dmsg("\n[INFO] %s : enters for page = %d in mapper = %x\n",
1569             __FUNCTION__ , page->index , mapper );
1570
1571    // get FS type
1572    vfs_fs_type_t fs_type = mapper->type;
1573
1574    // call relevant FS function
1575    if( fs_type == FS_TYPE_FATFS )
1576    {
1577        rwlock_wr_lock( &mapper->lock );
1578        error = fatfs_mapper_move_page( page , to_mapper ); 
1579        rwlock_wr_unlock( &mapper->lock );
1580    }
1581    else if( fs_type == FS_TYPE_RAMFS )
1582    {
1583        assert( false , __FUNCTION__ , "should not be called for RAMFS\n" );
1584    }
1585    else if( fs_type == FS_TYPE_DEVFS )
1586    {
1587        assert( false , __FUNCTION__ , "should not be called for DEVFS\n" );
1588    }
1589    else
1590    {
1591        assert( false , __FUNCTION__ , "undefined file system type\n" );
1592    }
1593
1594    vfs_dmsg("\n[INFO] %s : exit for page = %d in mapper = %x\n",
1595             __FUNCTION__ , page->index , mapper );
1596
1597    return error;
1598
1599}  // end vfs_move_page()
1600
1601//////////////////////////////////////////////////
1602error_t vfs_mapper_load_all( vfs_inode_t * inode )
1603{
1604    assert( (inode != NULL) , __FUNCTION__ , "inode pointer is NULL\n" );
1605
1606    uint32_t   index;
1607    page_t   * page;
1608
1609    mapper_t * mapper = inode->mapper;
1610    uint32_t   size   = inode->size;
1611
1612    assert( (mapper != NULL) , __FUNCTION__ , "mapper pointer is NULL\n" );
1613
1614    uint32_t npages = size >> CONFIG_PPM_PAGE_SHIFT;
1615    if( (size & CONFIG_PPM_PAGE_MASK) || (size == 0) ) npages++;
1616
1617    // loop on pages
1618    for( index = 0 ; index < npages ; index ++ )
1619    {
1620        // this function allocates the missing page in mapper,
1621        // and call the vfs_mapper_move_page() to load the page from device
1622        page = mapper_get_page( mapper , index );
1623
1624        if( page == NULL ) return EIO;
1625    }
1626
1627    return 0;
1628
1629}  // end vfs_mapper_load_all()
1630
Note: See TracBrowser for help on using the repository browser.