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