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

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

Redefine the PIC device API.

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