xref: /freebsd/tools/test/stress2/misc/newfs5.sh (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1#!/bin/sh
2
3#
4# Copyright (c) 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# Variation of newfs4.sh, using a swap backed MD disk
34
35odir=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $odir/$0 > newfs5.c
38mycc -o newfs5 -Wall -Wextra newfs5.c
39rm -f newfs5.c
40cd $odir
41
42mount | grep "$mntpoint" | grep md$mdstart > /dev/null && umount $mntpoint
43mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
44
45blocksize="-b 65536"
46opt="-O2 -U"
47size=9	# Gb
48mdconfig -a -t swap -s ${size}g -u $mdstart
49newfs $blocksize $opt md$mdstart > /dev/null
50mount /dev/md$mdstart $mntpoint
51
52cd $mntpoint
53truncate -s 2g f1
54truncate -s 2g f2
55truncate -s 2g f3
56truncate -s 2g f4
57/tmp/newfs5 f1 &
58/tmp/newfs5 f2 &
59/tmp/newfs5 f3 &
60/tmp/newfs5 f4 &
61wait
62
63while mount | grep "$mntpoint" | grep -q md$mdstart; do
64	umount -f $mntpoint || sleep 1
65done
66checkfs /dev/md$mdstart; s=$?
67
68mdconfig -d -u $mdstart
69rm -f $diskimage
70rm -f /tmp/newfs5
71exit $s
72
73EOF
74#include <sys/types.h>
75#include <sys/stat.h>
76#include <err.h>
77#include <fcntl.h>
78#include <stdio.h>
79#include <stdlib.h>
80#include <unistd.h>
81
82/* Perform random IO operations on a file */
83
84int
85main(int argc, char **argv)
86{
87	struct stat sb;
88	char buf[256];
89	off_t bp, maxb;
90	int fd;
91	long i;
92
93	if (argc != 2) {
94		fprintf(stderr, "Usage %s: file\n", argv[0]);
95		return (1);
96	}
97	if ((fd = open(argv[1], O_RDWR)) == -1)
98		err(1, "open(%s)", argv[1]);
99	if (fstat(fd, &sb) == -1)
100		err(1, "fstatf(stdin)");
101	maxb = sb.st_size - sizeof(buf);
102
103	for (i = 0; i < 10000; i++) {
104		bp = arc4random();
105		bp = (bp << 31 | arc4random()) % maxb;
106
107		if (lseek(fd, bp, 0) == -1)
108			err(1, "lseek()");
109		if (write(fd, buf, sizeof(buf)) != sizeof(buf))
110			err(1, "write()");
111	}
112	close(fd);
113
114	return (0);
115}
116