| 1 | /* | 
|---|
| 2 | * vfs.c - Virtual File System implementation. | 
|---|
| 3 | * | 
|---|
| 4 | * Author  Mohamed Lamine Karaoui (2015) | 
|---|
| 5 | *         Alain Greiner (2016,2017,2018) | 
|---|
| 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_kernel_types.h> | 
|---|
| 28 | #include <hal_atomic.h> | 
|---|
| 29 | #include <hal_special.h> | 
|---|
| 30 | #include <printk.h> | 
|---|
| 31 | #include <list.h> | 
|---|
| 32 | #include <xlist.h> | 
|---|
| 33 | #include <slist.h> | 
|---|
| 34 | #include <xhtab.h> | 
|---|
| 35 | #include <string.h> | 
|---|
| 36 | #include <rpc.h> | 
|---|
| 37 | #include <errno.h> | 
|---|
| 38 | #include <kmem.h> | 
|---|
| 39 | #include <mapper.h> | 
|---|
| 40 | #include <thread.h> | 
|---|
| 41 | #include <chdev.h> | 
|---|
| 42 | #include <process.h> | 
|---|
| 43 | #include <cluster.h> | 
|---|
| 44 | #include <vfs.h> | 
|---|
| 45 | #include <fatfs.h> | 
|---|
| 46 | #include <ramfs.h> | 
|---|
| 47 | #include <devfs.h> | 
|---|
| 48 | #include <syscalls.h> | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 52 | //           Extern variables | 
|---|
| 53 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 54 |  | 
|---|
| 55 | extern vfs_ctx_t          fs_context[FS_TYPES_NR];    // allocated in kernel_init.c | 
|---|
| 56 | extern chdev_directory_t  chdev_dir;                  // allocated in kernel_init.c | 
|---|
| 57 | extern char *             lock_type_str[];            // allocated in kernel_init.c | 
|---|
| 58 |  | 
|---|
| 59 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 60 | //           Context related functions | 
|---|
| 61 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 62 |  | 
|---|
| 63 | //////////////////////////////////////// | 
|---|
| 64 | void vfs_ctx_init( vfs_fs_type_t   type, | 
|---|
| 65 | uint32_t        attr, | 
|---|
| 66 | uint32_t        total_clusters, | 
|---|
| 67 | uint32_t        cluster_size, | 
|---|
| 68 | xptr_t          vfs_root_xp, | 
|---|
| 69 | void          * extend ) | 
|---|
| 70 | { | 
|---|
| 71 | vfs_ctx_t * vfs_ctx = &fs_context[type]; | 
|---|
| 72 |  | 
|---|
| 73 | vfs_ctx->type           = type; | 
|---|
| 74 | vfs_ctx->attr           = attr; | 
|---|
| 75 | vfs_ctx->total_clusters = total_clusters; | 
|---|
| 76 | vfs_ctx->cluster_size   = cluster_size; | 
|---|
| 77 | vfs_ctx->vfs_root_xp    = vfs_root_xp; | 
|---|
| 78 | vfs_ctx->extend         = extend; | 
|---|
| 79 |  | 
|---|
| 80 | busylock_init( &vfs_ctx->lock , LOCK_VFS_CTX ); | 
|---|
| 81 |  | 
|---|
| 82 | bitmap_init( vfs_ctx->bitmap , BITMAP_SIZE(CONFIG_VFS_MAX_INODES) ); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | //////////////////////////////////////////// | 
|---|
| 86 | error_t vfs_ctx_inum_alloc( vfs_ctx_t * ctx, | 
|---|
| 87 | uint32_t  * inum ) | 
|---|
| 88 | { | 
|---|
| 89 | // get lock on inum allocator | 
|---|
| 90 | busylock_acquire( &ctx->lock ); | 
|---|
| 91 |  | 
|---|
| 92 | // get lid from local inum allocator | 
|---|
| 93 | uint32_t lid = bitmap_ffc( ctx->bitmap , CONFIG_VFS_MAX_INODES ); | 
|---|
| 94 |  | 
|---|
| 95 | if( lid == 0xFFFFFFFF )   // no more free slot => error | 
|---|
| 96 | { | 
|---|
| 97 | // release lock | 
|---|
| 98 | busylock_release( &ctx->lock ); | 
|---|
| 99 |  | 
|---|
| 100 | // return error | 
|---|
| 101 | return 1; | 
|---|
| 102 | } | 
|---|
| 103 | else              // found => return inum | 
|---|
| 104 | { | 
|---|
| 105 | // set slot allocated | 
|---|
| 106 | bitmap_set( ctx->bitmap , lid ); | 
|---|
| 107 |  | 
|---|
| 108 | // release lock | 
|---|
| 109 | busylock_release( &ctx->lock ); | 
|---|
| 110 |  | 
|---|
| 111 | // return inum | 
|---|
| 112 | *inum = (((uint32_t)local_cxy) << 16) | (lid & 0xFFFF); | 
|---|
| 113 | return 0; | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | //////////////////////////////////////////// | 
|---|
| 118 | void vfs_ctx_inum_release( vfs_ctx_t * ctx, | 
|---|
| 119 | uint32_t    inum ) | 
|---|
| 120 | { | 
|---|
| 121 | bitmap_clear( ctx->bitmap , inum & 0xFFFF ); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 125 | //           Inode related functions | 
|---|
| 126 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 127 |  | 
|---|
| 128 | static const char * vfs_inode_type_str( vfs_inode_type_t type ) | 
|---|
| 129 | { | 
|---|
| 130 | switch ( type ) { | 
|---|
| 131 | case INODE_TYPE_FILE: return "FILE"; | 
|---|
| 132 | case INODE_TYPE_DIR:  return "DIR "; | 
|---|
| 133 | case INODE_TYPE_FIFO: return "FIFO"; | 
|---|
| 134 | case INODE_TYPE_PIPE: return "PIPE"; | 
|---|
| 135 | case INODE_TYPE_SOCK: return "SOCK"; | 
|---|
| 136 | case INODE_TYPE_DEV:  return "DEV "; | 
|---|
| 137 | case INODE_TYPE_SYML: return "SYML"; | 
|---|
| 138 | default:              return "undefined"; | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | ////////////////////////////////////////////////////// | 
|---|
| 143 | error_t vfs_inode_create( xptr_t            dentry_xp, | 
|---|
| 144 | vfs_fs_type_t     fs_type, | 
|---|
| 145 | vfs_inode_type_t  inode_type, | 
|---|
| 146 | void            * extend, | 
|---|
| 147 | uint32_t          attr, | 
|---|
| 148 | uint32_t          rights, | 
|---|
| 149 | uid_t             uid, | 
|---|
| 150 | gid_t             gid, | 
|---|
| 151 | xptr_t          * inode_xp ) | 
|---|
| 152 | { | 
|---|
| 153 | mapper_t         * mapper;     // associated mapper( to be allocated) | 
|---|
| 154 | vfs_inode_t      * inode;      // inode descriptor (to be allocated) | 
|---|
| 155 | uint32_t           inum;       // inode identifier (to be allocated) | 
|---|
| 156 | vfs_ctx_t        * ctx;        // file system context | 
|---|
| 157 | kmem_req_t         req;        // request to kernel memory allocator | 
|---|
| 158 | error_t            error; | 
|---|
| 159 |  | 
|---|
| 160 | #if DEBUG_VFS_INODE_CREATE | 
|---|
| 161 | char           name[CONFIG_VFS_MAX_NAME_LENGTH]; | 
|---|
| 162 | uint32_t       cycle      = (uint32_t)hal_get_cycles(); | 
|---|
| 163 | cxy_t          dentry_cxy = GET_CXY( dentry_xp ); | 
|---|
| 164 | vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp ); | 
|---|
| 165 | thread_t *     this       = CURRENT_THREAD; | 
|---|
| 166 | if( dentry_xp != XPTR_NULL ) hal_remote_strcpy( XPTR( local_cxy  , name ), | 
|---|
| 167 | XPTR( dentry_cxy , dentry_ptr->name ) ); | 
|---|
| 168 | else                         strcpy( name , "/" ); | 
|---|
| 169 | if( DEBUG_VFS_INODE_CREATE < cycle ) | 
|---|
| 170 | printk("\n[DBG] %s : thread %x in process %x enter for <%s> / cycle %d\n", | 
|---|
| 171 | __FUNCTION__, this->trdid, this->process->pid, name, cycle ); | 
|---|
| 172 | #endif | 
|---|
| 173 |  | 
|---|
| 174 | // check fs type and get pointer on context | 
|---|
| 175 | if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS]; | 
|---|
| 176 | else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS]; | 
|---|
| 177 | else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS]; | 
|---|
| 178 | else | 
|---|
| 179 | { | 
|---|
| 180 | ctx = NULL; | 
|---|
| 181 | assert( false , "illegal file system type = %d\n" , fs_type ); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | // allocate inum | 
|---|
| 185 | error = vfs_ctx_inum_alloc( ctx , &inum ); | 
|---|
| 186 |  | 
|---|
| 187 | if( error ) | 
|---|
| 188 | { | 
|---|
| 189 | printk("\n[ERROR] in %s : cannot allocate inum\n", __FUNCTION__ ); | 
|---|
| 190 | return ENOMEM; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | // allocate memory for mapper | 
|---|
| 194 | mapper = mapper_create( fs_type ); | 
|---|
| 195 |  | 
|---|
| 196 | if( mapper == NULL ) | 
|---|
| 197 | { | 
|---|
| 198 | printk("\n[ERROR] in %s : cannot allocate mapper\n", __FUNCTION__ ); | 
|---|
| 199 | vfs_ctx_inum_release( ctx , inum ); | 
|---|
| 200 | return ENOMEM; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | // allocate memory for VFS inode descriptor | 
|---|
| 204 | req.type  = KMEM_VFS_INODE; | 
|---|
| 205 | req.size  = sizeof(vfs_inode_t); | 
|---|
| 206 | req.flags = AF_KERNEL | AF_ZERO; | 
|---|
| 207 | inode     = (vfs_inode_t *)kmem_alloc( &req ); | 
|---|
| 208 |  | 
|---|
| 209 | if( inode == NULL ) | 
|---|
| 210 | { | 
|---|
| 211 | printk("\n[ERROR] in %s : cannot allocate inode descriptor\n", __FUNCTION__ ); | 
|---|
| 212 | vfs_ctx_inum_release( ctx , inum ); | 
|---|
| 213 | mapper_destroy( mapper ); | 
|---|
| 214 | return ENOMEM; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | // initialize inode descriptor | 
|---|
| 218 | inode->gc         = 0; | 
|---|
| 219 | inode->type       = inode_type; | 
|---|
| 220 | inode->inum       = inum; | 
|---|
| 221 | inode->attr       = attr; | 
|---|
| 222 | inode->rights     = rights; | 
|---|
| 223 | inode->uid        = uid; | 
|---|
| 224 | inode->gid        = gid; | 
|---|
| 225 | inode->refcount   = 0; | 
|---|
| 226 | inode->parent_xp  = dentry_xp; | 
|---|
| 227 | inode->ctx        = ctx; | 
|---|
| 228 | inode->mapper     = mapper; | 
|---|
| 229 | inode->extend     = extend; | 
|---|
| 230 |  | 
|---|
| 231 | // initialise inode field in mapper | 
|---|
| 232 | mapper->inode     = inode; | 
|---|
| 233 |  | 
|---|
| 234 | // initialise threads waiting queue | 
|---|
| 235 | xlist_root_init( XPTR( local_cxy , &inode->wait_root ) ); | 
|---|
| 236 |  | 
|---|
| 237 | // initialize dentries hash table | 
|---|
| 238 | xhtab_init( &inode->children , XHTAB_DENTRY_TYPE ); | 
|---|
| 239 |  | 
|---|
| 240 | // initialize inode lock | 
|---|
| 241 | remote_rwlock_init( XPTR( local_cxy , &inode->data_lock ), LOCK_VFS_INODE ); | 
|---|
| 242 |  | 
|---|
| 243 | // initialise lock protecting inode three traversal | 
|---|
| 244 | remote_busylock_init( XPTR( local_cxy , &inode->main_lock ), LOCK_VFS_MAIN ); | 
|---|
| 245 |  | 
|---|
| 246 | #if DEBUG_VFS_INODE_CREATE | 
|---|
| 247 | cycle      = (uint32_t)hal_get_cycles(); | 
|---|
| 248 | if( DEBUG_VFS_INODE_CREATE < cycle ) | 
|---|
| 249 | printk("\n[DBG] %s : thread %x in process %x exit for <%s> / inode [%x,%x] / cycle %d\n", | 
|---|
| 250 | __FUNCTION__, this->trdid, this->process->pid, name, local_cxy, inode, cycle ); | 
|---|
| 251 | #endif | 
|---|
| 252 |  | 
|---|
| 253 | // return extended pointer on inode | 
|---|
| 254 | *inode_xp = XPTR( local_cxy , inode ); | 
|---|
| 255 | return 0; | 
|---|
| 256 |  | 
|---|
| 257 | }  // end vfs_inode_create() | 
|---|
| 258 |  | 
|---|
| 259 | //////////////////////////////////////////////// | 
|---|
| 260 | error_t vfs_inode_destroy( vfs_inode_t * inode ) | 
|---|
| 261 | { | 
|---|
| 262 | assert( (inode->refcount == 0) , "inode refcount non zero\n" ); | 
|---|
| 263 |  | 
|---|
| 264 | // release memory allocated for mapper | 
|---|
| 265 | mapper_destroy( inode->mapper ); | 
|---|
| 266 |  | 
|---|
| 267 | // release memory allocate for inode descriptor | 
|---|
| 268 | kmem_req_t req; | 
|---|
| 269 | req.ptr   = inode; | 
|---|
| 270 | req.type  = KMEM_VFS_INODE; | 
|---|
| 271 | kmem_free( &req ); | 
|---|
| 272 |  | 
|---|
| 273 | return 0; | 
|---|
| 274 |  | 
|---|
| 275 | }  // end vfs_inode_destroy() | 
|---|
| 276 |  | 
|---|
| 277 | ///////////////////////////////////////////// | 
|---|
| 278 | error_t vfs_inode_load( vfs_inode_t * parent, | 
|---|
| 279 | char        * name, | 
|---|
| 280 | xptr_t        child_xp ) | 
|---|
| 281 | { | 
|---|
| 282 |  | 
|---|
| 283 | #if DEBUG_VFS_INODE_LOAD | 
|---|
| 284 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 285 | if( DEBUG_VFS_INODE_LOAD < cycle ) | 
|---|
| 286 | printk("\n[DBG] %s : thread %x enter for <%s> / cycle %d\n", | 
|---|
| 287 | __FUNCTION__, CURRENT_THREAD , name , cycle ); | 
|---|
| 288 | #endif | 
|---|
| 289 |  | 
|---|
| 290 | error_t error = 0; | 
|---|
| 291 |  | 
|---|
| 292 | assert( (parent != NULL) , "parent pointer is NULL\n"); | 
|---|
| 293 |  | 
|---|
| 294 | assert( (child_xp != XPTR_NULL) , "child pointer is NULL\n"); | 
|---|
| 295 |  | 
|---|
| 296 | // get parent inode FS type | 
|---|
| 297 | vfs_fs_type_t fs_type = parent->ctx->type; | 
|---|
| 298 |  | 
|---|
| 299 | // call relevant FS function | 
|---|
| 300 | if( fs_type == FS_TYPE_FATFS ) | 
|---|
| 301 | { | 
|---|
| 302 | error = fatfs_inode_load( parent , name , child_xp ); | 
|---|
| 303 | } | 
|---|
| 304 | else if( fs_type == FS_TYPE_RAMFS ) | 
|---|
| 305 | { | 
|---|
| 306 | assert( false , "should not be called for RAMFS\n" ); | 
|---|
| 307 | } | 
|---|
| 308 | else if( fs_type == FS_TYPE_DEVFS ) | 
|---|
| 309 | { | 
|---|
| 310 | assert( false , "should not be called for DEVFS\n" ); | 
|---|
| 311 | } | 
|---|
| 312 | else | 
|---|
| 313 | { | 
|---|
| 314 | assert( false , "undefined file system type\n" ); | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | #if DEBUG_VFS_INODE_LOAD | 
|---|
| 318 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 319 | if( DEBUG_VFS_INODE_LOAD < cycle ) | 
|---|
| 320 | printk("\n[DBG] %s : thread %x exit for <%s> / cycle %d\n", | 
|---|
| 321 | __FUNCTION__, CURRENT_THREAD , name , cycle ); | 
|---|
| 322 | #endif | 
|---|
| 323 |  | 
|---|
| 324 | return error; | 
|---|
| 325 |  | 
|---|
| 326 | } // end vfs_inode_load() | 
|---|
| 327 |  | 
|---|
| 328 | //////////////////////////////////////////// | 
|---|
| 329 | void vfs_inode_remote_up( xptr_t  inode_xp ) | 
|---|
| 330 | { | 
|---|
| 331 | // get inode cluster and local pointer | 
|---|
| 332 | cxy_t         inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 333 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 334 |  | 
|---|
| 335 | hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->refcount ) , 1 ); | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | ////////////////////////////////////////////// | 
|---|
| 339 | void vfs_inode_remote_down( xptr_t  inode_xp ) | 
|---|
| 340 | { | 
|---|
| 341 | // get inode cluster and local pointer | 
|---|
| 342 | cxy_t         inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 343 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 344 |  | 
|---|
| 345 | hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->refcount ) , -1 ); | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 | ////////////////////////////////////////////// | 
|---|
| 349 | uint32_t vfs_inode_get_size( xptr_t inode_xp ) | 
|---|
| 350 | { | 
|---|
| 351 | // get inode cluster and local pointer | 
|---|
| 352 | cxy_t         cxy = GET_CXY( inode_xp ); | 
|---|
| 353 | vfs_inode_t * ptr = GET_PTR( inode_xp ); | 
|---|
| 354 |  | 
|---|
| 355 | // get size | 
|---|
| 356 | remote_rwlock_rd_acquire( XPTR( cxy , &ptr->data_lock ) ); | 
|---|
| 357 | uint32_t size = hal_remote_l32( XPTR( cxy , &ptr->size ) ); | 
|---|
| 358 | remote_rwlock_rd_release( XPTR( cxy , &ptr->data_lock ) ); | 
|---|
| 359 | return size; | 
|---|
| 360 | } | 
|---|
| 361 |  | 
|---|
| 362 | //////////////////////////////////////////// | 
|---|
| 363 | void vfs_inode_set_size( xptr_t    inode_xp, | 
|---|
| 364 | uint32_t  size ) | 
|---|
| 365 | { | 
|---|
| 366 | // get inode cluster and local pointer | 
|---|
| 367 | cxy_t         cxy = GET_CXY( inode_xp ); | 
|---|
| 368 | vfs_inode_t * ptr = GET_PTR( inode_xp ); | 
|---|
| 369 |  | 
|---|
| 370 | // set size | 
|---|
| 371 | remote_rwlock_wr_release( XPTR( cxy , &ptr->data_lock ) ); | 
|---|
| 372 | hal_remote_s32( XPTR( cxy , &ptr->size ) , size ); | 
|---|
| 373 | remote_rwlock_wr_release( XPTR( cxy , &ptr->data_lock ) ); | 
|---|
| 374 | } | 
|---|
| 375 |  | 
|---|
| 376 | //////////////////////////////////////// | 
|---|
| 377 | void vfs_inode_unlock( xptr_t inode_xp ) | 
|---|
| 378 | { | 
|---|
| 379 | // get inode cluster and local pointer | 
|---|
| 380 | cxy_t         cxy = GET_CXY( inode_xp ); | 
|---|
| 381 | vfs_inode_t * ptr = GET_PTR( inode_xp ); | 
|---|
| 382 |  | 
|---|
| 383 | // release the main lock | 
|---|
| 384 | remote_busylock_release( XPTR( cxy , &ptr->main_lock ) ); | 
|---|
| 385 | } | 
|---|
| 386 |  | 
|---|
| 387 | ////////////////////////////////////// | 
|---|
| 388 | void vfs_inode_lock( xptr_t inode_xp ) | 
|---|
| 389 | { | 
|---|
| 390 | // get inode cluster and local pointer | 
|---|
| 391 | cxy_t         cxy = GET_CXY( inode_xp ); | 
|---|
| 392 | vfs_inode_t * ptr = GET_PTR( inode_xp ); | 
|---|
| 393 |  | 
|---|
| 394 | // get the main lock | 
|---|
| 395 | remote_busylock_acquire( XPTR( cxy , &ptr->main_lock ) ); | 
|---|
| 396 | } | 
|---|
| 397 |  | 
|---|
| 398 | ///////////////////////////////////////// | 
|---|
| 399 | void vfs_inode_get_name( xptr_t inode_xp, | 
|---|
| 400 | char * name ) | 
|---|
| 401 | { | 
|---|
| 402 | cxy_t          inode_cxy; | 
|---|
| 403 | vfs_inode_t  * inode_ptr; | 
|---|
| 404 | xptr_t         dentry_xp; | 
|---|
| 405 | cxy_t          dentry_cxy; | 
|---|
| 406 | vfs_dentry_t * dentry_ptr; | 
|---|
| 407 |  | 
|---|
| 408 | // get inode cluster and local pointer | 
|---|
| 409 | inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 410 | inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 411 |  | 
|---|
| 412 | // get parent dentry | 
|---|
| 413 | dentry_xp  = hal_remote_l64( XPTR( inode_cxy , &inode_ptr->parent_xp ) ); | 
|---|
| 414 |  | 
|---|
| 415 | // get local copy of name | 
|---|
| 416 | if( dentry_xp == XPTR_NULL )  // it is the VFS root | 
|---|
| 417 | { | 
|---|
| 418 | strcpy( name , "/" ); | 
|---|
| 419 | } | 
|---|
| 420 | else                          // not the VFS root | 
|---|
| 421 | { | 
|---|
| 422 | dentry_cxy = GET_CXY( dentry_xp ); | 
|---|
| 423 | dentry_ptr = GET_PTR( dentry_xp ); | 
|---|
| 424 |  | 
|---|
| 425 | hal_remote_strcpy( XPTR( local_cxy  , name ) , | 
|---|
| 426 | XPTR( dentry_cxy , &dentry_ptr->name ) ); | 
|---|
| 427 | } | 
|---|
| 428 | }  // end vfs_inode_get_name() | 
|---|
| 429 |  | 
|---|
| 430 | //////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 431 | //           Dentry related functions | 
|---|
| 432 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 433 |  | 
|---|
| 434 | /////////////////////////////////////////////////// | 
|---|
| 435 | error_t vfs_dentry_create( vfs_fs_type_t   fs_type, | 
|---|
| 436 | char          * name, | 
|---|
| 437 | vfs_inode_t   * parent, | 
|---|
| 438 | xptr_t        * dentry_xp ) | 
|---|
| 439 | { | 
|---|
| 440 | vfs_ctx_t      * ctx;        // context descriptor | 
|---|
| 441 | vfs_dentry_t   * dentry;     // dentry descriptor (to be allocated) | 
|---|
| 442 | kmem_req_t       req;        // request to kernel memory allocator | 
|---|
| 443 | error_t          error; | 
|---|
| 444 |  | 
|---|
| 445 | #if DEBUG_VFS_DENTRY_CREATE | 
|---|
| 446 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 447 | if( DEBUG_VFS_DENTRY_CREATE < cycle ) | 
|---|
| 448 | printk("\n[DBG] %s : thread %x in process %x enter for <%s> / parent_inode %x / cycle %d\n", | 
|---|
| 449 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, name, parent, cycle ); | 
|---|
| 450 | #endif | 
|---|
| 451 |  | 
|---|
| 452 | // get pointer on context | 
|---|
| 453 | if     ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS]; | 
|---|
| 454 | else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS]; | 
|---|
| 455 | else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS]; | 
|---|
| 456 | else | 
|---|
| 457 | { | 
|---|
| 458 | ctx = NULL; | 
|---|
| 459 | return EINVAL; | 
|---|
| 460 | } | 
|---|
| 461 |  | 
|---|
| 462 | // get name length | 
|---|
| 463 | uint32_t length = strlen( name ); | 
|---|
| 464 |  | 
|---|
| 465 | if( length >= CONFIG_VFS_MAX_NAME_LENGTH ) return EINVAL; | 
|---|
| 466 |  | 
|---|
| 467 | // allocate memory for dentry descriptor | 
|---|
| 468 | req.type  = KMEM_VFS_DENTRY; | 
|---|
| 469 | req.size  = sizeof(vfs_dentry_t); | 
|---|
| 470 | req.flags = AF_KERNEL | AF_ZERO; | 
|---|
| 471 | dentry     = (vfs_dentry_t *)kmem_alloc( &req ); | 
|---|
| 472 |  | 
|---|
| 473 | if( dentry == NULL ) return ENOMEM; | 
|---|
| 474 |  | 
|---|
| 475 | // initialize dentry descriptor | 
|---|
| 476 |  | 
|---|
| 477 | dentry->ctx     = ctx; | 
|---|
| 478 | dentry->length  = length; | 
|---|
| 479 | dentry->parent  = parent; | 
|---|
| 480 | strcpy( dentry->name , name ); | 
|---|
| 481 |  | 
|---|
| 482 | #if( DEBUG_VFS_DENTRY_CREATE & 1 ) | 
|---|
| 483 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 484 | if( DEBUG_VFS_DENTRY_CREATE < cycle ) | 
|---|
| 485 | printk("\n[DBG] %s : thread %x in process %x / dentry <%s> initialised / cycle %d\n", | 
|---|
| 486 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, dentry->name, cycle ); | 
|---|
| 487 | #endif | 
|---|
| 488 |  | 
|---|
| 489 | // register dentry in hash table rooted in parent inode | 
|---|
| 490 | error = xhtab_insert( XPTR( local_cxy , &parent->children ), | 
|---|
| 491 | name, | 
|---|
| 492 | XPTR( local_cxy , &dentry->list ) ); | 
|---|
| 493 |  | 
|---|
| 494 | if( error ) return EINVAL; | 
|---|
| 495 |  | 
|---|
| 496 | #if( DEBUG_VFS_DENTRY_CREATE & 1 ) | 
|---|
| 497 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 498 | if( DEBUG_VFS_DENTRY_CREATE < cycle ) | 
|---|
| 499 | printk("\n[DBG] %s : thread %x in process %x / dentry <%s> registered / cycle %d\n", | 
|---|
| 500 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, dentry->name, cycle ); | 
|---|
| 501 | #endif | 
|---|
| 502 |  | 
|---|
| 503 | // return extended pointer on dentry | 
|---|
| 504 | *dentry_xp = XPTR( local_cxy , dentry ); | 
|---|
| 505 |  | 
|---|
| 506 | #if DEBUG_VFS_DENTRY_CREATE | 
|---|
| 507 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 508 | if( DEBUG_VFS_DENTRY_CREATE < cycle ) | 
|---|
| 509 | printk("\n[DBG] %s : thread %x in process %x exit for <%s> / dentry %x / cycle %d\n", | 
|---|
| 510 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, name, dentry, cycle ); | 
|---|
| 511 | #endif | 
|---|
| 512 |  | 
|---|
| 513 | return 0; | 
|---|
| 514 |  | 
|---|
| 515 | }  // end vfs_dentry_create() | 
|---|
| 516 |  | 
|---|
| 517 | /////////////////////////////////////////////////// | 
|---|
| 518 | error_t vfs_dentry_destroy( vfs_dentry_t * dentry ) | 
|---|
| 519 | { | 
|---|
| 520 | error_t error; | 
|---|
| 521 |  | 
|---|
| 522 | assert( (dentry->refcount == 0) , __FUNCTION__ , "dentry refcount non zero\n" ); | 
|---|
| 523 |  | 
|---|
| 524 | // get pointer on parent inode | 
|---|
| 525 | vfs_inode_t * parent = dentry->parent; | 
|---|
| 526 |  | 
|---|
| 527 | // remove this dentry from parent inode htab | 
|---|
| 528 | error = xhtab_remove( XPTR( local_cxy , &parent->children ), | 
|---|
| 529 | dentry->name, | 
|---|
| 530 | XPTR( local_cxy , &dentry->list ) ); | 
|---|
| 531 |  | 
|---|
| 532 | if( error ) return EINVAL; | 
|---|
| 533 |  | 
|---|
| 534 | // release memory allocated to dentry | 
|---|
| 535 | kmem_req_t req; | 
|---|
| 536 | req.ptr   = dentry; | 
|---|
| 537 | req.type  = KMEM_VFS_DENTRY; | 
|---|
| 538 | kmem_free( &req ); | 
|---|
| 539 |  | 
|---|
| 540 | return 0; | 
|---|
| 541 | } | 
|---|
| 542 |  | 
|---|
| 543 |  | 
|---|
| 544 |  | 
|---|
| 545 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 546 | //           File descriptor related functions | 
|---|
| 547 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 548 |  | 
|---|
| 549 | ///////////////////////////////////////////// | 
|---|
| 550 | error_t vfs_file_create( vfs_inode_t * inode, | 
|---|
| 551 | uint32_t      attr, | 
|---|
| 552 | xptr_t      * file_xp ) | 
|---|
| 553 | { | 
|---|
| 554 | vfs_file_t  * file; | 
|---|
| 555 | kmem_req_t    req; | 
|---|
| 556 |  | 
|---|
| 557 | #if DEBUG_VFS_FILE_CREATE | 
|---|
| 558 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 559 | if( DEBUG_VFS_OPEN < cycle ) | 
|---|
| 560 | printk("\n[DBG] %s : thread %x in process %x enter for inode %x in cluster %x / cycle %d\n", | 
|---|
| 561 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, inode, local_cxy, cycle ); | 
|---|
| 562 | #endif | 
|---|
| 563 |  | 
|---|
| 564 | // allocate memory for new file descriptor | 
|---|
| 565 | req.type  = KMEM_VFS_FILE; | 
|---|
| 566 | req.size  = sizeof(vfs_file_t); | 
|---|
| 567 | req.flags = AF_KERNEL | AF_ZERO; | 
|---|
| 568 | file      = (vfs_file_t *)kmem_alloc( &req ); | 
|---|
| 569 |  | 
|---|
| 570 | if( file == NULL ) return ENOMEM; | 
|---|
| 571 |  | 
|---|
| 572 | // initializes new file descriptor | 
|---|
| 573 | file->gc       = 0; | 
|---|
| 574 | file->type     = inode->type; | 
|---|
| 575 | file->attr     = attr; | 
|---|
| 576 | file->offset   = 0; | 
|---|
| 577 | file->refcount = 1; | 
|---|
| 578 | file->inode    = inode; | 
|---|
| 579 | file->ctx      = inode->ctx; | 
|---|
| 580 | file->mapper   = inode->mapper; | 
|---|
| 581 |  | 
|---|
| 582 | remote_rwlock_init( XPTR( local_cxy , &file->lock ), LOCK_VFS_FILE ); | 
|---|
| 583 |  | 
|---|
| 584 | *file_xp = XPTR( local_cxy , file ); | 
|---|
| 585 |  | 
|---|
| 586 | #if DEBUG_VFS_FILE_CREATE | 
|---|
| 587 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 588 | if( DEBUG_VFS_OPEN < cycle ) | 
|---|
| 589 | printk("\n[DBG] %s : thread %x in process %x created file %x in cluster %x / cycle %d\n", | 
|---|
| 590 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, file, local_cxy, cycle ); | 
|---|
| 591 | #endif | 
|---|
| 592 |  | 
|---|
| 593 | return 0; | 
|---|
| 594 |  | 
|---|
| 595 | }  // end vfs_file_create() | 
|---|
| 596 |  | 
|---|
| 597 | /////////////////////////////////////////// | 
|---|
| 598 | void vfs_file_destroy( vfs_file_t *  file ) | 
|---|
| 599 | { | 
|---|
| 600 | if( file->refcount ) | 
|---|
| 601 | { | 
|---|
| 602 | assert( false , "refcount non zero\n" ); | 
|---|
| 603 | } | 
|---|
| 604 |  | 
|---|
| 605 | kmem_req_t req; | 
|---|
| 606 | req.ptr   = file; | 
|---|
| 607 | req.type  = KMEM_VFS_FILE; | 
|---|
| 608 | kmem_free( &req ); | 
|---|
| 609 |  | 
|---|
| 610 | #if DEBUG_VFS_CLOSE | 
|---|
| 611 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 612 | if( DEBUG_VFS_CLOSE < cycle ) | 
|---|
| 613 | printk("\n[DBG] %s : thread %x in process %x deleted file %x in cluster %x / cycle %d\n", | 
|---|
| 614 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, file, local_cxy, cycle ); | 
|---|
| 615 | #endif | 
|---|
| 616 |  | 
|---|
| 617 | }  // end vfs_file_destroy() | 
|---|
| 618 |  | 
|---|
| 619 |  | 
|---|
| 620 | //////////////////////////////////////// | 
|---|
| 621 | void vfs_file_count_up( xptr_t file_xp ) | 
|---|
| 622 | { | 
|---|
| 623 | // get file cluster and local pointer | 
|---|
| 624 | cxy_t        file_cxy = GET_CXY( file_xp ); | 
|---|
| 625 | vfs_file_t * file_ptr = GET_PTR( file_xp ); | 
|---|
| 626 |  | 
|---|
| 627 | // atomically increment count | 
|---|
| 628 | hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , 1 ); | 
|---|
| 629 | } | 
|---|
| 630 |  | 
|---|
| 631 | ////////////////////////////////////////// | 
|---|
| 632 | void vfs_file_count_down( xptr_t file_xp ) | 
|---|
| 633 | { | 
|---|
| 634 | // get file cluster and local pointer | 
|---|
| 635 | cxy_t        file_cxy = GET_CXY( file_xp ); | 
|---|
| 636 | vfs_file_t * file_ptr = GET_PTR( file_xp ); | 
|---|
| 637 |  | 
|---|
| 638 | // atomically decrement count | 
|---|
| 639 | hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , -1 ); | 
|---|
| 640 | } | 
|---|
| 641 |  | 
|---|
| 642 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 643 | //           File access related functions | 
|---|
| 644 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 645 |  | 
|---|
| 646 | ////////////////////////////////////// | 
|---|
| 647 | error_t vfs_open( process_t * process, | 
|---|
| 648 | char      * path, | 
|---|
| 649 | uint32_t    flags, | 
|---|
| 650 | uint32_t    mode, | 
|---|
| 651 | xptr_t    * new_file_xp, | 
|---|
| 652 | uint32_t  * new_file_id ) | 
|---|
| 653 | { | 
|---|
| 654 | error_t       error; | 
|---|
| 655 | xptr_t        inode_xp;     // extended pointer on target inode | 
|---|
| 656 | cxy_t         inode_cxy;    // inode cluster identifier | 
|---|
| 657 | vfs_inode_t * inode_ptr;    // inode local pointer | 
|---|
| 658 | uint32_t      file_attr;    // file descriptor attributes | 
|---|
| 659 | uint32_t      lookup_mode;  // lookup working mode | 
|---|
| 660 | xptr_t        file_xp;      // extended pointer on created file descriptor | 
|---|
| 661 | uint32_t      file_id;      // created file descriptor index in reference fd_array | 
|---|
| 662 |  | 
|---|
| 663 | assert( (mode == 0), __FUNCTION__, | 
|---|
| 664 | "the mode parameter is not supported yet\n" ); | 
|---|
| 665 |  | 
|---|
| 666 | #if DEBUG_VFS_OPEN | 
|---|
| 667 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 668 | if( DEBUG_VFS_OPEN < cycle ) | 
|---|
| 669 | printk("\n[DBG] %s :  thread %x in process %x enter for <%s> / cycle %d\n", | 
|---|
| 670 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, path, cycle ); | 
|---|
| 671 | #endif | 
|---|
| 672 |  | 
|---|
| 673 | // compute lookup working mode | 
|---|
| 674 | lookup_mode = VFS_LOOKUP_OPEN; | 
|---|
| 675 | if( (flags & O_DIR    )      )  lookup_mode |= VFS_LOOKUP_DIR; | 
|---|
| 676 | if( (flags & O_CREAT  )      )  lookup_mode |= VFS_LOOKUP_CREATE; | 
|---|
| 677 | if( (flags & O_EXCL   )      )  lookup_mode |= VFS_LOOKUP_EXCL; | 
|---|
| 678 |  | 
|---|
| 679 | // compute attributes for the created file | 
|---|
| 680 | file_attr = 0; | 
|---|
| 681 | if( (flags & O_RDONLY ) == 0 )  file_attr |= FD_ATTR_WRITE_ENABLE; | 
|---|
| 682 | if( (flags & O_WRONLY ) == 0 )  file_attr |= FD_ATTR_READ_ENABLE; | 
|---|
| 683 | if( (flags & O_SYNC   )      )  file_attr |= FD_ATTR_SYNC; | 
|---|
| 684 | if( (flags & O_APPEND )      )  file_attr |= FD_ATTR_APPEND; | 
|---|
| 685 | if( (flags & O_CLOEXEC)      )  file_attr |= FD_ATTR_CLOSE_EXEC; | 
|---|
| 686 |  | 
|---|
| 687 | // get extended pointer on target inode | 
|---|
| 688 | error = vfs_lookup( process->vfs_cwd_xp , path , lookup_mode , &inode_xp ); | 
|---|
| 689 |  | 
|---|
| 690 | if( error ) return error; | 
|---|
| 691 |  | 
|---|
| 692 | // get target inode cluster and local pointer | 
|---|
| 693 | inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 694 | inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 695 |  | 
|---|
| 696 | // create a new file descriptor in cluster containing inode | 
|---|
| 697 | if( inode_cxy == local_cxy )      // target cluster is local | 
|---|
| 698 | { | 
|---|
| 699 | error = vfs_file_create( inode_ptr , file_attr , &file_xp ); | 
|---|
| 700 | } | 
|---|
| 701 | else                              // target cluster is remote | 
|---|
| 702 | { | 
|---|
| 703 | rpc_vfs_file_create_client( inode_cxy , inode_ptr , file_attr , &file_xp , &error ); | 
|---|
| 704 | } | 
|---|
| 705 |  | 
|---|
| 706 | if( error )  return error; | 
|---|
| 707 |  | 
|---|
| 708 | // allocate and register a new file descriptor index in reference process | 
|---|
| 709 | error = process_fd_register( process , file_xp , &file_id ); | 
|---|
| 710 |  | 
|---|
| 711 | if( error ) return error; | 
|---|
| 712 |  | 
|---|
| 713 | #if DEBUG_VFS_OPEN | 
|---|
| 714 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 715 | if( DEBUG_VFS_OPEN < cycle ) | 
|---|
| 716 | printk("\n[DBG] %s :  thread %x in process %x exit for <%s> / fdid %d / cluster %x / cycle %d\n", | 
|---|
| 717 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, path, | 
|---|
| 718 | file_id, GET_CXY( file_xp ), cycle ); | 
|---|
| 719 | #endif | 
|---|
| 720 |  | 
|---|
| 721 | // success | 
|---|
| 722 | *new_file_xp = file_xp; | 
|---|
| 723 | *new_file_id = file_id; | 
|---|
| 724 | return 0; | 
|---|
| 725 |  | 
|---|
| 726 | }  // end vfs_open() | 
|---|
| 727 |  | 
|---|
| 728 | ////////////////////////////////////// | 
|---|
| 729 | int vfs_user_move( bool_t   to_buffer, | 
|---|
| 730 | xptr_t   file_xp, | 
|---|
| 731 | void   * buffer, | 
|---|
| 732 | uint32_t size ) | 
|---|
| 733 | { | 
|---|
| 734 | assert( ( file_xp != XPTR_NULL ) , "file_xp == XPTR_NULL" ); | 
|---|
| 735 |  | 
|---|
| 736 | cxy_t              file_cxy;     // remote file descriptor cluster | 
|---|
| 737 | vfs_file_t       * file_ptr;     // remote file descriptor local pointer | 
|---|
| 738 | vfs_inode_type_t   inode_type; | 
|---|
| 739 | uint32_t           file_offset;  // current offset in file | 
|---|
| 740 | mapper_t         * mapper; | 
|---|
| 741 | error_t            error; | 
|---|
| 742 |  | 
|---|
| 743 | // get cluster and local pointer on remote file descriptor | 
|---|
| 744 | file_cxy  = GET_CXY( file_xp ); | 
|---|
| 745 | file_ptr  = GET_PTR( file_xp ); | 
|---|
| 746 |  | 
|---|
| 747 | // get inode type from remote file descriptor | 
|---|
| 748 | inode_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type   ) ); | 
|---|
| 749 |  | 
|---|
| 750 | assert( (inode_type == INODE_TYPE_FILE) , | 
|---|
| 751 | "inode type is not INODE_TYPE_FILE" ); | 
|---|
| 752 |  | 
|---|
| 753 | // get mapper pointer and file offset from file descriptor | 
|---|
| 754 | file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) ); | 
|---|
| 755 | mapper = (mapper_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) ); | 
|---|
| 756 |  | 
|---|
| 757 | // move data between mapper and buffer | 
|---|
| 758 | if( file_cxy == local_cxy ) | 
|---|
| 759 | { | 
|---|
| 760 | error = mapper_move_user( mapper, | 
|---|
| 761 | to_buffer, | 
|---|
| 762 | file_offset, | 
|---|
| 763 | buffer, | 
|---|
| 764 | size ); | 
|---|
| 765 | } | 
|---|
| 766 | else | 
|---|
| 767 | { | 
|---|
| 768 | rpc_mapper_move_buffer_client( file_cxy, | 
|---|
| 769 | mapper, | 
|---|
| 770 | to_buffer, | 
|---|
| 771 | true,          // user buffer | 
|---|
| 772 | file_offset, | 
|---|
| 773 | (uint64_t)(intptr_t)buffer, | 
|---|
| 774 | size, | 
|---|
| 775 | &error ); | 
|---|
| 776 | } | 
|---|
| 777 |  | 
|---|
| 778 | if( error ) return -1; | 
|---|
| 779 | else        return size; | 
|---|
| 780 |  | 
|---|
| 781 | }  // end vfs_user_move() | 
|---|
| 782 |  | 
|---|
| 783 | //////////////////////////////////////////// | 
|---|
| 784 | error_t vfs_kernel_move( bool_t   to_buffer, | 
|---|
| 785 | xptr_t   file_xp, | 
|---|
| 786 | xptr_t   buffer_xp, | 
|---|
| 787 | uint32_t size ) | 
|---|
| 788 | { | 
|---|
| 789 | assert( ( file_xp != XPTR_NULL ) , "file_xp == XPTR_NULL" ); | 
|---|
| 790 |  | 
|---|
| 791 | cxy_t              file_cxy;     // remote file descriptor cluster | 
|---|
| 792 | vfs_file_t       * file_ptr;     // remote file descriptor local pointer | 
|---|
| 793 | vfs_inode_type_t   inode_type; | 
|---|
| 794 | uint32_t           file_offset;  // current offset in file | 
|---|
| 795 | mapper_t         * mapper; | 
|---|
| 796 | error_t            error; | 
|---|
| 797 |  | 
|---|
| 798 | // get cluster and local pointer on remote file descriptor | 
|---|
| 799 | file_cxy  = GET_CXY( file_xp ); | 
|---|
| 800 | file_ptr  = GET_PTR( file_xp ); | 
|---|
| 801 |  | 
|---|
| 802 | // get inode type from remote file descriptor | 
|---|
| 803 | inode_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type   ) ); | 
|---|
| 804 |  | 
|---|
| 805 | // action depends on inode type | 
|---|
| 806 | if( inode_type == INODE_TYPE_FILE ) | 
|---|
| 807 | { | 
|---|
| 808 | // get mapper pointer and file offset from file descriptor | 
|---|
| 809 | file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) ); | 
|---|
| 810 | mapper = (mapper_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) ); | 
|---|
| 811 |  | 
|---|
| 812 | // move data between mapper and buffer | 
|---|
| 813 | if( file_cxy == local_cxy ) | 
|---|
| 814 | { | 
|---|
| 815 | error = mapper_move_kernel( mapper, | 
|---|
| 816 | to_buffer, | 
|---|
| 817 | file_offset, | 
|---|
| 818 | buffer_xp, | 
|---|
| 819 | size ); | 
|---|
| 820 | } | 
|---|
| 821 | else | 
|---|
| 822 | { | 
|---|
| 823 | rpc_mapper_move_buffer_client( file_cxy, | 
|---|
| 824 | mapper, | 
|---|
| 825 | to_buffer, | 
|---|
| 826 | false,          // kernel buffer | 
|---|
| 827 | file_offset, | 
|---|
| 828 | buffer_xp, | 
|---|
| 829 | size, | 
|---|
| 830 | &error ); | 
|---|
| 831 | } | 
|---|
| 832 |  | 
|---|
| 833 | if( error ) return -1; | 
|---|
| 834 | else        return 0; | 
|---|
| 835 | } | 
|---|
| 836 | else | 
|---|
| 837 | { | 
|---|
| 838 | printk("\n[ERROR] in %s : inode is not a file", __FUNCTION__ ); | 
|---|
| 839 | return -1; | 
|---|
| 840 | } | 
|---|
| 841 | }  // end vfs_kernel_move() | 
|---|
| 842 |  | 
|---|
| 843 | ////////////////////////////////////// | 
|---|
| 844 | error_t vfs_lseek( xptr_t     file_xp, | 
|---|
| 845 | uint32_t   offset, | 
|---|
| 846 | uint32_t   whence, | 
|---|
| 847 | uint32_t * new_offset ) | 
|---|
| 848 | { | 
|---|
| 849 | xptr_t         offset_xp; | 
|---|
| 850 | xptr_t         lock_xp; | 
|---|
| 851 | cxy_t          file_cxy; | 
|---|
| 852 | vfs_file_t  *  file_ptr; | 
|---|
| 853 | vfs_inode_t *  inode_ptr; | 
|---|
| 854 | uint32_t       new; | 
|---|
| 855 |  | 
|---|
| 856 | assert( (file_xp != XPTR_NULL) , "file_xp == XPTR_NULL" ); | 
|---|
| 857 |  | 
|---|
| 858 | // get cluster and local pointer on remote file descriptor | 
|---|
| 859 | file_cxy = GET_CXY( file_xp ); | 
|---|
| 860 | file_ptr = GET_PTR( file_xp ); | 
|---|
| 861 |  | 
|---|
| 862 | // build extended pointers on lock and offset | 
|---|
| 863 | offset_xp = XPTR( file_cxy , &file_ptr->offset ); | 
|---|
| 864 | lock_xp   = XPTR( file_cxy , &file_ptr->lock ); | 
|---|
| 865 |  | 
|---|
| 866 | // take file descriptor lock | 
|---|
| 867 | remote_rwlock_wr_acquire( lock_xp ); | 
|---|
| 868 |  | 
|---|
| 869 | if      ( whence == SEEK_CUR )   // new = current + offset | 
|---|
| 870 | { | 
|---|
| 871 | new = hal_remote_l32( offset_xp ) + offset; | 
|---|
| 872 | } | 
|---|
| 873 | else if ( whence == SEEK_SET )   // new = offset | 
|---|
| 874 | { | 
|---|
| 875 | new = offset; | 
|---|
| 876 | } | 
|---|
| 877 | else if ( whence == SEEK_END )   // new = size + offset | 
|---|
| 878 | { | 
|---|
| 879 | // get local pointer on remote inode | 
|---|
| 880 | inode_ptr = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) ); | 
|---|
| 881 |  | 
|---|
| 882 | new = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) ) + offset; | 
|---|
| 883 | } | 
|---|
| 884 | else | 
|---|
| 885 | { | 
|---|
| 886 | printk("\n[ERROR] in %s : illegal whence value\n", __FUNCTION__ ); | 
|---|
| 887 | remote_rwlock_wr_release( lock_xp ); | 
|---|
| 888 | return -1; | 
|---|
| 889 | } | 
|---|
| 890 |  | 
|---|
| 891 | // set new offset | 
|---|
| 892 | hal_remote_s32( offset_xp , new ); | 
|---|
| 893 |  | 
|---|
| 894 | // release file descriptor lock | 
|---|
| 895 | remote_rwlock_wr_release( lock_xp ); | 
|---|
| 896 |  | 
|---|
| 897 | // success | 
|---|
| 898 | if ( new_offset != NULL ) | 
|---|
| 899 | *new_offset = new; | 
|---|
| 900 | return 0; | 
|---|
| 901 |  | 
|---|
| 902 | }  // vfs_lseek() | 
|---|
| 903 |  | 
|---|
| 904 | /////////////////////////////////// | 
|---|
| 905 | error_t vfs_close( xptr_t   file_xp, | 
|---|
| 906 | uint32_t file_id ) | 
|---|
| 907 | { | 
|---|
| 908 | cluster_t  * cluster;          // local pointer on local cluster | 
|---|
| 909 | cxy_t        file_cxy;         // cluster containing the file descriptor. | 
|---|
| 910 | vfs_file_t * file_ptr;         // local ponter on file descriptor | 
|---|
| 911 | cxy_t        owner_cxy;        // process owner cluster | 
|---|
| 912 | lpid_t       lpid;             // process local index | 
|---|
| 913 | xptr_t       root_xp;          // root of list of process copies | 
|---|
| 914 | xptr_t       lock_xp;          // lock protecting the list of copies | 
|---|
| 915 | xptr_t       iter_xp;          // iterator on list of process copies | 
|---|
| 916 | xptr_t       process_xp;       // extended pointer on one process copy | 
|---|
| 917 | cxy_t        process_cxy;      // process copy cluster | 
|---|
| 918 | process_t  * process_ptr;      // process copy local pointer | 
|---|
| 919 |  | 
|---|
| 920 | assert( (file_xp != XPTR_NULL) , "file_xp == XPTR_NULL" ); | 
|---|
| 921 |  | 
|---|
| 922 | assert( (file_id < CONFIG_PROCESS_FILE_MAX_NR) , "illegal file_id" ); | 
|---|
| 923 |  | 
|---|
| 924 | thread_t  * this    = CURRENT_THREAD; | 
|---|
| 925 | process_t * process = this->process; | 
|---|
| 926 |  | 
|---|
| 927 | #if DEBUG_VFS_CLOSE | 
|---|
| 928 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 929 | if( DEBUG_VFS_CLOSE < cycle ) | 
|---|
| 930 | printk("\n[DBG] %s : thread %x in process %x enter / fdid %d / cycle %d\n", | 
|---|
| 931 | __FUNCTION__, this->trdid, process->pid, file_id, cycle ); | 
|---|
| 932 | #endif | 
|---|
| 933 |  | 
|---|
| 934 | // get local pointer on local cluster manager | 
|---|
| 935 | cluster = LOCAL_CLUSTER; | 
|---|
| 936 |  | 
|---|
| 937 | // get owner process cluster and lpid | 
|---|
| 938 | owner_cxy  = CXY_FROM_PID( process->pid ); | 
|---|
| 939 | lpid       = LPID_FROM_PID( process->pid ); | 
|---|
| 940 |  | 
|---|
| 941 | // get extended pointers on copies root and lock | 
|---|
| 942 | root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] ); | 
|---|
| 943 | lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] ); | 
|---|
| 944 |  | 
|---|
| 945 | // 1) loop on the process descriptor copies to reset all fd_array[file_id] entries | 
|---|
| 946 |  | 
|---|
| 947 | // take the lock protecting the list of copies | 
|---|
| 948 | remote_queuelock_acquire( lock_xp ); | 
|---|
| 949 |  | 
|---|
| 950 | XLIST_FOREACH( root_xp , iter_xp ) | 
|---|
| 951 | { | 
|---|
| 952 | process_xp  = XLIST_ELEMENT( iter_xp , process_t , copies_list ); | 
|---|
| 953 | process_cxy = GET_CXY( process_xp ); | 
|---|
| 954 | process_ptr = GET_PTR( process_xp ); | 
|---|
| 955 |  | 
|---|
| 956 | #if (DEBUG_VFS_CLOSE & 1 ) | 
|---|
| 957 | if( DEBUG_VFS_CLOSE < cycle ) | 
|---|
| 958 | printk("\n[DBG] %s : reset fd_array[%d] for process %x in cluster %x\n", | 
|---|
| 959 | __FUNCTION__, file_id, process_ptr, process_cxy ); | 
|---|
| 960 | #endif | 
|---|
| 961 |  | 
|---|
| 962 | // fd_array lock is required for atomic write of a 64 bits word | 
|---|
| 963 | // xptr_t fd_array_lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock ); | 
|---|
| 964 |  | 
|---|
| 965 | xptr_t entry_xp         = XPTR( process_cxy , &process_ptr->fd_array.array[file_id] ); | 
|---|
| 966 |  | 
|---|
| 967 | // remote_rwlock_wr_acquire( fd_array_lock_xp ); | 
|---|
| 968 |  | 
|---|
| 969 | hal_remote_s64( entry_xp , XPTR_NULL ); | 
|---|
| 970 |  | 
|---|
| 971 | // remote_rwlock_wr_release( fd_array_lock_xp ); | 
|---|
| 972 |  | 
|---|
| 973 | vfs_file_count_down( file_xp ); | 
|---|
| 974 |  | 
|---|
| 975 | hal_fence(); | 
|---|
| 976 | } | 
|---|
| 977 |  | 
|---|
| 978 | // release the lock protecting the list of copies | 
|---|
| 979 | remote_queuelock_release( lock_xp ); | 
|---|
| 980 |  | 
|---|
| 981 | #if (DEBUG_VFS_CLOSE & 1) | 
|---|
| 982 | if( DEBUG_VFS_CLOSE < cycle ) | 
|---|
| 983 | printk("\n[DBG] %s : thread %x in process %x reset all fd-array copies\n", | 
|---|
| 984 | __FUNCTION__, this->trdid, process->pid ); | 
|---|
| 985 | #endif | 
|---|
| 986 |  | 
|---|
| 987 | // 2) release memory allocated to file descriptor in remote cluster | 
|---|
| 988 |  | 
|---|
| 989 | // get cluster and local pointer on remote file descriptor | 
|---|
| 990 | file_cxy = GET_CXY( file_xp ); | 
|---|
| 991 | file_ptr = GET_PTR( file_xp ); | 
|---|
| 992 |  | 
|---|
| 993 | if( file_cxy == local_cxy )             // file cluster is local | 
|---|
| 994 | { | 
|---|
| 995 | vfs_file_destroy( file_ptr ); | 
|---|
| 996 | } | 
|---|
| 997 | else                                    // file cluster is local | 
|---|
| 998 | { | 
|---|
| 999 | rpc_vfs_file_destroy_client( file_cxy , file_ptr ); | 
|---|
| 1000 | } | 
|---|
| 1001 |  | 
|---|
| 1002 | #if DEBUG_VFS_CLOSE | 
|---|
| 1003 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1004 | if( DEBUG_VFS_CLOSE < cycle ) | 
|---|
| 1005 | printk("\n[DBG] %s : thread %x in process %x exit / fdid %d closed / cycle %d\n", | 
|---|
| 1006 | __FUNCTION__, this->trdid, process->pid, file_id, cycle ); | 
|---|
| 1007 | #endif | 
|---|
| 1008 |  | 
|---|
| 1009 | return 0; | 
|---|
| 1010 |  | 
|---|
| 1011 | }  // end vfs_close() | 
|---|
| 1012 |  | 
|---|
| 1013 | //////////////////////////////////// | 
|---|
| 1014 | error_t vfs_unlink( xptr_t   cwd_xp, | 
|---|
| 1015 | char   * path ) | 
|---|
| 1016 | { | 
|---|
| 1017 | assert( false , "not implemented cwd_xp %x, path <%s> \n", | 
|---|
| 1018 | cwd_xp, path ); | 
|---|
| 1019 | return 0; | 
|---|
| 1020 | } | 
|---|
| 1021 |  | 
|---|
| 1022 | //////////////////////////////////////// | 
|---|
| 1023 | error_t vfs_stat( xptr_t        file_xp, | 
|---|
| 1024 | struct stat * k_stat ) | 
|---|
| 1025 | { | 
|---|
| 1026 | assert( false , "not implemented file_xp: %x, k_stat ptr %x\n", | 
|---|
| 1027 | file_xp, k_stat ); | 
|---|
| 1028 | return 0; | 
|---|
| 1029 | } | 
|---|
| 1030 |  | 
|---|
| 1031 | ///////////////////////////////////////////// | 
|---|
| 1032 | error_t vfs_readdir( xptr_t          file_xp, | 
|---|
| 1033 | struct dirent * k_dirent ) | 
|---|
| 1034 | { | 
|---|
| 1035 | assert( false , "not implemented file_xp: %x, k_dirent ptr %x\n", | 
|---|
| 1036 | file_xp, k_dirent ); | 
|---|
| 1037 | return 0; | 
|---|
| 1038 | } | 
|---|
| 1039 |  | 
|---|
| 1040 | ////////////////////////////////////// | 
|---|
| 1041 | error_t vfs_mkdir( xptr_t     file_xp, | 
|---|
| 1042 | char     * path, | 
|---|
| 1043 | uint32_t   mode ) | 
|---|
| 1044 | { | 
|---|
| 1045 | assert( false , "not implemented file_xp: %x, path <%s>, mode: %x\n", | 
|---|
| 1046 | file_xp, path, mode ); | 
|---|
| 1047 | return 0; | 
|---|
| 1048 | } | 
|---|
| 1049 |  | 
|---|
| 1050 | //////////////////////////////////// | 
|---|
| 1051 | error_t vfs_rmdir( xptr_t   file_xp, | 
|---|
| 1052 | char   * path ) | 
|---|
| 1053 | { | 
|---|
| 1054 | assert( false , "not implemented file_xp: %x, path <%s>\n", | 
|---|
| 1055 | file_xp, path ); | 
|---|
| 1056 | return 0; | 
|---|
| 1057 | } | 
|---|
| 1058 |  | 
|---|
| 1059 | /////////////////////////////////// | 
|---|
| 1060 | error_t vfs_chdir( xptr_t   cwd_xp, | 
|---|
| 1061 | char   * path ) | 
|---|
| 1062 | { | 
|---|
| 1063 | error_t           error; | 
|---|
| 1064 | xptr_t            inode_xp;     // extended pointer on target inode | 
|---|
| 1065 | cxy_t             inode_cxy;    // target inode cluster identifier | 
|---|
| 1066 | vfs_inode_t     * inode_ptr;    // target inode local pointer | 
|---|
| 1067 | uint32_t          mode;         // lookup working mode | 
|---|
| 1068 | vfs_inode_type_t  inode_type;   // target inode type | 
|---|
| 1069 |  | 
|---|
| 1070 | // set lookup working mode | 
|---|
| 1071 | mode = 0; | 
|---|
| 1072 |  | 
|---|
| 1073 | // get extended pointer on target inode | 
|---|
| 1074 | error = vfs_lookup( cwd_xp , path , mode , &inode_xp ); | 
|---|
| 1075 |  | 
|---|
| 1076 | if( error ) return error; | 
|---|
| 1077 |  | 
|---|
| 1078 | // get inode cluster and local pointer | 
|---|
| 1079 | inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 1080 | inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 1081 |  | 
|---|
| 1082 | // get inode type from remote file | 
|---|
| 1083 | inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); | 
|---|
| 1084 |  | 
|---|
| 1085 | if( inode_type != INODE_TYPE_DIR ) | 
|---|
| 1086 | { | 
|---|
| 1087 | CURRENT_THREAD->errno = ENOTDIR; | 
|---|
| 1088 | return -1; | 
|---|
| 1089 | } | 
|---|
| 1090 |  | 
|---|
| 1091 | // TODO implement this function using process CWD lock | 
|---|
| 1092 |  | 
|---|
| 1093 | assert( false , "not implemented\n" ); | 
|---|
| 1094 |  | 
|---|
| 1095 | return 0; | 
|---|
| 1096 | } | 
|---|
| 1097 |  | 
|---|
| 1098 | /////////////////////////////////// | 
|---|
| 1099 | error_t vfs_chmod( xptr_t   cwd_xp, | 
|---|
| 1100 | char   * path, | 
|---|
| 1101 | uint32_t rights ) | 
|---|
| 1102 | { | 
|---|
| 1103 | error_t           error; | 
|---|
| 1104 | xptr_t            inode_xp;     // extended pointer on target inode | 
|---|
| 1105 | cxy_t             inode_cxy;    // inode cluster identifier | 
|---|
| 1106 | vfs_inode_t     * inode_ptr;    // inode local pointer | 
|---|
| 1107 | vfs_inode_type_t  inode_type;   // target inode type | 
|---|
| 1108 |  | 
|---|
| 1109 | // set lookup working mode | 
|---|
| 1110 | assert( (rights == 0), __FUNCTION__, | 
|---|
| 1111 | "access rights non implemented yet\n" ); | 
|---|
| 1112 |  | 
|---|
| 1113 | // get extended pointer on target inode | 
|---|
| 1114 | error = vfs_lookup( cwd_xp , path , 0 , &inode_xp ); | 
|---|
| 1115 |  | 
|---|
| 1116 | if( error ) return error; | 
|---|
| 1117 |  | 
|---|
| 1118 | // get inode cluster and local pointer | 
|---|
| 1119 | inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 1120 | inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 1121 |  | 
|---|
| 1122 | // get inode type from remote inode | 
|---|
| 1123 | inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); | 
|---|
| 1124 |  | 
|---|
| 1125 |  | 
|---|
| 1126 | assert( false , "not implemented\n" ); | 
|---|
| 1127 | return 0; | 
|---|
| 1128 | } | 
|---|
| 1129 |  | 
|---|
| 1130 | /////////////////////////////////// | 
|---|
| 1131 | error_t vfs_mkfifo( xptr_t   cwd_xp, | 
|---|
| 1132 | char   * path, | 
|---|
| 1133 | uint32_t rights ) | 
|---|
| 1134 | { | 
|---|
| 1135 | assert( false , "not implemented cwd_xp: %x, path <%s>, rights %x\n", | 
|---|
| 1136 | cwd_xp, path, rights ); | 
|---|
| 1137 | return 0; | 
|---|
| 1138 | } | 
|---|
| 1139 |  | 
|---|
| 1140 |  | 
|---|
| 1141 |  | 
|---|
| 1142 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1143 | //            Inode Tree functions | 
|---|
| 1144 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1145 |  | 
|---|
| 1146 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 1147 | // This static function is called by the vfs_display() function. | 
|---|
| 1148 | // that is supposed to take the TXT0 lock. | 
|---|
| 1149 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 1150 | static void vfs_recursive_display( xptr_t   inode_xp, | 
|---|
| 1151 | xptr_t   name_xp, | 
|---|
| 1152 | uint32_t indent ) | 
|---|
| 1153 | { | 
|---|
| 1154 | cxy_t              inode_cxy; | 
|---|
| 1155 | vfs_inode_t      * inode_ptr; | 
|---|
| 1156 | vfs_inode_type_t   inode_type; | 
|---|
| 1157 | xptr_t             children_xp;    // extended pointer on children xhtab | 
|---|
| 1158 |  | 
|---|
| 1159 | xptr_t             child_dentry_xp; | 
|---|
| 1160 | cxy_t              child_dentry_cxy; | 
|---|
| 1161 | vfs_dentry_t     * child_dentry_ptr; | 
|---|
| 1162 | xptr_t             child_inode_xp; | 
|---|
| 1163 | xptr_t             child_dentry_name_xp; | 
|---|
| 1164 |  | 
|---|
| 1165 | char               name[CONFIG_VFS_MAX_NAME_LENGTH]; | 
|---|
| 1166 |  | 
|---|
| 1167 | char *             indent_str[] = { "",                                  // level 0 | 
|---|
| 1168 | "  ",                                // level 1 | 
|---|
| 1169 | "    ",                              // level 2 | 
|---|
| 1170 | "      ",                            // level 3 | 
|---|
| 1171 | "        ",                          // level 4 | 
|---|
| 1172 | "          ",                        // level 5 | 
|---|
| 1173 | "            ",                      // level 6 | 
|---|
| 1174 | "              ",                    // level 7 | 
|---|
| 1175 | "                ",                  // level 8 | 
|---|
| 1176 | "                  ",                // level 9 | 
|---|
| 1177 | "                    ",              // level 10 | 
|---|
| 1178 | "                      ",            // level 11 | 
|---|
| 1179 | "                        ",          // level 12 | 
|---|
| 1180 | "                          ",        // level 13 | 
|---|
| 1181 | "                            ",      // level 14 | 
|---|
| 1182 | "                              " };  // level 15 | 
|---|
| 1183 |  | 
|---|
| 1184 | assert( (inode_xp != XPTR_NULL) , "inode_xp cannot be NULL\n" ); | 
|---|
| 1185 | assert( (name_xp  != XPTR_NULL) , "name_xp cannot be NULL\n" ); | 
|---|
| 1186 | assert( (indent < 16)           , "depth cannot be larger than 15\n" ); | 
|---|
| 1187 |  | 
|---|
| 1188 | // get inode cluster and local pointer | 
|---|
| 1189 | inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 1190 | inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 1191 |  | 
|---|
| 1192 | // get inode type | 
|---|
| 1193 | inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); | 
|---|
| 1194 |  | 
|---|
| 1195 | // get local pointer on associated mapper | 
|---|
| 1196 | mapper_t * mapper_ptr = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->mapper ) ); | 
|---|
| 1197 |  | 
|---|
| 1198 | // make a local copy of node name | 
|---|
| 1199 | hal_remote_strcpy( XPTR( local_cxy , name ) , name_xp ); | 
|---|
| 1200 |  | 
|---|
| 1201 | // display inode | 
|---|
| 1202 | nolock_printk("%s%s <%s> : inode = %x / mapper = %x / cluster %x\n", | 
|---|
| 1203 | indent_str[indent], vfs_inode_type_str( inode_type ), name, | 
|---|
| 1204 | inode_ptr , mapper_ptr , inode_cxy ); | 
|---|
| 1205 |  | 
|---|
| 1206 | // scan directory entries | 
|---|
| 1207 | if( inode_type == INODE_TYPE_DIR ) | 
|---|
| 1208 | { | 
|---|
| 1209 | // get extended pointer on directory entries xhtab | 
|---|
| 1210 | children_xp =  XPTR( inode_cxy , &inode_ptr->children ); | 
|---|
| 1211 |  | 
|---|
| 1212 | // get xhtab lock | 
|---|
| 1213 | xhtab_lock( children_xp ); | 
|---|
| 1214 |  | 
|---|
| 1215 | // get first dentry from xhtab | 
|---|
| 1216 | child_dentry_xp = xhtab_get_first( children_xp ); | 
|---|
| 1217 |  | 
|---|
| 1218 | while( child_dentry_xp != XPTR_NULL ) | 
|---|
| 1219 | { | 
|---|
| 1220 | // get dentry cluster and local pointer | 
|---|
| 1221 | child_dentry_cxy = GET_CXY( child_dentry_xp ); | 
|---|
| 1222 | child_dentry_ptr = GET_PTR( child_dentry_xp ); | 
|---|
| 1223 |  | 
|---|
| 1224 | // get extended pointer on child inode | 
|---|
| 1225 | child_inode_xp = hal_remote_l64( XPTR( child_dentry_cxy, | 
|---|
| 1226 | &child_dentry_ptr->child_xp ) ); | 
|---|
| 1227 |  | 
|---|
| 1228 | // get extended pointer on dentry name | 
|---|
| 1229 | child_dentry_name_xp = XPTR( child_dentry_cxy , &child_dentry_ptr->name ); | 
|---|
| 1230 |  | 
|---|
| 1231 | // recursive call on inode display | 
|---|
| 1232 | vfs_recursive_display( child_inode_xp, | 
|---|
| 1233 | child_dentry_name_xp, | 
|---|
| 1234 | indent+1 ); | 
|---|
| 1235 |  | 
|---|
| 1236 | // get next dentry | 
|---|
| 1237 | child_dentry_xp = xhtab_get_next( children_xp ); | 
|---|
| 1238 | } | 
|---|
| 1239 |  | 
|---|
| 1240 | // release xhtab lock | 
|---|
| 1241 | xhtab_unlock( children_xp ); | 
|---|
| 1242 | } | 
|---|
| 1243 | }  // end vfs_recursive_display() | 
|---|
| 1244 |  | 
|---|
| 1245 | /////////////////////////////////// | 
|---|
| 1246 | void vfs_display( xptr_t inode_xp ) | 
|---|
| 1247 | { | 
|---|
| 1248 | xptr_t         name_xp; | 
|---|
| 1249 | xptr_t         dentry_xp; | 
|---|
| 1250 | cxy_t          dentry_cxy; | 
|---|
| 1251 | vfs_dentry_t * dentry_ptr; | 
|---|
| 1252 |  | 
|---|
| 1253 | // get target inode cluster and local pointer | 
|---|
| 1254 | cxy_t         inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 1255 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 1256 |  | 
|---|
| 1257 | // get extended pointer on associated dentry | 
|---|
| 1258 | dentry_xp   = hal_remote_l64( XPTR( inode_cxy , &inode_ptr->parent_xp ) ); | 
|---|
| 1259 |  | 
|---|
| 1260 | // check if target inode is the File System root | 
|---|
| 1261 | if( dentry_xp == XPTR_NULL ) | 
|---|
| 1262 | { | 
|---|
| 1263 | // build extended pointer on root name | 
|---|
| 1264 | name_xp = XPTR( local_cxy , "/" ); | 
|---|
| 1265 | } | 
|---|
| 1266 | else | 
|---|
| 1267 | { | 
|---|
| 1268 | // get dentry cluster and local pointer | 
|---|
| 1269 | dentry_cxy = GET_CXY( dentry_xp ); | 
|---|
| 1270 | dentry_ptr = GET_PTR( dentry_xp ); | 
|---|
| 1271 |  | 
|---|
| 1272 | // get extended pointer on dentry name | 
|---|
| 1273 | name_xp = XPTR( dentry_cxy , &dentry_ptr->name ); | 
|---|
| 1274 | } | 
|---|
| 1275 |  | 
|---|
| 1276 | // get pointers on TXT0 chdev | 
|---|
| 1277 | xptr_t    txt0_xp  = chdev_dir.txt_tx[0]; | 
|---|
| 1278 | cxy_t     txt0_cxy = GET_CXY( txt0_xp ); | 
|---|
| 1279 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); | 
|---|
| 1280 |  | 
|---|
| 1281 | // get extended pointer on remote TXT0 chdev lock | 
|---|
| 1282 | xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); | 
|---|
| 1283 |  | 
|---|
| 1284 | // get TXT0 lock in busy waiting mode | 
|---|
| 1285 | remote_busylock_acquire( lock_xp ); | 
|---|
| 1286 |  | 
|---|
| 1287 | // print header | 
|---|
| 1288 | nolock_printk("\n***** file system state\n\n"); | 
|---|
| 1289 |  | 
|---|
| 1290 | // call recursive function | 
|---|
| 1291 | vfs_recursive_display( inode_xp , name_xp , 0 ); | 
|---|
| 1292 |  | 
|---|
| 1293 | // release lock | 
|---|
| 1294 | remote_busylock_release( lock_xp ); | 
|---|
| 1295 |  | 
|---|
| 1296 | }  // end vfs_display() | 
|---|
| 1297 |  | 
|---|
| 1298 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1299 | // This function is used by the vfs_lookup() function. | 
|---|
| 1300 | // It takes an extended pointer on a remote inode (parent directory inode), | 
|---|
| 1301 | // and check access_rights violation for the calling thread. | 
|---|
| 1302 | // It can be used by any thread running in any cluster. | 
|---|
| 1303 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1304 | // @ inode_xp    : extended pointer on inode. | 
|---|
| 1305 | // @ client_uid  : client thread user ID | 
|---|
| 1306 | // @ client_gid  : client thread group ID | 
|---|
| 1307 | // @ return true if access rights are violated. | 
|---|
| 1308 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1309 | bool_t vfs_access_denied( xptr_t   inode_xp, | 
|---|
| 1310 | uint32_t client_uid, | 
|---|
| 1311 | uint32_t client_gid ) | 
|---|
| 1312 | { | 
|---|
| 1313 | // get found inode cluster and local pointer | 
|---|
| 1314 | cxy_t         inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 1315 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 1316 |  | 
|---|
| 1317 | // get inode access mode, UID, and GID | 
|---|
| 1318 | // TODO uint32_t  mode = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->mode ) ); | 
|---|
| 1319 | uid_t     uid  = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->uid  ) ); | 
|---|
| 1320 | gid_t     gid  = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->gid  ) ); | 
|---|
| 1321 |  | 
|---|
| 1322 | // FIXME : me must use mode | 
|---|
| 1323 | if( (uid == client_uid) || (gid == client_gid) ) return false; | 
|---|
| 1324 | else                                             return true; | 
|---|
| 1325 | } | 
|---|
| 1326 |  | 
|---|
| 1327 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1328 | // This static function is used by the vfs_lookup() function. | 
|---|
| 1329 | // It takes an extended pointer on a remote parent directory inode, a directory | 
|---|
| 1330 | // entry name, and returns an extended pointer on the child inode. | 
|---|
| 1331 | // It can be used by any thread running in any cluster. | 
|---|
| 1332 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1333 | // @ parent_xp   : extended pointer on parent inode in remote cluster. | 
|---|
| 1334 | // @ name        : dentry name | 
|---|
| 1335 | // @ child_xp    : [out] buffer for extended pointer on child inode. | 
|---|
| 1336 | // @ return true if success / return false if not found. | 
|---|
| 1337 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1338 | static bool_t vfs_get_child( xptr_t   parent_xp, | 
|---|
| 1339 | char   * name, | 
|---|
| 1340 | xptr_t * child_xp ) | 
|---|
| 1341 | { | 
|---|
| 1342 | xptr_t  xhtab_xp;    // extended pointer on hash table containing children dentries | 
|---|
| 1343 | xptr_t  dentry_xp;   // extended pointer on children dentry | 
|---|
| 1344 |  | 
|---|
| 1345 | // get parent inode cluster and local pointer | 
|---|
| 1346 | cxy_t         parent_cxy = GET_CXY( parent_xp ); | 
|---|
| 1347 | vfs_inode_t * parent_ptr = GET_PTR( parent_xp ); | 
|---|
| 1348 |  | 
|---|
| 1349 | // get extended pointer on hash table of children directory entries | 
|---|
| 1350 | xhtab_xp = XPTR( parent_cxy , &parent_ptr->children ); | 
|---|
| 1351 |  | 
|---|
| 1352 | // search extended pointer on matching dentry | 
|---|
| 1353 | dentry_xp = xhtab_lookup( xhtab_xp , name ); | 
|---|
| 1354 |  | 
|---|
| 1355 | if( dentry_xp == XPTR_NULL ) return false; | 
|---|
| 1356 |  | 
|---|
| 1357 | // get dentry cluster and local pointer | 
|---|
| 1358 | cxy_t          dentry_cxy = GET_CXY( dentry_xp ); | 
|---|
| 1359 | vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp ); | 
|---|
| 1360 |  | 
|---|
| 1361 | // return child inode | 
|---|
| 1362 | *child_xp = (xptr_t)hal_remote_l64( XPTR( dentry_cxy , &dentry_ptr->child_xp ) ); | 
|---|
| 1363 | return true; | 
|---|
| 1364 |  | 
|---|
| 1365 | }  // end vfs_get_child() | 
|---|
| 1366 |  | 
|---|
| 1367 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1368 | // This static function is used by the vfs_lookup() function. | 
|---|
| 1369 | // It takes the <current> pointer on a buffer containing a complete pathname, and return | 
|---|
| 1370 | // in the <name> buffer, allocated by the caller, a single name in the path. | 
|---|
| 1371 | // It return also in the <next> pointer the next character to analyse in the path. | 
|---|
| 1372 | // Finally it returns a <last> boolean, that is true when the returned <name> is the | 
|---|
| 1373 | // last name in the path. The names are supposed to be separated by one or several '/' | 
|---|
| 1374 | // characters, that are not written in  the <name> buffer. | 
|---|
| 1375 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1376 | // @ current   : pointer on first character to analyse in buffer containing the path. | 
|---|
| 1377 | // @ name      : [out] pointer on buffer allocated by the caller for the returned name. | 
|---|
| 1378 | // @ next      : [out] pointer on next character to analyse in buffer containing the path. | 
|---|
| 1379 | // @ last      : [out] true if the returned name is the last (NUL character found). | 
|---|
| 1380 | // @ return 0 if success / return EINVAL if string empty (first chracter is NUL). | 
|---|
| 1381 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1382 | static error_t vfs_get_name_from_path( char     * current, | 
|---|
| 1383 | char     * name, | 
|---|
| 1384 | char    ** next, | 
|---|
| 1385 | bool_t   * last ) | 
|---|
| 1386 | { | 
|---|
| 1387 | char * ptr = current; | 
|---|
| 1388 |  | 
|---|
| 1389 | // skip leading '/' characters | 
|---|
| 1390 | while( *ptr == '/' ) ptr++; | 
|---|
| 1391 |  | 
|---|
| 1392 | // return EINVAL if string empty | 
|---|
| 1393 | if( *ptr == 0 ) return EINVAL; | 
|---|
| 1394 |  | 
|---|
| 1395 | // copy all characters in name until NUL or '/' | 
|---|
| 1396 | while( (*ptr != 0) && (*ptr !='/') )  *(name++) = *(ptr++); | 
|---|
| 1397 |  | 
|---|
| 1398 | // set NUL terminating character in name buffer | 
|---|
| 1399 | *(name++) = 0; | 
|---|
| 1400 |  | 
|---|
| 1401 | // return last an next | 
|---|
| 1402 | if( *ptr == 0 )             // last found character is NUL => last name in path | 
|---|
| 1403 | { | 
|---|
| 1404 | *last = true; | 
|---|
| 1405 | } | 
|---|
| 1406 | else                        // last found character is '/' => skip it | 
|---|
| 1407 | { | 
|---|
| 1408 | *last = false; | 
|---|
| 1409 | *next = ptr + 1; | 
|---|
| 1410 | } | 
|---|
| 1411 |  | 
|---|
| 1412 | return 0; | 
|---|
| 1413 |  | 
|---|
| 1414 | }  // end vfs_get name_from_path() | 
|---|
| 1415 |  | 
|---|
| 1416 | ////////////////////////////////////////////// | 
|---|
| 1417 | error_t vfs_lookup( xptr_t             cwd_xp, | 
|---|
| 1418 | char             * pathname, | 
|---|
| 1419 | uint32_t           mode, | 
|---|
| 1420 | xptr_t           * inode_xp ) | 
|---|
| 1421 | { | 
|---|
| 1422 | char               name[CONFIG_VFS_MAX_NAME_LENGTH];   // one name in path | 
|---|
| 1423 |  | 
|---|
| 1424 | xptr_t             parent_xp;    // extended pointer on parent inode | 
|---|
| 1425 | cxy_t              parent_cxy;   // cluster for parent inode | 
|---|
| 1426 | vfs_inode_t      * parent_ptr;   // local pointer on parent inode | 
|---|
| 1427 | xptr_t             child_xp;     // extended pointer on child inode | 
|---|
| 1428 | cxy_t              child_cxy;    // cluster for child inode | 
|---|
| 1429 | vfs_inode_t      * child_ptr;    // local pointer on child inode | 
|---|
| 1430 | vfs_fs_type_t      fs_type;      // File system type | 
|---|
| 1431 | vfs_ctx_t        * ctx_ptr;      // local pointer on FS context | 
|---|
| 1432 | char             * current;      // current pointer on path | 
|---|
| 1433 | char             * next;         // next value for current pointer | 
|---|
| 1434 | bool_t             last;         // true when the name is the last in path | 
|---|
| 1435 | bool_t             found;        // true when a child has been found | 
|---|
| 1436 | bool_t             dir;          // searched inode is a directory | 
|---|
| 1437 | bool_t             create;       // searched inode must be created if not found | 
|---|
| 1438 | bool_t             excl;         // searched inode must not exist | 
|---|
| 1439 | thread_t         * this;         // pointer on calling thread descriptor | 
|---|
| 1440 | process_t        * process;      // pointer on calling process descriptor | 
|---|
| 1441 | error_t            error; | 
|---|
| 1442 |  | 
|---|
| 1443 | this    = CURRENT_THREAD; | 
|---|
| 1444 | process = this->process; | 
|---|
| 1445 |  | 
|---|
| 1446 | #if DEBUG_VFS_LOOKUP | 
|---|
| 1447 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1448 | if( DEBUG_VFS_LOOKUP < cycle ) | 
|---|
| 1449 | printk("\n[DBG] %s : thread %x in process %x enter for <%s> / cycle %d\n", | 
|---|
| 1450 | __FUNCTION__, this->trdid, process->pid, pathname, cycle ); | 
|---|
| 1451 | #endif | 
|---|
| 1452 |  | 
|---|
| 1453 | // compute lookup flags | 
|---|
| 1454 | dir    = mode & VFS_LOOKUP_DIR; | 
|---|
| 1455 | create = mode & VFS_LOOKUP_CREATE; | 
|---|
| 1456 | excl   = mode & VFS_LOOKUP_EXCL; | 
|---|
| 1457 |  | 
|---|
| 1458 | // get extended pointer on first inode to search | 
|---|
| 1459 | if( pathname[0] == '/' ) parent_xp = process->vfs_root_xp; | 
|---|
| 1460 | else                     parent_xp = cwd_xp; | 
|---|
| 1461 |  | 
|---|
| 1462 | // initialise other loop variables | 
|---|
| 1463 | current  = pathname; | 
|---|
| 1464 | next     = NULL; | 
|---|
| 1465 | last     = false; | 
|---|
| 1466 | child_xp = XPTR_NULL; | 
|---|
| 1467 |  | 
|---|
| 1468 | // take lock on parent inode | 
|---|
| 1469 | vfs_inode_lock( parent_xp ); | 
|---|
| 1470 |  | 
|---|
| 1471 | // sequencially loop on nodes in pathname | 
|---|
| 1472 | // load from device if one node in path not found in inode tree | 
|---|
| 1473 | // exit loop when last name found (i.e. last == true) | 
|---|
| 1474 | do | 
|---|
| 1475 | { | 
|---|
| 1476 | // get one name from path, and "last" flag | 
|---|
| 1477 | vfs_get_name_from_path( current , name , &next , &last ); | 
|---|
| 1478 |  | 
|---|
| 1479 | #if (DEBUG_VFS_LOOKUP & 1) | 
|---|
| 1480 | if( DEBUG_VFS_LOOKUP < cycle ) | 
|---|
| 1481 | printk("\n[DBG] %s : look for <%s> / last = %d\n", | 
|---|
| 1482 | __FUNCTION__ , name , last ); | 
|---|
| 1483 | #endif | 
|---|
| 1484 |  | 
|---|
| 1485 | // search a child dentry matching name in parent inode | 
|---|
| 1486 | found = vfs_get_child( parent_xp, | 
|---|
| 1487 | name, | 
|---|
| 1488 | &child_xp ); | 
|---|
| 1489 |  | 
|---|
| 1490 | if (found == false )  // child not found in inode tree | 
|---|
| 1491 | { | 
|---|
| 1492 |  | 
|---|
| 1493 | #if (DEBUG_VFS_LOOKUP & 1) | 
|---|
| 1494 | if( DEBUG_VFS_LOOKUP < cycle ) | 
|---|
| 1495 | printk("\n[DBG] %s : miss <%s> node => try to create it\n", | 
|---|
| 1496 | __FUNCTION__ , name ); | 
|---|
| 1497 | #endif | 
|---|
| 1498 | // if a child node is not found in the inode tree, | 
|---|
| 1499 | // we introduce a new (dentry/inode) in inode tree, | 
|---|
| 1500 | // and try to find it by scanning the parent directory mapper. | 
|---|
| 1501 | // . if it is found in parent mapper: | 
|---|
| 1502 | //   - if the child is a directory, the child mapper is loaded from device | 
|---|
| 1503 | //   - if the child is not a directory, the search is completed | 
|---|
| 1504 | // . if it is not found in the parent mapper: | 
|---|
| 1505 | //   - if ( not last or not create ) an error is reported | 
|---|
| 1506 | //   - if (last and create and dir) a new directory is created | 
|---|
| 1507 | //   - if (last and create and not dir) a new file is created | 
|---|
| 1508 |  | 
|---|
| 1509 | // release lock on parent inode | 
|---|
| 1510 | vfs_inode_unlock( parent_xp ); | 
|---|
| 1511 |  | 
|---|
| 1512 | // get parent inode FS type | 
|---|
| 1513 | parent_cxy = GET_CXY( parent_xp ); | 
|---|
| 1514 | parent_ptr = GET_PTR( parent_xp ); | 
|---|
| 1515 | ctx_ptr    = (vfs_ctx_t *)hal_remote_lpt( XPTR( parent_cxy,&parent_ptr->ctx ) ); | 
|---|
| 1516 | fs_type    = hal_remote_l32( XPTR( parent_cxy , &ctx_ptr->type ) ); | 
|---|
| 1517 |  | 
|---|
| 1518 | // select a cluster for missing inode | 
|---|
| 1519 | child_cxy = cluster_random_select(); | 
|---|
| 1520 |  | 
|---|
| 1521 | // insert a new child dentry/inode in inode tree | 
|---|
| 1522 | error = vfs_add_child_in_parent( child_cxy, | 
|---|
| 1523 | 0,           // type will be updated later | 
|---|
| 1524 | fs_type, | 
|---|
| 1525 | parent_xp, | 
|---|
| 1526 | name, | 
|---|
| 1527 | NULL,        // fs_type_specific inode extend | 
|---|
| 1528 | &child_xp ); | 
|---|
| 1529 | if( error ) | 
|---|
| 1530 | { | 
|---|
| 1531 | printk("\n[ERROR] in %s : cannot create node %s for path <%s>\n", | 
|---|
| 1532 | __FUNCTION__ , name, pathname ); | 
|---|
| 1533 | return ENOMEM; | 
|---|
| 1534 | } | 
|---|
| 1535 |  | 
|---|
| 1536 | // get child inode cluster and local pointer | 
|---|
| 1537 | child_cxy = GET_CXY( child_xp ); | 
|---|
| 1538 | child_ptr = GET_PTR( child_xp ); | 
|---|
| 1539 |  | 
|---|
| 1540 | #if (DEBUG_VFS_LOOKUP & 1) | 
|---|
| 1541 | if( DEBUG_VFS_LOOKUP < cycle ) | 
|---|
| 1542 | printk("\n[DBG] %s : missing <%s> inode speculatively created / cxy %x / ptr %x\n", | 
|---|
| 1543 | __FUNCTION__ , name , child_cxy, child_ptr ); | 
|---|
| 1544 | #endif | 
|---|
| 1545 | // scan parent mapper to complete the missing inode | 
|---|
| 1546 | if( parent_cxy == local_cxy ) | 
|---|
| 1547 | { | 
|---|
| 1548 | error = vfs_inode_load( parent_ptr, | 
|---|
| 1549 | name, | 
|---|
| 1550 | child_xp ); | 
|---|
| 1551 | } | 
|---|
| 1552 | else | 
|---|
| 1553 | { | 
|---|
| 1554 | rpc_vfs_inode_load_client( parent_cxy, | 
|---|
| 1555 | parent_ptr, | 
|---|
| 1556 | name, | 
|---|
| 1557 | child_xp, | 
|---|
| 1558 | &error ); | 
|---|
| 1559 | } | 
|---|
| 1560 |  | 
|---|
| 1561 | if ( error )   // child not found in parent mapper | 
|---|
| 1562 | { | 
|---|
| 1563 | if( last && create && dir )  // new directory  => update inode type | 
|---|
| 1564 | { | 
|---|
| 1565 | hal_remote_s32( XPTR( child_cxy, &child_ptr->type ), INODE_TYPE_DIR ); | 
|---|
| 1566 |  | 
|---|
| 1567 | #if (DEBUG_VFS_LOOKUP & 1) | 
|---|
| 1568 | if( DEBUG_VFS_LOOKUP < cycle ) | 
|---|
| 1569 | printk("\n[DBG] %s : created node <%s> in path %s / type DIR\n", | 
|---|
| 1570 | __FUNCTION__ , name, pathname ); | 
|---|
| 1571 | #endif | 
|---|
| 1572 | } | 
|---|
| 1573 | else if ( last && create )   // new file => update inode type | 
|---|
| 1574 | { | 
|---|
| 1575 | hal_remote_s32( XPTR( child_cxy, &child_ptr->type ), INODE_TYPE_FILE ); | 
|---|
| 1576 |  | 
|---|
| 1577 | #if (DEBUG_VFS_LOOKUP & 1) | 
|---|
| 1578 | if( DEBUG_VFS_LOOKUP < cycle ) | 
|---|
| 1579 | printk("\n[DBG] %s : created node <%s> in path %s / type FILE\n", | 
|---|
| 1580 | __FUNCTION__ , name, pathname ); | 
|---|
| 1581 | #endif | 
|---|
| 1582 | } | 
|---|
| 1583 | else                         // not last or not create => remove created node | 
|---|
| 1584 | { | 
|---|
| 1585 | printk("\n[ERROR] in %s : <%s> node not found in parent for <%s>\n", | 
|---|
| 1586 | __FUNCTION__ , name , pathname ); | 
|---|
| 1587 | vfs_remove_child_from_parent( child_xp ); | 
|---|
| 1588 | return ENOENT; | 
|---|
| 1589 | } | 
|---|
| 1590 | } | 
|---|
| 1591 | else          // child found in parent | 
|---|
| 1592 | { | 
|---|
| 1593 | // load child mapper from device if child is a directory (prefetch) | 
|---|
| 1594 | if( hal_remote_l32( XPTR( child_cxy , &child_ptr->type ) ) == INODE_TYPE_DIR ) | 
|---|
| 1595 | { | 
|---|
| 1596 | if( child_cxy == local_cxy ) | 
|---|
| 1597 | { | 
|---|
| 1598 | error = vfs_mapper_load_all( child_ptr ); | 
|---|
| 1599 | } | 
|---|
| 1600 | else | 
|---|
| 1601 | { | 
|---|
| 1602 | rpc_vfs_mapper_load_all_client( child_cxy, | 
|---|
| 1603 | child_ptr, | 
|---|
| 1604 | &error ); | 
|---|
| 1605 | } | 
|---|
| 1606 | if ( error ) | 
|---|
| 1607 | { | 
|---|
| 1608 | printk("\n[ERROR] in %s : cannot load <%s> from device\n", | 
|---|
| 1609 | __FUNCTION__ , name ); | 
|---|
| 1610 | vfs_remove_child_from_parent( child_xp ); | 
|---|
| 1611 | return EIO; | 
|---|
| 1612 | } | 
|---|
| 1613 |  | 
|---|
| 1614 | #if (DEBUG_VFS_LOOKUP & 1) | 
|---|
| 1615 | if( DEBUG_VFS_LOOKUP < cycle ) | 
|---|
| 1616 | printk("\n[DBG] %s : load mapper from device for node <%s> in path %s\n", | 
|---|
| 1617 | __FUNCTION__ , name, pathname ); | 
|---|
| 1618 | #endif | 
|---|
| 1619 | } | 
|---|
| 1620 | } | 
|---|
| 1621 |  | 
|---|
| 1622 | // take lock on parent inode | 
|---|
| 1623 | vfs_inode_lock( parent_xp ); | 
|---|
| 1624 | } | 
|---|
| 1625 | else   // child found in inode tree | 
|---|
| 1626 | { | 
|---|
| 1627 |  | 
|---|
| 1628 | #if (DEBUG_VFS_LOOKUP & 1) | 
|---|
| 1629 | if( DEBUG_VFS_LOOKUP < cycle ) | 
|---|
| 1630 | printk("\n[DBG] %s : found <%s> / inode %x in cluster %x\n", | 
|---|
| 1631 | __FUNCTION__ , name , GET_PTR(child_xp) , GET_CXY(child_xp) ); | 
|---|
| 1632 | #endif | 
|---|
| 1633 | child_ptr  = GET_PTR( child_xp ); | 
|---|
| 1634 | child_cxy  = GET_CXY( child_xp ); | 
|---|
| 1635 | parent_cxy = GET_CXY( parent_xp ); | 
|---|
| 1636 | parent_ptr = GET_PTR( parent_xp ); | 
|---|
| 1637 |  | 
|---|
| 1638 | if( last && (mode & VFS_LOOKUP_CREATE) && (mode & VFS_LOOKUP_EXCL) ) | 
|---|
| 1639 | { | 
|---|
| 1640 | printk("\n[ERROR] in %s : node already exist <%s>\n", __FUNCTION__, name ); | 
|---|
| 1641 | return EINVAL; | 
|---|
| 1642 | } | 
|---|
| 1643 | } | 
|---|
| 1644 |  | 
|---|
| 1645 | // TODO check access rights here [AG] | 
|---|
| 1646 | // error = vfs_access_denied( child_xp, | 
|---|
| 1647 | //                            client_uid, | 
|---|
| 1648 | //                            client_gid ); | 
|---|
| 1649 | // if( error ) | 
|---|
| 1650 | // { | 
|---|
| 1651 | //     printk("\n[ERROR] in %s : thread %x / permission denied for %s\n", | 
|---|
| 1652 | //     __FUNCTION__ , this , name ); | 
|---|
| 1653 | //     return EACCES; | 
|---|
| 1654 | // } | 
|---|
| 1655 |  | 
|---|
| 1656 | // take lock on child inode and release lock on parent | 
|---|
| 1657 | vfs_inode_lock( child_xp ); | 
|---|
| 1658 | vfs_inode_unlock( parent_xp ); | 
|---|
| 1659 |  | 
|---|
| 1660 | // update loop variables | 
|---|
| 1661 | parent_xp = child_xp; | 
|---|
| 1662 | current   = next; | 
|---|
| 1663 | } | 
|---|
| 1664 | while( last == false ); | 
|---|
| 1665 |  | 
|---|
| 1666 | // release lock | 
|---|
| 1667 | vfs_inode_unlock( parent_xp ); | 
|---|
| 1668 |  | 
|---|
| 1669 | #if DEBUG_VFS_LOOKUP | 
|---|
| 1670 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1671 | if( DEBUG_VFS_LOOKUP < cycle ) | 
|---|
| 1672 | printk("\n[DBG] %s : thread %x in process %x exit for <%s>\n" | 
|---|
| 1673 | "     parent %x in cluster %x / child %x in cluster %x / cycle %d\n", | 
|---|
| 1674 | __FUNCTION__ , this->trdid, process->pid, pathname, | 
|---|
| 1675 | parent_ptr, parent_cxy, child_ptr, child_cxy, cycle ); | 
|---|
| 1676 | #endif | 
|---|
| 1677 |  | 
|---|
| 1678 | // return searched pointer | 
|---|
| 1679 | if( mode & VFS_LOOKUP_PARENT ) *inode_xp = parent_xp; | 
|---|
| 1680 | else                           *inode_xp = child_xp; | 
|---|
| 1681 |  | 
|---|
| 1682 | return 0; | 
|---|
| 1683 |  | 
|---|
| 1684 | }  // end vfs_lookup() | 
|---|
| 1685 |  | 
|---|
| 1686 | //////////////////////////////////////////// | 
|---|
| 1687 | error_t vfs_get_path( xptr_t    searched_xp, | 
|---|
| 1688 | char    * buffer, | 
|---|
| 1689 | uint32_t  max_size ) | 
|---|
| 1690 | { | 
|---|
| 1691 | xptr_t       dentry_xp;   // extended pointer on current dentry | 
|---|
| 1692 | char       * name;        // local pointer on current dentry name | 
|---|
| 1693 | uint32_t     length;      // length of current dentry name | 
|---|
| 1694 | uint32_t     count;       // number of characters written in buffer | 
|---|
| 1695 | uint32_t     index;       // slot index in buffer | 
|---|
| 1696 | xptr_t       inode_xp;    // extended pointer on | 
|---|
| 1697 |  | 
|---|
| 1698 | // implementation note: | 
|---|
| 1699 | // we use two variables "index" and "count" because the buffer | 
|---|
| 1700 | // is written in decreasing index order (from leaf to root) | 
|---|
| 1701 | // TODO  : handle conflict with a concurrent rename [AG] | 
|---|
| 1702 | // FIXME : handle synchro in the loop  [AG] | 
|---|
| 1703 |  | 
|---|
| 1704 | // set the NUL character in buffer / initialise buffer index and count | 
|---|
| 1705 | buffer[max_size - 1] = 0; | 
|---|
| 1706 | count    = 1; | 
|---|
| 1707 | index    = max_size - 2; | 
|---|
| 1708 |  | 
|---|
| 1709 | // initialize current inode | 
|---|
| 1710 | inode_xp  = searched_xp; | 
|---|
| 1711 |  | 
|---|
| 1712 | // exit when root inode found (i.e. dentry_xp == XPTR_NULL) | 
|---|
| 1713 | do | 
|---|
| 1714 | { | 
|---|
| 1715 | // get inode cluster and local pointer | 
|---|
| 1716 | cxy_t         inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 1717 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 1718 |  | 
|---|
| 1719 | // get extended pointer on parent dentry | 
|---|
| 1720 | dentry_xp = (xptr_t)hal_remote_l64( XPTR( inode_cxy , inode_ptr->parent_xp ) ); | 
|---|
| 1721 |  | 
|---|
| 1722 | // get dentry cluster and local pointer | 
|---|
| 1723 | cxy_t          dentry_cxy = GET_CXY( dentry_xp ); | 
|---|
| 1724 | vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp ); | 
|---|
| 1725 |  | 
|---|
| 1726 | // get dentry name length and pointer | 
|---|
| 1727 | length =  hal_remote_l32( XPTR( dentry_cxy , &dentry_ptr->length ) ); | 
|---|
| 1728 | name   = (char *)hal_remote_lpt( XPTR( dentry_cxy , &dentry_ptr->name ) ); | 
|---|
| 1729 |  | 
|---|
| 1730 | // update index and count | 
|---|
| 1731 | index -= (length + 1); | 
|---|
| 1732 | count += (length + 1); | 
|---|
| 1733 |  | 
|---|
| 1734 | // check buffer overflow | 
|---|
| 1735 | if( count >= max_size ) | 
|---|
| 1736 | { | 
|---|
| 1737 | printk("\n[ERROR] in %s : kernel buffer too small\n", __FUNCTION__ ); | 
|---|
| 1738 | return EINVAL; | 
|---|
| 1739 | } | 
|---|
| 1740 |  | 
|---|
| 1741 | // update pathname | 
|---|
| 1742 | hal_remote_memcpy( XPTR( local_cxy , &buffer[index + 1] ) , | 
|---|
| 1743 | XPTR( dentry_cxy , name ) , length ); | 
|---|
| 1744 | buffer[index] = '/'; | 
|---|
| 1745 |  | 
|---|
| 1746 | // get extended pointer on next inode | 
|---|
| 1747 | inode_xp = (xptr_t)hal_remote_l64( XPTR( dentry_cxy , dentry_ptr->parent ) ); | 
|---|
| 1748 | } | 
|---|
| 1749 | while( (dentry_xp != XPTR_NULL) ); | 
|---|
| 1750 |  | 
|---|
| 1751 | return 0; | 
|---|
| 1752 |  | 
|---|
| 1753 | }  // end vfs_get_path() | 
|---|
| 1754 |  | 
|---|
| 1755 |  | 
|---|
| 1756 | ////////////////////////////////////////////////////////////// | 
|---|
| 1757 | error_t vfs_add_child_in_parent( cxy_t              child_cxy, | 
|---|
| 1758 | vfs_inode_type_t   inode_type, | 
|---|
| 1759 | vfs_fs_type_t      fs_type, | 
|---|
| 1760 | xptr_t             parent_xp, | 
|---|
| 1761 | char             * name, | 
|---|
| 1762 | void             * extend, | 
|---|
| 1763 | xptr_t           * child_xp ) | 
|---|
| 1764 | { | 
|---|
| 1765 | error_t         error; | 
|---|
| 1766 | xptr_t          dentry_xp;   // extended pointer on created dentry | 
|---|
| 1767 | xptr_t          inode_xp;    // extended pointer on created inode | 
|---|
| 1768 | cxy_t           parent_cxy;  // parent inode cluster identifier | 
|---|
| 1769 | vfs_inode_t   * parent_ptr;  // parent inode local pointer | 
|---|
| 1770 |  | 
|---|
| 1771 | // get parent inode cluster and local pointer | 
|---|
| 1772 | parent_cxy = GET_CXY( parent_xp ); | 
|---|
| 1773 | parent_ptr = GET_PTR( parent_xp ); | 
|---|
| 1774 |  | 
|---|
| 1775 | #if DEBUG_VFS_ADD_CHILD | 
|---|
| 1776 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1777 | thread_t * this = CURRENT_THREAD; | 
|---|
| 1778 | if( DEBUG_VFS_ADD_CHILD < cycle ) | 
|---|
| 1779 | printk("\n[DBG] %s : thread %x in process %x enter for <%s>\n" | 
|---|
| 1780 | "      child_cxy = %x / parent_cxy = %x / cycle %d\n", | 
|---|
| 1781 | __FUNCTION__, this->trdid, this->process->pid, name, | 
|---|
| 1782 | child_cxy, parent_cxy, (uint32_t)hal_get_cycles() ); | 
|---|
| 1783 | #endif | 
|---|
| 1784 |  | 
|---|
| 1785 | // 1. create dentry | 
|---|
| 1786 | if( parent_cxy == local_cxy )      // parent cluster is the local cluster | 
|---|
| 1787 | { | 
|---|
| 1788 | error = vfs_dentry_create( fs_type, | 
|---|
| 1789 | name, | 
|---|
| 1790 | parent_ptr, | 
|---|
| 1791 | &dentry_xp ); | 
|---|
| 1792 | } | 
|---|
| 1793 | else                               // parent cluster is remote | 
|---|
| 1794 | { | 
|---|
| 1795 | rpc_vfs_dentry_create_client( parent_cxy, | 
|---|
| 1796 | fs_type, | 
|---|
| 1797 | name, | 
|---|
| 1798 | parent_ptr, | 
|---|
| 1799 | &dentry_xp, | 
|---|
| 1800 | &error ); | 
|---|
| 1801 | } | 
|---|
| 1802 |  | 
|---|
| 1803 | if( error ) | 
|---|
| 1804 | { | 
|---|
| 1805 | printk("\n[ERROR] in %s : cannot create dentry <%s> in cluster %x\n", | 
|---|
| 1806 | __FUNCTION__ , name , parent_cxy ); | 
|---|
| 1807 | return ENOMEM; | 
|---|
| 1808 | } | 
|---|
| 1809 |  | 
|---|
| 1810 | #if(DEBUG_VFS_ADD_CHILD & 1) | 
|---|
| 1811 | if( DEBUG_VFS_ADD_CHILD < cycle ) | 
|---|
| 1812 | printk("\n[DBG] %s : thread %x in process %x / dentry <%s> created in cluster %x\n", | 
|---|
| 1813 | __FUNCTION__, this->trdid, this->process->pid, name, parent_cxy ); | 
|---|
| 1814 | #endif | 
|---|
| 1815 |  | 
|---|
| 1816 | // 2. create child inode TODO : define attr / mode / uid / gid | 
|---|
| 1817 | uint32_t attr = 0; | 
|---|
| 1818 | uint32_t mode = 0; | 
|---|
| 1819 | uint32_t uid  = 0; | 
|---|
| 1820 | uint32_t gid  = 0; | 
|---|
| 1821 |  | 
|---|
| 1822 | if( child_cxy == local_cxy )      // child cluster is the local cluster | 
|---|
| 1823 | { | 
|---|
| 1824 | error = vfs_inode_create( dentry_xp, | 
|---|
| 1825 | fs_type, | 
|---|
| 1826 | inode_type, | 
|---|
| 1827 | extend, | 
|---|
| 1828 | attr, | 
|---|
| 1829 | mode, | 
|---|
| 1830 | uid, | 
|---|
| 1831 | gid, | 
|---|
| 1832 | &inode_xp ); | 
|---|
| 1833 | } | 
|---|
| 1834 | else                              // child cluster is remote | 
|---|
| 1835 | { | 
|---|
| 1836 | rpc_vfs_inode_create_client( child_cxy, | 
|---|
| 1837 | dentry_xp, | 
|---|
| 1838 | fs_type, | 
|---|
| 1839 | inode_type, | 
|---|
| 1840 | extend, | 
|---|
| 1841 | attr, | 
|---|
| 1842 | mode, | 
|---|
| 1843 | uid, | 
|---|
| 1844 | gid, | 
|---|
| 1845 | &inode_xp, | 
|---|
| 1846 | &error ); | 
|---|
| 1847 | } | 
|---|
| 1848 |  | 
|---|
| 1849 | if( error ) | 
|---|
| 1850 | { | 
|---|
| 1851 | printk("\n[ERROR] in %s : cannot create inode in cluster %x\n", | 
|---|
| 1852 | __FUNCTION__ , child_cxy ); | 
|---|
| 1853 |  | 
|---|
| 1854 | vfs_dentry_t * dentry = GET_PTR( dentry_xp ); | 
|---|
| 1855 | if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry ); | 
|---|
| 1856 | else rpc_vfs_dentry_destroy_client( parent_cxy , dentry , &error ); | 
|---|
| 1857 | return ENOMEM; | 
|---|
| 1858 | } | 
|---|
| 1859 |  | 
|---|
| 1860 | #if(DEBUG_VFS_ADD_CHILD & 1) | 
|---|
| 1861 | if( DEBUG_VFS_ADD_CHILD < cycle ) | 
|---|
| 1862 | printk("\n[DBG] %s : thread %x in process %x / inode <%s> created in cluster %x\n", | 
|---|
| 1863 | __FUNCTION__ , this->trdid, this->process->pid, name , child_cxy ); | 
|---|
| 1864 | #endif | 
|---|
| 1865 |  | 
|---|
| 1866 | // 3. update extended pointer on inode in dentry | 
|---|
| 1867 | cxy_t          dentry_cxy = GET_CXY( dentry_xp ); | 
|---|
| 1868 | vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp ); | 
|---|
| 1869 | hal_remote_s64( XPTR( dentry_cxy , &dentry_ptr->child_xp ) , inode_xp ); | 
|---|
| 1870 |  | 
|---|
| 1871 | #if DEBUG_VFS_ADD_CHILD | 
|---|
| 1872 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1873 | if( DEBUG_VFS_ADD_CHILD < cycle ) | 
|---|
| 1874 | printk("\n[DBG] %s : thread %x in process %x exit for <%s> / cycle %d\n", | 
|---|
| 1875 | __FUNCTION__, this->trdid, this->process->pid, name, (uint32_t)hal_get_cycles() ); | 
|---|
| 1876 | #endif | 
|---|
| 1877 |  | 
|---|
| 1878 | // success : return extended pointer on child inode | 
|---|
| 1879 | *child_xp = inode_xp; | 
|---|
| 1880 | return 0; | 
|---|
| 1881 |  | 
|---|
| 1882 | // FIXME update the refcount fields in both inode and dentry | 
|---|
| 1883 |  | 
|---|
| 1884 | }  // end vfs_add_child_in_parent() | 
|---|
| 1885 |  | 
|---|
| 1886 | /////////////////////////////////////////////////////// | 
|---|
| 1887 | error_t vfs_remove_child_from_parent( xptr_t inode_xp ) | 
|---|
| 1888 | { | 
|---|
| 1889 | cxy_t          inode_cxy; | 
|---|
| 1890 | vfs_inode_t  * inode_ptr; | 
|---|
| 1891 | xptr_t         dentry_xp; | 
|---|
| 1892 | cxy_t          dentry_cxy; | 
|---|
| 1893 | vfs_dentry_t * dentry_ptr; | 
|---|
| 1894 | error_t        error; | 
|---|
| 1895 |  | 
|---|
| 1896 | // get inode cluster and local pointer | 
|---|
| 1897 | inode_cxy = GET_CXY( inode_xp ); | 
|---|
| 1898 | inode_ptr = GET_PTR( inode_xp ); | 
|---|
| 1899 |  | 
|---|
| 1900 | // get cluster and pointers of associated dentry | 
|---|
| 1901 | dentry_xp  = hal_remote_l64( XPTR( inode_cxy , &inode_ptr->parent_xp ) ); | 
|---|
| 1902 | dentry_cxy = GET_CXY( dentry_xp ); | 
|---|
| 1903 | dentry_ptr = GET_PTR( dentry_xp ); | 
|---|
| 1904 |  | 
|---|
| 1905 | // FIXME update the refcount fields in both inode and dentry | 
|---|
| 1906 |  | 
|---|
| 1907 | // delete dentry | 
|---|
| 1908 | if( dentry_cxy == local_cxy ) | 
|---|
| 1909 | { | 
|---|
| 1910 | error = vfs_dentry_destroy( dentry_ptr ); | 
|---|
| 1911 | } | 
|---|
| 1912 | else | 
|---|
| 1913 | { | 
|---|
| 1914 | rpc_vfs_dentry_destroy_client( dentry_cxy, | 
|---|
| 1915 | dentry_ptr, | 
|---|
| 1916 | &error ); | 
|---|
| 1917 | } | 
|---|
| 1918 | if( error ) return EINVAL; | 
|---|
| 1919 |  | 
|---|
| 1920 | // delete inode | 
|---|
| 1921 | if( inode_cxy == local_cxy ) | 
|---|
| 1922 | { | 
|---|
| 1923 | vfs_inode_destroy( inode_ptr ); | 
|---|
| 1924 | } | 
|---|
| 1925 | else | 
|---|
| 1926 | { | 
|---|
| 1927 | rpc_vfs_inode_destroy_client( inode_cxy, | 
|---|
| 1928 | inode_ptr, | 
|---|
| 1929 | &error ); | 
|---|
| 1930 | } | 
|---|
| 1931 | if( error ) return EINVAL; | 
|---|
| 1932 |  | 
|---|
| 1933 | return 0; | 
|---|
| 1934 |  | 
|---|
| 1935 | }  // end vfs_remove_child_from_parent() | 
|---|
| 1936 |  | 
|---|
| 1937 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1938 | //            Mapper related functions | 
|---|
| 1939 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1940 |  | 
|---|
| 1941 | //////////////////////////////////////////// | 
|---|
| 1942 | error_t vfs_mapper_move_page( page_t * page, | 
|---|
| 1943 | bool_t   to_mapper ) | 
|---|
| 1944 | { | 
|---|
| 1945 | error_t error = 0; | 
|---|
| 1946 |  | 
|---|
| 1947 | assert( (page != NULL) , "page pointer is NULL\n" ); | 
|---|
| 1948 |  | 
|---|
| 1949 | mapper_t    * mapper = page->mapper; | 
|---|
| 1950 |  | 
|---|
| 1951 | assert( (mapper != NULL) , "no mapper for page\n" ); | 
|---|
| 1952 |  | 
|---|
| 1953 | #if DEBUG_VFS_MAPPER_MOVE | 
|---|
| 1954 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1955 | if( DEBUG_VFS_MAPPER_MOVE < cycle ) | 
|---|
| 1956 | printk("\n[DBG] %s : thread %x enter for page %d / mapper %x / inode %x / cycle %d\n", | 
|---|
| 1957 | __FUNCTION__, CURRENT_THREAD, page->index, mapper, mapper->inode, cycle ); | 
|---|
| 1958 | #endif | 
|---|
| 1959 |  | 
|---|
| 1960 | // get FS type | 
|---|
| 1961 | vfs_fs_type_t fs_type = mapper->type; | 
|---|
| 1962 |  | 
|---|
| 1963 | // call relevant FS function | 
|---|
| 1964 | if( fs_type == FS_TYPE_FATFS ) | 
|---|
| 1965 | { | 
|---|
| 1966 | // enter mapper in write mode | 
|---|
| 1967 | rwlock_wr_acquire( &mapper->lock ); | 
|---|
| 1968 |  | 
|---|
| 1969 | // move page to mapper | 
|---|
| 1970 | error = fatfs_mapper_move_page( page , to_mapper ); | 
|---|
| 1971 |  | 
|---|
| 1972 | // exit mapper in write mode | 
|---|
| 1973 | rwlock_wr_release( &mapper->lock ); | 
|---|
| 1974 | } | 
|---|
| 1975 | else if( fs_type == FS_TYPE_RAMFS ) | 
|---|
| 1976 | { | 
|---|
| 1977 | assert( false , "should not be called for RAMFS\n" ); | 
|---|
| 1978 | } | 
|---|
| 1979 | else if( fs_type == FS_TYPE_DEVFS ) | 
|---|
| 1980 | { | 
|---|
| 1981 | assert( false , "should not be called for DEVFS\n" ); | 
|---|
| 1982 | } | 
|---|
| 1983 | else | 
|---|
| 1984 | { | 
|---|
| 1985 | assert( false , "undefined file system type\n" ); | 
|---|
| 1986 | } | 
|---|
| 1987 |  | 
|---|
| 1988 | #if DEBUG_VFS_MAPPER_MOVE | 
|---|
| 1989 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1990 | if( DEBUG_VFS_MAPPER_MOVE < cycle ) | 
|---|
| 1991 | printk("\n[DBG] %s : thread %x exit for page %d / mapper %x / inode %x / cycle %d\n", | 
|---|
| 1992 | __FUNCTION__, CURRENT_THREAD, page->index, mapper, mapper->inode, cycle ); | 
|---|
| 1993 | #endif | 
|---|
| 1994 |  | 
|---|
| 1995 | return error; | 
|---|
| 1996 |  | 
|---|
| 1997 | }  // end vfs_move_page() | 
|---|
| 1998 |  | 
|---|
| 1999 | ////////////////////////////////////////////////// | 
|---|
| 2000 | error_t vfs_mapper_load_all( vfs_inode_t * inode ) | 
|---|
| 2001 | { | 
|---|
| 2002 | assert( (inode != NULL) , "inode pointer is NULL\n" ); | 
|---|
| 2003 |  | 
|---|
| 2004 | uint32_t   index; | 
|---|
| 2005 | page_t   * page; | 
|---|
| 2006 |  | 
|---|
| 2007 | mapper_t * mapper = inode->mapper; | 
|---|
| 2008 | uint32_t   size   = inode->size; | 
|---|
| 2009 |  | 
|---|
| 2010 | assert( (mapper != NULL) , "mapper pointer is NULL\n" ); | 
|---|
| 2011 |  | 
|---|
| 2012 | #if DEBUG_VFS_MAPPER_LOAD | 
|---|
| 2013 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 2014 | if( DEBUG_VFS_MAPPER_MOVE < cycle ) | 
|---|
| 2015 | printk("\n[DBG] %s : thread %x enter for inode %x in cluster %x / cycle %d\n", | 
|---|
| 2016 | __FUNCTION__, CURRENT_THREAD, inode, local_cxy, cycle ); | 
|---|
| 2017 | #endif | 
|---|
| 2018 |  | 
|---|
| 2019 | // compute number of pages | 
|---|
| 2020 | uint32_t npages = size >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 2021 | if( (size & CONFIG_PPM_PAGE_MASK) || (size == 0) ) npages++; | 
|---|
| 2022 |  | 
|---|
| 2023 | // loop on pages | 
|---|
| 2024 | for( index = 0 ; index < npages ; index ++ ) | 
|---|
| 2025 | { | 
|---|
| 2026 | // this function allocates the missing page in mapper, | 
|---|
| 2027 | // and call the vfs_mapper_move_page() to load the page from device | 
|---|
| 2028 | page = mapper_get_page( mapper , index ); | 
|---|
| 2029 |  | 
|---|
| 2030 | if( page == NULL ) return EIO; | 
|---|
| 2031 | } | 
|---|
| 2032 |  | 
|---|
| 2033 | #if DEBUG_VFS_MAPPER_LOAD | 
|---|
| 2034 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 2035 | if( DEBUG_VFS_MAPPER_MOVE < cycle ) | 
|---|
| 2036 | printk("\n[DBG] %s : thread %x exit for inode %x in cluster %x / cycle %d\n", | 
|---|
| 2037 | __FUNCTION__, CURRENT_THREAD, inode, local_cxy, cycle ); | 
|---|
| 2038 | #endif | 
|---|
| 2039 |  | 
|---|
| 2040 | return 0; | 
|---|
| 2041 |  | 
|---|
| 2042 | }  // end vfs_mapper_load_all() | 
|---|
| 2043 |  | 
|---|