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

Last change on this file since 396 was 395, checked in by max@…, 7 years ago

Use panic().

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