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