From: Darrick J. Wong Date: Thu, 16 Nov 2017 01:22:37 +0000 (-0800) Subject: aio-dio-append-write-read-race: abort if we encounter syscall errors X-Git-Tag: v2022.05.01~1777 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=b5b3e2220ace9f91ef8b9fc3b7593b1a3e46117d;p=users%2Fhch%2Fxfstests-dev.git aio-dio-append-write-read-race: abort if we encounter syscall errors If any of the library calls return error codes, just print out a message and abort the test. Whoever wrote the write test did not check for write failures, which means that if we ENOSPC without writing anything then the reader thread will loop forever trying to read. Signed-off-by: Darrick J. Wong Reviewed-by: Eryu Guan Signed-off-by: Eryu Guan --- diff --git a/src/aio-dio-regress/aio-dio-append-write-read-race.c b/src/aio-dio-regress/aio-dio-append-write-read-race.c index 1a78009eb..8f94d503c 100644 --- a/src/aio-dio-regress/aio-dio-append-write-read-race.c +++ b/src/aio-dio-regress/aio-dio-append-write-read-race.c @@ -70,6 +70,11 @@ static void *reader(void *arg) return NULL; } +#define fail(fmt , args...) do { \ + fprintf(stderr, fmt , ##args); \ + exit(1); \ +} while (0) + static void *writer(struct io_data *data) { int ret; @@ -84,28 +89,23 @@ static void *writer(struct io_data *data) struct iocb *iocbs[] = { &iocb }; ret = io_setup(1, &ctx); - if (ret) { - fprintf(stderr, "error %s during io_setup\n", - strerror(ret)); - return NULL; - } + if (ret) + fail("error %s during io_setup\n", strerror(ret)); io_prep_pwrite(&iocb, data->fd, data->buf, data->blksize, data->offset); ret = io_submit(ctx, 1, iocbs); - if (ret != 1) { - fprintf(stderr, "error %s during io_submit\n", - strerror(ret)); - return NULL; - } + if (ret != 1) + fail("error %s during io_submit\n", strerror(ret)); ret = io_getevents(ctx, 1, 1, evs, NULL); - if (ret != 1) { - fprintf(stderr, "error %s during io_getevents\n", - strerror(ret)); - return NULL; - } + if (ret != 1) + fail("error %s during io_getevents\n", strerror(ret)); + if ((signed)evs[0].res < 0) + fail("error %s during write\n", strerror(-evs[0].res)); + if ((signed)evs[0].res2 < 0) + fail("secondary error %s during write\n", strerror(-evs[0].res2)); } else { ret = pwrite(data->fd, data->buf, data->blksize, data->offset); if (ret < 0) - perror("write file failed"); + fail("write file failed: %s", strerror(errno)); } return NULL; @@ -161,14 +161,14 @@ int main(int argc, char *argv[]) ret = posix_memalign((void **)&wbuf, io_align, blksize); if (ret) { - fprintf(stderr, "failed to alloc memory: %s\n", strerror(ret)); + fail("failed to alloc memory: %s\n", strerror(ret)); ret = 1; goto err; } ret = posix_memalign((void **)&rbuf, io_align, blksize); if (ret) { - fprintf(stderr, "failed to alloc memory: %s\n", strerror(ret)); + fail("failed to alloc memory: %s\n", strerror(ret)); ret = 1; goto err; } @@ -189,8 +189,7 @@ int main(int argc, char *argv[]) reader_ready = 0; ret = pthread_create(&tid, NULL, reader, &rdata); if (ret) { - fprintf(stderr, "create reader thread failed: %s\n", - strerror(ret)); + fail("create reader thread failed: %s\n", strerror(ret)); ret = 1; goto err; } @@ -199,15 +198,14 @@ int main(int argc, char *argv[]) ret = pthread_join(tid, NULL); if (ret) { - fprintf(stderr, "pthread join reader failed: %s\n", - strerror(ret)); + fail("pthread join reader failed: %s\n", strerror(ret)); ret = 1; goto err; } for (j = 0; j < blksize; j++) { if (rdata.buf[j] != 'a') { - fprintf(stderr, "encounter an error: " + fail("encounter an error: " "block %d offset %d, content %x\n", i, j, rbuf[j]); ret = 1;