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