xref: /freebsd/tools/test/stress2/misc/ffs_sync.sh (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 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[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
30
31# "ffs_fsync: dirty" seen:
32# http://people.freebsd.org/~pho/stress/log/ffs_sync.txt
33
34. ../default.cfg
35
36here=`pwd`
37cd /tmp
38sed '1,/^EOF/d' < $here/$0 > ffs_sync.c
39mycc -o ffs_sync -Wall -Wextra ffs_sync.c || exit 1
40rm -f ffs_sync.c
41cd $here
42
43mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
44mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
45
46mdconfig -a -t swap -s 4g -u $mdstart || exit 1
47bsdlabel -w md$mdstart auto
48newfs $newfs_flags md${mdstart}$part > /dev/null
49mount /dev/md${mdstart}$part $mntpoint
50chmod 777 $mntpoint
51
52for i in `jot 3`; do
53	su $testuser -c "cd $mntpoint; /tmp/ffs_sync" &
54	sleep 60
55	killall -q ffs_sync
56	killall -q ffs_sync
57done
58
59while mount | grep $mntpoint | grep -q /dev/md; do
60	umount $mntpoint || sleep 1
61done
62rm -f /tmp/ffs_sync
63mdconfig -d -u $mdstart
64exit
65EOF
66#include <sys/param.h>
67#include <err.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <fts.h>
71#include <libutil.h>
72#include <stdint.h>
73#include <stdio.h>
74#include <stdlib.h>
75#include <string.h>
76#include <sys/stat.h>
77#include <sys/wait.h>
78#include <unistd.h>
79
80#define PARALLEL    12
81#define NRW       1000
82#define NFTS     10000
83#define NMKDIR    6000
84#define NSYMLINK 20000
85
86void
87slinktest(void)
88{
89        int i, j;
90        pid_t pid;
91        char file[128];
92
93	setproctitle("slink");
94        pid = getpid();
95        for (j = 0; j < NSYMLINK; j++) {
96                sprintf(file,"p%05d.%05d", pid, j);
97                if (symlink("/tmp/not/there", file) == -1) {
98                        if (errno != EINTR)
99                                warn("symlink(%s). %s.%d", file, __FILE__, __LINE__);
100                }
101        }
102
103        for (i = --j; i >= 0; i--) {
104                sprintf(file,"p%05d.%05d", pid, i);
105                if (unlink(file) == -1)
106                        err(3, "unlink(%s)", file);
107        }
108
109	_exit(0);
110}
111
112void
113mktest(void)
114{
115	int i;
116	char path[80];
117
118	setproctitle("mkdir");
119	sprintf(path, "d%06d", getpid());
120	if (mkdir(path, 0770) == -1)
121		err(1, "mkdir(%s)", path);
122	chdir(path);
123
124	sprintf(path, "d");
125	for (i = 0; i < NMKDIR; i++) {
126		if (mkdir(path, 0770) == -1) {
127			warn("mkdir(%s),  %s:%d", path, __FILE__, __LINE__);
128		} else
129			chdir(path);
130
131	}
132	for (i = 0; i < NMKDIR; i++) {
133		chdir("..");
134		rmdir(path);
135	}
136	chdir("..");
137
138	_exit(0);
139}
140
141void
142rwtest(void)
143{
144	int fd, i, j;
145	char buf[80], file[80];
146
147	setproctitle("rw");
148	for (i = 0; i < NRW; i++) {
149		sprintf(file, "f%06d.%06d", getpid(), i);
150		if ((fd = open(file, O_CREAT | O_EXCL, 0644)) == -1)
151			err(1, "open(%s)", file);
152		for (j = 0; j < 1024; j++)
153			write(fd, buf, sizeof(buf));
154		lseek(fd, 0, SEEK_SET);
155		for (j = 0; j < 1024; j++)
156			read(fd, buf, sizeof(buf));
157		close(fd);
158	}
159	for (i = 0; i < NRW; i++) {
160		sprintf(file, "f%06d.%06d", getpid(), i);
161		if (unlink(file) == -1)
162			warn("unlink(%s)", file);
163	}
164	_exit(0);
165}
166
167void
168slink(void)
169{
170	int i;
171
172	for (i = 0; i < PARALLEL; i++)
173		if (fork() == 0)
174			slinktest();
175}
176
177void
178mk(void)
179{
180	int i;
181
182	for (i = 0; i < PARALLEL; i++)
183		if (fork() == 0)
184			mktest();
185}
186
187void
188rw(void)
189{
190	int i;
191
192	for (i = 0; i < PARALLEL; i++)
193		if (fork() == 0)
194			rwtest();
195}
196
197void
198ftstest(void)
199{
200	FTS *fts;
201	FTSENT *p;
202	int ftsoptions, i;
203	char *args[2];
204
205	setproctitle("fts");
206	ftsoptions = FTS_PHYSICAL;
207	args[0] = ".";
208	args[1] = 0;
209
210	for (i = 0; i < NFTS; i++) {
211		if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
212			err(1, "fts_open");
213
214		while ((p = fts_read(fts)) != NULL)
215				;
216
217		if (errno != 0 && errno != ENOENT)
218			err(1, "fts_read");
219		if (fts_close(fts) == -1)
220			err(1, "fts_close()");
221	}
222
223	_exit(0);
224}
225
226void
227fts(void)
228{
229	int i;
230
231	for (i = 0; i < PARALLEL; i++)
232		if (fork() == 0)
233			ftstest();
234}
235
236int
237main(void)
238{
239	int i;
240
241	slink();
242	mk();
243	rw();
244	fts();
245
246	for (i = 0; i < PARALLEL; i++) {
247		wait(NULL);
248		wait(NULL);
249		wait(NULL);
250		wait(NULL);
251	}
252
253	return (0);
254}
255