qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Nir Soffer <nsoffer@redhat.com>
To: Max Reitz <mreitz@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>, qemu-block <qemu-block@nongnu.org>,
	Nir Soffer <nirsof@gmail.com>,
	QEMU Developers <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] [PATCH] block: posix: Always allocate the first block
Date: Fri, 23 Aug 2019 19:30:33 +0300	[thread overview]
Message-ID: <CAMRbyyt5gAOzY7sTQc7_XE20-O4XunVjne2_2_1jmZtButc1eg@mail.gmail.com> (raw)
In-Reply-To: <e8db1edb-b1ee-8244-c772-8e08794181f0@redhat.com>

On Fri, Aug 23, 2019 at 4:58 PM Max Reitz <mreitz@redhat.com> wrote:

> On 22.08.19 21:01, Nir Soffer wrote:
>
...

> >     >     > @@ -1794,6 +1815,8 @@ static int handle_aiocb_truncate(void
> >     *opaque)
> >     >     >                  /* posix_fallocate() doesn't set errno. */
> >     >     >                  error_setg_errno(errp, -result,
> >     >     >                                   "Could not preallocate new
> >     data");
> >     >     > +            } else if (current_length == 0) {
> >     >     > +                allocate_first_block(fd);
> >     >
> >     >     Should posix_fallocate() not take care of precisely this?
> >     >
> >     >
> >     > Only if the filesystem does not support fallocate() (e.g. NFS <
> 4.2).
> >     >
> >     > In this case posix_fallocate() is doing:
> >     >
> >     >   for (offset += (len - 1) % increment; len > 0; offset +=
> increment)
> >     >     {
> >     >       len -= increment;
> >     >       if (offset < st.st_size)
> >     >         {
> >     >           unsigned char c;
> >     >           ssize_t rsize = __pread (fd, &c, 1, offset);
> >     >           if (rsize < 0)
> >     >             return errno;
> >     >           /* If there is a non-zero byte, the block must have been
> >     >              allocated already.  */
> >     >           else if (rsize == 1 && c != 0)
> >     >             continue;
> >     >         }
> >     >       if (__pwrite (fd, "", 1, offset) != 1)
> >     >         return errno;
> >     >     }
> >     >
> >     >
> >
> https://code.woboq.org/userspace/glibc/sysdeps/posix/posix_fallocate.c.html#96
> >     >
> >     > So opening a file with O_DIRECT will break preallocation=falloc on
> >     such
> >     > filesystems,
> >
> >     But won’t the function above just fail with EINVAL?
> >     allocate_first_block() is executed only in case of success.
> >
> >
> > Sure, but if posix_fallocate() fails, we fail qemu-img create/convert.
>
> Exactly.  But if posix_fallocate() works, it should have allocated the
> first block.
>

Only if the file system does not support fallocate(). posix_fallocate()
first try
fallocate(), and fall back to manual preallocation:
https://code.woboq.org/userspace/glibc/sysdeps/unix/sysv/linux/posix_fallocate.c.html#27

Here is an example using fallocate --posix:

$ sudo mount -t glusterfs gluster1:/gv0 /tmp/gv0

(gv0 is gluster volume backed by XFS on top of VDO device with 4k sector
size)

$ fallocate -l 1g --posix empty.raw
$ dd if=empty.raw bs=1 count=1 of=/dev/null iflag=direct status=none

$ dd if=/dev/zero bs=1 count=1 of=empty.raw conv=notrunc status=none
$ dd if=empty.raw bs=1 count=1 of=/dev/null iflag=direct status=none
dd: error reading 'empty.raw': Invalid argument

$ dd if=empty.raw bs=512 count=1 of=/dev/null iflag=direct status=none
dd: error reading 'empty.raw': Invalid argument

$ dd if=empty.raw bs=4096 count=1 of=/dev/null iflag=direct status=none

Here is example using gluster storage with sector size of 512 bytes.

$ sudo mount -t glusterfs gluster1:/gv1 /tmp/gv1

$ fallocate -l 1g --posix empty.raw
$ dd if=empty.raw bs=1 count=1 of=/dev/null iflag=direct status=none

$ dd if=/dev/zero bs=1 count=1 of=empty.raw conv=notrunc status=none

$ dd if=empty.raw bs=1 count=1 of=/dev/null iflag=direct status=none
dd: error reading 'empty.raw': Invalid argument

$ dd if=empty.raw bs=512 count=1 of=/dev/null iflag=direct status=none

So we must allocated using write() after calling posix_fallocate().

Nir

  reply	other threads:[~2019-08-23 16:34 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-16 21:21 [Qemu-devel] [PATCH] block: posix: Always allocate the first block Nir Soffer
2019-08-16 21:57 ` [Qemu-devel] [Qemu-block] " John Snow
2019-08-16 22:45   ` Nir Soffer
2019-08-16 23:00     ` John Snow
2019-08-22 11:30 ` [Qemu-devel] " Nir Soffer
2019-08-22 14:28 ` Max Reitz
2019-08-22 16:39   ` Nir Soffer
2019-08-22 18:11     ` Max Reitz
2019-08-22 19:01       ` Nir Soffer
2019-08-23 13:58         ` Max Reitz
2019-08-23 16:30           ` Nir Soffer [this message]
2019-08-23 17:41             ` Max Reitz
2019-08-23 16:48           ` Nir Soffer
2019-08-23 17:53             ` Max Reitz
2019-08-24 22:57               ` Nir Soffer
2019-08-25  7:44 ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-08-25 19:51   ` Nir Soffer
2019-08-25 22:17     ` Maxim Levitsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMRbyyt5gAOzY7sTQc7_XE20-O4XunVjne2_2_1jmZtButc1eg@mail.gmail.com \
    --to=nsoffer@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=nirsof@gmail.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).