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

Last change on this file since 519 was 492, checked in by viala@…, 6 years ago

Refactoring assert calling to conform with new assert macro.

Made with this command for the general case.
find ./kernel/ hal/ -name "*.c" | xargs sed -i -e '/assert(/ s/,[ ]*FUNCTION[ ]*,/,/'

And some done by hand.

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