| 1 | /* |
|---|
| 2 | * vfs.c - Virtual File System implementation. |
|---|
| 3 | * |
|---|
| 4 | * Author Mohamed Lamine Karaoui (2015) |
|---|
| 5 | * Alain Greiner (2016,2017,2018) |
|---|
| 6 | * |
|---|
| 7 | * Copyright (c) UPMC Sorbonne Universites |
|---|
| 8 | * |
|---|
| 9 | * This file is part of ALMOS-MKH. |
|---|
| 10 | * |
|---|
| 11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
|---|
| 12 | * under the terms of the GNU General Public License as published by |
|---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
|---|
| 14 | * |
|---|
| 15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
|---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 18 | * General Public License for more details. |
|---|
| 19 | * |
|---|
| 20 | * You should have received a copy of the GNU General Public License |
|---|
| 21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
|---|
| 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | #include <kernel_config.h> |
|---|
| 27 | #include <hal_kernel_types.h> |
|---|
| 28 | #include <hal_atomic.h> |
|---|
| 29 | #include <hal_special.h> |
|---|
| 30 | #include <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> |
|---|
| 37 | #include <string.h> |
|---|
| 38 | #include <rpc.h> |
|---|
| 39 | #include <errno.h> |
|---|
| 40 | #include <kmem.h> |
|---|
| 41 | #include <mapper.h> |
|---|
| 42 | #include <thread.h> |
|---|
| 43 | #include <chdev.h> |
|---|
| 44 | #include <process.h> |
|---|
| 45 | #include <vfs.h> |
|---|
| 46 | #include <fatfs.h> |
|---|
| 47 | #include <ramfs.h> |
|---|
| 48 | #include <devfs.h> |
|---|
| 49 | #include <syscalls.h> |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 53 | // Extern variables |
|---|
| 54 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 55 | |
|---|
| 56 | extern vfs_ctx_t fs_context[FS_TYPES_NR]; // allocated in kernel_init.c |
|---|
| 57 | |
|---|
| 58 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
|---|
| 59 | |
|---|
| 60 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 61 | // Context related functions |
|---|
| 62 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 63 | |
|---|
| 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 | |
|---|
| 86 | //////////////////////////////////////////// |
|---|
| 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 |
|---|
| 94 | uint32_t lid = bitmap_ffc( ctx->bitmap , CONFIG_VFS_MAX_INODES ); |
|---|
| 95 | |
|---|
| 96 | if( lid == 0xFFFFFFFF ) // 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 |
|---|
| 107 | bitmap_set( ctx->bitmap , lid ); |
|---|
| 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 | { |
|---|
| 122 | bitmap_clear( ctx->bitmap , inum & 0xFFFF ); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 126 | // Inode related functions |
|---|
| 127 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 128 | |
|---|
| 129 | static const char * vfs_inode_type_str( vfs_inode_type_t type ) |
|---|
| 130 | { |
|---|
| 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 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 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, |
|---|
| 147 | void * extend, |
|---|
| 148 | uint32_t attr, |
|---|
| 149 | uint32_t rights, |
|---|
| 150 | uid_t uid, |
|---|
| 151 | gid_t gid, |
|---|
| 152 | xptr_t * inode_xp ) |
|---|
| 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 | |
|---|
| 161 | #if DEBUG_VFS_INODE_CREATE |
|---|
| 162 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 163 | if( DEBUG_VFS_INODE_CREATE < cycle ) |
|---|
| 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 |
|---|
| 167 | |
|---|
| 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]; |
|---|
| 172 | else |
|---|
| 173 | { |
|---|
| 174 | ctx = NULL; |
|---|
| 175 | assert( false , "illegal file system type = %d\n" , fs_type ); |
|---|
| 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 |
|---|
| 188 | mapper = mapper_create( fs_type ); |
|---|
| 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 | |
|---|
| 197 | // allocate memory for VFS inode descriptor |
|---|
| 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; |
|---|
| 213 | inode->type = inode_type; |
|---|
| 214 | inode->inum = inum; |
|---|
| 215 | inode->attr = attr; |
|---|
| 216 | inode->rights = rights; |
|---|
| 217 | inode->uid = uid; |
|---|
| 218 | inode->gid = gid; |
|---|
| 219 | inode->refcount = 0; |
|---|
| 220 | inode->parent_xp = dentry_xp; |
|---|
| 221 | inode->ctx = ctx; |
|---|
| 222 | inode->mapper = mapper; |
|---|
| 223 | inode->extend = extend; |
|---|
| 224 | |
|---|
| 225 | // initialise inode field in mapper |
|---|
| 226 | mapper->inode = inode; |
|---|
| 227 | |
|---|
| 228 | // initialise threads waiting queue |
|---|
| 229 | xlist_root_init( XPTR( local_cxy , &inode->wait_root ) ); |
|---|
| 230 | |
|---|
| 231 | // initialize dentries hash table |
|---|
| 232 | xhtab_init( &inode->children , XHTAB_DENTRY_TYPE ); |
|---|
| 233 | |
|---|
| 234 | // initialize inode locks |
|---|
| 235 | remote_rwlock_init( XPTR( local_cxy , &inode->data_lock ) ); |
|---|
| 236 | remote_spinlock_init( XPTR( local_cxy , &inode->main_lock ) ); |
|---|
| 237 | |
|---|
| 238 | #if DEBUG_VFS_INODE_CREATE |
|---|
| 239 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 240 | if( DEBUG_VFS_INODE_CREATE < cycle ) |
|---|
| 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 |
|---|
| 244 | |
|---|
| 245 | // return extended pointer on inode |
|---|
| 246 | *inode_xp = XPTR( local_cxy , inode ); |
|---|
| 247 | return 0; |
|---|
| 248 | |
|---|
| 249 | } // end vfs_inode_create() |
|---|
| 250 | |
|---|
| 251 | //////////////////////////////////////////////// |
|---|
| 252 | error_t vfs_inode_destroy( vfs_inode_t * inode ) |
|---|
| 253 | { |
|---|
| 254 | assert( (inode->refcount == 0) , "inode refcount non zero\n" ); |
|---|
| 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 | |
|---|
| 265 | return 0; |
|---|
| 266 | |
|---|
| 267 | } // end vfs_inode_destroy() |
|---|
| 268 | |
|---|
| 269 | ///////////////////////////////////////////// |
|---|
| 270 | error_t vfs_inode_load( vfs_inode_t * parent, |
|---|
| 271 | char * name, |
|---|
| 272 | xptr_t child_xp ) |
|---|
| 273 | { |
|---|
| 274 | |
|---|
| 275 | #if DEBUG_VFS_INODE_LOAD |
|---|
| 276 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 277 | if( DEBUG_VFS_INODE_LOAD < cycle ) |
|---|
| 278 | printk("\n[DBG] %s : thread %x enter for <%s> / cycle %d\n", |
|---|
| 279 | __FUNCTION__, CURRENT_THREAD , name , cycle ); |
|---|
| 280 | #endif |
|---|
| 281 | |
|---|
| 282 | error_t error = 0; |
|---|
| 283 | |
|---|
| 284 | assert( (parent != NULL) , "parent pointer is NULL\n"); |
|---|
| 285 | |
|---|
| 286 | assert( (child_xp != XPTR_NULL) , "child pointer is NULL\n"); |
|---|
| 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 | { |
|---|
| 298 | assert( false , "should not be called for RAMFS\n" ); |
|---|
| 299 | } |
|---|
| 300 | else if( fs_type == FS_TYPE_DEVFS ) |
|---|
| 301 | { |
|---|
| 302 | assert( false , "should not be called for DEVFS\n" ); |
|---|
| 303 | } |
|---|
| 304 | else |
|---|
| 305 | { |
|---|
| 306 | assert( false , "undefined file system type\n" ); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | #if DEBUG_VFS_INODE_LOAD |
|---|
| 310 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 311 | if( DEBUG_VFS_INODE_LOAD < cycle ) |
|---|
| 312 | printk("\n[DBG] %s : thread %x exit for <%s> / cycle %d\n", |
|---|
| 313 | __FUNCTION__, CURRENT_THREAD , name , cycle ); |
|---|
| 314 | #endif |
|---|
| 315 | |
|---|
| 316 | return error; |
|---|
| 317 | |
|---|
| 318 | } // end vfs_inode_load() |
|---|
| 319 | |
|---|
| 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 ); |
|---|
| 325 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); |
|---|
| 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 ); |
|---|
| 335 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); |
|---|
| 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 ); |
|---|
| 345 | vfs_inode_t * ptr = GET_PTR( inode_xp ); |
|---|
| 346 | |
|---|
| 347 | // get size |
|---|
| 348 | remote_rwlock_rd_lock( XPTR( cxy , &ptr->data_lock ) ); |
|---|
| 349 | uint32_t size = hal_remote_lw( XPTR( cxy , &ptr->size ) ); |
|---|
| 350 | remote_rwlock_rd_unlock( XPTR( cxy , &ptr->data_lock ) ); |
|---|
| 351 | return size; |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | //////////////////////////////////////////// |
|---|
| 355 | void vfs_inode_set_size( xptr_t inode_xp, |
|---|
| 356 | uint32_t size ) |
|---|
| 357 | { |
|---|
| 358 | // get inode cluster and local pointer |
|---|
| 359 | cxy_t cxy = GET_CXY( inode_xp ); |
|---|
| 360 | vfs_inode_t * ptr = GET_PTR( inode_xp ); |
|---|
| 361 | |
|---|
| 362 | // set size |
|---|
| 363 | remote_rwlock_wr_unlock( XPTR( cxy , &ptr->data_lock ) ); |
|---|
| 364 | hal_remote_sw( XPTR( cxy , &ptr->size ) , size ); |
|---|
| 365 | remote_rwlock_wr_unlock( XPTR( cxy , &ptr->data_lock ) ); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | //////////////////////////////////////// |
|---|
| 369 | void vfs_inode_unlock( xptr_t inode_xp ) |
|---|
| 370 | { |
|---|
| 371 | // get inode cluster and local pointer |
|---|
| 372 | cxy_t cxy = GET_CXY( inode_xp ); |
|---|
| 373 | vfs_inode_t * ptr = GET_PTR( inode_xp ); |
|---|
| 374 | |
|---|
| 375 | // release the main lock |
|---|
| 376 | remote_spinlock_unlock( XPTR( cxy , &ptr->main_lock ) ); |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | ////////////////////////////////////// |
|---|
| 380 | void vfs_inode_lock( xptr_t inode_xp ) |
|---|
| 381 | { |
|---|
| 382 | // get inode cluster and local pointer |
|---|
| 383 | cxy_t cxy = GET_CXY( inode_xp ); |
|---|
| 384 | vfs_inode_t * ptr = GET_PTR( inode_xp ); |
|---|
| 385 | |
|---|
| 386 | // get the main lock |
|---|
| 387 | remote_spinlock_lock( XPTR( cxy , &ptr->main_lock ) ); |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | ///////////////////////////////////////// |
|---|
| 391 | void vfs_inode_get_name( xptr_t inode_xp, |
|---|
| 392 | char * name ) |
|---|
| 393 | { |
|---|
| 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 ); |
|---|
| 402 | inode_ptr = GET_PTR( inode_xp ); |
|---|
| 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 ); |
|---|
| 415 | dentry_ptr = GET_PTR( dentry_xp ); |
|---|
| 416 | |
|---|
| 417 | hal_remote_strcpy( XPTR( local_cxy , name ) , |
|---|
| 418 | XPTR( dentry_cxy , &dentry_ptr->name ) ); |
|---|
| 419 | } |
|---|
| 420 | } // end vfs_inode_get_name() |
|---|
| 421 | |
|---|
| 422 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 423 | // Dentry related functions |
|---|
| 424 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 425 | |
|---|
| 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 ) |
|---|
| 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 |
|---|
| 435 | error_t error; |
|---|
| 436 | |
|---|
| 437 | #if DEBUG_VFS_DENTRY_CREATE |
|---|
| 438 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 439 | if( DEBUG_VFS_DENTRY_CREATE < cycle ) |
|---|
| 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 ); |
|---|
| 442 | #endif |
|---|
| 443 | |
|---|
| 444 | // get pointer on context |
|---|
| 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]; |
|---|
| 448 | else |
|---|
| 449 | { |
|---|
| 450 | ctx = NULL; |
|---|
| 451 | return EINVAL; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | // get name length |
|---|
| 455 | uint32_t length = strlen( name ); |
|---|
| 456 | |
|---|
| 457 | if( length >= CONFIG_VFS_MAX_NAME_LENGTH ) return EINVAL; |
|---|
| 458 | |
|---|
| 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 | |
|---|
| 465 | if( dentry == NULL ) return ENOMEM; |
|---|
| 466 | |
|---|
| 467 | // initialize dentry descriptor |
|---|
| 468 | |
|---|
| 469 | dentry->ctx = ctx; |
|---|
| 470 | dentry->length = length; |
|---|
| 471 | dentry->parent = parent; |
|---|
| 472 | strcpy( dentry->name , name ); |
|---|
| 473 | |
|---|
| 474 | #if( DEBUG_VFS_DENTRY_CREATE & 1 ) |
|---|
| 475 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 476 | if( DEBUG_VFS_DENTRY_CREATE < cycle ) |
|---|
| 477 | printk("\n[DBG] %s : dentry initialised\n", __FUNCTION__ ); |
|---|
| 478 | #endif |
|---|
| 479 | |
|---|
| 480 | // register dentry in hash table rooted in parent inode |
|---|
| 481 | error = xhtab_insert( XPTR( local_cxy , &parent->children ), |
|---|
| 482 | name, |
|---|
| 483 | XPTR( local_cxy , &dentry->list ) ); |
|---|
| 484 | |
|---|
| 485 | if( error ) return EINVAL; |
|---|
| 486 | |
|---|
| 487 | #if( DEBUG_VFS_DENTRY_CREATE & 1 ) |
|---|
| 488 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 489 | if( DEBUG_VFS_DENTRY_CREATE < cycle ) |
|---|
| 490 | printk("\n[DBG] %s : dentry registerd in htab\n", __FUNCTION__ ); |
|---|
| 491 | #endif |
|---|
| 492 | |
|---|
| 493 | // return extended pointer on dentry |
|---|
| 494 | *dentry_xp = XPTR( local_cxy , dentry ); |
|---|
| 495 | |
|---|
| 496 | #if DEBUG_VFS_DENTRY_CREATE |
|---|
| 497 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 498 | if( DEBUG_VFS_DENTRY_CREATE < cycle ) |
|---|
| 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 ); |
|---|
| 501 | #endif |
|---|
| 502 | |
|---|
| 503 | return 0; |
|---|
| 504 | |
|---|
| 505 | } // end vfs_dentry_create() |
|---|
| 506 | |
|---|
| 507 | /////////////////////////////////////////////////// |
|---|
| 508 | error_t vfs_dentry_destroy( vfs_dentry_t * dentry ) |
|---|
| 509 | { |
|---|
| 510 | error_t error; |
|---|
| 511 | |
|---|
| 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 |
|---|
| 525 | kmem_req_t req; |
|---|
| 526 | req.ptr = dentry; |
|---|
| 527 | req.type = KMEM_VFS_DENTRY; |
|---|
| 528 | kmem_free( &req ); |
|---|
| 529 | |
|---|
| 530 | return 0; |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 536 | // File descriptor related functions |
|---|
| 537 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 538 | |
|---|
| 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; |
|---|
| 560 | file->refcount = 1; |
|---|
| 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 ); |
|---|
| 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 | |
|---|
| 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 | { |
|---|
| 585 | assert( false , "refcount non zero\n" ); |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | kmem_req_t req; |
|---|
| 589 | req.ptr = file; |
|---|
| 590 | req.type = KMEM_VFS_FILE; |
|---|
| 591 | kmem_free( &req ); |
|---|
| 592 | |
|---|
| 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 | |
|---|
| 600 | } // end vfs_file_destroy() |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 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 ); |
|---|
| 608 | vfs_file_t * file_ptr = GET_PTR( file_xp ); |
|---|
| 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 ); |
|---|
| 619 | vfs_file_t * file_ptr = GET_PTR( file_xp ); |
|---|
| 620 | |
|---|
| 621 | // atomically decrement count |
|---|
| 622 | hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , -1 ); |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 626 | // File access related functions |
|---|
| 627 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 628 | |
|---|
| 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 ) |
|---|
| 636 | { |
|---|
| 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 |
|---|
| 645 | |
|---|
| 646 | assert( (mode == 0), __FUNCTION__, |
|---|
| 647 | "the mode parameter is not supported yet\n" ); |
|---|
| 648 | |
|---|
| 649 | #if DEBUG_VFS_OPEN |
|---|
| 650 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 651 | if( DEBUG_VFS_OPEN < cycle ) |
|---|
| 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 ); |
|---|
| 654 | #endif |
|---|
| 655 | |
|---|
| 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; |
|---|
| 664 | if( (flags & O_RDONLY ) == 0 ) file_attr |= FD_ATTR_WRITE_ENABLE; |
|---|
| 665 | if( (flags & O_WRONLY ) == 0 ) file_attr |= FD_ATTR_READ_ENABLE; |
|---|
| 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; |
|---|
| 669 | |
|---|
| 670 | // get extended pointer on target inode |
|---|
| 671 | error = vfs_lookup( process->vfs_cwd_xp , path , lookup_mode , &inode_xp ); |
|---|
| 672 | |
|---|
| 673 | if( error ) return error; |
|---|
| 674 | |
|---|
| 675 | // get target inode cluster and local pointer |
|---|
| 676 | inode_cxy = GET_CXY( inode_xp ); |
|---|
| 677 | inode_ptr = GET_PTR( inode_xp ); |
|---|
| 678 | |
|---|
| 679 | // create a new file descriptor in cluster containing inode |
|---|
| 680 | if( inode_cxy == local_cxy ) // target cluster is local |
|---|
| 681 | { |
|---|
| 682 | error = vfs_file_create( inode_ptr , file_attr , &file_xp ); |
|---|
| 683 | } |
|---|
| 684 | else // target cluster is remote |
|---|
| 685 | { |
|---|
| 686 | rpc_vfs_file_create_client( inode_cxy , inode_ptr , file_attr , &file_xp , &error ); |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | if( error ) return error; |
|---|
| 690 | |
|---|
| 691 | // allocate and register a new file descriptor index in reference process |
|---|
| 692 | error = process_fd_register( process , file_xp , &file_id ); |
|---|
| 693 | |
|---|
| 694 | if( error ) return error; |
|---|
| 695 | |
|---|
| 696 | #if DEBUG_VFS_OPEN |
|---|
| 697 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 698 | if( DEBUG_VFS_OPEN < cycle ) |
|---|
| 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 ); |
|---|
| 702 | #endif |
|---|
| 703 | |
|---|
| 704 | // success |
|---|
| 705 | *new_file_xp = file_xp; |
|---|
| 706 | *new_file_id = file_id; |
|---|
| 707 | return 0; |
|---|
| 708 | |
|---|
| 709 | } // end vfs_open() |
|---|
| 710 | |
|---|
| 711 | ////////////////////////////////////// |
|---|
| 712 | int vfs_user_move( bool_t to_buffer, |
|---|
| 713 | xptr_t file_xp, |
|---|
| 714 | void * buffer, |
|---|
| 715 | uint32_t size ) |
|---|
| 716 | { |
|---|
| 717 | assert( ( file_xp != XPTR_NULL ) , "file_xp == XPTR_NULL" ); |
|---|
| 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 ); |
|---|
| 728 | file_ptr = GET_PTR( file_xp ); |
|---|
| 729 | |
|---|
| 730 | // get inode type from remote file descriptor |
|---|
| 731 | inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) ); |
|---|
| 732 | |
|---|
| 733 | assert( (inode_type == INODE_TYPE_FILE) , |
|---|
| 734 | "inode type is not INODE_TYPE_FILE" ); |
|---|
| 735 | |
|---|
| 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 ) ); |
|---|
| 739 | |
|---|
| 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 ); |
|---|
| 748 | } |
|---|
| 749 | else |
|---|
| 750 | { |
|---|
| 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 | |
|---|
| 764 | } // end vfs_user_move() |
|---|
| 765 | |
|---|
| 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 | { |
|---|
| 772 | assert( ( file_xp != XPTR_NULL ) , "file_xp == XPTR_NULL" ); |
|---|
| 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 ); |
|---|
| 783 | file_ptr = GET_PTR( file_xp ); |
|---|
| 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 | |
|---|
| 826 | ////////////////////////////////////// |
|---|
| 827 | error_t vfs_lseek( xptr_t file_xp, |
|---|
| 828 | uint32_t offset, |
|---|
| 829 | uint32_t whence, |
|---|
| 830 | uint32_t * new_offset ) |
|---|
| 831 | { |
|---|
| 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 | |
|---|
| 839 | assert( (file_xp != XPTR_NULL) , "file_xp == XPTR_NULL" ); |
|---|
| 840 | |
|---|
| 841 | // get cluster and local pointer on remote file descriptor |
|---|
| 842 | file_cxy = GET_CXY( file_xp ); |
|---|
| 843 | file_ptr = GET_PTR( file_xp ); |
|---|
| 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 |
|---|
| 881 | if ( new_offset != NULL ) |
|---|
| 882 | *new_offset = new; |
|---|
| 883 | return 0; |
|---|
| 884 | |
|---|
| 885 | } // vfs_lseek() |
|---|
| 886 | |
|---|
| 887 | /////////////////////////////////// |
|---|
| 888 | error_t vfs_close( xptr_t file_xp, |
|---|
| 889 | uint32_t file_id ) |
|---|
| 890 | { |
|---|
| 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 | |
|---|
| 903 | assert( (file_xp != XPTR_NULL) , "file_xp == XPTR_NULL" ); |
|---|
| 904 | |
|---|
| 905 | assert( (file_id < CONFIG_PROCESS_FILE_MAX_NR) , "illegal file_id" ); |
|---|
| 906 | |
|---|
| 907 | thread_t * this = CURRENT_THREAD; |
|---|
| 908 | process_t * process = this->process; |
|---|
| 909 | |
|---|
| 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 |
|---|
| 916 | |
|---|
| 917 | // get local pointer on local cluster manager |
|---|
| 918 | cluster = LOCAL_CLUSTER; |
|---|
| 919 | |
|---|
| 920 | // get owner process cluster and lpid |
|---|
| 921 | owner_cxy = CXY_FROM_PID( process->pid ); |
|---|
| 922 | lpid = LPID_FROM_PID( process->pid ); |
|---|
| 923 | |
|---|
| 924 | // get extended pointers on copies root and lock |
|---|
| 925 | root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] ); |
|---|
| 926 | lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] ); |
|---|
| 927 | |
|---|
| 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 |
|---|
| 931 | remote_spinlock_lock( lock_xp ); |
|---|
| 932 | |
|---|
| 933 | XLIST_FOREACH( root_xp , iter_xp ) |
|---|
| 934 | { |
|---|
| 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 ); |
|---|
| 938 | |
|---|
| 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 |
|---|
| 944 | |
|---|
| 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 | |
|---|
| 952 | hal_remote_swd( entry_xp , XPTR_NULL ); |
|---|
| 953 | |
|---|
| 954 | // remote_rwlock_wr_unlock( fd_array_lock_xp ); |
|---|
| 955 | |
|---|
| 956 | vfs_file_count_down( file_xp ); |
|---|
| 957 | |
|---|
| 958 | hal_fence(); |
|---|
| 959 | } |
|---|
| 960 | |
|---|
| 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 | |
|---|
| 970 | // 2) release memory allocated to file descriptor in remote cluster |
|---|
| 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 | |
|---|
| 976 | if( file_cxy == local_cxy ) // file cluster is local |
|---|
| 977 | { |
|---|
| 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 | } |
|---|
| 984 | |
|---|
| 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 | |
|---|
| 992 | return 0; |
|---|
| 993 | |
|---|
| 994 | } // end vfs_close() |
|---|
| 995 | |
|---|
| 996 | //////////////////////////////////// |
|---|
| 997 | error_t vfs_unlink( xptr_t cwd_xp, |
|---|
| 998 | char * path ) |
|---|
| 999 | { |
|---|
| 1000 | assert( false , "not implemented cwd_xp %x, path <%s> \n", |
|---|
| 1001 | cwd_xp, path ); |
|---|
| 1002 | return 0; |
|---|
| 1003 | } |
|---|
| 1004 | |
|---|
| 1005 | //////////////////////////////////////// |
|---|
| 1006 | error_t vfs_stat( xptr_t file_xp, |
|---|
| 1007 | struct stat * k_stat ) |
|---|
| 1008 | { |
|---|
| 1009 | assert( false , "not implemented file_xp: %x, k_stat ptr %x\n", |
|---|
| 1010 | file_xp, k_stat ); |
|---|
| 1011 | return 0; |
|---|
| 1012 | } |
|---|
| 1013 | |
|---|
| 1014 | ///////////////////////////////////////////// |
|---|
| 1015 | error_t vfs_readdir( xptr_t file_xp, |
|---|
| 1016 | struct dirent * k_dirent ) |
|---|
| 1017 | { |
|---|
| 1018 | assert( false , "not implemented file_xp: %x, k_dirent ptr %x\n", |
|---|
| 1019 | file_xp, k_dirent ); |
|---|
| 1020 | return 0; |
|---|
| 1021 | } |
|---|
| 1022 | |
|---|
| 1023 | ////////////////////////////////////// |
|---|
| 1024 | error_t vfs_mkdir( xptr_t file_xp, |
|---|
| 1025 | char * path, |
|---|
| 1026 | uint32_t mode ) |
|---|
| 1027 | { |
|---|
| 1028 | assert( false , "not implemented file_xp: %x, path <%s>, mode: %x\n", |
|---|
| 1029 | file_xp, path, mode ); |
|---|
| 1030 | return 0; |
|---|
| 1031 | } |
|---|
| 1032 | |
|---|
| 1033 | //////////////////////////////////// |
|---|
| 1034 | error_t vfs_rmdir( xptr_t file_xp, |
|---|
| 1035 | char * path ) |
|---|
| 1036 | { |
|---|
| 1037 | assert( false , "not implemented file_xp: %x, path <%s>\n", |
|---|
| 1038 | file_xp, path ); |
|---|
| 1039 | return 0; |
|---|
| 1040 | } |
|---|
| 1041 | |
|---|
| 1042 | /////////////////////////////////// |
|---|
| 1043 | error_t vfs_chdir( xptr_t cwd_xp, |
|---|
| 1044 | char * path ) |
|---|
| 1045 | { |
|---|
| 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 ); |
|---|
| 1063 | inode_ptr = GET_PTR( inode_xp ); |
|---|
| 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 | |
|---|
| 1074 | assert( false , "not implemented\n" ); |
|---|
| 1075 | return 0; |
|---|
| 1076 | } |
|---|
| 1077 | |
|---|
| 1078 | /////////////////////////////////// |
|---|
| 1079 | error_t vfs_chmod( xptr_t cwd_xp, |
|---|
| 1080 | char * path, |
|---|
| 1081 | uint32_t rights ) |
|---|
| 1082 | { |
|---|
| 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 |
|---|
| 1090 | assert( (rights == 0), __FUNCTION__, |
|---|
| 1091 | "access rights non implemented yet\n" ); |
|---|
| 1092 | |
|---|
| 1093 | // get extended pointer on target inode |
|---|
| 1094 | error = vfs_lookup( cwd_xp , path , 0 , &inode_xp ); |
|---|
| 1095 | |
|---|
| 1096 | if( error ) return error; |
|---|
| 1097 | |
|---|
| 1098 | // get inode cluster and local pointer |
|---|
| 1099 | inode_cxy = GET_CXY( inode_xp ); |
|---|
| 1100 | inode_ptr = GET_PTR( inode_xp ); |
|---|
| 1101 | |
|---|
| 1102 | // get inode type from remote inode |
|---|
| 1103 | inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) ); |
|---|
| 1104 | |
|---|
| 1105 | |
|---|
| 1106 | assert( false , "not implemented\n" ); |
|---|
| 1107 | return 0; |
|---|
| 1108 | } |
|---|
| 1109 | |
|---|
| 1110 | /////////////////////////////////// |
|---|
| 1111 | error_t vfs_mkfifo( xptr_t cwd_xp, |
|---|
| 1112 | char * path, |
|---|
| 1113 | uint32_t rights ) |
|---|
| 1114 | { |
|---|
| 1115 | assert( false , "not implemented cwd_xp: %x, path <%s>, rights %x\n", |
|---|
| 1116 | cwd_xp, path, rights ); |
|---|
| 1117 | return 0; |
|---|
| 1118 | } |
|---|
| 1119 | |
|---|
| 1120 | |
|---|
| 1121 | |
|---|
| 1122 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1123 | // Inode Tree functions |
|---|
| 1124 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1125 | |
|---|
| 1126 | |
|---|
| 1127 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 1128 | // This static function is called by the vfs_display() function. |
|---|
| 1129 | // that is supposed to take the TXT0 lock. |
|---|
| 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; |
|---|
| 1138 | xptr_t children_xp; // extended pointer on children xhtab |
|---|
| 1139 | |
|---|
| 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; |
|---|
| 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 | |
|---|
| 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" ); |
|---|
| 1168 | |
|---|
| 1169 | // get inode cluster and local pointer |
|---|
| 1170 | inode_cxy = GET_CXY( inode_xp ); |
|---|
| 1171 | inode_ptr = GET_PTR( inode_xp ); |
|---|
| 1172 | |
|---|
| 1173 | // get inode type |
|---|
| 1174 | inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) ); |
|---|
| 1175 | |
|---|
| 1176 | // get local pointer on associated mapper |
|---|
| 1177 | mapper_t * mapper_ptr = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->mapper ) ); |
|---|
| 1178 | |
|---|
| 1179 | // make a local copy of node name |
|---|
| 1180 | hal_remote_strcpy( XPTR( local_cxy , name ) , name_xp ); |
|---|
| 1181 | |
|---|
| 1182 | // display inode |
|---|
| 1183 | nolock_printk("%s%s <%s> : inode = %x / mapper = %x / cluster %x\n", |
|---|
| 1184 | indent_str[indent], vfs_inode_type_str( inode_type ), name, |
|---|
| 1185 | inode_ptr , mapper_ptr , inode_cxy ); |
|---|
| 1186 | |
|---|
| 1187 | // scan directory entries |
|---|
| 1188 | if( inode_type == INODE_TYPE_DIR ) |
|---|
| 1189 | { |
|---|
| 1190 | // get extended pointer on directory entries xhtab |
|---|
| 1191 | children_xp = XPTR( inode_cxy , &inode_ptr->children ); |
|---|
| 1192 | |
|---|
| 1193 | // get xhtab lock |
|---|
| 1194 | xhtab_read_lock( children_xp ); |
|---|
| 1195 | |
|---|
| 1196 | // get first dentry from xhtab |
|---|
| 1197 | child_dentry_xp = xhtab_get_first( children_xp ); |
|---|
| 1198 | |
|---|
| 1199 | while( child_dentry_xp != XPTR_NULL ) |
|---|
| 1200 | { |
|---|
| 1201 | // get dentry cluster and local pointer |
|---|
| 1202 | child_dentry_cxy = GET_CXY( child_dentry_xp ); |
|---|
| 1203 | child_dentry_ptr = GET_PTR( child_dentry_xp ); |
|---|
| 1204 | |
|---|
| 1205 | // get extended pointer on child inode |
|---|
| 1206 | child_inode_xp = hal_remote_lwd( XPTR( child_dentry_cxy, |
|---|
| 1207 | &child_dentry_ptr->child_xp ) ); |
|---|
| 1208 | |
|---|
| 1209 | // get extended pointer on dentry name |
|---|
| 1210 | child_dentry_name_xp = XPTR( child_dentry_cxy , &child_dentry_ptr->name ); |
|---|
| 1211 | |
|---|
| 1212 | // recursive call on child inode |
|---|
| 1213 | vfs_recursive_display( child_inode_xp, |
|---|
| 1214 | child_dentry_name_xp, |
|---|
| 1215 | indent+1 ); |
|---|
| 1216 | |
|---|
| 1217 | // get next dentry |
|---|
| 1218 | child_dentry_xp = xhtab_get_next( children_xp ); |
|---|
| 1219 | } |
|---|
| 1220 | |
|---|
| 1221 | // release xhtab lock |
|---|
| 1222 | xhtab_read_unlock( children_xp ); |
|---|
| 1223 | } |
|---|
| 1224 | } // end vfs_recursive_display() |
|---|
| 1225 | |
|---|
| 1226 | /////////////////////////////////// |
|---|
| 1227 | void vfs_display( xptr_t inode_xp ) |
|---|
| 1228 | { |
|---|
| 1229 | xptr_t name_xp; |
|---|
| 1230 | xptr_t dentry_xp; |
|---|
| 1231 | cxy_t dentry_cxy; |
|---|
| 1232 | vfs_dentry_t * dentry_ptr; |
|---|
| 1233 | uint32_t save_sr; |
|---|
| 1234 | |
|---|
| 1235 | // get target inode cluster and local pointer |
|---|
| 1236 | cxy_t inode_cxy = GET_CXY( inode_xp ); |
|---|
| 1237 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); |
|---|
| 1238 | |
|---|
| 1239 | // get extended pointer on associated dentry |
|---|
| 1240 | dentry_xp = hal_remote_lwd( XPTR( inode_cxy , &inode_ptr->parent_xp ) ); |
|---|
| 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 ); |
|---|
| 1252 | dentry_ptr = GET_PTR( dentry_xp ); |
|---|
| 1253 | |
|---|
| 1254 | // get extended pointer on dentry name |
|---|
| 1255 | name_xp = XPTR( dentry_cxy , &dentry_ptr->name ); |
|---|
| 1256 | } |
|---|
| 1257 | |
|---|
| 1258 | // get pointers on TXT0 chdev |
|---|
| 1259 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
|---|
| 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 | |
|---|
| 1269 | // print header |
|---|
| 1270 | nolock_printk("\n***** file system state\n\n"); |
|---|
| 1271 | |
|---|
| 1272 | // call recursive function |
|---|
| 1273 | vfs_recursive_display( inode_xp , name_xp , 0 ); |
|---|
| 1274 | |
|---|
| 1275 | // release lock |
|---|
| 1276 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
|---|
| 1277 | |
|---|
| 1278 | } // end vfs_display() |
|---|
| 1279 | |
|---|
| 1280 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1281 | // This function is used by the vfs_lookup() function. |
|---|
| 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 ); |
|---|
| 1297 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); |
|---|
| 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. |
|---|
| 1311 | // It takes an extended pointer on a remote parent directory inode, a directory |
|---|
| 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 ); |
|---|
| 1329 | vfs_inode_t * parent_ptr = GET_PTR( parent_xp ); |
|---|
| 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 ); |
|---|
| 1341 | vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp ); |
|---|
| 1342 | |
|---|
| 1343 | // return child inode |
|---|
| 1344 | *child_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , &dentry_ptr->child_xp ) ); |
|---|
| 1345 | return true; |
|---|
| 1346 | |
|---|
| 1347 | } // end vfs_get_child() |
|---|
| 1348 | |
|---|
| 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 | |
|---|
| 1380 | // set NUL terminating character in name buffer |
|---|
| 1381 | *(name++) = 0; |
|---|
| 1382 | |
|---|
| 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; |
|---|
| 1395 | |
|---|
| 1396 | } // end vfs_get name_from_path() |
|---|
| 1397 | |
|---|
| 1398 | ////////////////////////////////////////////// |
|---|
| 1399 | error_t vfs_lookup( xptr_t cwd_xp, |
|---|
| 1400 | char * pathname, |
|---|
| 1401 | uint32_t mode, |
|---|
| 1402 | xptr_t * inode_xp ) |
|---|
| 1403 | { |
|---|
| 1404 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; // one name in path |
|---|
| 1405 | |
|---|
| 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 |
|---|
| 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 |
|---|
| 1421 | thread_t * this; // pointer on calling thread descriptor |
|---|
| 1422 | process_t * process; // pointer on calling process descriptor |
|---|
| 1423 | error_t error; |
|---|
| 1424 | |
|---|
| 1425 | this = CURRENT_THREAD; |
|---|
| 1426 | process = this->process; |
|---|
| 1427 | |
|---|
| 1428 | #if DEBUG_VFS_LOOKUP |
|---|
| 1429 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1430 | if( DEBUG_VFS_LOOKUP < cycle ) |
|---|
| 1431 | printk("\n[DBG] %s : thread %x in process %x enter for <%s> / cycle %d\n", |
|---|
| 1432 | __FUNCTION__, this->trdid, process->pid, pathname, cycle ); |
|---|
| 1433 | #endif |
|---|
| 1434 | |
|---|
| 1435 | // compute lookup flags |
|---|
| 1436 | dir = mode & VFS_LOOKUP_DIR; |
|---|
| 1437 | create = mode & VFS_LOOKUP_CREATE; |
|---|
| 1438 | excl = mode & VFS_LOOKUP_EXCL; |
|---|
| 1439 | |
|---|
| 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 | |
|---|
| 1444 | // initialise other loop variables |
|---|
| 1445 | current = pathname; |
|---|
| 1446 | next = NULL; |
|---|
| 1447 | last = false; |
|---|
| 1448 | child_xp = XPTR_NULL; |
|---|
| 1449 | |
|---|
| 1450 | // take lock on parent inode |
|---|
| 1451 | vfs_inode_lock( parent_xp ); |
|---|
| 1452 | |
|---|
| 1453 | // sequencially loop on nodes in pathname |
|---|
| 1454 | // load from device if one node in path not found in inode tree |
|---|
| 1455 | // exit loop when last name found (i.e. last == true) |
|---|
| 1456 | do |
|---|
| 1457 | { |
|---|
| 1458 | // get one name from path, and "last" flag |
|---|
| 1459 | vfs_get_name_from_path( current , name , &next , &last ); |
|---|
| 1460 | |
|---|
| 1461 | #if (DEBUG_VFS_LOOKUP & 1) |
|---|
| 1462 | if( DEBUG_VFS_LOOKUP < cycle ) |
|---|
| 1463 | printk("\n[DBG] %s : look for <%s> / last = %d\n", |
|---|
| 1464 | __FUNCTION__ , name , last ); |
|---|
| 1465 | #endif |
|---|
| 1466 | |
|---|
| 1467 | // search a child dentry matching name in parent inode |
|---|
| 1468 | found = vfs_get_child( parent_xp, |
|---|
| 1469 | name, |
|---|
| 1470 | &child_xp ); |
|---|
| 1471 | |
|---|
| 1472 | if (found == false ) // child not found in inode tree |
|---|
| 1473 | { |
|---|
| 1474 | |
|---|
| 1475 | #if (DEBUG_VFS_LOOKUP & 1) |
|---|
| 1476 | if( DEBUG_VFS_LOOKUP < cycle ) |
|---|
| 1477 | printk("\n[DBG] %s : miss <%s> node => try to create it\n", |
|---|
| 1478 | __FUNCTION__ , name ); |
|---|
| 1479 | #endif |
|---|
| 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 |
|---|
| 1490 | |
|---|
| 1491 | // release lock on parent inode |
|---|
| 1492 | vfs_inode_unlock( parent_xp ); |
|---|
| 1493 | |
|---|
| 1494 | // get parent inode FS type |
|---|
| 1495 | parent_cxy = GET_CXY( parent_xp ); |
|---|
| 1496 | parent_ptr = GET_PTR( parent_xp ); |
|---|
| 1497 | ctx_ptr = (vfs_ctx_t *)hal_remote_lpt( XPTR( parent_cxy,&parent_ptr->ctx ) ); |
|---|
| 1498 | fs_type = hal_remote_lw( XPTR( parent_cxy , &ctx_ptr->type ) ); |
|---|
| 1499 | |
|---|
| 1500 | // select a cluster for missing inode |
|---|
| 1501 | child_cxy = cluster_random_select(); |
|---|
| 1502 | |
|---|
| 1503 | // insert a new child dentry/inode in inode tree |
|---|
| 1504 | error = vfs_add_child_in_parent( child_cxy, |
|---|
| 1505 | 0, // type will be updated later |
|---|
| 1506 | fs_type, |
|---|
| 1507 | parent_xp, |
|---|
| 1508 | name, |
|---|
| 1509 | NULL, // fs_type_specific inode extend |
|---|
| 1510 | &child_xp ); |
|---|
| 1511 | if( error ) |
|---|
| 1512 | { |
|---|
| 1513 | printk("\n[ERROR] in %s : cannot create node %s for path <%s>\n", |
|---|
| 1514 | __FUNCTION__ , name, pathname ); |
|---|
| 1515 | return ENOMEM; |
|---|
| 1516 | } |
|---|
| 1517 | |
|---|
| 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 |
|---|
| 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 | |
|---|
| 1543 | if ( error ) // child not found in parent mapper |
|---|
| 1544 | { |
|---|
| 1545 | if( last && create && dir ) // new directory => update inode type |
|---|
| 1546 | { |
|---|
| 1547 | hal_remote_sw( XPTR( child_cxy, &child_ptr->type ), INODE_TYPE_DIR ); |
|---|
| 1548 | |
|---|
| 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 |
|---|
| 1554 | } |
|---|
| 1555 | else if ( last && create ) // new file => update inode type |
|---|
| 1556 | { |
|---|
| 1557 | hal_remote_sw( XPTR( child_cxy, &child_ptr->type ), INODE_TYPE_FILE ); |
|---|
| 1558 | |
|---|
| 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 |
|---|
| 1564 | } |
|---|
| 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 | } |
|---|
| 1572 | } |
|---|
| 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 | } |
|---|
| 1595 | |
|---|
| 1596 | #if (DEBUG_VFS_LOOKUP & 1) |
|---|
| 1597 | if( DEBUG_VFS_LOOKUP < cycle ) |
|---|
| 1598 | printk("\n[DBG] %s : load mapper from device for node <%s> in path %s\n", |
|---|
| 1599 | __FUNCTION__ , name, pathname ); |
|---|
| 1600 | #endif |
|---|
| 1601 | } |
|---|
| 1602 | } |
|---|
| 1603 | |
|---|
| 1604 | // take lock on parent inode |
|---|
| 1605 | vfs_inode_lock( parent_xp ); |
|---|
| 1606 | } |
|---|
| 1607 | else // child found in inode tree |
|---|
| 1608 | { |
|---|
| 1609 | |
|---|
| 1610 | #if (DEBUG_VFS_LOOKUP & 1) |
|---|
| 1611 | if( DEBUG_VFS_LOOKUP < cycle ) |
|---|
| 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 |
|---|
| 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 ); |
|---|
| 1619 | |
|---|
| 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 | |
|---|
| 1627 | // TODO check access rights here [AG] |
|---|
| 1628 | // error = vfs_access_denied( child_xp, |
|---|
| 1629 | // client_uid, |
|---|
| 1630 | // client_gid ); |
|---|
| 1631 | // if( error ) |
|---|
| 1632 | // { |
|---|
| 1633 | // printk("\n[ERROR] in %s : thread %x / permission denied for %s\n", |
|---|
| 1634 | // __FUNCTION__ , this , name ); |
|---|
| 1635 | // return EACCES; |
|---|
| 1636 | // } |
|---|
| 1637 | |
|---|
| 1638 | // take lock on child inode and release lock on parent |
|---|
| 1639 | vfs_inode_lock( child_xp ); |
|---|
| 1640 | vfs_inode_unlock( parent_xp ); |
|---|
| 1641 | |
|---|
| 1642 | // update loop variables |
|---|
| 1643 | parent_xp = child_xp; |
|---|
| 1644 | current = next; |
|---|
| 1645 | } |
|---|
| 1646 | while( last == false ); |
|---|
| 1647 | |
|---|
| 1648 | // release lock |
|---|
| 1649 | vfs_inode_unlock( parent_xp ); |
|---|
| 1650 | |
|---|
| 1651 | #if DEBUG_VFS_LOOKUP |
|---|
| 1652 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1653 | if( DEBUG_VFS_LOOKUP < cycle ) |
|---|
| 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 ); |
|---|
| 1658 | #endif |
|---|
| 1659 | |
|---|
| 1660 | // return searched pointer |
|---|
| 1661 | if( mode & VFS_LOOKUP_PARENT ) *inode_xp = parent_xp; |
|---|
| 1662 | else *inode_xp = child_xp; |
|---|
| 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 |
|---|
| 1678 | xptr_t inode_xp; // extended pointer on |
|---|
| 1679 | |
|---|
| 1680 | // implementation note: |
|---|
| 1681 | // we use two variables "index" and "count" because the buffer |
|---|
| 1682 | // is written in decreasing index order (from leaf to root) |
|---|
| 1683 | // TODO : handle conflict with a concurrent rename [AG] |
|---|
| 1684 | // FIXME : handle synchro in the loop [AG] |
|---|
| 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 ); |
|---|
| 1699 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); |
|---|
| 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 ); |
|---|
| 1706 | vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp ); |
|---|
| 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 | |
|---|
| 1737 | |
|---|
| 1738 | ////////////////////////////////////////////////////////////// |
|---|
| 1739 | error_t vfs_add_child_in_parent( cxy_t child_cxy, |
|---|
| 1740 | vfs_inode_type_t inode_type, |
|---|
| 1741 | vfs_fs_type_t fs_type, |
|---|
| 1742 | xptr_t parent_xp, |
|---|
| 1743 | char * name, |
|---|
| 1744 | void * extend, |
|---|
| 1745 | xptr_t * child_xp ) |
|---|
| 1746 | { |
|---|
| 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 |
|---|
| 1752 | |
|---|
| 1753 | // get parent inode cluster and local pointer |
|---|
| 1754 | parent_cxy = GET_CXY( parent_xp ); |
|---|
| 1755 | parent_ptr = GET_PTR( parent_xp ); |
|---|
| 1756 | |
|---|
| 1757 | #if DEBUG_VFS_ADD_CHILD |
|---|
| 1758 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1759 | if( DEBUG_VFS_ADD_CHILD < cycle ) |
|---|
| 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() ); |
|---|
| 1763 | #endif |
|---|
| 1764 | |
|---|
| 1765 | // 1. create dentry |
|---|
| 1766 | if( parent_cxy == local_cxy ) // parent cluster is the local cluster |
|---|
| 1767 | { |
|---|
| 1768 | error = vfs_dentry_create( fs_type, |
|---|
| 1769 | name, |
|---|
| 1770 | parent_ptr, |
|---|
| 1771 | &dentry_xp ); |
|---|
| 1772 | |
|---|
| 1773 | #if (DEBUG_VFS_ADD_CHILD & 1) |
|---|
| 1774 | if( (DEBUG_VFS_ADD_CHILD < cycle) && (error == 0) ) |
|---|
| 1775 | printk("\n[DBG] %s : dentry <%s> created in cluster %x\n", __FUNCTION__, name, local_cxy ); |
|---|
| 1776 | #endif |
|---|
| 1777 | |
|---|
| 1778 | } |
|---|
| 1779 | else // parent cluster is remote |
|---|
| 1780 | { |
|---|
| 1781 | rpc_vfs_dentry_create_client( parent_cxy, |
|---|
| 1782 | fs_type, |
|---|
| 1783 | name, |
|---|
| 1784 | parent_ptr, |
|---|
| 1785 | &dentry_xp, |
|---|
| 1786 | &error ); |
|---|
| 1787 | |
|---|
| 1788 | #if (DEBUG_VFS_ADD_CHILD & 1) |
|---|
| 1789 | if( (DEBUG_VFS_ADD_CHILD < cycle) && (error == 0) ) |
|---|
| 1790 | printk("\n[DBG] %s : dentry <%s> created in cluster %x\n", __FUNCTION__, name, parent_cxy ); |
|---|
| 1791 | #endif |
|---|
| 1792 | |
|---|
| 1793 | } |
|---|
| 1794 | |
|---|
| 1795 | if( error ) |
|---|
| 1796 | { |
|---|
| 1797 | printk("\n[ERROR] in %s : cannot create dentry <%s> in cluster %x\n", |
|---|
| 1798 | __FUNCTION__ , name , parent_cxy ); |
|---|
| 1799 | return ENOMEM; |
|---|
| 1800 | } |
|---|
| 1801 | |
|---|
| 1802 | // 2. create child inode TODO : define attr / mode / uid / gid |
|---|
| 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, |
|---|
| 1811 | fs_type, |
|---|
| 1812 | inode_type, |
|---|
| 1813 | extend, |
|---|
| 1814 | attr, |
|---|
| 1815 | mode, |
|---|
| 1816 | uid, |
|---|
| 1817 | gid, |
|---|
| 1818 | &inode_xp ); |
|---|
| 1819 | |
|---|
| 1820 | #if (DEBUG_VFS_ADD_CHILD & 1) |
|---|
| 1821 | if( DEBUG_VFS_ADD_CHILD < cycle ) |
|---|
| 1822 | printk("\n[DBG] %s : inode <%x> created in cluster %x\n", |
|---|
| 1823 | __FUNCTION__ , GET_PTR(inode_xp) , local_cxy ); |
|---|
| 1824 | #endif |
|---|
| 1825 | |
|---|
| 1826 | } |
|---|
| 1827 | else // child cluster is remote |
|---|
| 1828 | { |
|---|
| 1829 | rpc_vfs_inode_create_client( child_cxy, |
|---|
| 1830 | dentry_xp, |
|---|
| 1831 | fs_type, |
|---|
| 1832 | inode_type, |
|---|
| 1833 | extend, |
|---|
| 1834 | attr, |
|---|
| 1835 | mode, |
|---|
| 1836 | uid, |
|---|
| 1837 | gid, |
|---|
| 1838 | &inode_xp, |
|---|
| 1839 | &error ); |
|---|
| 1840 | |
|---|
| 1841 | #if (DEBUG_VFS_ADD_CHILD & 1) |
|---|
| 1842 | if( DEBUG_VFS_ADD_CHILD < cycle ) |
|---|
| 1843 | printk("\n[DBG] %s : inode <%s> created in cluster %x\n", |
|---|
| 1844 | __FUNCTION__ , GET_PTR(inode_xp) , child_cxy ); |
|---|
| 1845 | #endif |
|---|
| 1846 | |
|---|
| 1847 | } |
|---|
| 1848 | |
|---|
| 1849 | if( error ) |
|---|
| 1850 | { |
|---|
| 1851 | printk("\n[ERROR] in %s : cannot create inode in cluster %x\n", |
|---|
| 1852 | __FUNCTION__ , child_cxy ); |
|---|
| 1853 | |
|---|
| 1854 | vfs_dentry_t * dentry = GET_PTR( dentry_xp ); |
|---|
| 1855 | if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry ); |
|---|
| 1856 | else rpc_vfs_dentry_destroy_client( parent_cxy , dentry , &error ); |
|---|
| 1857 | return ENOMEM; |
|---|
| 1858 | } |
|---|
| 1859 | |
|---|
| 1860 | // 3. update extended pointer on inode in dentry |
|---|
| 1861 | cxy_t dentry_cxy = GET_CXY( dentry_xp ); |
|---|
| 1862 | vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp ); |
|---|
| 1863 | hal_remote_swd( XPTR( dentry_cxy , &dentry_ptr->child_xp ) , inode_xp ); |
|---|
| 1864 | |
|---|
| 1865 | #if DEBUG_VFS_ADD_CHILD |
|---|
| 1866 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1867 | if( DEBUG_VFS_ADD_CHILD < cycle ) |
|---|
| 1868 | printk("\n[DBG] %s : thread %x in process %x exit for <%s>\n", |
|---|
| 1869 | __FUNCTION__, CURRENT_THREAD, CURRENT_THREAD->process->pid, name ); |
|---|
| 1870 | #endif |
|---|
| 1871 | |
|---|
| 1872 | // success : return extended pointer on child inode |
|---|
| 1873 | *child_xp = inode_xp; |
|---|
| 1874 | return 0; |
|---|
| 1875 | |
|---|
| 1876 | // FIXME update the refcount fields in both inode and dentry |
|---|
| 1877 | |
|---|
| 1878 | } // end vfs_add_child_in_parent() |
|---|
| 1879 | |
|---|
| 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 | |
|---|
| 1931 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1932 | // Mapper related functions |
|---|
| 1933 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1934 | |
|---|
| 1935 | //////////////////////////////////////////// |
|---|
| 1936 | error_t vfs_mapper_move_page( page_t * page, |
|---|
| 1937 | bool_t to_mapper ) |
|---|
| 1938 | { |
|---|
| 1939 | error_t error = 0; |
|---|
| 1940 | |
|---|
| 1941 | assert( (page != NULL) , "page pointer is NULL\n" ); |
|---|
| 1942 | |
|---|
| 1943 | mapper_t * mapper = page->mapper; |
|---|
| 1944 | |
|---|
| 1945 | assert( (mapper != NULL) , "no mapper for page\n" ); |
|---|
| 1946 | |
|---|
| 1947 | #if DEBUG_VFS_MAPPER_MOVE |
|---|
| 1948 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1949 | if( DEBUG_VFS_MAPPER_MOVE < cycle ) |
|---|
| 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 |
|---|
| 1953 | |
|---|
| 1954 | // get FS type |
|---|
| 1955 | vfs_fs_type_t fs_type = mapper->type; |
|---|
| 1956 | |
|---|
| 1957 | // call relevant FS function |
|---|
| 1958 | if( fs_type == FS_TYPE_FATFS ) |
|---|
| 1959 | { |
|---|
| 1960 | rwlock_wr_lock( &mapper->lock ); |
|---|
| 1961 | error = fatfs_mapper_move_page( page , to_mapper ); |
|---|
| 1962 | rwlock_wr_unlock( &mapper->lock ); |
|---|
| 1963 | } |
|---|
| 1964 | else if( fs_type == FS_TYPE_RAMFS ) |
|---|
| 1965 | { |
|---|
| 1966 | assert( false , "should not be called for RAMFS\n" ); |
|---|
| 1967 | } |
|---|
| 1968 | else if( fs_type == FS_TYPE_DEVFS ) |
|---|
| 1969 | { |
|---|
| 1970 | assert( false , "should not be called for DEVFS\n" ); |
|---|
| 1971 | } |
|---|
| 1972 | else |
|---|
| 1973 | { |
|---|
| 1974 | assert( false , "undefined file system type\n" ); |
|---|
| 1975 | } |
|---|
| 1976 | |
|---|
| 1977 | #if DEBUG_VFS_MAPPER_MOVE |
|---|
| 1978 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 1979 | if( DEBUG_VFS_MAPPER_MOVE < cycle ) |
|---|
| 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 |
|---|
| 1983 | |
|---|
| 1984 | return error; |
|---|
| 1985 | |
|---|
| 1986 | } // end vfs_move_page() |
|---|
| 1987 | |
|---|
| 1988 | ////////////////////////////////////////////////// |
|---|
| 1989 | error_t vfs_mapper_load_all( vfs_inode_t * inode ) |
|---|
| 1990 | { |
|---|
| 1991 | assert( (inode != NULL) , "inode pointer is NULL\n" ); |
|---|
| 1992 | |
|---|
| 1993 | uint32_t index; |
|---|
| 1994 | page_t * page; |
|---|
| 1995 | |
|---|
| 1996 | mapper_t * mapper = inode->mapper; |
|---|
| 1997 | uint32_t size = inode->size; |
|---|
| 1998 | |
|---|
| 1999 | assert( (mapper != NULL) , "mapper pointer is NULL\n" ); |
|---|
| 2000 | |
|---|
| 2001 | #if DEBUG_VFS_MAPPER_LOAD |
|---|
| 2002 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 2003 | if( DEBUG_VFS_MAPPER_MOVE < cycle ) |
|---|
| 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 |
|---|
| 2007 | |
|---|
| 2008 | // compute number of pages |
|---|
| 2009 | uint32_t npages = size >> CONFIG_PPM_PAGE_SHIFT; |
|---|
| 2010 | if( (size & CONFIG_PPM_PAGE_MASK) || (size == 0) ) npages++; |
|---|
| 2011 | |
|---|
| 2012 | // loop on pages |
|---|
| 2013 | for( index = 0 ; index < npages ; index ++ ) |
|---|
| 2014 | { |
|---|
| 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 ); |
|---|
| 2018 | |
|---|
| 2019 | if( page == NULL ) return EIO; |
|---|
| 2020 | } |
|---|
| 2021 | |
|---|
| 2022 | #if DEBUG_VFS_MAPPER_LOAD |
|---|
| 2023 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 2024 | if( DEBUG_VFS_MAPPER_MOVE < cycle ) |
|---|
| 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 |
|---|
| 2028 | |
|---|
| 2029 | return 0; |
|---|
| 2030 | |
|---|
| 2031 | } // end vfs_mapper_load_all() |
|---|
| 2032 | |
|---|