LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-08-2010, 10:33 AM   #1
pupillo
LQ Newbie
 
Registered: Jul 2009
Posts: 22

Rep: Reputation: 15
Question does fwrite retry in case of error


Hi,
I'm using fwrite to write into a file stored in a normal USB flash drive.
My question is: if for example a sector is damaged, does fwrite try with another one before getting me a result?
If not, If I recall fwrite, will it try with a different one or with the same?
Regards
Tommaso
 
Old 02-08-2010, 11:17 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Buster (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

Rep: Reputation: 359Reputation: 359Reputation: 359Reputation: 359
If there is a sector failure on flash memory, the sector is rewritten to a spare one. This is invisible to the operating system (apart from taking longer). Only if it runs out of spare sectors will the operating system see a bad block error. So if fwrite reports a write error on a USB flash, there is no recovery possible by re-calling it.

Indeed, most file systems don't remap bad blocks on the fly, so this applies to any media, not just flash drives. The file systems have provision for bad blocks, but don't automatically add to the bad block list.

Last edited by neonsignal; 02-08-2010 at 11:19 AM.
 
Old 02-09-2010, 03:17 AM   #3
pupillo
LQ Newbie
 
Registered: Jul 2009
Posts: 22

Original Poster
Rep: Reputation: 15
So, let's suppose that my usb disk contains 1000 blocks whose 10 are currently full (full, not only partially used) and there are 990 spare blocks. Now I try to "modify" a byte within a file. The OS tries to rewrite the entire page in a new block. It gets an error, then it tries with another block without marking the previous as bad. It finally can.

If I modify once again that byte in the file, the OS will try again with the bad block (because it doesn't know that it's bad), it will get the error, then it will try with the other (good) block, whose 2nd sector will be successfully written (the 1st sector was written before).
Is that right?
Thanks
 
Old 02-09-2010, 04:20 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Buster (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

Rep: Reputation: 359Reputation: 359Reputation: 359Reputation: 359
No, the 'spare' blocks in a flash memory are not the unused ones, but blocks that are reserved for the purpose of replacing faulty ones. These are not directly accessible by the operating system, and there are not many of them (typically less than 1% of the flash capacity).

If there is a write failure, the flash itself will assign one of the reserved blocks to replace the bad one. The operating system doesn't see this, all that it means is that the write takes a few more cycles.

If the flash runs out of reserved blocks, the situation changes. When there is a write failure, the flash hardware will report the error. The operating system will log the error (and may retry the write to the same sector a number of times), but it will not try another block. If the retry continues to fail (as it probably will on a flash device), then the error is returned to the application (for example, the call to fwrite).

If you application calls fwrite again after a failure, it is as if the previous fwrite had succeeded; the next fwrite will just add more data to the end of the file. Obviously this is undesirable, so most applications will report the error to the user and abort.

On most filesystems, there is a means of testing a file storage device and 'removing' any bad blocks (eg using scandisk in Windows, or badblocks in Linux). These blocks are removed from the list of free blocks. However, this is not an automated process.

Last edited by neonsignal; 02-09-2010 at 04:37 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CUPS Error -- recoverable: Network host 'sandler' is busy; will retry in 30 seconds.. runnerbrian Red Hat 4 12-03-2011 06:26 AM
Mail is not going retry timeout exceed error Terry56 Linux - Networking 48 11-11-2009 01:00 AM
Disk error 01, AX = 4209, drive 9F Boot failed: press a key to retry. ... katemiz Linux - Hardware 1 09-11-2006 08:19 AM
dd retry on error? SDraconis Linux - General 4 06-12-2005 02:20 AM
urpmi how to avoid some files missing error (tho works on retry) peb Linux - Newbie 0 01-09-2004 10:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration