4 # Copyright (c) 2011 Peter Holm <pho@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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
29 [ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
31 # Demonstrate rename(2) cache problem, where the original name lingers in the VFS cache.
33 # Original test scenario by Anton Yuzhaninov <citrin citrin ru>
39 sed '1,/^EOF/d' < $here/$0 > rename6.c
40 mycc -o rename6 -Wall -Wextra -O2 rename6.c
44 mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
45 mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart
46 mdconfig -a -t swap -s 2g -u $mdstart
47 bsdlabel -w md$mdstart auto
48 newfs $newfs_flags md${mdstart}$part > /dev/null
49 mount /dev/md${mdstart}$part $mntpoint
52 su $testuser -c "cd $mntpoint; /tmp/rename6"
54 while mount | grep -q md${mdstart}$part; do
55 umount $mntpoint || sleep 1
57 mdconfig -d -u $mdstart
69 #include <sys/param.h>
77 const char *logfile = "test.log";
82 if (kill(spid, SIGINT) == -1 && errno != ESRCH)
83 err(1, "kill(%d)", spid);
94 for (i = 0; i < 1000; i++) {
104 struct stat sb1, sb2;
108 if ((spid = fork()) == 0)
111 setproctitle("main");
113 for (i = 0; i < 20000; i++) {
114 sprintf(new, "test.log.%05d", i);
115 if ((fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
116 err(1, "creat(%s)", logfile);
119 if (rename(logfile, new) == -1)
120 warn("rename(%s, %s)", logfile, new);
122 /* No cache problem is seen */
123 if (link(logfile, new) == -1)
124 err(1, "link(%s, %s)", logfile, new);
125 if (unlink(logfile) == -1)
126 err(1, "unlink(%s)", logfile);
129 * stat() for logfile and new will be identical sometimes,
130 * but only when Stat() is running.
132 if (stat(logfile, &sb1) == 0 && stat(new, &sb2) == 0 &&
133 bcmp(&sb1, &sb2, sizeof(sb1)) == 0) {
134 fprintf(stderr, "At loop #%d\n", i);
135 fprintf(stderr, "%-15s: ino = %ju, nlink = %ju,"
136 " size = %jd\n", logfile, (uintmax_t)sb1.st_ino,
137 (uintmax_t)sb1.st_nlink, sb1.st_blocks);
138 fprintf(stderr, "%-15s: ino = %ju, nlink = %ju, "
139 "size = %jd\n", new , (uintmax_t)sb2.st_ino,
140 (uintmax_t)sb2.st_nlink, sb2.st_blocks);