title: Out of space on NTFS disk after a series of same-sized files creation/deletion
body: <p>I've ran into a really weird problem while working on a large project. I write a bunch of <em>same-size</em> files on a partition (tried both RAM disks and virtual disks created via <code>diskmgmt.msc</code>). When there is not enough free space to fit another file (as reported by <code>GetDiskFreeSpaceExW</code>), I delete one <em>(only one)</em> of the previously created ones and write the new one. Then, I delete another old file and write a new one, ad infinitum (so, you may think of the partition as of a ring buffer of equally sized files). After a series of writes-deletes (from few hundreds to few thousands), I run into a <code>no free space</code> error while writing a new file (prior to which, <code>GetDiskFreeSpaceExW</code> reports enough space). I asked a few colleagues of mine to try and reproduce the problem on their hardware, but the problem <em>did not</em> resurface.</p>
  
  <p>To clarify things a bit, here's the exact algorithm:</p>
  
  <ol>
  <li>Choose file size (say, S bytes)</li>
  <li>Check free space with GetDiskFreeSpaceExW</li>
  <li>If free_space > S: write new file of size S and goto 2</li>
  <li>Else: Delete one file and goto 2</li>
  </ol>
  
  <p>It is important to note that I write data to files in blocks of size 4096 bytes (problem may or may not resurface depending on the block size). File size is 5MB. NTFS partition size is 21 MiB. Cluster size is 512 B (again, changing these parameters affects the results). With these parameters, the failure occurs during creation of the 684'th file. It doesn't depend on whether I use a RAM disk or a virtual disk (hence, it is not a problem of a particular implementation).</p>
  
  <p>I analyzed the resulting disk image dumps after the failure and found that the files were heavily fragmented. Chkdsk reports no problems neither before nor after the experiment. No errors were found in the system logs.</p>
  
  <p>Possibly relevant parameters of my netbook (Dell Inspiron 1110):</p>
  
  <ul>
  <li>Pentium SU4100, Relatively slow dual-core x64 CULV CPU (1.3 GHz)</li>
  <li>Windows 7 Ultimate x64 edition</li>
  <li>2 GB RAM</li>
  </ul>
  
  <p>Does anyone have any idea about what's going on and how to debug it? Where can I look for additional info? I'm out of ideas already, and I need to solve this issue as soon as possible...</p>
  
  <p><strong>UPD:</strong> the problem occurs when I'm writing file data (i.e. <code>write()</code> fails), <em>not</em> when I create the file. So, it doesn't look like I'm lacking MFT entries.</p>
  
  <p><strong>UPD2:</strong> answering a few of the questions that were asked</p>
  
  <ul>
  <li>The partition is a freshly formatted one, hence, no specific attributes on files, no directory structure, nothing</li>
  <li>Permissions are default</li>
  <li>No .lnk's, no hardlinks - _only_ the files I write</li>
  <li>All files are written to the root dir, no more directories are created</li>
  <li>Filenames are simply the ordinal numbers of files (i.e. 1, 2, 3, ...)</li>
  <li>No alternate data streams, files are created using `fopen()`, written to with `fwrite()` and closed with `fclose()`</li>
  <li>$Txf gets created, indeed</li>
  <li>No bad clusters, this is a virtual (or a RAM) disk</li>
  </ul>