xref: /freebsd/tools/test/stress2/misc/fsync3.sh (revision 4dcc6baddb446d27a60e30ccc24b29d9e47b553a)
1*4dcc6badSPeter Holm#!/bin/sh
2*4dcc6badSPeter Holm
3*4dcc6badSPeter Holm#
4*4dcc6badSPeter Holm# SPDX-License-Identifier: BSD-2-Clause
5*4dcc6badSPeter Holm#
6*4dcc6badSPeter Holm# Copyright (c) 2023 Peter Holm <pho@FreeBSD.org>
7*4dcc6badSPeter Holm#
8*4dcc6badSPeter Holm# Redistribution and use in source and binary forms, with or without
9*4dcc6badSPeter Holm# modification, are permitted provided that the following conditions
10*4dcc6badSPeter Holm# are met:
11*4dcc6badSPeter Holm# 1. Redistributions of source code must retain the above copyright
12*4dcc6badSPeter Holm#    notice, this list of conditions and the following disclaimer.
13*4dcc6badSPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
14*4dcc6badSPeter Holm#    notice, this list of conditions and the following disclaimer in the
15*4dcc6badSPeter Holm#    documentation and/or other materials provided with the distribution.
16*4dcc6badSPeter Holm#
17*4dcc6badSPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*4dcc6badSPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*4dcc6badSPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*4dcc6badSPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*4dcc6badSPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*4dcc6badSPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*4dcc6badSPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*4dcc6badSPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*4dcc6badSPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*4dcc6badSPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*4dcc6badSPeter Holm# SUCH DAMAGE.
28*4dcc6badSPeter Holm#
29*4dcc6badSPeter Holm
30*4dcc6badSPeter Holm# Regression test for D37997: ffs_syncvnode(): avoid a LoR for SU
31*4dcc6badSPeter Holm# https://people.freebsd.org/~pho/stress/log/log0402.txt
32*4dcc6badSPeter Holm# Fixed by 6e1eabadcb1d - main - ffs_syncvnode(): avoid a LoR for SU
33*4dcc6badSPeter Holm
34*4dcc6badSPeter Holm# Test scenario based on report by jkim
35*4dcc6badSPeter Holm
36*4dcc6badSPeter Holm. ../default.cfg
37*4dcc6badSPeter Holm[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
38*4dcc6badSPeter Holm
39*4dcc6badSPeter Holmdir=/tmp
40*4dcc6badSPeter Holmodir=`pwd`
41*4dcc6badSPeter Holmprog=$(basename "$0" .sh)
42*4dcc6badSPeter Holmcd $dir
43*4dcc6badSPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/$prog.c
44*4dcc6badSPeter Holmmycc -o $prog -Wall -Wextra -O0 -g $prog.c -lpthread || exit 1
45*4dcc6badSPeter Holmrm -f $prog.c
46*4dcc6badSPeter Holmcd $odir
47*4dcc6badSPeter Holm
48*4dcc6badSPeter Holmset -eu
49*4dcc6badSPeter Holmmount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
50*4dcc6badSPeter Holm[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
51*4dcc6badSPeter Holmmdconfig -a -t swap -s 1g -u $mdstart
52*4dcc6badSPeter Holmnewfs -U md$mdstart > /dev/null
53*4dcc6badSPeter Holmmount /dev/md$mdstart $mntpoint
54*4dcc6badSPeter Holmset +e
55*4dcc6badSPeter Holm
56*4dcc6badSPeter Holmcd $odir
57*4dcc6badSPeter Holm../testcases/swap/swap -t 1m -i 20 -l 100 > /dev/null &
58*4dcc6badSPeter Holmsleep .5
59*4dcc6badSPeter Holmcd $mntpoint
60*4dcc6badSPeter Holmmkdir -p d1/d2/d3/d4/d5
61*4dcc6badSPeter Holmfor i in `jot 8`; do
62*4dcc6badSPeter Holm	$dir/$prog $i &
63*4dcc6badSPeter Holmdone
64*4dcc6badSPeter Holmcd $odir
65*4dcc6badSPeter Holmwait
66*4dcc6badSPeter Holm
67*4dcc6badSPeter Holmfor i in `jot 6`; do
68*4dcc6badSPeter Holm	mount | grep -q "on $mntpoint " || break
69*4dcc6badSPeter Holm	umount $mntpoint && break || sleep 10
70*4dcc6badSPeter Holm	[ $i -eq 6 ] &&
71*4dcc6badSPeter Holm	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
72*4dcc6badSPeter Holmdone
73*4dcc6badSPeter Holmmdconfig -d -u $mdstart
74*4dcc6badSPeter Holmrm -rf $dir/$prog
75*4dcc6badSPeter Holmexit 0
76*4dcc6badSPeter HolmEOF
77*4dcc6badSPeter Holm#include <sys/types.h>
78*4dcc6badSPeter Holm#include <sys/stat.h>
79*4dcc6badSPeter Holm
80*4dcc6badSPeter Holm#include <err.h>
81*4dcc6badSPeter Holm#include <fcntl.h>
82*4dcc6badSPeter Holm#include <pthread.h>
83*4dcc6badSPeter Holm#include <pthread_np.h>
84*4dcc6badSPeter Holm#include <signal.h>
85*4dcc6badSPeter Holm#include <stdio.h>
86*4dcc6badSPeter Holm#include <stdlib.h>
87*4dcc6badSPeter Holm#include <string.h>
88*4dcc6badSPeter Holm#include <unistd.h>
89*4dcc6badSPeter Holm
90*4dcc6badSPeter Holm#define RUNTIME 60
91*4dcc6badSPeter Holm
92*4dcc6badSPeter Holmstatic time_t start;
93*4dcc6badSPeter Holmstatic volatile int fd, n;
94*4dcc6badSPeter Holmstatic char buf[4096];
95*4dcc6badSPeter Holm
96*4dcc6badSPeter Holmstatic void *
97*4dcc6badSPeter Holmt1(void *data __unused)
98*4dcc6badSPeter Holm{
99*4dcc6badSPeter Holm	char *path = "d1/d2/d3/d4/d5";
100*4dcc6badSPeter Holm	char d1[1024], d2[1024], file[1024];
101*4dcc6badSPeter Holm
102*4dcc6badSPeter Holm	pthread_set_name_np(pthread_self(), __func__);
103*4dcc6badSPeter Holm	snprintf(d1, sizeof(d1), "%s/dir.%d", path, getpid());
104*4dcc6badSPeter Holm	snprintf(d2, sizeof(d2), "%s/new.%d", path, getpid());
105*4dcc6badSPeter Holm	snprintf(file, sizeof(file), "%s/../file.%d", path, getpid());
106*4dcc6badSPeter Holm	while (time(NULL) - start < RUNTIME) {
107*4dcc6badSPeter Holm		if (mkdir(d1, 0740) == -1)
108*4dcc6badSPeter Holm			err(1, "mkdir(%s)", d1);
109*4dcc6badSPeter Holm		if (rename(d1, d2) == -1)
110*4dcc6badSPeter Holm			err(1, "rename(%s, %s)", d1, d2);
111*4dcc6badSPeter Holm		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) == -1)
112*4dcc6badSPeter Holm			err(1, "open%s()", file);
113*4dcc6badSPeter Holm		if (write(fd, buf, sizeof(buf)) != sizeof(buf))
114*4dcc6badSPeter Holm			err(1, "write()");
115*4dcc6badSPeter Holm		close(fd);
116*4dcc6badSPeter Holm		if (rename(d2, d1) == -1)
117*4dcc6badSPeter Holm			err(1, "rename(%s, %s)", d2, d1);
118*4dcc6badSPeter Holm		if (rmdir(d1) == -1)
119*4dcc6badSPeter Holm			err(1, "rmdir(%s)", d1);
120*4dcc6badSPeter Holm	}
121*4dcc6badSPeter Holm
122*4dcc6badSPeter Holm	return (NULL);
123*4dcc6badSPeter Holm}
124*4dcc6badSPeter Holm
125*4dcc6badSPeter Holmstatic void *
126*4dcc6badSPeter Holmt2(void *data __unused)
127*4dcc6badSPeter Holm{
128*4dcc6badSPeter Holm	pthread_set_name_np(pthread_self(), __func__);
129*4dcc6badSPeter Holm	while (time(NULL) - start < RUNTIME) {
130*4dcc6badSPeter Holm		fsync(fd);
131*4dcc6badSPeter Holm		usleep(arc4random() % 500);
132*4dcc6badSPeter Holm	}
133*4dcc6badSPeter Holm
134*4dcc6badSPeter Holm	return (NULL);
135*4dcc6badSPeter Holm}
136*4dcc6badSPeter Holm
137*4dcc6badSPeter Holmint
138*4dcc6badSPeter Holmmain(int argc __unused, char *argv[])
139*4dcc6badSPeter Holm{
140*4dcc6badSPeter Holm	pthread_t tid[2];
141*4dcc6badSPeter Holm	int r;
142*4dcc6badSPeter Holm
143*4dcc6badSPeter Holm	n = atoi(argv[1]);
144*4dcc6badSPeter Holm	start = time(NULL);
145*4dcc6badSPeter Holm	if ((r = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
146*4dcc6badSPeter Holm		errc(1, r, "pthread_create");
147*4dcc6badSPeter Holm	if ((r = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
148*4dcc6badSPeter Holm		errc(1, r, "pthread_create");
149*4dcc6badSPeter Holm
150*4dcc6badSPeter Holm	if ((r = pthread_join(tid[0], NULL)) != 0)
151*4dcc6badSPeter Holm		errc(1, r, "pthread_join");
152*4dcc6badSPeter Holm	if ((r = pthread_join(tid[1], NULL)) != 0)
153*4dcc6badSPeter Holm		errc(1, r, "pthread_join");
154*4dcc6badSPeter Holm
155*4dcc6badSPeter Holm	return (0);
156*4dcc6badSPeter Holm}
157