xref: /freebsd/tools/test/stress2/misc/zfs12.sh (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2021 Peter Holm
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# lseek(SEEK_HOLE): finds hole
31# Test scenario suggestion by: kib@
32# Related to:
33# Bug 256205 - ZFS: data corruption with SEEK_HOLE/SEEK_DATA on dirty files ...
34
35[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36kldstat -v | grep -q zfs.ko  || { kldload zfs.ko; loaded=1; } ||
37    exit 0
38
39. ../default.cfg
40
41here=`pwd`
42cd /tmp
43sed '1,/^EOF/d' < $here/$0 > zfs12.c
44mycc -o zfs12 -Wall -Wextra -O0 -g zfs12.c || exit 1
45rm -f zfs12.c
46cc -o /tmp/lsholes -Wall -Wextra -O2 $here/../tools/lsholes.c | exit 1
47
48mp1=/stress2_tank/test
49u1=$mdstart
50u2=$((u1 + 1))
51
52set -e
53mdconfig -l | grep -q md$u1 && mdconfig -d -u $u1
54mdconfig -l | grep -q md$u2 && mdconfig -d -u $u2
55
56mdconfig -s 2g -u $u1
57mdconfig -s 2g -u $u2
58
59zpool list | egrep -q "^stress2_tank" && zpool destroy stress2_tank
60[ -d /stress2_tank ] && rm -rf /stress2_tank
61zpool create stress2_tank md$u1 md$u2
62zfs create stress2_tank/test
63set +e
64
65(cd $here/../testcases/swap; ./swap -t 2m -i 20 -l 100 -h > /dev/null) &
66file1=$mp1/file		# zfs path
67sleep 20
68/tmp/zfs12 $file1
69/tmp/lsholes $file1
70s=$?
71while pkill swap; do sleep 1; done
72wait
73
74zfs umount stress2_tank/test
75zfs destroy -r stress2_tank
76zpool destroy stress2_tank
77mdconfig -d -u $u1
78mdconfig -d -u $u2
79rm -f /tmp/zfs12 /tmp/lsholes
80[ $loaded ] && kldunload zfs.ko
81exit $s
82EOF
83#include <sys/types.h>
84#include <err.h>
85#include <stdio.h>
86#include <stdlib.h>
87#include <unistd.h>
88#include <fcntl.h>
89#include <sys/mman.h>
90#include <sys/param.h>
91#include <string.h>
92#include <unistd.h>
93#include <errno.h>
94
95#define SIZ  (500UL * 1024 * 1024)
96
97int
98main(int argc __unused, char *argv[])
99{
100	off_t hole;
101	size_t len;
102	int fd;
103	char *p, *path;
104
105	len = SIZ;
106
107	path = argv[1];
108	if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1)
109		err(1,"open()");
110	if (ftruncate(fd, len) == -1)
111		err(1, "ftruncate");
112	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
113			MAP_FAILED) {
114		if (errno == ENOMEM)
115			return (1);
116		err(1, "mmap(1)");
117	}
118	p[1 * 1024] = 1;
119	p[2 * 1024] = 1;
120	p[4 * 1024] = 1;
121
122	if (msync(p, len, MS_SYNC | MS_INVALIDATE) == -1)
123		err(1, "msync()");
124
125	if ((hole = lseek(fd, 0, SEEK_HOLE)) == -1)
126		err(1, "lseek(SEEK_HOLE)");
127	if (hole != SIZ)
128		printf("--> hole = %jd, file size=%jd\n",
129		    (intmax_t)hole, (intmax_t)SIZ);
130	close(fd);
131
132	return (hole == SIZ ? 0 : 1);
133}
134