xref: /freebsd/tools/test/stress2/misc/newfs4.sh (revision 963f5dc7a30624e95d72fb7f87b8892651164e46)
1#!/bin/sh
2
3#
4# Copyright (c) 2008-2013 Peter Holm <pho@FreeBSD.org>
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. ../default.cfg
32
33# Deadlock problems. Test scenario by Lev Serebryakov <lev@freebsd.org>
34# newfs -O2 -U -b 65536
35# The io programs will get stuck in nbufkv wait state.
36
37# Threads stuck in newbuf:
38# https://people.freebsd.org/~pho/stress/log/newfs4-2.txt
39
40odir=`pwd`
41cd /tmp
42sed '1,/^EOF/d' < $odir/$0 > newfs4.c
43mycc -o newfs4 -Wall -Wextra newfs4.c || exit 1
44rm -f newfs4.c
45cd $odir
46
47mount | grep "$mntpoint" | grep -q md${mdstart}$part && umount $mntpoint
48mdconfig -l | grep md$mdstart > /dev/null &&  mdconfig -d -u $mdstart
49
50size=9	# Gb
51[ `df -k $(dirname $diskimage) | tail -1 | \
52    awk '{print $4}'` -lt $((size * 1024 * 1024)) ] && \
53    echo "Not enough disk space on `dirname $diskimage`." && exit 1
54trap "rm -f $diskimage" EXIT INT
55dd if=/dev/zero of=$diskimage bs=1m count=$((size * 1024)) status=none ||
56	exit 1
57
58blocksize="-b 65536"
59opt="-O2 -U"
60mdconfig -a -t vnode -f $diskimage -u $mdstart
61bsdlabel -w md$mdstart auto
62newfs $blocksize $opt md${mdstart}$part > /dev/null
63mount /dev/md${mdstart}$part $mntpoint
64
65cd $mntpoint
66truncate -s 2g f1
67truncate -s 2g f2
68truncate -s 2g f3
69truncate -s 2g f4
70/tmp/newfs4 f1 &
71/tmp/newfs4 f2 &
72/tmp/newfs4 f3 &
73/tmp/newfs4 f4 &
74wait
75
76while mount | grep "$mntpoint" | grep -q md${mdstart}$part; do
77	umount -f $mntpoint || sleep 1
78done
79checkfs /dev/md${mdstart}$part; s=$?
80
81mdconfig -d -u $mdstart
82rm -f /tmp/newfs4
83exit $s
84
85EOF
86#include <sys/types.h>
87#include <sys/stat.h>
88
89#include <err.h>
90#include <fcntl.h>
91#include <stdio.h>
92#include <stdlib.h>
93#include <unistd.h>
94
95/* Perform random IO operations on a file */
96
97int
98main(int argc, char **argv)
99{
100	struct stat sb;
101	off_t bp, maxb;
102	long i;
103	int fd;
104	char buf[256];
105
106	if (argc != 2) {
107		fprintf(stderr, "Usage %s: file\n", argv[0]);
108		return (1);
109	}
110	if ((fd = open(argv[1], O_RDWR)) == -1)
111		err(1, "open(%s)", argv[1]);
112	if (fstat(fd, &sb) == -1)
113		err(1, "fstatf(stdin)");
114	maxb = sb.st_size - sizeof(buf);
115
116	for (i = 0; i < 10000; i++) {
117		bp = arc4random();
118		bp = (bp << 31 | arc4random()) % maxb;
119
120		if (lseek(fd, bp, 0) == -1)
121			err(1, "lseek()");
122		if (write(fd, buf, sizeof(buf)) != sizeof(buf))
123			err(1, "write()");
124	}
125	close(fd);
126
127	return (0);
128}
129