xref: /freebsd/tools/test/stress2/misc/tmpfs17.sh (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
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(4) option nonc test scenario
30
31. ../default.cfg
32[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33
34dir=/tmp
35odir=`pwd`
36cd $dir
37sed '1,/^EOF/d' < $odir/$0 > $dir/tmpfs17.c
38mycc -o tmpfs17 -Wall -Wextra -O0 -g tmpfs17.c || exit 1
39rm -f tmpfs17.c
40cd $odir
41
42mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
43mount -o nonc -t tmpfs tmpfs $mntpoint || { rm /tmp/tmpfs17; exit 0; }
44
45cd $mntpoint
46/tmp/tmpfs17 &
47pid=$!
48sleep .2
49pid2=`pgrep tmpfs17`
50for i in `jot 5`; do
51	for p in $pid2; do
52		while true; do procstat -f $p > /dev/null 2>&1; done &
53		pids="$pids $!"
54	done
55done
56while kill -0 $pid 2>/dev/null; do
57	sleep 2
58done
59kill $pids 2>/dev/null
60wait
61cd $odir
62
63for i in `jot 10`; do
64	umount $mntpoint && break
65	sleep 1
66done
67s=0
68mount | grep -q "on $mntpoint " && { s=1; umount -f $mntpoint; }
69rm -rf /tmp/tmpfs17
70exit $s
71
72EOF
73#include <sys/param.h>
74#include <sys/mman.h>
75#include <sys/stat.h>
76#include <sys/wait.h>
77
78#include <machine/atomic.h>
79
80#include <err.h>
81#include <errno.h>
82#include <fcntl.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <time.h>
86#include <unistd.h>
87
88static volatile u_int *share;
89
90#define PARALLEL 4
91#define RUNTIME (5 * 60)
92#define SYNC 0
93
94static void
95test(void)
96{
97	pid_t pid;
98	time_t start;
99	int fd, i;
100	char file[80];
101
102	atomic_add_int(&share[SYNC], 1);
103	while (share[SYNC] != PARALLEL)
104		;
105
106	i= 0;
107	pid = getpid();
108	start = time(NULL);
109	while ((time(NULL) - start) < RUNTIME) {
110		snprintf(file, sizeof(file), "file.%06d.%03d", pid, i++);
111		if ((fd = open(file, O_RDWR|O_CREAT, DEFFILEMODE)) == -1)
112			err(1, "open(%s)", file);
113		close(fd);
114		if (unlink(file) == -1)
115			err(1, "unlink(%s)", file);
116	}
117
118	_exit(0);
119}
120
121int
122main(void)
123{
124	size_t len;
125	int e, i, pids[PARALLEL], status;
126
127	e = 0;
128	len = PAGE_SIZE;
129	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
130	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
131		err(1, "mmap");
132
133	for (i = 0; i < PARALLEL; i++) {
134		if ((pids[i] = fork()) == 0)
135			test();
136	}
137	for (i = 0; i < PARALLEL; i++) {
138		if (waitpid(pids[i], &status, 0) == -1)
139			err(1, "waitpid(%d)", pids[i]);
140		e += status == 0 ? 0 : 1;
141	}
142
143	return (e);
144}
145