title: Block ram disk fails to read/write with offset
body: <p>I'm creating a very very simple block RAM disk based on sbull.</p>
  
  <p>So far it works fine if I read/write blocks of data using dd, but whenever I try mounting a filesystem on it (and sometimes creating a file system) my driver crashes.</p>
  
  <p>After long weeks of debugging, I finally found out what is wrong, even though I can't really find a way to solve the problem. Hence my question here :)</p>
  
  <p>Whenever a user space application creates a request to the device WITH AN OFFSET, the driver won't work! Let me show you the source code in order to clarify:</p>
  
  <p>First of all, I'm handling requests using mk_request (not using a request_queue):</p>
  
  <pre><code>static void escsi_mk_request(struct request_queue *q, struct bio *bio)
  {
          struct block_device *bdev = bio-&gt;bi_bdev;
          struct escsi_dev *esd = bdev-&gt;bd_disk-&gt;private_data;
          int rw;
          struct bio_vec *bvec;
          sector_t sector;
          int i;
          int err = -EIO;
  
          printk("request received nr. sectors = %lu\n",bio_sectors(bio));
  
          sector = bio-&gt;bi_sector;
          if (bio_end_sector(bio) &gt; get_capacity(bdev-&gt;bd_disk))
                  goto out;
  
          if (unlikely(bio-&gt;bi_rw &amp; REQ_DISCARD)) {
                  err = 0;
                  goto out;
          }
  
          rw = bio_rw(bio);
          if (rw == READA)
              rw = READ;
  
          bio_for_each_segment(bvec, bio, i) {
                  unsigned int len = bvec-&gt;bv_len;
                  err = esd_do_bvec(esd, bvec-&gt;bv_page, len, bvec-&gt;bv_offset, rw, sector);
                  if (err) {
                          printk("err!\n");
                          break;
                  }
                  sector += len &gt;&gt; SECTOR_SHIFT;
          }
  
  out:
          bio_endio(bio, err);
  }
  </code></pre>
  
  <p>The esd_do_bvec function:</p>
  
  <pre><code>static int esd_do_bvec(struct escsi_dev *esd, struct page *page,
                           unsigned int len, unsigned int off, int rw,
                           sector_t sector)
   {
              void *mem;
              int err = 0;
              unsigned int offset;
              int i;
  
          offset = off + sector * 512;
  
          printk("ESD RW=%d, len=%d, off=%d, offset=%d, sector=%lu\n",rw,len,off,offset,sector);
  
          mem = kmap_atomic(page);
          if (rw == READ) {
                  memcpy(mem,esd-&gt;data+offset,len);
          } else {
                  memcpy(esd-&gt;data+offset,mem,len);
          }
          kunmap_atomic(mem);
  
  out:
          return err;
  }
  </code></pre>
  
  <p>OK, so basically when I read or write data using dd, the variable "off" in esd_do_bvec() is always 0, regardless of where and how many bytes I want to write. The file system obviously always performs I/O in 4KB chunks and will write a full block even when only one byte needs to be replaced. </p>
  
  <p>I am sure that reads and writes are working correctly when there's no offset because I created a file that is the same size as my block RAM disk and dumped the entire file into my device using dd, then got the output of the device (also using dd), and the input and output files are exactly the same. I also wrote the same file into a brd (Linux kernel original block RAM disk driver) and the outputs are the same comparing my device and the brd device.</p>
  
  <p>BUT -- in some specific situations I try to mount or create a new file system on my device and somehow it gets I/O requests with an offset, and at that point my driver fails. I assume that I'm not handling the offset properly. For example, when I try "mount -t ext2 /dev/esda":</p>
  
  <pre><code>linux-xjwl:/home/phil/escsi # mount /dev/esda -t ext2 /mnt/esda1/
  mount: wrong fs type, bad option, bad superblock on /dev/esda,
         missing codepage or helper program, or other error
         In some cases useful info is found in syslog - try
         dmesg | tail or so
  linux-xjwl:/home/phil/escsi # dmesg|tail -n 10
  [ 2239.275901] ESD RW=0, len=4096, off=0, offset=16384, sector=32
  [ 2239.275947] request received nr. sectors = 8
  [ 2239.275959] ESD RW=0, len=4096, off=0, offset=4096, sector=8
  [ 2239.276516] request received nr. sectors = 8
  [ 2239.276537] ESD RW=0, len=4096, off=0, offset=2097152, sector=4096
  [ 2239.276606] request received nr. sectors = 8
  [ 2239.276626] ESD RW=0, len=4096, off=0, offset=28672, sector=56
  [ 2239.277535] request received nr. sectors = 2
  [ 2239.277535] ESD RW=0, len=1024, off=1024, offset=2048, sector=2
  [ 2239.277535] EXT4-fs (esda): VFS: Can't find ext4 filesystem
  </code></pre>
  
  <p>(p.s.: the output shows "EXT4" but I am running with "-t ext2")</p>
  
  <p>I have checked the contents of sector n. 2 in my device and it does contain the ext2 metadata (since I ran mkfs.ext2 prior to trying to mount, of course). So I believe there's a problem with the offset. So far I can't really debug my driver because I wasn't able to come up with a request which would cause an I/O request with an offset (e.g., if I try writing a single byte into my device, Linux will read the whole block and rewrite it with only one different byte).</p>
  
  <p>Hope it's not a too simple question for you.</p>
  
  <p>Thanks in advance,
  Phil</p>
  
  <hr>
  
  <hr>
  
  <hr>
  
  <p>Please see the answer provided by Peter below.</p>
  
  <p>If you're wondering what the esd_do_bvec() function looks like now, here it comes:</p>
  
  <pre><code>static int esd_do_bvec(struct escsi_dev *esd, char *buf,
                          unsigned int len, int rw, sector_t sector)
  {
          int err = 0;
          unsigned int offset;
  
          // Please notice that we STILL have an offset to deal with, but
          // this offset comes in sectors and needs to be converted to a
          // a byte offset.
          offset = sector &lt;&lt; SECTOR_SHIFT; // or multiply by 512
  
          //printk("ESD RW=%d, len=%d, off=%d, offset=%d, sector=%lu\n",rw,len,off,offset,sector);
  
          if (rw == READ) {
                  memcpy(buf,esd-&gt;data+offset,len);
          } else {
                  memcpy(esd-&gt;data+offset,buf,len);
          }
          return err;
  }
  </code></pre>