diff mbox

[RFC,v3,3/6] block/raw-posix: implement bdrv_preallocate

Message ID c81636cc16a515f5947bdb3ca1ced960ec910f07.1387419339.git.hutao@cn.fujitsu.com
State New
Headers show

Commit Message

Hu Tao Dec. 19, 2013, 2:27 a.m. UTC
Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
---
 block/raw-posix.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

Comments

Stefan Hajnoczi Dec. 20, 2013, 10:14 a.m. UTC | #1
On Thu, Dec 19, 2013 at 10:27:38AM +0800, Hu Tao wrote:
> +static int raw_preallocate2(int fd, int64_t offset, int64_t length)
> +{
> +    int ret = -1;
> +
> +    ret = fallocate(fd, 0, offset, length);
> +
> +    /* fallback to posix_fallocate() if fallocate() is not supported */
> +    if (ret < 0 && (errno == ENOSYS || errno == EOPNOTSUPP)) {
> +        ret = posix_fallocate(fd, offset, length);
> +    }
> +
> +    return ret;

Return value semantics differ between the two functions:
 * fallocate - return 0 or -1 with errno set
 * posix_fallocate - return 0 or error number (without using errno!)

Please make it consistent.  Usually in QEMU we return 0 on success and
-errno on failure.
Hu Tao Dec. 23, 2013, 1:45 a.m. UTC | #2
On Fri, Dec 20, 2013 at 11:14:05AM +0100, Stefan Hajnoczi wrote:
> On Thu, Dec 19, 2013 at 10:27:38AM +0800, Hu Tao wrote:
> > +static int raw_preallocate2(int fd, int64_t offset, int64_t length)
> > +{
> > +    int ret = -1;
> > +
> > +    ret = fallocate(fd, 0, offset, length);
> > +
> > +    /* fallback to posix_fallocate() if fallocate() is not supported */
> > +    if (ret < 0 && (errno == ENOSYS || errno == EOPNOTSUPP)) {
> > +        ret = posix_fallocate(fd, offset, length);
> > +    }
> > +
> > +    return ret;
> 
> Return value semantics differ between the two functions:
>  * fallocate - return 0 or -1 with errno set
>  * posix_fallocate - return 0 or error number (without using errno!)
> 
> Please make it consistent.  Usually in QEMU we return 0 on success and
> -errno on failure.

Thanks for catching this!
diff mbox

Patch

diff --git a/block/raw-posix.c b/block/raw-posix.c
index 10c6b34..19181f2 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -1160,6 +1160,39 @@  static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
     return (int64_t)st.st_blocks * 512;
 }
 
+#ifdef __linux__
+static int raw_preallocate2(int fd, int64_t offset, int64_t length)
+{
+    int ret = -1;
+
+    ret = fallocate(fd, 0, offset, length);
+
+    /* fallback to posix_fallocate() if fallocate() is not supported */
+    if (ret < 0 && (errno == ENOSYS || errno == EOPNOTSUPP)) {
+        ret = posix_fallocate(fd, offset, length);
+    }
+
+    return ret;
+}
+#else
+static int raw_preallocate2(int fd, int64_t offset, int64_t length)
+{
+    return posix_fallocate(fd, offset, length);
+}
+#endif
+
+static int raw_preallocate(BlockDriverState *bs, int64_t offset, int64_t length)
+{
+    BDRVRawState *s = bs->opaque;
+    int64_t len = bdrv_getlength(bs);
+
+    if (offset + length < 0 || offset + length > len) {
+        return -EINVAL;
+    }
+
+    return raw_preallocate2(s->fd, offset, length);
+}
+
 static int raw_create(const char *filename, QEMUOptionParameter *options,
                       Error **errp)
 {
@@ -1356,6 +1389,7 @@  static BlockDriver bdrv_file = {
     .bdrv_close = raw_close,
     .bdrv_create = raw_create,
     .bdrv_has_zero_init = bdrv_has_zero_init_1,
+    .bdrv_preallocate = raw_preallocate,
     .bdrv_co_get_block_status = raw_co_get_block_status,
     .bdrv_co_write_zeroes = raw_co_write_zeroes,