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, where the original name lingers in the VFS cache. 32 33# Original test scenario by Anton Yuzhaninov <citrin citrin ru> 34 35. ../default.cfg 36 37here=`pwd` 38cd /tmp 39sed '1,/^EOF/d' < $here/$0 > rename6.c 40mycc -o rename6 -Wall -Wextra -O2 rename6.c 41rm -f rename6.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; /tmp/rename6" 52 53while mount | grep -q md$mdstart; do 54 umount $mntpoint || sleep 1 55done 56mdconfig -d -u $mdstart 57rm -f /tmp/rename6 58exit 59EOF 60#include <err.h> 61#include <errno.h> 62#include <fcntl.h> 63#include <sched.h> 64#include <signal.h> 65#include <stdio.h> 66#include <stdlib.h> 67#include <strings.h> 68#include <sys/param.h> 69#include <sys/stat.h> 70#include <sys/time.h> 71#include <sys/wait.h> 72#include <time.h> 73#include <unistd.h> 74 75pid_t spid; 76const char *logfile = "test.log"; 77 78void 79cleanup() 80{ 81 if (kill(spid, SIGINT) == -1 && errno != ESRCH) 82 err(1, "kill(%d)", spid); 83} 84 85static void 86Stat() 87{ 88 struct stat sb; 89 int i; 90 91 setproctitle("Stat"); 92 for (;;) { 93 for (i = 0; i < 1000; i++) { 94 stat(logfile, &sb); 95 } 96 usleep(1000); 97 } 98} 99 100int 101main(void) 102{ 103 struct stat sb1, sb2; 104 int fd, i; 105 char new[128]; 106 107 if ((spid = fork()) == 0) 108 Stat(); 109 110 setproctitle("main"); 111 atexit(cleanup); 112 for (i = 0; i < 20000; i++) { 113 sprintf(new, "test.log.%05d", i); 114 if ((fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) 115 err(1, "creat(%s)", logfile); 116 close(fd); 117#if 1 118 if (rename(logfile, new) == -1) 119 warn("rename(%s, %s)", logfile, new); 120#else 121 /* No cache problem is seen */ 122 if (link(logfile, new) == -1) 123 err(1, "link(%s, %s)", logfile, new); 124 if (unlink(logfile) == -1) 125 err(1, "unlink(%s)", logfile); 126#endif 127 /* 128 * stat() for logfile and new will be identical sometimes, 129 * but only when Stat() is running. 130 */ 131 if (stat(logfile, &sb1) == 0 && stat(new, &sb2) == 0 && 132 bcmp(&sb1, &sb2, sizeof(sb1)) == 0) { 133 fprintf(stderr, "At loop #%d\n", i); 134 fprintf(stderr, "%-15s: ino = %ju, nlink = %ju," 135 " size = %jd\n", logfile, (uintmax_t)sb1.st_ino, 136 (uintmax_t)sb1.st_nlink, sb1.st_blocks); 137 fprintf(stderr, "%-15s: ino = %ju, nlink = %ju, " 138 "size = %jd\n", new , (uintmax_t)sb2.st_ino, 139 (uintmax_t)sb2.st_nlink, sb2.st_blocks); 140 } 141 unlink(new); 142 } 143 144 kill(spid, SIGINT); 145 wait(NULL); 146 147 return (0); 148} 149