xref: /freebsd/tools/test/stress2/misc/rename11.sh (revision 608c97bff921c8936eb71f4b1a177625cfa60250)
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# SU problem with "mkdir(d.008740.000987): Too many links" seen.
30
31[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > rename11.c
38mycc -o rename11 -Wall -Wextra rename11.c || exit 1
39rm -f rename11.c
40
41mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
42mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
43
44mdconfig -a -t swap -s 2g -u $mdstart || exit 1
45
46[ $# -eq 1 ] && newfs_flags=$1	# Problem only seen with SU
47echo newfs $newfs_flags md$mdstart
48newfs $newfs_flags md$mdstart > /dev/null
49mount /dev/md$mdstart $mntpoint
50
51mkdir $mntpoint/dir
52(
53	cd $mntpoint/dir
54	/tmp/rename11 || echo FAIL
55)
56
57while mount | grep "on $mntpoint " | grep -q /dev/md; do
58	umount $mntpoint || sleep 1
59done
60
61checkfs /dev/md$mdstart; s=$?
62mdconfig -d -u $mdstart
63rm -rf /tmp/rename11
64exit $s
65EOF
66#include <sys/types.h>
67#include <sys/stat.h>
68#include <sys/wait.h>
69#include <err.h>
70#include <fcntl.h>
71#include <libgen.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#include <unistd.h>
76
77#define LOOPS 10
78#define PARALLEL 3
79#define ND 5000
80
81static void
82check(char *name)
83{
84	struct stat sb;
85
86	if (stat(name, &sb) == -1)
87		warn("stat(%s)", name);
88	else
89		warnx("stat(%s) succeeded!", name);
90}
91
92static void
93dirs(void)
94{
95	pid_t pid;
96	int i, j;
97	char name[80], to[80];
98
99	pid = getpid();
100
101	for (j = 0; j < LOOPS; j++) {
102		for (i = 0; i < ND; i++) {
103			snprintf(name, sizeof(name), "d.%06d.%06d", pid, i);
104			if (mkdir(name, 0700) == -1)
105				err(1, "mkdir(%s)", name);
106			snprintf(to  , sizeof(to  ), "d2.%06d.%06d", pid, i);
107			if (rename(name, to) == -1)
108				warn("rename(%s, %s)", name, to);
109			if (rename(to, name) == -1)
110				warn("rename(%s, %s)", to, name);
111			if (rmdir(name) == -1) {
112				check(name);
113				err(1, "rmdir(%s)", name);
114			}
115		}
116	}
117
118	_exit(0);
119}
120
121int
122main(void)
123{
124	int i, r, s;
125
126	r = 0;
127	for (i = 0; i < PARALLEL; i++) {
128		if (fork() == 0)
129			dirs();
130	}
131
132	for (i = 0; i < PARALLEL; i++) {
133		wait(&s);
134		r += WEXITSTATUS(s);
135	}
136
137	return (r);
138}
139