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