[1] | 1 | /* |
---|
| 2 | * vfs.c - Virtual File System implementation. |
---|
| 3 | * |
---|
| 4 | * Author Mohamed Lamine Karaoui (2015) |
---|
[623] | 5 | * Alain Greiner (2016,2017,2018,2019) |
---|
[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 | |
---|
[14] | 25 | #include <kernel_config.h> |
---|
[457] | 26 | #include <hal_kernel_types.h> |
---|
[1] | 27 | #include <hal_atomic.h> |
---|
| 28 | #include <hal_special.h> |
---|
| 29 | #include <printk.h> |
---|
| 30 | #include <list.h> |
---|
| 31 | #include <xlist.h> |
---|
| 32 | #include <slist.h> |
---|
| 33 | #include <xhtab.h> |
---|
[430] | 34 | #include <string.h> |
---|
[23] | 35 | #include <rpc.h> |
---|
[1] | 36 | #include <errno.h> |
---|
| 37 | #include <kmem.h> |
---|
| 38 | #include <mapper.h> |
---|
| 39 | #include <thread.h> |
---|
[337] | 40 | #include <chdev.h> |
---|
[1] | 41 | #include <process.h> |
---|
[568] | 42 | #include <cluster.h> |
---|
[23] | 43 | #include <vfs.h> |
---|
[1] | 44 | #include <fatfs.h> |
---|
| 45 | #include <ramfs.h> |
---|
[23] | 46 | #include <devfs.h> |
---|
| 47 | #include <syscalls.h> |
---|
[1] | 48 | |
---|
| 49 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[50] | 50 | // Extern variables |
---|
[1] | 51 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 52 | |
---|
[337] | 53 | extern vfs_ctx_t fs_context[FS_TYPES_NR]; // allocated in kernel_init.c |
---|
| 54 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
[568] | 55 | extern char * lock_type_str[]; // allocated in kernel_init.c |
---|
[50] | 56 | |
---|
[602] | 57 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 58 | // VFS Context related functions |
---|
[1] | 59 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 60 | |
---|
[188] | 61 | //////////////////////////////////////// |
---|
| 62 | void vfs_ctx_init( vfs_fs_type_t type, |
---|
| 63 | uint32_t attr, |
---|
| 64 | uint32_t total_clusters, |
---|
| 65 | uint32_t cluster_size, |
---|
| 66 | xptr_t vfs_root_xp, |
---|
| 67 | void * extend ) |
---|
| 68 | { |
---|
| 69 | vfs_ctx_t * vfs_ctx = &fs_context[type]; |
---|
| 70 | |
---|
| 71 | vfs_ctx->type = type; |
---|
| 72 | vfs_ctx->attr = attr; |
---|
| 73 | vfs_ctx->total_clusters = total_clusters; |
---|
| 74 | vfs_ctx->cluster_size = cluster_size; |
---|
| 75 | vfs_ctx->vfs_root_xp = vfs_root_xp; |
---|
| 76 | vfs_ctx->extend = extend; |
---|
| 77 | |
---|
[568] | 78 | busylock_init( &vfs_ctx->lock , LOCK_VFS_CTX ); |
---|
[188] | 79 | |
---|
| 80 | bitmap_init( vfs_ctx->bitmap , BITMAP_SIZE(CONFIG_VFS_MAX_INODES) ); |
---|
| 81 | } |
---|
| 82 | |
---|
[23] | 83 | //////////////////////////////////////////// |
---|
[1] | 84 | error_t vfs_ctx_inum_alloc( vfs_ctx_t * ctx, |
---|
| 85 | uint32_t * inum ) |
---|
| 86 | { |
---|
| 87 | // get lock on inum allocator |
---|
[568] | 88 | busylock_acquire( &ctx->lock ); |
---|
[1] | 89 | |
---|
| 90 | // get lid from local inum allocator |
---|
[23] | 91 | uint32_t lid = bitmap_ffc( ctx->bitmap , CONFIG_VFS_MAX_INODES ); |
---|
[1] | 92 | |
---|
[473] | 93 | if( lid == 0xFFFFFFFF ) // no more free slot => error |
---|
[1] | 94 | { |
---|
| 95 | // release lock |
---|
[568] | 96 | busylock_release( &ctx->lock ); |
---|
[1] | 97 | |
---|
| 98 | // return error |
---|
| 99 | return 1; |
---|
| 100 | } |
---|
| 101 | else // found => return inum |
---|
| 102 | { |
---|
| 103 | // set slot allocated |
---|
[23] | 104 | bitmap_set( ctx->bitmap , lid ); |
---|
[1] | 105 | |
---|
| 106 | // release lock |
---|
[568] | 107 | busylock_release( &ctx->lock ); |
---|
[1] | 108 | |
---|
| 109 | // return inum |
---|
| 110 | *inum = (((uint32_t)local_cxy) << 16) | (lid & 0xFFFF); |
---|
| 111 | return 0; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | //////////////////////////////////////////// |
---|
| 116 | void vfs_ctx_inum_release( vfs_ctx_t * ctx, |
---|
| 117 | uint32_t inum ) |
---|
| 118 | { |
---|
[23] | 119 | bitmap_clear( ctx->bitmap , inum & 0xFFFF ); |
---|
[1] | 120 | } |
---|
| 121 | |
---|
| 122 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 123 | // VFS inode descriptor related functions |
---|
[1] | 124 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 125 | |
---|
[598] | 126 | const char * vfs_inode_type_str( vfs_inode_type_t type ) |
---|
[188] | 127 | { |
---|
[598] | 128 | switch ( type ) |
---|
| 129 | { |
---|
| 130 | case INODE_TYPE_FILE: return "FILE"; |
---|
| 131 | case INODE_TYPE_DIR: return "DIR "; |
---|
| 132 | case INODE_TYPE_FIFO: return "FIFO"; |
---|
| 133 | case INODE_TYPE_PIPE: return "PIPE"; |
---|
| 134 | case INODE_TYPE_SOCK: return "SOCK"; |
---|
| 135 | case INODE_TYPE_DEV: return "DEV "; |
---|
[611] | 136 | case INODE_TYPE_BLK: return "BLK "; |
---|
[598] | 137 | case INODE_TYPE_SYML: return "SYML"; |
---|
| 138 | default: return "undefined"; |
---|
[527] | 139 | } |
---|
[188] | 140 | } |
---|
| 141 | |
---|
[610] | 142 | //////////////////////////////////////////////////// |
---|
| 143 | error_t vfs_inode_create( vfs_fs_type_t fs_type, |
---|
[23] | 144 | uint32_t attr, |
---|
| 145 | uint32_t rights, |
---|
| 146 | uid_t uid, |
---|
| 147 | gid_t gid, |
---|
| 148 | xptr_t * inode_xp ) |
---|
[1] | 149 | { |
---|
| 150 | mapper_t * mapper; // associated mapper( to be allocated) |
---|
| 151 | vfs_inode_t * inode; // inode descriptor (to be allocated) |
---|
| 152 | uint32_t inum; // inode identifier (to be allocated) |
---|
| 153 | vfs_ctx_t * ctx; // file system context |
---|
| 154 | kmem_req_t req; // request to kernel memory allocator |
---|
| 155 | error_t error; |
---|
| 156 | |
---|
[438] | 157 | #if DEBUG_VFS_INODE_CREATE |
---|
[568] | 158 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 159 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 160 | cxy_t dentry_cxy = GET_CXY( dentry_xp ); |
---|
| 161 | vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp ); |
---|
| 162 | thread_t * this = CURRENT_THREAD; |
---|
| 163 | if( dentry_xp != XPTR_NULL ) hal_remote_strcpy( XPTR( local_cxy , name ), |
---|
| 164 | XPTR( dentry_cxy , dentry_ptr->name ) ); |
---|
| 165 | else strcpy( name , "/" ); |
---|
[438] | 166 | if( DEBUG_VFS_INODE_CREATE < cycle ) |
---|
[598] | 167 | printk("\n[%s] thread[%x,%x] enter for <%s> / cycle %d\n", |
---|
| 168 | __FUNCTION__, this->process->pid, this->trdid, name, cycle ); |
---|
[433] | 169 | #endif |
---|
[279] | 170 | |
---|
[23] | 171 | // check fs type and get pointer on context |
---|
| 172 | if ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS]; |
---|
| 173 | else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS]; |
---|
| 174 | else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS]; |
---|
[1] | 175 | else |
---|
| 176 | { |
---|
[625] | 177 | printk("\n[ERROR] in %s : illegal FS type\n", __FUNCTION__ ); |
---|
| 178 | return -1; |
---|
[1] | 179 | } |
---|
| 180 | |
---|
| 181 | // allocate inum |
---|
| 182 | error = vfs_ctx_inum_alloc( ctx , &inum ); |
---|
| 183 | |
---|
| 184 | if( error ) |
---|
| 185 | { |
---|
| 186 | printk("\n[ERROR] in %s : cannot allocate inum\n", __FUNCTION__ ); |
---|
[625] | 187 | return -1; |
---|
[1] | 188 | } |
---|
| 189 | |
---|
| 190 | // allocate memory for mapper |
---|
[246] | 191 | mapper = mapper_create( fs_type ); |
---|
[1] | 192 | |
---|
| 193 | if( mapper == NULL ) |
---|
| 194 | { |
---|
| 195 | printk("\n[ERROR] in %s : cannot allocate mapper\n", __FUNCTION__ ); |
---|
| 196 | vfs_ctx_inum_release( ctx , inum ); |
---|
| 197 | return ENOMEM; |
---|
| 198 | } |
---|
| 199 | |
---|
[23] | 200 | // allocate memory for VFS inode descriptor |
---|
[1] | 201 | req.type = KMEM_VFS_INODE; |
---|
| 202 | req.size = sizeof(vfs_inode_t); |
---|
| 203 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 204 | inode = (vfs_inode_t *)kmem_alloc( &req ); |
---|
| 205 | |
---|
| 206 | if( inode == NULL ) |
---|
| 207 | { |
---|
| 208 | printk("\n[ERROR] in %s : cannot allocate inode descriptor\n", __FUNCTION__ ); |
---|
| 209 | vfs_ctx_inum_release( ctx , inum ); |
---|
| 210 | mapper_destroy( mapper ); |
---|
[610] | 211 | return -1; |
---|
[1] | 212 | } |
---|
| 213 | |
---|
| 214 | // initialize inode descriptor |
---|
[623] | 215 | inode->type = INODE_TYPE_FILE; // default value |
---|
[1] | 216 | inode->inum = inum; |
---|
| 217 | inode->attr = attr; |
---|
[23] | 218 | inode->rights = rights; |
---|
[1] | 219 | inode->uid = uid; |
---|
| 220 | inode->gid = gid; |
---|
| 221 | inode->ctx = ctx; |
---|
[246] | 222 | inode->mapper = mapper; |
---|
[602] | 223 | inode->extend = NULL; |
---|
[610] | 224 | inode->links = 0; |
---|
[1] | 225 | |
---|
[246] | 226 | // initialise inode field in mapper |
---|
| 227 | mapper->inode = inode; |
---|
| 228 | |
---|
[610] | 229 | // initialize chidren dentries xhtab |
---|
[204] | 230 | xhtab_init( &inode->children , XHTAB_DENTRY_TYPE ); |
---|
[1] | 231 | |
---|
[610] | 232 | // initialize parents dentries xlist |
---|
| 233 | xlist_root_init( XPTR( local_cxy , &inode->parents ) ); |
---|
| 234 | |
---|
| 235 | // initialize lock protecting size |
---|
| 236 | remote_rwlock_init( XPTR( local_cxy , &inode->size_lock ), LOCK_VFS_SIZE ); |
---|
[1] | 237 | |
---|
[610] | 238 | // initialise lock protecting inode tree traversal |
---|
| 239 | remote_rwlock_init( XPTR( local_cxy , &inode->main_lock ), LOCK_VFS_MAIN ); |
---|
[568] | 240 | |
---|
[610] | 241 | // return extended pointer on inode |
---|
| 242 | *inode_xp = XPTR( local_cxy , inode ); |
---|
| 243 | |
---|
[438] | 244 | #if DEBUG_VFS_INODE_CREATE |
---|
[568] | 245 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 246 | if( DEBUG_VFS_INODE_CREATE < cycle ) |
---|
[598] | 247 | printk("\n[%s] thread[%x,%x] exit for <%s> / inode [%x,%x] / cycle %d\n", |
---|
| 248 | __FUNCTION__, this->process->pid, this->trdid, name, local_cxy, inode, cycle ); |
---|
[433] | 249 | #endif |
---|
[401] | 250 | |
---|
[1] | 251 | return 0; |
---|
| 252 | |
---|
| 253 | } // end vfs_inode_create() |
---|
| 254 | |
---|
[602] | 255 | ///////////////////////////////////////////// |
---|
| 256 | void vfs_inode_destroy( vfs_inode_t * inode ) |
---|
[1] | 257 | { |
---|
| 258 | // release memory allocated for mapper |
---|
| 259 | mapper_destroy( inode->mapper ); |
---|
| 260 | |
---|
| 261 | // release memory allocate for inode descriptor |
---|
| 262 | kmem_req_t req; |
---|
| 263 | req.ptr = inode; |
---|
| 264 | req.type = KMEM_VFS_INODE; |
---|
| 265 | kmem_free( &req ); |
---|
| 266 | |
---|
| 267 | } // end vfs_inode_destroy() |
---|
| 268 | |
---|
| 269 | ////////////////////////////////////////////// |
---|
| 270 | uint32_t vfs_inode_get_size( xptr_t inode_xp ) |
---|
| 271 | { |
---|
| 272 | // get inode cluster and local pointer |
---|
| 273 | cxy_t cxy = GET_CXY( inode_xp ); |
---|
[473] | 274 | vfs_inode_t * ptr = GET_PTR( inode_xp ); |
---|
[1] | 275 | |
---|
[623] | 276 | // build extended pointers on lock & size |
---|
| 277 | xptr_t lock_xp = XPTR( cxy , &ptr->size_lock ); |
---|
| 278 | xptr_t size_xp = XPTR( cxy , &ptr->size ); |
---|
| 279 | |
---|
| 280 | // take lock in read mode |
---|
| 281 | remote_rwlock_rd_acquire( lock_xp ); |
---|
| 282 | |
---|
[1] | 283 | // get size |
---|
[623] | 284 | uint32_t size = hal_remote_l32( size_xp ); |
---|
| 285 | |
---|
| 286 | // release lock from read mode |
---|
| 287 | remote_rwlock_rd_release( lock_xp ); |
---|
| 288 | |
---|
[1] | 289 | return size; |
---|
| 290 | } |
---|
| 291 | |
---|
[623] | 292 | /////////////////////////////////////////////// |
---|
| 293 | void vfs_inode_update_size( xptr_t inode_xp, |
---|
| 294 | uint32_t size ) |
---|
[1] | 295 | { |
---|
| 296 | // get inode cluster and local pointer |
---|
| 297 | cxy_t cxy = GET_CXY( inode_xp ); |
---|
[473] | 298 | vfs_inode_t * ptr = GET_PTR( inode_xp ); |
---|
[1] | 299 | |
---|
[623] | 300 | // build extended pointers on lock & size |
---|
| 301 | xptr_t lock_xp = XPTR( cxy , &ptr->size_lock ); |
---|
| 302 | xptr_t size_xp = XPTR( cxy , &ptr->size ); |
---|
| 303 | |
---|
| 304 | // take lock in write mode |
---|
| 305 | remote_rwlock_wr_acquire( lock_xp ); |
---|
| 306 | |
---|
| 307 | // get current size |
---|
| 308 | uint32_t current_size = hal_remote_l32( size_xp ); |
---|
| 309 | |
---|
| 310 | // set size if required |
---|
| 311 | if( current_size < size ) hal_remote_s32( size_xp , size ); |
---|
| 312 | |
---|
| 313 | // release lock from write mode |
---|
| 314 | remote_rwlock_wr_release( lock_xp ); |
---|
[1] | 315 | } |
---|
| 316 | |
---|
[101] | 317 | //////////////////////////////////////// |
---|
| 318 | void vfs_inode_unlock( xptr_t inode_xp ) |
---|
[1] | 319 | { |
---|
| 320 | // get inode cluster and local pointer |
---|
| 321 | cxy_t cxy = GET_CXY( inode_xp ); |
---|
[473] | 322 | vfs_inode_t * ptr = GET_PTR( inode_xp ); |
---|
[1] | 323 | |
---|
| 324 | // release the main lock |
---|
[568] | 325 | remote_busylock_release( XPTR( cxy , &ptr->main_lock ) ); |
---|
[1] | 326 | } |
---|
| 327 | |
---|
[101] | 328 | ////////////////////////////////////// |
---|
| 329 | void vfs_inode_lock( xptr_t inode_xp ) |
---|
[1] | 330 | { |
---|
| 331 | // get inode cluster and local pointer |
---|
| 332 | cxy_t cxy = GET_CXY( inode_xp ); |
---|
[473] | 333 | vfs_inode_t * ptr = GET_PTR( inode_xp ); |
---|
[1] | 334 | |
---|
| 335 | // get the main lock |
---|
[568] | 336 | remote_busylock_acquire( XPTR( cxy , &ptr->main_lock ) ); |
---|
[1] | 337 | } |
---|
| 338 | |
---|
[610] | 339 | /////////////////////////////////////////// |
---|
| 340 | void vfs_inode_get_name( xptr_t inode_xp, |
---|
| 341 | char * name ) |
---|
[101] | 342 | { |
---|
[610] | 343 | cxy_t inode_cxy; // inode cluster identifier |
---|
| 344 | vfs_inode_t * inode_ptr; // local pointer on inode |
---|
| 345 | xptr_t parents_root_xp; // extended pointer on inode parents root |
---|
[204] | 346 | |
---|
| 347 | // get inode cluster and local pointer |
---|
| 348 | inode_cxy = GET_CXY( inode_xp ); |
---|
[473] | 349 | inode_ptr = GET_PTR( inode_xp ); |
---|
[204] | 350 | |
---|
[610] | 351 | // build extended pointer on parents dentries root |
---|
| 352 | parents_root_xp = XPTR( inode_cxy , &inode_ptr->parents ); |
---|
[204] | 353 | |
---|
[610] | 354 | // check VFS root |
---|
| 355 | if( xlist_is_empty( parents_root_xp ) ) // inode is the VFS root |
---|
[204] | 356 | { |
---|
| 357 | strcpy( name , "/" ); |
---|
| 358 | } |
---|
[610] | 359 | else // not the VFS root |
---|
[204] | 360 | { |
---|
[610] | 361 | xptr_t dentry_xp; |
---|
| 362 | cxy_t dentry_cxy; |
---|
| 363 | vfs_dentry_t * dentry_ptr; |
---|
| 364 | |
---|
| 365 | // get first name in list of parents |
---|
| 366 | dentry_xp = XLIST_FIRST( parents_root_xp , vfs_dentry_t , parents ); |
---|
[204] | 367 | dentry_cxy = GET_CXY( dentry_xp ); |
---|
[473] | 368 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
[204] | 369 | |
---|
| 370 | hal_remote_strcpy( XPTR( local_cxy , name ) , |
---|
[610] | 371 | XPTR( dentry_cxy , dentry_ptr->name ) ); |
---|
[204] | 372 | } |
---|
[610] | 373 | |
---|
[409] | 374 | } // end vfs_inode_get_name() |
---|
[204] | 375 | |
---|
[602] | 376 | /////////////////////////////////////////////////////// |
---|
| 377 | error_t vfs_inode_load_all_pages( vfs_inode_t * inode ) |
---|
| 378 | { |
---|
| 379 | |
---|
[625] | 380 | assert( (inode != NULL) , "inode pointer is NULL" ); |
---|
[602] | 381 | |
---|
| 382 | uint32_t page_id; |
---|
| 383 | xptr_t page_xp; |
---|
| 384 | |
---|
| 385 | mapper_t * mapper = inode->mapper; |
---|
| 386 | uint32_t size = inode->size; |
---|
| 387 | |
---|
[625] | 388 | assert( (mapper != NULL) , "mapper pointer is NULL" ); |
---|
[602] | 389 | |
---|
| 390 | #if DEBUG_VFS_INODE_LOAD_ALL |
---|
| 391 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 392 | thread_t * this = CURRENT_THREAD; |
---|
| 393 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 394 | vfs_inode_get_name( XPTR( local_cxy , inode ) , name ); |
---|
| 395 | if( DEBUG_VFS_INODE_LOAD_ALL < cycle ) |
---|
| 396 | printk("\n[%s] thread[%x,%x] enter for <%s> in cluster %x / cycle %d\n", |
---|
| 397 | __FUNCTION__, this->process->pid, this->trdid, name, local_cxy, cycle ); |
---|
| 398 | #endif |
---|
| 399 | |
---|
| 400 | // compute number of pages |
---|
| 401 | uint32_t npages = size >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 402 | if( (size & CONFIG_PPM_PAGE_MASK) || (size == 0) ) npages++; |
---|
| 403 | |
---|
| 404 | // loop on pages |
---|
| 405 | for( page_id = 0 ; page_id < npages ; page_id ++ ) |
---|
| 406 | { |
---|
| 407 | // If the mage is missing, this function allocates the missing page, |
---|
| 408 | // and load the page from IOC device into mapper |
---|
| 409 | page_xp = mapper_remote_get_page( XPTR( local_cxy , mapper ), page_id ); |
---|
| 410 | |
---|
| 411 | if( page_xp == XPTR_NULL ) return -1; |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | #if DEBUG_VFS_INODE_LOAD_ALL |
---|
| 415 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 416 | if( DEBUG_VFS_INODE_LOAD_ALL < cycle ) |
---|
| 417 | printk("\n[%s] thread[%x,%x] exit for <%x> in cluster %x / cycle %d\n", |
---|
| 418 | __FUNCTION__, this->process->pid, this->trdid, name, local_cxy, cycle ); |
---|
| 419 | #endif |
---|
| 420 | |
---|
| 421 | return 0; |
---|
| 422 | |
---|
| 423 | } // end vfs_inode_load_all_pages() |
---|
| 424 | |
---|
[626] | 425 | ///////////////////////////////////////// |
---|
| 426 | void vfs_inode_display( xptr_t inode_xp ) |
---|
| 427 | { |
---|
| 428 | assert( (inode_xp != XPTR_NULL), "inode pointer is NULL"); |
---|
| 429 | |
---|
| 430 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 431 | |
---|
| 432 | vfs_inode_get_name( inode_xp , name ); |
---|
| 433 | |
---|
| 434 | cxy_t inode_cxy = GET_CXY( inode_xp ); |
---|
| 435 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); |
---|
| 436 | |
---|
| 437 | vfs_inode_type_t type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
| 438 | uint32_t attr = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->attr ) ); |
---|
| 439 | uint32_t size = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->size ) ); |
---|
| 440 | uint32_t parents = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->links ) ); |
---|
| 441 | mapper_t * mapper = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->mapper ) ); |
---|
| 442 | void * extend = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->extend ) ); |
---|
| 443 | |
---|
| 444 | printk("\n**** inode <%s>\n" |
---|
| 445 | " - type = %s\n" |
---|
| 446 | " - attr = %x\n" |
---|
| 447 | " - size = %d\n" |
---|
| 448 | " - parents = %d\n" |
---|
| 449 | " - cxy = %x\n" |
---|
| 450 | " - inode = %x\n" |
---|
| 451 | " - mapper = %x\n" |
---|
| 452 | " - extend = %x\n", |
---|
| 453 | name, |
---|
| 454 | vfs_inode_type_str( type ), |
---|
| 455 | attr, |
---|
| 456 | size, |
---|
| 457 | parents, |
---|
| 458 | inode_cxy, |
---|
| 459 | inode_ptr, |
---|
| 460 | mapper, |
---|
| 461 | extend ); |
---|
| 462 | |
---|
| 463 | } // end vfs_inode_display() |
---|
| 464 | |
---|
| 465 | |
---|
[204] | 466 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 467 | // VFS dentry descriptor related functions |
---|
[1] | 468 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 469 | |
---|
[23] | 470 | /////////////////////////////////////////////////// |
---|
| 471 | error_t vfs_dentry_create( vfs_fs_type_t fs_type, |
---|
| 472 | char * name, |
---|
| 473 | xptr_t * dentry_xp ) |
---|
[1] | 474 | { |
---|
| 475 | vfs_ctx_t * ctx; // context descriptor |
---|
| 476 | vfs_dentry_t * dentry; // dentry descriptor (to be allocated) |
---|
| 477 | kmem_req_t req; // request to kernel memory allocator |
---|
| 478 | |
---|
[438] | 479 | #if DEBUG_VFS_DENTRY_CREATE |
---|
[598] | 480 | thread_t * this = CURRENT_THREAD; |
---|
[433] | 481 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 482 | if( DEBUG_VFS_DENTRY_CREATE < cycle ) |
---|
[614] | 483 | printk("\n[%s] thread[%x,%x] enter for <%s> / cycle %d\n", |
---|
| 484 | __FUNCTION__, this->process->pid, this->trdid, name, cycle ); |
---|
[433] | 485 | #endif |
---|
[296] | 486 | |
---|
[188] | 487 | // get pointer on context |
---|
[23] | 488 | if ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS]; |
---|
| 489 | else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS]; |
---|
| 490 | else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS]; |
---|
[459] | 491 | else |
---|
[1] | 492 | { |
---|
| 493 | ctx = NULL; |
---|
[610] | 494 | return -1; |
---|
[1] | 495 | } |
---|
| 496 | |
---|
| 497 | // get name length |
---|
| 498 | uint32_t length = strlen( name ); |
---|
| 499 | |
---|
[459] | 500 | if( length >= CONFIG_VFS_MAX_NAME_LENGTH ) return EINVAL; |
---|
[437] | 501 | |
---|
[1] | 502 | // allocate memory for dentry descriptor |
---|
| 503 | req.type = KMEM_VFS_DENTRY; |
---|
| 504 | req.size = sizeof(vfs_dentry_t); |
---|
| 505 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 506 | dentry = (vfs_dentry_t *)kmem_alloc( &req ); |
---|
| 507 | |
---|
[610] | 508 | if( dentry == NULL ) |
---|
| 509 | { |
---|
| 510 | printk("\n[ERROR] in %s : cannot allocate dentry descriptor\n", |
---|
| 511 | __FUNCTION__ ); |
---|
| 512 | return -1; |
---|
| 513 | } |
---|
[437] | 514 | |
---|
[1] | 515 | // initialize dentry descriptor |
---|
| 516 | dentry->ctx = ctx; |
---|
| 517 | dentry->length = length; |
---|
[602] | 518 | dentry->extend = NULL; |
---|
[1] | 519 | strcpy( dentry->name , name ); |
---|
| 520 | |
---|
[23] | 521 | // return extended pointer on dentry |
---|
[1] | 522 | *dentry_xp = XPTR( local_cxy , dentry ); |
---|
| 523 | |
---|
[438] | 524 | #if DEBUG_VFS_DENTRY_CREATE |
---|
[433] | 525 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 526 | if( DEBUG_VFS_DENTRY_CREATE < cycle ) |
---|
[602] | 527 | printk("\n[%s] thread[%x,%x] exit for <%s> / dentry [%x,%x] / cycle %d\n", |
---|
| 528 | __FUNCTION__, this->process->pid, this->trdid, name, local_cxy, dentry, cycle ); |
---|
[433] | 529 | #endif |
---|
[296] | 530 | |
---|
[1] | 531 | return 0; |
---|
| 532 | |
---|
| 533 | } // end vfs_dentry_create() |
---|
| 534 | |
---|
[602] | 535 | //////////////////////////////////////////////// |
---|
| 536 | void vfs_dentry_destroy( vfs_dentry_t * dentry ) |
---|
[1] | 537 | { |
---|
[459] | 538 | // release memory allocated to dentry |
---|
[1] | 539 | kmem_req_t req; |
---|
| 540 | req.ptr = dentry; |
---|
| 541 | req.type = KMEM_VFS_DENTRY; |
---|
| 542 | kmem_free( &req ); |
---|
[459] | 543 | |
---|
[602] | 544 | } // end vfs_dentry_destroy() |
---|
| 545 | |
---|
| 546 | |
---|
[1] | 547 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 548 | // VFS file descriptor related functions |
---|
[1] | 549 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 550 | |
---|
[23] | 551 | ///////////////////////////////////////////// |
---|
| 552 | error_t vfs_file_create( vfs_inode_t * inode, |
---|
| 553 | uint32_t attr, |
---|
| 554 | xptr_t * file_xp ) |
---|
| 555 | { |
---|
| 556 | vfs_file_t * file; |
---|
| 557 | kmem_req_t req; |
---|
| 558 | |
---|
[568] | 559 | #if DEBUG_VFS_FILE_CREATE |
---|
[598] | 560 | thread_t * this = CURRENT_THREAD; |
---|
[568] | 561 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 562 | if( DEBUG_VFS_OPEN < cycle ) |
---|
[598] | 563 | printk("\n[%s] thread[%x,%x] enter for inode %x in cluster %x / cycle %d\n", |
---|
| 564 | __FUNCTION__, this->process->pid, this->trdid, inode, local_cxy, cycle ); |
---|
[568] | 565 | #endif |
---|
| 566 | |
---|
[23] | 567 | // allocate memory for new file descriptor |
---|
| 568 | req.type = KMEM_VFS_FILE; |
---|
| 569 | req.size = sizeof(vfs_file_t); |
---|
| 570 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 571 | file = (vfs_file_t *)kmem_alloc( &req ); |
---|
| 572 | |
---|
| 573 | if( file == NULL ) return ENOMEM; |
---|
| 574 | |
---|
| 575 | // initializes new file descriptor |
---|
| 576 | file->gc = 0; |
---|
| 577 | file->type = inode->type; |
---|
| 578 | file->attr = attr; |
---|
| 579 | file->offset = 0; |
---|
[337] | 580 | file->refcount = 1; |
---|
[23] | 581 | file->inode = inode; |
---|
| 582 | file->ctx = inode->ctx; |
---|
| 583 | file->mapper = inode->mapper; |
---|
| 584 | |
---|
[568] | 585 | remote_rwlock_init( XPTR( local_cxy , &file->lock ), LOCK_VFS_FILE ); |
---|
[23] | 586 | |
---|
| 587 | *file_xp = XPTR( local_cxy , file ); |
---|
[459] | 588 | |
---|
[568] | 589 | #if DEBUG_VFS_FILE_CREATE |
---|
| 590 | cycle = (uint32_t)hal_get_cycles(); |
---|
[459] | 591 | if( DEBUG_VFS_OPEN < cycle ) |
---|
[598] | 592 | printk("\n[%s] thread[%x,%x] created file %x in cluster %x / cycle %d\n", |
---|
| 593 | __FUNCTION__, this->process->pid, this->trdid, file, local_cxy, cycle ); |
---|
[459] | 594 | #endif |
---|
| 595 | |
---|
[23] | 596 | return 0; |
---|
| 597 | |
---|
| 598 | } // end vfs_file_create() |
---|
| 599 | |
---|
| 600 | /////////////////////////////////////////// |
---|
| 601 | void vfs_file_destroy( vfs_file_t * file ) |
---|
| 602 | { |
---|
| 603 | kmem_req_t req; |
---|
| 604 | req.ptr = file; |
---|
| 605 | req.type = KMEM_VFS_FILE; |
---|
| 606 | kmem_free( &req ); |
---|
| 607 | |
---|
[459] | 608 | #if DEBUG_VFS_CLOSE |
---|
[623] | 609 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 610 | vfs_file_get_name( XPTR( local_cxy , file ) , name ); |
---|
[598] | 611 | thread_t * this = CURRENT_THREAD; |
---|
[459] | 612 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 613 | if( DEBUG_VFS_CLOSE < cycle ) |
---|
[623] | 614 | printk("\n[%s] thread[%x,%x] deleted file <%s> in cluster %x / cycle %d\n", |
---|
| 615 | __FUNCTION__, this->process->pid, this->trdid, name, local_cxy, cycle ); |
---|
[459] | 616 | #endif |
---|
| 617 | |
---|
[23] | 618 | } // end vfs_file_destroy() |
---|
| 619 | |
---|
| 620 | |
---|
[1] | 621 | //////////////////////////////////////// |
---|
| 622 | void vfs_file_count_up( xptr_t file_xp ) |
---|
| 623 | { |
---|
| 624 | // get file cluster and local pointer |
---|
| 625 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
[459] | 626 | vfs_file_t * file_ptr = GET_PTR( file_xp ); |
---|
[1] | 627 | |
---|
| 628 | // atomically increment count |
---|
| 629 | hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , 1 ); |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | ////////////////////////////////////////// |
---|
| 633 | void vfs_file_count_down( xptr_t file_xp ) |
---|
| 634 | { |
---|
| 635 | // get file cluster and local pointer |
---|
| 636 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
[459] | 637 | vfs_file_t * file_ptr = GET_PTR( file_xp ); |
---|
[1] | 638 | |
---|
| 639 | // atomically decrement count |
---|
| 640 | hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , -1 ); |
---|
| 641 | } |
---|
| 642 | |
---|
[623] | 643 | /////////////////////////////////////// |
---|
| 644 | void vfs_file_get_name( xptr_t file_xp, |
---|
| 645 | char * name ) |
---|
| 646 | { |
---|
| 647 | // get cluster and local pointer on remote file |
---|
| 648 | vfs_file_t * file_ptr = GET_PTR( file_xp ); |
---|
| 649 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
| 650 | |
---|
| 651 | // get pointers on remote inode |
---|
| 652 | vfs_inode_t * inode_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) ); |
---|
| 653 | xptr_t inode_xp = XPTR( file_cxy , inode_ptr ); |
---|
| 654 | |
---|
| 655 | // call the relevant function |
---|
| 656 | vfs_inode_get_name( inode_xp , name ); |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | |
---|
[23] | 660 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 661 | // "syscalls" API related functions |
---|
[23] | 662 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 663 | |
---|
[407] | 664 | ////////////////////////////////////// |
---|
[610] | 665 | error_t vfs_open( xptr_t root_xp, |
---|
[407] | 666 | char * path, |
---|
[610] | 667 | xptr_t process_xp, |
---|
[407] | 668 | uint32_t flags, |
---|
| 669 | uint32_t mode, |
---|
| 670 | xptr_t * new_file_xp, |
---|
| 671 | uint32_t * new_file_id ) |
---|
[1] | 672 | { |
---|
[610] | 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 |
---|
| 681 | xptr_t vfs_root_xp; // extended pointer on VFS root inode |
---|
| 682 | vfs_inode_t * vfs_root_ptr; // local pointer on VFS root inode |
---|
| 683 | cxy_t vfs_root_cxy; // VFS root inode cluster identifier |
---|
| 684 | xptr_t lock_xp; // extended pointer on Inode Tree lock |
---|
[1] | 685 | |
---|
[602] | 686 | if( mode != 0 ) |
---|
| 687 | { |
---|
| 688 | printk("\n[ERROR] in %s : the mode parameter is not supported yet\n" ); |
---|
| 689 | return -1; |
---|
| 690 | } |
---|
| 691 | |
---|
[610] | 692 | thread_t * this = CURRENT_THREAD; |
---|
| 693 | process_t * process = this->process; |
---|
| 694 | |
---|
[438] | 695 | #if DEBUG_VFS_OPEN |
---|
[433] | 696 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 697 | if( DEBUG_VFS_OPEN < cycle ) |
---|
[610] | 698 | printk("\n[%s] thread[%x,%x] enter for <%s> / root_inode (%x,%x) / cycle %d\n", |
---|
| 699 | __FUNCTION__, process->pid, this->trdid, path, GET_CXY(root_xp), GET_PTR(root_xp), cycle ); |
---|
[433] | 700 | #endif |
---|
[101] | 701 | |
---|
[23] | 702 | // compute lookup working mode |
---|
| 703 | lookup_mode = VFS_LOOKUP_OPEN; |
---|
| 704 | if( (flags & O_DIR ) ) lookup_mode |= VFS_LOOKUP_DIR; |
---|
| 705 | if( (flags & O_CREAT ) ) lookup_mode |= VFS_LOOKUP_CREATE; |
---|
| 706 | if( (flags & O_EXCL ) ) lookup_mode |= VFS_LOOKUP_EXCL; |
---|
| 707 | |
---|
| 708 | // compute attributes for the created file |
---|
| 709 | file_attr = 0; |
---|
[407] | 710 | if( (flags & O_RDONLY ) == 0 ) file_attr |= FD_ATTR_WRITE_ENABLE; |
---|
| 711 | if( (flags & O_WRONLY ) == 0 ) file_attr |= FD_ATTR_READ_ENABLE; |
---|
[23] | 712 | if( (flags & O_SYNC ) ) file_attr |= FD_ATTR_SYNC; |
---|
| 713 | if( (flags & O_APPEND ) ) file_attr |= FD_ATTR_APPEND; |
---|
| 714 | if( (flags & O_CLOEXEC) ) file_attr |= FD_ATTR_CLOSE_EXEC; |
---|
[1] | 715 | |
---|
[610] | 716 | // build extended pointer on lock protecting Inode Tree |
---|
| 717 | vfs_root_xp = process->vfs_root_xp; |
---|
| 718 | vfs_root_ptr = GET_PTR( vfs_root_xp ); |
---|
| 719 | vfs_root_cxy = GET_CXY( vfs_root_xp ); |
---|
| 720 | lock_xp = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock ); |
---|
| 721 | |
---|
| 722 | // take lock protecting Inode Tree in read mode |
---|
| 723 | remote_rwlock_rd_acquire( lock_xp ); |
---|
| 724 | |
---|
[23] | 725 | // get extended pointer on target inode |
---|
[610] | 726 | error = vfs_lookup( root_xp, |
---|
| 727 | path, |
---|
| 728 | lookup_mode, |
---|
| 729 | &inode_xp, |
---|
| 730 | NULL ); |
---|
[23] | 731 | |
---|
[610] | 732 | // release lock protecting Inode Tree |
---|
| 733 | remote_rwlock_rd_release( lock_xp ); |
---|
[23] | 734 | |
---|
[610] | 735 | if( error ) |
---|
| 736 | { |
---|
| 737 | printk("\n[ERROR] in %s : cannot get inode <%s>\n", |
---|
| 738 | __FUNCTION__ , path ); |
---|
| 739 | return -1; |
---|
| 740 | } |
---|
| 741 | |
---|
[23] | 742 | // get target inode cluster and local pointer |
---|
| 743 | inode_cxy = GET_CXY( inode_xp ); |
---|
[473] | 744 | inode_ptr = GET_PTR( inode_xp ); |
---|
[23] | 745 | |
---|
[610] | 746 | #if (DEBUG_VFS_OPEN & 1) |
---|
| 747 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 748 | if( DEBUG_VFS_OPEN < cycle ) |
---|
| 749 | printk("\n[%s] thread[%x,%x] found inode(%x,%x) for <%s>\n", |
---|
| 750 | __FUNCTION__, process->pid, this->trdid, inode_cxy, inode_ptr, path ); |
---|
| 751 | #endif |
---|
| 752 | |
---|
[23] | 753 | // create a new file descriptor in cluster containing inode |
---|
| 754 | if( inode_cxy == local_cxy ) // target cluster is local |
---|
[1] | 755 | { |
---|
[23] | 756 | error = vfs_file_create( inode_ptr , file_attr , &file_xp ); |
---|
[1] | 757 | } |
---|
[23] | 758 | else // target cluster is remote |
---|
| 759 | { |
---|
| 760 | rpc_vfs_file_create_client( inode_cxy , inode_ptr , file_attr , &file_xp , &error ); |
---|
| 761 | } |
---|
[1] | 762 | |
---|
[23] | 763 | if( error ) return error; |
---|
[1] | 764 | |
---|
[610] | 765 | #if (DEBUG_VFS_OPEN & 1) |
---|
| 766 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 767 | if( DEBUG_VFS_OPEN < cycle ) |
---|
| 768 | printk("\n[%s] thread[%x,%x] created file descriptor (%x,%x) for <%s>\n", |
---|
| 769 | __FUNCTION__, process->pid, this->trdid, GET_CXY(file_xp), GET_PTR(file_xp), path ); |
---|
| 770 | #endif |
---|
| 771 | |
---|
[407] | 772 | // allocate and register a new file descriptor index in reference process |
---|
[610] | 773 | error = process_fd_register( process_xp , file_xp , &file_id ); |
---|
[1] | 774 | |
---|
[23] | 775 | if( error ) return error; |
---|
[1] | 776 | |
---|
[438] | 777 | #if DEBUG_VFS_OPEN |
---|
[433] | 778 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 779 | if( DEBUG_VFS_OPEN < cycle ) |
---|
[626] | 780 | printk("\n[%s] thread[%x,%x] exit for <%s> / fdid %d / cxy %x / cycle %d\n", |
---|
[610] | 781 | __FUNCTION__, process->pid, this->trdid, path, file_id, GET_CXY( file_xp ), cycle ); |
---|
[433] | 782 | #endif |
---|
[238] | 783 | |
---|
[23] | 784 | // success |
---|
| 785 | *new_file_xp = file_xp; |
---|
| 786 | *new_file_id = file_id; |
---|
| 787 | return 0; |
---|
[1] | 788 | |
---|
[23] | 789 | } // end vfs_open() |
---|
| 790 | |
---|
[407] | 791 | ////////////////////////////////////// |
---|
| 792 | int vfs_user_move( bool_t to_buffer, |
---|
| 793 | xptr_t file_xp, |
---|
| 794 | void * buffer, |
---|
| 795 | uint32_t size ) |
---|
[23] | 796 | { |
---|
| 797 | cxy_t file_cxy; // remote file descriptor cluster |
---|
| 798 | vfs_file_t * file_ptr; // remote file descriptor local pointer |
---|
| 799 | vfs_inode_type_t inode_type; |
---|
| 800 | uint32_t file_offset; // current offset in file |
---|
| 801 | mapper_t * mapper; |
---|
| 802 | error_t error; |
---|
| 803 | |
---|
[602] | 804 | // check argument |
---|
[625] | 805 | assert( (file_xp != XPTR_NULL), "file_xp == XPTR_NULL" ); |
---|
[602] | 806 | |
---|
[23] | 807 | // get cluster and local pointer on remote file descriptor |
---|
| 808 | file_cxy = GET_CXY( file_xp ); |
---|
[473] | 809 | file_ptr = GET_PTR( file_xp ); |
---|
[23] | 810 | |
---|
| 811 | // get inode type from remote file descriptor |
---|
[568] | 812 | inode_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) ); |
---|
[23] | 813 | |
---|
[602] | 814 | // check inode type |
---|
[625] | 815 | assert( (inode_type == INODE_TYPE_FILE), "bad inode type" ); |
---|
[23] | 816 | |
---|
[628] | 817 | // get mapper pointer and file offset from file descriptor |
---|
| 818 | file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) ); |
---|
| 819 | mapper = hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) ); |
---|
| 820 | |
---|
[626] | 821 | #if DEBUG_VFS_USER_MOVE |
---|
| 822 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 823 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 824 | thread_t * this = CURRENT_THREAD; |
---|
| 825 | vfs_inode_t * inode = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) ); |
---|
| 826 | vfs_inode_get_name( XPTR( file_cxy , inode ) , name ); |
---|
| 827 | if( cycle > DEBUG_VFS_USER_MOVE ) |
---|
| 828 | { |
---|
| 829 | if( to_buffer ) |
---|
[628] | 830 | printk("\n[%s] thread[%x,%x] enter / %d bytes / map(%s) -> buf(%x) / offset %d / cycle %d\n", |
---|
| 831 | __FUNCTION__ , this->process->pid, this->trdid, size, name, buffer, file_offset, cycle ); |
---|
[626] | 832 | else |
---|
[628] | 833 | printk("\n[%s] thread[%x,%x] enter / %d bytes / buf(%x) -> map(%s) / offset %d / cycle %d\n", |
---|
| 834 | __FUNCTION__ , this->process->pid, this->trdid, size, buffer, name, file_offset, cycle ); |
---|
[626] | 835 | } |
---|
| 836 | #endif |
---|
| 837 | |
---|
[407] | 838 | // move data between mapper and buffer |
---|
[610] | 839 | error = mapper_move_user( XPTR( file_cxy , mapper ), |
---|
| 840 | to_buffer, |
---|
| 841 | file_offset, |
---|
| 842 | buffer, |
---|
| 843 | size ); |
---|
[602] | 844 | if( error ) |
---|
| 845 | { |
---|
[625] | 846 | printk("\n[ERROR] in %s : cannot move data", __FUNCTION__ ); |
---|
[602] | 847 | return -1; |
---|
| 848 | } |
---|
| 849 | |
---|
[625] | 850 | // update file offset in file descriptor |
---|
| 851 | hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->offset ) , size ); |
---|
| 852 | |
---|
| 853 | #if DEBUG_VFS_USER_MOVE |
---|
[626] | 854 | cycle = (uint32_t)hal_get_cycles(); |
---|
[625] | 855 | if( cycle > DEBUG_VFS_USER_MOVE ) |
---|
| 856 | { |
---|
| 857 | if( to_buffer ) |
---|
[628] | 858 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
| 859 | __FUNCTION__ , this->process->pid, cycle ); |
---|
[625] | 860 | else |
---|
[628] | 861 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
| 862 | __FUNCTION__ , this->process->pid, cycle ); |
---|
[625] | 863 | } |
---|
| 864 | #endif |
---|
| 865 | |
---|
[602] | 866 | return size; |
---|
| 867 | |
---|
[313] | 868 | } // end vfs_user_move() |
---|
[23] | 869 | |
---|
[317] | 870 | //////////////////////////////////////////// |
---|
| 871 | error_t vfs_kernel_move( bool_t to_buffer, |
---|
| 872 | xptr_t file_xp, |
---|
| 873 | xptr_t buffer_xp, |
---|
| 874 | uint32_t size ) |
---|
| 875 | { |
---|
| 876 | cxy_t file_cxy; // remote file descriptor cluster |
---|
| 877 | vfs_file_t * file_ptr; // remote file descriptor local pointer |
---|
[602] | 878 | vfs_inode_type_t inode_type; // remote file type |
---|
[317] | 879 | uint32_t file_offset; // current offset in file |
---|
[602] | 880 | mapper_t * mapper_ptr; // remote mapper local pointer |
---|
| 881 | xptr_t mapper_xp; // remote mapper extended pointer |
---|
[317] | 882 | error_t error; |
---|
| 883 | |
---|
[602] | 884 | // check argument |
---|
[625] | 885 | assert( (file_xp != XPTR_NULL) , "file_xp == XPTR_NULL" ); |
---|
[602] | 886 | |
---|
[317] | 887 | // get cluster and local pointer on remote file descriptor |
---|
| 888 | file_cxy = GET_CXY( file_xp ); |
---|
[473] | 889 | file_ptr = GET_PTR( file_xp ); |
---|
[317] | 890 | |
---|
| 891 | // get inode type from remote file descriptor |
---|
[568] | 892 | inode_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) ); |
---|
[610] | 893 | |
---|
[625] | 894 | // check inode type |
---|
| 895 | assert( (inode_type == INODE_TYPE_FILE), "bad file type" ); |
---|
[317] | 896 | |
---|
[625] | 897 | // get mapper pointers and file offset from file descriptor |
---|
| 898 | file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) ); |
---|
| 899 | mapper_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) ); |
---|
| 900 | mapper_xp = XPTR( file_cxy , mapper_ptr ); |
---|
| 901 | |
---|
| 902 | // move data between mapper and buffer |
---|
| 903 | error = mapper_move_kernel( mapper_xp, |
---|
| 904 | to_buffer, |
---|
| 905 | file_offset, |
---|
| 906 | buffer_xp, |
---|
| 907 | size ); |
---|
| 908 | if( error ) |
---|
[317] | 909 | { |
---|
[625] | 910 | printk("\n[ERROR] in %s : cannot move data", __FUNCTION__ ); |
---|
[317] | 911 | return -1; |
---|
| 912 | } |
---|
[602] | 913 | |
---|
[625] | 914 | #if DEBUG_VFS_KERNEL_MOVE |
---|
| 915 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 916 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 917 | thread_t * this = CURRENT_THREAD; |
---|
| 918 | cxy_t buffer_cxy = GET_CXY( buffer_xp ); |
---|
| 919 | void * buffer_ptr = GET_PTR( buffer_xp ); |
---|
| 920 | vfs_inode_t * inode = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) ); |
---|
| 921 | vfs_inode_get_name( XPTR( file_cxy , inode ) , name ); |
---|
| 922 | if( cycle > DEBUG_VFS_KERNEL_MOVE ) |
---|
| 923 | { |
---|
| 924 | if( to_buffer ) |
---|
| 925 | printk("\n[%s] thread[%x,%x] moves %d bytes from <%s> mapper to buffer(%x,%x) / cycle %d\n", |
---|
| 926 | __FUNCTION__ , this->process->pid, this->trdid, size, name, buffer_cxy, buffer_ptr ); |
---|
| 927 | else |
---|
| 928 | printk("\n[%s] thread[%x,%x] moves %d bytes from buffer(%x,%x) to <%s> mapper / cycle %d\n", |
---|
| 929 | __FUNCTION__ , this->process->pid, this->trdid, size, buffer_cxy, buffer_ptr, name ); |
---|
| 930 | } |
---|
| 931 | #endif |
---|
| 932 | |
---|
[602] | 933 | return 0; |
---|
| 934 | |
---|
[317] | 935 | } // end vfs_kernel_move() |
---|
| 936 | |
---|
[23] | 937 | ////////////////////////////////////// |
---|
| 938 | error_t vfs_lseek( xptr_t file_xp, |
---|
| 939 | uint32_t offset, |
---|
| 940 | uint32_t whence, |
---|
| 941 | uint32_t * new_offset ) |
---|
| 942 | { |
---|
[266] | 943 | xptr_t offset_xp; |
---|
| 944 | xptr_t lock_xp; |
---|
[602] | 945 | xptr_t size_xp; |
---|
[266] | 946 | cxy_t file_cxy; |
---|
| 947 | vfs_file_t * file_ptr; |
---|
| 948 | vfs_inode_t * inode_ptr; |
---|
| 949 | uint32_t new; |
---|
| 950 | |
---|
[602] | 951 | // check argument |
---|
[625] | 952 | assert( (file_xp != XPTR_NULL) , "file_xp == XPTR_NULL" ); |
---|
[266] | 953 | |
---|
| 954 | // get cluster and local pointer on remote file descriptor |
---|
| 955 | file_cxy = GET_CXY( file_xp ); |
---|
[473] | 956 | file_ptr = GET_PTR( file_xp ); |
---|
[266] | 957 | |
---|
[602] | 958 | // get local pointer on remote inode |
---|
| 959 | inode_ptr = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) ); |
---|
| 960 | |
---|
| 961 | // build extended pointers on lock, offset and size |
---|
[266] | 962 | offset_xp = XPTR( file_cxy , &file_ptr->offset ); |
---|
| 963 | lock_xp = XPTR( file_cxy , &file_ptr->lock ); |
---|
[602] | 964 | size_xp = XPTR( file_cxy , &inode_ptr->size ); |
---|
[266] | 965 | |
---|
| 966 | // take file descriptor lock |
---|
[568] | 967 | remote_rwlock_wr_acquire( lock_xp ); |
---|
[266] | 968 | |
---|
| 969 | if ( whence == SEEK_CUR ) // new = current + offset |
---|
| 970 | { |
---|
[568] | 971 | new = hal_remote_l32( offset_xp ) + offset; |
---|
[266] | 972 | } |
---|
| 973 | else if ( whence == SEEK_SET ) // new = offset |
---|
| 974 | { |
---|
| 975 | new = offset; |
---|
| 976 | } |
---|
| 977 | else if ( whence == SEEK_END ) // new = size + offset |
---|
| 978 | { |
---|
[602] | 979 | new = hal_remote_l32( size_xp ) + offset; |
---|
[266] | 980 | } |
---|
| 981 | else |
---|
| 982 | { |
---|
| 983 | printk("\n[ERROR] in %s : illegal whence value\n", __FUNCTION__ ); |
---|
[568] | 984 | remote_rwlock_wr_release( lock_xp ); |
---|
[266] | 985 | return -1; |
---|
| 986 | } |
---|
| 987 | |
---|
[602] | 988 | #if DEBUG_VFS_LSEEK |
---|
| 989 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 990 | thread_t * this = CURRENT_THREAD; |
---|
| 991 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 992 | vfs_inode_get_name( XPTR( file_cxy , inode_ptr ) , name ); |
---|
| 993 | if( cycle > DEBUG_VFS_LSEEK ) |
---|
| 994 | printk("\n[%s] thread[%x,%x] for <%s> / new offset %d / cycle %d\n", |
---|
| 995 | __FUNCTION__ , this->process->pid, this->trdid, name, new, cycle ); |
---|
| 996 | #endif |
---|
| 997 | |
---|
[266] | 998 | // set new offset |
---|
[568] | 999 | hal_remote_s32( offset_xp , new ); |
---|
[266] | 1000 | |
---|
| 1001 | // release file descriptor lock |
---|
[568] | 1002 | remote_rwlock_wr_release( lock_xp ); |
---|
[266] | 1003 | |
---|
| 1004 | // success |
---|
[602] | 1005 | if ( new_offset != NULL ) *new_offset = new; |
---|
[1] | 1006 | return 0; |
---|
| 1007 | |
---|
[23] | 1008 | } // vfs_lseek() |
---|
| 1009 | |
---|
[623] | 1010 | //////////////////////////////////// |
---|
[23] | 1011 | error_t vfs_close( xptr_t file_xp, |
---|
| 1012 | uint32_t file_id ) |
---|
[1] | 1013 | { |
---|
[623] | 1014 | cxy_t file_cxy; // cluster containing the file descriptor. |
---|
| 1015 | vfs_file_t * file_ptr; // local ponter on file descriptor |
---|
| 1016 | cxy_t owner_cxy; // process owner cluster |
---|
| 1017 | pid_t pid; // process identifier |
---|
| 1018 | lpid_t lpid; // process local index |
---|
| 1019 | xptr_t root_xp; // root of xlist (processes , or dentries) |
---|
| 1020 | xptr_t lock_xp; // lock protecting the xlist |
---|
| 1021 | xptr_t iter_xp; // iterator on xlist |
---|
| 1022 | mapper_t * mapper_ptr; // local pointer on associated mapper |
---|
| 1023 | vfs_inode_t * inode_ptr; // local pointer on associated inode |
---|
| 1024 | uint32_t size; // current file size (from inode descriptor) |
---|
| 1025 | error_t error; |
---|
[459] | 1026 | |
---|
[623] | 1027 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; // file name |
---|
[23] | 1028 | |
---|
[623] | 1029 | // check argument |
---|
[625] | 1030 | assert( (file_xp != XPTR_NULL) , "file_xp is XPTR_NULL" ); |
---|
[623] | 1031 | |
---|
[23] | 1032 | thread_t * this = CURRENT_THREAD; |
---|
| 1033 | process_t * process = this->process; |
---|
[623] | 1034 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
[23] | 1035 | |
---|
[623] | 1036 | // get file name |
---|
| 1037 | vfs_file_get_name( file_xp , name ); |
---|
| 1038 | |
---|
[459] | 1039 | #if DEBUG_VFS_CLOSE |
---|
| 1040 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1041 | if( DEBUG_VFS_CLOSE < cycle ) |
---|
[623] | 1042 | printk("\n[%s] thread[%x,%x] enter for <%s> / cycle %d\n", |
---|
| 1043 | __FUNCTION__, process->pid, this->trdid, name, cycle ); |
---|
[459] | 1044 | #endif |
---|
[1] | 1045 | |
---|
[623] | 1046 | // get cluster and local pointer on remote file descriptor |
---|
| 1047 | file_cxy = GET_CXY( file_xp ); |
---|
| 1048 | file_ptr = GET_PTR( file_xp ); |
---|
[23] | 1049 | |
---|
[623] | 1050 | //////// 1) update all dirty pages from mapper to device |
---|
[23] | 1051 | |
---|
[633] | 1052 | // get local pointer on mapper associated to file |
---|
[623] | 1053 | mapper_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) ); |
---|
[23] | 1054 | |
---|
[623] | 1055 | // copy all dirty pages from mapper to device |
---|
| 1056 | if( file_cxy == local_cxy ) |
---|
| 1057 | { |
---|
| 1058 | error = mapper_sync( mapper_ptr ); |
---|
| 1059 | } |
---|
| 1060 | else |
---|
| 1061 | { |
---|
| 1062 | rpc_mapper_sync_client( file_cxy, |
---|
| 1063 | mapper_ptr, |
---|
| 1064 | &error ); |
---|
| 1065 | } |
---|
[459] | 1066 | |
---|
[623] | 1067 | if( error ) |
---|
| 1068 | { |
---|
| 1069 | printk("\n[ERROR] in %s : cannot synchronise dirty pages for <%s>\n", |
---|
| 1070 | __FUNCTION__, name ); |
---|
| 1071 | return -1; |
---|
| 1072 | } |
---|
[23] | 1073 | |
---|
[623] | 1074 | #if DEBUG_VFS_CLOSE |
---|
| 1075 | if( DEBUG_VFS_CLOSE < cycle ) |
---|
| 1076 | printk("\n[%s] thread[%x,%x] synchronised mapper of <%s> to device\n", |
---|
| 1077 | __FUNCTION__, process->pid, this->trdid, name ); |
---|
| 1078 | #endif |
---|
| 1079 | |
---|
[625] | 1080 | //////// 2) update file size in all parent directory mapper(s) and update device |
---|
[623] | 1081 | |
---|
[633] | 1082 | // get local pointer on remote inode |
---|
[623] | 1083 | inode_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) ); |
---|
| 1084 | |
---|
| 1085 | // get file size from remote inode |
---|
| 1086 | size = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) ); |
---|
| 1087 | |
---|
| 1088 | // get root of list of parents dentry |
---|
| 1089 | root_xp = XPTR( file_cxy , &inode_ptr->parents ); |
---|
| 1090 | |
---|
| 1091 | // loop on all parents |
---|
[23] | 1092 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
[1] | 1093 | { |
---|
[623] | 1094 | // get pointers on parent directory dentry |
---|
| 1095 | xptr_t parent_dentry_xp = XLIST_ELEMENT( iter_xp , vfs_dentry_t , parents ); |
---|
| 1096 | cxy_t parent_cxy = GET_CXY( parent_dentry_xp ); |
---|
| 1097 | vfs_dentry_t * parent_dentry_ptr = GET_PTR( parent_dentry_xp ); |
---|
[1] | 1098 | |
---|
[623] | 1099 | // get local pointer on parent directory inode |
---|
| 1100 | vfs_inode_t * parent_inode_ptr = hal_remote_lpt( XPTR( parent_cxy, |
---|
| 1101 | &parent_dentry_ptr->parent ) ); |
---|
| 1102 | |
---|
| 1103 | // get local pointer on parent directory mapper |
---|
| 1104 | mapper_t * parent_mapper_ptr = hal_remote_lpt( XPTR( parent_cxy, |
---|
| 1105 | &parent_inode_ptr->mapper ) ); |
---|
| 1106 | |
---|
| 1107 | // update dentry size in parent directory mapper |
---|
| 1108 | if( parent_cxy == local_cxy ) |
---|
| 1109 | { |
---|
| 1110 | error = vfs_fs_update_dentry( parent_inode_ptr, |
---|
| 1111 | parent_dentry_ptr, |
---|
| 1112 | size ); |
---|
| 1113 | } |
---|
| 1114 | else |
---|
| 1115 | { |
---|
| 1116 | rpc_vfs_fs_update_dentry_client( parent_cxy, |
---|
| 1117 | parent_inode_ptr, |
---|
| 1118 | parent_dentry_ptr, |
---|
| 1119 | size, |
---|
| 1120 | &error ); |
---|
| 1121 | } |
---|
| 1122 | |
---|
| 1123 | if( error ) |
---|
| 1124 | { |
---|
| 1125 | printk("\n[ERROR] in %s : cannot update size in parent\n", |
---|
| 1126 | __FUNCTION__ ); |
---|
| 1127 | return -1; |
---|
| 1128 | } |
---|
| 1129 | |
---|
| 1130 | #if DEBUG_VFS_CLOSE |
---|
| 1131 | char parent_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1132 | vfs_inode_get_name( XPTR( parent_cxy , parent_inode_ptr ) , parent_name ); |
---|
[459] | 1133 | if( DEBUG_VFS_CLOSE < cycle ) |
---|
[625] | 1134 | printk("\n[%s] thread[%x,%x] updated <%s> in <%s> / size = %d bytes\n", |
---|
| 1135 | __FUNCTION__, process->pid, this->trdid, name, parent_name, size ); |
---|
[459] | 1136 | #endif |
---|
[23] | 1137 | |
---|
[623] | 1138 | // copy all dirty pages from parent mapper to device |
---|
| 1139 | if( parent_cxy == local_cxy ) |
---|
| 1140 | { |
---|
| 1141 | error = mapper_sync( parent_mapper_ptr ); |
---|
| 1142 | } |
---|
| 1143 | else |
---|
| 1144 | { |
---|
| 1145 | rpc_mapper_sync_client( parent_cxy, |
---|
| 1146 | parent_mapper_ptr, |
---|
| 1147 | &error ); |
---|
| 1148 | } |
---|
[459] | 1149 | |
---|
[623] | 1150 | if( error ) |
---|
| 1151 | { |
---|
| 1152 | printk("\n[ERROR] in %s : cannot synchronise parent mapper to device\n", |
---|
| 1153 | __FUNCTION__ ); |
---|
| 1154 | return -1; |
---|
| 1155 | } |
---|
[459] | 1156 | |
---|
[623] | 1157 | #if DEBUG_VFS_CLOSE |
---|
| 1158 | if( DEBUG_VFS_CLOSE < cycle ) |
---|
| 1159 | printk("\n[%s] thread[%x,%x] synchonized mapper of parent <%s> to device\n", |
---|
| 1160 | __FUNCTION__, process->pid, this->trdid, parent_name ); |
---|
| 1161 | #endif |
---|
[459] | 1162 | |
---|
[623] | 1163 | } |
---|
| 1164 | |
---|
| 1165 | //////// 3) loop on the process copies to reset all fd_array[file_id] entries |
---|
| 1166 | |
---|
| 1167 | // get owner process cluster and lpid |
---|
| 1168 | pid = process->pid; |
---|
| 1169 | owner_cxy = CXY_FROM_PID( pid ); |
---|
| 1170 | lpid = LPID_FROM_PID( pid ); |
---|
| 1171 | |
---|
| 1172 | // get extended pointers on copies root and lock |
---|
| 1173 | root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] ); |
---|
| 1174 | lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] ); |
---|
| 1175 | |
---|
| 1176 | // take the lock protecting the list of copies |
---|
| 1177 | remote_queuelock_acquire( lock_xp ); |
---|
| 1178 | |
---|
| 1179 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 1180 | { |
---|
| 1181 | xptr_t process_xp = XLIST_ELEMENT( iter_xp , process_t , copies_list ); |
---|
| 1182 | cxy_t process_cxy = GET_CXY( process_xp ); |
---|
| 1183 | process_t * process_ptr = GET_PTR( process_xp ); |
---|
| 1184 | |
---|
| 1185 | xptr_t entry_xp = XPTR( process_cxy , &process_ptr->fd_array.array[file_id] ); |
---|
[568] | 1186 | hal_remote_s64( entry_xp , XPTR_NULL ); |
---|
[459] | 1187 | vfs_file_count_down( file_xp ); |
---|
[124] | 1188 | hal_fence(); |
---|
[23] | 1189 | } |
---|
| 1190 | |
---|
[459] | 1191 | // release the lock protecting the list of copies |
---|
[568] | 1192 | remote_queuelock_release( lock_xp ); |
---|
[459] | 1193 | |
---|
[623] | 1194 | #if DEBUG_VFS_CLOSE |
---|
[459] | 1195 | if( DEBUG_VFS_CLOSE < cycle ) |
---|
[625] | 1196 | printk("\n[%s] thread[%x,%x] reset all fd-array copies for <%s>\n", |
---|
[623] | 1197 | __FUNCTION__, process->pid, this->trdid, name ); |
---|
[459] | 1198 | #endif |
---|
| 1199 | |
---|
[623] | 1200 | //////// 4) release memory allocated to file descriptor in remote cluster |
---|
[459] | 1201 | |
---|
[23] | 1202 | if( file_cxy == local_cxy ) // file cluster is local |
---|
[1] | 1203 | { |
---|
[23] | 1204 | vfs_file_destroy( file_ptr ); |
---|
| 1205 | } |
---|
| 1206 | else // file cluster is local |
---|
| 1207 | { |
---|
| 1208 | rpc_vfs_file_destroy_client( file_cxy , file_ptr ); |
---|
| 1209 | } |
---|
[1] | 1210 | |
---|
[459] | 1211 | #if DEBUG_VFS_CLOSE |
---|
| 1212 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 1213 | if( DEBUG_VFS_CLOSE < cycle ) |
---|
[625] | 1214 | printk("\n[%s] thread[%x,%x] exit / closed <%s> in process %x / cycle %d\n", |
---|
| 1215 | __FUNCTION__, process->pid, this->trdid, name, process->pid, cycle ); |
---|
[459] | 1216 | #endif |
---|
| 1217 | |
---|
[23] | 1218 | return 0; |
---|
[1] | 1219 | |
---|
[23] | 1220 | } // end vfs_close() |
---|
[1] | 1221 | |
---|
| 1222 | //////////////////////////////////// |
---|
[610] | 1223 | error_t vfs_mkdir( xptr_t root_xp, |
---|
| 1224 | char * path, |
---|
| 1225 | uint32_t rights ) |
---|
| 1226 | { |
---|
| 1227 | error_t error; |
---|
| 1228 | xptr_t vfs_root_xp; // extended pointer on VFS root inode |
---|
| 1229 | vfs_inode_t * vfs_root_ptr; // local pointer on VFS root inode |
---|
| 1230 | cxy_t vfs_root_cxy; // VFS root inode cluster identifier |
---|
| 1231 | xptr_t lock_xp; // extended pointer on lock protecting Inode Tree |
---|
[611] | 1232 | xptr_t inode_xp; // extended pointer on new directory inode |
---|
| 1233 | vfs_inode_t * inode_ptr; // local pointer on new directory inode |
---|
| 1234 | cxy_t inode_cxy; // new directory inode cluster identifier |
---|
[610] | 1235 | xptr_t dentry_xp; // extended pointer on new dentry |
---|
[611] | 1236 | vfs_dentry_t * dentry_ptr; // new dentry local pointer |
---|
| 1237 | xptr_t parent_xp; // extended pointer on parent inode |
---|
| 1238 | vfs_inode_t * parent_ptr; // local pointer on parent inode |
---|
| 1239 | cxy_t parent_cxy; // parent inode cluster identifier |
---|
| 1240 | vfs_ctx_t * parent_ctx_ptr; // local pointer on parent inode context |
---|
| 1241 | uint32_t parent_fs_type; // parent inode file system type |
---|
[610] | 1242 | |
---|
| 1243 | xptr_t parents_root_xp; // extended pointer on parents field in inode (root) |
---|
| 1244 | xptr_t parents_entry_xp; // extended pointer on parents field in dentry |
---|
| 1245 | xptr_t children_xhtab_xp; // extended pointer on children field in inode (root) |
---|
| 1246 | xptr_t children_entry_xp; // extended pointer on children field in dentry |
---|
| 1247 | |
---|
| 1248 | char last_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1249 | |
---|
| 1250 | thread_t * this = CURRENT_THREAD; |
---|
| 1251 | process_t * process = this->process; |
---|
| 1252 | |
---|
| 1253 | #if DEBUG_VFS_MKDIR |
---|
| 1254 | char root_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1255 | vfs_inode_get_name( root_xp , root_name ); |
---|
| 1256 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1257 | if( DEBUG_VFS_MKDIR < cycle ) |
---|
| 1258 | printk("\n[%s] thread[%x,%x] enter / root <%s> / path <%s> / cycle %d\n", |
---|
| 1259 | __FUNCTION__, process->pid, this->trdid, root_name, path, cycle ); |
---|
| 1260 | #endif |
---|
| 1261 | |
---|
| 1262 | // build extended pointer on lock protecting Inode Tree (in VFS root inode) |
---|
| 1263 | vfs_root_xp = process->vfs_root_xp; |
---|
| 1264 | vfs_root_ptr = GET_PTR( vfs_root_xp ); |
---|
| 1265 | vfs_root_cxy = GET_CXY( vfs_root_xp ); |
---|
| 1266 | lock_xp = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock ); |
---|
| 1267 | |
---|
| 1268 | // take the lock protecting Inode Tree in write mode |
---|
| 1269 | remote_rwlock_wr_acquire( lock_xp ); |
---|
| 1270 | |
---|
| 1271 | // 1. get pointers on parent inode |
---|
| 1272 | error = vfs_lookup( root_xp, |
---|
| 1273 | path, |
---|
| 1274 | VFS_LOOKUP_DIR | VFS_LOOKUP_PARENT, |
---|
| 1275 | &parent_xp, |
---|
| 1276 | last_name ); |
---|
| 1277 | if( error ) |
---|
| 1278 | { |
---|
| 1279 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1280 | printk("\n[ERROR] in %s : cannot get parent inode for <%s>\n", |
---|
| 1281 | __FUNCTION__, path ); |
---|
| 1282 | return -1; |
---|
| 1283 | } |
---|
| 1284 | |
---|
| 1285 | // get parent inode cluster and local pointer |
---|
| 1286 | parent_cxy = GET_CXY( parent_xp ); |
---|
| 1287 | parent_ptr = GET_PTR( parent_xp ); |
---|
| 1288 | |
---|
| 1289 | #if( DEBUG_VFS_MKDIR & 1 ) |
---|
| 1290 | if( DEBUG_VFS_MKDIR < cycle ) |
---|
| 1291 | printk("\n[%s] thread[%x,%x] get parent inode (%x,%x) for <%s>\n", |
---|
| 1292 | __FUNCTION__, process->pid, this->trdid, parent_cxy, parent_ptr, path ); |
---|
| 1293 | #endif |
---|
| 1294 | |
---|
| 1295 | // get parent inode context, and FS type |
---|
| 1296 | parent_ctx_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->ctx ) ); |
---|
| 1297 | parent_fs_type = hal_remote_l32( XPTR( parent_cxy , &parent_ctx_ptr->type ) ); |
---|
| 1298 | |
---|
| 1299 | // 2. create one new dentry in parent cluster |
---|
| 1300 | if( parent_cxy == local_cxy ) |
---|
| 1301 | { |
---|
| 1302 | error = vfs_dentry_create( parent_fs_type, |
---|
| 1303 | last_name, |
---|
| 1304 | &dentry_xp ); |
---|
| 1305 | } |
---|
| 1306 | else |
---|
| 1307 | { |
---|
| 1308 | rpc_vfs_dentry_create_client( parent_cxy, |
---|
| 1309 | parent_fs_type, |
---|
| 1310 | last_name, |
---|
| 1311 | &dentry_xp, |
---|
| 1312 | &error ); |
---|
| 1313 | } |
---|
| 1314 | |
---|
| 1315 | if( error ) |
---|
| 1316 | { |
---|
| 1317 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1318 | printk("\n[ERROR] in %s : cannot create new dentry in cluster %x for <%s>\n", |
---|
| 1319 | __FUNCTION__, parent_cxy, path ); |
---|
| 1320 | return -1; |
---|
| 1321 | } |
---|
| 1322 | |
---|
| 1323 | // get local pointer on dentry |
---|
| 1324 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
| 1325 | |
---|
| 1326 | #if( DEBUG_VFS_MKDIR & 1 ) |
---|
| 1327 | if( DEBUG_VFS_MKDIR < cycle ) |
---|
| 1328 | printk("\n[%s] thread[%x,%x] created new dentry (%x,%x) for <%s>\n", |
---|
| 1329 | __FUNCTION__, process->pid, this->trdid, parent_cxy, dentry_ptr, path ); |
---|
| 1330 | #endif |
---|
| 1331 | |
---|
[611] | 1332 | // 3. create new directory inode |
---|
[610] | 1333 | // TODO : define attr / uid / gid |
---|
| 1334 | uint32_t attr = 0; |
---|
| 1335 | uint32_t uid = 0; |
---|
| 1336 | uint32_t gid = 0; |
---|
| 1337 | |
---|
| 1338 | // select a target cluster for new inode |
---|
| 1339 | inode_cxy = cluster_random_select(); |
---|
| 1340 | |
---|
[611] | 1341 | if( inode_cxy == local_cxy ) // target cluster is local |
---|
[610] | 1342 | { |
---|
| 1343 | error = vfs_inode_create( parent_fs_type, |
---|
| 1344 | attr, |
---|
| 1345 | rights, |
---|
| 1346 | uid, |
---|
| 1347 | gid, |
---|
| 1348 | &inode_xp ); |
---|
| 1349 | } |
---|
[611] | 1350 | else // target cluster is remote |
---|
[610] | 1351 | { |
---|
| 1352 | rpc_vfs_inode_create_client( inode_cxy, |
---|
| 1353 | parent_fs_type, |
---|
| 1354 | attr, |
---|
| 1355 | rights, |
---|
| 1356 | uid, |
---|
| 1357 | gid, |
---|
| 1358 | &inode_xp, |
---|
| 1359 | &error ); |
---|
| 1360 | } |
---|
| 1361 | |
---|
| 1362 | if( error ) |
---|
| 1363 | { |
---|
[611] | 1364 | remote_rwlock_wr_release( lock_xp ); |
---|
[610] | 1365 | printk("\n[ERROR] in %s : cannot create new inode in cluster %x for <%s>\n", |
---|
| 1366 | __FUNCTION__ , inode_cxy , path ); |
---|
| 1367 | if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry_ptr ); |
---|
| 1368 | else rpc_vfs_dentry_destroy_client( parent_cxy , dentry_ptr ); |
---|
| 1369 | return -1; |
---|
| 1370 | } |
---|
| 1371 | |
---|
| 1372 | // get new inode local pointer |
---|
| 1373 | inode_ptr = GET_PTR( inode_xp ); |
---|
[623] | 1374 | |
---|
| 1375 | // update inode "type" field |
---|
| 1376 | hal_remote_s32( XPTR( inode_cxy , &inode_ptr->type ) , INODE_TYPE_DIR ); |
---|
[610] | 1377 | |
---|
| 1378 | #if(DEBUG_VFS_MKDIR & 1) |
---|
| 1379 | if( DEBUG_VFS_MKDIR < cycle ) |
---|
| 1380 | printk("\n[%s] thread[%x,%x] created new inode (%x,%x) for <%s>\n", |
---|
| 1381 | __FUNCTION__ , process->pid, this->trdid, inode_cxy, inode_ptr, path ); |
---|
| 1382 | #endif |
---|
| 1383 | |
---|
| 1384 | // 4. register dentry in new inode list of parents |
---|
| 1385 | parents_root_xp = XPTR( inode_cxy , &inode_ptr->parents ); |
---|
| 1386 | parents_entry_xp = XPTR( parent_cxy , &dentry_ptr->parents ); |
---|
| 1387 | xlist_add_first( parents_root_xp , parents_entry_xp ); |
---|
| 1388 | hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->links ) , 1 ); |
---|
| 1389 | |
---|
| 1390 | // 5. register dentry in parent inode |
---|
| 1391 | children_xhtab_xp = XPTR( parent_cxy , &parent_ptr->children ); |
---|
| 1392 | children_entry_xp = XPTR( parent_cxy , &dentry_ptr->children ); |
---|
| 1393 | xhtab_insert( children_xhtab_xp , last_name , children_entry_xp ); |
---|
| 1394 | |
---|
| 1395 | // 6. update "parent" and "child_xp" fields in dentry |
---|
| 1396 | hal_remote_s64( XPTR( parent_cxy , &dentry_ptr->child_xp ) , inode_xp ); |
---|
| 1397 | hal_remote_spt( XPTR( parent_cxy , &dentry_ptr->parent ) , parent_ptr ); |
---|
| 1398 | |
---|
| 1399 | #if(DEBUG_VFS_MKDIR & 1) |
---|
| 1400 | if( DEBUG_VFS_MKDIR < cycle ) |
---|
| 1401 | printk("\n[%s] thread[%x,%x] updated Inode Tree for <%s>\n", |
---|
| 1402 | __FUNCTION__, process->pid, this->trdid, path ); |
---|
| 1403 | #endif |
---|
| 1404 | |
---|
[611] | 1405 | // 7. create the two special dentries <.> and <..> in new directory |
---|
| 1406 | // both the new directory mapper, and the Inode Tree are updated |
---|
| 1407 | error = vfs_add_special_dentries( inode_xp, |
---|
| 1408 | parent_xp ); |
---|
| 1409 | |
---|
| 1410 | if( error ) |
---|
| 1411 | { |
---|
| 1412 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1413 | printk("\n[ERROR] in %s : cannot create new inode in cluster %x for <%s>\n", |
---|
| 1414 | __FUNCTION__ , inode_cxy , path ); |
---|
| 1415 | if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry_ptr ); |
---|
| 1416 | else rpc_vfs_dentry_destroy_client( parent_cxy , dentry_ptr ); |
---|
| 1417 | return -1; |
---|
| 1418 | } |
---|
| 1419 | |
---|
[610] | 1420 | // release the lock protecting Inode Tree |
---|
| 1421 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1422 | |
---|
[611] | 1423 | // 8. update parent directory mapper |
---|
[610] | 1424 | // and synchronize the parent directory on IOC device |
---|
| 1425 | if (parent_cxy == local_cxy) |
---|
| 1426 | { |
---|
| 1427 | error = vfs_fs_add_dentry( parent_ptr, |
---|
| 1428 | dentry_ptr ); |
---|
| 1429 | } |
---|
| 1430 | else |
---|
| 1431 | { |
---|
| 1432 | rpc_vfs_fs_add_dentry_client( parent_cxy, |
---|
| 1433 | parent_ptr, |
---|
| 1434 | dentry_ptr, |
---|
| 1435 | &error ); |
---|
| 1436 | } |
---|
| 1437 | |
---|
| 1438 | if( error ) |
---|
| 1439 | { |
---|
| 1440 | printk("\n[ERROR] in %s : cannot update parent directory for <%s>\n", |
---|
| 1441 | __FUNCTION__, path ); |
---|
| 1442 | return -1; |
---|
| 1443 | } |
---|
| 1444 | |
---|
| 1445 | #if(DEBUG_VFS_MKDIR & 1) |
---|
| 1446 | if( DEBUG_VFS_MKDIR < cycle ) |
---|
| 1447 | printk("\n[%s] thread[%x,%x] updated parent dir (mapper and IOC) for <%s>\n", |
---|
| 1448 | __FUNCTION__, process->pid, this->trdid, path ); |
---|
| 1449 | #endif |
---|
| 1450 | |
---|
| 1451 | return 0; |
---|
| 1452 | |
---|
| 1453 | } // end vfs_mkdir() |
---|
| 1454 | |
---|
| 1455 | /////////////////////////////////////// |
---|
| 1456 | error_t vfs_link( xptr_t old_root_xp, |
---|
| 1457 | char * old_path, |
---|
| 1458 | xptr_t new_root_xp, |
---|
| 1459 | char * new_path ) |
---|
| 1460 | { |
---|
| 1461 | error_t error; |
---|
| 1462 | xptr_t vfs_root_xp; // extended pointer on VFS root inode |
---|
| 1463 | vfs_inode_t * vfs_root_ptr; // local pointer on VFS root inode |
---|
| 1464 | cxy_t vfs_root_cxy; // VFS root inode cluster identifier |
---|
| 1465 | xptr_t lock_xp; // extended pointer on lock protecting Inode Tree |
---|
| 1466 | xptr_t inode_xp; // extended pointer on target inode |
---|
| 1467 | vfs_inode_t * inode_ptr; // local pointer on target inode |
---|
| 1468 | cxy_t inode_cxy; // target inode cluster identifier |
---|
| 1469 | uint32_t inode_type; // target inode type |
---|
| 1470 | vfs_ctx_t * inode_ctx_ptr; // local pointer on target inode context |
---|
| 1471 | uint32_t inode_fs_type; // target inode file system type |
---|
| 1472 | xptr_t dentry_xp; // extended pointer on new dentry |
---|
| 1473 | vfs_dentry_t * dentry_ptr; // target dentry local pointer |
---|
| 1474 | xptr_t new_parent_xp; // extended pointer on new parent inode |
---|
| 1475 | vfs_inode_t * new_parent_ptr; // local pointer on new parent inode |
---|
| 1476 | cxy_t new_parent_cxy; // new parent inode cluster identifier |
---|
| 1477 | |
---|
| 1478 | xptr_t parents_root_xp; // extended pointer on parents field in inode (root) |
---|
| 1479 | xptr_t parents_entry_xp; // extended pointer on parents field in dentry |
---|
| 1480 | xptr_t children_xhtab_xp; // extended pointer on children field in inode (root) |
---|
| 1481 | xptr_t children_entry_xp; // extended pointer on children field in dentry |
---|
| 1482 | |
---|
| 1483 | char new_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1484 | |
---|
| 1485 | thread_t * this = CURRENT_THREAD; |
---|
| 1486 | process_t * process = this->process; |
---|
| 1487 | |
---|
| 1488 | #if DEBUG_VFS_LINK |
---|
| 1489 | char old_root_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1490 | char new_root_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1491 | vfs_inode_get_name( old_root_xp , old_root_name ); |
---|
| 1492 | vfs_inode_get_name( new_root_xp , new_root_name ); |
---|
| 1493 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1494 | if( DEBUG_VFS_LINK < cycle ) |
---|
| 1495 | printk("\n[%s] thread[%x,%x] enter / old_root <%s> / old_path <%s> / " |
---|
| 1496 | "new_root <%s> / new_path <%s> / cycle %d\n", |
---|
| 1497 | __FUNCTION__, process->pid, this->trdid, |
---|
| 1498 | old_root_name, old_path, new_root_name, new_path, cycle ); |
---|
| 1499 | #endif |
---|
| 1500 | |
---|
| 1501 | // build extended pointer on lock protecting Inode Tree (in VFS root inode) |
---|
| 1502 | vfs_root_xp = process->vfs_root_xp; |
---|
| 1503 | vfs_root_ptr = GET_PTR( vfs_root_xp ); |
---|
| 1504 | vfs_root_cxy = GET_CXY( vfs_root_xp ); |
---|
| 1505 | lock_xp = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock ); |
---|
| 1506 | |
---|
| 1507 | // take the lock protecting Inode Tree in write mode |
---|
| 1508 | remote_rwlock_wr_acquire( lock_xp ); |
---|
| 1509 | |
---|
| 1510 | // get extended pointer on target inode |
---|
| 1511 | error = vfs_lookup( old_root_xp, |
---|
| 1512 | old_path, |
---|
| 1513 | 0, |
---|
| 1514 | &inode_xp, |
---|
| 1515 | NULL ); |
---|
| 1516 | if( error ) |
---|
| 1517 | { |
---|
| 1518 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1519 | printk("\n[ERROR] in %s : cannot get target inode for <%s>\n", |
---|
| 1520 | __FUNCTION__, old_path ); |
---|
| 1521 | return -1; |
---|
| 1522 | } |
---|
| 1523 | |
---|
| 1524 | #if( DEBUG_VFS_LINK & 1 ) |
---|
| 1525 | if( DEBUG_VFS_LINK < cycle ) |
---|
| 1526 | printk("\n[%s] thread[%x,%x] get child inode (%x,%x) for <%s>\n", |
---|
| 1527 | __FUNCTION__, process->pid, this->trdid, |
---|
| 1528 | GET_CXY(inode_xp), GET_PTR(inode_xp), old_path, cycle ); |
---|
| 1529 | #endif |
---|
| 1530 | |
---|
| 1531 | // get extended pointer on parent inode in new path |
---|
| 1532 | error = vfs_lookup( new_root_xp, |
---|
| 1533 | new_path, |
---|
| 1534 | VFS_LOOKUP_PARENT, |
---|
| 1535 | &new_parent_xp, |
---|
| 1536 | new_name ); |
---|
| 1537 | if( error ) |
---|
| 1538 | { |
---|
| 1539 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1540 | printk("\n[ERROR] in %s : cannot get parent inode for <%s>\n", |
---|
| 1541 | __FUNCTION__, new_path ); |
---|
| 1542 | return -1; |
---|
| 1543 | } |
---|
| 1544 | |
---|
| 1545 | #if( DEBUG_VFS_LINK & 1 ) |
---|
| 1546 | if( DEBUG_VFS_LINK < cycle ) |
---|
| 1547 | printk("\n[%s] thread[%x,%x] get parent inode (%x,%x) for <%s>\n", |
---|
| 1548 | __FUNCTION__, process->pid, this->trdid, |
---|
| 1549 | GET_CXY(new_parent_xp), GET_PTR(new_parent_xp), new_path ); |
---|
| 1550 | #endif |
---|
| 1551 | |
---|
| 1552 | // get target inode cluster and local pointer |
---|
| 1553 | inode_cxy = GET_CXY( inode_xp ); |
---|
| 1554 | inode_ptr = GET_PTR( inode_xp ); |
---|
| 1555 | |
---|
| 1556 | // get target inode type, context, and FS type |
---|
| 1557 | inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
| 1558 | inode_ctx_ptr = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->ctx ) ); |
---|
| 1559 | inode_fs_type = hal_remote_l32( XPTR( inode_cxy , &inode_ctx_ptr->type ) ); |
---|
| 1560 | |
---|
| 1561 | // get new parent inode cluster an local pointer |
---|
| 1562 | new_parent_ptr = GET_PTR( new_parent_xp ); |
---|
| 1563 | new_parent_cxy = GET_CXY( new_parent_xp ); |
---|
| 1564 | |
---|
| 1565 | /////////////////////////////////////////////////////////////////////// |
---|
| 1566 | if( (inode_type == INODE_TYPE_FILE) || (inode_type == INODE_TYPE_DIR) ) |
---|
| 1567 | { |
---|
| 1568 | // 1. create one new dentry |
---|
| 1569 | if( new_parent_cxy == local_cxy ) |
---|
| 1570 | { |
---|
| 1571 | error = vfs_dentry_create( inode_fs_type, |
---|
| 1572 | new_name, |
---|
| 1573 | &dentry_xp ); |
---|
| 1574 | } |
---|
| 1575 | else |
---|
| 1576 | { |
---|
| 1577 | rpc_vfs_dentry_create_client( new_parent_cxy, |
---|
| 1578 | inode_fs_type, |
---|
| 1579 | new_name, |
---|
| 1580 | &dentry_xp, |
---|
| 1581 | &error ); |
---|
| 1582 | } |
---|
| 1583 | |
---|
| 1584 | if( error ) |
---|
| 1585 | { |
---|
| 1586 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1587 | printk("\n[ERROR] in %s : cannot create new dentry for <%s>\n", |
---|
| 1588 | __FUNCTION__, new_path ); |
---|
| 1589 | return -1; |
---|
| 1590 | } |
---|
| 1591 | |
---|
| 1592 | // get local pointer on dentry |
---|
| 1593 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
| 1594 | |
---|
| 1595 | // 2. register dentry in target inode |
---|
| 1596 | parents_root_xp = XPTR( inode_cxy , &inode_ptr->parents ); |
---|
| 1597 | parents_entry_xp = XPTR( new_parent_cxy , &dentry_ptr->parents ); |
---|
| 1598 | xlist_add_first( parents_root_xp , parents_entry_xp ); |
---|
| 1599 | hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->links ) , 1 ); |
---|
| 1600 | |
---|
| 1601 | // 3. register dentry in parent inode |
---|
| 1602 | children_xhtab_xp = XPTR( new_parent_cxy , &new_parent_ptr->children ); |
---|
| 1603 | children_entry_xp = XPTR( new_parent_cxy , &dentry_ptr->children ); |
---|
| 1604 | xhtab_insert( children_xhtab_xp , new_name , children_entry_xp ); |
---|
| 1605 | |
---|
| 1606 | // 4. update "parent" and "child_xp" fields in dentry |
---|
| 1607 | hal_remote_s64( XPTR( new_parent_cxy , &dentry_ptr->child_xp ) , inode_xp ); |
---|
| 1608 | hal_remote_spt( XPTR( new_parent_cxy , &dentry_ptr->parent ) , new_parent_ptr ); |
---|
| 1609 | |
---|
| 1610 | #if(DEBUG_VFS_LINK & 1) |
---|
| 1611 | if( DEBUG_VFS_LINK < cycle ) |
---|
| 1612 | printk("\n[%s] thread[%x,%x] updated Inode Tree / old <%s> / new <%s>\n", |
---|
| 1613 | __FUNCTION__, process->pid, this->trdid, old_path, new_path ); |
---|
| 1614 | vfs_display( new_parent_xp ); |
---|
| 1615 | #endif |
---|
| 1616 | |
---|
| 1617 | // release the lock protecting Inode Tree |
---|
| 1618 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1619 | |
---|
| 1620 | // 5. update new parent directory mapper in Inode Tree |
---|
| 1621 | // and synchronize the parent directory on IOC device |
---|
| 1622 | if (new_parent_cxy == local_cxy) |
---|
| 1623 | { |
---|
| 1624 | error = vfs_fs_add_dentry( new_parent_ptr, |
---|
| 1625 | dentry_ptr ); |
---|
| 1626 | } |
---|
| 1627 | else |
---|
| 1628 | { |
---|
| 1629 | rpc_vfs_fs_add_dentry_client( new_parent_cxy, |
---|
| 1630 | new_parent_ptr, |
---|
| 1631 | dentry_ptr, |
---|
| 1632 | &error ); |
---|
| 1633 | } |
---|
| 1634 | if( error ) |
---|
| 1635 | { |
---|
| 1636 | printk("\n[ERROR] in %s : cannot update new parent directory for <%s>\n", |
---|
| 1637 | __FUNCTION__, new_path ); |
---|
| 1638 | return -1; |
---|
| 1639 | } |
---|
| 1640 | |
---|
| 1641 | #if(DEBUG_VFS_LINK & 1) |
---|
| 1642 | if( DEBUG_VFS_LINK < cycle ) |
---|
| 1643 | printk("\n[%s] thread[%x,%x] updated new parent dir (mapper and IOC) / old <%s> / new <%s>\n", |
---|
| 1644 | __FUNCTION__, process->pid, this->trdid, old_path, new_path ); |
---|
| 1645 | #endif |
---|
| 1646 | return 0; |
---|
| 1647 | } |
---|
| 1648 | else |
---|
| 1649 | { |
---|
| 1650 | // release the lock protecting Inode Tree |
---|
| 1651 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1652 | |
---|
| 1653 | printk("\n[ERROR] in %s : unsupported inode type %s\n", |
---|
| 1654 | __FUNCTION__ , vfs_inode_type_str( inode_type ) ); |
---|
| 1655 | return -1; |
---|
| 1656 | } |
---|
| 1657 | |
---|
| 1658 | } // end vfs_link() |
---|
| 1659 | |
---|
| 1660 | ///////////////////////////////////// |
---|
| 1661 | error_t vfs_unlink( xptr_t root_xp, |
---|
[23] | 1662 | char * path ) |
---|
[1] | 1663 | { |
---|
[602] | 1664 | error_t error; |
---|
[610] | 1665 | xptr_t vfs_root_xp; // extended pointer on VFS root inode |
---|
| 1666 | vfs_inode_t * vfs_root_ptr; // local_pointer on VFS root inode |
---|
| 1667 | cxy_t vfs_root_cxy; // VFS root inode cluster identifier |
---|
| 1668 | xptr_t lock_xp; // extended pointer on lock protecting Inode Tree |
---|
| 1669 | xptr_t parent_xp; // extended pointer on target inode |
---|
| 1670 | cxy_t parent_cxy; // target inode cluster identifier |
---|
| 1671 | vfs_inode_t * parent_ptr; // target inode local pointer |
---|
[602] | 1672 | xptr_t inode_xp; // extended pointer on target inode |
---|
| 1673 | cxy_t inode_cxy; // target inode cluster identifier |
---|
| 1674 | vfs_inode_t * inode_ptr; // target inode local pointer |
---|
[610] | 1675 | uint32_t inode_links; // target inode links count |
---|
| 1676 | vfs_inode_type_t inode_type; // target inode type |
---|
| 1677 | uint32_t inode_children; // target inode number of children |
---|
| 1678 | xptr_t dentry_xp; // extended pointer on dentry to unlink |
---|
| 1679 | vfs_dentry_t * dentry_ptr; // local pointer on dentry to unlink |
---|
[623] | 1680 | vfs_ctx_t * ctx_ptr; // local pointer on FS context |
---|
| 1681 | vfs_fs_type_t fs_type; // File system type |
---|
[1] | 1682 | |
---|
[610] | 1683 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; // name of link to remove |
---|
| 1684 | |
---|
| 1685 | thread_t * this = CURRENT_THREAD; |
---|
| 1686 | process_t * process = this->process; |
---|
| 1687 | |
---|
[602] | 1688 | #if DEBUG_VFS_UNLINK |
---|
| 1689 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[610] | 1690 | char root_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1691 | vfs_inode_get_name( root_xp , root_name ); |
---|
[602] | 1692 | if( DEBUG_VFS_UNLINK < cycle ) |
---|
[623] | 1693 | printk("\n[%s] thread[%x,%x] : enter for root <%s> / path <%s> / cycle %d\n", |
---|
[610] | 1694 | __FUNCTION__, process->pid, this->trdid, root_name, path, cycle ); |
---|
[602] | 1695 | #endif |
---|
| 1696 | |
---|
[610] | 1697 | // build extended pointer on lock protecting Inode Tree (in VFS root inode) |
---|
| 1698 | vfs_root_xp = process->vfs_root_xp; |
---|
[633] | 1699 | vfs_root_ptr = GET_PTR( vfs_root_xp ); |
---|
| 1700 | vfs_root_cxy = GET_CXY( vfs_root_xp ); |
---|
[610] | 1701 | lock_xp = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock ); |
---|
[602] | 1702 | |
---|
[610] | 1703 | // take the lock protecting Inode Tree |
---|
| 1704 | remote_rwlock_wr_acquire( lock_xp ); |
---|
[602] | 1705 | |
---|
[610] | 1706 | // get extended pointer on parent inode |
---|
| 1707 | error = vfs_lookup( root_xp, |
---|
| 1708 | path, |
---|
| 1709 | VFS_LOOKUP_PARENT, |
---|
| 1710 | &parent_xp, |
---|
| 1711 | name ); |
---|
| 1712 | if( error ) |
---|
| 1713 | { |
---|
| 1714 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1715 | printk("\n[ERROR] in %s : cannot get parent inode for <%s> in <%s>\n", |
---|
| 1716 | __FUNCTION__, name, path ); |
---|
| 1717 | return -1; |
---|
| 1718 | } |
---|
| 1719 | |
---|
| 1720 | // get parent inode cluster and local pointer |
---|
| 1721 | parent_cxy = GET_CXY( parent_xp ); |
---|
| 1722 | parent_ptr = GET_PTR( parent_xp ); |
---|
| 1723 | |
---|
| 1724 | #if( DEBUG_VFS_UNLINK & 1 ) |
---|
| 1725 | char parent_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1726 | vfs_inode_get_name( parent_xp , parent_name ); |
---|
| 1727 | if( DEBUG_VFS_UNLINK < cycle ) |
---|
[623] | 1728 | printk("\n[%s] thread[%x,%x] : parent inode <%s> is (%x,%x)\n", |
---|
[610] | 1729 | __FUNCTION__, process->pid, this->trdid, parent_name, parent_cxy, parent_ptr ); |
---|
| 1730 | #endif |
---|
| 1731 | |
---|
| 1732 | // build extended pointer on parent inode "children" xhtab |
---|
| 1733 | xptr_t children_xp = XPTR( parent_cxy , &parent_ptr->children ); |
---|
| 1734 | |
---|
[623] | 1735 | // try to get extended pointer on dentry from Inode Tree |
---|
[610] | 1736 | dentry_xp = xhtab_lookup( children_xp , name ); |
---|
| 1737 | |
---|
[623] | 1738 | // when dentry not found in Inode Tree, try to get it from inode tree |
---|
| 1739 | |
---|
| 1740 | if( dentry_xp == XPTR_NULL ) // miss target dentry in Inode Tree |
---|
[610] | 1741 | { |
---|
| 1742 | |
---|
| 1743 | #if( DEBUG_VFS_UNLINK & 1 ) |
---|
| 1744 | if( DEBUG_VFS_UNLINK < cycle ) |
---|
[623] | 1745 | printk("\n[%s] thread[%x,%x] : inode <%s> not found => scan parent mapper\n", |
---|
| 1746 | __FUNCTION__, process->pid, this->trdid, name ); |
---|
[610] | 1747 | #endif |
---|
[623] | 1748 | // get parent inode FS type |
---|
| 1749 | ctx_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->ctx ) ); |
---|
| 1750 | fs_type = hal_remote_l32( XPTR( parent_cxy , &ctx_ptr->type ) ); |
---|
[610] | 1751 | |
---|
[623] | 1752 | // select a cluster for new inode |
---|
| 1753 | inode_cxy = cluster_random_select(); |
---|
| 1754 | |
---|
| 1755 | // speculatively insert a new child dentry/inode couple in inode tree |
---|
| 1756 | error = vfs_add_child_in_parent( inode_cxy, |
---|
| 1757 | fs_type, |
---|
| 1758 | parent_xp, |
---|
| 1759 | name, |
---|
| 1760 | &dentry_xp, |
---|
| 1761 | &inode_xp ); |
---|
| 1762 | if( error ) |
---|
| 1763 | { |
---|
| 1764 | printk("\n[ERROR] in %s : cannot create inode <%s> in path <%s>\n", |
---|
| 1765 | __FUNCTION__ , name, path ); |
---|
| 1766 | |
---|
| 1767 | vfs_remove_child_from_parent( dentry_xp ); |
---|
| 1768 | return -1; |
---|
| 1769 | } |
---|
| 1770 | |
---|
| 1771 | // get local pointers on new dentry and new inode descriptors |
---|
| 1772 | inode_ptr = GET_PTR( inode_xp ); |
---|
| 1773 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
| 1774 | |
---|
| 1775 | // scan parent mapper to find the missing dentry, and complete |
---|
| 1776 | // initialisation of new dentry and new inode descriptors In Inode Tree |
---|
| 1777 | if( parent_cxy == local_cxy ) |
---|
| 1778 | { |
---|
| 1779 | error = vfs_fs_new_dentry( parent_ptr, |
---|
| 1780 | name, |
---|
| 1781 | inode_xp ); |
---|
| 1782 | } |
---|
| 1783 | else |
---|
| 1784 | { |
---|
| 1785 | rpc_vfs_fs_new_dentry_client( parent_cxy, |
---|
| 1786 | parent_ptr, |
---|
| 1787 | name, |
---|
| 1788 | inode_xp, |
---|
| 1789 | &error ); |
---|
| 1790 | } |
---|
| 1791 | |
---|
| 1792 | if ( error ) // dentry not found in parent mapper |
---|
| 1793 | { |
---|
| 1794 | printk("\n[ERROR] in %s : cannot get dentry <%s> in path <%s>\n", |
---|
| 1795 | __FUNCTION__ , name, path ); |
---|
| 1796 | return -1; |
---|
| 1797 | } |
---|
| 1798 | |
---|
| 1799 | #if (DEBUG_VFS_UNLINK & 1) |
---|
| 1800 | if( DEBUG_VFS_UNLINK < cycle ) |
---|
| 1801 | printk("\n[%s] thread[%x,%x] : created missing inode & dentry <%s> in cluster %x\n", |
---|
| 1802 | __FUNCTION__, process->pid, this->trdid, name, inode_cxy ); |
---|
| 1803 | #endif |
---|
| 1804 | |
---|
| 1805 | } |
---|
| 1806 | else // found target dentry in Inode Tree |
---|
| 1807 | { |
---|
| 1808 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
| 1809 | |
---|
| 1810 | // get pointer on target inode from dentry |
---|
| 1811 | inode_xp = hal_remote_l64( XPTR( parent_cxy , &dentry_ptr->child_xp ) ); |
---|
| 1812 | inode_cxy = GET_CXY( inode_xp ); |
---|
| 1813 | inode_ptr = GET_PTR( inode_xp ); |
---|
| 1814 | } |
---|
| 1815 | |
---|
| 1816 | // At this point the Inode Tree contains the target dentry and child inode |
---|
| 1817 | // we can safely remove this dentry from both the parent mapper, and the Inode Tree. |
---|
| 1818 | |
---|
[610] | 1819 | #if( DEBUG_VFS_UNLINK & 1 ) |
---|
| 1820 | if( DEBUG_VFS_UNLINK < cycle ) |
---|
[623] | 1821 | printk("\n[%s] thread[%x,%x] : dentry (%x,%x) / inode (%x,%x)\n", |
---|
| 1822 | __FUNCTION__, process->pid, this->trdid, parent_cxy, dentry_ptr, inode_cxy, inode_ptr ); |
---|
[610] | 1823 | #endif |
---|
[602] | 1824 | |
---|
[610] | 1825 | // get target inode "type" and "links" |
---|
| 1826 | inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
| 1827 | inode_links = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->links ) ); |
---|
[602] | 1828 | |
---|
[610] | 1829 | /////////////////////////////////////////////////////////////////////// |
---|
| 1830 | if( (inode_type == INODE_TYPE_FILE) || (inode_type == INODE_TYPE_DIR) ) |
---|
[602] | 1831 | { |
---|
[623] | 1832 | |
---|
| 1833 | #if( DEBUG_VFS_UNLINK & 1 ) |
---|
| 1834 | if( DEBUG_VFS_UNLINK < cycle ) |
---|
| 1835 | printk("\n[%s] thread[%x,%x] : unlink inode <%s> / type %s / %d links\n", |
---|
| 1836 | __FUNCTION__, process->pid, this->trdid, name, vfs_inode_type_str(inode_type), inode_links ); |
---|
| 1837 | #endif |
---|
| 1838 | |
---|
[610] | 1839 | // 1. Release clusters allocated to target inode |
---|
| 1840 | // and synchronize the FAT on IOC device if last link. |
---|
| 1841 | if( inode_links == 1 ) |
---|
[602] | 1842 | { |
---|
[610] | 1843 | // build extended pointer on target inode "children" number |
---|
| 1844 | xptr_t inode_children_xp = XPTR( inode_cxy , &inode_ptr->children.items ); |
---|
[602] | 1845 | |
---|
[610] | 1846 | // get target inode number of children |
---|
| 1847 | inode_children = hal_remote_l32( inode_children_xp ); |
---|
| 1848 | |
---|
| 1849 | // check no children |
---|
| 1850 | if( inode_children != 0 ) |
---|
| 1851 | { |
---|
| 1852 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1853 | printk("\n[ERROR] in %s : cannot remove <%s> inode that has children\n", |
---|
| 1854 | __FUNCTION__, path ); |
---|
| 1855 | return -1; |
---|
| 1856 | } |
---|
| 1857 | |
---|
| 1858 | // release clusters on IOC device |
---|
| 1859 | error = vfs_fs_release_inode( inode_xp ); |
---|
| 1860 | |
---|
| 1861 | if( error ) |
---|
| 1862 | { |
---|
| 1863 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1864 | printk("\n[ERROR] in %s : cannot update FAT mapper to remove <%s> inode\n", |
---|
| 1865 | __FUNCTION__ , path ); |
---|
| 1866 | return -1; |
---|
| 1867 | } |
---|
| 1868 | |
---|
[602] | 1869 | #if(DEBUG_VFS_UNLINK & 1) |
---|
| 1870 | if( DEBUG_VFS_UNLINK < cycle ) |
---|
| 1871 | printk("\n[%s] thread[%x,%x] removed <%s> inode from FAT (mapper and IOC device)\n", |
---|
[610] | 1872 | __FUNCTION__, process->pid, this->trdid, path ); |
---|
[602] | 1873 | #endif |
---|
[610] | 1874 | } |
---|
[602] | 1875 | |
---|
[610] | 1876 | // 2. update parent directory mapper |
---|
| 1877 | // and synchronize the parent directory on IOC device |
---|
| 1878 | if (parent_cxy == local_cxy) |
---|
[602] | 1879 | { |
---|
[610] | 1880 | error = vfs_fs_remove_dentry( parent_ptr, |
---|
[602] | 1881 | dentry_ptr ); |
---|
| 1882 | } |
---|
[610] | 1883 | else |
---|
[602] | 1884 | { |
---|
[610] | 1885 | rpc_vfs_fs_remove_dentry_client( parent_cxy, |
---|
| 1886 | parent_ptr, |
---|
[602] | 1887 | dentry_ptr, |
---|
| 1888 | &error ); |
---|
| 1889 | } |
---|
[610] | 1890 | |
---|
[602] | 1891 | if( error ) |
---|
| 1892 | { |
---|
[610] | 1893 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1894 | printk("\n[ERROR] in %s : cannot update dentry on device for <%s>\n", |
---|
| 1895 | __FUNCTION__ , path ); |
---|
[602] | 1896 | return -1; |
---|
| 1897 | } |
---|
| 1898 | |
---|
| 1899 | #if(DEBUG_VFS_UNLINK & 1) |
---|
| 1900 | if( DEBUG_VFS_UNLINK < cycle ) |
---|
| 1901 | printk("\n[%s] thread[%x,%x] removed <%s> inode from parent dir (mapper and IOC device)\n", |
---|
[610] | 1902 | __FUNCTION__, process->pid, this->trdid, path ); |
---|
[602] | 1903 | #endif |
---|
[610] | 1904 | // 3. remove dentry from Inode Tree (and associated chils inode when last link) |
---|
| 1905 | vfs_remove_child_from_parent( dentry_xp ); |
---|
[602] | 1906 | |
---|
[610] | 1907 | // release the lock protecting Inode Tree |
---|
| 1908 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1909 | |
---|
[602] | 1910 | #if DEBUG_VFS_UNLINK |
---|
| 1911 | if( DEBUG_VFS_UNLINK < cycle ) |
---|
| 1912 | printk("\n[%s] thread[%x,%x] exit / removed <%s> inode from Inode Tree / cycle %d\n", |
---|
[610] | 1913 | __FUNCTION__, process->pid, this->trdid, path, cycle ); |
---|
[602] | 1914 | #endif |
---|
| 1915 | return 0; |
---|
| 1916 | } |
---|
| 1917 | else |
---|
| 1918 | { |
---|
[610] | 1919 | remote_rwlock_wr_release( lock_xp ); |
---|
| 1920 | printk("\n[ERROR] in %s : unsupported inode type %s\n", |
---|
| 1921 | __FUNCTION__ , vfs_inode_type_str( inode_type ) ); |
---|
[602] | 1922 | return -1; |
---|
| 1923 | } |
---|
| 1924 | |
---|
| 1925 | } // end vfs_unlink() |
---|
| 1926 | |
---|
[611] | 1927 | //////////////////////////////////////////////// |
---|
| 1928 | error_t vfs_stat( xptr_t root_inode_xp, |
---|
| 1929 | char * path, |
---|
| 1930 | struct stat * st ) |
---|
[1] | 1931 | { |
---|
[610] | 1932 | error_t error; |
---|
| 1933 | xptr_t inode_xp; // extended pointer on target inode |
---|
| 1934 | vfs_inode_t * inode_ptr; // local pointer on target inode |
---|
| 1935 | cxy_t inode_cxy; // target inode cluster identifier |
---|
| 1936 | xptr_t vfs_root_xp; // extended pointer on VFS root inode |
---|
| 1937 | vfs_inode_t * vfs_root_ptr; // local_pointer on VFS root inode |
---|
| 1938 | cxy_t vfs_root_cxy; // VFS root inode cluster identifier |
---|
| 1939 | xptr_t lock_xp; // extended pointer on lock protecting Inode Tree |
---|
| 1940 | |
---|
| 1941 | thread_t * this = CURRENT_THREAD; |
---|
| 1942 | process_t * process = this->process; |
---|
| 1943 | |
---|
| 1944 | // build extended pointer on lock protecting Inode Tree (in VFS root inode) |
---|
| 1945 | vfs_root_xp = process->vfs_root_xp; |
---|
| 1946 | vfs_root_ptr = GET_PTR( vfs_root_xp ); |
---|
| 1947 | vfs_root_cxy = GET_CXY( vfs_root_xp ); |
---|
| 1948 | lock_xp = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock ); |
---|
| 1949 | |
---|
| 1950 | // get the lock protecting Inode Tree in read mode |
---|
| 1951 | remote_rwlock_rd_acquire( lock_xp ); |
---|
| 1952 | |
---|
| 1953 | // get extended pointer on target inode |
---|
| 1954 | error = vfs_lookup( root_inode_xp, |
---|
| 1955 | path, |
---|
| 1956 | 0, |
---|
| 1957 | &inode_xp, |
---|
| 1958 | NULL ); |
---|
| 1959 | |
---|
| 1960 | // release the lock protecting Inode Tree |
---|
| 1961 | remote_rwlock_rd_release( lock_xp ); |
---|
| 1962 | |
---|
| 1963 | if( error ) |
---|
| 1964 | { |
---|
| 1965 | printk("\n[ERROR] in %s : cannot found inode <%s>\n", |
---|
| 1966 | __FUNCTION__ , path ); |
---|
| 1967 | return -1; |
---|
| 1968 | } |
---|
| 1969 | |
---|
[598] | 1970 | // get cluster and local pointer on inode descriptor |
---|
[610] | 1971 | inode_ptr = GET_PTR( inode_xp ); |
---|
| 1972 | inode_cxy = GET_CXY( inode_xp ); |
---|
[598] | 1973 | |
---|
| 1974 | // get relevant infos from inode descriptor |
---|
| 1975 | uint32_t inum = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->inum ) ); |
---|
| 1976 | uint32_t size = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->size ) ); |
---|
| 1977 | uint32_t uid = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->uid ) ); |
---|
| 1978 | uint32_t gid = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->gid ) ); |
---|
| 1979 | uint32_t type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
| 1980 | uint32_t rights = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->rights ) ); |
---|
| 1981 | |
---|
| 1982 | // set stat structure fields |
---|
| 1983 | st->st_ino = inum; |
---|
| 1984 | st->st_gid = gid; |
---|
| 1985 | st->st_uid = uid; |
---|
| 1986 | st->st_size = size; |
---|
| 1987 | st->st_mode = (type << 16) | rights; |
---|
| 1988 | |
---|
| 1989 | #if DEBUG_VFS_STAT |
---|
| 1990 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1991 | if( DEBUG_VFS_STAT < cycle ) |
---|
| 1992 | printk("\n[%s] thread[%x,%x] set stat %x for inode %x in cluster %x / cycle %d\n" |
---|
| 1993 | " %s / inum %d / size %d\n", |
---|
[610] | 1994 | __FUNCTION__, process->pid, this->trdid, st, inode_ptr, inode_cxy, cycle, |
---|
[598] | 1995 | vfs_inode_type_str( type ), inum, size ); |
---|
| 1996 | #endif |
---|
| 1997 | |
---|
[1] | 1998 | return 0; |
---|
| 1999 | |
---|
[602] | 2000 | } // end vfs_stat() |
---|
| 2001 | |
---|
[23] | 2002 | //////////////////////////////////// |
---|
[610] | 2003 | error_t vfs_chdir( xptr_t root_xp, |
---|
[23] | 2004 | char * path ) |
---|
[1] | 2005 | { |
---|
[23] | 2006 | error_t error; |
---|
[610] | 2007 | xptr_t inode_xp; // extended pointer on target inode |
---|
| 2008 | cxy_t inode_cxy; // target inode cluster identifier |
---|
| 2009 | vfs_inode_t * inode_ptr; // target inode local pointer |
---|
| 2010 | vfs_inode_type_t inode_type; // target inode type |
---|
| 2011 | xptr_t vfs_root_xp; // extended pointer on VFS root inode |
---|
| 2012 | vfs_inode_t * vfs_root_ptr; // local_pointer on VFS root inode |
---|
| 2013 | cxy_t vfs_root_cxy; // VFS root inode cluster identifier |
---|
| 2014 | xptr_t main_lock_xp; // extended pointer on lock protecting Inode Tree |
---|
| 2015 | xptr_t ref_xp; // extended pointer on reference process |
---|
| 2016 | process_t * ref_ptr; // local pointer on reference process |
---|
| 2017 | cxy_t ref_cxy; // reference process cluster |
---|
| 2018 | xptr_t cwd_lock_xp; // extended pointer on lock protecting CWD change |
---|
| 2019 | xptr_t cwd_xp_xp; // extended pointer on cwd_xp in reference process |
---|
[23] | 2020 | |
---|
[610] | 2021 | thread_t * this = CURRENT_THREAD; |
---|
| 2022 | process_t * process = this->process; |
---|
[23] | 2023 | |
---|
[610] | 2024 | #if DEBUG_VFS_CHDIR |
---|
| 2025 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2026 | if( DEBUG_VFS_CHDIR < cycle ) |
---|
| 2027 | printk("\n[%s] thread[%x,%x] enter for path <%s> / cycle %d\n", |
---|
| 2028 | __FUNCTION__, process->pid, this->trdid, path, cycle ); |
---|
| 2029 | #endif |
---|
| 2030 | |
---|
| 2031 | // build extended pointer on lock protecting Inode Tree (in VFS root inode) |
---|
| 2032 | vfs_root_xp = process->vfs_root_xp; |
---|
| 2033 | vfs_root_ptr = GET_PTR( vfs_root_xp ); |
---|
| 2034 | vfs_root_cxy = GET_CXY( vfs_root_xp ); |
---|
| 2035 | main_lock_xp = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock ); |
---|
| 2036 | |
---|
| 2037 | // take lock protecting Inode Tree in read mode |
---|
| 2038 | remote_rwlock_rd_acquire( main_lock_xp ); |
---|
| 2039 | |
---|
[23] | 2040 | // get extended pointer on target inode |
---|
[610] | 2041 | error = vfs_lookup( root_xp, |
---|
| 2042 | path, |
---|
| 2043 | VFS_LOOKUP_DIR, |
---|
| 2044 | &inode_xp, |
---|
| 2045 | NULL ); |
---|
[23] | 2046 | |
---|
[610] | 2047 | // release lock protecting Inode Tree in read mode |
---|
| 2048 | remote_rwlock_rd_release( main_lock_xp ); |
---|
[23] | 2049 | |
---|
[610] | 2050 | if( error ) |
---|
| 2051 | { |
---|
| 2052 | printk("\n[ERROR] in %s : <%s> not found\n", |
---|
| 2053 | __FUNCTION__, path ); |
---|
| 2054 | return -1; |
---|
| 2055 | } |
---|
| 2056 | |
---|
| 2057 | // get inode type from remote file |
---|
[23] | 2058 | inode_cxy = GET_CXY( inode_xp ); |
---|
[473] | 2059 | inode_ptr = GET_PTR( inode_xp ); |
---|
[568] | 2060 | inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
[23] | 2061 | |
---|
| 2062 | if( inode_type != INODE_TYPE_DIR ) |
---|
| 2063 | { |
---|
[610] | 2064 | printk("\n[ERROR] in %s : <%s> is not a directory\n", |
---|
| 2065 | __FUNCTION__, path ); |
---|
[23] | 2066 | return -1; |
---|
| 2067 | } |
---|
| 2068 | |
---|
[610] | 2069 | // build extended pointer on cwd_lock and cwd_xp |
---|
| 2070 | ref_xp = process->ref_xp; |
---|
| 2071 | ref_ptr = GET_PTR( ref_xp ); |
---|
| 2072 | ref_cxy = GET_CXY( ref_xp ); |
---|
| 2073 | cwd_lock_xp = XPTR( ref_cxy , &ref_ptr->cwd_lock ); |
---|
| 2074 | cwd_xp_xp = XPTR( ref_cxy , &ref_ptr->cwd_xp ); |
---|
[568] | 2075 | |
---|
[610] | 2076 | // take lock protecting CWD changes |
---|
| 2077 | remote_busylock_acquire( cwd_lock_xp ); |
---|
[568] | 2078 | |
---|
[610] | 2079 | // update cwd_xp field in reference process descriptor |
---|
| 2080 | hal_remote_s64( cwd_xp_xp , inode_xp ); |
---|
| 2081 | |
---|
| 2082 | // release lock protecting CWD changes |
---|
| 2083 | remote_busylock_release( cwd_lock_xp ); |
---|
| 2084 | |
---|
| 2085 | #if DEBUG_VFS_CHDIR |
---|
| 2086 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 2087 | if( DEBUG_VFS_CHDIR < cycle ) |
---|
| 2088 | printk("\n[%s] thread[%x,%x] exit : inode (%x,%x) / &cwd_xp (%x,%x) / cycle %d\n", |
---|
| 2089 | __FUNCTION__, process->pid, this->trdid, inode_cxy, inode_ptr, |
---|
| 2090 | GET_CXY(cwd_xp_xp), GET_PTR(cwd_xp_xp), cycle ); |
---|
| 2091 | #endif |
---|
| 2092 | |
---|
[1] | 2093 | return 0; |
---|
| 2094 | |
---|
[610] | 2095 | } // end vfs_chdir() |
---|
| 2096 | |
---|
[23] | 2097 | /////////////////////////////////// |
---|
| 2098 | error_t vfs_chmod( xptr_t cwd_xp, |
---|
| 2099 | char * path, |
---|
| 2100 | uint32_t rights ) |
---|
[1] | 2101 | { |
---|
[23] | 2102 | error_t error; |
---|
| 2103 | xptr_t inode_xp; // extended pointer on target inode |
---|
| 2104 | cxy_t inode_cxy; // inode cluster identifier |
---|
| 2105 | vfs_inode_t * inode_ptr; // inode local pointer |
---|
| 2106 | |
---|
[625] | 2107 | // check lookup working mode |
---|
| 2108 | assert( (rights == 0), "access rights non implemented yet" ); |
---|
[23] | 2109 | |
---|
| 2110 | // get extended pointer on target inode |
---|
[610] | 2111 | error = vfs_lookup( cwd_xp, |
---|
| 2112 | path, |
---|
| 2113 | 0, |
---|
| 2114 | &inode_xp, |
---|
| 2115 | NULL ); |
---|
[23] | 2116 | |
---|
| 2117 | if( error ) return error; |
---|
| 2118 | |
---|
| 2119 | // get inode cluster and local pointer |
---|
| 2120 | inode_cxy = GET_CXY( inode_xp ); |
---|
[473] | 2121 | inode_ptr = GET_PTR( inode_xp ); |
---|
[23] | 2122 | |
---|
| 2123 | // get inode type from remote inode |
---|
[633] | 2124 | // inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
[23] | 2125 | |
---|
[633] | 2126 | // TODO finalize implementation |
---|
[610] | 2127 | |
---|
[625] | 2128 | assert( false , "not implemented" ); |
---|
[610] | 2129 | |
---|
[633] | 2130 | // set inode rights in remote inode |
---|
| 2131 | hal_remote_s32( XPTR( inode_cxy , &inode_ptr->rights ) , rights ); |
---|
| 2132 | |
---|
[1] | 2133 | return 0; |
---|
| 2134 | } |
---|
| 2135 | |
---|
[23] | 2136 | /////////////////////////////////// |
---|
| 2137 | error_t vfs_mkfifo( xptr_t cwd_xp, |
---|
| 2138 | char * path, |
---|
| 2139 | uint32_t rights ) |
---|
| 2140 | { |
---|
[625] | 2141 | assert( false , "not implemented %l %x %x", cwd_xp, path, rights ); |
---|
[23] | 2142 | return 0; |
---|
| 2143 | } |
---|
[1] | 2144 | |
---|
| 2145 | |
---|
| 2146 | |
---|
[188] | 2147 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 2148 | // Distributed Inode Tree access related functions |
---|
[1] | 2149 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2150 | |
---|
[188] | 2151 | ////////////////////////////////////////////////////////////////////////// |
---|
| 2152 | // This static function is called by the vfs_display() function. |
---|
[337] | 2153 | // that is supposed to take the TXT0 lock. |
---|
[188] | 2154 | ////////////////////////////////////////////////////////////////////////// |
---|
| 2155 | static void vfs_recursive_display( xptr_t inode_xp, |
---|
| 2156 | xptr_t name_xp, |
---|
| 2157 | uint32_t indent ) |
---|
| 2158 | { |
---|
| 2159 | cxy_t inode_cxy; |
---|
| 2160 | vfs_inode_t * inode_ptr; |
---|
| 2161 | vfs_inode_type_t inode_type; |
---|
[598] | 2162 | uint32_t inode_size; |
---|
| 2163 | uint32_t inode_attr; |
---|
| 2164 | uint32_t inode_dirty; |
---|
[625] | 2165 | void * inode_extd; |
---|
| 2166 | |
---|
[204] | 2167 | xptr_t children_xp; // extended pointer on children xhtab |
---|
[188] | 2168 | |
---|
[204] | 2169 | xptr_t child_dentry_xp; |
---|
| 2170 | cxy_t child_dentry_cxy; |
---|
| 2171 | vfs_dentry_t * child_dentry_ptr; |
---|
| 2172 | xptr_t child_inode_xp; |
---|
| 2173 | xptr_t child_dentry_name_xp; |
---|
[598] | 2174 | mapper_t * mapper_ptr; |
---|
[188] | 2175 | |
---|
| 2176 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 2177 | |
---|
| 2178 | char * indent_str[] = { "", // level 0 |
---|
| 2179 | " ", // level 1 |
---|
| 2180 | " ", // level 2 |
---|
| 2181 | " ", // level 3 |
---|
| 2182 | " ", // level 4 |
---|
| 2183 | " ", // level 5 |
---|
| 2184 | " ", // level 6 |
---|
| 2185 | " ", // level 7 |
---|
| 2186 | " ", // level 8 |
---|
| 2187 | " ", // level 9 |
---|
| 2188 | " ", // level 10 |
---|
| 2189 | " ", // level 11 |
---|
| 2190 | " ", // level 12 |
---|
| 2191 | " ", // level 13 |
---|
| 2192 | " ", // level 14 |
---|
| 2193 | " " }; // level 15 |
---|
| 2194 | |
---|
[625] | 2195 | assert( (inode_xp != XPTR_NULL) , "inode_xp cannot be NULL" ); |
---|
| 2196 | assert( (name_xp != XPTR_NULL) , "name_xp cannot be NULL" ); |
---|
| 2197 | assert( (indent < 16) , "depth cannot be larger than 15" ); |
---|
[188] | 2198 | |
---|
[610] | 2199 | // get current inode cluster and local pointer |
---|
[188] | 2200 | inode_cxy = GET_CXY( inode_xp ); |
---|
[473] | 2201 | inode_ptr = GET_PTR( inode_xp ); |
---|
[188] | 2202 | |
---|
[598] | 2203 | // get inode type, size, attr, mapper, and inum |
---|
| 2204 | inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
| 2205 | inode_size = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->size ) ); |
---|
| 2206 | inode_attr = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->attr ) ); |
---|
[625] | 2207 | inode_extd = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->extend ) ); |
---|
[598] | 2208 | mapper_ptr = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->mapper ) ); |
---|
[188] | 2209 | |
---|
| 2210 | // make a local copy of node name |
---|
| 2211 | hal_remote_strcpy( XPTR( local_cxy , name ) , name_xp ); |
---|
| 2212 | |
---|
[598] | 2213 | // compute dirty |
---|
| 2214 | inode_dirty = ((inode_attr & INODE_ATTR_DIRTY) != 0); |
---|
| 2215 | |
---|
[188] | 2216 | // display inode |
---|
[626] | 2217 | nolock_printk("%s<%s> : %s / extd %x / %d bytes / dirty %d / cxy %x / inode %x / mapper %x\n", |
---|
[625] | 2218 | indent_str[indent], name, vfs_inode_type_str( inode_type ), (uint32_t)inode_extd, |
---|
| 2219 | inode_size, inode_dirty, inode_cxy, inode_ptr, mapper_ptr ); |
---|
[188] | 2220 | |
---|
[611] | 2221 | // scan directory entries when current inode is a directory |
---|
| 2222 | // don't scan the the "." and ".." directories to break loops |
---|
| 2223 | if( (inode_type == INODE_TYPE_DIR) && |
---|
| 2224 | (strcmp( name , "." ) != 0) && |
---|
| 2225 | (strcmp( name , ".." ) != 0) ) |
---|
[188] | 2226 | { |
---|
| 2227 | // get extended pointer on directory entries xhtab |
---|
[204] | 2228 | children_xp = XPTR( inode_cxy , &inode_ptr->children ); |
---|
[188] | 2229 | |
---|
| 2230 | // get xhtab lock |
---|
[568] | 2231 | xhtab_lock( children_xp ); |
---|
[188] | 2232 | |
---|
| 2233 | // get first dentry from xhtab |
---|
[204] | 2234 | child_dentry_xp = xhtab_get_first( children_xp ); |
---|
[188] | 2235 | |
---|
[204] | 2236 | while( child_dentry_xp != XPTR_NULL ) |
---|
[188] | 2237 | { |
---|
| 2238 | // get dentry cluster and local pointer |
---|
[204] | 2239 | child_dentry_cxy = GET_CXY( child_dentry_xp ); |
---|
[473] | 2240 | child_dentry_ptr = GET_PTR( child_dentry_xp ); |
---|
[188] | 2241 | |
---|
| 2242 | // get extended pointer on child inode |
---|
[568] | 2243 | child_inode_xp = hal_remote_l64( XPTR( child_dentry_cxy, |
---|
[204] | 2244 | &child_dentry_ptr->child_xp ) ); |
---|
[188] | 2245 | |
---|
| 2246 | // get extended pointer on dentry name |
---|
[204] | 2247 | child_dentry_name_xp = XPTR( child_dentry_cxy , &child_dentry_ptr->name ); |
---|
[188] | 2248 | |
---|
[568] | 2249 | // recursive call on inode display |
---|
[204] | 2250 | vfs_recursive_display( child_inode_xp, |
---|
| 2251 | child_dentry_name_xp, |
---|
| 2252 | indent+1 ); |
---|
[188] | 2253 | |
---|
| 2254 | // get next dentry |
---|
[204] | 2255 | child_dentry_xp = xhtab_get_next( children_xp ); |
---|
[188] | 2256 | } |
---|
| 2257 | |
---|
| 2258 | // release xhtab lock |
---|
[568] | 2259 | xhtab_unlock( children_xp ); |
---|
[188] | 2260 | } |
---|
| 2261 | } // end vfs_recursive_display() |
---|
| 2262 | |
---|
| 2263 | /////////////////////////////////// |
---|
| 2264 | void vfs_display( xptr_t inode_xp ) |
---|
| 2265 | { |
---|
[204] | 2266 | xptr_t name_xp; |
---|
[188] | 2267 | xptr_t dentry_xp; |
---|
| 2268 | cxy_t dentry_cxy; |
---|
| 2269 | vfs_dentry_t * dentry_ptr; |
---|
[610] | 2270 | xptr_t parents_root_xp; // root of parent dentries xlist |
---|
[188] | 2271 | |
---|
| 2272 | // get target inode cluster and local pointer |
---|
| 2273 | cxy_t inode_cxy = GET_CXY( inode_xp ); |
---|
[473] | 2274 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); |
---|
[188] | 2275 | |
---|
[610] | 2276 | // build extended pointer on parents dentries root |
---|
| 2277 | parents_root_xp = XPTR( inode_cxy , &inode_ptr->parents ); |
---|
[188] | 2278 | |
---|
[610] | 2279 | // check VFS root |
---|
| 2280 | if( xlist_is_empty( parents_root_xp ) ) // inode is the VFS root |
---|
[188] | 2281 | { |
---|
| 2282 | // build extended pointer on root name |
---|
| 2283 | name_xp = XPTR( local_cxy , "/" ); |
---|
| 2284 | } |
---|
| 2285 | else |
---|
| 2286 | { |
---|
[610] | 2287 | // get first parent dentry cluster and pointers |
---|
| 2288 | dentry_xp = XLIST_FIRST( parents_root_xp , vfs_dentry_t , parents ); |
---|
[188] | 2289 | dentry_cxy = GET_CXY( dentry_xp ); |
---|
[473] | 2290 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
[188] | 2291 | |
---|
| 2292 | // get extended pointer on dentry name |
---|
| 2293 | name_xp = XPTR( dentry_cxy , &dentry_ptr->name ); |
---|
| 2294 | } |
---|
| 2295 | |
---|
[337] | 2296 | // get pointers on TXT0 chdev |
---|
[407] | 2297 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
[337] | 2298 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
| 2299 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
| 2300 | |
---|
| 2301 | // get extended pointer on remote TXT0 chdev lock |
---|
| 2302 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
| 2303 | |
---|
| 2304 | // get TXT0 lock in busy waiting mode |
---|
[568] | 2305 | remote_busylock_acquire( lock_xp ); |
---|
[337] | 2306 | |
---|
[188] | 2307 | // print header |
---|
[401] | 2308 | nolock_printk("\n***** file system state\n\n"); |
---|
[188] | 2309 | |
---|
| 2310 | // call recursive function |
---|
[473] | 2311 | vfs_recursive_display( inode_xp , name_xp , 0 ); |
---|
[188] | 2312 | |
---|
[337] | 2313 | // release lock |
---|
[568] | 2314 | remote_busylock_release( lock_xp ); |
---|
[337] | 2315 | |
---|
[204] | 2316 | } // end vfs_display() |
---|
[188] | 2317 | |
---|
[602] | 2318 | /* |
---|
[1] | 2319 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 2320 | // This static function is used by the vfs_lookup() function. |
---|
[1] | 2321 | // It takes an extended pointer on a remote inode (parent directory inode), |
---|
| 2322 | // and check access_rights violation for the calling thread. |
---|
| 2323 | // It can be used by any thread running in any cluster. |
---|
| 2324 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2325 | // @ inode_xp : extended pointer on inode. |
---|
| 2326 | // @ client_uid : client thread user ID |
---|
| 2327 | // @ client_gid : client thread group ID |
---|
| 2328 | // @ return true if access rights are violated. |
---|
| 2329 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 2330 | static bool_t vfs_access_denied( xptr_t inode_xp, |
---|
[1] | 2331 | uint32_t client_uid, |
---|
| 2332 | uint32_t client_gid ) |
---|
| 2333 | { |
---|
| 2334 | // get found inode cluster and local pointer |
---|
| 2335 | cxy_t inode_cxy = GET_CXY( inode_xp ); |
---|
[473] | 2336 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); |
---|
[1] | 2337 | |
---|
| 2338 | // get inode access mode, UID, and GID |
---|
[568] | 2339 | // TODO uint32_t mode = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->mode ) ); |
---|
| 2340 | uid_t uid = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->uid ) ); |
---|
| 2341 | gid_t gid = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->gid ) ); |
---|
[1] | 2342 | |
---|
[634] | 2343 | // TODO : me must use mode |
---|
[1] | 2344 | if( (uid == client_uid) || (gid == client_gid) ) return false; |
---|
| 2345 | else return true; |
---|
| 2346 | } |
---|
[602] | 2347 | */ |
---|
[1] | 2348 | |
---|
| 2349 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2350 | // This static function is used by the vfs_lookup() function. |
---|
[204] | 2351 | // It takes an extended pointer on a remote parent directory inode, a directory |
---|
[626] | 2352 | // entry name, and and scan the XHTAB associated to the parent inode to find the |
---|
| 2353 | // searched dentry. It does NOT modify the inode tree in case of miss. |
---|
[1] | 2354 | // It can be used by any thread running in any cluster. |
---|
| 2355 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2356 | // @ parent_xp : extended pointer on parent inode in remote cluster. |
---|
| 2357 | // @ name : dentry name |
---|
| 2358 | // @ child_xp : [out] buffer for extended pointer on child inode. |
---|
| 2359 | // @ return true if success / return false if not found. |
---|
| 2360 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2361 | static bool_t vfs_get_child( xptr_t parent_xp, |
---|
| 2362 | char * name, |
---|
| 2363 | xptr_t * child_xp ) |
---|
| 2364 | { |
---|
[610] | 2365 | xptr_t xhtab_xp; // extended pointer on hash table for children dentries |
---|
| 2366 | xptr_t dentry_xp; // extended pointer on children dentry |
---|
| 2367 | cxy_t dentry_cxy; |
---|
| 2368 | vfs_dentry_t * dentry_ptr; |
---|
[1] | 2369 | |
---|
| 2370 | // get parent inode cluster and local pointer |
---|
| 2371 | cxy_t parent_cxy = GET_CXY( parent_xp ); |
---|
[473] | 2372 | vfs_inode_t * parent_ptr = GET_PTR( parent_xp ); |
---|
[1] | 2373 | |
---|
| 2374 | // get extended pointer on hash table of children directory entries |
---|
| 2375 | xhtab_xp = XPTR( parent_cxy , &parent_ptr->children ); |
---|
| 2376 | |
---|
[610] | 2377 | // get pointers on matching dentry |
---|
| 2378 | dentry_xp = xhtab_lookup( xhtab_xp , name ); |
---|
| 2379 | dentry_cxy = GET_CXY( dentry_xp ); |
---|
| 2380 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
[1] | 2381 | |
---|
[610] | 2382 | if( dentry_xp == XPTR_NULL ) |
---|
| 2383 | { |
---|
| 2384 | return false; |
---|
| 2385 | } |
---|
| 2386 | else |
---|
| 2387 | { |
---|
| 2388 | *child_xp = (xptr_t)hal_remote_l64( XPTR( dentry_cxy , &dentry_ptr->child_xp ) ); |
---|
| 2389 | return true; |
---|
| 2390 | } |
---|
[1] | 2391 | |
---|
[204] | 2392 | } // end vfs_get_child() |
---|
| 2393 | |
---|
[1] | 2394 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2395 | // This static function is used by the vfs_lookup() function. |
---|
| 2396 | // It takes the <current> pointer on a buffer containing a complete pathname, and return |
---|
| 2397 | // in the <name> buffer, allocated by the caller, a single name in the path. |
---|
| 2398 | // It return also in the <next> pointer the next character to analyse in the path. |
---|
| 2399 | // Finally it returns a <last> boolean, that is true when the returned <name> is the |
---|
| 2400 | // last name in the path. The names are supposed to be separated by one or several '/' |
---|
| 2401 | // characters, that are not written in the <name> buffer. |
---|
[610] | 2402 | // |
---|
| 2403 | // WARNING: the leading characters '/' in the path are skiped before analysis. |
---|
| 2404 | // The path "/" identifies the VFS root, and is therefore anaysed as an empty |
---|
| 2405 | // string. This empty string is dignaled by the (-1) return value. |
---|
[1] | 2406 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2407 | // @ current : pointer on first character to analyse in buffer containing the path. |
---|
| 2408 | // @ name : [out] pointer on buffer allocated by the caller for the returned name. |
---|
| 2409 | // @ next : [out] pointer on next character to analyse in buffer containing the path. |
---|
| 2410 | // @ last : [out] true if the returned name is the last (NUL character found). |
---|
[610] | 2411 | // @ return 0 if success / return -1 if string empty (first chracter is NUL). |
---|
[1] | 2412 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2413 | static error_t vfs_get_name_from_path( char * current, |
---|
| 2414 | char * name, |
---|
| 2415 | char ** next, |
---|
| 2416 | bool_t * last ) |
---|
| 2417 | { |
---|
| 2418 | char * ptr = current; |
---|
| 2419 | |
---|
| 2420 | // skip leading '/' characters |
---|
| 2421 | while( *ptr == '/' ) ptr++; |
---|
| 2422 | |
---|
[610] | 2423 | // signal empty string |
---|
| 2424 | if( *ptr == 0 ) |
---|
| 2425 | { |
---|
| 2426 | *last = true; |
---|
| 2427 | return -1; |
---|
| 2428 | } |
---|
[1] | 2429 | |
---|
| 2430 | // copy all characters in name until NUL or '/' |
---|
| 2431 | while( (*ptr != 0) && (*ptr !='/') ) *(name++) = *(ptr++); |
---|
| 2432 | |
---|
[204] | 2433 | // set NUL terminating character in name buffer |
---|
| 2434 | *(name++) = 0; |
---|
| 2435 | |
---|
[1] | 2436 | // return last an next |
---|
| 2437 | if( *ptr == 0 ) // last found character is NUL => last name in path |
---|
| 2438 | { |
---|
| 2439 | *last = true; |
---|
| 2440 | } |
---|
| 2441 | else // last found character is '/' => skip it |
---|
| 2442 | { |
---|
| 2443 | *last = false; |
---|
| 2444 | *next = ptr + 1; |
---|
| 2445 | } |
---|
| 2446 | |
---|
| 2447 | return 0; |
---|
[204] | 2448 | |
---|
| 2449 | } // end vfs_get name_from_path() |
---|
[188] | 2450 | |
---|
[610] | 2451 | /////////////////////////////////////////////// |
---|
| 2452 | error_t vfs_lookup( xptr_t root_xp, |
---|
[23] | 2453 | char * pathname, |
---|
[610] | 2454 | uint32_t lookup_mode, |
---|
| 2455 | xptr_t * inode_xp, |
---|
| 2456 | char * last_name ) |
---|
[1] | 2457 | { |
---|
[101] | 2458 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; // one name in path |
---|
[1] | 2459 | |
---|
[23] | 2460 | xptr_t parent_xp; // extended pointer on parent inode |
---|
| 2461 | cxy_t parent_cxy; // cluster for parent inode |
---|
| 2462 | vfs_inode_t * parent_ptr; // local pointer on parent inode |
---|
[602] | 2463 | xptr_t dentry_xp; // extended pointer on dentry |
---|
[23] | 2464 | xptr_t child_xp; // extended pointer on child inode |
---|
| 2465 | cxy_t child_cxy; // cluster for child inode |
---|
[602] | 2466 | vfs_inode_t * child_ptr; // local pointer on child inode |
---|
[23] | 2467 | vfs_fs_type_t fs_type; // File system type |
---|
| 2468 | vfs_ctx_t * ctx_ptr; // local pointer on FS context |
---|
| 2469 | char * current; // current pointer on path |
---|
| 2470 | char * next; // next value for current pointer |
---|
| 2471 | bool_t last; // true when the name is the last in path |
---|
| 2472 | bool_t found; // true when a child has been found |
---|
[459] | 2473 | bool_t create; // searched inode must be created if not found |
---|
| 2474 | bool_t excl; // searched inode must not exist |
---|
[626] | 2475 | bool_t parent; // searched inode is the parent |
---|
[23] | 2476 | thread_t * this; // pointer on calling thread descriptor |
---|
| 2477 | process_t * process; // pointer on calling process descriptor |
---|
| 2478 | error_t error; |
---|
[1] | 2479 | |
---|
| 2480 | this = CURRENT_THREAD; |
---|
| 2481 | process = this->process; |
---|
| 2482 | |
---|
[610] | 2483 | // check pathname / root_xp consistency |
---|
| 2484 | assert( ((pathname[0] != '/') || (root_xp == process->vfs_root_xp)), |
---|
[625] | 2485 | "root inode must be VFS root for path <%s>", pathname ); |
---|
[610] | 2486 | |
---|
[438] | 2487 | #if DEBUG_VFS_LOOKUP |
---|
[433] | 2488 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[610] | 2489 | char root_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 2490 | vfs_inode_get_name( root_xp , root_name ); |
---|
[438] | 2491 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
[610] | 2492 | printk("\n[%s] thread[%x,%x] enter / root <%s> / path <%s> / mode %x / cycle %d\n", |
---|
| 2493 | __FUNCTION__, process->pid, this->trdid, root_name, pathname, lookup_mode, cycle ); |
---|
[433] | 2494 | #endif |
---|
[380] | 2495 | |
---|
[459] | 2496 | // compute lookup flags |
---|
[610] | 2497 | create = (lookup_mode & VFS_LOOKUP_CREATE) == VFS_LOOKUP_CREATE; |
---|
| 2498 | excl = (lookup_mode & VFS_LOOKUP_EXCL) == VFS_LOOKUP_EXCL; |
---|
[626] | 2499 | parent = (lookup_mode & VFS_LOOKUP_PARENT) == VFS_LOOKUP_PARENT; |
---|
[1] | 2500 | |
---|
[610] | 2501 | // initialise loop variables |
---|
| 2502 | parent_xp = root_xp; |
---|
| 2503 | current = pathname; |
---|
| 2504 | next = NULL; |
---|
| 2505 | last = false; |
---|
| 2506 | child_xp = XPTR_NULL; |
---|
[1] | 2507 | |
---|
[610] | 2508 | // loop on nodes in pathname |
---|
| 2509 | // load from device if one node in path not found in Inode Tree |
---|
[401] | 2510 | // exit loop when last name found (i.e. last == true) |
---|
[610] | 2511 | while( 1 ) |
---|
[1] | 2512 | { |
---|
[610] | 2513 | // get parent inode cluster and local pointer |
---|
| 2514 | parent_cxy = GET_CXY( parent_xp ); |
---|
| 2515 | parent_ptr = GET_PTR( parent_xp ); |
---|
[1] | 2516 | |
---|
[610] | 2517 | // get one "name" from path, and "last" flag |
---|
| 2518 | error = vfs_get_name_from_path( current , name , &next , &last ); |
---|
| 2519 | |
---|
[611] | 2520 | // handle VFS root case |
---|
[610] | 2521 | if ( error ) |
---|
| 2522 | { |
---|
| 2523 | |
---|
| 2524 | #if DEBUG_VFS_LOOKUP |
---|
| 2525 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 2526 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
| 2527 | printk("\n[%s] thread[%x,%x] exit / parent inode(%x,%x) / <%s> / cycle %d\n", |
---|
| 2528 | __FUNCTION__ , process->pid, this->trdid, parent_cxy, parent_ptr, pathname, cycle ); |
---|
| 2529 | #endif |
---|
| 2530 | *inode_xp = process->vfs_root_xp; |
---|
| 2531 | break; |
---|
| 2532 | } |
---|
| 2533 | |
---|
[438] | 2534 | #if (DEBUG_VFS_LOOKUP & 1) |
---|
| 2535 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
[628] | 2536 | printk("\n[%s] thread[%x,%x] search <%s> in <%s> / last = %d\n", |
---|
[602] | 2537 | __FUNCTION__, process->pid, this->trdid, name, pathname, last ); |
---|
[433] | 2538 | #endif |
---|
[101] | 2539 | |
---|
[610] | 2540 | // search the child dentry matching name in parent inode |
---|
[1] | 2541 | found = vfs_get_child( parent_xp, |
---|
| 2542 | name, |
---|
| 2543 | &child_xp ); |
---|
| 2544 | |
---|
[611] | 2545 | // get child inode local pointer and cluster |
---|
| 2546 | child_ptr = GET_PTR( child_xp ); |
---|
| 2547 | child_cxy = GET_CXY( child_xp ); |
---|
| 2548 | |
---|
[610] | 2549 | if( found == false ) // not found in Inode Tree |
---|
[1] | 2550 | { |
---|
[610] | 2551 | // when a inode is not found in the Inode Tree: |
---|
[626] | 2552 | // - if (last and parent) the Inode Tree is not modified |
---|
[610] | 2553 | // - else we speculatively introduce a new (dentry/inode) in inode tree, |
---|
| 2554 | // and scan the parent directory mapper to initialise it. |
---|
| 2555 | // . if it is not found in the parent mapper: |
---|
| 2556 | // - if(last and create), a brand new file or directory is created |
---|
| 2557 | // - else, an error is reported |
---|
| 2558 | // . if it is found in parent mapper: |
---|
| 2559 | // - if( last and excl ), an error is reported |
---|
| 2560 | // - else the new child (inode & dentry) is initialised in Inode Tree |
---|
| 2561 | // - if the child is a directory, the child mapper is loaded from device |
---|
[101] | 2562 | |
---|
[626] | 2563 | if( last && parent ) // does nothing |
---|
[610] | 2564 | { |
---|
| 2565 | |
---|
[438] | 2566 | #if (DEBUG_VFS_LOOKUP & 1) |
---|
| 2567 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
[610] | 2568 | printk("\n[%s] thread[%x,%x] child not found but only parent requested in <%s>\n", |
---|
| 2569 | __FUNCTION__, process->pid, this->trdid, pathname ); |
---|
| 2570 | #endif |
---|
| 2571 | } |
---|
| 2572 | else // try to get it from parent mapper |
---|
| 2573 | { |
---|
| 2574 | |
---|
| 2575 | #if (DEBUG_VFS_LOOKUP & 1) |
---|
| 2576 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
| 2577 | printk("\n[%s] thread[%x,%x] miss <%s> inode in Inode Tree => build from parent mapper\n", |
---|
[602] | 2578 | __FUNCTION__, process->pid, this->trdid, name ); |
---|
[433] | 2579 | #endif |
---|
[610] | 2580 | // get parent inode FS type |
---|
| 2581 | ctx_ptr = hal_remote_lpt( XPTR( parent_cxy,&parent_ptr->ctx ) ); |
---|
| 2582 | fs_type = hal_remote_l32( XPTR( parent_cxy , &ctx_ptr->type ) ); |
---|
[407] | 2583 | |
---|
[610] | 2584 | // select a cluster for new inode |
---|
| 2585 | child_cxy = cluster_random_select(); |
---|
[602] | 2586 | |
---|
[611] | 2587 | // insert a new child dentry/inode couple in inode tree |
---|
[610] | 2588 | error = vfs_add_child_in_parent( child_cxy, |
---|
| 2589 | fs_type, |
---|
| 2590 | parent_xp, |
---|
| 2591 | name, |
---|
| 2592 | &dentry_xp, |
---|
| 2593 | &child_xp ); |
---|
| 2594 | if( error ) |
---|
| 2595 | { |
---|
| 2596 | printk("\n[ERROR] in %s : cannot create inode <%s> in path <%s>\n", |
---|
| 2597 | __FUNCTION__ , name, pathname ); |
---|
| 2598 | return -1; |
---|
| 2599 | } |
---|
[238] | 2600 | |
---|
[610] | 2601 | // get child inode local pointer |
---|
| 2602 | child_ptr = GET_PTR( child_xp ); |
---|
[459] | 2603 | |
---|
| 2604 | #if (DEBUG_VFS_LOOKUP & 1) |
---|
| 2605 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
[629] | 2606 | printk("\n[%s] thread[%x,%x] created missing inode for <%s> in cluster %x\n", |
---|
[602] | 2607 | __FUNCTION__, process->pid, this->trdid, name, child_cxy ); |
---|
[459] | 2608 | #endif |
---|
[610] | 2609 | // scan parent mapper to find the missing dentry, and complete |
---|
[611] | 2610 | // the initialisation of dentry and child inode descriptors |
---|
[610] | 2611 | if( parent_cxy == local_cxy ) |
---|
| 2612 | { |
---|
[623] | 2613 | error = vfs_fs_new_dentry( parent_ptr, |
---|
[610] | 2614 | name, |
---|
| 2615 | child_xp ); |
---|
| 2616 | } |
---|
| 2617 | else |
---|
| 2618 | { |
---|
[623] | 2619 | rpc_vfs_fs_new_dentry_client( parent_cxy, |
---|
[610] | 2620 | parent_ptr, |
---|
| 2621 | name, |
---|
| 2622 | child_xp, |
---|
| 2623 | &error ); |
---|
| 2624 | } |
---|
[238] | 2625 | |
---|
[626] | 2626 | // when the missing dentry is not in the parent mapper, |
---|
[629] | 2627 | // a new dentry must be registered in parent directory mapper |
---|
[626] | 2628 | if ( error ) |
---|
[459] | 2629 | { |
---|
[625] | 2630 | if ( last && create ) // add a brand new dentry in parent directory |
---|
[602] | 2631 | { |
---|
[612] | 2632 | error = vfs_new_dentry_init( parent_xp, |
---|
| 2633 | dentry_xp, |
---|
| 2634 | child_xp ); |
---|
[610] | 2635 | if ( error ) |
---|
| 2636 | { |
---|
| 2637 | printk("\n[ERROR] in %s : cannot init inode <%s> in path <%s>\n", |
---|
| 2638 | __FUNCTION__, name, pathname ); |
---|
| 2639 | vfs_remove_child_from_parent( dentry_xp ); |
---|
| 2640 | return -1; |
---|
| 2641 | } |
---|
[1] | 2642 | |
---|
[459] | 2643 | #if (DEBUG_VFS_LOOKUP & 1) |
---|
| 2644 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
[626] | 2645 | printk("\n[%s] thread[%x,%x] child <%s> not found in parent mapper => created it\n", |
---|
[602] | 2646 | __FUNCTION__, process->pid, this->trdid, name ); |
---|
[626] | 2647 | vfs_inode_display( child_xp ); |
---|
[459] | 2648 | #endif |
---|
[626] | 2649 | |
---|
| 2650 | |
---|
[610] | 2651 | } |
---|
| 2652 | else // not last or not create => error |
---|
| 2653 | { |
---|
| 2654 | printk("\n[ERROR] in %s : <%s> node not found in parent for <%s>\n", |
---|
| 2655 | __FUNCTION__ , name , pathname ); |
---|
| 2656 | vfs_remove_child_from_parent( dentry_xp ); |
---|
| 2657 | return -1; |
---|
| 2658 | } |
---|
[238] | 2659 | } |
---|
[610] | 2660 | else // child has been found in parent mapper |
---|
[459] | 2661 | { |
---|
[610] | 2662 | // check the excl |
---|
| 2663 | if( last && create && excl ) |
---|
[459] | 2664 | { |
---|
[610] | 2665 | printk("\n[ERROR] in %s : node already exist <%s>\n", |
---|
| 2666 | __FUNCTION__, name ); |
---|
| 2667 | return -1; |
---|
[459] | 2668 | } |
---|
[610] | 2669 | |
---|
| 2670 | #if (DEBUG_VFS_LOOKUP & 1) |
---|
| 2671 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
| 2672 | printk("\n[%s] thread[%x,%x] initialised inode <%s> from parent mapper\n", |
---|
| 2673 | __FUNCTION__, process->pid, this->trdid, name ); |
---|
| 2674 | #endif |
---|
| 2675 | // load child mapper from device if child is a directory (prefetch) |
---|
| 2676 | uint32_t type = hal_remote_l32( XPTR( child_cxy , &child_ptr->type ) ); |
---|
| 2677 | if( type == INODE_TYPE_DIR ) |
---|
[459] | 2678 | { |
---|
[610] | 2679 | if( child_cxy == local_cxy ) |
---|
| 2680 | { |
---|
| 2681 | error = vfs_inode_load_all_pages( child_ptr ); |
---|
| 2682 | } |
---|
| 2683 | else |
---|
| 2684 | { |
---|
| 2685 | rpc_vfs_inode_load_all_pages_client( child_cxy, |
---|
| 2686 | child_ptr, |
---|
| 2687 | &error ); |
---|
| 2688 | } |
---|
| 2689 | if ( error ) |
---|
| 2690 | { |
---|
| 2691 | printk("\n[ERROR] in %s : cannot load <%s> from device\n", |
---|
| 2692 | __FUNCTION__ , name ); |
---|
| 2693 | vfs_remove_child_from_parent( dentry_xp ); |
---|
| 2694 | return -1; |
---|
| 2695 | } |
---|
[238] | 2696 | |
---|
[438] | 2697 | #if (DEBUG_VFS_LOOKUP & 1) |
---|
| 2698 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
[610] | 2699 | printk("\n[%s] thread[%x,%x] loaded directory mapper for <%s> from IOC\n", |
---|
| 2700 | __FUNCTION__ , process->pid, this->trdid, name ); |
---|
[433] | 2701 | #endif |
---|
[610] | 2702 | } |
---|
[459] | 2703 | } |
---|
| 2704 | } |
---|
[1] | 2705 | } |
---|
[610] | 2706 | else // child directly found in inode tree |
---|
[459] | 2707 | { |
---|
| 2708 | |
---|
[438] | 2709 | #if (DEBUG_VFS_LOOKUP & 1) |
---|
| 2710 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
[610] | 2711 | printk("\n[%s] thread[%x,%x] found <%s> in Inode Tree / inode (%x,%x)\n", |
---|
[611] | 2712 | __FUNCTION__, process->pid, this->trdid, name, child_cxy, child_ptr ); |
---|
[433] | 2713 | #endif |
---|
[610] | 2714 | // check the excl flag |
---|
| 2715 | if( last && create && excl ) |
---|
[459] | 2716 | { |
---|
[610] | 2717 | printk("\n[ERROR] in %s : node <%s> already exist\n", |
---|
| 2718 | __FUNCTION__, name ); |
---|
| 2719 | return -1; |
---|
[459] | 2720 | } |
---|
| 2721 | } |
---|
| 2722 | |
---|
[380] | 2723 | // TODO check access rights here [AG] |
---|
[23] | 2724 | // error = vfs_access_denied( child_xp, |
---|
| 2725 | // client_uid, |
---|
| 2726 | // client_gid ); |
---|
| 2727 | // if( error ) |
---|
| 2728 | // { |
---|
[441] | 2729 | // printk("\n[ERROR] in %s : thread %x / permission denied for %s\n", |
---|
| 2730 | // __FUNCTION__ , this , name ); |
---|
[23] | 2731 | // return EACCES; |
---|
| 2732 | // } |
---|
[1] | 2733 | |
---|
[238] | 2734 | // take lock on child inode and release lock on parent |
---|
[610] | 2735 | // vfs_inode_lock( child_xp ); |
---|
| 2736 | // vfs_inode_unlock( parent_xp ); |
---|
[1] | 2737 | |
---|
[610] | 2738 | // exit when last |
---|
| 2739 | if ( last ) // last inode in path => return relevant info |
---|
| 2740 | { |
---|
[626] | 2741 | if ( parent ) // return parent inode and child name |
---|
[610] | 2742 | { |
---|
[1] | 2743 | |
---|
[610] | 2744 | #if DEBUG_VFS_LOOKUP |
---|
| 2745 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 2746 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
| 2747 | printk("\n[%s] thread[%x,%x] exit / parent inode(%x,%x) / <%s> / cycle %d\n", |
---|
| 2748 | __FUNCTION__ , process->pid, this->trdid, parent_cxy, parent_ptr, pathname, cycle ); |
---|
| 2749 | #endif |
---|
| 2750 | *inode_xp = parent_xp; |
---|
| 2751 | strcpy( last_name , name ); |
---|
| 2752 | break; |
---|
| 2753 | } |
---|
| 2754 | else // return child inode name |
---|
| 2755 | { |
---|
[1] | 2756 | |
---|
[438] | 2757 | #if DEBUG_VFS_LOOKUP |
---|
[433] | 2758 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 2759 | if( DEBUG_VFS_LOOKUP < cycle ) |
---|
[610] | 2760 | printk("\n[%s] thread[%x,%x] exit / child inode (%x,%x) / <%s> / cycle %d\n", |
---|
| 2761 | __FUNCTION__ , process->pid, this->trdid, child_cxy, child_ptr, pathname, cycle ); |
---|
[433] | 2762 | #endif |
---|
[610] | 2763 | *inode_xp = child_xp; |
---|
| 2764 | break; |
---|
| 2765 | } |
---|
| 2766 | } |
---|
| 2767 | else // not the last inode in path => update loop variables |
---|
| 2768 | { |
---|
| 2769 | parent_xp = child_xp; |
---|
| 2770 | current = next; |
---|
| 2771 | } |
---|
| 2772 | } |
---|
[1] | 2773 | |
---|
| 2774 | return 0; |
---|
| 2775 | |
---|
| 2776 | } // end vfs_lookup() |
---|
| 2777 | |
---|
[612] | 2778 | //////////////////////////////////////////////// |
---|
| 2779 | error_t vfs_new_dentry_init( xptr_t parent_xp, |
---|
| 2780 | xptr_t dentry_xp, |
---|
| 2781 | xptr_t child_xp ) |
---|
[602] | 2782 | { |
---|
| 2783 | error_t error; |
---|
| 2784 | uint32_t cluster; |
---|
| 2785 | uint32_t child_type; |
---|
| 2786 | uint32_t child_size; |
---|
| 2787 | |
---|
[625] | 2788 | #if DEBUG_VFS_NEW_DENTRY_INIT |
---|
[602] | 2789 | char parent_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 2790 | char child_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 2791 | vfs_inode_get_name( parent_xp , parent_name ); |
---|
| 2792 | vfs_inode_get_name( child_xp , child_name ); |
---|
| 2793 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2794 | thread_t * this = CURRENT_THREAD; |
---|
[625] | 2795 | if( DEBUG_VFS_NEW_DENTRY_INIT < cycle ) |
---|
[602] | 2796 | printk("\n[%s] thread[%x,%x] enter / parent <%s> / child <%s> / cycle %d\n", |
---|
| 2797 | __FUNCTION__ , this->process->pid, this->trdid, parent_name, child_name, cycle ); |
---|
| 2798 | #endif |
---|
| 2799 | |
---|
| 2800 | // get parent inode cluster and local pointer |
---|
| 2801 | cxy_t parent_cxy = GET_CXY( parent_xp ); |
---|
| 2802 | vfs_inode_t * parent_ptr = GET_PTR( parent_xp ); |
---|
| 2803 | |
---|
| 2804 | // get dentry local pointer |
---|
| 2805 | vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp ); |
---|
| 2806 | |
---|
| 2807 | // get child inode cluster and local pointer |
---|
| 2808 | cxy_t child_cxy = GET_CXY( child_xp ); |
---|
| 2809 | vfs_inode_t * child_ptr = GET_PTR( child_xp ); |
---|
| 2810 | |
---|
[626] | 2811 | // 1. allocate one free cluster in file system to child inode, |
---|
[629] | 2812 | // and update the File Allocation Table in both the FAT mapper and IOC device. |
---|
[626] | 2813 | // It depends on the child inode FS type. |
---|
[602] | 2814 | vfs_ctx_t * ctx = hal_remote_lpt( XPTR( child_cxy , &child_ptr->ctx ) ); |
---|
| 2815 | |
---|
| 2816 | error = vfs_fs_cluster_alloc( ctx->type, |
---|
| 2817 | &cluster ); |
---|
| 2818 | if ( error ) |
---|
| 2819 | { |
---|
| 2820 | printk("\n[ERROR] in %s : cannot find a free VFS cluster\n", |
---|
| 2821 | __FUNCTION__ ); |
---|
| 2822 | return -1; |
---|
| 2823 | } |
---|
| 2824 | |
---|
[625] | 2825 | #if( DEBUG_VFS_NEW_DENTRY_INIT & 1) |
---|
| 2826 | if( DEBUG_VFS_NEW_DENTRY_INIT < cycle ) |
---|
[626] | 2827 | printk("\n[%s] thread[%x,%x] allocated FS cluster %x to <%s>\n", |
---|
[625] | 2828 | __FUNCTION__ , this->process->pid, this->trdid, cluster, child_name ); |
---|
[602] | 2829 | #endif |
---|
| 2830 | |
---|
[626] | 2831 | // 2. update the child inode descriptor size and extend |
---|
[602] | 2832 | child_type = hal_remote_l32( XPTR( child_cxy , &child_ptr->type ) ); |
---|
| 2833 | child_size = (child_type == INODE_TYPE_DIR) ? 4096 : 0; |
---|
| 2834 | |
---|
| 2835 | hal_remote_s32( XPTR( child_cxy , &child_ptr->size ) , child_size ); |
---|
| 2836 | hal_remote_spt( XPTR( child_cxy , &child_ptr->extend ) , (void*)(intptr_t)cluster ); |
---|
| 2837 | |
---|
| 2838 | // 3. update the parent inode mapper, and |
---|
| 2839 | // update the dentry extension if required |
---|
| 2840 | if( local_cxy == parent_cxy ) |
---|
| 2841 | { |
---|
| 2842 | error = vfs_fs_add_dentry( parent_ptr, |
---|
| 2843 | dentry_ptr ); |
---|
| 2844 | } |
---|
| 2845 | else |
---|
| 2846 | { |
---|
| 2847 | rpc_vfs_fs_add_dentry_client( parent_cxy, |
---|
| 2848 | parent_ptr, |
---|
| 2849 | dentry_ptr, |
---|
| 2850 | &error ); |
---|
| 2851 | } |
---|
| 2852 | if ( error ) |
---|
| 2853 | { |
---|
| 2854 | printk("\n[ERROR] in %s : cannot register child in parent directory\n", |
---|
| 2855 | __FUNCTION__ ); |
---|
| 2856 | return -1; |
---|
| 2857 | } |
---|
| 2858 | |
---|
[625] | 2859 | #if DEBUG_VFS_NEW_DENTRY_INIT |
---|
[602] | 2860 | cycle = (uint32_t)hal_get_cycles(); |
---|
[625] | 2861 | if( DEBUG_VFS_NEW_DENTRY_INIT < cycle ) |
---|
[610] | 2862 | printk("\n[%s] thread[%x,%x] exit / parent <%s> / child <%s> / cycle %d\n", |
---|
[602] | 2863 | __FUNCTION__ , this->process->pid, this->trdid, parent_name, child_name, cycle ); |
---|
| 2864 | #endif |
---|
| 2865 | |
---|
| 2866 | return 0; |
---|
| 2867 | |
---|
[612] | 2868 | } // end vfs_new_dentry_init() |
---|
[602] | 2869 | |
---|
[611] | 2870 | /////////////////////////////////////////////////// |
---|
| 2871 | error_t vfs_add_special_dentries( xptr_t child_xp, |
---|
| 2872 | xptr_t parent_xp ) |
---|
| 2873 | { |
---|
| 2874 | error_t error; |
---|
| 2875 | vfs_inode_t * child_ptr; // local pointer on child inode directory |
---|
| 2876 | cxy_t child_cxy; // child inode directory cluster identifier |
---|
| 2877 | vfs_ctx_t * ctx_ptr; // local pointer on child inode FS context |
---|
| 2878 | vfs_fs_type_t fs_type; // FS type of child inode |
---|
| 2879 | xptr_t dentry_xp; // extended pointer on dentry (used for . and ..) |
---|
| 2880 | vfs_dentry_t * dentry_ptr; // local pointer on dentry (used for . and ..) |
---|
| 2881 | |
---|
[612] | 2882 | // xptr_t parents_root_xp; // extended pointer on inode "parents" field |
---|
| 2883 | // xptr_t parents_entry_xp; // extended pointer on dentry "parents" field |
---|
[611] | 2884 | xptr_t children_xhtab_xp; // extended pointer on inode "children" field |
---|
| 2885 | xptr_t children_entry_xp; // extended pointer on dentry "children" field |
---|
| 2886 | |
---|
| 2887 | #if DEBUG_VFS_ADD_SPECIAL |
---|
| 2888 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2889 | thread_t * this = CURRENT_THREAD; |
---|
| 2890 | char child_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 2891 | char parent_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 2892 | vfs_inode_get_name( child_xp , child_name ); |
---|
| 2893 | vfs_inode_get_name( parent_xp , parent_name ); |
---|
| 2894 | if( DEBUG_VFS_ADD_SPECIAL < cycle ) |
---|
[612] | 2895 | printk("\n[%s] thread[%x,%x] enter for child <%s> in parent <%s> / cycle %d\n", |
---|
[611] | 2896 | __FUNCTION__, this->process->pid, this->trdid, child_name, parent_name, cycle ); |
---|
| 2897 | #endif |
---|
| 2898 | |
---|
| 2899 | // get new directory cluster and local pointer |
---|
| 2900 | child_cxy = GET_CXY( child_xp ); |
---|
| 2901 | child_ptr = GET_PTR( child_xp ); |
---|
| 2902 | |
---|
| 2903 | // get child inode FS type |
---|
| 2904 | ctx_ptr = hal_remote_lpt( XPTR( child_cxy , &child_ptr->ctx ) ); |
---|
| 2905 | fs_type = hal_remote_l32( XPTR( child_cxy , &ctx_ptr->type ) ); |
---|
| 2906 | |
---|
[630] | 2907 | //////////////////////////// create <.> dentry ////////////////////// |
---|
[611] | 2908 | if( child_cxy == local_cxy ) |
---|
| 2909 | { |
---|
| 2910 | error = vfs_dentry_create( fs_type, |
---|
| 2911 | ".", |
---|
| 2912 | &dentry_xp ); |
---|
| 2913 | } |
---|
| 2914 | else |
---|
| 2915 | { |
---|
| 2916 | rpc_vfs_dentry_create_client( child_cxy, |
---|
| 2917 | fs_type, |
---|
| 2918 | ".", |
---|
| 2919 | &dentry_xp, |
---|
| 2920 | &error ); |
---|
| 2921 | } |
---|
| 2922 | if( error ) |
---|
| 2923 | { |
---|
| 2924 | printk("\n[ERROR] in %s : cannot create dentry <.> in cluster %x\n", |
---|
| 2925 | __FUNCTION__ , child_cxy ); |
---|
| 2926 | return -1; |
---|
| 2927 | } |
---|
| 2928 | |
---|
| 2929 | // get <.> dentry local pointer |
---|
| 2930 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
| 2931 | |
---|
| 2932 | #if(DEBUG_VFS_ADD_SPECIAL & 1) |
---|
[630] | 2933 | cycle = (uint32_t)hal_get_cycles(); |
---|
[611] | 2934 | if( DEBUG_VFS_ADD_SPECIAL < cycle ) |
---|
[630] | 2935 | printk("\n[%s] thread[%x,%x] created dentry <.> (%x,%x) / cycle %d\n", |
---|
| 2936 | __FUNCTION__, this->process->pid, this->trdid, child_cxy, dentry_ptr, cycle ); |
---|
[611] | 2937 | #endif |
---|
| 2938 | |
---|
| 2939 | // register <.> dentry in child inode xhtab of children |
---|
| 2940 | children_xhtab_xp = XPTR( child_cxy , &child_ptr->children ); |
---|
| 2941 | children_entry_xp = XPTR( child_cxy , &dentry_ptr->children ); |
---|
| 2942 | error = xhtab_insert( children_xhtab_xp , "." , children_entry_xp ); |
---|
| 2943 | if( error ) |
---|
| 2944 | { |
---|
| 2945 | printk("\n[ERROR] in %s : cannot register dentry <.> in xhtab\n", |
---|
| 2946 | __FUNCTION__ ); |
---|
| 2947 | return -1; |
---|
| 2948 | } |
---|
| 2949 | |
---|
[612] | 2950 | |
---|
| 2951 | // don't register <.> dentry in child_inode xlist of parents |
---|
| 2952 | // parents_root_xp = XPTR( child_cxy , &child_ptr->parents ); |
---|
| 2953 | // parents_entry_xp = XPTR( child_cxy , &dentry_ptr->parents ); |
---|
| 2954 | // xlist_add_first( parents_root_xp , parents_entry_xp ); |
---|
| 2955 | // hal_remote_atomic_add( XPTR( child_cxy , &child_ptr->links ) , 1 ); |
---|
[611] | 2956 | |
---|
| 2957 | // update "parent" and "child_xp" fields in <.> dentry |
---|
| 2958 | hal_remote_s64( XPTR( child_cxy , &dentry_ptr->child_xp ) , child_xp ); |
---|
| 2959 | hal_remote_spt( XPTR( child_cxy , &dentry_ptr->parent ) , child_ptr ); |
---|
| 2960 | |
---|
| 2961 | #if(DEBUG_VFS_ADD_SPECIAL & 1) |
---|
[630] | 2962 | cycle = (uint32_t)hal_get_cycles(); |
---|
[611] | 2963 | if( DEBUG_VFS_ADD_SPECIAL < cycle ) |
---|
[630] | 2964 | printk("\n[%s] thread[%x,%x] linked dentry <.> to parent and child inodes / cycle %d\n", |
---|
| 2965 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
[611] | 2966 | #endif |
---|
| 2967 | |
---|
| 2968 | // introduce <.> dentry into child directory mapper |
---|
[630] | 2969 | // only if the target directory is not the root VFS |
---|
| 2970 | if( child_xp != parent_xp ) |
---|
[611] | 2971 | { |
---|
[630] | 2972 | if( child_cxy == local_cxy ) |
---|
| 2973 | { |
---|
| 2974 | error = vfs_fs_add_dentry( child_ptr, |
---|
| 2975 | dentry_ptr ); |
---|
| 2976 | } |
---|
| 2977 | else |
---|
| 2978 | { |
---|
| 2979 | rpc_vfs_fs_add_dentry_client( child_cxy, |
---|
| 2980 | child_ptr, |
---|
| 2981 | dentry_ptr, |
---|
| 2982 | &error ); |
---|
| 2983 | } |
---|
| 2984 | if( error ) |
---|
| 2985 | { |
---|
| 2986 | printk("\n[ERROR] in %s : cannot introduce dentry <..> in mapper %x\n", |
---|
| 2987 | __FUNCTION__ ); |
---|
| 2988 | return -1; |
---|
| 2989 | } |
---|
[611] | 2990 | |
---|
| 2991 | #if(DEBUG_VFS_ADD_SPECIAL & 1) |
---|
[630] | 2992 | cycle = (uint32_t)hal_get_cycles(); |
---|
[611] | 2993 | if( DEBUG_VFS_ADD_SPECIAL < cycle ) |
---|
[630] | 2994 | printk("\n[%s] thread[%x,%x] registered dentry <.> in child mapper / cycle %d\n", |
---|
| 2995 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
[611] | 2996 | #endif |
---|
| 2997 | |
---|
[630] | 2998 | } |
---|
| 2999 | |
---|
| 3000 | ///////////////////////////// create <..> dentry /////////////////////// |
---|
[611] | 3001 | if( child_cxy == local_cxy ) |
---|
| 3002 | { |
---|
| 3003 | error = vfs_dentry_create( fs_type, |
---|
| 3004 | "..", |
---|
| 3005 | &dentry_xp ); |
---|
| 3006 | } |
---|
| 3007 | else |
---|
| 3008 | { |
---|
| 3009 | rpc_vfs_dentry_create_client( child_cxy, |
---|
| 3010 | fs_type, |
---|
| 3011 | "..", |
---|
| 3012 | &dentry_xp, |
---|
| 3013 | &error ); |
---|
| 3014 | } |
---|
| 3015 | if( error ) |
---|
| 3016 | { |
---|
| 3017 | printk("\n[ERROR] in %s : cannot create dentry <..> in cluster %x\n", |
---|
| 3018 | __FUNCTION__ , child_cxy ); |
---|
| 3019 | return -1; |
---|
| 3020 | } |
---|
| 3021 | |
---|
| 3022 | // get <..> dentry local pointer |
---|
| 3023 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
| 3024 | |
---|
| 3025 | #if(DEBUG_VFS_ADD_SPECIAL & 1) |
---|
[630] | 3026 | cycle = (uint32_t)hal_get_cycles(); |
---|
[611] | 3027 | if( DEBUG_VFS_ADD_SPECIAL < cycle ) |
---|
[630] | 3028 | printk("\n[%s] thread[%x,%x] created dentry <..> (%x,%x) / cycle %d\n", |
---|
| 3029 | __FUNCTION__, this->process->pid, this->trdid, child_cxy, dentry_ptr, cycle ); |
---|
[611] | 3030 | #endif |
---|
| 3031 | |
---|
| 3032 | // register <..> dentry in child_inode xhtab of children |
---|
| 3033 | children_xhtab_xp = XPTR( child_cxy , &child_ptr->children ); |
---|
| 3034 | children_entry_xp = XPTR( child_cxy , &dentry_ptr->children ); |
---|
| 3035 | error = xhtab_insert( children_xhtab_xp , ".." , children_entry_xp ); |
---|
| 3036 | if( error ) |
---|
| 3037 | { |
---|
| 3038 | printk("\n[ERROR] in %s : cannot register dentry <..> in xhtab\n", |
---|
| 3039 | __FUNCTION__ ); |
---|
| 3040 | return -1; |
---|
| 3041 | } |
---|
| 3042 | |
---|
[612] | 3043 | // don't register <..> dentry in parent_inode xlist of parents |
---|
[611] | 3044 | |
---|
| 3045 | // update "parent" and "child_xp" fields in <..> dentry |
---|
| 3046 | hal_remote_s64( XPTR( child_cxy , &dentry_ptr->child_xp ) , parent_xp ); |
---|
| 3047 | hal_remote_spt( XPTR( child_cxy , &dentry_ptr->parent ) , child_ptr ); |
---|
| 3048 | |
---|
| 3049 | #if(DEBUG_VFS_ADD_SPECIAL & 1) |
---|
[630] | 3050 | cycle = (uint32_t)hal_get_cycles(); |
---|
[611] | 3051 | if( DEBUG_VFS_ADD_SPECIAL < cycle ) |
---|
[630] | 3052 | printk("\n[%s] thread[%x,%x] linked dentry <..> to parent and child inodes / cycle %d\n", |
---|
| 3053 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
[611] | 3054 | #endif |
---|
| 3055 | |
---|
| 3056 | // introduce <..> dentry into child directory mapper |
---|
[630] | 3057 | // only if the target directory is not the root VFS |
---|
| 3058 | if( child_xp != parent_xp ) |
---|
[611] | 3059 | { |
---|
[630] | 3060 | if( child_cxy == local_cxy ) |
---|
| 3061 | { |
---|
| 3062 | error = vfs_fs_add_dentry( child_ptr, |
---|
| 3063 | dentry_ptr ); |
---|
| 3064 | } |
---|
| 3065 | else |
---|
| 3066 | { |
---|
| 3067 | rpc_vfs_fs_add_dentry_client( child_cxy, |
---|
| 3068 | child_ptr, |
---|
| 3069 | dentry_ptr, |
---|
| 3070 | &error ); |
---|
| 3071 | } |
---|
| 3072 | if( error ) |
---|
| 3073 | { |
---|
| 3074 | printk("\n[ERROR] in %s : cannot introduce dentry <..> in mapper %x\n", |
---|
| 3075 | __FUNCTION__ ); |
---|
| 3076 | return -1; |
---|
| 3077 | } |
---|
[611] | 3078 | |
---|
| 3079 | #if(DEBUG_VFS_ADD_SPECIAL & 1) |
---|
[630] | 3080 | cycle = (uint32_t)hal_get_cycles(); |
---|
[611] | 3081 | if( DEBUG_VFS_ADD_SPECIAL < cycle ) |
---|
[630] | 3082 | printk("\n[%s] thread[%x,%x] registered dentry <..> in child mapper / cycle %d\n", |
---|
| 3083 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
[611] | 3084 | #endif |
---|
| 3085 | |
---|
[630] | 3086 | } |
---|
| 3087 | |
---|
[611] | 3088 | #if DEBUG_VFS_ADD_SPECIAL |
---|
| 3089 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 3090 | if( DEBUG_VFS_ADD_SPECIAL < cycle ) |
---|
[612] | 3091 | printk("\n[%s] thread[%x,%x] exit for child <%s> in parent <%s> / cycle %d\n", |
---|
| 3092 | __FUNCTION__, this->process->pid, this->trdid, child_name, parent_name, cycle ); |
---|
[611] | 3093 | #endif |
---|
| 3094 | |
---|
| 3095 | return 0; |
---|
| 3096 | |
---|
| 3097 | } // end vfs_add_special_dentries() |
---|
| 3098 | |
---|
[610] | 3099 | ////////////////////////////////////////// |
---|
| 3100 | error_t vfs_get_path( xptr_t inode_xp, |
---|
| 3101 | char * buffer, |
---|
| 3102 | char ** first, |
---|
| 3103 | uint32_t max_size ) |
---|
[1] | 3104 | { |
---|
[610] | 3105 | xptr_t dentry_xp; // extended pointer on current dentry |
---|
| 3106 | vfs_dentry_t * dentry_ptr; // local pointer on current dentry |
---|
| 3107 | cxy_t dentry_cxy; // current dentry cluster identifier |
---|
| 3108 | xptr_t name_xp; // extended pointer on current dentry name |
---|
| 3109 | uint32_t length; // length of current dentry name |
---|
| 3110 | int32_t index; // slot index in buffer |
---|
| 3111 | xptr_t current_xp; // extended pointer on current inode |
---|
| 3112 | vfs_inode_t * current_ptr; // local pointer on current inode |
---|
| 3113 | cxy_t current_cxy; // current inode cluster identifier |
---|
| 3114 | xptr_t vfs_root_xp; // extended pointer on VFS root inode |
---|
| 3115 | vfs_inode_t * vfs_root_ptr; // local pointer on VFS root inode |
---|
| 3116 | cxy_t vfs_root_cxy; // VFS root inode cluster identifier |
---|
| 3117 | xptr_t lock_xp; // extended pointer on Inode Tree lock |
---|
| 3118 | xptr_t parents_root_xp; // extended pointer on current inode parents root |
---|
| 3119 | bool_t found; // condition to exit the while loop |
---|
[1] | 3120 | |
---|
[610] | 3121 | thread_t * this = CURRENT_THREAD; |
---|
| 3122 | process_t * process = this->process; |
---|
[1] | 3123 | |
---|
[610] | 3124 | #if DEBUG_VFS_GET_PATH |
---|
| 3125 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 3126 | if( DEBUG_VFS_GET_PATH < cycle ) |
---|
| 3127 | printk("\n[%s] thread[%x,%x] enter : inode (%x,%x) / cycle %d\n", |
---|
| 3128 | __FUNCTION__ , process->pid, this->trdid, |
---|
| 3129 | GET_CXY( inode_xp ), GET_PTR( inode_xp ), cycle ); |
---|
| 3130 | #endif |
---|
| 3131 | |
---|
| 3132 | // set the NUL character in buffer / initialise buffer index |
---|
[1] | 3133 | buffer[max_size - 1] = 0; |
---|
[610] | 3134 | index = (int32_t)(max_size - 1); |
---|
[1] | 3135 | |
---|
| 3136 | // initialize current inode |
---|
[610] | 3137 | current_xp = inode_xp; |
---|
[1] | 3138 | |
---|
[610] | 3139 | // build extended pointer on lock protecting Inode Tree |
---|
| 3140 | vfs_root_xp = process->vfs_root_xp; |
---|
| 3141 | vfs_root_ptr = GET_PTR( vfs_root_xp ); |
---|
| 3142 | vfs_root_cxy = GET_CXY( vfs_root_xp ); |
---|
| 3143 | lock_xp = XPTR( vfs_root_cxy , &vfs_root_ptr->main_lock ); |
---|
| 3144 | |
---|
| 3145 | // take lock protecting Inode Tree in read mode |
---|
| 3146 | remote_rwlock_rd_acquire( lock_xp ); |
---|
| 3147 | |
---|
| 3148 | // traverse Inode Tree from target inode to VFS root |
---|
| 3149 | // selecting always the first parent dentry |
---|
| 3150 | // the buffer is written in "reverse order" (from target inode to root) |
---|
| 3151 | // exit the while loop when the VFS root has been found |
---|
[1] | 3152 | do |
---|
| 3153 | { |
---|
[610] | 3154 | // get current inode cluster and local pointer |
---|
| 3155 | current_cxy = GET_CXY( current_xp ); |
---|
| 3156 | current_ptr = GET_PTR( current_xp ); |
---|
[1] | 3157 | |
---|
[610] | 3158 | // build extended pointer on parents dentries root |
---|
| 3159 | parents_root_xp = XPTR( current_cxy , ¤t_ptr->parents ); |
---|
[1] | 3160 | |
---|
[610] | 3161 | // compute exit condition <=> current inode is VFS root |
---|
| 3162 | found = xlist_is_empty( parents_root_xp ); |
---|
[1] | 3163 | |
---|
[610] | 3164 | if( found ) // parent is the VFS root |
---|
| 3165 | { |
---|
| 3166 | if( index == (int32_t)(max_size - 1) ) |
---|
| 3167 | { |
---|
| 3168 | // update index |
---|
| 3169 | index--; |
---|
| 3170 | |
---|
| 3171 | // set separator |
---|
| 3172 | buffer[index] = '/'; |
---|
[1] | 3173 | |
---|
[610] | 3174 | // check buffer overflow |
---|
[625] | 3175 | assert( (index >= 0) , "kernel buffer too small" ); |
---|
[1] | 3176 | |
---|
[610] | 3177 | } |
---|
| 3178 | } |
---|
| 3179 | else // not the VFS root |
---|
[1] | 3180 | { |
---|
[610] | 3181 | // get first parent dentry cluster and pointers |
---|
| 3182 | dentry_xp = XLIST_FIRST( parents_root_xp , vfs_dentry_t , parents ); |
---|
| 3183 | dentry_cxy = GET_CXY( dentry_xp ); |
---|
| 3184 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
[1] | 3185 | |
---|
[610] | 3186 | // get extended pointer on dentry name and name length |
---|
| 3187 | name_xp = XPTR( dentry_cxy , dentry_ptr->name ); |
---|
| 3188 | length = hal_remote_l32( XPTR( dentry_cxy , &dentry_ptr->length ) ); |
---|
[1] | 3189 | |
---|
[610] | 3190 | #if (DEBUG_VFS_GET_PATH & 1) |
---|
| 3191 | char debug_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 3192 | hal_remote_strcpy( XPTR( local_cxy , debug_name ) , name_xp ); |
---|
| 3193 | if( DEBUG_VFS_GET_PATH < cycle ) |
---|
| 3194 | printk("\n[%s] thread(%x,%s) get current dentry <%s> in cluster %x\n", |
---|
| 3195 | __FUNCTION__ , process->pid, this->trdid, debug_name, current_cxy ); |
---|
| 3196 | #endif |
---|
| 3197 | // update index |
---|
| 3198 | index -= (length + 1); |
---|
| 3199 | |
---|
| 3200 | // check buffer overflow |
---|
[625] | 3201 | assert( (index >= 0) , "kernel buffer too small" ); |
---|
[610] | 3202 | |
---|
| 3203 | // update pathname |
---|
| 3204 | hal_remote_memcpy( XPTR( local_cxy , &buffer[index + 1] ) , |
---|
| 3205 | name_xp , length ); |
---|
| 3206 | |
---|
| 3207 | // set separator |
---|
| 3208 | buffer[index] = '/'; |
---|
| 3209 | |
---|
| 3210 | // get extended pointer on parent inode |
---|
| 3211 | current_xp = XPTR( dentry_cxy , |
---|
| 3212 | hal_remote_lpt( XPTR( dentry_cxy , &dentry_ptr->parent ) ) ); |
---|
| 3213 | } |
---|
[1] | 3214 | } |
---|
[610] | 3215 | while( found == false ); |
---|
[1] | 3216 | |
---|
[610] | 3217 | // release lock protecting Inode Tree in read mode |
---|
| 3218 | remote_rwlock_rd_release( lock_xp ); |
---|
| 3219 | |
---|
| 3220 | #if DEBUG_VFS_GET_PATH |
---|
| 3221 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 3222 | if( DEBUG_VFS_GET_PATH < cycle ) |
---|
| 3223 | printk("\n[%s] thread[%x,%x] exit : path <%s> / cycle %d\n", |
---|
| 3224 | __FUNCTION__ , process->pid, this->trdid, &buffer[index], cycle ); |
---|
| 3225 | #endif |
---|
| 3226 | |
---|
| 3227 | // return pointer on first character in buffer |
---|
| 3228 | *first = &buffer[index]; |
---|
[1] | 3229 | return 0; |
---|
| 3230 | |
---|
| 3231 | } // end vfs_get_path() |
---|
| 3232 | |
---|
[188] | 3233 | |
---|
[602] | 3234 | //////////////////////////////////////////////////////////////////// |
---|
[610] | 3235 | error_t vfs_add_child_in_parent( cxy_t child_cxy, |
---|
[23] | 3236 | vfs_fs_type_t fs_type, |
---|
[602] | 3237 | xptr_t parent_inode_xp, |
---|
[23] | 3238 | char * name, |
---|
[610] | 3239 | xptr_t * child_dentry_xp, |
---|
[602] | 3240 | xptr_t * child_inode_xp ) |
---|
[1] | 3241 | { |
---|
[610] | 3242 | error_t error; |
---|
| 3243 | cxy_t parent_cxy; // parent inode cluster identifier |
---|
| 3244 | vfs_inode_t * parent_inode_ptr; // parent inode local pointer |
---|
| 3245 | xptr_t new_dentry_xp; // extended pointer on created dentry |
---|
| 3246 | vfs_dentry_t * new_dentry_ptr; // created dentry local pointer |
---|
| 3247 | xptr_t new_inode_xp; // extended pointer on created child inode |
---|
| 3248 | vfs_inode_t * new_inode_ptr; // local pointer on created child inode |
---|
[1] | 3249 | |
---|
[610] | 3250 | xptr_t parents_root_xp; // extended pointer on child inode "parents" field |
---|
| 3251 | xptr_t parents_entry_xp; // extended pointer on child dentry "parents" field |
---|
| 3252 | xptr_t children_xhtab_xp; // extended pointer on parent inode "children" field |
---|
| 3253 | xptr_t children_entry_xp; // extended pointer on child dentry "children" field |
---|
| 3254 | |
---|
| 3255 | // get parent inode cluster and pointer |
---|
| 3256 | parent_cxy = GET_CXY( parent_inode_xp ); |
---|
[602] | 3257 | parent_inode_ptr = GET_PTR( parent_inode_xp ); |
---|
[1] | 3258 | |
---|
[438] | 3259 | #if DEBUG_VFS_ADD_CHILD |
---|
[602] | 3260 | char parent_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 3261 | vfs_inode_get_name( parent_inode_xp , parent_name ); |
---|
[433] | 3262 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[568] | 3263 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 3264 | if( DEBUG_VFS_ADD_CHILD < cycle ) |
---|
[610] | 3265 | printk("\n[%s] thread[%x,%x] enter / child <%s> / parent <%s> / cycle %d\n", |
---|
| 3266 | __FUNCTION__, this->process->pid, this->trdid, name, |
---|
| 3267 | parent_name, (uint32_t)hal_get_cycles() ); |
---|
[433] | 3268 | #endif |
---|
[279] | 3269 | |
---|
[610] | 3270 | // 1. create dentry in parent cluster |
---|
| 3271 | if( parent_cxy == local_cxy ) // parent cluster is local |
---|
[1] | 3272 | { |
---|
[23] | 3273 | error = vfs_dentry_create( fs_type, |
---|
[1] | 3274 | name, |
---|
[602] | 3275 | &new_dentry_xp ); |
---|
[1] | 3276 | } |
---|
[610] | 3277 | else // parent cluster is remote |
---|
[1] | 3278 | { |
---|
[610] | 3279 | rpc_vfs_dentry_create_client( parent_cxy, |
---|
[23] | 3280 | fs_type, |
---|
[1] | 3281 | name, |
---|
[602] | 3282 | &new_dentry_xp, |
---|
[1] | 3283 | &error ); |
---|
| 3284 | } |
---|
| 3285 | |
---|
| 3286 | if( error ) |
---|
| 3287 | { |
---|
[437] | 3288 | printk("\n[ERROR] in %s : cannot create dentry <%s> in cluster %x\n", |
---|
[610] | 3289 | __FUNCTION__ , name , parent_cxy ); |
---|
[602] | 3290 | return -1; |
---|
[1] | 3291 | } |
---|
| 3292 | |
---|
[602] | 3293 | // get dentry local pointer |
---|
| 3294 | new_dentry_ptr = GET_PTR( new_dentry_xp ); |
---|
| 3295 | |
---|
[568] | 3296 | #if(DEBUG_VFS_ADD_CHILD & 1) |
---|
| 3297 | if( DEBUG_VFS_ADD_CHILD < cycle ) |
---|
[610] | 3298 | printk("\n[%s] thread[%x,%x] / dentry <%s> created (%x,%x)\n", |
---|
| 3299 | __FUNCTION__, this->process->pid, this->trdid, name, parent_cxy, new_dentry_ptr ); |
---|
[568] | 3300 | #endif |
---|
| 3301 | |
---|
[610] | 3302 | // 2. create child inode in child cluster |
---|
| 3303 | // TODO : define attr / mode / uid / gid |
---|
[1] | 3304 | uint32_t attr = 0; |
---|
| 3305 | uint32_t mode = 0; |
---|
| 3306 | uint32_t uid = 0; |
---|
| 3307 | uint32_t gid = 0; |
---|
| 3308 | |
---|
[610] | 3309 | if( child_cxy == local_cxy ) // child cluster is local |
---|
[1] | 3310 | { |
---|
[610] | 3311 | error = vfs_inode_create( fs_type, |
---|
[1] | 3312 | attr, |
---|
| 3313 | mode, |
---|
| 3314 | uid, |
---|
| 3315 | gid, |
---|
[602] | 3316 | &new_inode_xp ); |
---|
[1] | 3317 | } |
---|
| 3318 | else // child cluster is remote |
---|
| 3319 | { |
---|
[610] | 3320 | rpc_vfs_inode_create_client( child_cxy, |
---|
[23] | 3321 | fs_type, |
---|
[1] | 3322 | attr, |
---|
| 3323 | mode, |
---|
| 3324 | uid, |
---|
| 3325 | gid, |
---|
[602] | 3326 | &new_inode_xp, |
---|
[1] | 3327 | &error ); |
---|
| 3328 | } |
---|
| 3329 | |
---|
| 3330 | if( error ) |
---|
| 3331 | { |
---|
| 3332 | printk("\n[ERROR] in %s : cannot create inode in cluster %x\n", |
---|
[610] | 3333 | __FUNCTION__ , child_cxy ); |
---|
[1] | 3334 | |
---|
[610] | 3335 | if( parent_cxy == local_cxy ) vfs_dentry_destroy( new_dentry_ptr ); |
---|
| 3336 | else rpc_vfs_dentry_destroy_client( parent_cxy , new_dentry_ptr ); |
---|
[602] | 3337 | return -1; |
---|
[1] | 3338 | } |
---|
| 3339 | |
---|
[610] | 3340 | // get new inode local pointer |
---|
| 3341 | new_inode_ptr = GET_PTR( new_inode_xp ); |
---|
| 3342 | |
---|
[568] | 3343 | #if(DEBUG_VFS_ADD_CHILD & 1) |
---|
| 3344 | if( DEBUG_VFS_ADD_CHILD < cycle ) |
---|
[610] | 3345 | printk("\n[%s] thread[%x,%x] / inode <%s> created (%x,%x)\n", |
---|
| 3346 | __FUNCTION__ , this->process->pid, this->trdid, name , child_cxy, new_inode_ptr ); |
---|
[568] | 3347 | #endif |
---|
| 3348 | |
---|
[610] | 3349 | // 3. register new_dentry in new_inode xlist of parents |
---|
| 3350 | parents_root_xp = XPTR( child_cxy , &new_inode_ptr->parents ); |
---|
| 3351 | parents_entry_xp = XPTR( parent_cxy, &new_dentry_ptr->parents ); |
---|
| 3352 | xlist_add_first( parents_root_xp , parents_entry_xp ); |
---|
| 3353 | hal_remote_atomic_add( XPTR( child_cxy , &new_inode_ptr->links ) , 1 ); |
---|
[204] | 3354 | |
---|
[610] | 3355 | #if(DEBUG_VFS_ADD_CHILD & 1) |
---|
[611] | 3356 | if( DEBUG_VFS_ADD_CHILD < cycle ) |
---|
[633] | 3357 | printk("\n[%s] thread[%x,%x] link dentry(%x,%x) to child inode(%x,%x)\n", |
---|
[610] | 3358 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 3359 | parent_cxy, new_dentry_ptr, child_cxy, new_inode_ptr ); |
---|
| 3360 | #endif |
---|
| 3361 | |
---|
[626] | 3362 | // 4. register new_dentry in parent_inode xhtab of children |
---|
[610] | 3363 | children_xhtab_xp = XPTR( parent_cxy , &parent_inode_ptr->children ); |
---|
| 3364 | children_entry_xp = XPTR( parent_cxy , &new_dentry_ptr->children ); |
---|
| 3365 | xhtab_insert( children_xhtab_xp , name , children_entry_xp ); |
---|
| 3366 | |
---|
| 3367 | #if(DEBUG_VFS_ADD_CHILD & 1) |
---|
| 3368 | if( DEBUG_VFS_ADD_CHILD < cycle ) |
---|
[633] | 3369 | printk("\n[%s] thread[%x,%x] link dentry(%x,%x) to parent inode(%x,%x)\n", |
---|
[610] | 3370 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 3371 | parent_cxy, new_dentry_ptr, parent_cxy, parent_inode_ptr ); |
---|
| 3372 | #endif |
---|
| 3373 | |
---|
[626] | 3374 | // 5. update "parent" and "child_xp" fields in new_dentry |
---|
[610] | 3375 | hal_remote_s64( XPTR( parent_cxy , &new_dentry_ptr->child_xp ) , new_inode_xp ); |
---|
| 3376 | hal_remote_spt( XPTR( parent_cxy , &new_dentry_ptr->parent ) , parent_inode_ptr ); |
---|
| 3377 | |
---|
[438] | 3378 | #if DEBUG_VFS_ADD_CHILD |
---|
[433] | 3379 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 3380 | if( DEBUG_VFS_ADD_CHILD < cycle ) |
---|
[598] | 3381 | printk("\n[%s] thread[%x,%x] exit for <%s> / cycle %d\n", |
---|
[633] | 3382 | __FUNCTION__, this->process->pid, this->trdid, name, cycle ); |
---|
[433] | 3383 | #endif |
---|
[296] | 3384 | |
---|
[602] | 3385 | // return extended pointer on dentry & child inode |
---|
[610] | 3386 | *child_dentry_xp = new_dentry_xp; |
---|
| 3387 | *child_inode_xp = new_inode_xp; |
---|
[1] | 3388 | return 0; |
---|
| 3389 | |
---|
| 3390 | } // end vfs_add_child_in_parent() |
---|
| 3391 | |
---|
[610] | 3392 | ///////////////////////////////////////////////////// |
---|
| 3393 | void vfs_remove_child_from_parent( xptr_t dentry_xp ) |
---|
[459] | 3394 | { |
---|
[610] | 3395 | cxy_t parent_cxy; // parent inode cluster identifier |
---|
| 3396 | cxy_t child_cxy; // child inode cluster identifier |
---|
| 3397 | vfs_dentry_t * dentry_ptr; // local pointer on dentry |
---|
| 3398 | xptr_t child_inode_xp; // extended pointer on child inode |
---|
| 3399 | vfs_inode_t * child_inode_ptr; // local pointer on child inode |
---|
| 3400 | vfs_inode_t * parent_inode_ptr; // local pointer on parent inode |
---|
| 3401 | uint32_t links; // number of child inode parents |
---|
| 3402 | |
---|
| 3403 | char dentry_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
[459] | 3404 | |
---|
[610] | 3405 | // get parent cluster and dentry local pointer |
---|
| 3406 | parent_cxy = GET_CXY( dentry_xp ); |
---|
[459] | 3407 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
| 3408 | |
---|
[610] | 3409 | // get a local copy of dentry name |
---|
| 3410 | hal_remote_strcpy( XPTR( local_cxy , dentry_name ), |
---|
| 3411 | XPTR( parent_cxy , &dentry_ptr->name ) ); |
---|
[459] | 3412 | |
---|
[610] | 3413 | // get parent_inode local pointer |
---|
| 3414 | parent_inode_ptr = hal_remote_lpt( XPTR( parent_cxy , &dentry_ptr->parent ) ); |
---|
| 3415 | |
---|
| 3416 | // get child cluster and child_inode pointers |
---|
| 3417 | child_inode_xp = hal_remote_l64( XPTR( parent_cxy , &dentry_ptr->child_xp ) ); |
---|
| 3418 | child_cxy = GET_CXY( child_inode_xp ); |
---|
| 3419 | child_inode_ptr = GET_PTR( child_inode_xp ); |
---|
[602] | 3420 | |
---|
[610] | 3421 | // remove dentry from parent_inode |
---|
| 3422 | xhtab_remove( XPTR( parent_cxy , &parent_inode_ptr->children ), |
---|
| 3423 | dentry_name, |
---|
| 3424 | XPTR( parent_cxy , &dentry_ptr->children ) ); |
---|
[602] | 3425 | |
---|
[610] | 3426 | // remove dentry from child_inode |
---|
| 3427 | xlist_unlink( XPTR( parent_cxy , &dentry_ptr->parents ) ); |
---|
| 3428 | links = hal_remote_atomic_add( XPTR( child_cxy , &child_inode_ptr->links ) , -1 ); |
---|
| 3429 | |
---|
| 3430 | // delete dentry descriptor |
---|
| 3431 | if( parent_cxy == local_cxy ) |
---|
[459] | 3432 | { |
---|
[602] | 3433 | vfs_dentry_destroy( dentry_ptr ); |
---|
[459] | 3434 | } |
---|
| 3435 | else |
---|
| 3436 | { |
---|
[610] | 3437 | rpc_vfs_dentry_destroy_client( parent_cxy, |
---|
[602] | 3438 | dentry_ptr ); |
---|
[459] | 3439 | } |
---|
| 3440 | |
---|
[610] | 3441 | // delete child_inode descriptor if last link |
---|
| 3442 | if( links == 1 ) |
---|
[459] | 3443 | { |
---|
[610] | 3444 | if( child_cxy == local_cxy ) |
---|
| 3445 | { |
---|
| 3446 | vfs_inode_destroy( child_inode_ptr ); |
---|
| 3447 | } |
---|
| 3448 | else |
---|
| 3449 | { |
---|
| 3450 | rpc_vfs_inode_destroy_client( child_cxy , child_inode_ptr ); |
---|
| 3451 | } |
---|
[459] | 3452 | } |
---|
| 3453 | |
---|
| 3454 | } // end vfs_remove_child_from_parent() |
---|
| 3455 | |
---|
[612] | 3456 | |
---|
| 3457 | |
---|
| 3458 | |
---|
[23] | 3459 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 3460 | // API used by VFS to access a specific FS |
---|
[23] | 3461 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 3462 | |
---|
[614] | 3463 | ////////////////////////////////////////////// |
---|
| 3464 | error_t vfs_fs_move_page( xptr_t page_xp, |
---|
| 3465 | cmd_type_t cmd_type ) |
---|
[23] | 3466 | { |
---|
[204] | 3467 | error_t error = 0; |
---|
[23] | 3468 | |
---|
[625] | 3469 | assert( (page_xp != XPTR_NULL) , "page pointer is NULL" ); |
---|
[23] | 3470 | |
---|
[602] | 3471 | page_t * page_ptr = GET_PTR( page_xp ); |
---|
| 3472 | cxy_t page_cxy = GET_CXY( page_xp ); |
---|
[23] | 3473 | |
---|
[602] | 3474 | // get local pointer on page mapper |
---|
| 3475 | mapper_t * mapper = hal_remote_lpt( XPTR( page_cxy , &page_ptr->mapper ) ); |
---|
[23] | 3476 | |
---|
[625] | 3477 | assert( (mapper != NULL) , "no mapper for page" ); |
---|
[246] | 3478 | |
---|
[23] | 3479 | // get FS type |
---|
[602] | 3480 | vfs_fs_type_t fs_type = hal_remote_l32( XPTR( page_cxy , &mapper->type ) ); |
---|
[23] | 3481 | |
---|
[238] | 3482 | // call relevant FS function |
---|
[23] | 3483 | if( fs_type == FS_TYPE_FATFS ) |
---|
| 3484 | { |
---|
[614] | 3485 | error = fatfs_move_page( page_xp , cmd_type ); |
---|
[602] | 3486 | } |
---|
| 3487 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3488 | { |
---|
| 3489 | assert( false , "should not be called for RAMFS\n" ); |
---|
| 3490 | } |
---|
| 3491 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3492 | { |
---|
| 3493 | assert( false , "should not be called for DEVFS\n" ); |
---|
| 3494 | } |
---|
| 3495 | else |
---|
| 3496 | { |
---|
[625] | 3497 | assert( false , "undefined file system type" ); |
---|
[602] | 3498 | } |
---|
[568] | 3499 | |
---|
[602] | 3500 | return error; |
---|
[568] | 3501 | |
---|
[602] | 3502 | } // end vfs_fs_move_page() |
---|
| 3503 | |
---|
| 3504 | //////////////////////////////////////////////// |
---|
| 3505 | error_t vfs_fs_add_dentry( vfs_inode_t * inode, |
---|
| 3506 | vfs_dentry_t * dentry ) |
---|
| 3507 | { |
---|
| 3508 | error_t error = 0; |
---|
| 3509 | |
---|
[625] | 3510 | assert( (inode != NULL) , "inode pointer is NULL" ); |
---|
| 3511 | assert( (dentry != NULL) , "dentry pointer is NULL" ); |
---|
[602] | 3512 | |
---|
| 3513 | mapper_t * mapper = inode->mapper; |
---|
| 3514 | |
---|
[625] | 3515 | assert( (mapper != NULL) , "mapper pointer is NULL" ); |
---|
[602] | 3516 | |
---|
| 3517 | // get FS type |
---|
| 3518 | vfs_fs_type_t fs_type = mapper->type; |
---|
| 3519 | |
---|
| 3520 | // call relevant FS function |
---|
| 3521 | if( fs_type == FS_TYPE_FATFS ) |
---|
| 3522 | { |
---|
| 3523 | error = fatfs_add_dentry( inode , dentry ); |
---|
[23] | 3524 | } |
---|
| 3525 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3526 | { |
---|
[612] | 3527 | error = 0; // does nothing for RAMFS |
---|
[23] | 3528 | } |
---|
| 3529 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3530 | { |
---|
[612] | 3531 | error = 0; // does nothing for DEVFS |
---|
[23] | 3532 | } |
---|
| 3533 | else |
---|
| 3534 | { |
---|
[625] | 3535 | assert( false , "undefined file system type" ); |
---|
[23] | 3536 | } |
---|
| 3537 | |
---|
| 3538 | return error; |
---|
| 3539 | |
---|
[602] | 3540 | } // end vfs_fs_add_dentry() |
---|
[23] | 3541 | |
---|
[602] | 3542 | /////////////////////////////////////////////////// |
---|
| 3543 | error_t vfs_fs_remove_dentry( vfs_inode_t * inode, |
---|
| 3544 | vfs_dentry_t * dentry ) |
---|
[23] | 3545 | { |
---|
[602] | 3546 | error_t error = 0; |
---|
[23] | 3547 | |
---|
[625] | 3548 | assert( (inode != NULL) , "inode pointer is NULL" ); |
---|
| 3549 | assert( (dentry != NULL) , "dentry pointer is NULL" ); |
---|
[23] | 3550 | |
---|
[238] | 3551 | mapper_t * mapper = inode->mapper; |
---|
[23] | 3552 | |
---|
[625] | 3553 | assert( (mapper != NULL) , "mapper pointer is NULL" ); |
---|
[23] | 3554 | |
---|
[602] | 3555 | // get FS type |
---|
| 3556 | vfs_fs_type_t fs_type = mapper->type; |
---|
[401] | 3557 | |
---|
[602] | 3558 | // call relevant FS function |
---|
| 3559 | if( fs_type == FS_TYPE_FATFS ) |
---|
| 3560 | { |
---|
| 3561 | error = fatfs_remove_dentry( inode , dentry ); |
---|
| 3562 | } |
---|
| 3563 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3564 | { |
---|
[612] | 3565 | error = 0; // does nothing for RAMFS |
---|
[602] | 3566 | } |
---|
| 3567 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3568 | { |
---|
[612] | 3569 | error = 0; // does nothing for DEVFS |
---|
[602] | 3570 | } |
---|
| 3571 | else |
---|
| 3572 | { |
---|
[625] | 3573 | assert( false , "undefined file system type" ); |
---|
[602] | 3574 | } |
---|
[238] | 3575 | |
---|
[602] | 3576 | return error; |
---|
| 3577 | |
---|
| 3578 | } // end vfs_fs_remove_dentry() |
---|
| 3579 | |
---|
| 3580 | //////////////////////////////////////////////// |
---|
[623] | 3581 | error_t vfs_fs_new_dentry( vfs_inode_t * parent, |
---|
[602] | 3582 | char * name, |
---|
| 3583 | xptr_t child_xp ) |
---|
| 3584 | { |
---|
| 3585 | error_t error = 0; |
---|
| 3586 | |
---|
| 3587 | // check arguments |
---|
[625] | 3588 | assert( (parent != NULL) , "parent pointer is NULL"); |
---|
| 3589 | assert( (child_xp != XPTR_NULL) , "child pointer is NULL"); |
---|
[602] | 3590 | |
---|
| 3591 | // get parent inode FS type |
---|
| 3592 | vfs_fs_type_t fs_type = parent->ctx->type; |
---|
| 3593 | |
---|
| 3594 | // call relevant FS function |
---|
| 3595 | if( fs_type == FS_TYPE_FATFS ) |
---|
[23] | 3596 | { |
---|
[623] | 3597 | error = fatfs_new_dentry( parent , name , child_xp ); |
---|
[602] | 3598 | } |
---|
| 3599 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3600 | { |
---|
[625] | 3601 | assert( false , "should not be called for RAMFS" ); |
---|
[602] | 3602 | } |
---|
| 3603 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3604 | { |
---|
[625] | 3605 | assert( false , "should not be called for DEVFS" ); |
---|
[602] | 3606 | } |
---|
| 3607 | else |
---|
| 3608 | { |
---|
[625] | 3609 | assert( false , "undefined file system type" ); |
---|
[602] | 3610 | } |
---|
[23] | 3611 | |
---|
[602] | 3612 | return error; |
---|
| 3613 | |
---|
[623] | 3614 | } // end vfs_fs_new_dentry() |
---|
[602] | 3615 | |
---|
[612] | 3616 | /////////////////////////////////////////////////// |
---|
[623] | 3617 | error_t vfs_fs_update_dentry( vfs_inode_t * inode, |
---|
| 3618 | vfs_dentry_t * dentry, |
---|
| 3619 | uint32_t size ) |
---|
| 3620 | { |
---|
| 3621 | error_t error = 0; |
---|
| 3622 | |
---|
| 3623 | // check arguments |
---|
[625] | 3624 | assert( (inode != NULL) , "inode pointer is NULL"); |
---|
| 3625 | assert( (dentry != NULL) , "dentry pointer is NULL"); |
---|
[623] | 3626 | |
---|
| 3627 | // get parent inode FS type |
---|
| 3628 | vfs_fs_type_t fs_type = inode->ctx->type; |
---|
| 3629 | |
---|
| 3630 | // call relevant FS function |
---|
| 3631 | if( fs_type == FS_TYPE_FATFS ) |
---|
| 3632 | { |
---|
| 3633 | error = fatfs_update_dentry( inode , dentry , size ); |
---|
| 3634 | } |
---|
| 3635 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3636 | { |
---|
[625] | 3637 | assert( false , "should not be called for RAMFS" ); |
---|
[623] | 3638 | } |
---|
| 3639 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3640 | { |
---|
[625] | 3641 | assert( false , "should not be called for DEVFS" ); |
---|
[623] | 3642 | } |
---|
| 3643 | else |
---|
| 3644 | { |
---|
[625] | 3645 | assert( false , "undefined file system type" ); |
---|
[623] | 3646 | } |
---|
| 3647 | |
---|
| 3648 | return error; |
---|
| 3649 | |
---|
| 3650 | } // end vfs_fs_update_dentry() |
---|
| 3651 | |
---|
| 3652 | /////////////////////////////////////////////////// |
---|
[612] | 3653 | error_t vfs_fs_get_user_dir( vfs_inode_t * inode, |
---|
| 3654 | struct dirent * array, |
---|
| 3655 | uint32_t max_dirent, |
---|
| 3656 | uint32_t min_dentry, |
---|
| 3657 | bool_t detailed, |
---|
| 3658 | uint32_t * entries, |
---|
| 3659 | bool_t * done ) |
---|
| 3660 | { |
---|
| 3661 | error_t error = 0; |
---|
| 3662 | |
---|
| 3663 | // check arguments |
---|
[625] | 3664 | assert( (inode != NULL) , "parent pointer is NULL"); |
---|
| 3665 | assert( (array != NULL) , "child pointer is NULL"); |
---|
[612] | 3666 | assert( (detailed == false) , "detailed argument not supported\n"); |
---|
| 3667 | |
---|
| 3668 | // check inode type |
---|
| 3669 | if( inode->type != INODE_TYPE_DIR ) |
---|
| 3670 | { |
---|
| 3671 | printk("\n[ERROR] in %s : target inode is not a directory\n", |
---|
| 3672 | __FUNCTION__ ); |
---|
| 3673 | return -1; |
---|
| 3674 | } |
---|
| 3675 | |
---|
| 3676 | // get parent inode FS type |
---|
| 3677 | vfs_fs_type_t fs_type = inode->ctx->type; |
---|
| 3678 | |
---|
| 3679 | // call relevant FS function |
---|
| 3680 | if( fs_type == FS_TYPE_FATFS ) |
---|
| 3681 | { |
---|
| 3682 | error = fatfs_get_user_dir( inode, |
---|
| 3683 | array, |
---|
| 3684 | max_dirent, |
---|
| 3685 | min_dentry, |
---|
| 3686 | detailed, |
---|
| 3687 | entries, |
---|
| 3688 | done ); |
---|
| 3689 | } |
---|
| 3690 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3691 | { |
---|
[625] | 3692 | assert( false , "should not be called for RAMFS" ); |
---|
[612] | 3693 | } |
---|
| 3694 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3695 | { |
---|
| 3696 | error = devfs_get_user_dir( inode, |
---|
| 3697 | array, |
---|
| 3698 | max_dirent, |
---|
| 3699 | min_dentry, |
---|
| 3700 | detailed, |
---|
| 3701 | entries, |
---|
| 3702 | done ); |
---|
| 3703 | } |
---|
| 3704 | else |
---|
| 3705 | { |
---|
[625] | 3706 | assert( false , "undefined file system type" ); |
---|
[612] | 3707 | } |
---|
| 3708 | |
---|
| 3709 | return error; |
---|
| 3710 | |
---|
| 3711 | } // end vfs_fs_get_user_dir() |
---|
| 3712 | |
---|
[610] | 3713 | //////////////////////////////////////////////// |
---|
| 3714 | error_t vfs_fs_sync_inode( vfs_inode_t * inode ) |
---|
| 3715 | { |
---|
| 3716 | error_t error = 0; |
---|
| 3717 | |
---|
| 3718 | // check arguments |
---|
[625] | 3719 | assert( (inode != NULL) , "inode pointer is NULL"); |
---|
[610] | 3720 | |
---|
| 3721 | // get inode FS type |
---|
| 3722 | vfs_fs_type_t fs_type = inode->ctx->type; |
---|
| 3723 | |
---|
| 3724 | // call relevant FS function |
---|
| 3725 | if( fs_type == FS_TYPE_FATFS ) |
---|
| 3726 | { |
---|
| 3727 | error = fatfs_sync_inode( inode ); |
---|
| 3728 | } |
---|
| 3729 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3730 | { |
---|
[625] | 3731 | assert( false , "should not be called for RAMFS" ); |
---|
[610] | 3732 | } |
---|
| 3733 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3734 | { |
---|
[625] | 3735 | assert( false , "should not be called for DEVFS" ); |
---|
[610] | 3736 | } |
---|
| 3737 | else |
---|
| 3738 | { |
---|
[625] | 3739 | assert( false , "undefined file system type" ); |
---|
[610] | 3740 | } |
---|
| 3741 | |
---|
| 3742 | return error; |
---|
| 3743 | |
---|
| 3744 | } // end vfs_fs_sync_inode() |
---|
| 3745 | |
---|
| 3746 | //////////////////////////////////////////////// |
---|
| 3747 | error_t vfs_fs_sync_fat( vfs_fs_type_t fs_type ) |
---|
| 3748 | { |
---|
| 3749 | error_t error = 0; |
---|
| 3750 | |
---|
| 3751 | // call relevant FS function |
---|
| 3752 | if( fs_type == FS_TYPE_FATFS ) |
---|
| 3753 | { |
---|
| 3754 | error = fatfs_sync_fat(); |
---|
| 3755 | } |
---|
| 3756 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3757 | { |
---|
[625] | 3758 | assert( false , "should not be called for RAMFS" ); |
---|
[610] | 3759 | } |
---|
| 3760 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3761 | { |
---|
[625] | 3762 | assert( false , "should not be called for DEVFS" ); |
---|
[610] | 3763 | } |
---|
| 3764 | else |
---|
| 3765 | { |
---|
[625] | 3766 | assert( false , "undefined file system type" ); |
---|
[610] | 3767 | } |
---|
| 3768 | |
---|
| 3769 | return error; |
---|
| 3770 | |
---|
| 3771 | } // end vfs_fs_sync_fat() |
---|
| 3772 | |
---|
| 3773 | ////////////////////////////////////////////////////// |
---|
| 3774 | error_t vfs_fs_sync_free_info( vfs_fs_type_t fs_type ) |
---|
| 3775 | { |
---|
| 3776 | error_t error = 0; |
---|
| 3777 | |
---|
| 3778 | // call relevant FS function |
---|
| 3779 | if( fs_type == FS_TYPE_FATFS ) |
---|
| 3780 | { |
---|
| 3781 | error = fatfs_sync_free_info(); |
---|
| 3782 | } |
---|
| 3783 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3784 | { |
---|
[625] | 3785 | assert( false , "should not be called for RAMFS" ); |
---|
[610] | 3786 | } |
---|
| 3787 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3788 | { |
---|
[625] | 3789 | assert( false , "should not be called for DEVFS" ); |
---|
[610] | 3790 | } |
---|
| 3791 | else |
---|
| 3792 | { |
---|
[625] | 3793 | assert( false , "undefined file system type" ); |
---|
[610] | 3794 | } |
---|
| 3795 | |
---|
| 3796 | return error; |
---|
| 3797 | |
---|
| 3798 | } // end vfs_fs_sync_fat() |
---|
| 3799 | |
---|
[602] | 3800 | ///////////////////////////////////////////////// |
---|
| 3801 | error_t vfs_fs_cluster_alloc( uint32_t fs_type, |
---|
| 3802 | uint32_t * cluster ) |
---|
| 3803 | { |
---|
| 3804 | error_t error = 0; |
---|
| 3805 | |
---|
| 3806 | // call relevant FS function |
---|
| 3807 | if( fs_type == FS_TYPE_FATFS ) |
---|
| 3808 | { |
---|
| 3809 | error = fatfs_cluster_alloc( cluster ); |
---|
[23] | 3810 | } |
---|
[602] | 3811 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3812 | { |
---|
[625] | 3813 | assert( false , "should not be called for RAMFS" ); |
---|
[602] | 3814 | } |
---|
| 3815 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3816 | { |
---|
[625] | 3817 | assert( false , "should not be called for DEVFS" ); |
---|
[602] | 3818 | } |
---|
| 3819 | else |
---|
| 3820 | { |
---|
[625] | 3821 | assert( false , "undefined file system type" ); |
---|
[602] | 3822 | } |
---|
[23] | 3823 | |
---|
[602] | 3824 | return error; |
---|
[401] | 3825 | |
---|
[602] | 3826 | } // end vfs_fs_alloc_cluster() |
---|
[23] | 3827 | |
---|
[602] | 3828 | //////////////////////////////////////////////// |
---|
| 3829 | error_t vfs_fs_release_inode( xptr_t inode_xp ) |
---|
| 3830 | { |
---|
| 3831 | error_t error = 0; |
---|
[23] | 3832 | |
---|
[625] | 3833 | assert( (inode_xp != XPTR_NULL) , "inode pointer is NULL") |
---|
[602] | 3834 | |
---|
| 3835 | vfs_inode_t * inode_ptr = GET_PTR( inode_xp ); |
---|
| 3836 | cxy_t inode_cxy = GET_CXY( inode_xp ); |
---|
| 3837 | |
---|
| 3838 | // get local pointer on page mapper |
---|
| 3839 | mapper_t * mapper = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->mapper ) ); |
---|
| 3840 | |
---|
[625] | 3841 | assert( (mapper != NULL) , "mapper pointer is NULL") |
---|
[602] | 3842 | |
---|
| 3843 | // get FS type from mapper |
---|
| 3844 | vfs_fs_type_t fs_type = hal_remote_l32( XPTR( inode_cxy , &mapper->type ) ); |
---|
| 3845 | |
---|
| 3846 | // call relevant FS function |
---|
| 3847 | if( fs_type == FS_TYPE_FATFS ) |
---|
| 3848 | { |
---|
| 3849 | error = fatfs_release_inode( inode_xp ); |
---|
| 3850 | } |
---|
| 3851 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
| 3852 | { |
---|
[625] | 3853 | assert( false , "should not be called for RAMFS" ); |
---|
[602] | 3854 | } |
---|
| 3855 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
| 3856 | { |
---|
[625] | 3857 | assert( false , "should not be called for DEVFS" ); |
---|
[602] | 3858 | } |
---|
| 3859 | else |
---|
| 3860 | { |
---|
[625] | 3861 | assert( false , "undefined file system type" ); |
---|
[602] | 3862 | } |
---|
| 3863 | |
---|
| 3864 | return error; |
---|
| 3865 | |
---|
| 3866 | } // end vfs_fs_release_inode() |
---|
| 3867 | |
---|
| 3868 | |
---|