xref: /freebsd/tools/test/stress2/misc/link.sh (revision bfcc09ddd422c95a1a2e4e794b63ee54c4902398)
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
48bsdlabel -w md$mdstart auto
49newfs $newfs_flags md${mdstart}$part > /dev/null
50mount /dev/md${mdstart}$part $mntpoint
51
52daemon sh -c "(cd $here/../testcases/swap; ./swap -t 5m -i 20 -h -l 100)" \
53    > /dev/null 2>&1
54/tmp/link $mntpoint > /dev/null 2>&1 &
55
56for i in `jot 100`; do
57	umount -f $mntpoint &&
58	    mount /dev/md${mdstart}$part $mntpoint
59	sleep .1
60done
61pkill -9 link
62wait
63
64while mount | grep $mntpoint | grep -q /dev/md; do
65	umount $mntpoint || sleep 1
66done
67mdconfig -d -u $mdstart
68
69# tmpfs
70mount -t tmpfs tmpfs $mntpoint
71
72/tmp/link $mntpoint > /dev/null 2>&1 &
73
74for i in `jot 100`; do
75	umount -f $mntpoint &&
76	    mount -t tmpfs tmpfs $mntpoint
77	sleep .1
78done
79pkill -9 link swap
80wait
81
82while pkill -9 swap; do
83	:
84done > /dev/null 2>&1
85while mount | grep $mntpoint | grep -q tmpfs; do
86	umount $mntpoint || sleep 1
87done
88[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
89rm -f /tmp/link
90exit 0
91EOF
92#include <sys/param.h>
93#include <sys/wait.h>
94
95#include <err.h>
96#include <errno.h>
97#include <fcntl.h>
98#include <stdio.h>
99#include <time.h>
100#include <stdlib.h>
101#include <unistd.h>
102
103#define PARALLEL 16
104#define RUNTIME 300
105
106time_t start;
107char *dir;
108
109void
110trename(void)
111{
112	int fd;
113	char name1[MAXPATHLEN + 1];
114	char name2[MAXPATHLEN + 1];
115
116	setproctitle(__func__);
117	snprintf(name1, sizeof(name1), "%s/r1.%05d", dir, getpid());
118	snprintf(name2, sizeof(name2), "%s/r2.%05d", dir, getpid());
119	if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
120		err(1, "open(%s)", name1);
121	close(fd);
122
123	while (time(NULL) - start < RUNTIME) {
124		if (rename(name1, name2) == -1) {
125			if (errno == ENOENT) {
126				if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
127					err(1, "open(%s)", name1);
128				close(fd);
129				continue;
130			} else
131				warn("link(%s, %s)", name1, name2);
132		}
133		if (rename(name2, name1) == -1)
134			warn("link(%s, %s)", name2, name1);
135	}
136	unlink(name1);
137	unlink(name2);
138	_exit(0);
139}
140
141void
142tlink(void)
143{
144	int fd;
145	char name1[MAXPATHLEN + 1];
146	char name2[MAXPATHLEN + 1];
147
148	setproctitle(__func__);
149	snprintf(name1, sizeof(name1), "%s/f1.%05d", dir, getpid());
150	snprintf(name2, sizeof(name2), "%s/f2.%05d", dir, getpid());
151	if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
152		err(1, "open(%s)", name1);
153	close(fd);
154
155	while (time(NULL) - start < RUNTIME) {
156		unlink(name2);
157		if (link(name1, name2) == -1) {
158			if (errno == ENOENT) {
159				if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
160					err(1, "open(%s)", name1);
161				close(fd);
162				continue;
163			} else
164				warn("link(%s, %s)", name1, name2);
165		}
166	}
167	unlink(name1);
168	unlink(name2);
169	_exit(0);
170}
171
172int
173main(int argc, char **argv)
174{
175	int i, type;
176
177	if (argc != 2)
178		errx(1, "Usage: %s <full path to dir>", argv[0]);
179	dir = argv[1];
180	type = arc4random() % 2; /* test either link() or rename() */
181
182	start = time(NULL);
183	for (i = 0; i < PARALLEL; i++) {
184		if (type == 0 && fork() == 0)
185			tlink();
186		if (type == 1 && fork() == 0)
187			trename();
188	}
189	for (i = 0; i < PARALLEL; i++)
190		wait(NULL);
191
192	return(0);
193}
194