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