xref: /freebsd/tools/test/stress2/misc/rename5.sh (revision 19261079b74319502c6ffa1249920079f0f69a72)
1#!/bin/sh
2
3#
4# Copyright (c) 2011 Peter Holm <pho@FreeBSD.org>
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# Rename scenario from kern/156545 by Konstantin,
30# konstantin malov kaspersky com
31
32# On 8.2-STABLE fsck reports
33# "xxx IS AN EXTRANEOUS HARD LINK TO DIRECTORY yyy"
34# Fixed by r220986
35
36[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
37
38. ../default.cfg
39
40mount | grep -q "$mntpoint" && umount $mntpoint
41mdconfig -l | grep -q $mdstart &&  mdconfig -d -u $mdstart
42
43mdconfig -a -t swap -s 1g -u $mdstart
44bsdlabel -w md$mdstart auto
45
46newfs $newfs_flags md${mdstart}$part > /dev/null
47mount /dev/md${mdstart}$part $mntpoint
48
49here=`pwd`
50cd /tmp
51sed '1,/^EOF/d' < $here/$0 > rename5.c
52mycc -o rename5 -Wall -Wextra -O2 rename5.c
53rm -f rename5.c
54
55cd $mntpoint
56
57/tmp/rename5
58
59cd $here
60rm -f /tmp/rename5
61
62while mount | grep -q md${mdstart}$part; do
63	umount $mntpoint || sleep 1
64done
65
66checkfs /dev/md${mdstart}$part; s=$?
67
68mdconfig -d -u $mdstart
69exit $s
70EOF
71#include <sys/stat.h>
72#include <sys/wait.h>
73
74#include <err.h>
75#include <fcntl.h>
76#include <stdio.h>
77#include <stdlib.h>
78#include <time.h>
79#include <unistd.h>
80
81#define N 1000
82#define RUNTIME (5 * 60)
83
84void
85test(void)
86{
87	pid_t pid;
88	int i;
89	char from[128], to[128];
90
91	pid = getpid();
92	for (i = 0; i < N; i++) {
93		snprintf(from, sizeof(from), "src/dir.%06d", i);
94		snprintf(to  , sizeof(to),   "src/dir.%06d.%06d", i, pid);
95		(void)rename(from, to);
96	}
97	_exit(0);
98}
99
100int
101main()
102{
103	time_t start;
104	int fd, i;
105	char dir[128], file[128];
106
107	start = time(NULL);
108	while (time(NULL) - start < RUNTIME) {
109		if (mkdir("src", 0700) == -1)
110			err(1, "mkdir(src)");
111
112		for (i = 0; i < N; i++) {
113			snprintf(dir, sizeof(dir), "src/dir.%06d", i);
114			if (mkdir(dir, 0700) == -1)
115				err(1, "mkdir(%s)", dir);
116			snprintf(file, sizeof(file), "%s/meta", dir);
117			if ((fd = open(file, O_RDWR | O_CREAT, 0600)) < 0)
118				err(1, "open(%s)", file);
119			close(fd);
120			snprintf(file, sizeof(file), "%s/data", dir);
121			if ((fd = open(file, O_RDWR | O_CREAT, 0600)) < 0)
122				err(1, "open(%s)", file);
123			close(fd);
124		}
125
126		for (i = 0; i < 2; i++) {
127			if (fork() == 0)
128				test();
129		}
130		for (i = 0; i < 2; i++)
131			wait(NULL);
132
133		system("rm -rf src > /dev/null 2>&1");
134	}
135
136        return (0);
137}
138