Index: trunk/kernel/kernel_config.h
===================================================================
--- trunk/kernel/kernel_config.h	(revision 590)
+++ trunk/kernel/kernel_config.h	(revision 607)
@@ -35,186 +35,198 @@
 ////////////////////////////////////////////////////////////////////////////////////////////
 
-#define DEBUG_BARRIER                  0
-
-#define DEBUG_BUSYLOCK                 1
-#define DEBUG_BUSYLOCK_THREAD_XP       0x0000000000ULL  // selected thread extended pointer
+#define DEBUG_BARRIER                     0
+
+#define DEBUG_BUSYLOCK                    1
+#define DEBUG_BUSYLOCK_THREAD_XP          0x0000000000ULL  // selected thread xptr
                  
-#define DEBUG_CHDEV_CMD_RX             0
-#define DEBUG_CHDEV_CMD_TX             0
-#define DEBUG_CHDEV_SERVER_RX          0
-#define DEBUG_CHDEV_SERVER_TX          0
-
-#define DEBUG_CLUSTER_INIT             0
-#define DEBUG_CLUSTER_PID_ALLOC        0
-#define DEBUG_CLUSTER_PID_RELEASE      0
-#define DEBUG_CLUSTER_PROCESS_COPIES   0
-
-#define DEBUG_DEV_TXT_RX               0
-#define DEBUG_DEV_TXT_TX               0
-#define DEBUG_DEV_IOC_RX               0
-#define DEBUG_DEV_IOC_TX               0
-#define DEBUG_DEV_NIC_RX               0
-#define DEBUG_DEV_NIC_RX               0
-#define DEBUG_DEV_FBF_RX               0
-#define DEBUG_DEV_FBF_TX               0
-#define DEBUG_DEV_DMA                  0
-#define DEBUG_DEV_MMC                  0
-#define DEBUG_DEV_PIC                  0
-
-#define DEBUG_DEVFS_INIT               0
-#define DEBUG_DEVFS_MOVE               0
-
-#define DEBUG_DQDT_INIT                0
-#define DEBUG_DQDT_UPDATE_THREADS      0
-#define DEBUG_DQDT_SELECT_FOR_PROCESS  0
-#define DEBUG_DQDT_UPDATE_PAGES        0
-#define DEBUG_DQDT_SELECT_FOR_MEMORY   0
-
-#define DEBUG_FATFS_INIT               0
-#define DEBUG_FATFS_MOVE               0
-#define DEBUG_FATFS_LOAD               0
-#define DEBUG_FATFS_GET_CLUSTER        0
-
-#define DEBUG_HAL_GPT_SET_PTE          0
-#define DEBUG_HAL_GPT_COPY             0
-#define DEBUG_HAL_GPT_CREATE           0
-#define DEBUG_HAL_GPT_DESTROY          0
-
-#define DEBUG_HAL_KENTRY               0
-#define DEBUG_HAL_EXCEPTIONS           0
-#define DEBUG_HAL_IRQS                 0       
-#define DEBUG_HAL_TXT_RX               0
-#define DEBUG_HAL_TXT_TX               0
-#define DEBUG_HAL_IOC_RX               0
-#define DEBUG_HAL_IOC_TX               0
-#define DEBUG_HAL_GPT                  0
-
-#define DEBUG_KCM                      0
-#define DEBUG_KMEM                     0
-
-#define DEBUG_KERNEL_INIT              0
-
-#define DEBUG_MAPPER_GET_PAGE          0
-#define DEBUG_MAPPER_MOVE_USER         0
-#define DEBUG_MAPPER_MOVE_KERNEL       0
-
-#define DEBUG_MUTEX                    0
-
-#define DEBUG_PPM_ALLOC_PAGES          0
-#define DEBUG_PPM_FREE_PAGES           0
-
-#define DEBUG_PROCESS_COPY_INIT        0
-#define DEBUG_PROCESS_DESTROY          0
-#define DEBUG_PROCESS_GET_LOCAL_COPY   0
-#define DEBUG_PROCESS_INIT_CREATE      0
-#define DEBUG_PROCESS_MAKE_EXEC        0
-#define DEBUG_PROCESS_MAKE_FORK        0
-#define DEBUG_PROCESS_REFERENCE_INIT   0
-#define DEBUG_PROCESS_SIGACTION        0
-#define DEBUG_PROCESS_TXT              0
-#define DEBUG_PROCESS_ZERO_CREATE      0
-
-#define DEBUG_QUEUELOCK                0
-
-#define DEBUG_RPC_CLIENT_GENERIC       0
-#define DEBUG_RPC_SERVER_GENERIC       0
-
-#define DEBUG_RPC_KCM_ALLOC            0
-#define DEBUG_RPC_KCM_FREE             0
-#define DEBUG_RPC_PMEM_GET_PAGES       0
-#define DEBUG_RPC_PMEM_RELEASE_PAGES   0
-#define DEBUG_RPC_PROCESS_MAKE_FORK    0
-#define DEBUG_RPC_PROCESS_SIGACTION    0
-#define DEBUG_RPC_THREAD_USER_CREATE   0
-#define DEBUG_RPC_THREAD_KERNEL_CREATE 0
-#define DEBUG_RPC_VFS_INODE_CREATE     0
-#define DEBUG_RPC_VFS_INODE_DESTROY    0
-#define DEBUG_RPC_VFS_DENTRY_CREATE    0
-#define DEBUG_RPC_VFS_DENTRY_DESTROY   0
-#define DEBUG_RPC_VFS_FILE_CREATE      0
-#define DEBUG_RPC_VFS_FILE_DESTROY     0
-#define DEBUG_RPC_VMM_GET_PTE          0
-#define DEBUG_RPC_VMM_GET_VSEG         0
-
-#define DEBUG_RWLOCK                   0
-
-#define DEBUG_SCHED_HANDLE_SIGNALS     2
-#define DEBUG_SCHED_YIELD              2    // must be activated by the trace() syscall
-#define DEBUG_SCHED_RPC_ACTIVATE       0
-
-#define DEBUG_SEM                      0
-
-#define DEBUG_SYSCALLS_ERROR           2
-
-#define DEBUG_SYS_BARRIER              0
-#define DEBUG_SYS_CLOSE                0
-#define DEBUG_SYS_CONDVAR              0
-#define DEBUG_SYS_DISPLAY              0
-#define DEBUG_SYS_EXEC                 0
-#define DEBUG_SYS_EXIT                 0
-#define DEBUG_SYS_FG                   0
-#define DEBUG_SYS_FORK                 0
-#define DEBUG_SYS_GET_CONFIG           0
-#define DEBUG_SYS_GETPID               0
-#define DEBUG_SYS_ISATTY               0
-#define DEBUG_SYS_IS_FG                0
-#define DEBUG_SYS_KILL                 0
-#define DEBUG_SYS_OPEN                 0
-#define DEBUG_SYS_MMAP                 0
-#define DEBUG_SYS_MUNMAP               0
-#define DEBUG_SYS_MUTEX                0
-#define DEBUG_SYS_READ                 0
-#define DEBUG_SYS_SEM                  0
-#define DEBUG_SYS_THREAD_CANCEL        0
-#define DEBUG_SYS_THREAD_CREATE        0
-#define DEBUG_SYS_THREAD_EXIT          0
-#define DEBUG_SYS_THREAD_JOIN          0
-#define DEBUG_SYS_THREAD_SLEEP         0
-#define DEBUG_SYS_THREAD_WAKEUP        0
-#define DEBUG_SYS_THREAD_YIELD         0
-#define DEBUG_SYS_TRACE                0
-#define DEBUG_SYS_WAIT                 0
-#define DEBUG_SYS_WRITE                0
-
-#define DEBUG_SPINLOCKS                0
-#define DEBUG_REMOTE_SPINLOCKS         0
-#define DEBUG_RWLOCKS                  0
-#define DEBUG_REMOTE_RWLOCKS           0
-
-#define DEBUG_THREAD_DELETE            0
-#define DEBUG_THREAD_DESTROY           0
-#define DEBUG_THREAD_GET_XPTR          0
-#define DEBUG_THREAD_IDLE              0
-#define DEBUG_THREAD_INIT              0
-#define DEBUG_THREAD_KERNEL_CREATE     0
-#define DEBUG_THREAD_USER_CREATE       0
-#define DEBUG_THREAD_USER_FORK         0
-#define DEBUG_THREAD_USER_EXEC         0
-#define DEBUG_THREAD_BLOCK             0
-
-#define DEBUG_VFS_ADD_CHILD            0
-#define DEBUG_VFS_CLOSE                0
-#define DEBUG_VFS_DENTRY_CREATE        0
-#define DEBUG_VFS_INODE_CREATE         0
-#define DEBUG_VFS_INODE_LOAD           0
-#define DEBUG_VFS_LOOKUP               0
-#define DEBUG_VFS_MAPPER_MOVE          0
-#define DEBUG_VFS_MAPPER_LOAD          0
-#define DEBUG_VFS_OPEN                 0
-
-#define DEBUG_VMM_CREATE_VSEG          0
-#define DEBUG_VMM_DESTROY              0
-#define DEBUG_VMM_FORK_COPY            0
-#define DEBUG_VMM_GET_ONE_PPN          0
-#define DEBUG_VMM_GET_PTE              0
-#define DEBUG_VMM_HANDLE_PAGE_FAULT    0
-#define DEBUG_VMM_HANDLE_COW           0
-#define DEBUG_VMM_INIT                 0
-#define DEBUG_VMM_PAGE_ALLOCATE        0
-#define DEBUG_VMM_SET_COW              0
-#define DEBUG_VMM_UNMAP_VSEG           0
-#define DEBUG_VMM_UPDATE_PTE           0
-
-#define DEBUG_XHTAB                    0
+#define DEBUG_CHDEV_CMD_RX                0
+#define DEBUG_CHDEV_CMD_TX                0
+#define DEBUG_CHDEV_SERVER_RX             0
+#define DEBUG_CHDEV_SERVER_TX             0
+
+#define DEBUG_CLUSTER_INIT                0
+#define DEBUG_CLUSTER_PID_ALLOC           0
+#define DEBUG_CLUSTER_PID_RELEASE         0
+#define DEBUG_CLUSTER_PROCESS_COPIES      0
+
+#define DEBUG_DEV_TXT_RX                  0
+#define DEBUG_DEV_TXT_TX                  0
+#define DEBUG_DEV_IOC_RX                  0
+#define DEBUG_DEV_IOC_TX                  0
+#define DEBUG_DEV_NIC_RX                  0
+#define DEBUG_DEV_NIC_RX                  0
+#define DEBUG_DEV_FBF_RX                  0
+#define DEBUG_DEV_FBF_TX                  0
+#define DEBUG_DEV_DMA                     0
+#define DEBUG_DEV_MMC                     0
+#define DEBUG_DEV_PIC                     0
+
+#define DEBUG_DEVFS_INIT                  0
+#define DEBUG_DEVFS_MOVE                  0
+
+#define DEBUG_ELF_LOAD                    0
+
+#define DEBUG_DQDT_INIT                   0
+#define DEBUG_DQDT_UPDATE_THREADS         0
+#define DEBUG_DQDT_SELECT_FOR_PROCESS     0
+#define DEBUG_DQDT_UPDATE_PAGES           0
+#define DEBUG_DQDT_SELECT_FOR_MEMORY      0
+
+#define DEBUG_FATFS_ADD_DENTRY            0
+#define DEBUG_FATFS_CHILD_INIT            0
+#define DEBUG_FATFS_CLUSTER_ALLOC         0
+#define DEBUG_FATFS_CTX_INIT              0
+#define DEBUG_FATFS_FREE_CLUSTERS         0
+#define DEBUG_FATFS_GET_CLUSTER           0
+#define DEBUG_FATFS_MOVE_PAGE             0
+#define DEBUG_FATFS_RELEASE_INODE         0
+#define DEBUG_FATFS_REMOVE_DENTRY         1
+#define DEBUG_FATFS_SYNC_FAT              0
+#define DEBUG_FATFS_SYNC_FSINFO           0
+#define DEBUG_FATFS_SYNC_INODE            0
+
+#define DEBUG_HAL_GPT_SET_PTE             0
+#define DEBUG_HAL_GPT_COPY                0
+#define DEBUG_HAL_GPT_CREATE              0
+#define DEBUG_HAL_GPT_DESTROY             0
+
+#define DEBUG_HAL_KENTRY                  0
+#define DEBUG_HAL_EXCEPTIONS              0
+#define DEBUG_HAL_IRQS                    0
+#define DEBUG_HAL_TXT_RX                  0
+#define DEBUG_HAL_TXT_TX                  0
+#define DEBUG_HAL_IOC_RX                  0
+#define DEBUG_HAL_IOC_TX                  0
+
+#define DEBUG_KCM                         0
+#define DEBUG_KMEM                        0
+
+#define DEBUG_KERNEL_INIT                 0
+
+#define DEBUG_MAPPER_GET_PAGE             1
+#define DEBUG_MAPPER_HANDLE_MISS          0
+#define DEBUG_MAPPER_MOVE_USER            0
+#define DEBUG_MAPPER_MOVE_KERNEL          0
+
+#define DEBUG_MUTEX                       0
+
+#define DEBUG_PPM_ALLOC_PAGES             0
+#define DEBUG_PPM_FREE_PAGES              0
+   
+#define DEBUG_PROCESS_COPY_INIT           0
+#define DEBUG_PROCESS_DESTROY             0
+#define DEBUG_PROCESS_GET_LOCAL_COPY      0
+#define DEBUG_PROCESS_INIT_CREATE         0
+#define DEBUG_PROCESS_MAKE_EXEC           0
+#define DEBUG_PROCESS_MAKE_FORK           0
+#define DEBUG_PROCESS_REFERENCE_INIT      0
+#define DEBUG_PROCESS_SIGACTION           0
+#define DEBUG_PROCESS_TXT                 0
+#define DEBUG_PROCESS_ZERO_CREATE         0
+
+#define DEBUG_QUEUELOCK                   0
+
+#define DEBUG_RPC_CLIENT_GENERIC          0
+#define DEBUG_RPC_SERVER_GENERIC          0
+
+#define DEBUG_RPC_KCM_ALLOC               0
+#define DEBUG_RPC_KCM_FREE                0
+#define DEBUG_RPC_MAPPER_rGT_PAGE         0
+#define DEBUG_RPC_MAPPER_MOVE_USER        0
+#define DEBUG_RPC_PMEM_GET_PAGES          0
+#define DEBUG_RPC_PMEM_RELEASE_PAGES      0
+#define DEBUG_RPC_PROCESS_MAKE_FORK       0
+#define DEBUG_RPC_PROCESS_SIGACTION       0
+#define DEBUG_RPC_THREAD_USER_CREATE      0
+#define DEBUG_RPC_THREAD_KERNEL_CREATE    0
+#define DEBUG_RPC_VFS_INODE_CREATE        0
+#define DEBUG_RPC_VFS_INODE_DESTROY       0
+#define DEBUG_RPC_VFS_DENTRY_CREATE       0
+#define DEBUG_RPC_VFS_DENTRY_DESTROY      0
+#define DEBUG_RPC_VFS_FILE_CREATE         0
+#define DEBUG_RPC_VFS_FILE_DESTROY        0
+#define DEBUG_RPC_VFS_DEVICE_GET_DENTRY   0
+#define DEBUG_RPC_VMM_GET_PTE             0
+#define DEBUG_RPC_VMM_GET_VSEG            0
+
+#define DEBUG_RWLOCK                      0
+
+#define DEBUG_SCHED_HANDLE_SIGNALS        2
+#define DEBUG_SCHED_YIELD                 2    // must be activated by the trace() syscall
+#define DEBUG_SCHED_RPC_ACTIVATE          0
+
+#define DEBUG_SEM                         0
+
+#define DEBUG_SYSCALLS_ERROR              2
+
+#define DEBUG_SYS_BARRIER                 0
+#define DEBUG_SYS_CLOSE                   0
+#define DEBUG_SYS_CONDVAR                 0
+#define DEBUG_SYS_DISPLAY                 0
+#define DEBUG_SYS_EXEC                    0
+#define DEBUG_SYS_EXIT                    0
+#define DEBUG_SYS_FG                      0
+#define DEBUG_SYS_FORK                    0
+#define DEBUG_SYS_GET_CONFIG              0
+#define DEBUG_SYS_GETPID                  0
+#define DEBUG_SYS_ISATTY                  0
+#define DEBUG_SYS_IS_FG                   0
+#define DEBUG_SYS_KILL                    0
+#define DEBUG_SYS_OPEN                    0
+#define DEBUG_SYS_MMAP                    0
+#define DEBUG_SYS_MUNMAP                  0
+#define DEBUG_SYS_MUTEX                   0
+#define DEBUG_SYS_READ                    0
+#define DEBUG_SYS_SEM                     0
+#define DEBUG_SYS_STAT                    0
+#define DEBUG_SYS_THREAD_CANCEL           0
+#define DEBUG_SYS_THREAD_CREATE           0
+#define DEBUG_SYS_THREAD_EXIT             0
+#define DEBUG_SYS_THREAD_JOIN             0
+#define DEBUG_SYS_THREAD_SLEEP            0
+#define DEBUG_SYS_THREAD_WAKEUP           0
+#define DEBUG_SYS_THREAD_YIELD            0
+#define DEBUG_SYS_TRACE                   0
+#define DEBUG_SYS_UNLINK                  0
+#define DEBUG_SYS_WAIT                    0
+#define DEBUG_SYS_WRITE                   0
+
+#define DEBUG_THREAD_BLOCK                0
+#define DEBUG_THREAD_DELETE               0
+#define DEBUG_THREAD_DESTROY              0
+#define DEBUG_THREAD_GET_XPTR             0
+#define DEBUG_THREAD_IDLE                 0
+#define DEBUG_THREAD_INIT                 0
+#define DEBUG_THREAD_KERNEL_CREATE        0
+#define DEBUG_THREAD_USER_CREATE          0
+#define DEBUG_THREAD_USER_FORK            0
+#define DEBUG_THREAD_USER_EXEC            0
+
+#define DEBUG_VFS_ADD_CHILD               0
+#define DEBUG_VFS_CLOSE                   0
+#define DEBUG_VFS_DENTRY_CREATE           0
+#define DEBUG_VFS_INODE_CREATE            0
+#define DEBUG_VFS_INODE_LOAD_ALL          0
+#define DEBUG_VFS_LOOKUP                  0
+#define DEBUG_VFS_LSEEK                   0
+#define DEBUG_VFS_NEW_CHILD_INIT          0
+#define DEBUG_VFS_OPEN                    0
+#define DEBUG_VFS_STAT                    0
+#define DEBUG_VFS_UNLINK                  1
+
+#define DEBUG_VMM_CREATE_VSEG             0
+#define DEBUG_VMM_DESTROY                 0
+#define DEBUG_VMM_FORK_COPY               0
+#define DEBUG_VMM_GET_ONE_PPN             0
+#define DEBUG_VMM_GET_PTE                 0
+#define DEBUG_VMM_HANDLE_PAGE_FAULT       0
+#define DEBUG_VMM_HANDLE_COW              0
+#define DEBUG_VMM_INIT                    0
+#define DEBUG_VMM_PAGE_ALLOCATE           0
+#define DEBUG_VMM_SET_COW                 0
+#define DEBUG_VMM_UNMAP_VSEG              0
+#define DEBUG_VMM_UPDATE_PTE              0
+
+#define DEBUG_XHTAB                       0
 
 
@@ -248,20 +260,21 @@
 
 #define LOCK_CLUSTER_PREFTBL  21   // local  (Q)  protect array of ref. processes in cluster
-#define LOCK_PPM_DIRTY        22   // local  (Q)  protect PPM dirty_pages list
-
+
+#define LOCK_PPM_DIRTY        22   // remote (Q)  protect PPM dirty_pages list
 #define LOCK_CLUSTER_LOCALS   23   // remote (Q)  protect xlist of local processes in cluster
 #define LOCK_CLUSTER_COPIES   24   // remote (Q)  protect xlist of process copies in cluster
 #define LOCK_PROCESS_CHILDREN 25   // remote (Q)  protect xlist of chidren process in process
-#define LOCK_PROCESS_USERSYNC 26   // remote (Q)  protect all lists of user synchros in  process
+#define LOCK_PROCESS_USERSYNC 26   // remote (Q)  protect all lists of user synchros in process
 #define LOCK_PROCESS_FDARRAY  27   // remote (Q)  protect array of open files in owner process 
-
-#define LOCK_MAPPER_STATE     28   // local  (RW) protect mapper state
+#define LOCK_FATFS_FREE       28   // remote (Q)  protect the FATFS context (free clusters)
+
 #define LOCK_PROCESS_THTBL    29   // local  (RW) protect local array of threads in a process 
 
-#define LOCK_PROCESS_CWD      30   // remote (RW) protect current working directory in process
-#define LOCK_VFS_INODE        31   // remote (RW) protect inode state and associated mapper
-#define LOCK_VFS_FILE         32   // remote (RW) protect file descriptor state
-#define LOCK_VMM_VSL          33   // remote (RW) protect VSL (local list of vsegs)
-#define LOCK_VMM_GPT          34   // remote (RW) protect GPT (local page table)
+#define LOCK_MAPPER_STATE     30   // remote (RW) protect mapper state
+#define LOCK_PROCESS_CWD      31   // remote (RW) protect current working directory in process
+#define LOCK_VFS_INODE        32   // remote (RW) protect inode state and associated mapper
+#define LOCK_VFS_FILE         33   // remote (RW) protect file descriptor state
+#define LOCK_VMM_VSL          34   // remote (RW) protect VSL (local list of vsegs)
+#define LOCK_VMM_GPT          35   // remote (RW) protect GPT (local page table)
 
 
@@ -305,6 +318,6 @@
 ////////////////////////////////////////////////////////////////////////////////////////////
 
-#define CONFIG_MAX_PROCESS_PER_CLUSTER      128        // max number of owned process
-#define CONFIG_PROCESS_FILE_MAX_NR          8          // max number of open files
+#define CONFIG_MAX_PROCESS_PER_CLUSTER      16         // max number of owned process
+#define CONFIG_PROCESS_FILE_MAX_NR          16         // max number of open files
 #define CONFIG_PROCESS_MAX_CHILDREN         1024       // max number of children process
 #define CONFIG_PROCESS_HEAP_MIN_SIZE        0x00010000 // user heap min size (bytes)
@@ -319,4 +332,6 @@
 #define CONFIG_VFS_MAX_NAME_LENGTH          32         // dentry name max length (bytes)
 #define CONFIG_VFS_MAX_PATH_LENGTH          256        // pathname max length (bytes)
+#define CONFIG_VFS_FREE_CLUSTERS_MIN        32         // min number of free clusters
+
 
 #define CONFIG_VFS_ROOT_IS_FATFS            1          // root FS is FATFS if non zero
@@ -366,7 +381,10 @@
 ////////////////////////////////////////////////////////////////////////////////////////////
 
-#define CONFIG_MAPPER_MAX_FRAGMENTS         10      // max number of fragments moved 
+#define CONFIG_MAPPER_MAX_FRAGMENTS         10     // max number of fragments moved 
 #define CONFIG_MAPPER_MIN                   CONFIG_VFS_INODE_MIN
 #define CONFIG_MAPPER_MAX                   CONFIG_VFS_INODE_MAX
+#define CONFIG_MAPPER_GRDXT_W1              7      // number of bits for RADIX_TREE_IX1
+#define CONFIG_MAPPER_GRDXT_W2              7      // number of bits for RADIX_TREE_IX2
+#define CONFIG_MAPPER_GRDXT_W3              6      // number of bits for RADIX_TREE_IX3
 
 ////////////////////////////////////////////////////////////////////////////////////////////
@@ -385,8 +403,4 @@
 #define CONFIG_VMM_ENVS_SIZE          0x000008     // envs vseg size         : 32  Kbytes
 #define CONFIG_VMM_STACK_SIZE         0x000100     // single stack vseg size : 1   Mbytes
-
-#define CONFIG_VMM_GRDXT_W1           7            // number of bits for RADIX_TREE_IX1
-#define CONFIG_VMM_GRDXT_W2           7            // number of bits for RADIX_TREE_IX2
-#define CONFIG_VMM_GRDXT_W3           6            // number of bits for RADIX_TREE_IX3
 
 ////////////////////////////////////////////////////////////////////////////////////////////
