1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2005 Jeff Moyer
7 * Test AIO read of last block of DIO file
9 * Code taken from an example posted to linux-aio at kvack.org
10 * http://marc.info/?l=linux-aio&m=112263621431161&w=2
11 * Original Author: Drangon Zhou
12 * Munged & rewritten by Jeff Moyer.
14 * Description: This source code implements a test to ensure that an AIO
15 * read of the last block in a file opened with O_DIRECT returns the proper
16 * amount of data. In the past, there was a bug that resulted in a return
17 * value of the requested block size, when in fact there was only a fraction
18 * of that data available. Thus, if the last data block contained 300 bytes
19 * worth of data, and the user issued a 4k read, we want to ensure that
20 * the return value is 300, not 4k.
29 /* Create a file of a size that is not a multiple of block size */
32 #define fail(fmt , args...) \
34 printf(fmt , ##args); \
38 static unsigned char buffer[4096] __attribute((aligned (4096)));
41 main(int argc, char **argv)
47 struct iocb *cb = &myiocb;
52 fail("only arg should be file name");
55 fd = open(filename, O_CREAT|O_RDWR|O_DIRECT, 0600);
57 fail("open returned error %d\n", errno);
59 ret = ftruncate(fd, FILE_SIZE);
61 fail("truncate returned error %d\n", errno);
63 /* <1> use normal disk read, this should be ok */
64 ret = read(fd, buffer, 4096);
66 fail("buffered read returned %d, should be 300\n", ret);
68 /* <2> use AIO disk read, it sees error. */
69 memset(&myiocb, 0, sizeof(myiocb));
72 cb->aio_lio_opcode = IO_CMD_PREAD;
76 cb->u.c.nbytes = 4096;
79 ret = io_queue_init(1, &ioctx);
81 fail("io_queue_init returned error %d\n", ret);
83 ret = io_submit(ioctx, 1, &cb);
85 fail("io_submit returned error %d\n", ret);
87 ret = io_getevents(ioctx, 1, 1, &ie, NULL);
89 fail("io_getevents returned %d\n", ret);
92 * If all goes well, we should see 300 bytes read. If things
93 * are broken, we may very well see a result of 4k.
95 if (ie.res != FILE_SIZE)
96 fail("AIO read of last block in file returned %ld bytes, "
97 "expected %d\n", ie.res, FILE_SIZE);
99 printf("AIO read of last block in file succeeded.\n");