xref: /freebsd/tools/test/stress2/misc/rename5.sh (revision 1d386b48)
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
44
45newfs $newfs_flags md$mdstart > /dev/null
46mount /dev/md$mdstart $mntpoint
47
48here=`pwd`
49cd /tmp
50sed '1,/^EOF/d' < $here/$0 > rename5.c
51mycc -o rename5 -Wall -Wextra -O2 rename5.c
52rm -f rename5.c
53
54cd $mntpoint
55
56/tmp/rename5
57
58cd $here
59rm -f /tmp/rename5
60
61while mount | grep -q md$mdstart; do
62	umount $mntpoint || sleep 1
63done
64
65checkfs /dev/md$mdstart; s=$?
66
67mdconfig -d -u $mdstart
68exit $s
69EOF
70#include <sys/stat.h>
71#include <sys/wait.h>
72
73#include <err.h>
74#include <fcntl.h>
75#include <stdio.h>
76#include <stdlib.h>
77#include <time.h>
78#include <unistd.h>
79
80#define N 1000
81#define RUNTIME (5 * 60)
82
83void
84test(void)
85{
86	pid_t pid;
87	int i;
88	char from[128], to[128];
89
90	pid = getpid();
91	for (i = 0; i < N; i++) {
92		snprintf(from, sizeof(from), "src/dir.%06d", i);
93		snprintf(to  , sizeof(to),   "src/dir.%06d.%06d", i, pid);
94		(void)rename(from, to);
95	}
96	_exit(0);
97}
98
99int
100main()
101{
102	time_t start;
103	int fd, i;
104	char dir[128], file[128];
105
106	start = time(NULL);
107	while (time(NULL) - start < RUNTIME) {
108		if (mkdir("src", 0700) == -1)
109			err(1, "mkdir(src)");
110
111		for (i = 0; i < N; i++) {
112			snprintf(dir, sizeof(dir), "src/dir.%06d", i);
113			if (mkdir(dir, 0700) == -1)
114				err(1, "mkdir(%s)", dir);
115			snprintf(file, sizeof(file), "%s/meta", dir);
116			if ((fd = open(file, O_RDWR | O_CREAT, 0600)) < 0)
117				err(1, "open(%s)", file);
118			close(fd);
119			snprintf(file, sizeof(file), "%s/data", dir);
120			if ((fd = open(file, O_RDWR | O_CREAT, 0600)) < 0)
121				err(1, "open(%s)", file);
122			close(fd);
123		}
124
125		for (i = 0; i < 2; i++) {
126			if (fork() == 0)
127				test();
128		}
129		for (i = 0; i < 2; i++)
130			wait(NULL);
131
132		system("rm -rf src > /dev/null 2>&1");
133	}
134
135        return (0);
136}
137