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