xref: /freebsd/tools/test/stress2/misc/tmpfs8.sh (revision 7937bfbc0ca53fe7cdd0d54414f9296e273a518e)
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# Demonstrate rename(2) cache problem for tmpfs(4). Fixed in r226987.
32# Variation of rename6.sh
33
34. ../default.cfg
35
36here=`pwd`
37cd /tmp
38sed '1,/^EOF/d' < $here/$0 > tmpfs8.c
39mycc -o tmpfs8 -Wall -Wextra -O2 tmpfs8.c
40rm -f tmpfs8.c
41cd $here
42
43mount | grep $mntpoint | grep -q tmpfs && umount -f $mntpoint
44mount -t tmpfs tmpfs $mntpoint
45chmod 777 $mntpoint
46
47su $testuser -c "cd $mntpoint; /tmp/tmpfs8"
48
49while mount | grep $mntpoint | grep -q tmpfs; do
50	umount $mntpoint || sleep 1
51done
52rm -f /tmp/tmpfs8
53exit
54EOF
55#include <err.h>
56#include <errno.h>
57#include <fcntl.h>
58#include <sched.h>
59#include <signal.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <strings.h>
63#include <sys/param.h>
64#include <sys/stat.h>
65#include <sys/time.h>
66#include <sys/wait.h>
67#include <time.h>
68#include <unistd.h>
69
70pid_t spid;
71const char *logfile = "test.log";
72char new[128];
73
74void
75cleanup()
76{
77	kill(spid, SIGINT);
78}
79
80static void
81Stat()
82{
83	struct stat sb;
84	int i;
85
86	setproctitle("Stat");
87	for (;;) {
88		for (i = 0; i < 1000; i++) {
89			stat(logfile, &sb);
90			stat(new, &sb);
91		}
92//		usleep(1000);
93		usleep(100);
94	}
95}
96
97int
98main(void)
99{
100	struct stat sb1, sb2, sb3;
101	int fd, i, r1, r2, r3;
102
103	if ((spid = fork()) == 0)
104		Stat();
105
106	setproctitle("main");
107	atexit(cleanup);
108	for (i = 0; i < 200000; i++) {
109		bzero(&sb1, sizeof(sb1));
110		bzero(&sb2, sizeof(sb2));
111		if ((fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
112			err(1, "creat(%s)", logfile);
113		close(fd);
114
115		sprintf(new, "test.log.%05d", i);
116		if ((fd = open(new, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
117			err(1, "creat(%s)", new);
118		write(fd, "xxx", 3);
119		close(fd);
120		if ((r3 = stat(new, &sb3)) == -1)
121			err(1, "stat(%s)", new);
122#if 1
123		if (rename(logfile, new) == -1)
124			warn("rename(%s, %s)", logfile, new);
125#else
126		/* No cache problem is seen */
127		if (link(logfile, new) == -1)
128			err(1, "link(%s, %s)", logfile, new);
129		if (unlink(logfile) == -1)
130			err(1, "unlink(%s)", logfile);
131#endif
132		/*
133		 * stat() for logfile and new will be identical sometimes,
134		 * but only when Stat() is running.
135		 */
136		r1 = stat(logfile, &sb1);
137		r2 = stat(new, &sb2);
138		if (r1 == 0 && r2 == 0 &&
139		    bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
140			fprintf(stderr, "FAIL 1\n");
141			fprintf(stderr, "%-15s: ino = %4ju, nlink = %ju, "
142			    "size  = %jd\n", logfile, (uintmax_t)sb1.st_ino,
143			   (uintmax_t)sb1.st_nlink, sb1.st_blocks);
144			fprintf(stderr, "%-15s: ino = %4ju, nlink = %ju, "
145			    "size = %jd\n", new    , (uintmax_t)sb2.st_ino,
146			    (uintmax_t)sb2.st_nlink, sb2.st_blocks);
147		}
148		if (bcmp(&sb2, &sb3, sizeof(sb2)) == 0) {
149			fprintf(stderr, "Old to file is lingering\n");
150		}
151		if (sb2.st_ino == sb3.st_ino) {
152			fprintf(stderr, "FAIL 2\n");
153			if (r1 == 0)
154				fprintf(stderr,
155				    "sb1: %-15s: ino = %4ju, nlink = %ju, "
156				    "size = %jd\n", logfile,
157				    (uintmax_t)sb1.st_ino,
158				    (uintmax_t)sb1.st_nlink, sb1.st_blocks);
159			if (r2 == 0)
160				fprintf(stderr,
161				    "sb2: %-15s: ino = %4ju, nlink = %ju, "
162				    "size = %jd\n", new, (uintmax_t)sb2.st_ino,
163				    (uintmax_t)sb2.st_nlink, sb2.st_blocks);
164			if (r3 == 0)
165				fprintf(stderr,
166				    "sb3: %-15s: ino = %4ju, nlink = %ju, "
167				    "size = %jd\n", new, (uintmax_t)sb3.st_ino,
168				    (uintmax_t)sb3.st_nlink, sb3.st_blocks);
169			exit(1);
170		}
171		unlink(new);
172	}
173
174	kill(spid, SIGINT);
175	wait(NULL);
176
177	return (0);
178}
179