public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Brian Foster <bfoster@redhat.com>
To: Avi Kivity <avi@scylladb.com>
Cc: linux-aio@kvack.org, xfs@oss.sgi.com
Subject: Re: AIO read returns negative number for bytes read
Date: Mon, 16 Nov 2015 10:00:29 -0500	[thread overview]
Message-ID: <20151116150028.GA60592@bfoster.bfoster> (raw)
In-Reply-To: <564883BD.8070607@scylladb.com>

On Sun, Nov 15, 2015 at 03:08:13PM +0200, Avi Kivity wrote:
> Due to a bug in my program, I initiated a read beyond eof. Specifically, the
> file size is 13002 bytes and the read offset is 13312 (0x3400).
> 
> I would expect such a read to return 0 bytes read, but io_getevents returns
> -310, which is suspiciously equal to (13002 - 13312).
> 
> I attach a reproducer.
> 
> 4.2.5-201.fc22.x86_64
> 
> Are my expectations incorrect, or is this a bug in aio or xfs?

FWIW, I added some printk()'s and reproduced. This looks like a dio
issue to me (a sync dio read reproduces just the same as aio). See the
short read check in dio_complete():


	...
        /* Check for short read case */
        if ((dio->rw == READ) && ((offset + transferred) > dio->i_size))
                transferred = dio->i_size - offset;
	...

So if offset starts beyond i_size, we set transferred to a negative
value. I suppose we could check for offset >= i_size here independently,
but I'm not necessarily sure something earlier doesn't need to change
(e.g., I see dio->result = 3072 in this case and I'm not familiar enough
with core dio to know if that's expected).

Brian

> #define _GNU_SOURCE
> 
> #include <libaio.h>
> #include <assert.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <unistd.h>
> #include <stdio.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> 
> int main(int ac, char** av) {
>   int fd;
>   char* buf;
>   io_context_t ioc = NULL;
>   int r;
>   struct iocb iocb;
>   struct iocb *iocbp[1];
>   struct io_event ioev;
> 
>   buf = aligned_alloc(4096, 4096*4);
>   assert(buf);
>   r = io_setup(1, &ioc);
>   assert(r == 0);
>   fd = open("tmp.tmp", O_RDWR | O_CREAT | O_DIRECT, 0600);
>   assert(fd >= 0);
>   io_prep_pwrite(&iocb, fd, buf, 4096*4, 0);
>   iocbp[0] = &iocb;
>   r = io_submit(ioc, 1, iocbp);
>   assert(r == 1);
>   r = io_getevents(ioc, 1, 1, &ioev, NULL);
>   assert(r == 1);
>   assert(ioev.res == 4*4096);
>   ftruncate(fd, 13002);
>   io_prep_pread(&iocb, fd, buf, 8192, 13312);
>   r = io_submit(ioc, 1, iocbp);
>   assert(r == 1);
>   r = io_getevents(ioc, 1, 1, &ioev, NULL);
>   assert(r == 1);
>   printf("read result: %d\n", (int)ioev.res);
>   return 0;
> }

> _______________________________________________
> xfs mailing list
> xfs@oss.sgi.com
> http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

  reply	other threads:[~2015-11-16 15:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-15 13:08 AIO read returns negative number for bytes read Avi Kivity
2015-11-16 15:00 ` Brian Foster [this message]
2015-11-16 15:19 ` Alireza Haghdoost
2015-11-16 15:32   ` Avi Kivity
2015-11-16 19:27 ` Jeff Moyer
2015-11-17 12:52   ` Avi Kivity
2015-11-19 15:30     ` Jan Kara
2015-11-19 15:32       ` Avi Kivity
2015-11-19 15:33       ` Jeff Moyer

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=20151116150028.GA60592@bfoster.bfoster \
    --to=bfoster@redhat.com \
    --cc=avi@scylladb.com \
    --cc=linux-aio@kvack.org \
    --cc=xfs@oss.sgi.com \
    /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