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