title: Why vfs_write() adds arbitrary number of zero bytes
body: <p>I am on the kernel 2.6.37 on an embedded system writing some very specific module driver. And I need to store some data into a file. So, when the module is 'insmoded' I do:</p>
  <pre><code>static struct file *filp;
  static loff_t pos, mycnt;
  
  static __init int mymodule_init(void) {
  oldfs = get_fs();
  set_fs(get_ds());
  filp = filp_open(&quot;/home/mydata.txt&quot;, O_WRONLY|O_CREAT, 0644);
  set_fs(oldfs);
  pos = 0; mycnt=0;
  ......
  </code></pre>
  <p>and then, when needed, the function to write data is called:</p>
  <pre><code>static int mywrite(const char __user *buf, size_t count) {
  char str[256]; int i, j;
  mm_segment_t oldfs;
  int ret;
  //Filter out zeroes, if exist
  for(i = 0; i &lt; 255; i++) {
      if (i == count) break;
      if (buf[i] == 0) break;
      str[i+1] = buf[i];
  }
  str[0]=i++;  //Put length in a first byte
  //Checking for zeroes ------
  for(j = 0; j &lt; i; j++) {
      if (str[j] == 0) {
          printk(&quot;0 exist!\n&quot;);
          str[j] = 0x02; }
  }
  //Write 'str' to the file ------
  oldfs = get_fs();
  set_fs(get_ds());
  pos = mycnt;
  ret = vfs_write(filp, str, i, &amp;pos);
  filp-&gt;f_pos = pos;
  mycnt += i;
  if (mycnt != pos) {
      pos = mycnt;
      vfs_llseek(filp, pos, SEEK_SET);
      printk(&quot;Corrected!!!!!!!!!!!!!!!\n&quot;); }
  set_fs(oldfs);
  //Checking for zeroes again ------
  for(j = 0; j &lt; i; j++) {
      if (str[j] == 0)
          printk(&quot;0 exist!\n&quot;);
  }
  return count;
  }
  </code></pre>
  <p>Now, the problem is that the resulting file contains sometimes (rare) blocks of zeroes which shouldn't be there. You can see in the writing function code special code to check that there is no zeroes, only supposed ASCII normal text. Also the first byte of each string contains its length. And looking into the file I can see that no zeroes are counted by it. Still, these zeroes are there.
  Also, I compared the 'pos' after vfs_write with expected value - it differs exactly the same number of times as zeroes are inserted (3 times in my test example of 20KB file). And &quot;correction&quot; does not work...<br />
  So, what do I do wrong?</p>