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