Index: trunk/kernel/mm/mapper.c
===================================================================
--- trunk/kernel/mm/mapper.c	(revision 18)
+++ trunk/kernel/mm/mapper.c	(revision 23)
@@ -26,4 +26,5 @@
 #include <hal_types.h>
 #include <hal_special.h>
+#include <hal_uspace.h>
 #include <grdxt.h>
 #include <rwlock.h>
@@ -108,5 +109,5 @@
         {
             // remove page from mapper and release to PPM
-	        error = mapper_release_page( mapper , found_index , page );
+	        error = mapper_release_page( mapper , page );
 
             if ( error ) return error;
@@ -148,5 +149,5 @@
 
     // test if page available in mapper
-    if( ( page == NULL) || page_is_flag( page , PG_INLOAD ) )  // page not available            /
+    if( ( page == NULL) || page_is_flag( page , PG_INLOAD ) )  // page not available 
     {
         // release the lock in READ_MODE and take it in WRITE_MODE
@@ -194,5 +195,5 @@
                 printk("\n[ERROR] in %s : thread %x cannot insert page in mapper\n",
                        __FUNCTION__ , this->trdid );
-                mapper_release_page( mapper , index , page );
+                mapper_release_page( mapper , page );
                 page_clear_flag( page , PG_ALL );
                 req.ptr  = page;
@@ -203,5 +204,5 @@
 
             // launch I/O operation to load page from file system
-            error = mapper_updt_page( mapper , index , page );
+            error = vfs_move_page_to_mapper( page );
 
             if( error )
@@ -209,5 +210,5 @@
                 printk("\n[ERROR] in %s : thread %x cannot load page from device\n",
                        __FUNCTION__ , this->trdid );
-                mapper_release_page( mapper , index , page );
+                mapper_release_page( mapper , page );
                 page_clear_flag( page , PG_ALL );
                 req.ptr  = page;
@@ -255,5 +256,4 @@
 ///////////////////////////////////////////////
 error_t mapper_release_page( mapper_t * mapper,
-                             uint32_t   index,
                              page_t   * page )
 {
@@ -261,5 +261,5 @@
 
     // lauch IO operation to update page to file system
-    error = mapper_sync_page( mapper , index , page );
+    error = vfs_move_page_from_mapper( page );
 
     if( error )
@@ -288,306 +288,71 @@
 }  // end mapper_release_page()
 
-////////////////////////////////////////////
-error_t mapper_updt_page( mapper_t * mapper,
-                          uint32_t   index,
-                          page_t   * page )
-{
-    uint32_t      type;
-    vfs_inode_t * inode;
-    error_t       error = 0;
-
-    if( page == NULL )
-    {
-        printk("\n[PANIC] in %s : page pointer is NULL\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    if( mapper == NULL )
-    {
-        printk("\n[PANIC] in %s : no mapper for this page\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    // get file system type and inode pointer
-    inode = mapper->inode;
-    type  = inode->ctx->type;
-
-    // get page lock
-    page_lock( page );
-
-    // get mapper lock in WRITE_MODE
-    rwlock_wr_lock( &mapper->lock );
-
-    // call proper I/O operation to update file system
-    if     ( type == FS_TYPE_FATFS ) error = fatfs_read_page( page );
-    else if( type == FS_TYPE_RAMFS ) error = ramfs_read_page( page );
-    else
-    {
-        printk("\n[PANIC] in %s : undefined file system type\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    // release mapper lock from WRITE_MODE
-    rwlock_wr_unlock( &mapper->lock );
-
-    // release page lock
-    page_unlock( page );
-
-    if( error )
-    {
-        printk("\n[PANIC] in %s : cannot access file system\n", __FUNCTION__ );
-        return EIO;
-    }
-
-    return 0;
-}  // end mapper_updt_page
-
-////////////////////////////////////////////
-error_t mapper_sync_page( mapper_t * mapper,
-                          uint32_t   index,
-                          page_t   * page )
-{
-    uint32_t      type;
-    vfs_inode_t * inode;
-    error_t       error = 0;
-
-    if( page == NULL )
-    {
-        printk("\n[PANIC] in %s : page pointer is NULL\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    if( mapper == NULL )
-    {
-        printk("\n[PANIC] in %s : no mapper for this page\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-	if( page_is_flag( page , PG_DIRTY ) )
-	{
-        // get file system type and inode pointer
-        inode = mapper->inode;
-        type  = inode->ctx->type;
-
-        // get page lock
-        page_lock( page );
-
-        // get mapper lock in READ_MODE
-        rwlock_rd_lock( &mapper->lock );
-
-        // call proper I/O operation to update file system
-        if     ( type == FS_TYPE_FATFS ) error = fatfs_write_page( page );
-        else if( type == FS_TYPE_RAMFS ) error = ramfs_write_page( page );
-        else
-        {
-            printk("\n[PANIC] in %s : undefined file system type\n", __FUNCTION__ );
-            hal_core_sleep();
-        }
-
-        // release mapper lock from READ_MODE
-        rwlock_rd_unlock( &mapper->lock );
-
-        // release page lock
-        page_unlock( page );
-
-        if( error )
-        {
-            printk("\n[PANIC] in %s : cannot update file system\n", __FUNCTION__ );
-            return EIO;
-        }
-
-        // clear dirty bit
-		page_undo_dirty( page );
-     }
-
-    return 0;
-
-}  // end mapper_sync_page()
-
-///////////////////////////////////////////////////////////////////////////////////////
-// This static function is called by the mapper_move fragments() function.
-// It moves one fragment between an user buffer and the kernel mapper.
-// Implementation Note: It can require access to one or two pages in mapper:
-//  [max_page_index == min_page_index]     <=>  fragment fit in one mapper page
-//  [max_page index == min_page_index + 1] <=>  fragment spread on two mapper pages
-///////////////////////////////////////////////////////////////////////////////////////
-static error_t mapper_move_one_fragment( mapper_t   * mapper,
-                                         bool_t       to_buffer,
-                                         fragment_t * fragment )
-{
-    uint32_t   size;                 // number of bytes in fragment
-    cxy_t      buf_cxy;              // cluster identifier for user buffer
-    uint8_t  * buf_ptr;              // local pointer on first byte in user buffer
-
-    xptr_t     xp_buf;               // extended pointer on byte in user buffer
-    xptr_t     xp_map;               // extended pointer on byte in kernel mapper
-
-    uint32_t   min_file_offset;      // offset of first byte in file
-    uint32_t   max_file_offset;      // offset of last byte in file
-
-    uint32_t   first_page_index;     // index of first page in mapper
-    uint32_t   first_page_offset;    // offset of first byte in first page in mapper
-    uint32_t   first_page_size;      // offset of first byte in first page in mapper
-
-    uint32_t   second_page_index;    // index of last page in mapper
-    uint32_t   second_page_offset;   // offset of last byte in last page in mapper
-    uint32_t   second_page_size;     // offset of last byte in last page in mapper
-
-    page_t   * page;                 // pointer on one page descriptor in mapper
-    uint8_t  * map_ptr;              // local pointer on first byte in mapper
-
-    // get fragment attributes in user buffer
-    buf_cxy = fragment->buf_cxy;
-    buf_ptr = fragment->buf_ptr;
-    size    = fragment->size;
-
-    if( size > CONFIG_PPM_PAGE_SIZE )
-    {
-        printk("\n[PANIC] in %s : illegal fragment size = %d\n",
-               __FUNCTION__ , size );
-        return EINVAL;
-    }
+/////////////////////////////////////////
+error_t mapper_move( mapper_t  *  mapper,
+                     bool_t       to_buffer,
+                     uint32_t     file_offset,
+                     void      *  buffer,
+                     uint32_t     size )
+{
+    uint32_t   page_offset;    // first byte to move to/from a mapper page
+    uint32_t   page_count;     // number of bytes to move to/from a mapper page
+    uint32_t   index;          // current mapper page index
+    uint32_t   done;           // number of moved bytes
+    page_t   * page;           // current mapper page descriptor
+    uint8_t  * map_ptr;        // current mapper  address
+    uint8_t  * buf_ptr;        // current buffer  address
 
     // compute offsets of first and last bytes in file
-    min_file_offset = fragment->file_offset;
-    max_file_offset = min_file_offset + size;
+    uint32_t min_byte = file_offset;
+    uint32_t max_byte = file_offset + size -1;
 
     // compute indexes of pages for first and last byte in mapper
-    first_page_index  = min_file_offset >> CONFIG_PPM_PAGE_SHIFT;
-    second_page_index = max_file_offset >> CONFIG_PPM_PAGE_SHIFT;
-
-    if ( first_page_index == second_page_index )  // only one page in mapper
-    {
-        // compute offset and size for page in mapper
-        first_page_offset = min_file_offset & (1<<CONFIG_PPM_PAGE_SHIFT);
-        first_page_size   = size;
-
-        // get pointer on first page in mapper
-        page = mapper_get_page( mapper , first_page_index );
+    uint32_t first = min_byte >> CONFIG_PPM_PAGE_SHIFT;
+    uint32_t last  = max_byte >> CONFIG_PPM_PAGE_SHIFT;
+
+    done = 0;
+
+    // loop on pages in mapper
+    for( index = first ; index <= last ; index++ )
+    {
+        // compute page_offset 
+        if( index == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK;
+        else                 page_offset = 0;
+
+        // compute page_count 
+        if      ( first == last  ) page_count = size;
+        else if ( index == first ) page_count = CONFIG_PPM_PAGE_SIZE - page_offset;
+        else if ( index == last  ) page_count = (max_byte & CONFIG_PPM_PAGE_MASK) + 1;
+        else                       page_count = CONFIG_PPM_PAGE_SIZE;
+
+        // get page descriptor
+        page = mapper_get_page( mapper , index );
 
         if ( page == NULL ) return EINVAL;
 
-        // compute pointer on fragment first byte in mapper
-        map_ptr = (uint8_t *)ppm_page2base( page ) + first_page_offset;
-
-        // compute extended pointers in mapper and in buffer
-        xp_map = XPTR( local_cxy , map_ptr );
-        xp_buf = XPTR( buf_cxy , buf_ptr );
+        // compute pointer in mapper
+        map_ptr = (uint8_t *)ppm_page2base( page ) + page_offset;
+
+        // compute pointer in buffer
+        buf_ptr = (uint8_t *)buffer + done;
 
         // move fragment
         if( to_buffer )
         {
-            hal_remote_memcpy( xp_buf , xp_map , first_page_size );
+            hal_copy_to_uspace( buf_ptr , map_ptr , page_count );
         }
         else
         {
             page_do_dirty( page );
-            hal_remote_memcpy( xp_map , xp_buf , first_page_size );
-        }
-    }
-    else                                        // two pages in mapper
-    {
-        // compute offset and size for first page in mapper
-        first_page_offset = min_file_offset & (1<<CONFIG_PPM_PAGE_SHIFT);
-        first_page_size   = CONFIG_PPM_PAGE_SIZE - first_page_offset;
-
-        // get pointer on first page descriptor in mapper
-        page = mapper_get_page( mapper , first_page_index );
-
-        if ( page == NULL ) return EINVAL;
-
-        // compute local pointer on first byte in first page in mapper
-        map_ptr = (uint8_t *)ppm_page2base(page) + first_page_offset;
-
-        // compute extended pointers
-        xp_map = XPTR( local_cxy , map_ptr );
-        xp_buf = XPTR( buf_cxy , buf_ptr );
-
-        // move fragment to/from first page
-        if( to_buffer )
-        {
-            hal_remote_memcpy( xp_buf , xp_map , first_page_size );
-        }
-        else
-        {
-            page_do_dirty( page );
-            hal_remote_memcpy( xp_map , xp_buf , first_page_size );
-        }
-
-        // compute offset and size for second page in mapper
-        second_page_offset = 0;
-        second_page_size   = size - first_page_size;
-
-        // get pointer on second page in mapper
-        page = mapper_get_page( mapper , second_page_index );
-
-        if ( page == NULL ) return EINVAL;
-
-        // compute local pointer on first byte in second page in mapper
-        map_ptr = (uint8_t *)ppm_page2base( page ) + second_page_offset;
-
-        // compute extended pointers
-        xp_map = XPTR( local_cxy , map_ptr );
-        xp_buf = XPTR( buf_cxy , buf_ptr + first_page_offset );
-
-        // move fragment to/from second page
-        if( to_buffer )
-        {
-            hal_remote_memcpy( xp_buf , xp_map , second_page_size );
-        }
-        else
-        {
-            page_do_dirty( page );
-            hal_remote_memcpy( xp_map , xp_buf , second_page_size );
-        }
+            hal_copy_from_uspace( map_ptr , buf_ptr , page_count );
+        }
+
+        done += page_count;
     }
 
     return 0;
-}  // end mapper_move_one_fragment()
-
-/////////////////////////////////////////////////
-error_t mapper_move_fragments( mapper_t * mapper,
-                               bool_t     read,
-                               uint32_t   nb_frags,
-                               xptr_t     xp_frags )
-{
-	uint32_t     index;
-	error_t      error;
-    fragment_t   local_frags[CONFIG_MAPPER_MAX_FRAGMENTS];   // local copy of fragments array
-    fragment_t * frags_array;                                // pointer on fragments array
-
-    // check nb_frags
-    if( nb_frags > CONFIG_MAPPER_MAX_FRAGMENTS )
-    {
-        printk("\n[PANIC] in %s : number of fragments cannot be larger than %d\n",
-               __FUNCTION__ , CONFIG_MAPPER_MAX_FRAGMENTS );
-        return EINVAL;
-    }
-
-    // get client cluster and local pointer on fragments array
-    cxy_t        client_cxy   = GET_CXY( xp_frags );
-    fragment_t * client_frags = (fragment_t *)GET_PTR( xp_frags );
-
-    if ( local_cxy == client_cxy ) // use the local fragments array if possible
-    {
-        frags_array = client_frags;
-    }
-    else                           // make a local copy of fragments array
-    {
-        hal_remote_memcpy( XPTR( local_cxy , local_frags ) , xp_frags ,
-                           sizeof(fragment_t) * nb_frags );
-        frags_array = local_frags;
-    }
-
-    // loop on fragments
-    for( index = 0 ; index < nb_frags ; index ++ )
-    {
-        error = mapper_move_one_fragment( mapper , read , &frags_array[index] );
-        if ( error ) return error;
-    }
-
-    return 0;
-
-}  // end mapper_move_fragments()
-
-
+
+}  // end mapper_move()
+
+
+
