2 * aio-dio-subblock-eof-read - test AIO read of last block of DIO file
3 * Copyright (C) 2005 Jeff Moyer
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Code taken from an example posted to linux-aio at kvack.org
22 * http://marc.info/?l=linux-aio&m=112263621431161&w=2
23 * Original Author: Drangon Zhou
24 * Munged & rewritten by Jeff Moyer.
26 * Description: This source code implements a test to ensure that an AIO
27 * read of the last block in a file opened with O_DIRECT returns the proper
28 * amount of data. In the past, there was a bug that resulted in a return
29 * value of the requested block size, when in fact there was only a fraction
30 * of that data available. Thus, if the last data block contained 300 bytes
31 * worth of data, and the user issued a 4k read, we want to ensure that
32 * the return value is 300, not 4k.
41 /* Create a file of a size that is not a multiple of block size */
44 #define fail(fmt , args...) \
46 printf(fmt , ##args); \
50 static unsigned char buffer[4096] __attribute((aligned (4096)));
53 main(int argc, char **argv)
59 struct iocb *cb = &myiocb;
64 fail("only arg should be file name");
67 fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0600);
69 fail("open returned error %d\n", errno);
71 ret = ftruncate(fd, FILE_SIZE);
73 fail("truncate returned error %d\n", errno);
75 /* <1> use normal disk read, this should be ok */
76 ret = read(fd, buffer, 4096);
78 fail("buffered read returned %d, should be 300\n", ret);
80 /* <2> use AIO disk read, it sees error. */
81 memset(&myiocb, 0, sizeof(myiocb));
84 cb->aio_lio_opcode = IO_CMD_PREAD;
88 cb->u.c.nbytes = 4096;
91 ret = io_queue_init(1, &ioctx);
93 fail("io_queue_init returned error %d\n", ret);
95 ret = io_submit(ioctx, 1, &cb);
97 fail("io_submit returned error %d\n", ret);
99 ret = io_getevents(ioctx, 1, 1, &ie, NULL);
101 fail("io_getevents returned %d\n", ret);
104 * If all goes well, we should see 300 bytes read. If things
105 * are broken, we may very well see a result of 4k.
107 if (ie.res != FILE_SIZE)
108 fail("AIO read of last block in file returned %ld bytes, "
109 "expected %d\n", ie.res, FILE_SIZE);
111 printf("AIO read of last block in file succeeded.\n");