xref: /freebsd/tools/test/stress2/misc/tmpfs10.sh (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 EMC Corp.
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# tmpfs(5) name lookup problem seen:
30
31# $ ./tmpfs10.sh
32# tmpfs10: unlink(p01193.14729) at loop #2: No such file or directory
33# unlink(p01193.14729) succeeded at call #2.
34# tmpfs10: unlink(p01186.14409) at loop #2: No such file or directory
35# unlink(p01186.14409) succeeded at call #2.
36# FAIL
37# $
38
39# Fixed in r253967.
40
41[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
42
43. ../default.cfg
44
45odir=`pwd`
46cd /tmp
47sed '1,/^EOF/d' < $odir/$0 > tmpfs10.c
48mycc -o tmpfs10 -Wall -Wextra -O2 -g tmpfs10.c || exit 1
49rm -f tmpfs10.c
50cd $odir
51
52mount | grep -q "on $mntpoint " && umount $mntpoint
53mount -t tmpfs tmpfs $mntpoint
54cd $mntpoint
55/tmp/tmpfs10 ||
56{ find $mntpoint ! -type d | xargs ls -il; echo FAIL; }
57cd $odir
58umount $mntpoint
59rm -f /tmp/tmpfs10
60exit 0
61EOF
62#include <err.h>
63#include <errno.h>
64#include <fcntl.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <sys/param.h>
68#include <sys/stat.h>
69#include <sys/wait.h>
70#include <unistd.h>
71
72static int loop;
73int error;
74
75#define PARALLEL 20
76#define SIZE 16000
77
78void
79test2(void)
80{
81	int i, j, k;
82	pid_t pid;
83	char file[128], dir[128];
84
85	loop++;
86	pid = getpid();
87	sprintf(dir,"%s.%05d", getprogname(), pid);
88	if (mkdir(dir, 0770) < 0)
89		err(1, "mkdir(%s), %s:%d", dir, __FILE__, __LINE__);
90
91	if (chdir(dir) == -1)
92		err(1, "chdir(%s), %s:%d", dir, __FILE__, __LINE__);
93
94	for (j = 0; j < SIZE; j++) {
95		sprintf(file,"p%05d.%05d", pid, j);
96		if (symlink("/tmp/not/there", file) == -1) {
97			if (errno != EINTR) {
98				warn("symlink(%s). %s.%d", file, __FILE__, __LINE__);
99				break;
100			}
101		}
102	}
103
104	for (i = --j; i >= 0; i--) {
105		sprintf(file,"p%05d.%05d", pid, i);
106		if (unlink(file) == -1) {
107			warn("unlink(%s) at loop #%d", file, loop);
108			error++;
109			for (k = 0; k < 10; k++) {
110				usleep(10000);
111				if (unlink(file) == 0) {
112					fprintf(stderr,
113					    "unlink(%s) succeeded at call #%d.\n",
114					    file, k + 2);
115					break;
116				}
117			}
118		}
119	}
120
121	(void)chdir("..");
122	if (rmdir(dir) == -1)
123		warn("rmdir(%s), %s:%d", dir, __FILE__, __LINE__);
124
125}
126
127void
128test(void)
129{
130	sleep(arc4random() % 3 + 1);
131
132	test2();
133	if (error == 0)
134		test2();
135
136	_exit(error);
137}
138
139int
140main(void)
141{
142	int e, i, status;
143
144	for (i = 0; i < PARALLEL; i++)
145		if (fork() == 0)
146			test();
147
148	e = 0;
149	for (i = 0; i < PARALLEL; i++) {
150		wait(&status);
151		e += WEXITSTATUS(status);
152	}
153
154	return (e);
155}
156