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