From: sf Date: Mon, 29 May 2017 11:01:16 +0000 (+0000) Subject: Regress test for sparse files & mmap with different FSs X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=8b1dbb2a194fd677ad3a9b1b64d7a57fd9e11cae;p=openbsd Regress test for sparse files & mmap with different FSs This test creates a file with a hole in the middle and then checks that when reading or mmaping the whole file, the data is correct. This catches the bug introduced by the "Implement VFS read clustering for MSDOSFS" commit that hat to be reverted. For now, we run the test with FAT16, FAT32, FFS With much advice from bluhm@ --- diff --git a/regress/sys/fileops/Makefile b/regress/sys/fileops/Makefile new file mode 100644 index 00000000000..31cfd9c0ece --- /dev/null +++ b/regress/sys/fileops/Makefile @@ -0,0 +1,9 @@ +# $OpenBSD: Makefile,v 1.1 2017/05/29 11:01:16 sf Exp $ + +SUBDIR= msdos32 msdos16 ffs +PROG= fileops +NOMAN= yes + +CFLAGS += -std=gnu99 -Wall -Werror -Wextra -g + +.include diff --git a/regress/sys/fileops/Makefile.inc b/regress/sys/fileops/Makefile.inc new file mode 100644 index 00000000000..74ef34ffc13 --- /dev/null +++ b/regress/sys/fileops/Makefile.inc @@ -0,0 +1,51 @@ +# $OpenBSD: Makefile.inc,v 1.1 2017/05/29 11:01:16 sf Exp $ + +TESTS= create read mmap + +FILEOPS_MNT= /mnt/regress-fileops +FILEOPS_PROG= ${.OBJDIR}/../fileops +IMG= ${.OBJDIR}/diskimage +CLEANFILES= ${IMG} stamp-* + + +.poison !defined (MOUNT) +.poison !defined (NEWFS) + +.PHONY: disk mount unconfig clean + +disk: unconfig + dd if=/dev/urandom of=${IMG} bs=1M count=32 + vnconfig vnd0 ${IMG} + ${NEWFS} /dev/rvnd0c + +mount: disk + mkdir -p ${FILEOPS_MNT} + ${MOUNT} /dev/vnd0c ${FILEOPS_MNT} + +unconfig: + -umount -f /dev/vnd0c 2>/dev/null || true + -rmdir ${FILEOPS_MNT} 2>/dev/null || true + -vnconfig -u vnd0 2>/dev/null || true + -rm -f stamp-setup ${IMG} + +stamp-setup: + @echo '\n======== $@ ========' + ${.MAKE} -C ${.CURDIR} mount + date >$@ + +${.OBJDIR}/../fileops: + ${.MAKE} -C ${.CURDIR}/.. fileops + +.for t in ${TESTS} +REGRESS_TARGETS+= run-regress-${t} +run-regress-${t}: stamp-setup ${.OBJDIR}/../fileops + @echo '\n======== $@ ========' + cd ${FILEOPS_MNT} && \ + ${FILEOPS_PROG} ${t} ${FILEOPS_MNT}/file +.endfor + +REGRESS_TARGETS+= run-regress-cleanup +run-regress-cleanup: + @echo '\n======== $@ ========' + umount ${FILEOPS_MNT} + ${.MAKE} -C ${.CURDIR} unconfig diff --git a/regress/sys/fileops/ffs/Makefile b/regress/sys/fileops/ffs/Makefile new file mode 100644 index 00000000000..abcbb411389 --- /dev/null +++ b/regress/sys/fileops/ffs/Makefile @@ -0,0 +1,6 @@ +# $OpenBSD: Makefile,v 1.1 2017/05/29 11:01:16 sf Exp $ + +NEWFS = newfs +MOUNT = mount + +.include diff --git a/regress/sys/fileops/fileops.c b/regress/sys/fileops/fileops.c new file mode 100644 index 00000000000..c552ca451ed --- /dev/null +++ b/regress/sys/fileops/fileops.c @@ -0,0 +1,261 @@ +/* $OpenBSD: fileops.c,v 1.1 2017/05/29 11:01:16 sf Exp $ */ +/* + * Copyright (c) 2017 Stefan Fritsch + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFSIZE (16 * 1024) +#define HOLESIZE (16 * BUFSIZE) + +#define myerr(rc, fmt, ...) \ + do { err(rc, fmt "\nFAILED", ## __VA_ARGS__); } while (0) +#define myerrx(rc, fmt, ...) \ + do { errx(rc, fmt "\nFAILED", ## __VA_ARGS__); } while (0) + +static int debug = 0; +static int fd = -1; +static off_t curpos = 0; +static char *fname; +static char *gbuf; +static char *mbuf; + +void +gen_data(void *buf, size_t size, uint32_t seed) +{ + assert(size % 4 == 0); + if (debug) + printf("%s: size %zd seed %#08x\n", __func__, size, seed); + uint32_t *ibuf = buf; + for (size_t i = 0; i < size / 4; i++) + ibuf[i] = seed + i; +} + +void +check_data(const void *buf, size_t size, uint32_t seed) +{ + assert(size % 4 == 0); + const uint32_t *ibuf = buf; + for (size_t i = 0; i < size / 4; i++) { + if (ibuf[i] != seed + i) { + myerrx(3, "%s: pos %zd/%zd: expected %#08zx got %#08x\n", __func__, 4 * i, size, seed + i, ibuf[i]); + } + } +} + +void +check_zero(const void *buf, size_t size) +{ + assert(size % 4 == 0); + const uint32_t *ibuf = buf; + for (size_t i = 0; i < size / 4; i++) { + if (ibuf[i] != 0) { + myerrx(3, "%s: pos %zd/%zd: expected 0 got %#08x\n", __func__, 4 * i, size, ibuf[i]); + } + } +} + +void +check(const char *what, int64_t have, int64_t want) +{ + if (have != want) { + if (have == -1) + myerr(2, "%s returned %lld, expected %lld", what, have, want); + else + myerrx(2, "%s returned %lld, expected %lld", what, have, want); + } + + if (debug) + printf("%s returned %lld\n", what, have); +} + +void +c_write(void *buf, size_t size) +{ + ssize_t ret = write(fd, buf, size); + check("write", ret, size); + curpos += ret; +} + +void +c_read(void *buf, size_t size) +{ + ssize_t ret = read(fd, buf, size); + check("read", ret, size); + curpos += ret; +} + +void +c_fsync(void) +{ + int ret = fsync(fd); + check("fsync", ret, 0); +} + +void +c_lseek(off_t offset, int whence) +{ + off_t ret = lseek(fd, offset, whence); + switch (whence) { + case SEEK_SET: + curpos = offset; + break; + case SEEK_CUR: + curpos += offset; + break; + default: + myerrx(1, "c_lseek not supported"); + } + check("lseek", ret, curpos); + if (debug) + printf("curpos: %lld\n", (long long int)curpos); +} + +void +c_mmap(size_t size) +{ + mbuf = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, curpos); + if (mbuf == MAP_FAILED) + myerr(2, "mmap %zd pos %lld failed", size, (long long)curpos); + curpos += size; + if (debug) + printf("mmap: %p\n", mbuf); +} + +void +c_munmap(size_t size) +{ + int ret = munmap(mbuf, size); + if (ret != 0) + myerr(2, "munmap"); +} + +void +c_open(int flags) +{ + fd = open(fname, flags, S_IRUSR|S_IWUSR); + if (fd == -1) + myerr(1, "open"); +} + +void +check_read(size_t size, int hole) +{ + size_t pos = 0; + while (pos < size) { + size_t to_read = size - pos; + uint32_t seed = curpos; + if (to_read > BUFSIZE) + to_read = BUFSIZE; + c_read(gbuf, to_read); + if (hole) + check_zero(gbuf, to_read); + else + check_data(gbuf, to_read, seed); + pos += to_read; + } +} + +/* XXX this assumes size is a multiple of the page size */ +void +check_mmap(size_t size, int hole) +{ + size_t pos = 0; + while (pos < size) { + size_t to_read = size - pos; + uint32_t seed = curpos; + if (to_read > BUFSIZE) + to_read = BUFSIZE; + c_mmap(to_read); + if (hole) + check_zero(mbuf, to_read); + else + check_data(mbuf, to_read, seed); + c_munmap(to_read); + pos += to_read; + } +} + +void +do_create(void) +{ + unlink(fname); + c_open(O_EXCL|O_CREAT|O_RDWR); + + gen_data(gbuf, BUFSIZE, curpos); + c_write(gbuf, BUFSIZE); + + c_lseek(HOLESIZE, SEEK_CUR); + + gen_data(gbuf, BUFSIZE, curpos); + c_write(gbuf, BUFSIZE); + c_fsync(); +} + +void +do_read(void) +{ + c_open(O_RDWR); + check_read(BUFSIZE, 0); + check_read(HOLESIZE, 1); + check_read(BUFSIZE, 0); +} + +void +do_mmap(void) +{ + c_open(O_RDWR); + check_mmap(BUFSIZE, 0); + check_mmap(HOLESIZE, 1); + check_mmap(BUFSIZE, 0); +} + +void +usage(void) +{ + myerrx(1, "usage: fileops (create|read|mmap) filename"); +} + +int main(int argc, char **argv) +{ + if (argc != 3) + usage(); + + fname = argv[2]; + gbuf = malloc(BUFSIZE); + if (gbuf == NULL) + myerrx(1, "malloc"); + + if (strcmp(argv[1], "create") == 0) { + do_create(); + } else if (strcmp(argv[1], "read") == 0) { + do_read(); + } else if (strcmp(argv[1], "mmap") == 0) { + do_mmap(); + } else { + usage(); + } + + printf("ok\n"); + return 0; +} diff --git a/regress/sys/fileops/msdos16/Makefile b/regress/sys/fileops/msdos16/Makefile new file mode 100644 index 00000000000..ecb1347f237 --- /dev/null +++ b/regress/sys/fileops/msdos16/Makefile @@ -0,0 +1,6 @@ +# $OpenBSD: Makefile,v 1.1 2017/05/29 11:01:16 sf Exp $ + +NEWFS = newfs_msdos -F 16 +MOUNT = mount_msdos + +.include diff --git a/regress/sys/fileops/msdos32/Makefile b/regress/sys/fileops/msdos32/Makefile new file mode 100644 index 00000000000..3ed021981b6 --- /dev/null +++ b/regress/sys/fileops/msdos32/Makefile @@ -0,0 +1,6 @@ +# $OpenBSD: Makefile,v 1.1 2017/05/29 11:01:16 sf Exp $ + +NEWFS = newfs_msdos -F 32 +MOUNT = mount_msdos + +.include