Changeset 279 for trunk/kernel/vfs/fatfs.c
- Timestamp:
- Jul 27, 2017, 12:23:29 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/vfs/fatfs.c
r265 r279 217 217 } // end get_name_from_long() 218 218 219 //////////////////////////////////////////////////////////////////////////////////////////220 // This function returns the FATFS cluster index of a page identified by its page221 // index in the file, using the FAT mapper. It scans the FAT mapper, starting from the222 // FATFS cluster index allocated to the first page of the file, until it reaches the223 // searched page. The FAT mapper is automatically updated in case of miss.224 // This function can be called by any thread running in any cluster, as it uses the225 // RPC_FATFS_GET_CLUSTER to access the remote FAT mapper if required.226 // We use a RPC to scan the FAT because the RPC_FIFO will avoid contention227 // in the cluster containing the FAT mapper, and the RPC latency is not critical228 // compared to the device access latency.229 //////////////////////////////////////////////////////////////////////////////////////////230 // @ ctx : pointer on local FATFS context.231 // @ first_cluster : first cluster allocated to a file in FATFS.232 // @ page_index : index of searched page in file (one page occupies one cluster).233 // @ cluster_index : [out] pointer on buffer for FATFS cluster index.234 // @ return 0 if success / return EIO if a FAT cluster miss cannot be solved.235 //////////////////////////////////////////////////////////////////////////////////////////236 static error_t fatfs_cluster_from_index( fatfs_ctx_t * ctx,237 uint32_t first_cluster,238 uint32_t page_index,239 uint32_t * cluster_index )240 {241 uint32_t searched_cluster; // searched FATFS cluster index242 error_t error;243 244 // get extended pointer on FAT mapper245 xptr_t fat_mapper_xp = ctx->fat_mapper_xp;246 247 // get cluster cxy and local pointer on FAT mapper248 cxy_t fat_mapper_cxy = GET_CXY( fat_mapper_xp );249 mapper_t * fat_mapper_ptr = (mapper_t *)GET_PTR( fat_mapper_xp );250 251 if( fat_mapper_cxy == local_cxy ) // FAT mapper is local252 {253 error = fatfs_get_cluster( fat_mapper_ptr,254 first_cluster,255 page_index,256 &searched_cluster );257 }258 else // FAT mapper is remote259 {260 rpc_fatfs_get_cluster_client( fat_mapper_cxy,261 fat_mapper_ptr,262 first_cluster,263 page_index,264 &searched_cluster,265 &error );266 }267 268 if( error )269 {270 printk("\n[ERROR] in %s : cannot access FAT\n", __FUNCTION__ );271 return error;272 }273 274 // return success275 *cluster_index = searched_cluster;276 return 0;277 278 } // end fatfs_cluster_from_index()279 219 280 220 ////////////////////////////////////////////////////////////////////////////////////////// … … 400 340 uint8_t * buffer; 401 341 402 fatfs_dmsg("\n[INFO] %s : enter sfor fatfs_ctx = %x\n",342 fatfs_dmsg("\n[INFO] %s : enter for fatfs_ctx = %x\n", 403 343 __FUNCTION__ , fatfs_ctx ); 404 344 … … 414 354 "cannot allocate memory for 512 bytes buffer\n" ); 415 355 356 fatfs_dmsg("\n[INFO] %s : allocated 512 bytes buffer\n", __FUNCTION__ ); 357 416 358 // load the boot record from device 417 359 // using a synchronous access to IOC device 418 360 error = dev_ioc_sync_read( buffer , 0 , 1 ); 361 362 fatfs_dmsg("\n[INFO] %s : buffer loaded\n", __FUNCTION__ ); 419 363 420 364 assert( (error == 0) , __FUNCTION__ , … … 441 385 uint32_t sector_size = fatfs_get_record( BPB_BYTSPERSEC , buffer , 1 ); 442 386 443 nolock_assert( (sector_size == 512) , __FUNCTION__ ,444 387 assert( (sector_size == 512) , __FUNCTION__ , 388 "sector size must be 512 bytes\n" ); 445 389 446 390 // check cluster size from boot record 447 391 uint32_t nb_sectors = fatfs_get_record( BPB_SECPERCLUS , buffer , 1 ); 448 392 449 nolock_assert( (nb_sectors == 8) , __FUNCTION__ ,450 393 assert( (nb_sectors == 8) , __FUNCTION__ , 394 "cluster size must be 8 sectors\n" ); 451 395 452 396 // check number of FAT copies from boot record 453 397 uint32_t nb_fats = fatfs_get_record( BPB_NUMFATS , buffer , 1 ); 454 398 455 nolock_assert( (nb_fats == 1) , __FUNCTION__ ,456 399 assert( (nb_fats == 1) , __FUNCTION__ , 400 "number of FAT copies must be 1\n" ); 457 401 458 402 // get & check number of sectors in FAT from boot record 459 403 uint32_t fat_sectors = fatfs_get_record( BPB_FAT32_FATSZ32 , buffer , 1 ); 460 404 461 nolock_assert( ((fat_sectors & 0xF) == 0) , __FUNCTION__ ,462 405 assert( ((fat_sectors & 0xF) == 0) , __FUNCTION__ , 406 "FAT not multiple of 16 sectors\n"); 463 407 464 408 // get and check root cluster from boot record 465 409 uint32_t root_cluster = fatfs_get_record( BPB_FAT32_ROOTCLUS , buffer , 1 ); 466 410 467 nolock_assert( (root_cluster == 2) , __FUNCTION__ ,468 411 assert( (root_cluster == 2) , __FUNCTION__ , 412 "root cluster index must be 2\n"); 469 413 470 414 // get FAT lba from boot record … … 475 419 req.ptr = buffer; 476 420 kmem_free( &req ); 421 422 fatfs_dmsg("\n[INFO] %s : boot record read & released\n", 423 __FUNCTION__ ); 477 424 478 425 // allocate a mapper for the FAT itself … … 494 441 fatfs_ctx->last_allocated_index = 0; // TODO ??? 495 442 fatfs_ctx->fat_mapper_xp = XPTR( local_cxy , fat_mapper ); 443 444 fatfs_dmsg("\n[INFO] %s : exit for fatfs_ctx = %x\n", 445 __FUNCTION__ , fatfs_ctx ); 496 446 497 447 } // end fatfs_ctx_init()
Note: See TracChangeset
for help on using the changeset viewer.