aboutsummaryrefslogtreecommitdiff
path: root/tools/test/stress2/misc/rename6.sh
blob: 6c27c5601d2aa8337dafe8b8348bff860e91fa11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/sh

#
# Copyright (c) 2011 Peter Holm <pho@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1

# Demonstrate rename(2) cache problem, where the original name lingers in the VFS cache.

# Original test scenario by Anton Yuzhaninov <citrin citrin ru>

. ../default.cfg

here=`pwd`
cd /tmp
sed '1,/^EOF/d' < $here/$0 > rename6.c
mycc -o rename6 -Wall -Wextra -O2 rename6.c
rm -f rename6.c
cd $here

mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
mdconfig -a -t swap -s 2g -u $mdstart
bsdlabel -w md$mdstart auto
newfs $newfs_flags md${mdstart}$part > /dev/null
mount /dev/md${mdstart}$part $mntpoint
chmod 777 $mntpoint

su $testuser -c "cd $mntpoint; /tmp/rename6"

while mount | grep -q md${mdstart}$part; do
	umount $mntpoint || sleep 1
done
mdconfig -d -u $mdstart
rm -f /tmp/rename6
exit
EOF
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

pid_t spid;
const char *logfile = "test.log";

void
cleanup()
{
	if (kill(spid, SIGINT) == -1 && errno != ESRCH)
		err(1, "kill(%d)", spid);
}

static void
Stat()
{
	struct stat sb;
	int i;

	setproctitle("Stat");
	for (;;) {
		for (i = 0; i < 1000; i++) {
			stat(logfile, &sb);
		}
		usleep(1000);
	}
}

int
main(void)
{
	struct stat sb1, sb2;
	int fd, i;
	char new[128];

	if ((spid = fork()) == 0)
		Stat();

	setproctitle("main");
	atexit(cleanup);
	for (i = 0; i < 20000; i++) {
		sprintf(new, "test.log.%05d", i);
		if ((fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
			err(1, "creat(%s)", logfile);
		close(fd);
#if 1
		if (rename(logfile, new) == -1)
			warn("rename(%s, %s)", logfile, new);
#else
		/* No cache problem is seen */
		if (link(logfile, new) == -1)
			err(1, "link(%s, %s)", logfile, new);
		if (unlink(logfile) == -1)
			err(1, "unlink(%s)", logfile);
#endif
		/*
		 * stat() for logfile and new will be identical sometimes,
		 * but only when Stat() is running.
		 */
		if (stat(logfile, &sb1) == 0 && stat(new, &sb2) == 0 &&
		    bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
			fprintf(stderr, "At loop #%d\n", i);
			fprintf(stderr, "%-15s: ino = %ju, nlink = %ju,"
			   " size = %jd\n", logfile, (uintmax_t)sb1.st_ino,
			   (uintmax_t)sb1.st_nlink, sb1.st_blocks);
			fprintf(stderr, "%-15s: ino = %ju, nlink = %ju, "
			    "size = %jd\n", new    , (uintmax_t)sb2.st_ino,
			    (uintmax_t)sb2.st_nlink, sb2.st_blocks);
		}
		unlink(new);
	}

	kill(spid, SIGINT);
	wait(NULL);

	return (0);
}