xref: /freebsd/tools/test/stress2/misc/rename9.sh (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1#!/bin/sh
2
3#
4# Copyright (c) 2012 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# Variation of rename6.sh. Cache problem of "to" file name seen.
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > rename9.c
38mycc -o rename9 -Wall -Wextra -O2 rename9.c
39rm -f rename9.c
40cd $here
41
42mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
43mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
44mdconfig -a -t swap -s 2g -u $mdstart
45newfs $newfs_flags md$mdstart > /dev/null
46mount /dev/md$mdstart $mntpoint
47rm -rf $mntpoint/.snap
48chmod 777 $mntpoint
49
50(while true; do ls -lRi $mntpoint > /dev/null 2>&1; done) &
51su $testuser -c "cd $mntpoint; /tmp/rename9"
52kill $! > /dev/null 2>&1
53wait
54ls -ilR $mntpoint | egrep -v "^total "
55
56while mount | grep -q md$mdstart; do
57	umount $mntpoint || sleep 1
58done
59mdconfig -d -u $mdstart
60rm -f /tmp/rename9
61exit
62EOF
63#include <err.h>
64#include <errno.h>
65#include <fcntl.h>
66#include <sched.h>
67#include <signal.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <strings.h>
71#include <sys/param.h>
72#include <sys/stat.h>
73#include <sys/time.h>
74#include <sys/wait.h>
75#include <time.h>
76#include <unistd.h>
77
78pid_t spid;
79char *fromFile = "fromFile.log";
80char toFile[128];
81
82void
83cleanup()
84{
85	kill(spid, SIGINT);
86}
87
88static void
89statFrom()
90{
91	struct stat sb;
92
93	setproctitle("Stat");
94	for (;;) {
95		stat(fromFile, &sb);
96	}
97}
98
99int
100main(void)
101{
102	struct stat fb, tb, fa, ta;
103	int fd, i;
104
105	if ((spid = fork()) == 0)
106		statFrom();
107
108	setproctitle("main");
109	atexit(cleanup);
110	for (i = 0;i < 100000; i++) {
111		bzero(&fb, sizeof(fb));
112		bzero(&tb, sizeof(tb));
113		bzero(&fa, sizeof(fa));
114		bzero(&ta, sizeof(ta));
115
116		if ((fd = open(fromFile, O_RDWR | O_CREAT | O_TRUNC, 0644))
117		    == -1)
118			err(1, "creat(%s)", fromFile);
119		close(fd);
120
121		sprintf(toFile, "toFile.log.%05d", i);
122		if ((fd = open(toFile, O_RDWR | O_CREAT | O_TRUNC, 0644))
123		    == -1)
124			err(1, "creat(%s)", toFile);
125		write(fd, "xxx", 3);
126		close(fd);
127
128		stat(fromFile, &fb);
129		stat(toFile, &tb);
130		if (rename(fromFile, toFile) == -1)
131			warn("rename(%s, %s)", fromFile, toFile);
132		stat(fromFile, &fa);
133		if (stat(toFile, &ta) == -1)
134			err(1, "stat(%s)", toFile);
135
136		if (tb.st_ino == ta.st_ino) {
137			fprintf(stderr, "FAIL: old and new \"To\" inode "
138			    "number is identical\n");
139			fprintf(stderr, "stat() before the rename():\n");
140			fprintf(stderr,
141			    "%-16s: ino = %4ju, nlink = %ju, size = %jd\n",
142			    fromFile, (uintmax_t)fb.st_ino, (uintmax_t)fb.st_nlink,
143			    fb.st_blocks);
144			fprintf(stderr,
145			    "%-16s: ino = %4ju, nlink = %ju, size = %jd\n",
146			    toFile, (uintmax_t)tb.st_ino, (uintmax_t)tb.st_nlink,
147			    tb.st_blocks);
148			fprintf(stderr, "\nstat() after the rename():\n");
149			if (fa.st_ino != 0)
150				fprintf(stderr,
151				    "%-16s: ino = %4ju, nlink = %ju, size = "
152				    "%jd\n", fromFile, (uintmax_t)fa.st_ino,
153				    (uintmax_t)fa.st_nlink, fa.st_blocks);
154			fprintf(stderr,
155			    "%-16s: ino = %4ju, nlink = %ju, size = %jd\n",
156			    toFile, (uintmax_t)ta.st_ino, (uintmax_t)ta.st_nlink,
157			    ta.st_blocks);
158			kill(spid, SIGINT);
159			exit(1);
160		}
161		unlink(toFile);
162	}
163
164	kill(spid, SIGINT);
165	wait(NULL);
166
167	return (0);
168}
169