title: Ddrescue(GNU) algorithm
body: <p><a href="http://www.gnu.org/software/ddrescue/manual/ddrescue_manual.html" rel="nofollow">http://www.gnu.org/software/ddrescue/manual/ddrescue_manual.html</a></p>
  
  <blockquote>
    <p>The algorithm of ddrescue is as follows (the user may interrupt the
    process at any point, but be aware that a bad drive can block ddrescue
    for a long time until the kernel gives up):</p>
    
    <p>1) Optionally read a logfile describing the status of a multi-part or
    previously interrupted rescue. If no logfile is specified or is empty
    or does not exist, mark all the rescue domain as non-tried.</p>
    
    <p>2) (First phase; Copying) Read the non-tried parts of the input file,
    marking the failed blocks as non-trimmed and skipping beyond them,
    until all the rescue domain is tried. Only non-tried areas are read in
    large blocks. Trimming, splitting and retrying are done sector by
    sector. Each sector is tried at most two times; the first in this step
    as part of a large block read, the second in one of the steps below as
    a single sector read.</p>
    
    <p>3) (Second phase; Trimming) Read forwards one sector at a time from
    the leading edge of the largest non-trimmed block, until a bad sector
    is found. Then read backwards one sector at a time from the trailing
    edge of the same block, until a bad sector is found. For each
    non-trimmed block, mark the bad sectors found as bad-sector and mark
    the rest of that block as non-split. Repeat until there are no more
    non-trimmed blocks.</p>
    
    <p>4) (Third phase; Splitting) Read forwards one sector at a time from
    the center of the largest non-split block, until a bad sector is
    found. Then read backwards one sector at a time from the center of the
    same block, until a bad sector is found. If the logfile is larger than
    <code>--logfile-size', read the smallest non-split blocks until the number
    of entries in the logfile drops below</code>--logfile-size'. Repeat until
    all remaining non-split blocks have less than 5 sectors. Then read the
    remaining non-split blocks sequentially.</p>
    
    <p>5) (Fourth phase; Retrying) Optionally try to read again the bad
    sectors until the specified number of retries is reached.</p>
    
    <p>6) Optionally write a logfile for later use.</p>
  </blockquote>
  
  <p>My understanding:
  ** When he is copying **
  He just copies blocks one by one, never retreating to copy old blocks. Any block containing a defective sector is marked bad (the entire block). Bad blocks are marked untrimmed.</p>
  
  <p>Legend: </p>
  
  <pre><code>=1 2 3= is a bad BLOCK
  [1 2 3] is a good block
  x is a bad sector
  @ is the read cursor
  in a =1 2 3= center sector(2) is always bad
  </code></pre>
  
  <p><strong>When he is Trimming</strong>
  Read forwards one sector at a time from the leading edge of the largest non-trimmed block, until a bad sector is found.</p>
  
  <pre><code>=1 2 3= [4 5 6] =7 8 9=@ [10 11 12] =13 14 15=
  
  =1 2 3= [4 5 6] =7 8 9= [10 11 12] =13 @14 15=
  </code></pre>
  
  <p>Then read backwards one sector at a time from the trailing edge of the same block, until a bad sector is found.</p>
  
  <pre><code>=1 2 3= [4 5 6] @=7 8 9= [10 11 12] =13 14 15=
  
  =1 @2 3= [4 5 6] =7 8 9= [10 11 12] =13 14 15=
  </code></pre>
  
  <p>For each non-trimmed block, mark the bad sectors found as bad-sector and mark the rest of that block as non-split.</p>
  
  <pre><code>=1 x 3= [4 5 6] =7 8 9= [10 11 12] =13 x 15=
  </code></pre>
  
  <ol>
  <li><p>Why is he doing this?? Why can't he just copy linearly and every time he encounters a bad sector, just skip it - seems to me all he's doing is extracting working sectors from a bad block.</p></li>
  <li><p>I got stuck in the 'Trimming' and Splitting is worse - can someone read that document explain it all!</p></li>
  <li><p>How can he even retry! He can retry the bad sectors but since he's already trimmed the blocks and written them out - how will he insert the recovered data that comes from retrying?</p></li>
  </ol>