xref: /freebsd/tools/test/stress2/misc/rename10.sh (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 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[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
30
31# Regression test for rename(2) problem with missing reference release of
32# a busy "to" vnode, resulting in a leak.
33# Fixed in r253998.
34
35. ../default.cfg
36
37here=`pwd`
38cd /tmp
39sed '1,/^EOF/d' < $here/$0 > rename10.c
40mycc -o rename10 -Wall -Wextra -g -O2 rename10.c || exit 1
41rm -f rename10.c
42cd $here
43
44mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
45mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
46
47mdconfig -a -t swap -s 4g -u $mdstart || exit 1
48newfs $newfs_flags md$mdstart > /dev/null
49mount /dev/md$mdstart $mntpoint
50avail=`df -k $mntpoint | tail -1 | awk '{print $4}'`
51
52cd $mntpoint
53/tmp/rename10; s=$?
54cd $here
55
56for i in `jot 3`; do
57	sync
58	sleep 2
59done
60
61if [ `df -k $mntpoint | tail -1 | awk '{print $4}'` -lt $avail ]; then
62	echo FAIL
63	ls -ial $mntpoint
64	df -i $mntpoint
65fi
66
67n=0
68while mount | grep "on $mntpoint " | grep -q /dev/md; do
69	umount $mntpoint || sleep 1
70	n=$((n + 1))
71	[ $n -gt 5 ] && { umount -f $mntpoint; break; }
72done
73
74checkfs /dev/md$mdstart || s=$?
75rm -f /tmp/rename10
76mdconfig -d -u $mdstart
77exit $s
78EOF
79#include <err.h>
80#include <fcntl.h>
81#include <pthread.h>
82#include <signal.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <sys/stat.h>
86#include <sys/types.h>
87#include <sys/wait.h>
88#include <unistd.h>
89
90#define PARALLEL 4
91#define SIZE (1 * 1024 * 1024)
92
93static char *logfile = "logfile";
94static char *oldfiles[] = {
95    "logfile.0", "logfile.1", "logfile.2", "logfile.3", "logfile.4"
96};
97
98void *
99logger(void)
100{
101	int fd;
102	char * cp;
103
104	setproctitle("logger");
105	cp = calloc(1, SIZE);
106	for(;;) {
107		if ((fd = open(logfile, O_RDWR | O_APPEND)) != -1) {
108			if (write(fd, cp, SIZE) != SIZE)
109				err(1, "write()");
110			close(fd);
111		}
112		usleep(1);
113	}
114
115	_exit(0);
116}
117
118void *
119spin(void)
120{
121	int fd, i;
122
123	setproctitle("spin");
124	for(;;) {
125		for (i = 0; i < 5; i++) {
126			if ((fd = open(oldfiles[i], O_RDWR | O_APPEND)) != -1)
127				close(fd);
128		}
129		usleep(1);
130	}
131	_exit(0);
132}
133
134void
135renamer()
136{
137	int fd, i;
138	time_t start;
139
140	setproctitle("renamer");
141	start = time(NULL);
142	i = 0;
143	while (time(NULL) - start < 60) {
144		if ((fd = open(logfile, O_RDWR | O_CREAT | O_EXCL, 0644)) == -1)
145			err(1, "creat(%s)", logfile);
146		close(fd);
147		if (rename(logfile, oldfiles[i]) == -1)
148			err(1, "rename(%s, %s)", logfile, oldfiles[i]);
149		i = (i + 1) % 5;
150	}
151	for (i = 0; i < 5; i++) {
152		unlink(oldfiles[i]);
153	}
154	unlink(logfile);
155
156}
157
158int
159main() {
160	pid_t pids[PARALLEL], spids[PARALLEL];
161	int i;
162
163	for (i = 0; i < PARALLEL; i++) {
164		if ((pids[i] = fork()) == 0)
165			logger();
166		if ((spids[i] = fork()) == 0)
167			spin();
168	}
169
170	renamer();
171
172	for (i = 0; i < PARALLEL; i++) {
173		if (kill(pids[i], SIGINT) == -1)
174			err(1, "kill(%d)", pids[i]);
175		if (kill(spids[i], SIGINT) == -1)
176			err(1, "kill(%d)", spids[i]);
177	}
178	for (i = 0; i < PARALLEL * 2; i++)
179		wait(NULL);
180	wait(NULL);
181
182	return (0);
183}
184