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

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

1) Introduce independant command fields for the various devices in the thread descriptor.
2) Introduce a new dev_pic_enable_ipi() function in the generic PIC device
3) Fix two bugs identified by Maxime in the scheduler initialisation, and in the sched_select().
4) fix several bugs in the TSAR hal_kentry.S.
5) Introduce a third kgiet segment (besides kdata and kcode) in the TSAR bootloader.

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