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