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