1#!/bin/sh
2
3#
4# Copyright (c) 2008 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# Alternate buffer flush path test (Not verified).
30# Regression test for r169006.
31# Apply this patch to amplify the problem:
32#
33# diff -r1.520 vfs_bio.c
34# 894c894
35# <       if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
36# ---
37# >       if (bo->bo_dirty.bv_cnt > dirtybufthresh /*+ 10*/) {
38
39. ../default.cfg
40
41odir=`pwd`
42dir=$RUNDIR/alternativeFlushPath
43
44[ -d $dir ] && find $dir -type f | xargs rm
45rm -rf $dir
46mkdir -p $dir
47cd $dir
48sed '1,/^EOF/d' < $odir/$0 > $dir/alternativeFlushPath.c
49mycc -o /tmp/alternativeFlushPath -Wall -Wextra alternativeFlushPath.c ||
50    exit 1
51rm -f alternativeFlushPath.c
52
53for j in `jot 10`; do
54   /tmp/alternativeFlushPath &
55done
56wait
57sysctl vfs.altbufferflushes
58
59cd $odir
60rm -rf /tmp/alternativeFlushPath $dir
61
62exit
63
64EOF
65#include <stdio.h>
66#include <unistd.h>
67#include <stdlib.h>
68#include <fcntl.h>
69#include <sys/signal.h>
70#include <sys/types.h>
71#include <sys/time.h>
72#include <sys/resource.h>
73#include <err.h>
74
75#define MAXNOFILE 500000	/* To limit runtime */
76
77static volatile sig_atomic_t more;
78
79static void
80handler(int i __unused) {
81	more = 0;
82}
83
84void
85test(void)
86{
87	int i, j;
88        char name[80];
89        pid_t mypid;
90        int *fd;
91        struct rlimit rlp;
92
93        if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
94                err(1, "getrlimit(RLIMIT_NOFILE)");
95	if (rlp.rlim_cur > MAXNOFILE)
96		rlp.rlim_cur = MAXNOFILE;
97	rlp.rlim_cur /= 10;
98        mypid = getpid();
99        fd = malloc(rlp.rlim_cur * sizeof(int));
100
101	for (i = 0, j = 0; i < rlp.rlim_cur && more == 1; i++, j++) {
102		sprintf(name, "f%05d.%05d", mypid, i);
103		if ((fd[i] = open(name, O_CREAT|O_WRONLY, 0666)) == -1) {
104			warn("open(%s)", name);
105			more = 0;
106			break;
107		}
108	}
109	for (i = 0; i < j; i++) {
110		sprintf(name, "f%05d.%05d", mypid, i);
111		if (unlink(name) == -1)
112			warn("unlink(%s)", name);
113	}
114	for (i = 0; i < j; i++) {
115		if (close(fd[i]) == -1)
116			warn("close(%d)", i);
117	}
118	free(fd);
119}
120
121int
122main()
123{
124	more = 1;
125	signal(SIGALRM, handler);
126	alarm(20 * 60);
127	while (more == 1)
128		test();
129
130        return(0);
131}
132