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

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

Fix a major bug in FATFS : miss handling in the FAT mapper.

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                  xptr_t   file_xp,
655                  void   * buffer,
656                  uint32_t size )
657{
658    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
659
660    cxy_t              file_cxy;     // remote file descriptor cluster
661    vfs_file_t       * file_ptr;     // remote file descriptor local pointer
662    vfs_inode_type_t   inode_type;
663    uint32_t           file_offset;  // current offset in file
664    mapper_t         * mapper;
665    error_t            error;
666
667    // get cluster and local pointer on remote file descriptor
668    file_cxy  = GET_CXY( file_xp );
669    file_ptr  = (vfs_file_t *)GET_PTR( file_xp );
670
671    // get inode type from remote file descriptor
672    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type   ) );
673   
674    // action depends on inode type
675    if( inode_type == INODE_TYPE_FILE )
676    {
677        // get mapper pointer and file offset from file descriptor
678        file_offset = hal_remote_lw( XPTR( file_cxy , &file_ptr->offset ) );
679        mapper = (mapper_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) );
680
681        // move data between mapper and buffer
682        if( file_cxy == local_cxy )
683        {
684            error = mapper_move( mapper,
685                                 to_buffer,
686                                 file_offset,
687                                 buffer,
688                                 size );
689        }
690        else
691        {
692            rpc_mapper_move_client( file_cxy,
693                                    mapper,
694                                    to_buffer,
695                                    file_offset,
696                                    buffer,
697                                    size,
698                                    &error );
699        } 
700
701        return error;
702    }
703    else if (inode_type == INODE_TYPE_DIR )
704    {
705        printk("\n[ERROR] in %s : inode is a directory", __FUNCTION__ );
706        return EINVAL;
707    }
708    else if (inode_type == INODE_TYPE_DEV )
709    {
710        // TODO  [AG]
711        return 0;
712    }
713    else
714    {
715        printk("\n[PANIC] in %s : illegal inode type\n", __FUNCTION__ );
716        hal_core_sleep();
717        return -1;
718    }
719}  // end vfs_move()
720
721//////////////////////////////////////
722error_t vfs_lseek( xptr_t     file_xp,
723                   uint32_t   offset,
724                   uint32_t   whence, 
725                   uint32_t * new_offset )
726{
727    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
728    hal_core_sleep();
729    return 0;
730
731    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
732
733}  // vfs_lseek()
734
735///////////////////////////////////
736error_t vfs_close( xptr_t   file_xp,
737                   uint32_t file_id )
738{
739    assert( (file_xp != XPTR_NULL) , __FUNCTION__ , "file_xp == XPTR_NULL" );
740
741    assert( (file_id < CONFIG_PROCESS_FILE_MAX_NR) , __FUNCTION__ , "illegal file_id" );
742
743    thread_t  * this    = CURRENT_THREAD;
744    process_t * process = this->process;
745
746    // get cluster and local pointer on remote file descriptor
747    cxy_t        file_cxy = GET_CXY( file_xp );
748    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
749
750    // get local pointer on local cluster manager
751    cluster_t * cluster = LOCAL_CLUSTER;
752
753    // get owner process cluster and lpid
754    cxy_t   owner_cxy  = CXY_FROM_PID( process->pid );
755    lpid_t  lpid       = LPID_FROM_PID( process->pid );
756
757    // get extended pointers on copies root and lock
758    xptr_t root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] );
759    xptr_t lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] );
760
761    // take the lock protecting the copies
762    remote_spinlock_lock( lock_xp );
763
764    // 1) loop on the process descriptor copies to cancel all fd_array[file_id] entries
765    xptr_t  iter_xp;
766    XLIST_FOREACH( root_xp , iter_xp )
767    {
768        xptr_t      process_xp  = XLIST_ELEMENT( iter_xp , process_t , copies_list );
769        cxy_t       process_cxy = GET_CXY( process_xp );
770        process_t * process_ptr = (process_t *)GET_PTR( process_xp );
771
772        xptr_t lock_xp  = XPTR( process_cxy , &process_ptr->fd_array.lock );
773        xptr_t entry_xp = XPTR( process_cxy , &process_ptr->fd_array.array[file_id] );
774
775        // lock is required for atomic write of a 64 bits word
776        remote_rwlock_wr_lock( lock_xp );
777        hal_remote_swd( entry_xp , XPTR_NULL );
778        remote_rwlock_wr_unlock( lock_xp );
779
780        hal_fence();
781    }   
782
783    // 2) release memory allocated to file descriptor in remote cluster
784    if( file_cxy == local_cxy )             // file cluster is local
785    {
786        vfs_file_destroy( file_ptr );
787    }
788    else                                    // file cluster is local
789    {
790        rpc_vfs_file_destroy_client( file_cxy , file_ptr );
791    }
792
793    return 0;
794
795}  // end vfs_close()
796
797////////////////////////////////////
798error_t vfs_unlink( xptr_t   cwd_xp,
799                    char   * path )
800{
801    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
802    hal_core_sleep();
803    return 0;
804}  // vfs_unlink()
805
806///////////////////////////////////////
807error_t vfs_stat( xptr_t       file_xp,
808                  vfs_stat_t * k_stat )
809{
810    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
811    hal_core_sleep();
812    return 0;
813}
814
815////////////////////////////////////////////
816error_t vfs_readdir( xptr_t         file_xp,
817                     vfs_dirent_t * k_dirent )
818{
819    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
820    hal_core_sleep();
821    return 0;
822}
823
824//////////////////////////////////////
825error_t vfs_mkdir( xptr_t     file_xp,
826                   char     * path,
827                   uint32_t   mode )
828{
829    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
830    hal_core_sleep();
831    return 0;
832}
833
834////////////////////////////////////
835error_t vfs_rmdir( xptr_t   file_xp,
836                   char   * path )
837{
838    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
839    hal_core_sleep();
840    return 0;
841}
842
843///////////////////////////////////
844error_t vfs_chdir( xptr_t   cwd_xp,
845                   char   * path )
846{
847    error_t           error;
848    xptr_t            inode_xp;     // extended pointer on target inode
849    cxy_t             inode_cxy;    // target inode cluster identifier       
850    vfs_inode_t     * inode_ptr;    // target inode local pointer
851    uint32_t          mode;         // lookup working mode       
852    vfs_inode_type_t  inode_type;   // target inode type
853
854    // set lookup working mode
855    mode = 0;
856
857    // get extended pointer on target inode
858    error = vfs_lookup( cwd_xp , path , mode , &inode_xp );
859
860    if( error ) return error;
861
862    // get inode cluster and local pointer
863    inode_cxy = GET_CXY( inode_xp );
864    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
865
866    // get inode type from remote file
867    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
868
869    if( inode_type != INODE_TYPE_DIR )
870    {
871        CURRENT_THREAD->errno = ENOTDIR;
872        return -1;
873    }
874
875    printk("\n[PANIC] %s non fully implemented\n", __FUNCTION__ );
876    hal_core_sleep();
877    return 0;
878}
879
880///////////////////////////////////
881error_t vfs_chmod( xptr_t   cwd_xp,
882                   char   * path,
883                   uint32_t rights )
884{
885    error_t           error;
886    xptr_t            inode_xp;     // extended pointer on target inode
887    cxy_t             inode_cxy;    // inode cluster identifier       
888    vfs_inode_t     * inode_ptr;    // inode local pointer
889    uint32_t          mode;         // lookup working mode
890    vfs_inode_type_t  inode_type;   // target inode type
891
892    // set lookup working mode
893    mode = 0;
894 
895    // get extended pointer on target inode
896    error = vfs_lookup( cwd_xp , path , mode , &inode_xp );
897
898    if( error ) return error;
899
900    // get inode cluster and local pointer
901    inode_cxy = GET_CXY( inode_xp );
902    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
903   
904    // get inode type from remote inode
905    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
906
907   
908    printk("\n[PANIC] %s non fully implemented\n", __FUNCTION__ );
909    hal_core_sleep();
910    return 0;
911}
912
913///////////////////////////////////
914error_t vfs_mkfifo( xptr_t   cwd_xp,
915                    char   * path,
916                    uint32_t rights )
917{
918    printk("\n[PANIC] in %s : not implemented yet\n", __FUNCTION__ );
919    hal_core_sleep(); 
920    return 0;
921}
922
923
924
925//////////////////////////////////////////////////////////////////////////////////////////
926//            Inode Tree functions
927//////////////////////////////////////////////////////////////////////////////////////////
928
929/////////////////////////////////
930cxy_t vfs_cluster_random_select()
931{
932    uint32_t  x_size    = LOCAL_CLUSTER->x_size;
933    uint32_t  y_size    = LOCAL_CLUSTER->y_size;
934    uint32_t  y_width   = LOCAL_CLUSTER->y_width;
935    uint32_t  index     = ( hal_get_cycles() + hal_get_gid() ) % (x_size * y_size);
936    uint32_t  x         = index / y_size;   
937    uint32_t  y         = index % y_size;
938
939    return (x<<y_width) + y;
940}
941
942
943//////////////////////////////////////////////////////////////////////////
944// This static function is called by the vfs_display() function.
945//////////////////////////////////////////////////////////////////////////
946static void vfs_recursive_display( xptr_t   inode_xp,
947                                   xptr_t   name_xp,
948                                   xptr_t   dentry_xp,
949                                   uint32_t indent )
950{
951    cxy_t              inode_cxy;
952    vfs_inode_t      * inode_ptr;
953    vfs_inode_type_t   inode_type;
954    xptr_t             children_xp;    // extended pointer on children xhtab
955
956    xptr_t             child_dentry_xp;
957    cxy_t              child_dentry_cxy;
958    vfs_dentry_t     * child_dentry_ptr;
959    xptr_t             child_inode_xp;
960    xptr_t             child_dentry_name_xp;
961
962    char               name[CONFIG_VFS_MAX_NAME_LENGTH];
963
964    char *             indent_str[] = { "",                                  // level 0
965                                        "  ",                                // level 1
966                                        "    ",                              // level 2
967                                        "      ",                            // level 3
968                                        "        ",                          // level 4
969                                        "          ",                        // level 5
970                                        "            ",                      // level 6
971                                        "              ",                    // level 7
972                                        "                ",                  // level 8
973                                        "                  ",                // level 9
974                                        "                    ",              // level 10
975                                        "                      ",            // level 11
976                                        "                        ",          // level 12
977                                        "                          ",        // level 13
978                                        "                            ",      // level 14
979                                        "                              " };  // level 15
980
981    assert( (inode_xp != XPTR_NULL) , __FUNCTION__ , "inode_xp cannot be NULL\n" );
982    assert( (name_xp  != XPTR_NULL) , __FUNCTION__ , "name_xp cannot be NULL\n" );
983    assert( (indent < 16)           , __FUNCTION__ , "depth cannot be larger than 15\n" );
984   
985    // get inode cluster and local pointer
986    inode_cxy = GET_CXY( inode_xp );
987    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
988
989    // get inode type
990    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
991
992    // make a local copy of node name
993    hal_remote_strcpy( XPTR( local_cxy , name ) , name_xp );
994
995    // display inode
996    printk("%s%s <%s> inode_xp = %l / dentry_xp = %l\n",
997           indent_str[indent], vfs_inode_type_str( inode_type ), 
998           name , inode_xp , dentry_xp );
999
1000    // scan directory entries 
1001    if( inode_type == INODE_TYPE_DIR )
1002    {
1003        // get extended pointer on directory entries xhtab
1004        children_xp =  XPTR( inode_cxy , &inode_ptr->children );
1005
1006        // get xhtab lock
1007        xhtab_read_lock( children_xp );
1008
1009        // get first dentry from xhtab
1010        child_dentry_xp = xhtab_get_first( children_xp );
1011
1012        while( child_dentry_xp != XPTR_NULL )
1013        {
1014            // get dentry cluster and local pointer
1015            child_dentry_cxy = GET_CXY( child_dentry_xp );
1016            child_dentry_ptr = (vfs_dentry_t *)GET_PTR( child_dentry_xp );
1017
1018            // get extended pointer on child inode
1019            child_inode_xp = hal_remote_lwd( XPTR( child_dentry_cxy,
1020                                                   &child_dentry_ptr->child_xp ) );
1021
1022            // get extended pointer on dentry name
1023            child_dentry_name_xp = XPTR( child_dentry_cxy , &child_dentry_ptr->name );
1024
1025            // recursive call on child inode
1026            vfs_recursive_display( child_inode_xp,
1027                                   child_dentry_name_xp,
1028                                   child_dentry_xp,
1029                                   indent+1 );
1030
1031            // get next dentry
1032            child_dentry_xp = xhtab_get_next( children_xp );
1033        }
1034
1035        // release xhtab lock
1036        xhtab_read_unlock( children_xp );
1037    }
1038}  // end vfs_recursive_display()
1039
1040///////////////////////////////////
1041void vfs_display( xptr_t inode_xp )
1042{
1043    xptr_t         name_xp;
1044    xptr_t         dentry_xp; 
1045    cxy_t          dentry_cxy;
1046    vfs_dentry_t * dentry_ptr;
1047
1048    // get target inode cluster and local pointer
1049    cxy_t         inode_cxy = GET_CXY( inode_xp );
1050    vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
1051
1052    // get extended pointer on associated dentry
1053    dentry_xp = hal_remote_lwd( XPTR( inode_cxy , &inode_ptr->parent_xp ) );
1054
1055    // check if target inode is the File System root
1056    if( dentry_xp == XPTR_NULL )
1057    {
1058        // build extended pointer on root name
1059        name_xp = XPTR( local_cxy , "/" );
1060    }
1061    else
1062    {
1063        // get dentry cluster and local pointer
1064        dentry_cxy = GET_CXY( dentry_xp );
1065        dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
1066
1067        // get extended pointer on dentry name
1068        name_xp = XPTR( dentry_cxy , &dentry_ptr->name );
1069    }
1070
1071    // print header
1072    printk("\n*** VFS ***\n");
1073
1074    // call recursive function
1075    vfs_recursive_display( inode_xp , name_xp , dentry_xp , 0 );
1076
1077}  // end vfs_display()
1078
1079//////////////////////////////////////////////////////////////////////////////////////////
1080// This function is used by the vfs_lookup() function.
1081// It takes an extended pointer on a remote inode (parent directory inode),
1082// and check access_rights violation for the calling thread.
1083// It can be used by any thread running in any cluster.
1084//////////////////////////////////////////////////////////////////////////////////////////
1085// @ inode_xp    : extended pointer on inode.
1086// @ client_uid  : client thread user ID
1087// @ client_gid  : client thread group ID
1088// @ return true if access rights are violated.
1089//////////////////////////////////////////////////////////////////////////////////////////
1090bool_t vfs_access_denied( xptr_t   inode_xp,
1091                          uint32_t client_uid,
1092                          uint32_t client_gid )
1093{
1094    // get found inode cluster and local pointer
1095    cxy_t         inode_cxy = GET_CXY( inode_xp );
1096    vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
1097
1098    // get inode access mode, UID, and GID
1099    // TODO uint32_t  mode = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->mode ) );
1100    uid_t     uid  = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->uid  ) );
1101    gid_t     gid  = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->gid  ) );
1102
1103    // FIXME : me must use mode
1104    if( (uid == client_uid) || (gid == client_gid) ) return false;
1105    else                                             return true;
1106}
1107
1108//////////////////////////////////////////////////////////////////////////////////////////
1109// This static function is used by the vfs_lookup() function.
1110// It takes an extended pointer on a remote parent directory inode, a directory
1111// entry name, and returns an extended pointer on the child inode.
1112// It can be used by any thread running in any cluster.
1113//////////////////////////////////////////////////////////////////////////////////////////
1114// @ parent_xp   : extended pointer on parent inode in remote cluster.
1115// @ name        : dentry name
1116// @ child_xp    : [out] buffer for extended pointer on child inode.
1117// @ return true if success / return false if not found.
1118//////////////////////////////////////////////////////////////////////////////////////////
1119static bool_t vfs_get_child( xptr_t   parent_xp,
1120                             char   * name,
1121                             xptr_t * child_xp )
1122{
1123    xptr_t  xhtab_xp;    // extended pointer on hash table containing children dentries
1124    xptr_t  dentry_xp;   // extended pointer on children dentry
1125
1126    // get parent inode cluster and local pointer
1127    cxy_t         parent_cxy = GET_CXY( parent_xp );
1128    vfs_inode_t * parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
1129
1130    // get extended pointer on hash table of children directory entries
1131    xhtab_xp = XPTR( parent_cxy , &parent_ptr->children );
1132
1133    // search extended pointer on matching dentry
1134    dentry_xp = xhtab_lookup( xhtab_xp , name );
1135
1136    if( dentry_xp == XPTR_NULL ) return false;
1137
1138    // get dentry cluster and local pointer
1139    cxy_t          dentry_cxy = GET_CXY( dentry_xp );
1140    vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
1141
1142    // return child inode
1143    *child_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , &dentry_ptr->child_xp ) );
1144    return true;
1145
1146}  // end vfs_get_child()
1147
1148//////////////////////////////////////////////////////////////////////////////////////////
1149// This static function is used by the vfs_lookup() function.
1150// It takes the <current> pointer on a buffer containing a complete pathname, and return
1151// in the <name> buffer, allocated by the caller, a single name in the path.
1152// It return also in the <next> pointer the next character to analyse in the path.
1153// Finally it returns a <last> boolean, that is true when the returned <name> is the
1154// last name in the path. The names are supposed to be separated by one or several '/'
1155// characters, that are not written in  the <name> buffer.
1156//////////////////////////////////////////////////////////////////////////////////////////
1157// @ current   : pointer on first character to analyse in buffer containing the path.
1158// @ name      : [out] pointer on buffer allocated by the caller for the returned name.
1159// @ next      : [out] pointer on next character to analyse in buffer containing the path.
1160// @ last      : [out] true if the returned name is the last (NUL character found).
1161// @ return 0 if success / return EINVAL if string empty (first chracter is NUL).
1162//////////////////////////////////////////////////////////////////////////////////////////
1163static error_t vfs_get_name_from_path( char     * current,
1164                                       char     * name,
1165                                       char    ** next,
1166                                       bool_t   * last )
1167{
1168    char * ptr = current;
1169
1170    // skip leading '/' characters
1171    while( *ptr == '/' ) ptr++;
1172
1173    // return EINVAL if string empty
1174    if( *ptr == 0 ) return EINVAL;
1175
1176    // copy all characters in name until NUL or '/'
1177    while( (*ptr != 0) && (*ptr !='/') )  *(name++) = *(ptr++);
1178
1179    // set NUL terminating character in name buffer
1180    *(name++) = 0;
1181
1182    // return last an next
1183    if( *ptr == 0 )             // last found character is NUL => last name in path
1184    {
1185        *last = true;
1186    }
1187    else                        // last found character is '/' => skip it
1188    {
1189        *last = false;
1190        *next = ptr + 1;
1191    }
1192
1193    return 0;
1194
1195}  // end vfs_get name_from_path()
1196   
1197//////////////////////////////////////////////
1198error_t vfs_lookup( xptr_t             cwd_xp,
1199                    char             * pathname,
1200                    uint32_t           mode,
1201                                        xptr_t           * inode_xp )
1202{
1203    char               name[CONFIG_VFS_MAX_NAME_LENGTH];   // one name in path
1204
1205    xptr_t             parent_xp;    // extended pointer on parent inode
1206    cxy_t              parent_cxy;   // cluster for parent inode
1207    vfs_inode_t      * parent_ptr;   // local pointer on parent inode 
1208    xptr_t             child_xp;     // extended pointer on child inode
1209    cxy_t              child_cxy;    // cluster for child inode
1210    vfs_inode_t      * child_ptr;    // local pointer on child inode 
1211    vfs_inode_type_t   child_type;   // child inode type
1212    vfs_fs_type_t      fs_type;      // File system type
1213    vfs_ctx_t        * ctx_ptr;      // local pointer on FS context
1214    char             * current;      // current pointer on path
1215    char             * next;         // next value for current pointer   
1216    bool_t             last;         // true when the name is the last in path
1217    bool_t             found;        // true when a child has been found
1218    thread_t         * this;         // pointer on calling thread descriptor
1219    process_t        * process;      // pointer on calling process descriptor
1220    error_t            error;
1221
1222    vfs_dmsg("\n[INFO] %s : enters for <%s> at cycle %d\n",
1223             __FUNCTION__ , pathname , (uint32_t)hal_time_stamp() );
1224
1225    this    = CURRENT_THREAD;
1226    process = this->process;
1227
1228    // get extended pointer on first inode to search
1229    if( pathname[0] == '/' ) parent_xp = process->vfs_root_xp;
1230    else                     parent_xp = cwd_xp;
1231
1232    // initialise other loop variables
1233    current  = pathname;
1234    next     = NULL;
1235    last     = false;
1236    child_xp = XPTR_NULL;
1237
1238    // take lock on parent inode
1239    vfs_inode_lock( parent_xp );
1240
1241    // load from device if one intermediate node not found
1242    // exit while loop when last name found (i.e. last == true)
1243    do
1244    {
1245        // get one name from path, and the "last" flag
1246        vfs_get_name_from_path( current , name , &next , &last );
1247
1248        vfs_dmsg("\n[INFO] %s : looking for <%s> / last = %d\n",
1249                 __FUNCTION__ , name , last );
1250
1251        // search a child dentry matching name in parent inode
1252        found = vfs_get_child( parent_xp,
1253                               name,
1254                               &child_xp );
1255
1256        // if a child inode is not found in the inode tree:
1257        // - we create the missing inode/dentry couple in the inode tree,
1258        // - we scan the parent mapper to complete the child inode (type and extension),
1259        // - we return an error if child not found on device.
1260        // - if the missing child is a directory, we load the child mapper from device
1261
1262        // for the last name, the behaviour depends on the "mode" argument:
1263
1264        if (found == false ) // child node not found in inode tree
1265        {
1266            vfs_dmsg("\n[INFO] %s : <%s> not found, try to load it\n",
1267                     __FUNCTION__ , name );
1268
1269            // release lock on parent inode
1270            vfs_inode_unlock( parent_xp );
1271
1272            // get parent inode FS type
1273            parent_cxy = GET_CXY( parent_xp );
1274            parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
1275
1276            ctx_ptr    = (vfs_ctx_t *)hal_remote_lpt( XPTR( parent_cxy ,
1277                                                            &parent_ptr->ctx ) );
1278            fs_type    = hal_remote_lw( XPTR( parent_cxy , &ctx_ptr->type ) );
1279
1280            // select a cluster for missing inode
1281            child_cxy = vfs_cluster_random_select();
1282                     
1283            // insert a new child dentry/inode in parent inode
1284            error = vfs_add_child_in_parent( child_cxy,
1285                                             INODE_TYPE_DIR,
1286                                             fs_type, 
1287                                             parent_xp, 
1288                                             name, 
1289                                             NULL,     // fs_type_specific inode extend
1290                                             &child_xp );
1291            if( error )
1292            {
1293                printk("\n[ERROR] in %s : no memory for inode %s in path %s\n",
1294                       __FUNCTION__ , name , pathname );
1295                return ENOMEM;
1296            }
1297
1298            // scan parent mapper to complete the missing inode
1299            if( parent_cxy == local_cxy )
1300            {
1301                error = vfs_inode_load( parent_ptr,
1302                                        name,
1303                                        child_xp );
1304            }
1305            else
1306            {
1307                rpc_vfs_inode_load_client( parent_cxy,
1308                                           parent_ptr,
1309                                           name,
1310                                           child_xp,
1311                                           &error );
1312            }
1313
1314            if ( error )
1315            {
1316                printk("\n[ERROR] in %s : node %s not found in path %s\n",
1317                       __FUNCTION__ , name , pathname );
1318                return ENOENT;
1319            }
1320
1321            // get child inode type
1322            child_ptr  = (vfs_inode_t *)GET_PTR( child_xp );
1323            child_type = hal_remote_lw( XPTR( child_cxy , &child_ptr->type ) );
1324
1325            // load child mapper from device if it is a directory
1326            if( child_type == INODE_TYPE_DIR )
1327            {
1328                if( child_cxy == local_cxy )
1329                {
1330                    error = vfs_mapper_load_all( child_ptr );
1331                }
1332                else
1333                {
1334                    rpc_vfs_mapper_load_all_client( child_cxy,
1335                                                    child_ptr,
1336                                                    &error );
1337                }
1338
1339                if ( error )
1340                {
1341                    printk("\n[ERROR] in %s : cannot access device for node %s in path %s\n",
1342                           __FUNCTION__ , name , pathname );
1343                    return EIO;
1344                }
1345            }
1346
1347            // TODO handle lookup mode here [AG]
1348
1349            // take lock on parent inode
1350            vfs_inode_lock( parent_xp );
1351        }
1352
1353        vfs_dmsg("\n[INFO] %s : found <%s> / parent = %l / child = %l / last = %d\n",
1354                     __FUNCTION__ , name , parent_xp , child_xp , last );
1355
1356        // TODO check access rights
1357        // error = vfs_access_denied( child_xp,
1358        //                            client_uid,
1359        //                            client_gid );
1360        // if( error )
1361        // {
1362        //     printk("\n[ERROR] in %s : permission denied for %s\n", __FUNCTION__ , name );
1363        //     return EACCES;
1364        // }
1365
1366        // take lock on child inode and release lock on parent
1367        vfs_inode_lock( child_xp );
1368        vfs_inode_unlock( parent_xp );
1369
1370        // update loop variables
1371        parent_xp = child_xp;
1372        current   = next;
1373    }
1374    while( last == false );
1375
1376    // release lock
1377    vfs_inode_unlock( parent_xp );
1378
1379    vfs_dmsg("\n[INFO] in %s : exit / %s found / inode = %l\n",
1380                 __FUNCTION__ , pathname , child_xp );
1381
1382    // return searched pointer
1383    *inode_xp = child_xp;
1384
1385    return 0;
1386
1387}  // end vfs_lookup()
1388
1389////////////////////////////////////////////
1390error_t vfs_get_path( xptr_t    searched_xp,
1391                      char    * buffer,
1392                      uint32_t  max_size )
1393{
1394        xptr_t       dentry_xp;   // extended pointer on current dentry
1395    char       * name;        // local pointer on current dentry name
1396        uint32_t     length;      // length of current dentry name
1397        uint32_t     count;       // number of characters written in buffer
1398        uint32_t     index;       // slot index in buffer
1399    xptr_t       inode_xp;    // extended pointer on   
1400
1401    // implementation note:
1402    // we use two variables "index" and "count" because the buffer
1403    // is actually written in decreasing index order (from leaf to root)
1404    // TODO : handle conflict with a concurrent rename
1405    // FIXME : handle synchro in the loop ... [AG]
1406
1407        // set the NUL character in buffer / initialise buffer index and count
1408        buffer[max_size - 1] = 0;
1409        count    = 1;
1410    index    = max_size - 2;
1411
1412    // initialize current inode
1413    inode_xp  = searched_xp;
1414
1415    // exit when root inode found (i.e. dentry_xp == XPTR_NULL)
1416        do
1417    {
1418        // get inode cluster and local pointer
1419        cxy_t         inode_cxy = GET_CXY( inode_xp );
1420        vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
1421
1422        // get extended pointer on parent dentry               
1423        dentry_xp = (xptr_t)hal_remote_lwd( XPTR( inode_cxy , inode_ptr->parent_xp ) );
1424
1425        // get dentry cluster and local pointer
1426        cxy_t          dentry_cxy = GET_CXY( dentry_xp );
1427        vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
1428
1429        // get dentry name length and pointer
1430        length =  hal_remote_lw( XPTR( dentry_cxy , &dentry_ptr->length ) );
1431        name   = (char *)hal_remote_lpt( XPTR( dentry_cxy , &dentry_ptr->name ) );
1432
1433        // update index and count
1434        index -= (length + 1); 
1435        count += (length + 1);
1436
1437        // check buffer overflow
1438        if( count >= max_size )
1439        {
1440            printk("\n[ERROR] in %s : kernel buffer too small\n", __FUNCTION__ );
1441            return EINVAL;
1442        }
1443
1444        // update pathname
1445        hal_remote_memcpy( XPTR( local_cxy , &buffer[index + 1] ) ,
1446                           XPTR( dentry_cxy , name ) , length );
1447                buffer[index] = '/';
1448
1449                // get extended pointer on next inode
1450        inode_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , dentry_ptr->parent ) );
1451    }
1452    while( (dentry_xp != XPTR_NULL) );
1453
1454        return 0;
1455
1456}  // end vfs_get_path()
1457
1458     
1459//////////////////////////////////////////////////////////////
1460error_t vfs_add_child_in_parent( cxy_t              child_cxy,
1461                                 vfs_inode_type_t   inode_type,
1462                                 vfs_fs_type_t      fs_type,
1463                                 xptr_t             parent_xp,
1464                                 char             * name,
1465                                 void             * extend,
1466                                 xptr_t           * child_xp )
1467{
1468    error_t         error;
1469    xptr_t          dentry_xp;   // extended pointer on created dentry
1470    xptr_t          inode_xp;    // extended pointer on created inode
1471    cxy_t           parent_cxy;  // parent inode cluster identifier
1472    vfs_inode_t   * parent_ptr;  // parent inode local pointer
1473
1474    // get parent inode cluster and local pointer
1475    parent_cxy = GET_CXY( parent_xp );
1476    parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
1477
1478    // 1. create dentry
1479    if( parent_cxy == local_cxy )      // parent cluster is the local cluster
1480    {
1481        error = vfs_dentry_create( fs_type,
1482                                   name,
1483                                   parent_ptr,
1484                                   &dentry_xp );
1485    }
1486    else                               // parent cluster is remote
1487    {
1488        rpc_vfs_dentry_create_client( parent_cxy,
1489                                      fs_type,
1490                                      name,
1491                                      parent_ptr,
1492                                      &dentry_xp,
1493                                      &error );
1494    }
1495                                     
1496    if( error )
1497    {
1498        printk("\n[ERROR] in %s : cannot create dentry in cluster %x\n",
1499               __FUNCTION__ , parent_cxy );
1500        return ENOMEM;
1501    }
1502
1503    // 2. create child inode TODO : define attr / mode / uid / gid
1504    uint32_t attr = 0;
1505    uint32_t mode = 0;
1506    uint32_t uid  = 0;
1507    uint32_t gid  = 0;
1508   
1509    if( child_cxy == local_cxy )      // child cluster is the local cluster
1510    {
1511        error = vfs_inode_create( dentry_xp,
1512                                  fs_type,
1513                                  inode_type,
1514                                  extend,
1515                                  attr,
1516                                  mode,
1517                                  uid,
1518                                  gid,
1519                                  &inode_xp );
1520    }
1521    else                              // child cluster is remote
1522    {
1523        rpc_vfs_inode_create_client( child_cxy,
1524                                     dentry_xp,
1525                                     fs_type,
1526                                     inode_type,
1527                                     extend,
1528                                     attr,
1529                                     mode,
1530                                     uid,
1531                                     gid,
1532                                     &inode_xp,
1533                                     &error );
1534    }
1535                                     
1536    if( error )
1537    {
1538        printk("\n[ERROR] in %s : cannot create inode in cluster %x\n",
1539               __FUNCTION__ , child_cxy );
1540 
1541        vfs_dentry_t * dentry = (vfs_dentry_t *)GET_PTR( dentry_xp );
1542        if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry );
1543        else rpc_vfs_dentry_destroy_client( parent_cxy , dentry );
1544        return ENOMEM;
1545    }
1546
1547    // 3. update extended pointer on inode in dentry
1548    cxy_t          dentry_cxy = GET_CXY( dentry_xp );
1549    vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
1550    hal_remote_swd( XPTR( dentry_cxy , &dentry_ptr->child_xp ) , inode_xp );
1551
1552    // success : return extended pointer on child inode
1553    *child_xp = inode_xp;
1554    return 0;
1555
1556}  // end vfs_add_child_in_parent()
1557
1558//////////////////////////////////////////////////////////////////////////////////////////
1559//            Mapper related functions
1560//////////////////////////////////////////////////////////////////////////////////////////
1561
1562////////////////////////////////////////////
1563error_t vfs_mapper_move_page( page_t * page,
1564                              bool_t   to_mapper )
1565{
1566    error_t error = 0;
1567
1568    assert( (page != NULL) , __FUNCTION__ , "page pointer is NULL\n" );
1569
1570    mapper_t    * mapper = page->mapper;
1571
1572
1573    assert( (mapper != NULL) , __FUNCTION__ , "no mapper for page\n" );
1574
1575    vfs_dmsg("\n[INFO] %s : enters for page = %d in mapper = %x\n",
1576             __FUNCTION__ , page->index , mapper );
1577
1578    // get FS type
1579    vfs_fs_type_t fs_type = mapper->type;
1580
1581    // call relevant FS function
1582    if( fs_type == FS_TYPE_FATFS )
1583    {
1584        rwlock_wr_lock( &mapper->lock );
1585        error = fatfs_mapper_move_page( page , to_mapper ); 
1586        rwlock_wr_unlock( &mapper->lock );
1587    }
1588    else if( fs_type == FS_TYPE_RAMFS )
1589    {
1590        assert( false , __FUNCTION__ , "should not be called for RAMFS\n" );
1591    }
1592    else if( fs_type == FS_TYPE_DEVFS )
1593    {
1594        assert( false , __FUNCTION__ , "should not be called for DEVFS\n" );
1595    }
1596    else
1597    {
1598        assert( false , __FUNCTION__ , "undefined file system type\n" );
1599    }
1600
1601    vfs_dmsg("\n[INFO] %s : exit for page = %d in mapper = %x\n",
1602             __FUNCTION__ , page->index , mapper );
1603
1604    return error;
1605
1606}  // end vfs_move_page()
1607
1608//////////////////////////////////////////////////
1609error_t vfs_mapper_load_all( vfs_inode_t * inode )
1610{
1611    assert( (inode != NULL) , __FUNCTION__ , "page pointer is NULL\n" );
1612
1613    uint32_t   index;
1614    page_t   * page;
1615
1616    mapper_t * mapper = inode->mapper;
1617    uint32_t   size   = inode->size;
1618
1619    assert( (mapper != NULL) , __FUNCTION__ , "no mapper for page\n" );
1620
1621    assert( (size != 0) , __FUNCTION__ , "size cannot be 0\n");
1622
1623    uint32_t npages = size >> CONFIG_PPM_PAGE_SHIFT;
1624    if( size & CONFIG_PPM_PAGE_MASK ) npages++;
1625
1626    // loop on all pages
1627    for( index = 0 ; index < npages ; index ++ )
1628    {
1629        // this function allocates the missing page in mapper,
1630        // and call the vfs_mapper_move_page() to load the page from device
1631        page = mapper_get_page( mapper , index );
1632
1633        if( page == NULL ) return EIO;
1634    }
1635
1636    return 0;
1637
1638}  // end vfs_mapper_load_all()
1639
Note: See TracBrowser for help on using the repository browser.