os/kernelhwsrv/userlibandfileserver/fileserver/sfat/sl_drv.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // f32\sfat\sl_drv.cpp
    15 // 
    16 //
    17 
    18 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    19 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    20 //!!
    21 //!! WARNING!! DO NOT edit this file !! '\sfat' component is obsolete and is not being used. '\sfat32'replaces it
    22 //!!
    23 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    24 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    25 
    26 
    27 #include "sl_std.h"
    28 #include "sl_cache.h"
    29 
    30 const TInt KMaxRecoverableRetries=10;
    31 const TInt KMaxCriticalRetries=10;
    32 
    33 
    34 //---------------------------------------------------------------------------------------------------------------------------------------
    35 
    36 
    37 TFatDriveInterface::TFatDriveInterface() 
    38                    :iMount(NULL)
    39 {
    40 }
    41 
    42 /**
    43     Initialise the interface object.
    44     @param  aMount the CFatMountCB that owns this object
    45 */
    46 TBool TFatDriveInterface::Init(CFatMountCB* aMount)
    47 {
    48     ASSERT(aMount);
    49     iMount = aMount;
    50     aMount->LocalDrive()->SetMount(aMount);
    51     return iProxyDrive.Init(aMount->LocalDrive());
    52 }
    53 
    54 /**
    55     pseudo-destructor. 
    56 */
    57 void TFatDriveInterface::Close()
    58 {
    59      if(iMount)
    60         iMount->LocalDrive()->SetMount(NULL);
    61      iMount = NULL;
    62 }
    63 
    64 //---------------------------------------------------------------------------------------------------------------------------------------
    65 
    66 /**
    67     Read data from the media via CProxyDrive interface.
    68     This is non-critical read: on error Non-critical notifier is involved
    69 
    70     @param  aPos        absolute media position
    71     @param  aLength     how many bytes to read
    72     @param  aTrg        data descriptor
    73 
    74     @return KErrNone - success
    75     @return KErrNotReady - non-critical error
    76     @return KErrCorrupt - an illegal write is detected
    77     @return KErrBadPower - failure due to low power
    78 
    79 */
    80 TInt TFatDriveInterface::ReadNonCritical(TInt64 aPos, TInt aLength, TDes8& aTrg) const
    81 {
    82     TInt nRes = KErrNone;
    83     TInt cntRetry = KMaxRecoverableRetries;
    84 
    85     //__PRINT2(_L("#=+++ Read_nc1: pos:%LU, len:%u"), aPos, aLength);
    86 
    87     for(;;)
    88     {
    89         nRes = iProxyDrive.Read(aPos,aLength,aTrg);
    90         if (nRes==KErrNone)
    91             break;
    92 
    93         __PRINT4(_L("TFatDriveInterface::ReadNonCritical() failure! drv:%d Posl=%LU len=%d retval=%d"), iMount->DriveNumber(), aPos, aLength, nRes);
    94         
    95         if(--cntRetry <= 0)
    96         {
    97             nRes = KErrCorrupt;
    98             break;
    99         }
   100 
   101         nRes = HandleRecoverableError(nRes);
   102         if (nRes !=ERetry)
   103             break;
   104     }
   105 
   106     return nRes;
   107 }
   108 
   109 //---------------------------------------------------------------------------------------------------------------------------------------
   110 
   111 /**
   112     Read data from the media via CProxyDrive interface. 
   113     This is non-critical read: on error Non-critical notifier is involved
   114 
   115     @param  aPos        absolute media position
   116     @param  aLength     how many bytes to read
   117     @param  aTrg        data descriptor
   118     @param  aMessage
   119     @param  anOffset
   120 
   121     @return KErrNone - success
   122     @return KErrNotReady - non-critical error
   123     @return KErrCorrupt - an illegal write is detected
   124     @return KErrBadPower - failure due to low power
   125 
   126 */
   127 TInt TFatDriveInterface::ReadNonCritical(TInt64 aPos,TInt aLength,const TAny* aTrg,const RMessagePtr2 &aMessage,TInt anOffset) const
   128 {
   129     //__PRINT2(_L("#=+++ Read_nc2: pos:%LU, len:%u"), aPos, aLength);
   130 
   131     TInt nRes = KErrNone;
   132     TInt cntRetry = KMaxRecoverableRetries;
   133 
   134     for(;;)
   135     {
   136         nRes = iProxyDrive.Read(aPos, aLength, aTrg, aMessage, anOffset);
   137         if (nRes==KErrNone)
   138             break;
   139 
   140         __PRINT4(_L("TFatDriveInterface::ReadNonCritical() Failure! drv:%d aPosl=%d len=%d anOffset=%d"), iMount->DriveNumber(), aPos,aLength, anOffset);
   141         
   142         if(--cntRetry <= 0)
   143         {
   144             nRes = KErrCorrupt;
   145             break;
   146         }
   147 
   148         nRes = HandleRecoverableError(nRes);
   149         if (nRes !=ERetry)
   150             break;
   151     }
   152 
   153     return nRes;
   154 }
   155 
   156 //---------------------------------------------------------------------------------------------------------------------------------------
   157 
   158 /**
   159     Read data from the media via CProxyDrive interface with a critical notifier.
   160     This method shall be used to read critical filesystem data, such as directory entries, FAT data.
   161 
   162     @param  aPos        absolute media position
   163     @param  aLength     how many bytes to read
   164     @param  aTrg        data descriptor
   165 
   166     @return KErrNone - success
   167     @return KErrNotReady - non-critical error
   168     @return KErrCorrupt - an illegal write is detected
   169     @return KErrAbort - user aborted read
   170 */
   171 TInt TFatDriveInterface::ReadCritical(TInt64 aPos,TInt aLength,TDes8& aTrg) const
   172 {
   173     //__PRINT2(_L("#=+++ Read_C: pos:%LU, len:%u"), aPos, aLength);
   174 
   175     TInt nRes = KErrNone;
   176 
   177     for(;;)
   178     {
   179         nRes = iProxyDrive.Read(aPos, aLength, aTrg);
   180         if(nRes == KErrNone)
   181             break;
   182 
   183         __PRINT4(_L("TFatDriveInterface::ReadCritical() Error! drv:%d Posl=%LU len=%d retval=%d"), iMount->DriveNumber(), aPos, aLength, nRes);
   184         
   185         nRes=HandleCriticalError(nRes);
   186         if (nRes != ERetry)
   187             break;
   188     }
   189 
   190     return nRes;
   191 }
   192 
   193 //---------------------------------------------------------------------------------------------------------------------------------------
   194 
   195 /**
   196     Write data to the media via CProxyDrive interface.
   197     
   198     @param  aPos        absolute media position
   199     @param  aLength     how many bytes to write
   200     @param  aSrc        pointer to the data 
   201     @param  aMessage
   202     @param  anOffset
   203     
   204     @return KErrNone - success
   205     @return KErrNotReady - non-critical error
   206     @return KErrBadPower - write not attempted due to low batteries
   207     @return KErrCorrupt - an illegal write is detected
   208     @return KErrAccessDenied - write to protected media
   209 */
   210 TInt TFatDriveInterface::WriteNonCritical(TInt64 aPos, TInt aLength, const TAny* aSrc, const RMessagePtr2 &aMessage, TInt anOffset)
   211 {
   212     //__PRINT2(_L("#=+++ Write_NC: pos:%LU, len:%u"), aPos, aLength);
   213 
   214     
   215     TInt nRes = KErrNone;
   216     TInt cntRetry = KMaxRecoverableRetries;
   217 
   218     for(;;)
   219     {
   220         iMount->OpenMountForWrite(); //-- make a callback to CFatMountCB to perform some actions on 1st write.
   221         nRes = iProxyDrive.Write(aPos, aLength, aSrc, aMessage, anOffset);
   222         if (nRes==KErrNone)
   223             break;
   224 
   225         __PRINT4(_L("TFatDriveInterface::WriteNonCritical() failure! drv:%d, Pos=%LU len=%d anOffset=%d"), iMount->DriveNumber(), aPos, aLength, anOffset);
   226         
   227         if(--cntRetry <= 0)
   228         {
   229             nRes = KErrCorrupt;
   230             break;
   231         }
   232 
   233         nRes = HandleRecoverableError(nRes);
   234         if (nRes !=ERetry)
   235             break;
   236     }
   237 
   238 
   239     return nRes;
   240 }
   241 
   242 //---------------------------------------------------------------------------------------------------------------------------------------
   243 
   244 /**
   245     Write data to the media via CProxyDrive interface. On error this method can invoke a critical notifier.
   246     This method is intended to be called for the filesstem critical data, i.e. FAT metadata, such as directory entries,
   247     FAT table etc.
   248     
   249     @param  aPos  absolute media position
   250     @param  aSrc  descriptor with the source data
   251     
   252     @return KErrNone - success
   253     @return KErrNotReady - non-critical error
   254     @return KErrBadPower - write not attempted due to low batteries
   255     @return KErrCorrupt - an illegal write is detected
   256     @return KErrAccessDenied - write to protected media
   257 */
   258 TInt TFatDriveInterface::WriteCritical(TInt64 aPos, const TDesC8& aSrc)
   259 {
   260     //__PRINT2(_L("#=+++ Write_C: pos:%LU, len:%u"), aPos, aSrc.Length());
   261 
   262     TInt nRes = KErrNone;
   263 
   264 #ifdef _DEBUG
   265     
   266     TBool simulatedWriteFailure = EFalse; //-- if true it means that the write failure has been simulated
   267 
   268     //-- debug interface to simulate write failure
   269     if(iMount->IsWriteFail())
   270     {
   271         if(iMount->WriteFailCount() != 0)
   272         {
   273             iMount->DecWriteFailCount();
   274         }
   275         else
   276         {//-- simulate write failure
   277             if(iMount->WriteFailError()==-99)
   278                 UserSvr::ResetMachine(EStartupWarmReset);
   279             else
   280             {
   281                 //-- invalidate caches, because actual write to the drive isn't going to happen
   282                 if(iMount->RawDisk().DirCacheInterface())
   283                     iMount->RawDisk().DirCacheInterface()->InvalidateCache();
   284 
   285                 iMount->SetWriteFail(EFalse);
   286                 
   287                 TRAP_IGNORE(iMount->RawDisk().InvalidateUidCache()); //-- invalidate whole UID data cache
   288                 TRAP_IGNORE(iMount->FAT().InvalidateCacheL());       //-- invalidate whole FAT cache
   289                 
   290                 iMount->InvalidateLeafDirCache();
   291 
   292                 nRes = iMount->WriteFailError(); 
   293                 simulatedWriteFailure = ETrue; //-- won't perform actual write later
   294                 __PRINT4(_L("TFatDriveInterface::WriteCritical() Simulating write failure. drv:%d, aPos=%LU len=%d Code=%d"), iMount->DriveNumber(), aPos,aSrc.Length(),nRes);
   295 
   296             }
   297         }
   298     }//if(iMount->IsWriteFail())
   299 
   300     if(!simulatedWriteFailure)
   301 #endif // _DEBUG
   302     {
   303         //-- try to write data until success or user gives up
   304         for(;;)
   305         {
   306             for(TInt i=0; i<KMaxCriticalRetries; i++)
   307             {
   308                 iMount->OpenMountForWrite();  //-- make a callback to CFatMountCB to perform some actions on 1st write.
   309                 nRes=iProxyDrive.Write(aPos,aSrc);
   310                 if (nRes==KErrNone)
   311                     return nRes;
   312             }
   313 
   314             //-- write error occured
   315             __PRINT4(_L("TFatDriveInterface::WriteCritical() failure! drv:%d, aPos=%LU len=%d retval=%d"), iMount->DriveNumber(), aPos,aSrc.Length(),nRes);
   316 
   317             nRes=HandleCriticalError(nRes);
   318             if (nRes!=ERetry)
   319                 break;
   320 
   321         }//for(;;)
   322     
   323     }// if(!simulatedWriteFailure)
   324     
   325     return nRes;
   326 }
   327 
   328 
   329 /**
   330     Get Last Error Info from the proxy drive
   331         
   332     @param  aErrorInfo data descriptor for the error info.
   333     @return KErrNone - success, interrogate aErrorInfo for further info
   334     @return KErrNotSupported - media driver does not support
   335 */
   336 TInt TFatDriveInterface::GetLastErrorInfo(TDes8& aErrorInfo) const
   337 {
   338     return iProxyDrive.GetLastErrorInfo(aErrorInfo);
   339 }
   340 
   341 
   342 //---------------------------------------------------------------------------------------------------------------------------------------
   343 
   344 /**
   345     Handle critical error
   346 
   347     @param aResult result from the media driver (error code)
   348 
   349     @return ERetry - Attempt operation again
   350     @return KErrAbort - User aborted notifier
   351     @return KErrAccessDenied - media is read only
   352     @return KErrCorrupt - cf-card is corrupt
   353 */
   354 TInt TFatDriveInterface::HandleCriticalError(TInt aResult) const
   355     {
   356     __PRINT2(_L("TFatDriveInterface::HandleCriticalError drv:%d, code:%d"), iMount->DriveNumber(),aResult);
   357 
   358     TLocaleMessage line1;
   359     TLocaleMessage line2;
   360 
   361     TInt r=KErrAbort;
   362 
   363     if (aResult==KErrLocked)
   364     {
   365         r=KErrLocked;
   366         goto End;
   367     }
   368 
   369     if (aResult==KErrAccessDenied)
   370         {
   371         r=KErrAccessDenied;
   372         goto End;
   373         }
   374     
   375     if (aResult==KErrArgument || aResult==KErrBadDescriptor)
   376         {
   377         r=KErrCorrupt;
   378         goto End;
   379         }
   380 
   381     if (iMount->Drive().IsChanged())
   382         {//-- check if the media we accessing is the same as it used to be
   383           if(iMount->CheckVolumeTheSame())
   384             {//-- the media is the same
   385             if(!IsDriveWriteProtected())
   386                 {
   387                 iMount->Drive().SetChanged(EFalse);
   388                 r=ERetry;
   389                 goto End;
   390                 }
   391             }
   392         }
   393 
   394     if (aResult==KErrAbort && !iMount->Drive().IsChanged())
   395         {
   396         r=ERetry;
   397         goto End;
   398         }
   399 
   400     if (aResult==KErrBadPower)
   401         {
   402         line1=EFileServer_LowPowerLine1;
   403         line2=EFileServer_LowPowerLine2;
   404         }
   405     else if (iMount->Drive().IsChanged())
   406         {
   407         line1=EFileServer_PutTheCardBackLine1;
   408         line2=EFileServer_PutTheCardBackLine2;
   409         }
   410     else
   411         {
   412         line1=EFileServer_DiskErrorLine1;
   413         line2=EFileServer_DiskErrorLine2;
   414         }
   415     
   416     if (NotifyUser())
   417         {
   418         FOREVER
   419             {
   420             TInt buttonVal;
   421             TInt ret=iMount->Notifier()->Notify(TLocaleMessageText(line1),
   422                                                 TLocaleMessageText(line2),
   423                                                 TLocaleMessageText(EFileServer_Button1),
   424                                                 TLocaleMessageText(EFileServer_Button2),
   425                                                 buttonVal);
   426             if (ret!=KErrNone)
   427                 break; 
   428             if (buttonVal!=1)
   429                 break; // Abort
   430 
   431             if (iMount->Drive().IsChanged())
   432                 {
   433 //
   434 // Without this code, retry will indiscriminately write over whatever disk happens to be present.
   435 // However if the write error is to the bootsector remounting will always fail because the boot
   436 // sector will have changed and hence the disk is useless.
   437 // 
   438                 if(!iMount->CheckVolumeTheSame())
   439                     continue; //-- the media isn't the same as originally mounted; continue asking
   440 
   441                 if(IsDriveWriteProtected())
   442                     continue; //-- still can not write to the drive
   443 
   444 
   445                 iMount->Drive().SetChanged(EFalse);
   446                 }
   447 
   448             r=ERetry; // Retry
   449             break;
   450             }
   451         }
   452 End:
   453     return(r);
   454     }
   455 
   456 //---------------------------------------------------------------------------------------------------------------------------------------
   457 
   458 /**
   459     Handle recoverable error
   460 
   461     @param aResult result from the media driver (error code)
   462 
   463     @return ERetry - retry write
   464     @return KErrCorrupt - media is corrupt
   465     @return KErrBadPower - low power failure
   466     @return KErrNotReady - non-critical error
   467 */
   468 TInt TFatDriveInterface::HandleRecoverableError(TInt aResult) const
   469     {
   470     __PRINT2(_L("TFatDriveInterface::HandleRecoverableError drv:%d, code:%d"), iMount->DriveNumber(),aResult);
   471 
   472     if (aResult==KErrAccessDenied)
   473         return(KErrAccessDenied);
   474     if (aResult == KErrLocked)
   475         return KErrLocked;
   476     if (aResult==KErrArgument || aResult==KErrBadDescriptor)
   477         return(KErrCorrupt);
   478     if (aResult==KErrBadPower)
   479         return(KErrBadPower);
   480     if (aResult==KErrDied)  // client thread died
   481         return(KErrDied);
   482     if (iMount->Drive().IsChanged())
   483         {
   484 
   485         if(! iMount->CheckVolumeTheSame())
   486             {//-- the media is different now.
   487             return KErrNotReady;
   488             }
   489         else if(!IsRecoverableRemount())
   490             {
   491             return KErrAccessDenied;
   492             }
   493         }
   494     return(ERetry);
   495     }   
   496 
   497 /** @return true if the mount can be remounted for a recoverable error */
   498 TBool TFatDriveInterface::IsRecoverableRemount() const
   499     {
   500     if(IsDriveWriteProtected()&&(iMount->Drive().IsWriteableResource()||iMount->Drive().IsCurrentWriteFunction()))
   501         return(EFalse);
   502     return(ETrue);
   503     }
   504 
   505 /** return true if the media is write protected */
   506 TBool TFatDriveInterface::IsDriveWriteProtected() const
   507     {
   508     TLocalDriveCapsV2Buf localDriveCaps;
   509     TInt r=iProxyDrive.Caps(localDriveCaps);
   510 
   511     if(r!=KErrNone)
   512         return(EFalse);
   513 
   514     return((localDriveCaps().iMediaAtt&KMediaAttWriteProtected)!=0);
   515     }
   516 
   517 
   518 
   519 //---------------------------------------------------------------------------------------------------------------------------------------
   520 
   521 TFatDriveInterface::XProxyDriveWrapper::XProxyDriveWrapper() 
   522                    :iLocalDrive(0) 
   523 {
   524     TInt nRes = iLock.CreateLocal();
   525     ASSERT(nRes == KErrNone);
   526     (void)nRes; 
   527 }
   528 
   529 
   530 TFatDriveInterface::XProxyDriveWrapper::~XProxyDriveWrapper() 
   531 {
   532     iLock.Close();
   533 }
   534 
   535 /** 
   536     Initialise interface wrapper.
   537     @param  aProxyDrive pointer to the raw drive access interface
   538     @return true on success
   539 */
   540 TBool TFatDriveInterface::XProxyDriveWrapper::Init(CProxyDrive* aProxyDrive) 
   541 {
   542     ASSERT(aProxyDrive);
   543     if(!iLock.Handle()) //-- the mutex must have been created by constructor
   544         return EFalse;
   545     
   546     iLocalDrive = aProxyDrive;
   547     return ETrue;
   548 }
   549 
   550 //-- see original TFatDriveInterface methods
   551 
   552 TInt TFatDriveInterface::XProxyDriveWrapper::Read(TInt64 aPos,TInt aLength,const TAny* aTrg,const RMessagePtr2 &aMessage,TInt anOffset) const
   553 {
   554     EnterCriticalSection();
   555     TInt nRes = iLocalDrive->Read(aPos, aLength, aTrg, aMessage.Handle(), anOffset);
   556     LeaveCriticalSection();
   557     return nRes;
   558 }
   559        
   560 TInt TFatDriveInterface::XProxyDriveWrapper::Read(TInt64 aPos,TInt aLength,TDes8& aTrg) const
   561 {
   562     EnterCriticalSection();
   563     TInt nRes = iLocalDrive->Read(aPos, aLength, aTrg);
   564     LeaveCriticalSection();
   565     return nRes;
   566 }
   567 
   568 TInt TFatDriveInterface::XProxyDriveWrapper::Write(TInt64 aPos,TInt aLength,const TAny* aSrc,const RMessagePtr2 &aMessage,TInt anOffset)
   569 {
   570     EnterCriticalSection();
   571     TInt nRes = iLocalDrive->Write(aPos, aLength, aSrc, aMessage.Handle(), anOffset);
   572     LeaveCriticalSection();
   573     return nRes;
   574 }
   575 
   576 TInt TFatDriveInterface::XProxyDriveWrapper::Write(TInt64 aPos, const TDesC8& aSrc)
   577 {
   578     EnterCriticalSection();
   579     TInt nRes = iLocalDrive->Write(aPos, aSrc);
   580     LeaveCriticalSection();
   581     return nRes;
   582 }
   583 
   584 TInt TFatDriveInterface::XProxyDriveWrapper::GetLastErrorInfo(TDes8& aErrorInfo) const
   585 {
   586     EnterCriticalSection();
   587     TInt nRes = iLocalDrive->GetLastErrorInfo(aErrorInfo);
   588     LeaveCriticalSection();
   589     return nRes;
   590 }
   591 
   592 TInt TFatDriveInterface::XProxyDriveWrapper::Caps(TDes8& anInfo) const
   593 {
   594     EnterCriticalSection();
   595     TInt nRes = iLocalDrive->Caps(anInfo);
   596     LeaveCriticalSection();
   597     return nRes;
   598 }
   599 
   600 
   601 
   602 
   603 
   604 
   605 
   606 
   607 
   608 
   609 
   610