Home | History | Annotate | Line # | Download | only in sys
      1  1.1  christos /* $NetBSD: t_aio_rw.c,v 1.1 2025/10/10 15:53:55 christos Exp $ */
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2025 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17  1.1  christos  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  1.1  christos  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  christos  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21  1.1  christos  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  1.1  christos  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  1.1  christos  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.1  christos  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  1.1  christos  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  christos  */
     29  1.1  christos 
     30  1.1  christos #include <atf-c.h>
     31  1.1  christos 
     32  1.1  christos #include <aio.h>
     33  1.1  christos #include <errno.h>
     34  1.1  christos #include <fcntl.h>
     35  1.1  christos #include <signal.h>
     36  1.1  christos #include <stdint.h>
     37  1.1  christos #include <stdio.h>
     38  1.1  christos #include <stdlib.h>
     39  1.1  christos #include <string.h>
     40  1.1  christos #include <sys/stat.h>
     41  1.1  christos #include <sys/types.h>
     42  1.1  christos #include <time.h>
     43  1.1  christos #include <unistd.h>
     44  1.1  christos 
     45  1.1  christos static int	mktemp_file(char *, size_t);
     46  1.1  christos static void	fill_pattern(uint8_t *, size_t, uint8_t);
     47  1.1  christos static void	wait_all(const struct aiocb * const [], size_t);
     48  1.1  christos 
     49  1.1  christos static int
     50  1.1  christos mktemp_file(char *path, size_t pathlen)
     51  1.1  christos {
     52  1.1  christos 	int fd, n;
     53  1.1  christos 
     54  1.1  christos 	n = snprintf(path, pathlen, "t_aio_rw.XXXXXX");
     55  1.1  christos 	ATF_REQUIRE(n > 0 && (size_t)n < pathlen);
     56  1.1  christos 
     57  1.1  christos 	fd = mkstemp(path);
     58  1.1  christos 	ATF_REQUIRE(fd >= 0);
     59  1.1  christos 
     60  1.1  christos 	return fd;
     61  1.1  christos }
     62  1.1  christos 
     63  1.1  christos static void
     64  1.1  christos fill_pattern(uint8_t *buf, size_t len, uint8_t seed)
     65  1.1  christos {
     66  1.1  christos 	size_t i;
     67  1.1  christos 
     68  1.1  christos 	for (i = 0; i < len; i++) {
     69  1.1  christos 		buf[i] = (uint8_t)(seed + (i & 0xff));
     70  1.1  christos 	}
     71  1.1  christos }
     72  1.1  christos 
     73  1.1  christos static void
     74  1.1  christos wait_all(const struct aiocb * const list[], size_t nent)
     75  1.1  christos {
     76  1.1  christos 	size_t i;
     77  1.1  christos 	int pending, rv, error;
     78  1.1  christos 
     79  1.1  christos 	for (;;) {
     80  1.1  christos 		pending = 0;
     81  1.1  christos 
     82  1.1  christos 		for (i = 0; i < nent; i++) {
     83  1.1  christos 			if (list[i] == NULL) {
     84  1.1  christos 				continue;
     85  1.1  christos 			}
     86  1.1  christos 
     87  1.1  christos 			error = aio_error(list[i]);
     88  1.1  christos 			if (error == EINPROGRESS) {
     89  1.1  christos 				pending = 1;
     90  1.1  christos 			}
     91  1.1  christos 		}
     92  1.1  christos 
     93  1.1  christos 		if (!pending) {
     94  1.1  christos 			break;
     95  1.1  christos 		}
     96  1.1  christos 
     97  1.1  christos 		rv = aio_suspend(list, (int)nent, NULL);
     98  1.1  christos 		ATF_REQUIRE_EQ_MSG(0, rv, "aio_suspend failed: %s",
     99  1.1  christos 			strerror(errno));
    100  1.1  christos 	}
    101  1.1  christos }
    102  1.1  christos 
    103  1.1  christos /*
    104  1.1  christos  * write_then_read_back
    105  1.1  christos  * Write a block then read it back asynchronously and compare.
    106  1.1  christos  */
    107  1.1  christos ATF_TC_WITHOUT_HEAD(write_then_read_back);
    108  1.1  christos ATF_TC_BODY(write_then_read_back, tc)
    109  1.1  christos {
    110  1.1  christos 	char path[64];
    111  1.1  christos 	int fd, rv;
    112  1.1  christos 	const size_t blksz = 0x2000;
    113  1.1  christos 	uint8_t *wbuf, *rbuf;
    114  1.1  christos 	struct aiocb wcb, rcb;
    115  1.1  christos 	const struct aiocb *wlist[1], *rlist[1];
    116  1.1  christos 
    117  1.1  christos 	fd = mktemp_file(path, sizeof(path));
    118  1.1  christos 
    119  1.1  christos 	wbuf = malloc(blksz);
    120  1.1  christos 	rbuf = calloc(1, blksz);
    121  1.1  christos 	ATF_REQUIRE(wbuf != NULL && rbuf != NULL);
    122  1.1  christos 
    123  1.1  christos 	fill_pattern(wbuf, blksz, 0xA0);
    124  1.1  christos 
    125  1.1  christos 	memset(&wcb, 0, sizeof(wcb));
    126  1.1  christos 	wcb.aio_fildes = fd;
    127  1.1  christos 	wcb.aio_buf = wbuf;
    128  1.1  christos 	wcb.aio_nbytes = blksz;
    129  1.1  christos 	wcb.aio_offset = 0;
    130  1.1  christos 
    131  1.1  christos 	rv = aio_write(&wcb);
    132  1.1  christos 	ATF_REQUIRE_EQ(0, rv);
    133  1.1  christos 	wlist[0] = &wcb;
    134  1.1  christos 	wait_all(wlist, 1);
    135  1.1  christos 
    136  1.1  christos 	ATF_REQUIRE_EQ(0, aio_error(&wcb));
    137  1.1  christos 	ATF_REQUIRE_EQ((ssize_t)blksz, aio_return(&wcb));
    138  1.1  christos 
    139  1.1  christos 	memset(&rcb, 0, sizeof(rcb));
    140  1.1  christos 	rcb.aio_fildes = fd;
    141  1.1  christos 	rcb.aio_buf = rbuf;
    142  1.1  christos 	rcb.aio_nbytes = blksz;
    143  1.1  christos 	rcb.aio_offset = 0;
    144  1.1  christos 
    145  1.1  christos 	rv = aio_read(&rcb);
    146  1.1  christos 	ATF_REQUIRE_EQ(0, rv);
    147  1.1  christos 	rlist[0] = &rcb;
    148  1.1  christos 	wait_all(rlist, 1);
    149  1.1  christos 
    150  1.1  christos 	ATF_REQUIRE_EQ(0, aio_error(&rcb));
    151  1.1  christos 	ATF_REQUIRE_EQ((ssize_t)blksz, aio_return(&rcb));
    152  1.1  christos 	ATF_REQUIRE_EQ(0, memcmp(wbuf, rbuf, blksz));
    153  1.1  christos 
    154  1.1  christos 	rv = close(fd);
    155  1.1  christos 	ATF_REQUIRE_EQ(0, rv);
    156  1.1  christos 	rv = unlink(path);
    157  1.1  christos 	ATF_REQUIRE_EQ(0, rv);
    158  1.1  christos 
    159  1.1  christos 	free(wbuf);
    160  1.1  christos 	free(rbuf);
    161  1.1  christos }
    162  1.1  christos 
    163  1.1  christos ATF_TP_ADD_TCS(tp)
    164  1.1  christos {
    165  1.1  christos 	ATF_TP_ADD_TC(tp, write_then_read_back);
    166  1.1  christos 	return atf_no_error();
    167  1.1  christos }
    168