From owner-svn-src-user@freebsd.org  Tue Oct 27 20:13:34 2020
Return-Path: <owner-svn-src-user@freebsd.org>
Delivered-To: svn-src-user@mailman.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
 by mailman.nyi.freebsd.org (Postfix) with ESMTP id 17992459426
 for <svn-src-user@mailman.nyi.freebsd.org>;
 Tue, 27 Oct 2020 20:13:34 +0000 (UTC) (envelope-from pho@FreeBSD.org)
Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org
 [IPv6:2610:1c1:1:606c::19:3])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256
 client-signature RSA-PSS (4096 bits) client-digest SHA256)
 (Client CN "mxrelay.nyi.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 4CLNF96wThz4709;
 Tue, 27 Oct 2020 20:13:33 +0000 (UTC) (envelope-from pho@FreeBSD.org)
Received: from repo.freebsd.org (repo.freebsd.org
 [IPv6:2610:1c1:1:6068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D198511307;
 Tue, 27 Oct 2020 20:13:33 +0000 (UTC) (envelope-from pho@FreeBSD.org)
Received: from repo.freebsd.org ([127.0.1.37])
 by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 09RKDXKG018082;
 Tue, 27 Oct 2020 20:13:33 GMT (envelope-from pho@FreeBSD.org)
Received: (from pho@localhost)
 by repo.freebsd.org (8.15.2/8.15.2/Submit) id 09RKDXAq018081;
 Tue, 27 Oct 2020 20:13:33 GMT (envelope-from pho@FreeBSD.org)
Message-Id: <202010272013.09RKDXAq018081@repo.freebsd.org>
X-Authentication-Warning: repo.freebsd.org: pho set sender to pho@FreeBSD.org
 using -f
From: Peter Holm <pho@FreeBSD.org>
Date: Tue, 27 Oct 2020 20:13:33 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-user@freebsd.org
Subject: svn commit: r367092 - user/pho/stress2/misc
X-SVN-Group: user
X-SVN-Commit-Author: pho
X-SVN-Commit-Paths: user/pho/stress2/misc
X-SVN-Commit-Revision: 367092
X-SVN-Commit-Repository: base
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: svn-src-user@freebsd.org
X-Mailman-Version: 2.1.33
Precedence: list
List-Id: "SVN commit messages for the experimental &quot; user&quot;
 src tree" <svn-src-user.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/svn-src-user>,
 <mailto:svn-src-user-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-user/>
List-Post: <mailto:svn-src-user@freebsd.org>
List-Help: <mailto:svn-src-user-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/svn-src-user>,
 <mailto:svn-src-user-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Tue, 27 Oct 2020 20:13:34 -0000

Author: pho
Date: Tue Oct 27 20:13:33 2020
New Revision: 367092
URL: https://svnweb.freebsd.org/changeset/base/367092

Log:
  Added an old test, which just triggered a panic.

Added:
  user/pho/stress2/misc/fullpath2.sh   (contents, props changed)

Added: user/pho/stress2/misc/fullpath2.sh
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/pho/stress2/misc/fullpath2.sh	Tue Oct 27 20:13:33 2020	(r367092)
@@ -0,0 +1,215 @@
+#!/bin/sh
+
+#
+# Copyright (c) 2016 Dell EMC Isilon
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+
+# WiP: fullpath NULL reference problem hunt.
+
+# From the commit log of r308407:
+#   vn_fullpath1() checked VV_ROOT and then unreferenced
+#   vp->v_mount->mnt_vnodecovered unlocked.  This allowed unmount to race.
+#   Lock vnode after we noticed the VV_ROOT flag.  See comments for
+#   explanation why unlocked check for the flag is considered safe.
+
+# 'panic: namei: garbage in ni_resflags: 1':
+# https://people.freebsd.org/~pho/stress/log/fullpath2.txt
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+. ../default.cfg
+
+cont=/tmp/fullpath2.continue
+dir=/tmp
+odir=`pwd`
+cd $dir
+sed '1,/^EOF/d' < $odir/$0 > $dir/fullpath2.c
+mycc -o fullpath2 -Wall -Wextra -O2 -g fullpath2.c -lprocstat || exit 1
+rm -f fullpath2.c
+cd $odir
+
+mount | grep -q "on $mntpoint " && umount -f $mntpoint
+[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
+
+mdconfig -a -t swap -s 1g -u $mdstart
+gpart create -s GPT md$mdstart > /dev/null || exit 1
+gpart add -t freebsd-ufs md$mdstart > /dev/null || exit 1
+newfs -n $newfs_flags md${mdstart}p1 > /dev/null || exit 1
+mount /dev/md${mdstart}p1 $mntpoint
+touch $mntpoint/marker $cont
+trap "rm -f $cont" EXIT INT
+
+daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 4m -i 10 -l 100)" > \
+    /dev/null 2>&1
+
+for i in `jot $(jot -r 1 2 10)`; do
+	/tmp/fullpath2 $mntpoint &
+	pids="$pids $!"
+done
+
+for i in `jot $(jot -r 1 2 5)`; do
+	while [ -e $cont ]; do find $mntpoint -ls > /dev/null 2>&1; done &
+	pidf="$pidf $!"
+done
+
+umounts=0
+while pgrep -q fullpath2; do
+	for i in `jot 30`; do
+		umount -f $mntpoint && umounts=$((umounts+1)) &&
+		    mount /dev/md${mdstart}p1 $mntpoint
+		sleep 2
+	done
+done
+echo "$umounts umounts"
+rm -f $cont
+while mount | grep -q "on $mntpoint "; do
+	umount -f $mntpoint
+done
+for i in $pids; do
+	wait $i
+done
+while pgrep -q swap; do
+	pkill -9 swap
+done
+
+kill $pidp $pidf > /dev/null 2>&1
+wait
+
+rm -f $mntpoint/file.* /tmp/fullpath2 fullpath2.core
+mdconfig -d -u $mdstart
+
+exit 0
+EOF
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <libprocstat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static volatile u_int *share;
+
+#define NB 1024
+#define RUNTIME 300
+
+/* dtrace -w -n 'fbt::*vn_fullpath1:entry {@rw[execname,probefunc] = count(); }' */
+
+static void
+getfiles(pid_t pid)
+{
+	struct filestat_list *head;
+	struct kinfo_proc *p;
+	struct procstat *prstat;
+	unsigned int cnt;
+
+	if ((prstat = procstat_open_sysctl()) == NULL)
+		err(1, "procstat_open_sysctl");
+
+	if ((p = procstat_getprocs(prstat, KERN_PROC_PID,
+				    pid, &cnt)) == NULL)
+		err(1, "procstat_getprocs");
+
+	if ((head = procstat_getfiles(prstat, p, 0)) == NULL)
+		err(1, "procstat_getfiles");
+
+	procstat_freefiles(prstat, head);
+	procstat_freeprocs(prstat, p);
+	procstat_close(prstat);
+}
+
+int
+main(int argc, char *argv[])
+{
+	size_t len;
+	time_t start;
+	int fd[NB], i, n;
+	pid_t pid;
+
+	len = PAGE_SIZE;
+	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
+	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
+		err(1, "mmap");
+
+	if ((pid = fork()) == 0) {
+		setproctitle("getfiles");
+		while (share[0] == 0)
+			getfiles(pid);
+		_exit(0);
+	}
+
+	char file[MAXPATHLEN + 1];
+	char marker[MAXPATHLEN + 1];
+
+	if (argc != 2) {
+		fprintf(stderr, "Usage: %s <file path>\n", argv[0]);
+		exit(1);
+	}
+
+	memset(fd, 0, sizeof(fd));
+	snprintf(marker, sizeof(marker), "%s/marker", argv[1]);
+	i = n = 0;
+	start = time(NULL);
+	while (time(NULL) - start < RUNTIME) {
+		snprintf(file, sizeof(file), "%s/file.%06d.%02d", argv[1], getpid(), i);
+		if (access(marker, R_OK) == -1)
+			continue;
+		if (fd[i] > 0)
+			close(fd[i]);
+		if ((fd[i] = open(file, O_RDWR | O_CREAT | O_APPEND,
+		    DEFFILEMODE)) == -1) {
+			if (errno != ENOENT && errno != EBUSY)
+				warn("open(%s)", file);
+			continue;
+		}
+		n++;
+		write(fd[i], "a", 1);
+		usleep(arc4random() % 400);
+		if (arc4random() % 100 < 10) {
+			close(fd[i]);
+			unlink(file);
+			fd[i] = 0;
+		}
+		i++;
+		i = i % NB;
+	}
+	share[0] = 1;
+
+	if (waitpid(pid, NULL, 0) != pid)
+		err(1, "waitpid");
+	if (n < 100)
+		errx(1, "Short run: %d", n);
+
+	return (0);
+}

From owner-svn-src-user@freebsd.org  Thu Oct 29 14:28:28 2020
Return-Path: <owner-svn-src-user@freebsd.org>
Delivered-To: svn-src-user@mailman.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
 by mailman.nyi.freebsd.org (Postfix) with ESMTP id F3EC7453306
 for <svn-src-user@mailman.nyi.freebsd.org>;
 Thu, 29 Oct 2020 14:28:27 +0000 (UTC) (envelope-from pho@FreeBSD.org)
Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org
 [IPv6:2610:1c1:1:606c::19:3])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256
 client-signature RSA-PSS (4096 bits) client-digest SHA256)
 (Client CN "mxrelay.nyi.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 4CMSV36872z3Z6T;
 Thu, 29 Oct 2020 14:28:27 +0000 (UTC) (envelope-from pho@FreeBSD.org)
Received: from repo.freebsd.org (repo.freebsd.org
 [IPv6:2610:1c1:1:6068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id B657D2EDD9;
 Thu, 29 Oct 2020 14:28:27 +0000 (UTC) (envelope-from pho@FreeBSD.org)
Received: from repo.freebsd.org ([127.0.1.37])
 by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 09TESRZ4087723;
 Thu, 29 Oct 2020 14:28:27 GMT (envelope-from pho@FreeBSD.org)
Received: (from pho@localhost)
 by repo.freebsd.org (8.15.2/8.15.2/Submit) id 09TESRpM087722;
 Thu, 29 Oct 2020 14:28:27 GMT (envelope-from pho@FreeBSD.org)
Message-Id: <202010291428.09TESRpM087722@repo.freebsd.org>
X-Authentication-Warning: repo.freebsd.org: pho set sender to pho@FreeBSD.org
 using -f
From: Peter Holm <pho@FreeBSD.org>
Date: Thu, 29 Oct 2020 14:28:27 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-user@freebsd.org
Subject: svn commit: r367133 - user/pho/stress2/misc
X-SVN-Group: user
X-SVN-Commit-Author: pho
X-SVN-Commit-Paths: user/pho/stress2/misc
X-SVN-Commit-Revision: 367133
X-SVN-Commit-Repository: base
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: svn-src-user@freebsd.org
X-Mailman-Version: 2.1.33
Precedence: list
List-Id: "SVN commit messages for the experimental &quot; user&quot;
 src tree" <svn-src-user.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/svn-src-user>,
 <mailto:svn-src-user-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-user/>
List-Post: <mailto:svn-src-user@freebsd.org>
List-Help: <mailto:svn-src-user-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/svn-src-user>,
 <mailto:svn-src-user-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Thu, 29 Oct 2020 14:28:28 -0000

Author: pho
Date: Thu Oct 29 14:28:27 2020
New Revision: 367133
URL: https://svnweb.freebsd.org/changeset/base/367133

Log:
  Added when problem was fixed.

Modified:
  user/pho/stress2/misc/fullpath2.sh

Modified: user/pho/stress2/misc/fullpath2.sh
==============================================================================
--- user/pho/stress2/misc/fullpath2.sh	Thu Oct 29 14:23:52 2020	(r367132)
+++ user/pho/stress2/misc/fullpath2.sh	Thu Oct 29 14:28:27 2020	(r367133)
@@ -28,7 +28,7 @@
 # $FreeBSD$
 #
 
-# WiP: fullpath NULL reference problem hunt.
+# fullpath NULL reference problem hunt.
 
 # From the commit log of r308407:
 #   vn_fullpath1() checked VV_ROOT and then unreferenced
@@ -38,6 +38,7 @@
 
 # 'panic: namei: garbage in ni_resflags: 1':
 # https://people.freebsd.org/~pho/stress/log/fullpath2.txt
+# Fixed by r367130
 
 [ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
 . ../default.cfg

From owner-svn-src-user@freebsd.org  Fri Oct 30 06:30:47 2020
Return-Path: <owner-svn-src-user@freebsd.org>
Delivered-To: svn-src-user@mailman.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
 by mailman.nyi.freebsd.org (Postfix) with ESMTP id 7F897448EE7
 for <svn-src-user@mailman.nyi.freebsd.org>;
 Fri, 30 Oct 2020 06:30:47 +0000 (UTC) (envelope-from pho@FreeBSD.org)
Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org
 [IPv6:2610:1c1:1:606c::19:3])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256
 client-signature RSA-PSS (4096 bits) client-digest SHA256)
 (Client CN "mxrelay.nyi.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 4CMsrR2rg4z4pWG;
 Fri, 30 Oct 2020 06:30:47 +0000 (UTC) (envelope-from pho@FreeBSD.org)
Received: from repo.freebsd.org (repo.freebsd.org
 [IPv6:2610:1c1:1:6068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 42F54125A0;
 Fri, 30 Oct 2020 06:30:47 +0000 (UTC) (envelope-from pho@FreeBSD.org)
Received: from repo.freebsd.org ([127.0.1.37])
 by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 09U6UlQE080290;
 Fri, 30 Oct 2020 06:30:47 GMT (envelope-from pho@FreeBSD.org)
Received: (from pho@localhost)
 by repo.freebsd.org (8.15.2/8.15.2/Submit) id 09U6UlCh080289;
 Fri, 30 Oct 2020 06:30:47 GMT (envelope-from pho@FreeBSD.org)
Message-Id: <202010300630.09U6UlCh080289@repo.freebsd.org>
X-Authentication-Warning: repo.freebsd.org: pho set sender to pho@FreeBSD.org
 using -f
From: Peter Holm <pho@FreeBSD.org>
Date: Fri, 30 Oct 2020 06:30:47 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-user@freebsd.org
Subject: svn commit: r367156 - user/pho/stress2/misc
X-SVN-Group: user
X-SVN-Commit-Author: pho
X-SVN-Commit-Paths: user/pho/stress2/misc
X-SVN-Commit-Revision: 367156
X-SVN-Commit-Repository: base
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: svn-src-user@freebsd.org
X-Mailman-Version: 2.1.33
Precedence: list
List-Id: "SVN commit messages for the experimental &quot; user&quot;
 src tree" <svn-src-user.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/svn-src-user>,
 <mailto:svn-src-user-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-user/>
List-Post: <mailto:svn-src-user@freebsd.org>
List-Help: <mailto:svn-src-user-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/svn-src-user>,
 <mailto:svn-src-user-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Fri, 30 Oct 2020 06:30:47 -0000

Author: pho
Date: Fri Oct 30 06:30:46 2020
New Revision: 367156
URL: https://svnweb.freebsd.org/changeset/base/367156

Log:
  Added an old fsck_ffs(8) test scenario.

Added:
  user/pho/stress2/misc/fsck.sh   (contents, props changed)

Added: user/pho/stress2/misc/fsck.sh
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/pho/stress2/misc/fsck.sh	Fri Oct 30 06:30:46 2020	(r367156)
@@ -0,0 +1,149 @@
+#!/bin/sh
+
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2018 Dell EMC Isilon
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+
+# fsck_ffs(8) test.
+# "UFS /dev/md11 (/mnt11) cylinder checksum failed" seen.
+# Fixed by r341510.
+
+# 'panic: invalid counts on struct mount' seen:
+# https://people.freebsd.org/~pho/stress/log/fsck-4.txt
+
+. ../default.cfg
+
+[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
+
+echo 'int sync(void) { return (0); }' > /tmp/fsck_preload.c
+mycc -o /tmp/fsck_preload.so -shared -fpic /tmp/fsck_preload.c || exit 1
+cc -o /tmp/fsck_preload.so -shared -fpic /tmp/fsck_preload.c || exit 1
+rm /tmp/fsck_preload.c
+
+set -e
+u1=$mdstart
+u2=$((mdstart + 1))
+mp1=${mntpoint}$u1
+mp2=${mntpoint}$u2
+mkdir -p $mp1 $mp2
+log=$mp1/fsck.sh.log
+diskimage=$mp1/fsck.sh.diskimage
+backup=/tmp/fsck.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz
+asbs=0
+cleans=0
+reruns=0
+waccess=0
+
+max=$((10 * 1024 * 1024))
+[ "$newfs_flags" = "-j" ] &&
+    max=$((20 * 1024 * 1024))
+
+set +e
+mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
+[ -c /dev/md$u1 ] && mdconfig -d -u $u1
+mdconfig -a -t swap -s 1g -u $u1
+newfs $newfs_flags /dev/md$u1 > /dev/null
+mount /dev/md$u1 $mp1
+
+[ -c /dev/md$u2 ] && mdconfig -d -u $u2
+dd if=/dev/zero of=$diskimage bs=$max count=1 status=none
+mdconfig -a -t vnode -f $diskimage -u $u2
+backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \
+    tail -1 | sed 's/,//g'`
+newfs $newfs_flags md$u2 > /dev/null
+mount /dev/md$u2 $mp2
+[ -d /usr/include/sys ] && cp -r /usr/include/sys $mp2
+umount $mp2
+
+chk() {
+	local i
+
+	LD_PRELOAD=/tmp/fsck_preload.so  \
+	    fsck_ffs -fy $1 > $log 2>&1
+	r=$?
+	if grep -q "ALTERNATE SUPER-BLOCK FAILED" $log; then
+		for b in $backups; do
+			echo "Using alternate SB $b"
+			asbs=$((asbs + 1))
+			LD_PRELOAD=/tmp/fsck_preload.so  \
+			    fsck_ffs -b $b -fy $1 > $log 2>&1
+			grep -q "Superblock check-hash failed" $log ||
+			   break
+		done
+		usedasb=1
+	else
+		usedasb=0
+	fi
+	LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0
+	! grep -Eq "IS CLEAN|MARKED CLEAN" $log; clean=$?
+	! grep -q RERUN $log; rerun=$?
+	! grep -q "NO WRITE ACCESS" $log; waccess=$?
+	[ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1"
+
+}
+
+cd /tmp
+s=0
+start=`date +%s`
+while [ $((`date +%s` - start)) -lt 60 ]; do
+	gzip < $diskimage > $backup; sync
+	mount /dev/md$u2 $mp2 || { s=101; break; }
+	touch $mp2/`jot -rc 8 a z | tr -d '\n'`
+	umount $mp2
+	for i in `jot 2`; do
+		pos=`jot -r 1 0 $((max - 1))`
+		dd if=/dev/random of=$diskimage oseek=$pos bs=1 count=1 \
+		    conv=notrunc status=none
+	done
+	for i in `jot 3`; do
+		chk /dev/md$u2
+		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
+		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
+	done
+	[ $r -ne 0 -a $clean -eq 1 ] &&
+	    { echo "CLEAN && non zero exit code"; break; }
+	[ $clean -eq 1 ] && continue
+	[ $usedasb -eq 1 ] && { echo "Alt. SB failed"; s=103; }
+	[ $waccess -eq 1 ] && { echo "No write access"; s=555; }
+	break
+done
+[ $DEBUG ] &&
+    echo "$cleans cleans, $reruns reruns, $asbs alternate SBs." && cat $log
+if [ $clean -ne 1 ]; then
+	echo "FS still not clean. Last fsck_ffs exit code was $r."
+	cat $log
+	cp -v $log /tmp || rm $log
+	[ $s -eq 0 ] && s=104
+fi
+mdconfig -d -u $u2 || exit 1
+[ -f fsck_ffs.core ] && ls -l fsck_ffs.core
+
+umount $mp1
+mdconfig -d -u $u1
+rm -f /tmp/fsck_preload.so $backup
+exit $s