Index: trunk/kernel/vfs/fatfs.c
===================================================================
--- trunk/kernel/vfs/fatfs.c	(revision 238)
+++ trunk/kernel/vfs/fatfs.c	(revision 246)
@@ -73,4 +73,29 @@
 }
 
+//////////////////////////////////////////////////////////////////////////////////////////
+// This function display the content of the FATFS context.
+//////////////////////////////////////////////////////////////////////////////////////////
+void fatfs_ctx_display()
+{
+    uint32_t      type      = FS_TYPE_FATFS;
+    vfs_ctx_t   * vfs_ctx   = &fs_context[FS_TYPE_FATFS];
+    fatfs_ctx_t * fatfs_ctx = (fatfs_ctx_t *)vfs_ctx->extend;
+
+    printk("\n*** FAT context ***\n" 
+           "- fat_sectors      = %d\n"
+           "- sector size      = %d\n"
+           "- cluster size     = %d\n"
+           "- fat_first_lba    = %d\n"
+           "- data_first_lba   = %d\n"
+           "- root_dir_cluster = %d\n"
+           "- mapper_xp        = %l\n",
+           fatfs_ctx->fat_sectors_count,
+           fatfs_ctx->bytes_per_sector,
+           fatfs_ctx->sectors_per_cluster * fatfs_ctx->bytes_per_sector,
+           fatfs_ctx->fat_begin_lba,
+           fatfs_ctx->cluster_begin_lba,
+           fatfs_ctx->root_dir_cluster,
+           fatfs_ctx->fat_mapper_xp );
+}
 
 //////////////////////////////////////////////////////////////////////////////////////////
@@ -280,5 +305,5 @@
 
 //////////////////////////////////////////////////////////////////////////////////////////
-//              FATFS specific but public functions (used by RPC_FATFS_GET_CLUSTER) 
+//              FATFS specific but extern function (used by RPC_FATFS_GET_CLUSTER) 
 //////////////////////////////////////////////////////////////////////////////////////////
 
@@ -296,5 +321,20 @@
     uint32_t   current_cluster;        // content of current FAT slot
 
-    // compute number of FAT slots per PPM page
+    fatfs_dmsg("\n[INFO] %s : enters / first_cluster_id = %d / searched_page = %d\n",
+               __FUNCTION__ , first_cluster , searched_page );
+
+#if CONFIG_FATFS_DEBUG
+    uint32_t * buf = (uint32_t *)ppm_page2vaddr( mapper_get_page ( mapper , 0 ) );
+    uint32_t line , word;
+    printk("\n***  FAT content for first 128 entries ***\n");
+    for( line = 0 ; line < 8 ; line++ )
+    {
+        printk("%d : ");
+        for( word = 0 ; word < 16 ; word++ ) printk("%X ", buf[(line<<4) + word] );
+        printk("\n");
+    }
+#endif
+
+    // compute number of FAT slots per page
     uint32_t slots_per_page = CONFIG_PPM_PAGE_SIZE >> 2;
 
@@ -307,4 +347,8 @@
     while( page_count_in_file <= searched_page )
     {
+
+        fatfs_dmsg("\n[INFO] %s : page_index = %d / page_offset = %d / count = %d\n",
+               __FUNCTION__ , current_page_index , current_page_offset , page_count_in_file );
+
         // get pointer on current page descriptor
         current_page_desc = mapper_get_page( mapper , current_page_index );
@@ -324,5 +368,7 @@
     }
     
-    // return success
+    fatfs_dmsg("\n[INFO] %s : exit / cluster_id = %d\n",
+               __FUNCTION__ , current_cluster );
+
     *cluster = current_cluster;
     return 0;
@@ -355,15 +401,8 @@
     uint8_t     * buffer;
 
-    fatfs_dmsg("\n[INFO] %s : enters at cycle %d\n", 
-               __FUNCTION__ , hal_get_cycles() );
-
-    // allocate memory for FATFS context
-	req.type    = KMEM_FATFS_CTX;
-	req.size    = sizeof(fatfs_ctx_t);
-    req.flags   = AF_KERNEL | AF_ZERO;
-
-	fatfs_ctx = (fatfs_ctx_t *)kmem_alloc( &req );
-
-    nolock_assert( (fatfs_ctx != NULL) , __FUNCTION__ ,
+    fatfs_dmsg("\n[INFO] %s : enters for fatfs_ctx = %x\n",
+               __FUNCTION__ , fatfs_ctx );
+
+    assert( (fatfs_ctx != NULL) , __FUNCTION__ ,
                    "cannot allocate memory for FATFS context\n" );
 
@@ -373,5 +412,5 @@
 	buffer      = (uint8_t *)kmem_alloc( &req );
 
-    nolock_assert( (buffer != NULL) , __FUNCTION__ ,
+    assert( (buffer != NULL) , __FUNCTION__ ,
                    "cannot allocate memory for 512 bytes buffer\n" );
      
@@ -380,8 +419,8 @@
     error = dev_ioc_sync_read( buffer , 0 , 1 );
 
-    nolock_assert( (error == 0) , __FUNCTION__ ,
+    assert( (error == 0) , __FUNCTION__ ,
                    "cannot access boot record\n" );
 
-#if CONFIG_FATFS_DEBUG
+#if (CONFIG_FATFS_DEBUG > 1)
     uint32_t   line;
     uint32_t   byte = 0;
@@ -439,7 +478,10 @@
 
     // allocate a mapper for the FAT itself
-    mapper_t * fat_mapper = mapper_create();
+    mapper_t * fat_mapper = mapper_create( FS_TYPE_FATFS );
 
     assert( (fat_mapper != NULL) , __FUNCTION__ , "no memory for FAT mapper" );
+
+    // WARNING : the inode field MUST be NULL for the FAT mapper
+    fat_mapper->inode = NULL;
 
     // initialize the FATFS context
@@ -454,18 +496,4 @@
     fatfs_ctx->fat_mapper_xp         = XPTR( local_cxy , fat_mapper );
 
-    fatfs_dmsg("\n*** FAT context ***\n" 
-               "- fat_sectors     = %d\n"
-               "- sector size     = %d\n"
-               "- cluster size    = %d\n"
-               "- fat_first_lba   = %d\n"
-               "- data_first_lba  = %d\n"
-               "- mapper          = %l\n",
-               fatfs_ctx->fat_sectors_count,
-               fatfs_ctx->bytes_per_sector,
-               fatfs_ctx->bytes_per_cluster,
-               fatfs_ctx->fat_begin_lba,
-               fatfs_ctx->cluster_begin_lba,
-               fatfs_ctx->fat_mapper_xp );
-
 }  // end fatfs_ctx_init()
 
@@ -479,51 +507,80 @@
 }
 
-///////////////////////////////////////
-error_t fatfs_move_page( page_t * page,
-                         bool_t   to_mapper )
-{
+//////////////////////////////////////////////
+error_t fatfs_mapper_move_page( page_t * page,
+                                bool_t   to_mapper )
+{
+    error_t error;
+
+    // get pointer on source mapper and page index from page descriptor
+    mapper_t * mapper = page->mapper;
+    uint32_t   index  = page->index;
+
+    // get VFS inode pointer from mapper
+    vfs_inode_t * inode = mapper->inode;
+
+    fatfs_dmsg("\n[INFO] %s : enter for inode %x / page_index = %d / mapper = %x\n",
+               __FUNCTION__ , inode , index , mapper );
+
     // get memory buffer base address
     uint8_t * buffer = (uint8_t *)ppm_page2vaddr( page );
  
-    // get pointer on source mapper and page index from page descriptor
-    mapper_t * mapper      = page->mapper;
-    uint32_t   page_index  = page->index;
-
-    // get VFS inode pointer from mapper
-    vfs_inode_t * vfs_inode = mapper->inode;
-
-    // get first cluster index from VFS inode
-    uint32_t  first_cluster = (uint32_t)(intptr_t)vfs_inode->extend;
-
-    // get FATFS context pointer from VFS context
+    // get number of sectors from FATFS context
     fatfs_ctx_t * fatfs_ctx = (fatfs_ctx_t *)fs_context[FS_TYPE_FATFS].extend;
-
-    // get number of sectors 
     uint32_t count = fatfs_ctx->sectors_per_cluster;
 
-    // compute FATFS_cluster index for the accessed page
-    uint32_t cluster = 0;
-    error_t  error = fatfs_cluster_from_index( fatfs_ctx,
-                                               first_cluster,
-                                               page_index,
-                                               &cluster );
-    if( error ) return EIO;
-
-    // get lba from cluster
-    uint32_t lba = fatfs_lba_from_cluster( fatfs_ctx , cluster );
-
-    // access device
-    if( to_mapper ) error = dev_ioc_read ( buffer , lba , count );
-    else            error = dev_ioc_write( buffer , lba , count );     
-
-    if( error )
-    {
-        printk("\n[ERROR] in %s : cannot access IOC device\n", __FUNCTION__ );
-        return error;
-    } 
-
-    // successful access 
+    // analyse the mapper type : FAT or normal inode
+    if( inode == NULL )  // it is the FAT mapper
+    {
+        // get lba from page index 
+        uint32_t lba = fatfs_ctx->fat_begin_lba + (count * index);
+ 
+        fatfs_dmsg("\n[INFO] %s : for FAT / lba = %d\n",
+                   __FUNCTION__ , lba );
+
+        // access device
+        if( to_mapper ) error = dev_ioc_sync_read ( buffer , lba , count );
+        else            error = dev_ioc_write( buffer , lba , count );     
+
+        if( error ) return EIO;
+
+        fatfs_dmsg("\n[INFO] %s : exit for FAT / page_index = %d / mapper = %x\n",
+                   __FUNCTION__ , index , mapper );
+    }
+    else                 // it is a normal inode mapper
+    {
+        // get first cluster index from inode extension
+        uint32_t  first_cluster = (uint32_t)(intptr_t)inode->extend;
+
+        fatfs_dmsg("\n[INFO] %s : for inode %x / first cluster_id = %d\n",
+                   __FUNCTION__ , inode , first_cluster );
+    
+        // compute FATFS_cluster index for the accessed page
+        uint32_t cluster = 0;
+        error_t  error = fatfs_cluster_from_index( fatfs_ctx,
+                                                   first_cluster,
+                                                   index,
+                                                   &cluster );
+        if( error ) return EIO;
+
+        // get lba from cluster
+        uint32_t lba = fatfs_lba_from_cluster( fatfs_ctx , cluster );
+
+        fatfs_dmsg("\n[INFO] %s : for inode %x / page = %d / cluster_id = %d / lba = %x\n",
+                   __FUNCTION__ , inode , index , cluster , lba );
+
+        // access device
+        if( to_mapper ) error = dev_ioc_sync_read ( buffer , lba , count );
+        else            error = dev_ioc_write( buffer , lba , count );     
+
+        if( error ) return EIO;
+
+        fatfs_dmsg("\n[INFO] %s : exit for inode %x / page = %x / mapper = %x\n",
+                   __FUNCTION__ , inode , page , mapper );
+    }
+
     return 0;
-}
+
+}  // end fatfs_mapper_move_page()
 
 /////////////////////////////////////////////////////////////////
@@ -537,5 +594,5 @@
 
     fatfs_dmsg("\n[INFO] %s : enter for child <%s> in parent inode %l\n",
-               __FUNCTION__ , name , child_inode_xp );
+               __FUNCTION__ , name , XPTR( local_cxy , parent_inode ) );
 
     mapper_t * mapper = parent_inode->mapper;
@@ -641,6 +698,6 @@
     if ( found == -1 )  // found end of directory => failure 
     {
-        fatfs_dmsg("\n[INFO] %s : child <%s> not found in parent inode %l\n",
-                   __FUNCTION__ , name , parent_inode_xp );
+        fatfs_dmsg("\n[INFO] %s : exit / child <%s> not found in parent inode %l\n",
+                   __FUNCTION__ , name , XPTR( local_cxy , parent_inode ) );
 
         return ENOENT;
@@ -648,7 +705,4 @@
     else               // found searched child name
     {
-        fatfs_dmsg("\n[INFO] %s : child <%s> found in parent inode %l\n",
-                   __FUNCTION__ , name , parent_inode_xp );
-
         // get child inode cluster and local pointer
         cxy_t         child_cxy = GET_CXY( child_inode_xp );
@@ -662,4 +716,7 @@
         hal_remote_sw( XPTR( child_cxy , &child_ptr->extend ) , cluster );
 
+        fatfs_dmsg("\n[INFO] %s : exit / child <%s> found in parent inode %l\n",
+                   __FUNCTION__ , name , XPTR( local_cxy , parent_inode ) );
+
         return 0;
     }
