xref: /freebsd/tools/test/stress2/misc/link.sh (revision 608c97bf)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Deadlock seen for file systems which suspend writes on unmount, such as
30# UFS and tmpfs.
31# http://people.freebsd.org/~pho/stress/log/link.txt
32# Fixed by r272130
33
34[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
35
36. ../default.cfg
37
38here=`pwd`
39cd /tmp
40sed '1,/^EOF/d' < $here/$0 > link.c
41mycc -o link -Wall -Wextra -O2 -g link.c || exit 1
42rm -f link.c
43
44mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
45[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
46
47mdconfig -a -t swap -s 1g -u $mdstart || exit 1
48newfs $newfs_flags md$mdstart > /dev/null
49mount /dev/md$mdstart $mntpoint
50
51daemon sh -c "(cd $here/../testcases/swap; ./swap -t 5m -i 20 -h -l 100)" \
52    > /dev/null 2>&1
53/tmp/link $mntpoint > /dev/null 2>&1 &
54
55for i in `jot 100`; do
56	umount -f $mntpoint &&
57	    mount /dev/md$mdstart $mntpoint
58	sleep .1
59done
60pkill -9 link
61wait
62
63while mount | grep $mntpoint | grep -q /dev/md; do
64	umount $mntpoint || sleep 1
65done
66mdconfig -d -u $mdstart
67
68# tmpfs
69mount -t tmpfs tmpfs $mntpoint
70
71/tmp/link $mntpoint > /dev/null 2>&1 &
72
73for i in `jot 100`; do
74	umount -f $mntpoint &&
75	    mount -t tmpfs tmpfs $mntpoint
76	sleep .1
77done
78pkill -9 link swap
79wait
80
81while pkill -9 swap; do
82	:
83done > /dev/null 2>&1
84while mount | grep $mntpoint | grep -q tmpfs; do
85	umount $mntpoint || sleep 1
86done
87[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
88rm -f /tmp/link
89exit 0
90EOF
91#include <sys/param.h>
92#include <sys/wait.h>
93
94#include <err.h>
95#include <errno.h>
96#include <fcntl.h>
97#include <stdio.h>
98#include <time.h>
99#include <stdlib.h>
100#include <unistd.h>
101
102#define PARALLEL 16
103#define RUNTIME 300
104
105time_t start;
106char *dir;
107
108void
109trename(void)
110{
111	int fd;
112	char name1[MAXPATHLEN + 1];
113	char name2[MAXPATHLEN + 1];
114
115	setproctitle(__func__);
116	snprintf(name1, sizeof(name1), "%s/r1.%05d", dir, getpid());
117	snprintf(name2, sizeof(name2), "%s/r2.%05d", dir, getpid());
118	if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
119		err(1, "open(%s)", name1);
120	close(fd);
121
122	while (time(NULL) - start < RUNTIME) {
123		if (rename(name1, name2) == -1) {
124			if (errno == ENOENT) {
125				if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
126					err(1, "open(%s)", name1);
127				close(fd);
128				continue;
129			} else
130				warn("link(%s, %s)", name1, name2);
131		}
132		if (rename(name2, name1) == -1)
133			warn("link(%s, %s)", name2, name1);
134	}
135	unlink(name1);
136	unlink(name2);
137	_exit(0);
138}
139
140void
141tlink(void)
142{
143	int fd;
144	char name1[MAXPATHLEN + 1];
145	char name2[MAXPATHLEN + 1];
146
147	setproctitle(__func__);
148	snprintf(name1, sizeof(name1), "%s/f1.%05d", dir, getpid());
149	snprintf(name2, sizeof(name2), "%s/f2.%05d", dir, getpid());
150	if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
151		err(1, "open(%s)", name1);
152	close(fd);
153
154	while (time(NULL) - start < RUNTIME) {
155		unlink(name2);
156		if (link(name1, name2) == -1) {
157			if (errno == ENOENT) {
158				if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
159					err(1, "open(%s)", name1);
160				close(fd);
161				continue;
162			} else
163				warn("link(%s, %s)", name1, name2);
164		}
165	}
166	unlink(name1);
167	unlink(name2);
168	_exit(0);
169}
170
171int
172main(int argc, char **argv)
173{
174	int i, type;
175
176	if (argc != 2)
177		errx(1, "Usage: %s <full path to dir>", argv[0]);
178	dir = argv[1];
179	type = arc4random() % 2; /* test either link() or rename() */
180
181	start = time(NULL);
182	for (i = 0; i < PARALLEL; i++) {
183		if (type == 0 && fork() == 0)
184			tlink();
185		if (type == 1 && fork() == 0)
186			trename();
187	}
188	for (i = 0; i < PARALLEL; i++)
189		wait(NULL);
190
191	return(0);
192}
193