xref: /freebsd/tools/test/stress2/misc/fsync3.sh (revision 4d846d26)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2023 Peter Holm <pho@FreeBSD.org>
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# Regression test for D37997: ffs_syncvnode(): avoid a LoR for SU
31# https://people.freebsd.org/~pho/stress/log/log0402.txt
32# Fixed by 6e1eabadcb1d - main - ffs_syncvnode(): avoid a LoR for SU
33
34# Test scenario based on report by jkim
35
36. ../default.cfg
37[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
38
39dir=/tmp
40odir=`pwd`
41prog=$(basename "$0" .sh)
42cd $dir
43sed '1,/^EOF/d' < $odir/$0 > $dir/$prog.c
44mycc -o $prog -Wall -Wextra -O0 -g $prog.c -lpthread || exit 1
45rm -f $prog.c
46cd $odir
47
48set -eu
49mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
50[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
51mdconfig -a -t swap -s 1g -u $mdstart
52newfs -U md$mdstart > /dev/null
53mount /dev/md$mdstart $mntpoint
54set +e
55
56cd $odir
57../testcases/swap/swap -t 1m -i 20 -l 100 > /dev/null &
58sleep .5
59cd $mntpoint
60mkdir -p d1/d2/d3/d4/d5
61for i in `jot 8`; do
62	$dir/$prog $i &
63done
64cd $odir
65wait
66
67for i in `jot 6`; do
68	mount | grep -q "on $mntpoint " || break
69	umount $mntpoint && break || sleep 10
70	[ $i -eq 6 ] &&
71	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
72done
73mdconfig -d -u $mdstart
74rm -rf $dir/$prog
75exit 0
76EOF
77#include <sys/types.h>
78#include <sys/stat.h>
79
80#include <err.h>
81#include <fcntl.h>
82#include <pthread.h>
83#include <pthread_np.h>
84#include <signal.h>
85#include <stdio.h>
86#include <stdlib.h>
87#include <string.h>
88#include <unistd.h>
89
90#define RUNTIME 60
91
92static time_t start;
93static volatile int fd, n;
94static char buf[4096];
95
96static void *
97t1(void *data __unused)
98{
99	char *path = "d1/d2/d3/d4/d5";
100	char d1[1024], d2[1024], file[1024];
101
102	pthread_set_name_np(pthread_self(), __func__);
103	snprintf(d1, sizeof(d1), "%s/dir.%d", path, getpid());
104	snprintf(d2, sizeof(d2), "%s/new.%d", path, getpid());
105	snprintf(file, sizeof(file), "%s/../file.%d", path, getpid());
106	while (time(NULL) - start < RUNTIME) {
107		if (mkdir(d1, 0740) == -1)
108			err(1, "mkdir(%s)", d1);
109		if (rename(d1, d2) == -1)
110			err(1, "rename(%s, %s)", d1, d2);
111		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) == -1)
112			err(1, "open%s()", file);
113		if (write(fd, buf, sizeof(buf)) != sizeof(buf))
114			err(1, "write()");
115		close(fd);
116		if (rename(d2, d1) == -1)
117			err(1, "rename(%s, %s)", d2, d1);
118		if (rmdir(d1) == -1)
119			err(1, "rmdir(%s)", d1);
120	}
121
122	return (NULL);
123}
124
125static void *
126t2(void *data __unused)
127{
128	pthread_set_name_np(pthread_self(), __func__);
129	while (time(NULL) - start < RUNTIME) {
130		fsync(fd);
131		usleep(arc4random() % 500);
132	}
133
134	return (NULL);
135}
136
137int
138main(int argc __unused, char *argv[])
139{
140	pthread_t tid[2];
141	int r;
142
143	n = atoi(argv[1]);
144	start = time(NULL);
145	if ((r = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
146		errc(1, r, "pthread_create");
147	if ((r = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
148		errc(1, r, "pthread_create");
149
150	if ((r = pthread_join(tid[0], NULL)) != 0)
151		errc(1, r, "pthread_join");
152	if ((r = pthread_join(tid[1], NULL)) != 0)
153		errc(1, r, "pthread_join");
154
155	return (0);
156}
157