[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 01 of 21] blktap3/drivers: driver for RAW images



This patch copies from blktap2 (with few changes coming from blktap2.5) the
driver for RAW images.

Signed-off-by: Thanos Makatos <thanos.makatos@xxxxxxxxxx>

diff --git a/tools/blktap2/drivers/block-aio.c 
b/tools/blktap3/drivers/block-aio.c
copy from tools/blktap2/drivers/block-aio.c
copy to tools/blktap3/drivers/block-aio.c
--- a/tools/blktap2/drivers/block-aio.c
+++ b/tools/blktap3/drivers/block-aio.c
@@ -4,14 +4,14 @@
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in the
- *       documentation and/or other materials provided with the distribution.
- *     * Neither the name of XenSource Inc. nor the names of its contributors
- *       may be used to endorse or promote products derived from this software
- *       without specific prior written permission.
+ *        * Redistributions of source code must retain the above copyright
+ *              notice, this list of conditions and the following disclaimer.
+ *        * Redistributions in binary form must reproduce the above copyright
+ *              notice, this list of conditions and the following disclaimer 
in the
+ *              documentation and/or other materials provided with the 
distribution.
+ *        * Neither the name of XenSource Inc. nor the names of its 
contributors
+ *              may be used to endorse or promote products derived from this 
software
+ *              without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@@ -26,7 +26,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -35,8 +34,8 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#include <linux/fs.h>
 
-#include "blk.h"
 #include "tapdisk.h"
 #include "tapdisk-driver.h"
 #include "tapdisk-interface.h"
@@ -64,9 +63,7 @@ struct tdaio_state {
 static int tdaio_get_image_info(int fd, td_disk_info_t *info)
 {
        int ret;
-       long size;
-       unsigned long total_size;
-       struct statvfs statBuf;
+       unsigned long long bytes;
        struct stat stat;
 
        ret = fstat(fd, &stat);
@@ -78,17 +75,32 @@ static int tdaio_get_image_info(int fd, 
        if (S_ISBLK(stat.st_mode)) {
                /*Accessing block device directly*/
                info->size = 0;
-               if (blk_getimagesize(fd, &info->size) != 0)
+               if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
+                       info->size = bytes >> SECTOR_SHIFT;
+               } else if (ioctl(fd, BLKGETSIZE, &info->size) != 0) {
+                       DPRINTF
+                               ("ERR: BLKGETSIZE and BLKGETSIZE64 failed, 
couldn't stat image");
                        return -EINVAL;
+               }
 
                DPRINTF("Image size: \n\tpre sector_shift  [%llu]\n\tpost "
                        "sector_shift [%llu]\n",
                        (long long unsigned)(info->size << SECTOR_SHIFT),
                        (long long unsigned)info->size);
 
-               /*Get the sector size*/
-               if (blk_getsectorsize(fd, &info->sector_size) != 0)
+               /*Get the sector size */
+#if defined(BLKSSZGET)
+               {
                        info->sector_size = DEFAULT_SECTOR_SIZE;
+                       ioctl(fd, BLKSSZGET, &info->sector_size);
+
+                       if (info->sector_size != DEFAULT_SECTOR_SIZE)
+                               DPRINTF("Note: sector size is %ld (not %d)\n",
+                                               info->sector_size, 
DEFAULT_SECTOR_SIZE);
+               }
+#else
+               info->sector_size = DEFAULT_SECTOR_SIZE;
+#endif
 
        } else {
                /*Local file? try fstat instead*/
@@ -129,23 +141,25 @@ int tdaio_open(td_driver_t *driver, cons
        /* Open the file */
        o_flags = O_DIRECT | O_LARGEFILE | 
                ((flags & TD_OPEN_RDONLY) ? O_RDONLY : O_RDWR);
-        fd = open(name, o_flags);
+       fd = open(name, o_flags);
 
-        if ( (fd == -1) && (errno == EINVAL) ) {
+        if ( (fd == -1) && (errno == EINVAL)) {
 
-                /* Maybe O_DIRECT isn't supported. */
+               /* Maybe O_DIRECT isn't supported. */
                o_flags &= ~O_DIRECT;
-                fd = open(name, o_flags);
-                if (fd != -1) DPRINTF("WARNING: Accessing image without"
-                                     "O_DIRECT! (%s)\n", name);
+               fd = open(name, o_flags);
+               if (fd != -1)
+                       DPRINTF("WARNING: Accessing image without"
+                                       "O_DIRECT! (%s)\n", name);
 
-        } else if (fd != -1) DPRINTF("open(%s) with O_DIRECT\n", name);
-       
-        if (fd == -1) {
+       } else if (fd != -1)
+               DPRINTF("open(%s) with O_DIRECT\n", name);
+
+       if (fd == -1) {
                DPRINTF("Unable to open [%s] (%d)!\n", name, 0 - errno);
-               ret = 0 - errno;
-               goto done;
-        }
+               ret = 0 - errno;
+               goto done;
+       }
 
        ret = tdaio_get_image_info(fd, &driver->info);
        if (ret) {
@@ -153,106 +167,126 @@ int tdaio_open(td_driver_t *driver, cons
                goto done;
        }
 
-        prv->fd = fd;
+       prv->fd = fd;
 
-done:
-       return ret;     
+  done:
+       return ret;
 }
 
-void tdaio_complete(void *arg, struct tiocb *tiocb, int err)
+void
+tdaio_complete(void *arg, struct tiocb *tiocb __attribute__((unused)),
+               int err)
 {
-       struct aio_request *aio = (struct aio_request *)arg;
+       struct aio_request *aio = (struct aio_request *) arg;
        struct tdaio_state *prv = aio->state;
 
        td_complete_request(aio->treq, err);
        prv->aio_free_list[prv->aio_free_count++] = aio;
 }
 
-void tdaio_queue_read(td_driver_t *driver, td_request_t treq)
+void tdaio_queue_read(td_driver_t * driver, td_request_t treq)
 {
        int size;
        uint64_t offset;
        struct aio_request *aio;
        struct tdaio_state *prv;
 
-       prv    = (struct tdaio_state *)driver->data;
-       size   = treq.secs * driver->info.sector_size;
-       offset = treq.sec  * (uint64_t)driver->info.sector_size;
+       prv = (struct tdaio_state *) driver->data;
+       size = treq.secs * driver->info.sector_size;
+       offset = treq.sec * (uint64_t) driver->info.sector_size;
 
        if (prv->aio_free_count == 0)
                goto fail;
 
-       aio        = prv->aio_free_list[--prv->aio_free_count];
-       aio->treq  = treq;
+       aio = prv->aio_free_list[--prv->aio_free_count];
+       aio->treq = treq;
        aio->state = prv;
 
        td_prep_read(&aio->tiocb, prv->fd, treq.buf,
-                    size, offset, tdaio_complete, aio);
+                                size, offset, tdaio_complete, aio);
        td_queue_tiocb(driver, &aio->tiocb);
 
        return;
 
-fail:
+  fail:
        td_complete_request(treq, -EBUSY);
 }
 
-void tdaio_queue_write(td_driver_t *driver, td_request_t treq)
+void tdaio_queue_write(td_driver_t * driver, td_request_t treq)
 {
        int size;
        uint64_t offset;
        struct aio_request *aio;
        struct tdaio_state *prv;
 
-       prv     = (struct tdaio_state *)driver->data;
-       size    = treq.secs * driver->info.sector_size;
-       offset  = treq.sec  * (uint64_t)driver->info.sector_size;
+       prv = (struct tdaio_state *) driver->data;
+       size = treq.secs * driver->info.sector_size;
+       offset = treq.sec * (uint64_t) driver->info.sector_size;
 
        if (prv->aio_free_count == 0)
                goto fail;
 
-       aio        = prv->aio_free_list[--prv->aio_free_count];
-       aio->treq  = treq;
+       aio = prv->aio_free_list[--prv->aio_free_count];
+       aio->treq = treq;
        aio->state = prv;
 
        td_prep_write(&aio->tiocb, prv->fd, treq.buf,
-                     size, offset, tdaio_complete, aio);
+                                 size, offset, tdaio_complete, aio);
        td_queue_tiocb(driver, &aio->tiocb);
 
        return;
 
-fail:
+  fail:
        td_complete_request(treq, -EBUSY);
 }
 
-int tdaio_close(td_driver_t *driver)
+int tdaio_close(td_driver_t * driver)
 {
-       struct tdaio_state *prv = (struct tdaio_state *)driver->data;
-       
+       struct tdaio_state *prv = (struct tdaio_state *) driver->data;
+
        close(prv->fd);
 
        return 0;
 }
 
-int tdaio_get_parent_id(td_driver_t *driver, td_disk_id_t *id)
+int
+tdaio_get_parent_id(td_driver_t * driver __attribute__((unused)),
+               td_disk_id_t * id __attribute__((unused)))
 {
        return TD_NO_PARENT;
 }
 
-int tdaio_validate_parent(td_driver_t *driver,
-                         td_driver_t *pdriver, td_flag_t flags)
+int
+tdaio_validate_parent(td_driver_t * driver __attribute__((unused)),
+               td_driver_t * pdriver __attribute__((unused)),
+               td_flag_t flags  __attribute__((unused)))
 {
        return -EINVAL;
 }
 
+void tdaio_stats(td_driver_t * driver, td_stats_t * st)
+{
+       struct tdaio_state *prv = (struct tdaio_state *) driver->data;
+       int n_pending;
+
+       n_pending = MAX_AIO_REQS - prv->aio_free_count;
+
+       tapdisk_stats_field(st, "reqs", "{");
+       tapdisk_stats_field(st, "max", "lu", MAX_AIO_REQS);
+       tapdisk_stats_field(st, "pending", "d", n_pending);
+       tapdisk_stats_leave(st, '}');
+}
+
 struct tap_disk tapdisk_aio = {
-       .disk_type          = "tapdisk_aio",
-       .flags              = 0,
-       .private_data_size  = sizeof(struct tdaio_state),
-       .td_open            = tdaio_open,
-       .td_close           = tdaio_close,
-       .td_queue_read      = tdaio_queue_read,
-       .td_queue_write     = tdaio_queue_write,
-       .td_get_parent_id   = tdaio_get_parent_id,
+       .disk_type = "tapdisk_aio",
+       .flags = 0,
+       .private_data_size = sizeof(struct tdaio_state),
+       .td_open = tdaio_open,
+       .td_close = tdaio_close,
+       .td_queue_read = tdaio_queue_read,
+       .td_queue_write = tdaio_queue_write,
+       .td_get_parent_id = tdaio_get_parent_id,
        .td_validate_parent = tdaio_validate_parent,
-       .td_debug           = NULL,
+       .td_debug = NULL,
+       .td_stats = tdaio_stats,
 };

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.