xref: /freebsd/tools/test/stress2/misc/umountf10.sh (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
1#!/bin/sh
2
3#
4# Copyright (c) 2016 Dell EMC Isilon
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# Demonstrate deadlock
30# https://people.freebsd.org/~pho/stress/log/umountf10.txt
31
32# Test scenario suggestion by: kib
33# Fixed in r308618.
34
35[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36. ../default.cfg
37
38cont=/tmp/umountf10.continue
39dir=/tmp
40odir=`pwd`
41cd $dir
42sed '1,/^EOF/d' < $odir/$0 > $dir/umountf10.c
43mycc -o umountf10 -Wall -Wextra -O2 -g umountf10.c || exit 1
44rm -f umountf10.c
45cd $odir
46
47mount | grep -q "on $mntpoint " && umount -f $mntpoint
48[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
49
50mdconfig -a -t swap -s 512m -u $mdstart
51gpart create -s GPT md$mdstart > /dev/null || exit 1
52gpart add -t freebsd-ufs md$mdstart > /dev/null || exit 1
53newfs -n $newfs_flags md${mdstart}p1 > /dev/null || exit 1
54mount /dev/md${mdstart}p1 $mntpoint
55touch $mntpoint/marker $cont
56trap "rm -f $cont" EXIT INT
57
58daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 4m -i 20)" > \
59    /dev/null 2>&1
60sleep 5
61
62for i in `jot 4`; do
63	/tmp/umountf10 $mntpoint &
64	pid="$pid $!"
65done
66
67for i in `jot 10`; do
68	while [ -e $cont ]; do procstat -f $pid > /dev/null 2>&1; done &
69	pid2="$pid2 $!"
70done
71
72while [ -e $cont ]; do find $mntpoint -ls > /dev/null 2>&1; done &
73tpid=$!
74
75start=`date '+%s'`
76while [ $((`date '+%s'` - start)) -lt 300 ]; do
77	umount -f $mntpoint 2>/dev/null &&
78	    mount /dev/md${mdstart}p1 $mntpoint
79done
80while pgrep -q swap; do
81	pkill -9 swap
82done
83kill $pid $pid2 $tpid
84wait
85
86umount $mntpoint
87rm -f $mntpoint/file.* /tmp/umountf10
88mdconfig -d -u $mdstart
89
90exit 0
91EOF
92#include <sys/param.h>
93#include <sys/stat.h>
94
95#include <err.h>
96#include <errno.h>
97#include <fcntl.h>
98#include <stdio.h>
99#include <stdlib.h>
100#include <unistd.h>
101
102int
103main(int argc, char *argv[])
104{
105	int fd;
106	char file[MAXPATHLEN + 1];
107	char marker[MAXPATHLEN + 1];
108
109	if (argc != 2) {
110		fprintf(stderr, "Usage: %s <file path>", argv[0]);
111		exit(1);
112	}
113
114	alarm(600);
115	snprintf(file, sizeof(file), "%s/file.%06d", argv[1], getpid());
116	snprintf(marker, sizeof(marker), "%s/marker", argv[1]);
117	for (;;) {
118		if (access(marker, R_OK) == -1)
119			continue;
120		if ((fd = open(file, O_RDWR | O_CREAT | O_APPEND,
121		    DEFFILEMODE)) == -1) {
122			if (errno != ENOENT && errno != EBUSY)
123				warn("open(%s)", file);
124			continue;
125		}
126		write(fd, "a", 1);
127		usleep(arc4random() % 400);
128		close(fd);
129		unlink(argv[1]);
130	}
131
132	return(0);
133}
134