xref: /freebsd/tools/test/stress2/misc/rename8.sh (revision 61e21613)
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[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
30
31# Cache inconsistency seen on "to" file for rename(2).
32
33# Scenario by jhb@
34
35. ../default.cfg
36
37here=`pwd`
38cd /tmp
39sed '1,/^EOF/d' < $here/$0 > rename8.c
40mycc -o rename8 -Wall -Wextra -O2 rename8.c
41rm -f rename8.c
42cd $here
43
44mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
45mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
46mdconfig -a -t swap -s 2g -u $mdstart
47newfs $newfs_flags md$mdstart > /dev/null
48mount /dev/md$mdstart $mntpoint
49chmod 777 $mntpoint
50
51su $testuser -c "cd $mntpoint; mkdir r; /tmp/rename8 r"
52ls -li $mntpoint/r | egrep -v "^total"
53
54for i in `jot 10`; do
55        mount | grep -q md$mdstart  && \
56                umount $mntpoint && mdconfig -d -u $mdstart && break
57	sleep 1
58done
59if mount | grep -q md$mdstart; then
60	fuser $mntpoint
61        echo "umount $mntpoint failed"
62        exit 1
63fi
64
65mdconfig -l | grep -q md$mdstart &&
66	mdconfig -d -u $mdstart
67rm -f /tmp/rename8
68exit
69EOF
70#include <sys/types.h>
71#include <sys/stat.h>
72#include <err.h>
73#include <errno.h>
74#include <fcntl.h>
75#include <signal.h>
76#include <stdio.h>
77#include <stdlib.h>
78#include <sys/wait.h>
79#include <unistd.h>
80
81static char *always, *file1, *file2;
82static ino_t always_ino;
83
84static void
85usage(void)
86{
87	fprintf(stderr, "Usage: rename_race <dir>\n");
88	exit(1);
89}
90
91static void
92child(void)
93{
94	struct stat sb;
95
96	/* Exit as soon as our parent exits. */
97	while (getppid() != 1) {
98		stat(file1, &sb);
99	}
100	exit(0);
101}
102
103static void
104create_file(const char *path)
105{
106	int fd;
107
108	fd = open(path, O_CREAT, 0666);
109	if (fd < 0)
110		err(1, "open(%s)", path);
111	close(fd);
112}
113
114int
115main(int ac, char **av)
116{
117	struct stat sb, sb2;
118	pid_t pid;
119	int i, r;
120
121	if (ac != 2)
122		usage();
123	if (stat(av[1], &sb) != 0)
124		err(1, "stat(%s)", av[1]);
125	if (!S_ISDIR(sb.st_mode))
126		errx(1, "%s not a directory", av[1]);
127
128	asprintf(&always, "%s/file.always", av[1]);
129	asprintf(&file1, "%s/file1", av[1]);
130	asprintf(&file2, "%s/file2", av[1]);
131
132	create_file(always);
133	if (stat(always, &sb) != 0)
134		err(1, "stat(%s)", always);
135	always_ino = sb.st_ino;
136
137	pid = fork();
138	if (pid < 0)
139		err(1, "fork");
140	if (pid == 0)
141		child();
142	r = 0;
143	for (i = 0; i < 100000; i++) {
144		if (unlink(file1) < 0 && errno != ENOENT)
145			err(1, "unlink(%s)", file1);
146		if (link(always, file1) < 0)
147			err(1, "link(%s, %s)", always, file1);
148		create_file(file2);
149		if (stat(file2, &sb2) < 0)
150			err(1, "stat(%s)", file2);
151		if (rename(file2, file1) < 0)
152			err(1, "rename(%s, %s)", file2, file1);
153		if (stat(file1, &sb) < 0)
154			err(1, "stat(%s)", file1);
155		if (sb.st_ino != sb2.st_ino ||
156		    sb.st_ino == always_ino) {
157			printf("FAIL. Bad stat: always: %ju file1: %ju (should be %ju)\n",
158			    (uintmax_t)always_ino, (uintmax_t)sb.st_ino,
159			    (uintmax_t)sb2.st_ino);
160			r = EXIT_FAILURE;
161			break;
162		}
163	}
164	kill(pid, SIGINT);
165	wait(NULL);
166	if (r == 0) {
167		unlink(always);
168		unlink(file1);
169		unlink(file2);
170	}
171	return (r);
172}
173