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

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

bloup

File size: 43.0 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];    // allocate in kernel_init.c
55 
56//////////////////////////////////////////////////////////////////////////////////////////
57//           Context related functions
58//////////////////////////////////////////////////////////////////////////////////////////
59
60////////////////////////////////////////////
61error_t vfs_ctx_inum_alloc( vfs_ctx_t * ctx,
62                            uint32_t  * inum )
63{
64    // get lock on inum allocator
65    spinlock_lock( &ctx->lock );
66
67    // get lid from local inum allocator
68    uint32_t lid = bitmap_ffc( ctx->bitmap , CONFIG_VFS_MAX_INODES );
69
70    if( lid == -1 )   // no more free slot => error
71    {
72        // release lock
73        spinlock_unlock( &ctx->lock );
74
75        // return error
76        return 1;
77    }
78    else              // found => return inum
79    {
80        // set slot allocated
81        bitmap_set( ctx->bitmap , lid );
82
83        // release lock
84        spinlock_unlock( &ctx->lock );
85
86        // return inum
87        *inum = (((uint32_t)local_cxy) << 16) | (lid & 0xFFFF);
88        return 0;
89    }
90}
91
92////////////////////////////////////////////
93void vfs_ctx_inum_release( vfs_ctx_t * ctx,
94                           uint32_t    inum )
95{
96    bitmap_clear( ctx->bitmap , inum & 0xFFFF ); 
97}
98
99//////////////////////////////////////////////////////////////////////////////////////////
100//           Inode related functions
101//////////////////////////////////////////////////////////////////////////////////////////
102
103//////////////////////////////////////////////////////
104
105error_t vfs_inode_create( xptr_t            dentry_xp,
106                          vfs_fs_type_t     fs_type,
107                          vfs_inode_type_t  inode_type,
108                          uint32_t          attr,
109                          uint32_t          rights,
110                          uid_t             uid,
111                          gid_t             gid,
112                          xptr_t          * inode_xp )
113{
114    mapper_t         * mapper;     // associated mapper( to be allocated)
115    vfs_inode_t      * inode;      // inode descriptor (to be allocated)
116    uint32_t           inum;       // inode identifier (to be allocated)
117    vfs_ctx_t        * ctx;        // file system context
118        kmem_req_t         req;        // request to kernel memory allocator
119    error_t            error;
120
121    // check fs type and get pointer on context
122    if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
123    else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
124    else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS];
125    else
126    {
127        ctx = NULL;
128        printk("\n[PANIC] in %s : undefined file system type\n", __FUNCTION__ );
129        hal_core_sleep();
130    }
131
132    // allocate inum
133    error = vfs_ctx_inum_alloc( ctx , &inum );
134
135    if( error )
136    {
137        printk("\n[ERROR] in %s : cannot allocate inum\n", __FUNCTION__ );
138        return ENOMEM;
139    }
140
141    // allocate memory for mapper
142    mapper = mapper_create();
143
144    if( mapper == NULL )
145    {
146        printk("\n[ERROR] in %s : cannot allocate mapper\n", __FUNCTION__ );
147        vfs_ctx_inum_release( ctx , inum );
148        return ENOMEM;
149    }
150
151    // allocate memory for VFS inode descriptor
152        req.type  = KMEM_VFS_INODE;
153        req.size  = sizeof(vfs_inode_t);
154    req.flags = AF_KERNEL | AF_ZERO;
155        inode     = (vfs_inode_t *)kmem_alloc( &req );
156
157    if( inode == NULL )
158    {
159        printk("\n[ERROR] in %s : cannot allocate inode descriptor\n", __FUNCTION__ );
160        vfs_ctx_inum_release( ctx , inum );
161        mapper_destroy( mapper );
162        return ENOMEM;
163    }
164
165    // initialize inode descriptor
166    inode->gc         = 0;
167    inode->type       = inode_type;
168    inode->inum       = inum;
169    inode->attr       = attr;
170    inode->rights     = rights;
171    inode->uid        = uid;
172    inode->gid        = gid;
173    inode->refcount   = 0;
174    inode->parent_xp  = dentry_xp;
175    inode->ctx        = ctx;
176    inode->mapper     = NULL; 
177
178    // initialise threads waiting queue
179    xlist_root_init( XPTR( local_cxy , &inode->wait_root ) );
180
181    // initialize dentries hash table, if new inode is a directory
182    if( inode_type == INODE_TYPE_DIR ) xhtab_init( &inode->children , XHTAB_DENTRY_TYPE );
183
184    // initialize inode locks
185    remote_rwlock_init( XPTR( local_cxy , &inode->data_lock ) );
186    remote_spinlock_init( XPTR( local_cxy , &inode->main_lock ) );
187
188    // return extended pointer on inode
189    *inode_xp = XPTR( local_cxy , inode );
190    return 0;
191
192}  // end vfs_inode_create() 
193
194/////////////////////////////////////////////
195void vfs_inode_destroy( vfs_inode_t * inode )
196{
197    if( inode->refcount )
198    {
199        printk("\n[PANIC] in %s : inode refcount non zero\n", __FUNCTION__ );
200        hal_core_sleep(); 
201    }       
202
203    // release memory allocated for mapper
204    mapper_destroy( inode->mapper );
205
206    // release memory allocate for inode descriptor
207        kmem_req_t req;
208        req.ptr   = inode;
209        req.type  = KMEM_VFS_INODE;
210        kmem_free( &req );
211
212}  // end vfs_inode_destroy()
213
214////////////////////////////////////////////
215void vfs_inode_remote_up( xptr_t  inode_xp )
216{
217    // get inode cluster and local pointer
218    cxy_t         inode_cxy = GET_CXY( inode_xp );
219    vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
220
221    hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->refcount ) , 1 );   
222}
223
224//////////////////////////////////////////////
225void vfs_inode_remote_down( xptr_t  inode_xp )
226{
227    // get inode cluster and local pointer
228    cxy_t         inode_cxy = GET_CXY( inode_xp );
229    vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
230
231    hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->refcount ) , -1 );   
232}
233
234//////////////////////////////////////////////
235uint32_t vfs_inode_get_size( xptr_t inode_xp )
236{
237    // get inode cluster and local pointer
238    cxy_t         cxy = GET_CXY( inode_xp );
239    vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp );
240
241    // get size
242    remote_rwlock_rd_lock( XPTR( cxy , &ptr->data_lock ) );
243    uint32_t size = hal_remote_lw( XPTR( cxy , &ptr->size ) );
244    remote_rwlock_rd_unlock( XPTR( cxy , &ptr->data_lock ) );
245    return size;
246}
247
248/////////////////////////////////////////////////
249void vfs_inode_size_set_size( xptr_t    inode_xp,
250                              uint32_t  size )
251{
252    // get inode cluster and local pointer
253    cxy_t         cxy = GET_CXY( inode_xp );
254    vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp );
255
256    // set size
257    remote_rwlock_wr_unlock( XPTR( cxy , &ptr->data_lock ) );
258    hal_remote_sw( XPTR( cxy , &ptr->size ) , size );
259    remote_rwlock_wr_unlock( XPTR( cxy , &ptr->data_lock ) );
260}
261
262///////////////////////////////////////////////
263void vfs_inode_remote_unlock( xptr_t inode_xp )
264{
265    // get inode cluster and local pointer
266    cxy_t         cxy = GET_CXY( inode_xp );
267    vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp );
268
269    // release the main lock
270    remote_spinlock_unlock( XPTR( cxy , &ptr->main_lock ) );
271}
272
273/////////////////////////////////////////////
274void vfs_inode_remote_lock( xptr_t inode_xp )
275{
276    // get inode cluster and local pointer
277    cxy_t         cxy = GET_CXY( inode_xp );
278    vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp );
279
280    // get the main lock
281    remote_spinlock_lock( XPTR( cxy , &ptr->main_lock ) );
282}
283
284//////////////////////////////////////////////////////////////////////////////////////////
285//           Dentry related functions
286//////////////////////////////////////////////////////////////////////////////////////////
287
288///////////////////////////////////////////////////
289error_t vfs_dentry_create( vfs_fs_type_t   fs_type,
290                           char          * name,
291                           vfs_inode_t   * parent,
292                           xptr_t        * dentry_xp )
293{
294    vfs_ctx_t      * ctx;        // context descriptor
295    vfs_dentry_t   * dentry;     // dentry descriptor (to be allocated)
296        kmem_req_t       req;        // request to kernel memory allocator
297
298    // check type and get pointer on context
299    if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS];
300    else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS];
301    else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS];
302    else
303    {
304        ctx = NULL;
305        printk("\n[PANIC] in %s : undefined file system type\n", __FUNCTION__ );
306        hal_core_sleep();
307    }
308
309    // get name length
310    uint32_t length = strlen( name );
311
312    if( length >= CONFIG_VFS_MAX_NAME_LENGTH )
313    {
314        printk("\n[ERROR] in %s : name too long\n", __FUNCTION__ );
315        return EINVAL;
316    }
317
318    // allocate memory for dentry descriptor
319        req.type  = KMEM_VFS_DENTRY;
320        req.size  = sizeof(vfs_dentry_t);
321    req.flags = AF_KERNEL | AF_ZERO;
322        dentry     = (vfs_dentry_t *)kmem_alloc( &req );
323
324    if( dentry == NULL )
325    {
326        printk("\n[ERROR] in %s : cannot allocate dentry descriptor\n", __FUNCTION__ );
327        return ENOMEM;
328    }
329
330    // initialize dentry descriptor
331
332    dentry->ctx     = ctx;
333    dentry->length  = length;
334    dentry->parent  = parent;
335    strcpy( dentry->name , name );
336
337    // register dentry in hash table rooted in parent inode
338    xhtab_insert( XPTR( local_cxy , &parent->children ),
339                  name, 
340                  XPTR( local_cxy , &dentry->xlist ) );
341
342    // return extended pointer on dentry
343    *dentry_xp = XPTR( local_cxy , dentry );
344
345    return 0;
346
347}  // end vfs_dentry_create()
348
349////////////////////////////////////////////////
350void vfs_dentry_destroy( vfs_dentry_t * dentry )
351{
352    if( dentry->refcount )
353    {
354        printk("\n[PANIC] in %s : dentry refcount non zero\n", __FUNCTION__ );
355        hal_core_sleep(); 
356    }       
357
358        kmem_req_t req;
359        req.ptr   = dentry;
360        req.type  = KMEM_VFS_DENTRY;
361        kmem_free( &req );
362}
363
364
365//////////////////////////////////////////////////////////////////////////////////////////
366//           File descriptor related functions
367//////////////////////////////////////////////////////////////////////////////////////////
368
369/////////////////////////////////////////////
370error_t vfs_file_create( vfs_inode_t * inode,
371                         uint32_t      attr,
372                         xptr_t      * file_xp )
373{
374    vfs_file_t  * file;
375        kmem_req_t    req;
376
377    // allocate memory for new file descriptor
378        req.type  = KMEM_VFS_FILE;
379        req.size  = sizeof(vfs_file_t);
380    req.flags = AF_KERNEL | AF_ZERO;
381        file      = (vfs_file_t *)kmem_alloc( &req );
382
383    if( file == NULL ) return ENOMEM;
384
385    // initializes new file descriptor
386    file->gc       = 0;
387    file->type     = inode->type;
388    file->attr     = attr;
389    file->offset   = 0;
390    file->refcount = 0;
391    file->inode    = inode;
392    file->ctx      = inode->ctx;
393    file->mapper   = inode->mapper;
394
395    remote_rwlock_init( XPTR( local_cxy , &file->lock ) );
396
397    *file_xp = XPTR( local_cxy , file );
398    return 0;
399
400}  // end vfs_file_create()
401
402///////////////////////////////////////////
403void vfs_file_destroy( vfs_file_t *  file )
404{
405    if( file->refcount )
406    {
407        printk("\n[PANIC] in %s : file refcount non zero\n", __FUNCTION__ );
408        hal_core_sleep(); 
409    }       
410
411        kmem_req_t req;
412        req.ptr   = file;
413        req.type  = KMEM_VFS_FILE;
414        kmem_free( &req );
415
416}  // end vfs_file_destroy()
417
418
419////////////////////////////////////////
420void vfs_file_count_up( xptr_t file_xp )
421{
422    // get file cluster and local pointer
423    cxy_t        file_cxy = GET_CXY( file_xp );
424    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); 
425
426    // atomically increment count
427    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , 1 ); 
428}
429
430//////////////////////////////////////////
431void vfs_file_count_down( xptr_t file_xp )
432{
433    // get file cluster and local pointer
434    cxy_t        file_cxy = GET_CXY( file_xp );
435    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); 
436
437    // atomically decrement count
438    hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , -1 ); 
439}
440
441//////////////////////////////////////////////////////////////////////////////////////////
442//           File access related functions
443//////////////////////////////////////////////////////////////////////////////////////////
444
445////////////////////////////////////
446error_t vfs_open( xptr_t     cwd_xp,
447                          char     * path,
448                          uint32_t   flags,
449                  uint32_t   mode, 
450                          xptr_t   * new_file_xp,
451                  uint32_t * new_file_id )
452{
453    error_t       error;
454    xptr_t        inode_xp;     // extended pointer on target inode
455    cxy_t         inode_cxy;    // inode cluster identifier       
456    vfs_inode_t * inode_ptr;    // inode local pointer
457    uint32_t      file_attr;    // file descriptor attributes
458    uint32_t      lookup_mode;  // lookup working mode       
459    xptr_t        file_xp;      // extended pointer on created file descriptor
460    uint32_t      file_id;      // created file descriptor index in reference fd_array
461
462    // compute lookup working mode
463    lookup_mode = VFS_LOOKUP_OPEN;
464    if( (flags & O_DIR    )      )  lookup_mode |= VFS_LOOKUP_DIR;
465    if( (flags & O_CREAT  )      )  lookup_mode |= VFS_LOOKUP_CREATE;
466    if( (flags & O_EXCL   )      )  lookup_mode |= VFS_LOOKUP_EXCL;
467 
468    // compute attributes for the created file
469    file_attr = 0;
470    if( (flags & O_RDONLY ) == 0 )  file_attr |= FD_ATTR_READ_ENABLE;
471    if( (flags & O_WRONLY ) == 0 )  file_attr |= FD_ATTR_WRITE_ENABLE;
472    if( (flags & O_SYNC   )      )  file_attr |= FD_ATTR_SYNC;
473    if( (flags & O_APPEND )      )  file_attr |= FD_ATTR_APPEND;
474    if( (flags & O_CLOEXEC)      )  file_attr |= FD_ATTR_CLOSE_EXEC;
475
476    // get extended pointer on target inode
477    error = vfs_lookup( cwd_xp , path , lookup_mode , &inode_xp );
478
479    if( error ) return error;
480
481    // get target inode cluster and local pointer
482    inode_cxy = GET_CXY( inode_xp );
483    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
484   
485    // create a new file descriptor in cluster containing inode
486    if( inode_cxy == local_cxy )      // target cluster is local
487    {
488        error = vfs_file_create( inode_ptr , file_attr , &file_xp );
489    }
490    else                              // target cluster is remote
491    {
492        rpc_vfs_file_create_client( inode_cxy , inode_ptr , file_attr , &file_xp , &error );
493    }
494
495    if( error )  return error;
496
497    // allocate and register a new file descriptor index in reference cluster fd_array
498    error = process_fd_register( file_xp , &file_id );
499
500    if( error ) return error;
501
502    // success
503    *new_file_xp = file_xp;
504    *new_file_id = file_id;
505    return 0;
506
507}  // end vfs_open()
508
509/////////////////////////////////////
510error_t vfs_move( bool_t   to_buffer,
511                  xptr_t   file_xp,
512                  void   * buffer,
513                  uint32_t size )
514{
515    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
516
517    cxy_t              file_cxy;     // remote file descriptor cluster
518    vfs_file_t       * file_ptr;     // remote file descriptor local pointer
519    vfs_inode_type_t   inode_type;
520    uint32_t           file_offset;  // current offset in file
521    mapper_t         * mapper;
522    error_t            error;
523
524    // get cluster and local pointer on remote file descriptor
525    file_cxy  = GET_CXY( file_xp );
526    file_ptr  = (vfs_file_t *)GET_PTR( file_xp );
527
528    // get inode type from remote file descriptor
529    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type   ) );
530   
531    // action depends on inode type
532    if( inode_type == INODE_TYPE_FILE )
533    {
534        // get mapper pointer and file offset from file descriptor
535        file_offset = hal_remote_lw( XPTR( file_cxy , &file_ptr->offset ) );
536        mapper = (mapper_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) );
537
538        // move data between mapper and buffer
539        if( file_cxy == local_cxy )
540        {
541            error = mapper_move( mapper,
542                                 to_buffer,
543                                 file_offset,
544                                 buffer,
545                                 size );
546        }
547        else
548        {
549            rpc_mapper_move_client( file_cxy,
550                                    mapper,
551                                    to_buffer,
552                                    file_offset,
553                                    buffer,
554                                    size,
555                                    &error );
556        } 
557
558        return error;
559    }
560    else if (inode_type == INODE_TYPE_DIR )
561    {
562        printk("\n[ERROR] in %s : inode is a directory", __FUNCTION__ );
563        return EINVAL;
564    }
565    else if (inode_type == INODE_TYPE_DEV )
566    {
567        // TODO
568        return 0;
569    }
570    else
571    {
572        printk("\n[PANIC] in %s : illegal inode type\n", __FUNCTION__ );
573        hal_core_sleep();
574        return -1;
575    }
576}  // end vfs_access()
577
578//////////////////////////////////////
579error_t vfs_lseek( xptr_t     file_xp,
580                   uint32_t   offset,
581                   uint32_t   whence, 
582                   uint32_t * new_offset )
583{
584    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
585    hal_core_sleep();
586    return 0;
587
588    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
589
590}  // vfs_lseek()
591
592///////////////////////////////////
593error_t vfs_close( xptr_t   file_xp,
594                   uint32_t file_id )
595{
596    assert( (file_xp != XPTR_NULL) , __FUNCTION__ , "file_xp == XPTR_NULL" );
597
598    assert( (file_id < CONFIG_PROCESS_FILE_MAX_NR) , __FUNCTION__ , "illegal file_id" );
599
600    thread_t  * this    = CURRENT_THREAD;
601    process_t * process = this->process;
602
603    // get cluster and local pointer on remote file descriptor
604    cxy_t        file_cxy = GET_CXY( file_xp );
605    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
606
607    // get local pointer on local cluster manager
608    cluster_t * cluster = LOCAL_CLUSTER;
609
610    // get owner process cluster and lpid
611    cxy_t   owner_cxy  = CXY_FROM_PID( process->pid );
612    lpid_t  lpid       = LPID_FROM_PID( process->pid );
613
614    // get extended pointers on copies root and lock
615    xptr_t root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] );
616    xptr_t lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] );
617
618    // take the lock protecting the copies
619    remote_spinlock_lock( lock_xp );
620
621    // 1) loop on the process descriptor copies to cancel all fd_array[file_id] entries
622    xptr_t  iter_xp;
623    XLIST_FOREACH( root_xp , iter_xp )
624    {
625        xptr_t      process_xp  = XLIST_ELEMENT( iter_xp , process_t , copies_list );
626        cxy_t       process_cxy = GET_CXY( process_xp );
627        process_t * process_ptr = (process_t *)GET_PTR( process_xp );
628
629        xptr_t lock_xp  = XPTR( process_cxy , &process_ptr->fd_array.lock );
630        xptr_t entry_xp = XPTR( process_cxy , &process_ptr->fd_array.array[file_id] );
631
632        // lock is required for atomic write of a 64 bits word
633        remote_rwlock_wr_lock( lock_xp );
634        hal_remote_swd( entry_xp , XPTR_NULL );
635        remote_rwlock_wr_unlock( lock_xp );
636
637        hal_wbflush();
638    }   
639
640    // 2) release memory allocated to file descriptor in remote cluster
641    if( file_cxy == local_cxy )             // file cluster is local
642    {
643        vfs_file_destroy( file_ptr );
644    }
645    else                                    // file cluster is local
646    {
647        rpc_vfs_file_destroy_client( file_cxy , file_ptr );
648    }
649
650    return 0;
651
652}  // end vfs_close()
653
654////////////////////////////////////
655error_t vfs_unlink( xptr_t   cwd_xp,
656                    char   * path )
657{
658    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
659    hal_core_sleep();
660    return 0;
661}  // vfs_unlink()
662
663///////////////////////////////////////
664error_t vfs_stat( xptr_t       file_xp,
665                  vfs_stat_t * k_stat )
666{
667    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
668    hal_core_sleep();
669    return 0;
670}
671
672////////////////////////////////////////////
673error_t vfs_readdir( xptr_t         file_xp,
674                     vfs_dirent_t * k_dirent )
675{
676    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
677    hal_core_sleep();
678    return 0;
679}
680
681//////////////////////////////////////
682error_t vfs_mkdir( xptr_t     file_xp,
683                   char     * path,
684                   uint32_t   mode )
685{
686    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
687    hal_core_sleep();
688    return 0;
689}
690
691////////////////////////////////////
692error_t vfs_rmdir( xptr_t   file_xp,
693                   char   * path )
694{
695    printk("\n[PANIC] %s non implemented\n", __FUNCTION__ );
696    hal_core_sleep();
697    return 0;
698}
699
700///////////////////////////////////
701error_t vfs_chdir( xptr_t   cwd_xp,
702                   char   * path )
703{
704    error_t           error;
705    xptr_t            inode_xp;     // extended pointer on target inode
706    cxy_t             inode_cxy;    // target inode cluster identifier       
707    vfs_inode_t     * inode_ptr;    // target inode local pointer
708    uint32_t          mode;         // lookup working mode       
709    vfs_inode_type_t  inode_type;   // target inode type
710
711    // set lookup working mode
712    mode = 0;
713
714    // get extended pointer on target inode
715    error = vfs_lookup( cwd_xp , path , mode , &inode_xp );
716
717    if( error ) return error;
718
719    // get inode cluster and local pointer
720    inode_cxy = GET_CXY( inode_xp );
721    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
722
723    // get inode type from remote file
724    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
725
726    if( inode_type != INODE_TYPE_DIR )
727    {
728        CURRENT_THREAD->errno = ENOTDIR;
729        return -1;
730    }
731
732    printk("\n[PANIC] %s non fully implemented\n", __FUNCTION__ );
733    hal_core_sleep();
734    return 0;
735}
736
737///////////////////////////////////
738error_t vfs_chmod( xptr_t   cwd_xp,
739                   char   * path,
740                   uint32_t rights )
741{
742    error_t           error;
743    xptr_t            inode_xp;     // extended pointer on target inode
744    cxy_t             inode_cxy;    // inode cluster identifier       
745    vfs_inode_t     * inode_ptr;    // inode local pointer
746    uint32_t          mode;         // lookup working mode
747    vfs_inode_type_t  inode_type;   // target inode type
748
749    // set lookup working mode
750    mode = 0;
751 
752    // get extended pointer on target inode
753    error = vfs_lookup( cwd_xp , path , mode , &inode_xp );
754
755    if( error ) return error;
756
757    // get inode cluster and local pointer
758    inode_cxy = GET_CXY( inode_xp );
759    inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
760   
761    // get inode type from remote inode
762    inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) );
763
764   
765    printk("\n[PANIC] %s non fully implemented\n", __FUNCTION__ );
766    hal_core_sleep();
767    return 0;
768}
769
770///////////////////////////////////
771error_t vfs_mkfifo( xptr_t   cwd_xp,
772                    char   * path,
773                    uint32_t rights )
774{
775    printk("\n[PANIC] in %s : not implemented yet\n", __FUNCTION__ );
776    hal_core_sleep(); 
777    return 0;
778}
779
780
781
782/////////////////////////////////////////////////////////////////////////////////////////r
783//            Inode Tree functions
784//////////////////////////////////////////////////////////////////////////////////////////
785
786//////////////////////////////////////////////////////////////////////////////////////////
787// This function is used by the vfs_lookup() function.
788// It takes an extended pointer on a remote inode (parent directory inode),
789// and check access_rights violation for the calling thread.
790// It can be used by any thread running in any cluster.
791//////////////////////////////////////////////////////////////////////////////////////////
792// @ inode_xp    : extended pointer on inode.
793// @ client_uid  : client thread user ID
794// @ client_gid  : client thread group ID
795// @ return true if access rights are violated.
796//////////////////////////////////////////////////////////////////////////////////////////
797bool_t vfs_access_denied( xptr_t   inode_xp,
798                          uint32_t client_uid,
799                          uint32_t client_gid )
800{
801    // get found inode cluster and local pointer
802    cxy_t         inode_cxy = GET_CXY( inode_xp );
803    vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
804
805    // get inode access mode, UID, and GID
806    // TODO uint32_t  mode = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->mode ) );
807    uid_t     uid  = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->uid  ) );
808    gid_t     gid  = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->gid  ) );
809
810    // FIXME : me must use mode
811    if( (uid == client_uid) || (gid == client_gid) ) return false;
812    else                                             return true;
813}
814
815//////////////////////////////////////////////////////////////////////////////////////////
816// This static function is used by the vfs_lookup() function.
817// It takes an extended pointer on a remote inode (parent directory inode), a directory
818// entry name, and returns an extended pointer on the child inode.
819// It can be used by any thread running in any cluster.
820//////////////////////////////////////////////////////////////////////////////////////////
821// @ parent_xp   : extended pointer on parent inode in remote cluster.
822// @ name        : dentry name
823// @ child_xp    : [out] buffer for extended pointer on child inode.
824// @ return true if success / return false if not found.
825//////////////////////////////////////////////////////////////////////////////////////////
826static bool_t vfs_get_child( xptr_t   parent_xp,
827                             char   * name,
828                             xptr_t * child_xp )
829{
830    xptr_t  xhtab_xp;    // extended pointer on hash table containing children dentries
831    xptr_t  dentry_xp;   // extended pointer on children dentry
832
833    // get parent inode cluster and local pointer
834    cxy_t         parent_cxy = GET_CXY( parent_xp );
835    vfs_inode_t * parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
836
837    // get extended pointer on hash table of children directory entries
838    xhtab_xp = XPTR( parent_cxy , &parent_ptr->children );
839
840    // search extended pointer on matching dentry
841    dentry_xp = xhtab_lookup( xhtab_xp , name );
842
843    if( dentry_xp == XPTR_NULL ) return false;
844
845    // get dentry cluster and local pointer
846    cxy_t          dentry_cxy = GET_CXY( dentry_xp );
847    vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
848
849    // return child inode
850    *child_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , &dentry_ptr->parent ) );
851    return true;
852}
853
854//////////////////////////////////////////////////////////////////////////////////////////
855// This static function is used by the vfs_lookup() function.
856// It takes the <current> pointer on a buffer containing a complete pathname, and return
857// in the <name> buffer, allocated by the caller, a single name in the path.
858// It return also in the <next> pointer the next character to analyse in the path.
859// Finally it returns a <last> boolean, that is true when the returned <name> is the
860// last name in the path. The names are supposed to be separated by one or several '/'
861// characters, that are not written in  the <name> buffer.
862//////////////////////////////////////////////////////////////////////////////////////////
863// @ current   : pointer on first character to analyse in buffer containing the path.
864// @ name      : [out] pointer on buffer allocated by the caller for the returned name.
865// @ next      : [out] pointer on next character to analyse in buffer containing the path.
866// @ last      : [out] true if the returned name is the last (NUL character found).
867// @ return 0 if success / return EINVAL if string empty (first chracter is NUL).
868//////////////////////////////////////////////////////////////////////////////////////////
869static error_t vfs_get_name_from_path( char     * current,
870                                       char     * name,
871                                       char    ** next,
872                                       bool_t   * last )
873{
874    char * ptr = current;
875
876    // skip leading '/' characters
877    while( *ptr == '/' ) ptr++;
878
879    // return EINVAL if string empty
880    if( *ptr == 0 ) return EINVAL;
881
882    // copy all characters in name until NUL or '/'
883    while( (*ptr != 0) && (*ptr !='/') )  *(name++) = *(ptr++);
884
885    // return last an next
886    if( *ptr == 0 )             // last found character is NUL => last name in path
887    {
888        *last = true;
889    }
890    else                        // last found character is '/' => skip it
891    {
892        *last = false;
893        *next = ptr + 1;
894    }
895
896    return 0;
897}
898
899//////////////////////////////////////////////
900error_t vfs_lookup( xptr_t             cwd_xp,
901                    char             * pathname,
902                    uint32_t           mode,
903                                        xptr_t           * inode_xp )
904{
905    char          name[CONFIG_VFS_MAX_NAME_LENGTH];   // one name in path
906
907    xptr_t             parent_xp;    // extended pointer on parent inode
908    cxy_t              parent_cxy;   // cluster for parent inode
909    vfs_inode_t      * parent_ptr;   // local pointer on parent inode 
910    xptr_t             child_xp;     // extended pointer on child inode
911    cxy_t              child_cxy;    // cluster for child inode
912    vfs_inode_t      * child_ptr;    // local pointer on child inode 
913    vfs_inode_type_t   inode_type;   // child inode type
914    vfs_fs_type_t      fs_type;      // File system type
915    vfs_ctx_t        * ctx_ptr;      // local pointer on FS context
916    char             * current;      // current pointer on path
917    char             * next;         // next value for current pointer   
918    bool_t             last;         // true when the name is the last in path
919    bool_t             found;        // true when a child has been found
920    thread_t         * this;         // pointer on calling thread descriptor
921    process_t        * process;      // pointer on calling process descriptor
922    error_t            error;
923
924    this    = CURRENT_THREAD;
925    process = this->process;
926
927    // get extended pointer on first inode to search
928    if( pathname[0] == '/' ) parent_xp = process->vfs_root_xp;
929    else                     parent_xp = cwd_xp;
930
931    // initialise loop variables
932    current  = pathname;
933    next     = NULL;
934    last     = false;
935    child_xp = XPTR_NULL;
936
937    // take lock on parent inode
938    vfs_inode_remote_lock( parent_xp );
939
940    // break : if one intermediate name not found
941    // exit  : when last name found (i.e. last == true)
942    do
943    {
944        // get one name from path and the last flag
945        vfs_get_name_from_path( current , name , &next , &last );
946
947        // search a child dentry matching name for parent inode
948        found = vfs_get_child( parent_xp,
949                               name,
950                               &child_xp );
951
952        if( found == false ) // child inode not found in inode tree => try to load it
953        {
954            // release lock on parent inode
955            vfs_inode_remote_unlock( parent_xp );
956
957            // get cluster and local pointer on parent inode
958            parent_cxy = GET_CXY( parent_xp );
959            parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
960
961            // get parent inode FS type
962            ctx_ptr = (vfs_ctx_t *)hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->ctx ) );
963            fs_type = ctx_ptr->type;
964
965            // get child inode type
966            if( (last == false) || (mode & VFS_LOOKUP_DIR) ) inode_type = INODE_TYPE_DIR;
967            else                                             inode_type = INODE_TYPE_FILE;
968
969            // insert a child dentry/inode in parent inode
970            error = vfs_add_child_in_parent( inode_type,
971                                             fs_type, 
972                                             parent_xp, 
973                                             name, 
974                                             &child_xp );
975
976            if( error )
977            {
978                printk("\n[ERROR] in %s : inode %s not found in path %s\n",
979                       __FUNCTION__ , name , pathname );
980                return ENOENT;
981            }
982
983            // take lock on parent inode
984            vfs_inode_remote_lock( parent_xp );
985        }
986
987        // check access rights
988        // error = vfs_access_denied( child_xp,
989        //                            client_uid,
990        //                            client_gid );
991        // if( error )
992        // {
993        //     printk("\n[ERROR] in %s : permission denied for %s\n", __FUNCTION__ , name );
994        //     return EACCES;
995        // }
996
997        // take lock on child inode if not last
998        if( last == false ) vfs_inode_remote_lock( child_xp );
999
1000        // release lock on parent inode
1001        vfs_inode_remote_unlock( parent_xp );
1002
1003        // update loop variables
1004        parent_xp = child_xp;
1005        current   = next;
1006    }
1007    while( last == false );
1008
1009    vfs_dmsg("\n[INFO] in %s : searched inode found for %s\n",
1010                 __FUNCTION__ , pathname );
1011
1012    // get cluster and local pointer on child inode
1013    child_cxy = GET_CXY( child_xp );
1014    child_ptr = (vfs_inode_t *)GET_PTR( child_xp );
1015
1016    // return searched pointers
1017    *inode_xp = child_xp;
1018
1019    return 0;
1020
1021}  // end vfs_lookup()
1022
1023
1024////////////////////////////////////////////
1025error_t vfs_get_path( xptr_t    searched_xp,
1026                      char    * buffer,
1027                      uint32_t  max_size )
1028{
1029        xptr_t       dentry_xp;   // extended pointer on current dentry
1030    char       * name;        // local pointer on current dentry name
1031        uint32_t     length;      // length of current dentry name
1032        uint32_t     count;       // number of characters written in buffer
1033        uint32_t     index;       // slot index in buffer
1034    xptr_t       inode_xp;    // extended pointer on   
1035
1036    // implementation note:
1037    // we use two variables "index" and "count" because the buffer
1038    // is actually written in decreasing index order (from leaf to root)
1039    // TODO : handle conflict with a concurrent rename
1040    // FIXME : handle synchro in the loop ... [AG]
1041
1042        // set the NUL character in buffer / initialise buffer index and count
1043        buffer[max_size - 1] = 0;
1044        count    = 1;
1045    index    = max_size - 2;
1046
1047    // initialize current inode
1048    inode_xp  = searched_xp;
1049
1050    // exit when root inode found (i.e. dentry_xp == XPTR_NULL)
1051        do
1052    {
1053        // get inode cluster and local pointer
1054        cxy_t         inode_cxy = GET_CXY( inode_xp );
1055        vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp );
1056
1057        // get extended pointer on parent dentry               
1058        dentry_xp = (xptr_t)hal_remote_lwd( XPTR( inode_cxy , inode_ptr->parent_xp ) );
1059
1060        // get dentry cluster and local pointer
1061        cxy_t          dentry_cxy = GET_CXY( dentry_xp );
1062        vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp );
1063
1064        // get dentry name length and pointer
1065        length =  hal_remote_lw( XPTR( dentry_cxy , &dentry_ptr->length ) );
1066        name   = (char *)hal_remote_lpt( XPTR( dentry_cxy , &dentry_ptr->name ) );
1067
1068        // update index and count
1069        index -= (length + 1); 
1070        count += (length + 1);
1071
1072        // check buffer overflow
1073        if( count >= max_size )
1074        {
1075            printk("\n[ERROR] in %s : kernel buffer too small\n", __FUNCTION__ );
1076            return EINVAL;
1077        }
1078
1079        // update pathname
1080        hal_remote_memcpy( XPTR( local_cxy , &buffer[index + 1] ) ,
1081                           XPTR( dentry_cxy , name ) , length );
1082                buffer[index] = '/';
1083
1084                // get extended pointer on next inode
1085        inode_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , dentry_ptr->parent ) );
1086    }
1087    while( (dentry_xp != XPTR_NULL) );
1088
1089        return 0;
1090
1091}  // end vfs_get_path()
1092
1093///////////////////////////////////////////////////////////////
1094error_t vfs_add_child_in_parent( vfs_inode_type_t   inode_type,
1095                                 vfs_fs_type_t      fs_type,
1096                                 xptr_t             parent_xp,
1097                                 char             * name,
1098                                 xptr_t           * child_xp )
1099{
1100    error_t         error;
1101    xptr_t          dentry_xp;   // extended pointer on created dentry
1102    xptr_t          inode_xp;    // extended pointer on created inode
1103    cxy_t           parent_cxy;  // parent inode cluster identifier
1104    vfs_inode_t   * parent_ptr;  // parent inode local pointer
1105    vfs_ctx_t     * parent_ctx;  // parent inode context local pointer
1106
1107    // get parent inode cluster and local pointer
1108    parent_cxy = GET_CXY( parent_xp );
1109    parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp );
1110
1111    // get parent inode context local pointer
1112    parent_ctx = (vfs_ctx_t *)hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->ctx ) );
1113
1114    // create dentry
1115    if( parent_cxy == local_cxy )      // parent cluster is the local cluster
1116    {
1117        error = vfs_dentry_create( fs_type,
1118                                   name,
1119                                   parent_ptr,
1120                                   &dentry_xp );
1121    }
1122    else                               // parent cluster is remote
1123    {
1124        rpc_vfs_dentry_create_client( parent_cxy,
1125                                      fs_type,
1126                                      name,
1127                                      parent_ptr,
1128                                      &dentry_xp,
1129                                      &error );
1130    }
1131                                     
1132    if( error )
1133    {
1134        printk("\n[ERROR] in %s : cannot create dentry in cluster %x\n",
1135               __FUNCTION__ , parent_cxy );
1136
1137        return error;
1138    }
1139
1140    // select a target cluster for child inode
1141    uint32_t  x_size    = LOCAL_CLUSTER->x_size;
1142    uint32_t  y_size    = LOCAL_CLUSTER->y_size;
1143    uint32_t  y_width   = LOCAL_CLUSTER->y_width;
1144    uint32_t  index     = ( hal_time_stamp() + hal_get_gid() ) % (x_size * y_size);
1145    uint32_t  x         = index / y_size;   
1146    uint32_t  y         = index % y_size;
1147    cxy_t     child_cxy = (x<<y_width) + y;
1148                                     
1149    // create child inode TODO : define attr / mode / uid / gid
1150    uint32_t attr = 0;
1151    uint32_t mode = 0;
1152    uint32_t uid  = 0;
1153    uint32_t gid  = 0;
1154   
1155    if( child_cxy == local_cxy )      // child cluster is the local cluster
1156    {
1157        error = vfs_inode_create( dentry_xp,
1158                                  fs_type,
1159                                  inode_type,
1160                                  attr,
1161                                  mode,
1162                                  uid,
1163                                  gid,
1164                                  &inode_xp );
1165    }
1166    else                              // child cluster is remote
1167    {
1168        rpc_vfs_inode_create_client( child_cxy,
1169                                     dentry_xp,
1170                                     fs_type,
1171                                     inode_type,
1172                                     attr,
1173                                     mode,
1174                                     uid,
1175                                     gid,
1176                                     &inode_xp,
1177                                     &error );
1178    }
1179                                     
1180    if( error )
1181    {
1182        printk("\n[ERROR] in %s : cannot create inode in cluster %x\n",
1183               __FUNCTION__ , child_cxy );
1184 
1185        vfs_dentry_t * dentry = (vfs_dentry_t *)GET_PTR( dentry_xp );
1186        if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry );
1187        else rpc_vfs_dentry_destroy_client( parent_cxy , dentry );
1188        return error;
1189    }
1190
1191    // success : return extended pointer on child inode
1192    *child_xp = inode_xp;
1193    return 0;
1194
1195}  // end vfs_add_child_in_parent()
1196
1197
1198
1199
1200//////////////////////////////////////////////////////////////////////////////////////////
1201//            Mapper related functions
1202//////////////////////////////////////////////////////////////////////////////////////////
1203
1204////////////////////////////////////////////////
1205error_t vfs_move_page_to_mapper( page_t * page )
1206{
1207    error_t         error = 0;
1208
1209    assert( (page != NULL) , __FUNCTION__ , "page pointer is NULL\n" );
1210
1211    mapper_t * mapper = page->mapper;
1212
1213    assert( (mapper != NULL) , __FUNCTION__ , "no mapper for page\n" );
1214
1215    // get FS type
1216    vfs_fs_type_t fs_type = mapper->inode->ctx->type;
1217
1218    // update mapper if permitted by file system type
1219    if( fs_type == FS_TYPE_FATFS )
1220    {
1221        // get mapper lock in WRITE_MODE
1222        rwlock_wr_lock( &mapper->lock );
1223
1224        error = fatfs_read_page( page ); 
1225
1226        // release mapper lock
1227        rwlock_wr_unlock( &mapper->lock );
1228    }
1229    else if( fs_type == FS_TYPE_RAMFS )
1230    {
1231        assert( false , __FUNCTION__ , "should not be called for RAMFS\n" );
1232    }
1233    else if( fs_type == FS_TYPE_DEVFS )
1234    {
1235        assert( false , __FUNCTION__ , "should not be called for DEVFS\n" );
1236    }
1237    else
1238    {
1239        assert( false , __FUNCTION__ , "undefined file system type\n" );
1240    }
1241
1242    return error;
1243
1244}  // end vfs_move_page_to_mapper()
1245
1246//////////////////////////////////////////////////
1247error_t vfs_move_page_from_mapper( page_t * page )
1248{
1249    error_t         error = 0;
1250
1251    assert( (page != NULL) , __FUNCTION__ , "page pointer is NULL\n" );
1252
1253    mapper_t * mapper = page->mapper;
1254
1255    assert( (mapper != NULL) , __FUNCTION__ , "no mapper for page\n" );
1256
1257    // get FS type
1258    vfs_fs_type_t  fs_type = mapper->inode->ctx->type;
1259
1260    // update file system if permitted by file system type
1261    if( fs_type == FS_TYPE_FATFS )
1262    {
1263            if( page_is_flag( page , PG_DIRTY ) ) 
1264            {
1265            // get mapper lock in READ_MODE
1266            rwlock_rd_lock( &mapper->lock );
1267
1268            error = fatfs_write_page( page );
1269
1270            // release mapper lock from READ_MODE
1271            rwlock_rd_unlock( &mapper->lock );
1272
1273            // clear dirty bit if success
1274                    if( error == 0 ) page_undo_dirty( page );
1275         }
1276    }
1277    else if( fs_type == FS_TYPE_RAMFS )
1278    {
1279        assert( false , __FUNCTION__ , "should not be called for RAMFS\n" );
1280    }
1281    else if( fs_type == FS_TYPE_DEVFS )
1282    {
1283        assert( false , __FUNCTION__ , "should not be called for DEVFS\n" );
1284    }
1285    else
1286    {
1287        assert( false , __FUNCTION__ , "undefined file system type\n" );
1288    }
1289       
1290    return error;
1291
1292}  // end vfs_move_page_from_mapper()
1293
1294
Note: See TracBrowser for help on using the repository browser.