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