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