Skip to contentSkip to conversation

VTPM mini-os: posix IO layer for blkfront in mini-os

PATCH In review Matthew Fioravante 2 participants 6 messages Mar 11, 2011 – Mar 14, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 2/12 VTPM mini-os: posix IO layer for blkfront in mini-os

The following patch adds POSIX IO for the blkfront device driver.

If mini-os is built with #define HAVE_LIBC then one can use blkfront_open() to obtain a file descriptor to a block device. read(), write(), lseek() and fstat() can be used on the file descriptor to read and write to the block device in a syncronous manner.

Signed off by: Matthew Fioravante <[email protected]>

extras/mini-os/blkfront.c +112 -0
diff -Naur xen-unstable.hg-pristine/extras/mini-os/blkfront.c xen-unstable.hg/extras/mini-os/blkfront.c
@@ -546,9 +546,121 @@
 #ifdef HAVE_LIBC
 int blkfront_open(struct blkfront_dev *dev)
 {
+    /* Silently prevent multiple opens */
+    if(dev->fd != -1) {
+       return dev->fd;
+    }
     dev->fd = alloc_fd(FTYPE_BLK);
     printk("blk_open(%s) -> %d\n", dev->nodename, dev->fd);
     files[dev->fd].blk.dev = dev;
+    files[dev->fd].blk.offset = 0;
     return dev->fd;
 }
+
+int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int write) 
+{
+   struct blkfront_dev* dev = files[fd].blk.dev;
+   off_t offset = files[fd].blk.offset;
+   struct blkfront_aiocb aiocb;
+   unsigned long long disksize = dev->info.sectors * dev->info.sector_size;
+   unsigned int blocksize = dev->info.sector_size;
+
+   int blknum;
+   int blkoff;
+   size_t bytes;
+   int rc = 0;
+
+   /* RW 0 bytes is just a NOP */
+   if(count == 0) {
+      return 0;
+   }
+   /* Check for NULL buffer */
+   if( buf == NULL ) {
+      errno = EFAULT;
+      return -1;
+   }
+   /* Write mode checks */
+   if(write) {
+      /*Make sure we have write permission */
+      if(dev->info.info & VDISK_READONLY || dev->info.mode != O_RDWR) {
+	 errno = EACCES;
+	 return -1;
+      }
+      /*Make sure disk is big enough for this write */
+      if(offset + count > disksize) {
+	 errno = ENOSPC;
+	 return -1;
+      }
+   }
+   /* Read mode checks */
+   else 
+   {
+      /*If the requested read is bigger than the disk, just 
+       * read as much as we can until the end */
+      if(offset + count > disksize) {
+	 count = offset >= disksize ? 0 : disksize - offset;
+      }
+   }
+   /* Determine which block to start at and which offset inside of it */
+   blknum = offset / blocksize;
+   blkoff = offset % blocksize;
+
+   /* Setup aiocb block object */
+   aiocb.aio_dev = dev;
+   aiocb.aio_buf = _xmalloc(blocksize, dev->info.sector_size);
+   aiocb.aio_nbytes = blocksize;
+   aiocb.aio_offset = blknum * blocksize;
+   aiocb.aio_cb = NULL;
+   aiocb.data = NULL;
+
+   rc = count;
+   while(count > 0) {
+      /* determine how many bytes to read/write from/to the current block buffer */
+      bytes = count > (blocksize - blkoff) ? blocksize - blkoff : count;
+
+      /* read operation */
+      if(!write) {
+	 aiocb.aio_cb = NULL;
+	 blkfront_read(&aiocb);
+	 memcpy(buf, &aiocb.aio_buf[blkoff], bytes);
+      } 
+      /* Write operation */
+      else {
+	 /* If we're writing a partial block, we need to read the current contents first
+	  * so we don't overwrite the extra bits with garbage */
+	 if(blkoff != 0 || bytes < blocksize) {
+	    aiocb.aio_cb = NULL;
+	    blkfront_read(&aiocb);
+	 }
+	 aiocb.aio_cb = NULL;
+	 memcpy(&aiocb.aio_buf[blkoff], buf, bytes);
+	 blkfront_write(&aiocb);
+      }
+      /* Will start at beginning of all remaining blocks */
+      blkoff = 0;
+
+      /* Increment counters and continue */
+      count -= bytes;
+      buf += bytes;
+      aiocb.aio_offset += blocksize;
+   }
+
+   free(aiocb.aio_buf);
+   files[fd].blk.offset += rc;
+   return rc;
+
+}
+
+int blkfront_posix_fstat(int fd, struct stat* buf) 
+{
+   struct blkfront_dev* dev = files[fd].blk.dev;
+
+   buf->st_mode = dev->info.mode;
+   buf->st_uid = 0;
+   buf->st_gid = 0;
+   buf->st_size = dev->info.sectors * dev->info.sector_size;
+   buf->st_atime = buf->st_mtime = buf->st_ctime = time(NULL);
+
+   return 0;
+}
 #endif
extras/mini-os/include/blkfront.h +10 -0
diff -Naur xen-unstable.hg-pristine/extras/mini-os/include/blkfront.h xen-unstable.hg/extras/mini-os/include/blkfront.h
@@ -28,7 +28,17 @@
 };
 struct blkfront_dev *init_blkfront(char *nodename, struct blkfront_info *info);
 #ifdef HAVE_LIBC
+#include <sys/stat.h>
+/* POSIX IO functions:
+ * use blkfront_open() to get a file descriptor to the block device
+ * Don't use the other blkfront posix functions here directly, instead use 
+ * read(), write(), lseek() and fstat() on the file descriptor
+ */
 int blkfront_open(struct blkfront_dev *dev);
+int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int write);
+#define blkfront_posix_write(fd, buf, count) blkfront_posix_rwop(fd, (uint8_t*)buf, count, 1)
+#define blkfront_posix_read(fd, buf, count) blkfront_posix_rwop(fd, (uint8_t*)buf, count, 0)
+int blkfront_posix_fstat(int fd, struct stat* buf);
 #endif
 void blkfront_aio(struct blkfront_aiocb *aiocbp, int write);
 #define blkfront_aio_read(aiocbp) blkfront_aio(aiocbp, 0)
extras/mini-os/lib/sys.c +35 -2
diff -Naur xen-unstable.hg-pristine/extras/mini-os/lib/sys.c xen-unstable.hg/extras/mini-os/lib/sys.c
@@ -270,6 +270,8 @@
 	    }
 	    return ret * sizeof(union xenfb_in_event);
         }
+	case FTYPE_BLK:
+	    return blkfront_posix_read(fd, buf, nbytes);
 	default:
 	    break;
     }
@@ -300,6 +302,8 @@
 	case FTYPE_TAP:
 	    netfront_xmit(files[fd].tap.dev, (void*) buf, nbytes);
 	    return nbytes;
+	case FTYPE_BLK:
+	    return blkfront_posix_write(fd, buf, nbytes);
 	default:
 	    break;
     }
@@ -310,8 +314,35 @@
 
 off_t lseek(int fd, off_t offset, int whence)
 {
-    errno = ESPIPE;
-    return (off_t) -1;
+    switch(files[fd].type) {
+       case FTYPE_BLK:
+	  switch (whence) {
+	     case SEEK_SET:
+		files[fd].file.offset = offset;
+		break;
+	     case SEEK_CUR:
+		files[fd].file.offset += offset;
+		break;
+	     case SEEK_END: 
+		{
+		   struct stat st;
+		   int ret;
+		   ret = fstat(fd, &st);
+		   if (ret)
+		      return -1;
+		   files[fd].file.offset = st.st_size + offset;
+		   break;
+		}
+	     default:
+		errno = EINVAL;
+		return -1;
+	  }
+	  return files[fd].file.offset;
+	  break;
+       default: /* Not implemented on this FTYPE */
+	  errno = ESPIPE;
+	  return (off_t) -1;
+    }
 }
 
 int fsync(int fd) {
@@ -412,6 +445,8 @@
 	    buf->st_ctime = time(NULL);
 	    return 0;
 	}
+	case FTYPE_BLK:
+	   return blkfront_posix_fstat(fd, buf);
 	default:
 	    break;
     }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

extras/mini-os/blkfront.c line 585

Quoted 2 lines · Matthew Fioravante, le Fri 11 Mar 2011 17:34:26 -0500, a écrit :
extras/mini-os/blkfront.c +2 -0
+      /*Make sure we have write permission */
+      if(dev->info.info & VDISK_READONLY || dev->info.mode != O_RDWR) {

O_WRONLY too.

Quoted 4 lines
extras/mini-os/blkfront.c +4 -0
+   aiocb.aio_dev = dev;
+   aiocb.aio_buf = _xmalloc(blocksize, dev->info.sector_size);
+   aiocb.aio_nbytes = blocksize;
+   aiocb.aio_offset = blknum * blocksize;

extras/mini-os/blkfront.c line 626

Quoted 10 lines
extras/mini-os/blkfront.c +9 -0
+   aiocb.aio_cb = NULL;
+   aiocb.data = NULL;
+
 
+      /* read operation */
+      if(!write) {
+	 aiocb.aio_cb = NULL;
+	 blkfront_read(&aiocb);
+	 memcpy(buf, &aiocb.aio_buf[blkoff], bytes);
+      } 

Could you perhaps optimize when buf is actually aligned? That would save a copy.

extras/mini-os/blkfront.c line 632

Quoted 6 lines
extras/mini-os/blkfront.c +6 -0
+      /* Write operation */
+      else {
+	 /* If we're writing a partial block, we need to read the current contents first
+	  * so we don't overwrite the extra bits with garbage */
+	 if(blkoff != 0 || bytes < blocksize) {
+	    aiocb.aio_cb = NULL;

Maybe blkfront_aio_cb should do it itself? It looks odd to have to do it when reusing an aiocb structure.

Apart from that it looks good.

Samuel

#2 lore raw
Quoted 4 lines · Samuel Thibault

Review

Quoted 2 lines · Matthew Fioravante, le Fri 11 Mar 2011 17:34:26 -0500, a écrit :
(quoted diff) +2 -0
+      /*Make sure we have write permission */
+      if(dev->info.info&  VDISK_READONLY || dev->info.mode != O_RDWR) {

O_WRONLY too.

Good catch, actually testing a bitfield with != is a bad idea to begin with anyway.

Quoted 7 lines
Quoted 7 lines
extras/mini-os/blkfront.c +7 -0
+   aiocb.aio_dev = dev;
+   aiocb.aio_buf = _xmalloc(blocksize, dev->info.sector_size);
+   aiocb.aio_nbytes = blocksize;
+   aiocb.aio_offset = blknum * blocksize;
+   aiocb.aio_cb = NULL;
+   aiocb.data = NULL;
+
Quoted 9 lines

extras/mini-os/blkfront.c line 626

Quoted 6 lines
extras/mini-os/blkfront.c +6 -0
+      /* read operation */
+      if(!write) {
+	 aiocb.aio_cb = NULL;
+	 blkfront_read(&aiocb);
+	 memcpy(buf,&aiocb.aio_buf[blkoff], bytes);
+      }

Could you perhaps optimize when buf is actually aligned? That would save a copy.

This can be done but only if in the current iteration of the loop an entire block is being read. Since aiocb only operates on sectors it'll read at minimum a whole sector into buf. If buf isnt big enough to hold the data a secondary buffer with a copy operation will have to be done.

Quoted 9 lines

extras/mini-os/blkfront.c line 632

Quoted 6 lines
extras/mini-os/blkfront.c +6 -0
+      /* Write operation */
+      else {
+	 /* If we're writing a partial block, we need to read the current contents first
+	  * so we don't overwrite the extra bits with garbage */
+	 if(blkoff != 0 || bytes<  blocksize) {
+	    aiocb.aio_cb = NULL;

Maybe blkfront_aio_cb should do it itself? It looks odd to have to do it when reusing an aiocb structure.

It could, but then that changes the design of aiocb. Was it supposed to be a very low level interface for just reading and writing blocks onto the disk?

Right now you have to set aio_nbytes and aio_offset to a multiple of sector size. This could be changed to allow variable sizes. Alternatively 2 new fields could be added to specify which portion inside a block to operate on.

Can you send a partial block through the xen block frontend and backend interface? If not we would have to queue up a read and then a write internally when the user requests a write. Its possible some users may not want this forced behavior of 2 operations.

Quoted 3 lines

Apart from that it looks good.

Samuel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw
Quoted 7 lines · Matthew Fioravante, le Mon 14 Mar 2011 15:07:30 -0400, a écrit :
Quoted 4 lines · Samuel Thibault

Review

Quoted 2 lines · Matthew Fioravante, le Fri 11 Mar 2011 17:34:26 -0500, a écrit :
(quoted diff) +2 -0
+      /*Make sure we have write permission */
+      if(dev->info.info&  VDISK_READONLY || dev->info.mode != O_RDWR) {

O_WRONLY too.

Good catch, actually testing a bitfield with != is a bad idea to begin with anyway.

Err, it's not a bitfield, in mini-os it's a {0,1,2} enum.

Quoted 5 lines
Quoted 3 lines

Could you perhaps optimize when buf is actually aligned? That would save a copy.

This can be done but only if in the current iteration of the loop an entire block is being read.

Sure.

Quoted 3 lines

Since aiocb only operates on sectors it'll
read at minimum a whole sector into buf. If buf isnt big enough to hold the data a secondary buffer with a copy operation will have to be done.

Sure.

But I expect people using that interface to tend to allocate big aligned buffers.

Quoted 10 lines
Quoted 10 lines

extras/mini-os/blkfront.c line 632

Quoted 7 lines
extras/mini-os/blkfront.c +3 -0
+      /* Write operation */
+      else {
+	 /* If we're writing a partial block, we need to read the current 

contents first

extras/mini-os/blkfront.c +3 -0
+	  * so we don't overwrite the extra bits with garbage */
+	 if(blkoff != 0 || bytes<  blocksize) {
+	    aiocb.aio_cb = NULL;

Maybe blkfront_aio_cb should do it itself? It looks odd to have to do it when reusing an aiocb structure.

Quoted 3 lines

It could, but then that changes the design of aiocb. Was it supposed to be a very low level interface for just reading and writing blocks onto the disk?

Well, I'd say the whole blkfront itself is a low-level interrface, and your patch actually provides a higher one :)

This particular change in the design shouldn't break anything, since aio_cb is actually filled by blkfront itself, so it makes sense that it cleans it since it expects it to be cleaned.

Quoted 7 lines

Right now you have to set aio_nbytes and aio_offset to a multiple of sector size. This could be changed to allow variable sizes. Alternatively 2 new fields could be added to specify which portion inside a block to operate on.

Can you send a partial block through the xen block frontend and backend interface?

No.

Quoted 3 lines

If not we would have to queue up a read and then a write internally when the user requests a write. Its possible some users may not want this forced behavior of 2 operations.

That's why I wouldn't recommend aio_nbytes/offset to be allowed to be non-multiples of the sector size. That interface is meant to be an efficient sector-transfer interface. Your posix layer can handle flexibility for the user.

Samuel

#4 lore raw

I think I totally misunderstood you. With the last case are you just referring to how I keep having to set the callback to NULL everytime? If so I totally agree with you. It should just set it back to NULL when its done.

I thought you were talking about how I was handling writes to partial blocks and that somehow the low level blkfront functions should be doing this transparently.

Quoted 57 lines · Samuel Thibault
Quoted 7 lines · Matthew Fioravante, le Mon 14 Mar 2011 15:07:30 -0400, a écrit :
Quoted 4 lines · Samuel Thibault

Review

Quoted 2 lines · Matthew Fioravante, le Fri 11 Mar 2011 17:34:26 -0500, a écrit :
(quoted diff) +2 -0
+      /*Make sure we have write permission */
+      if(dev->info.info&   VDISK_READONLY || dev->info.mode != O_RDWR) {

O_WRONLY too.

Good catch, actually testing a bitfield with != is a bad idea to begin with anyway.

Err, it's not a bitfield, in mini-os it's a {0,1,2} enum.

Quoted 5 lines
Quoted 3 lines

Could you perhaps optimize when buf is actually aligned? That would save a copy.

This can be done but only if in the current iteration of the loop an entire block is being read.

Sure.

Quoted 3 lines

Since aiocb only operates on sectors it'll
read at minimum a whole sector into buf. If buf isnt big enough to hold the data a secondary buffer with a copy operation will have to be done.

Sure.

But I expect people using that interface to tend to allocate big aligned buffers.

Quoted 10 lines
Quoted 10 lines

extras/mini-os/blkfront.c line 632

Quoted 7 lines
extras/mini-os/blkfront.c +3 -0
+      /* Write operation */
+      else {
+	 /* If we're writing a partial block, we need to read the current

contents first

extras/mini-os/blkfront.c +3 -0
+	  * so we don't overwrite the extra bits with garbage */
+	 if(blkoff != 0 || bytes<   blocksize) {
+	    aiocb.aio_cb = NULL;

Maybe blkfront_aio_cb should do it itself? It looks odd to have to do it when reusing an aiocb structure.

Quoted 3 lines

It could, but then that changes the design of aiocb. Was it supposed to be a very low level interface for just reading and writing blocks onto the disk?

Well, I'd say the whole blkfront itself is a low-level interrface, and your patch actually provides a higher one :)

This particular change in the design shouldn't break anything, since aio_cb is actually filled by blkfront itself, so it makes sense that it cleans it since it expects it to be cleaned.

Quoted 7 lines

Right now you have to set aio_nbytes and aio_offset to a multiple of sector size. This could be changed to allow variable sizes. Alternatively 2 new fields could be added to specify which portion inside a block to operate on.

Can you send a partial block through the xen block frontend and backend interface?

No.

Quoted 3 lines

If not we would have to queue up a read and then a write internally when the user requests a write. Its possible some users may not want this forced behavior of 2 operations.

Quoted 6 lines

That's why I wouldn't recommend aio_nbytes/offset to be allowed to be non-multiples of the sector size. That interface is meant to be an efficient sector-transfer interface. Your posix layer can handle flexibility for the user.

Samuel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#5 lore raw
Quoted 6 lines · Matthew Fioravante, le Mon 14 Mar 2011 17:32:31 -0400, a écrit :
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	    aiocb.aio_cb = NULL;

Maybe blkfront_aio_cb should do it itself? It looks odd to have to do it when reusing an aiocb structure.

I think I totally misunderstood you. With the last case are you just referring to how I keep having to set the callback to NULL everytime?

Yes.

Quoted 6 lines

If so I totally agree with you. It should just set it back to NULL when its done.

I thought you were talking about how I was handling writes to partial blocks and that somehow the low level blkfront functions should be doing this transparently.

Nope.

Samuel