Ignore:
Timestamp:
Apr 10, 2019, 10:09:39 AM (2 years ago)
Author:
alain
Message:

Fix a bug in the vmm_remove_vseg() function: the physical pages
associated to an user DATA vseg were released to the kernel when
the target process descriptor was in the reference cluster.
This physical pages release should be done only when the page
forks counter value is zero.
All other modifications are cosmetic.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/fs/fatfs.c

    r623 r625  
    22 * fatfs.c - FATFS file system API implementation.
    33 *
    4  * Author    Alain Greiner (2016,2017,2018)
     4 * Author    Alain Greiner (2016,2017,2018,2019)
    55 *
    66 * Copyright (c) UPMC Sorbonne Universites
     
    374374
    375375//////////////////////////////////////////////////////////////////////////////////////////
    376 // This static function - atomically - decrements "free_clusters", and updates
    377 // the "free_cluster_hint" shared variables in the FATFS context in the FAT cluster.
    378 // It scan the FAT to find the first free slot larger than the <cluster> argument,
    379 // and set "free_cluster_hint" <= (free - 1).
     376// This static function decrements the  "free_clusters" variable, and updates the
     377// "free_cluster_hint" variable in the FATFS context, identified by the <fat_ctx_cxy>
     378// and <fat_ctx_ptr> arguments (cluster containing the FAT mapper).
     379// It scan all slots in the FAT mapper seen as an array of 32 bits words, looking for the
     380// first free slot larger than the <cluster> argument, to update "free_cluster_hint".
    380381//
    381382// WARNING : The free_lock protecting exclusive access to these variables
    382383//           must be taken by the calling function.
    383384//////////////////////////////////////////////////////////////////////////////////////////
    384 // @ cluster     : recently allocated cluster index in FAT.
    385 //////////////////////////////////////////////////////////////////////////////////////////
    386 static error_t fatfs_free_clusters_decrement( uint32_t  cluster )
    387 {
    388     fatfs_ctx_t * loc_ctx;      // local pointer on local FATFS context
    389     fatfs_ctx_t * fat_ctx;      // local pointer on FATFS in cluster containing FAT mapper
    390     cxy_t         mapper_cxy;   // cluster identifier for cluster containing FAT mapper
     385// @ fat_ctx_cxy  : FAT mapper cluster identifier.
     386// @ fat_ctx_ptr  : local pointer on FATFS context.
     387// @ cluster      : recently allocated cluster index in FAT.
     388//////////////////////////////////////////////////////////////////////////////////////////
     389static error_t fatfs_free_clusters_decrement( cxy_t         fat_ctx_cxy,
     390                                              fatfs_ctx_t * fat_ctx_ptr,
     391                                              uint32_t      cluster )
     392{
    391393    xptr_t        mapper_xp;    // extended pointer on FAT mapper
    392394    xptr_t        hint_xp;      // extended pointer on "free_cluster_hint" shared variable
     
    397399    uint32_t      page_max;     // max number of pages in FAT mapper
    398400    xptr_t        page_xp;      // extended pointer on current page in FAT mapper
     401    xptr_t        base_xp;      // extended pointer on current page base
    399402    xptr_t        slot_xp;      // extended pointer on current slot in FAT mapper
    400403
     
    407410#endif
    408411
    409     // get local pointer on local FATFS context
    410     loc_ctx = fs_context[FS_TYPE_FATFS].extend;
    411 
    412     // get cluster containing FAT mapper
    413     mapper_xp  = loc_ctx->fat_mapper_xp;
    414     mapper_cxy = GET_CXY( mapper_xp );
    415 
    416     // get local pointer on FATFS context in FAT cluster
    417     fat_ctx = hal_remote_lpt( XPTR( mapper_cxy , &fs_context[FS_TYPE_FATFS].extend ) );
    418 
    419412    // build extended pointers on free_clusters, and free_cluster_hint
    420     hint_xp = XPTR( mapper_cxy , &fat_ctx->free_cluster_hint );
    421     numb_xp = XPTR( mapper_cxy , &fat_ctx->free_clusters );
     413    hint_xp = XPTR( fat_ctx_cxy , &fat_ctx_ptr->free_cluster_hint );
     414    numb_xp = XPTR( fat_ctx_cxy , &fat_ctx_ptr->free_clusters );
    422415
    423416    // update "free_clusters"
     
    425418    hal_remote_s32( numb_xp , numb - 1 );
    426419
    427     // scan FAT mapper to find the first free slot > cluster
    428     // and update "free_cluster_hint" as (free - 1)
     420    // get extended pointer on FAT mapper
     421    mapper_xp = hal_remote_l64( XPTR( fat_ctx_cxy , &fat_ctx_ptr->fat_mapper_xp ) );
     422
     423    // initialise variables to scan the FAT mapper
     424    // and find the first free slot > cluster
    429425    page_id  = (cluster + 1) >> 10;
    430426    slot_id  = (cluster + 1) & 0x3FF;
    431     page_max = (loc_ctx->fat_sectors_count >> 3);
     427    page_max = hal_remote_l32( XPTR( fat_ctx_cxy, &fat_ctx_ptr->fat_sectors_count ) ) >> 3;
    432428
    433429    // scan FAT mapper / loop on pages
     
    443439        }
    444440
     441        // get extended pointer on page
     442        base_xp = ppm_page2base( page_xp );
     443
    445444        // scan FAT mapper / loop on slots
    446445        while ( slot_id < 1024 )
    447446        {
    448447            // get extended pointer on current slot
    449             slot_xp = ppm_page2base( page_xp ) + (slot_id << 2);
    450 
    451             // test FAT slot value
     448            slot_xp = base_xp + (slot_id << 2);
     449
     450            // test slot value
    452451            if ( hal_remote_l32( slot_xp ) == FREE_CLUSTER )
    453452            {
    454                 // update "free_cluster_hint" <= (free - 1)
     453                // update "free_cluster_hint"
    455454                hal_remote_s32( hint_xp , (page_id << 10) + slot_id - 1 );
    456455
     
    465464            }
    466465
    467             // increment slot_id
    468             slot_id++;
     466            // update slot_id
     467            slot_id = 0;
    469468
    470469        }  // end loop on slots
    471470
    472         // update loop variables
     471        // update (page_id,slot_id) variables
    473472        page_id++;
    474473        slot_id = 0;
     
    483482
    484483//////////////////////////////////////////////////////////////////////////////////////////
    485 // This static function atomically increments <free_clusters>, and updates
    486 // the <free_cluster_hint> shared variables in the FATFS context in the FAT cluster.
    487 // If the released cluster index is smaller than the current (hint + 1) value,
    488 // it set "free_cluster_hint" <= cluster - 1.
     484// This static function increments the "free_clusters" variable, and updates the
     485// "free_cluster_hint" variables in the FATFS context, identified by the <fat_ctx_cxy>
     486// and <fat_ctx_ptr> argument (cluster containing the FAT mapper).
     487// If the released cluster index is smaller than the current (hint) value,
     488// it set "free_cluster_hint" <= cluster.
    489489//
    490490// WARNING : The free_lock protecting exclusive access to these variables
    491491//           must be taken by the calling function.
    492492//////////////////////////////////////////////////////////////////////////////////////////
     493// @ fat_ctx_cxy  : FAT mapper cluster identifier.
     494// @ fat_ctx_ptr  : local pointer on FATFS context.
    493495// @ cluster     : recently released cluster index in FAT.
    494496//////////////////////////////////////////////////////////////////////////////////////////
    495 static void fatfs_free_clusters_increment( uint32_t  cluster )
    496 {
    497     fatfs_ctx_t * loc_ctx;      // local pointer on local FATFS context
    498     fatfs_ctx_t * fat_ctx;      // local pointer on FATFS in cluster containing FAT mapper
    499     cxy_t         fat_cxy;      // cluster identifier for cluster containing FAT mapper
     497static void fatfs_free_clusters_increment( cxy_t         fat_ctx_cxy,
     498                                           fatfs_ctx_t * fat_ctx_ptr,
     499                                           uint32_t      cluster )
     500{
    500501    xptr_t        hint_xp;      // extended pointer on "free_cluster_hint" shared variable
    501502    xptr_t        numb_xp;      // extended pointer on "free_clusters" shared variable
     
    503504    uint32_t      numb;         // "free_clusters" variable current value
    504505
    505     // get local pointer on local FATFS context
    506     loc_ctx = fs_context[FS_TYPE_FATFS].extend;
    507 
    508     // get cluster containing FAT mapper
    509     fat_cxy = GET_CXY( loc_ctx->fat_mapper_xp );
    510 
    511     // get local pointer on FATFS context in FAT cluster
    512     fat_ctx = hal_remote_lpt( XPTR( fat_cxy , &fs_context[FS_TYPE_FATFS].extend ) );
    513 
    514     // build extended pointers free_lock, free_clusters, and free_cluster_hint
    515     hint_xp = XPTR( fat_cxy , &fat_ctx->free_cluster_hint );
    516     numb_xp = XPTR( fat_cxy , &fat_ctx->free_clusters );
     506    // build extended pointers on free_clusters, and free_cluster_hint
     507    hint_xp = XPTR( fat_ctx_cxy , &fat_ctx_ptr->free_cluster_hint );
     508    numb_xp = XPTR( fat_ctx_cxy , &fat_ctx_ptr->free_clusters );
    517509
    518510    // get current value of free_cluster_hint and free_clusters
     
    521513
    522514    // update free_cluster_hint if required
    523     if ( cluster < (hint + 1) ) hal_remote_s32( hint_xp , (cluster - 1) );
     515    if ( (cluster - 1) < hint ) hal_remote_s32( hint_xp , (cluster - 1) );
    524516
    525517    // update free_clusters
     
    542534// It does NOT update the FS on the IOC device.
    543535//////////////////////////////////////////////////////////////////////////////////////////
    544 // @ fat_mapper_xp : extended pointer on FAT mapper.
    545 // @ cluster       : cluster index in FAT.
     536// @ mapper_cxy : FAT mapper cluster identifier.
     537// @ mapper_ptr : local pointer on FAT mapper.
     538// @ fatfs_ctx  : local pointer on FATFS context in FAT cluster.
     539// @ cluster    : index of cluster to be released from FAT mapper.
    546540// @ return 0 if success / return -1 if error (cannot access FAT)
    547541//////////////////////////////////////////////////////////////////////////////////////////
    548 static error_t fatfs_recursive_release( xptr_t    fat_mapper_xp,
    549                                         uint32_t  cluster )
     542static error_t fatfs_recursive_release( cxy_t         mapper_cxy,
     543                                        mapper_t    * mapper_ptr,
     544                                        fatfs_ctx_t * fatfs_ctx,
     545                                        uint32_t      cluster )
    550546{
    551547    uint32_t next;
    552548
    553     // get next cluster from FAT mapper
    554     if ( mapper_remote_get_32( fat_mapper_xp , cluster , &next ) ) return -1;
     549    // build extended pointer on FAT mapper
     550    xptr_t mapper_xp = XPTR( mapper_cxy , mapper_ptr );
     551
     552    // get next cluster index from FAT mapper
     553    if ( mapper_remote_get_32( mapper_xp,
     554                               cluster,
     555                               &next ) ) return -1;
    555556
    556557#if (DEBUG_FATFS_RELEASE_INODE & 1)
     
    564565    {
    565566        // call fatfs_recursive_release() on next cluster
    566         if ( fatfs_recursive_release( fat_mapper_xp , next ) ) return -1;
     567        if ( fatfs_recursive_release( mapper_cxy,
     568                                      mapper_ptr,
     569                                      fatfs_ctx,
     570                                      next ) ) return -1;
    567571    }       
    568572
    569573    // update current cluster in FAT mapper
    570     if ( mapper_remote_set_32( fat_mapper_xp, cluster , FREE_CLUSTER ) ) return -1;
     574    if ( mapper_remote_set_32( mapper_xp,
     575                               cluster,
     576                               FREE_CLUSTER ) ) return -1;
    571577
    572578    // Update free_cluster_hint and free_clusters in FAT context
    573     fatfs_free_clusters_increment( cluster );
     579    fatfs_free_clusters_increment( mapper_cxy,
     580                                   fatfs_ctx,
     581                                   cluster );
    574582
    575583    return 0;
     
    582590//////////////////////////////////////////////////////////////////////////////////////////
    583591
    584 //////////////////////////////
    585 void fatfs_ctx_display( void )
    586 {
    587     // get pointer on local FATFS context
    588     vfs_ctx_t   * vfs_ctx   = &fs_context[FS_TYPE_FATFS];
    589     fatfs_ctx_t * fatfs_ctx = (fatfs_ctx_t *)vfs_ctx->extend;
    590 
     592///////////////////////////////////////////
     593void fatfs_ctx_display( fatfs_ctx_t * ctx )
     594{
    591595    printk("\n*** FAT context ***\n"
    592596           "- fat_sectors       = %d\n"
     
    599603           "- free_cluster_hint = %d\n"
    600604           "- fat_mapper_xp     = %l\n",
    601            fatfs_ctx->fat_sectors_count,
    602            fatfs_ctx->bytes_per_sector,
    603            fatfs_ctx->sectors_per_cluster * fatfs_ctx->bytes_per_sector,
    604            fatfs_ctx->fat_begin_lba,
    605            fatfs_ctx->cluster_begin_lba,
    606            fatfs_ctx->root_dir_cluster,
    607            fatfs_ctx->free_clusters,
    608            fatfs_ctx->free_cluster_hint,
    609            fatfs_ctx->fat_mapper_xp );
    610 
    611 }  // end fatfs_ctx_display()
     605           ctx->fat_sectors_count,
     606           ctx->bytes_per_sector,
     607           ctx->sectors_per_cluster * ctx->bytes_per_sector,
     608           ctx->fat_begin_lba,
     609           ctx->cluster_begin_lba,
     610           ctx->root_dir_cluster,
     611           ctx->free_clusters,
     612           ctx->free_cluster_hint,
     613           ctx->fat_mapper_xp );
     614
     615}  // end ctx_display()
    612616
    613617//////////////////////////////////////////
     
    659663    uint32_t * buffer;                 // pointer on current page (array of uint32_t)
    660664    uint32_t   current_page_index;     // index of current page in FAT
    661     uint32_t   current_page_offset;    // offset of slot in current page
     665    uint32_t   current_slot_index;     // index of slot in current page
    662666    uint32_t   page_count_in_file;     // index of page in file (index in linked list)
    663667    uint32_t   next_cluster_id;        // content of current FAT slot
     
    670674thread_t * this  = CURRENT_THREAD;
    671675if( DEBUG_FATFS_GET_CLUSTER < cycle )
    672 printk("\n[%s] thread[%x,%x] enter / first_cluster_id %d / searched_index / cycle %d\n",
     676printk("\n[%s] thread[%x,%x] enter / first_cluster_id %d / searched_index %d / cycle %d\n",
    673677__FUNCTION__, this->process->pid, this->trdid, first_cluster_id, searched_page_index, cycle );
    674678#endif
     
    678682
    679683    // get extended pointer and cluster on FAT mapper
    680     xptr_t mapper_xp  = ctx->fat_mapper_xp;
    681     cxy_t  mapper_cxy = GET_CXY( mapper_xp );
     684    xptr_t fat_mapper_xp  = ctx->fat_mapper_xp;
     685    cxy_t  fat_mapper_cxy = GET_CXY( fat_mapper_xp );
    682686
    683687    // initialize loop variable (1024 slots per page)
    684688    current_page_index  = first_cluster_id >> 10;
    685     current_page_offset = first_cluster_id & 0x3FF;
     689    current_slot_index = first_cluster_id & 0x3FF;
    686690    page_count_in_file  = 0;
    687691    next_cluster_id     = 0xFFFFFFFF;
    688692
    689     // scan FAT (i.e. traverse FAT linked list)
     693    // scan FAT mapper (i.e. traverse FAT linked list)
    690694    while( page_count_in_file < searched_page_index )
    691695    {
    692         // get pointer on current page descriptor
    693         current_page_xp = mapper_remote_get_page( mapper_xp , current_page_index );
     696        // get pointer on current page descriptor in FAT mapper
     697        current_page_xp = mapper_remote_get_page( fat_mapper_xp , current_page_index );
    694698
    695699        if( current_page_xp == XPTR_NULL )
    696700        {
    697             // TODO
     701            printk("\n[ERROR] in %s : cannot get next page from FAT mapper\n", __FUNCTION__);
    698702            return -1;
    699703        }
     
    704708
    705709        // get FAT slot content
    706         next_cluster_id = hal_remote_l32( XPTR( mapper_cxy , &buffer[current_page_offset] ) );
     710        next_cluster_id = hal_remote_l32( XPTR( fat_mapper_cxy,
     711                                                &buffer[current_slot_index] ) );
    707712
    708713#if (DEBUG_FATFS_GET_CLUSTER & 1)
    709714if( DEBUG_FATFS_GET_CLUSTER < cycle )
    710715printk("\n[%s] traverse FAT / current_page_index = %d\n"
    711 "current_page_offset = %d / next_cluster_id = %d\n",
    712 __FUNCTION__, current_page_index, current_page_offset , next_cluster_id );
     716"current_slot_index = %d / next_cluster_id = %d\n",
     717__FUNCTION__, current_page_index, current_slot_index , next_cluster_id );
    713718#endif
    714719
    715720        // update loop variables
    716         current_page_index  = next_cluster_id >> 10;
    717         current_page_offset = next_cluster_id & 0x3FF;
     721        current_page_index = next_cluster_id >> 10;
     722        current_slot_index = next_cluster_id & 0x3FF;
    718723        page_count_in_file++;
    719724    }
    720725
    721     if( next_cluster_id == 0xFFFFFFFF ) return -1;
     726    if( next_cluster_id == 0xFFFFFFFF )
     727    {
     728        printk("\n[ERROR] in %s : searched_cluster_id not found in FAT\n", __FUNCTION__ );
     729        return -1;
     730    }
    722731   
    723732#if DEBUG_FATFS_GET_CLUSTER
     
    759768
    760769#if DEBUG_FATFS_CTX_INIT
    761 uint32_t cycle = (uint32_t)hal_get_cycles();
     770uint32_t   cycle = (uint32_t)hal_get_cycles();
     771thread_t * this  = CURRENT_THREAD;
    762772if( DEBUG_FATFS_CTX_INIT < cycle )
    763773printk("\n[%s] thread[%x,%x] enter for fatfs_ctx = %x / cycle %d\n",
     
    766776
    767777// check argument
    768 assert( (fatfs_ctx != NULL) , "pointer on FATFS context is NULL\n" );
     778assert( (fatfs_ctx != NULL) , "pointer on FATFS context is NULL" );
    769779
    770780// check only cluster 0 does FATFS init
    771 assert( (local_cxy == 0) , "only cluster 0 can initialize FATFS\n");
     781assert( (local_cxy == 0) , "only cluster 0 can initialize FATFS");
    772782
    773783    // allocate a 512 bytes buffer to store the boot record
     
    882892    // WARNING : the inode field MUST be NULL for the FAT mapper
    883893    fat_mapper->inode = NULL;
     894
    884895
    885896    // initialize the FATFS context
     
    895906
    896907    remote_queuelock_init( XPTR( local_cxy , &fatfs_ctx->free_lock ) , LOCK_FATFS_FREE );
     908
     909#if (DEBUG_FATFS_CTX_INIT & 0x1)
     910if( DEBUG_FATFS_CTX_INIT < cycle )
     911fatfs_ctx_display( fatfs_ctx );
     912#endif
    897913
    898914#if DEBUG_FATFS_CTX_INIT
     
    15251541                          xptr_t        child_inode_xp )
    15261542{
    1527     uint8_t  * entry;    // pointer on FAT32 directory entry (array of 32 bytes)
    1528     uint32_t   index;    // index of FAT32 directory entry in mapper
    1529     mapper_t * mapper;   // pointer on directory mapper
    1530     uint32_t   cluster;  // directory entry cluster
    1531     uint32_t   size;     // directory entry size
    1532     bool_t     is_dir;   // directory entry type (file/dir)
    1533     error_t    error;
     1543    uint8_t      * entry;            // pointer on FAT32 directory entry (array of 32 bytes)
     1544    uint32_t       index;            // index of FAT32 directory entry in mapper
     1545    mapper_t     * mapper;           // pointer on directory mapper
     1546    uint32_t       cluster;          // directory entry cluster
     1547    uint32_t       size;             // directory entry size
     1548    bool_t         is_dir;           // directory entry type (file/dir)
     1549    xptr_t         root_xp;          // extended pointer on root of parent dentries
     1550    xptr_t         iter_xp;          // iterator for this list
     1551    cxy_t          child_inode_cxy;  // child inode cluster 
     1552    vfs_inode_t  * child_inode_ptr;  // child inode local pointer
     1553    xptr_t         dentry_xp;        // extended pointer on searched dentry descriptor
     1554    cxy_t          dentry_cxy;       // cluster identifier of dentry (must be local_cxy)
     1555    vfs_dentry_t * dentry_ptr;       // local pointer
     1556    error_t        error;
     1557
     1558    char       dir_name[CONFIG_VFS_MAX_NAME_LENGTH];
    15341559
    15351560// check arguments
     
    15391564
    15401565#if DEBUG_FATFS_GET_DENTRY
    1541 char       parent_name[CONFIG_VFS_MAX_NAME_LENGTH];
    15421566uint32_t   cycle = (uint32_t)hal_get_cycles();
    15431567thread_t * this  = CURRENT_THREAD;
    1544 vfs_inode_get_name( XPTR( local_cxy , parent_inode ) , parent_name );
     1568vfs_inode_get_name( XPTR( local_cxy , parent_inode ) , dir_name );
    15451569if( DEBUG_FATFS_GET_DENTRY < cycle )
    15461570printk("\n[%s]  thread[%x,%x] enter for child <%s> in parent <%s> / cycle %d\n",
    1547 __FUNCTION__, this->process->pid, this->trdid, name , parent_name , cycle );
    1548 #endif
    1549 
    1550     // get pointer and index of searched directory entry in mapper
     1571__FUNCTION__, this->process->pid, this->trdid, name , dir_name , cycle );
     1572#endif
     1573
     1574    // get local pointer on parent mapper
    15511575    mapper = parent_inode->mapper;
     1576
     1577    // get pointer and index in mapper for searched directory entry
    15521578    error  = fatfs_scan_directory( mapper, name , &entry , &index );
    15531579
    1554     // update child inode and dentry descriptors if sucess
    1555     if( error == 0 )
     1580    if( error )
    15561581    {
     1582        vfs_inode_get_name( XPTR( local_cxy , parent_inode ) , dir_name );
     1583        printk("\n[ERROR] in %s : cannot find <%s> in parent mapper <%s>\n",
     1584        __FUNCTION__, name , dir_name );
     1585        return -1;
     1586    }
     1587
     1588    // get relevant infos from FAT32 directory entry
     1589    cluster = (fatfs_get_record( DIR_FST_CLUS_HI , entry , 1 ) << 16) |
     1590              (fatfs_get_record( DIR_FST_CLUS_LO , entry , 1 )      ) ;
     1591    is_dir  = (fatfs_get_record( DIR_ATTR        , entry , 1 ) & ATTR_DIRECTORY);
     1592    size    =  fatfs_get_record( DIR_FILE_SIZE   , entry , 1 );
     1593
     1594    // get child inode cluster and local pointer
     1595    child_inode_cxy = GET_CXY( child_inode_xp );
     1596    child_inode_ptr = GET_PTR( child_inode_xp );
     1597
     1598    // build extended pointer on root of list of parent dentries
     1599    root_xp = XPTR( child_inode_cxy , &child_inode_ptr->parents );
     1600
     1601// check child inode has at least one parent
     1602assert( (xlist_is_empty( root_xp ) == false ), "child inode must have one parent\n");
     1603
     1604    // scan list of parent dentries to search the parent_inode
     1605    bool_t found = false;
     1606    XLIST_FOREACH( root_xp , iter_xp )
     1607    {
     1608        // get pointers on dentry
     1609        dentry_xp  = XLIST_ELEMENT( iter_xp , vfs_dentry_t , parents );
     1610        dentry_cxy = GET_CXY( dentry_xp );
     1611        dentry_ptr = GET_PTR( dentry_xp );
     1612
     1613        // get local pointer on current parent directory inode
     1614        vfs_inode_t * current = hal_remote_lpt( XPTR( dentry_cxy , &dentry_ptr->parent ) );
     1615
     1616        // check if current parent is the searched parent
     1617        if( XPTR( dentry_cxy , current ) == XPTR( local_cxy , parent_inode ) )
     1618        {
     1619            found = true;
     1620            break;
     1621        }
     1622    }
     1623
     1624    if( found == false )
     1625    {
     1626        vfs_inode_get_name( XPTR( local_cxy , parent_inode ) , dir_name );
     1627        printk("\n[ERROR] in %s : cannot find <%s> directory in list of parents for <%s>\n",
     1628        __FUNCTION__, dir_name, name );
     1629        return -1;
     1630    }
     1631
     1632    // update the child inode "type", "size", and "extend" fields
     1633    vfs_inode_type_t type = (is_dir) ? INODE_TYPE_DIR : INODE_TYPE_FILE;
     1634
     1635    hal_remote_s32( XPTR( child_inode_cxy , &child_inode_ptr->type   ) , type );
     1636    hal_remote_s32( XPTR( child_inode_cxy , &child_inode_ptr->size   ) , size );
     1637    hal_remote_s32( XPTR( child_inode_cxy , &child_inode_ptr->extend ) , cluster );
     1638
     1639    // update the dentry "extend" field
     1640    dentry_ptr->extend = (void *)(intptr_t)index;
    15571641
    15581642#if DEBUG_FATFS_GET_DENTRY
    15591643cycle = (uint32_t)hal_get_cycles();
    15601644if( DEBUG_FATFS_GET_DENTRY < cycle )
    1561 printk("\n[%s]  thread[%x,%x] exit / intialised child <%s> in %s / cycle %d\n",
    1562 __FUNCTION__, this->process->pid, this->trdid, name, parent_name, cycle );
    1563 #endif
    1564         // get relevant infos from FAT32 directory entry
    1565         cluster = (fatfs_get_record( DIR_FST_CLUS_HI , entry , 1 ) << 16) |
    1566                   (fatfs_get_record( DIR_FST_CLUS_LO , entry , 1 )      ) ;
    1567         is_dir  = (fatfs_get_record( DIR_ATTR        , entry , 1 ) & ATTR_DIRECTORY);
    1568         size    =  fatfs_get_record( DIR_FILE_SIZE   , entry , 1 );
    1569 
    1570         // get child inode cluster and local pointer
    1571         cxy_t          inode_cxy = GET_CXY( child_inode_xp );
    1572         vfs_inode_t  * inode_ptr = GET_PTR( child_inode_xp );
    1573 
    1574         // build extended pointer on root of list of prent dentries
    1575         xptr_t parents_root_xp = XPTR( inode_cxy , &inode_ptr->parents );
    1576 
    1577 // check child inode has at least one parent
    1578 assert( (xlist_is_empty( parents_root_xp ) == false ), "child inode must have one parent\n");
    1579 
    1580         // get dentry pointers and cluster
    1581         xptr_t         dentry_xp  = XLIST_FIRST( parents_root_xp , vfs_dentry_t , parents );
    1582         vfs_dentry_t * dentry_ptr = GET_PTR( dentry_xp );
    1583         cxy_t          dentry_cxy = GET_CXY( dentry_xp );
    1584 
    1585 // check dentry descriptor in same cluster as parent inode
    1586 assert( (dentry_cxy == local_cxy) , "illegal dentry cluster\n" );
    1587 
    1588         // update the child inode "type", "size", and "extend" fields
    1589         vfs_inode_type_t type = (is_dir) ? INODE_TYPE_DIR : INODE_TYPE_FILE;
    1590 
    1591         hal_remote_s32( XPTR( inode_cxy , &inode_ptr->type   ) , type );
    1592         hal_remote_s32( XPTR( inode_cxy , &inode_ptr->size   ) , size );
    1593         hal_remote_s32( XPTR( inode_cxy , &inode_ptr->extend ) , cluster );
    1594 
    1595         // update the dentry "extend" field
    1596         dentry_ptr->extend = (void *)(intptr_t)index;
    1597 
    1598         return 0;
    1599     }
    1600     else
    1601     {
    1602         return -1;
    1603     }
     1645printk("\n[%s]  thread[%x,%x] exit / intialised inode & dentry for <%s> in <%s> / cycle %d\n",
     1646__FUNCTION__, this->process->pid, this->trdid, name, dir_name, cycle );
     1647#endif
     1648
     1649    return 0;
    16041650
    16051651}  // end fatfs_new_dentry()
     
    16151661    error_t    error;
    16161662
     1663    char       dir_name[CONFIG_VFS_MAX_NAME_LENGTH];
     1664
    16171665// check arguments
    16181666assert( (inode  != NULL) , "inode is NULL\n" );
     
    16211669
    16221670#if DEBUG_FATFS_UPDATE_DENTRY
    1623 char       dir_name[CONFIG_VFS_MAX_NAME_LENGTH];
    16241671uint32_t   cycle = (uint32_t)hal_get_cycles();
    16251672thread_t * this  = CURRENT_THREAD;
    16261673vfs_inode_get_name( XPTR( local_cxy , inode ) , dir_name );
    16271674if( DEBUG_FATFS_UPDATE_DENTRY < cycle )
    1628 printk("\n[%s]  thread[%x,%x] enter for entry <%s> in dir <%s> / cycle %d\n",
    1629 __FUNCTION__, this->process->pid, this->trdid, dentry->name , dir_name , cycle );
    1630 #endif
    1631 
    1632     // get pointer and index of searched directory entry in mapper
     1675printk("\n[%s]  thread[%x,%x] enter for <%s/%s> / size %d / cycle %d\n",
     1676__FUNCTION__, this->process->pid, this->trdid, dir_name, dentry->name, size, cycle );
     1677#endif
     1678
     1679    // get local pointer on mapper
    16331680    mapper = inode->mapper;
     1681
     1682    // get pointer and index in mapper for searched directory entry
    16341683    error  = fatfs_scan_directory( mapper, dentry->name , &entry , &index );
    16351684
    1636     // update size in mapper if found
    1637     if( error == 0 )
     1685    if( error )
    16381686    {
     1687        vfs_inode_get_name( XPTR( local_cxy , inode ) , dir_name );
     1688        printk("\n[ERROR] in %s : cannot find <%s> in parent mapper <%s>\n",
     1689        __FUNCTION__, dentry->name, dir_name );
     1690        return -1;
     1691    }
     1692
     1693    // set size in FAT32 directory entry
     1694    fatfs_set_record( DIR_FILE_SIZE , entry , 1 , size );
     1695
     1696    // get local pointer on modified page base
     1697    void * base = (void *)((intptr_t)entry & (~CONFIG_PPM_PAGE_MASK));
     1698
     1699    // get extended pointer on modified page descriptor
     1700    xptr_t page_xp = ppm_base2page( XPTR( local_cxy , base ) );
     1701
     1702    // synchronously update the modified page on device
     1703    error = fatfs_move_page( page_xp , IOC_SYNC_WRITE );
     1704
     1705    if( error )
     1706    {
     1707        vfs_inode_get_name( XPTR( local_cxy , inode ) , dir_name );
     1708        printk("\n[ERROR] in %s : cannot update parent directory <%s> on device\n",
     1709        __FUNCTION__, dir_name );
     1710        return -1;
     1711    }
    16391712
    16401713#if DEBUG_FATFS_UPDATE_DENTRY
    16411714cycle = (uint32_t)hal_get_cycles();
    16421715if( DEBUG_FATFS_UPDATE_DENTRY < cycle )
    1643 printk("\n[%s]  thread[%x,%x] exit / found entry <%s> in <%s> / cycle %d\n",
    1644 __FUNCTION__, this->process->pid, this->trdid, dentry->name, dir_name, cycle );
    1645 #endif
    1646         // set size in FAT32 directory entry
    1647         fatfs_set_record( DIR_FILE_SIZE , entry , 1 , size );
    1648 
    1649         // get local pointer on modified page base
    1650         void * base = (void *)((intptr_t)entry & (~CONFIG_PPM_PAGE_MASK));
    1651 
    1652         // get extended pointer on modified page descriptor
    1653         xptr_t    page_xp = ppm_base2page( XPTR( local_cxy , base ) );
    1654 
    1655         // mark page as dirty
    1656         ppm_page_do_dirty( page_xp );
    1657 
    1658         return 0;
    1659     }
    1660     else
    1661     {
    1662         return -1;
    1663     }
     1716printk("\n[%s]  thread[%x,%x] exit / updated size for <%s/%s> / cycle %d\n",
     1717__FUNCTION__, this->process->pid, this->trdid, dir_name, dentry->name, cycle );
     1718#endif
     1719
     1720    return 0;
    16641721
    16651722}  // end fatfs_update_dentry()
     
    20442101error_t fatfs_cluster_alloc( uint32_t * searched_cluster )
    20452102{
    2046     uint32_t      page_id;        // page index in mapper
     2103    uint32_t      page_id;        // page index in FAT mapper
    20472104    uint32_t      slot_id;        // slot index in page (1024 slots per page)
    2048     uint32_t      hint;           // first free cluster index in FAT
     2105    uint32_t      cluster;        // first free cluster index in FAT
    20492106    uint32_t      free_clusters;  // total number of free clusters
    20502107    vfs_ctx_t   * vfs_ctx;        // local pointer on VFS context (same in all clusters)
     
    20802137    fat_fatfs_ctx = hal_remote_lpt( XPTR( mapper_cxy , &vfs_ctx->extend ) );
    20812138
    2082     // build relevant extended pointers in on free clusters info in FAT cluster
     2139    // build relevant extended pointers on free clusters info in mapper cluster
    20832140    lock_xp = XPTR( mapper_cxy , &fat_fatfs_ctx->free_lock );
    20842141    hint_xp = XPTR( mapper_cxy , &fat_fatfs_ctx->free_cluster_hint );
     
    20892146
    20902147    // get hint and free_clusters values from FATFS context
    2091     hint          = hal_remote_l32( hint_xp );
     2148    cluster       = hal_remote_l32( hint_xp ) + 1;
    20922149    free_clusters = hal_remote_l32( numb_xp );
    20932150       
    2094     // get page index & slot index for the first free cluster
    2095     page_id  = (hint + 1) >> 10;
    2096     slot_id  = (hint + 1) & 0x3FF;
    2097 
    2098     // get relevant page from mapper
    2099     page_xp = mapper_remote_get_page( mapper_xp , page_id );
    2100 
    2101     if( page_xp == XPTR_NULL )
    2102     {
    2103         printk("\n[ERROR] in %s : cannot acces FAT mapper\n", __FUNCTION__ );
    2104         return -1;
    2105     }
    2106 
    2107     // build extended pointer on free cluster slot
    2108     slot_xp = ppm_page2base( page_xp ) + (slot_id<<2);
    2109          
    21102151#if (DEBUG_FATFS_CLUSTER_ALLOC & 1)
    21112152if( DEBUG_FATFS_CLUSTER_ALLOC < cycle )
    2112 printk("\n[%s] thread[%x,%x] get free info / hint %x / free_clusters %x\n",
    2113 __FUNCTION__, this->process->pid, this->trdid, hint, free_clusters );
     2153printk("\n[%s] thread[%x,%x] get free info : hint %x / free_clusters %x\n",
     2154__FUNCTION__, this->process->pid, this->trdid, (cluster - 1), free_clusters );
    21142155#endif
    21152156
     
    21272168    }
    21282169
    2129     // check "hint"
     2170
     2171
     2172    // get page index & slot index for selected cluster
     2173    page_id  = cluster >> 10;
     2174    slot_id  = cluster & 0x3FF;
     2175
     2176    // get relevant page descriptor from mapper
     2177    page_xp = mapper_remote_get_page( mapper_xp , page_id );
     2178
     2179    if( page_xp == XPTR_NULL )
     2180    {
     2181        printk("\n[ERROR] in %s : cannot acces FAT mapper\n", __FUNCTION__ );
     2182        return -1;
     2183    }
     2184
     2185    // build extended pointer on selected cluster slot in FAT mapper
     2186    slot_xp = ppm_page2base( page_xp ) + (slot_id << 2);
     2187         
     2188    // check selected cluster actually free
    21302189    if( hal_remote_l32( slot_xp ) != FREE_CLUSTER )
    21312190    {
    2132         printk("\n[ERROR] in %s : illegal hint cluster\n", __FUNCTION__ );
     2191        printk("\n[ERROR] in %s : selected cluster %x not free\n", __FUNCTION__, cluster );
    21332192        remote_queuelock_acquire( lock_xp );
    21342193        return -1;
    21352194    }
    21362195
    2137     // update allocated cluster in FAT mapper
    2138     hal_remote_s32( slot_xp , END_OF_CHAIN_CLUSTER_MAX );
    2139 
    2140     // update free cluster info
    2141     fatfs_free_clusters_decrement( hint + 1 );
     2196    // update free cluster info in FATFS context
     2197    fatfs_free_clusters_decrement( mapper_cxy , fat_fatfs_ctx , cluster );
    21422198
    21432199    // release free clusters busylock
    21442200    remote_queuelock_release( lock_xp );
     2201
     2202    // update FAT mapper
     2203    hal_remote_s32( slot_xp , END_OF_CHAIN_CLUSTER_MAX );
     2204
     2205    // synchronously update FAT on device
     2206    fatfs_move_page( page_xp , IOC_SYNC_WRITE );
    21452207
    21462208#if DEBUG_FATFS_CLUSTER_ALLOC
    21472209cycle = (uint32_t)hal_get_cycles();
    21482210if( DEBUG_FATFS_CLUSTER_ALLOC < cycle )
    2149 printk("\n[%s] thread[%x,%x] exit / cluster %x / cycle %d\n",
    2150 __FUNCTION__, this->process->pid, this->trdid, hint + 1, cycle );
    2151 #endif
    2152 
    2153     *searched_cluster = hint + 1;
     2211printk("\n[%s] thread[%x,%x] exit / updated cluster %x in FAT / cycle %d\n",
     2212__FUNCTION__, this->process->pid, this->trdid, cluster, cycle );
     2213#endif
     2214
     2215    *searched_cluster = cluster;
    21542216    return 0;
    21552217
     
    21642226    xptr_t        mapper_xp;      // extended pointer on FAT mapper
    21652227    cxy_t         mapper_cxy;     // Fat mapper cluster identifier
     2228    mapper_t    * mapper_ptr;     // local pointer on FAT mapper
    21662229    xptr_t        lock_xp;        // extended pointer on lock protecting free clusters info.
    21672230    xptr_t        first_xp;       // extended pointer on inode extension
     
    22042267    loc_fatfs_ctx = vfs_ctx->extend;
    22052268
    2206     // get extended pointer and cluster on FAT mapper
     2269    // get pointers and cluster on FAT mapper
    22072270    mapper_xp  = loc_fatfs_ctx->fat_mapper_xp;
    22082271    mapper_cxy = GET_CXY( mapper_xp );
     2272    mapper_ptr = GET_PTR( mapper_xp );
    22092273   
    22102274    // get local pointer on FATFS context in FAT cluster
     
    22182282
    22192283    // call the recursive function to release all clusters from FAT mapper
    2220     if ( fatfs_recursive_release( mapper_xp , first_cluster ) )
     2284    if ( fatfs_recursive_release( mapper_cxy,
     2285                                  mapper_ptr,
     2286                                  fat_fatfs_ctx,
     2287                                  first_cluster ) )
    22212288    {
    22222289        printk("\n[ERROR] in %s : cannot update FAT mapper\n", __FUNCTION__ );
     
    23002367    {
    23012368        // get lba from FATFS context and page_id
    2302         uint32_t      lba        = fatfs_ctx->fat_begin_lba + (page_id << 3);
     2369        uint32_t      lba = fatfs_ctx->fat_begin_lba + (page_id << 3);
    23032370 
    23042371        // access device
     
    23112378        if( error ) return EIO;
    23122379
    2313 #if (DEBUG_FATFS_MOVE_PAGE & 0x1)
    2314 if( DEBUG_FATFS_MOVE_PAGE < cycle )
    2315 mapper_display_page( XPTR(page_cxy , mapper_ptr) , page_id );
    2316 #endif
    2317 
    23182380#if DEBUG_FATFS_MOVE_PAGE
    2319 cycle = (uint32_t)hal_get_cycles();
    23202381if( DEBUG_FATFS_MOVE_PAGE < cycle )
    23212382{
     
    23572418                                       page_id,
    23582419                                       &searched_cluster );
    2359             if( error )  return EIO;
     2420            if( error )
     2421            {
     2422                printk("\n[ERROR] in %s : cannot access FAT mapper\n", __FUNCTION__ );
     2423                return -1;
     2424            }
    23602425        }
    23612426
    23622427        // get lba from searched_cluster
    23632428        uint32_t lba = fatfs_lba_from_cluster( fatfs_ctx , searched_cluster );
     2429
     2430#if DEBUG_FATFS_MOVE_PAGE
     2431if( DEBUG_FATFS_MOVE_PAGE < cycle )
     2432{
     2433    if ( (cmd_type == IOC_READ) || (cmd_type == IOC_SYNC_READ) )
     2434    printk("\n[%s] thread[%x,%x] load page %d of <%s> / cluster_id %x / cycle %d\n",
     2435    __FUNCTION__, this->process->pid, this->trdid, page_id, name, searched_cluster, cycle );
     2436    else
     2437    printk("\n[%s] thread[%x,%x] sync page %d of <%s> / cluster_id %x / cycle %d\n",
     2438    __FUNCTION__, this->process->pid, this->trdid, page_id, name, searched_cluster, cycle );
     2439}
     2440#endif
    23642441
    23652442        // access device
     
    23702447        else                                  error = -1;
    23712448
    2372         if( error ) return EIO;
    2373 
    2374 #if (DEBUG_FATFS_MOVE_PAGE & 0x1)
    2375 if( DEBUG_FATFS_MOVE_PAGE < cycle )
    2376 mapper_display_page( XPTR(page_cxy , mapper_ptr) , page_id );
    2377 #endif
    2378 
    2379 #if DEBUG_FATFS_MOVE_PAGE
    2380 cycle = (uint32_t)hal_get_cycles();
    2381 if(DEBUG_FATFS_MOVE_PAGE < cycle)
    2382 {
    2383     if ( (cmd_type == IOC_READ) || (cmd_type == IOC_SYNC_READ) )
    2384         printk("\n[%s] thread[%x,%x] load page %d of <%s> inode / cycle %d\n",
    2385         __FUNCTION__, this->process->pid, this->trdid, page_id, name, cycle );
    2386     else
    2387         printk("\n[%s] thread[%x,%x] sync page %d of <%s> inode / cycle %d\n",
    2388         __FUNCTION__, this->process->pid, this->trdid, page_id, name, cycle );
    2389 }
    2390 #endif
    2391 
     2449        if( error )
     2450        {
     2451            printk("\n[ERROR] in %s : cannot access device\n", __FUNCTION__ );
     2452            return -1;
     2453        }
    23922454    }
    23932455
Note: See TracChangeset for help on using the changeset viewer.