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