xref: /freebsd/tools/test/stress2/misc/fdatasync.sh (revision 1d386b48)
1#!/bin/sh
2
3#
4# Copyright (c) 2018 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# fdatasync(2) fuzz.
30
31# Deadlock seen:
32# https://people.freebsd.org/~pho/stress/log/fdatasync.txt
33
34[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
35
36. ../default.cfg
37
38dir=$RUNDIR
39nfiles=500
40[ `df -i $dir | tail -1 | awk '{print $7}'` -lt $nfiles ] && exit 0
41
42odir=`pwd`
43cd /tmp
44sed '1,/^EOF/d' < $odir/$0 > fdatasync.c
45rm -f /tmp/fdatasync
46mycc -o fdatasync -Wall -Wextra -O2 -g fdatasync.c -lpthread || exit 1
47rm -f fdatasync.c
48
49mkdir -p $dir && chmod 777 $dir
50
51cd $dir
52jot $nfiles | xargs touch
53jot $nfiles | xargs chmod 666
54cd $odir
55
56(cd /tmp; /tmp/fdatasync $dir)
57e=$?
58
59rm -rf $dir/* /tmp/fdatasync
60exit $e
61EOF
62#include <sys/param.h>
63#include <sys/resource.h>
64
65#include <err.h>
66#include <errno.h>
67#include <fcntl.h>
68#include <fts.h>
69#include <libutil.h>
70#include <pthread.h>
71#include <pwd.h>
72#include <stdint.h>
73#include <stdio.h>
74#include <stdlib.h>
75#include <string.h>
76#include <unistd.h>
77
78#define N (128 * 1024 / (int)sizeof(u_int32_t))
79#define RUNTIME 180
80#define THREADS 2
81
82static int fd[900];
83static u_int32_t r[N];
84static char *args[2];
85
86static unsigned long
87makearg(void)
88{
89	unsigned long val;
90
91	val = arc4random();
92#if defined(__LP64__)
93	val = (val << 32) | arc4random();
94	val = val & 0x00007fffffffffffUL;
95#endif
96
97	return(val);
98}
99
100static void *
101test(void *arg __unused)
102{
103	FTS *fts;
104	FTSENT *p;
105	int ftsoptions, i, n;
106
107	ftsoptions = FTS_PHYSICAL;
108
109	for (;;) {
110		for (i = 0; i < N; i++)
111			r[i] = arc4random();
112		if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
113			err(1, "fts_open");
114
115		i = n = 0;
116		while ((p = fts_read(fts)) != NULL) {
117			if (fd[i] > 0)
118				close(fd[i]);
119			if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
120				if ((fd[i] = open(p->fts_path, O_WRONLY)) ==
121				    -1)
122				continue;
123			if (ftruncate(fd[i], 0) != 0)
124				err(1, "ftruncate");
125			i++;
126			i = i % nitems(fd);
127		}
128
129		if (fts_close(fts) == -1)
130			err(1, "fts_close()");
131		sleep(1);
132	}
133	return(0);
134}
135
136static void *
137calls(void *arg __unused)
138{
139	off_t offset;
140	time_t start;
141	int fd2;
142
143	start = time(NULL);
144	while ((time(NULL) - start) < RUNTIME) {
145		fd2 = makearg() % nitems(fd) + 3;
146		offset = makearg();
147		if (lseek(fd2, offset - 1, SEEK_SET) != -1) {
148			if (write(fd2, "x", 1) != 1)
149				if (errno != EBADF && errno != ENOSPC &&
150				    errno != E2BIG && errno != ESTALE &&
151				    errno != EFBIG)
152					warn("write");
153		} else
154			if (errno != EBADF)
155				warn("lseek");
156		if (fdatasync(fd2) == -1)
157			if (errno != EBADF)
158				warn("x");
159
160	}
161
162	return (0);
163}
164
165int
166main(int argc, char **argv)
167{
168	struct passwd *pw;
169	pthread_t rp, cp[THREADS];
170	int e, i;
171
172	if (argc != 2) {
173		fprintf(stderr, "Usage: %s <dir>\n", argv[0]);
174		exit(1);
175	}
176	args[0] = argv[1];
177	args[1] = 0;
178
179	if ((pw = getpwnam("nobody")) == NULL)
180		err(1, "failed to resolve nobody");
181	if (setgroups(1, &pw->pw_gid) ||
182	    setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
183	    seteuid(pw->pw_uid) || setuid(pw->pw_uid))
184		err(1, "Can't drop privileges to \"nobody\"");
185	endpwent();
186
187	if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
188		errc(1, e, "pthread_create");
189	usleep(1000);
190	for (i = 0; i < THREADS; i++)
191		if ((e = pthread_create(&cp[i], NULL, calls, NULL)) != 0)
192			errc(1, e, "pthread_create");
193	for (i = 0; i < THREADS; i++)
194		pthread_join(cp[i], NULL);
195
196	return (0);
197}
198