xref: /freebsd/tools/test/stress2/misc/link.sh (revision 608c97bff921c8936eb71f4b1a177625cfa60250)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
48a272653SPeter Holm# Copyright (c) 2014 EMC Corp.
58a272653SPeter Holm# All rights reserved.
68a272653SPeter Holm#
78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
88a272653SPeter Holm# modification, are permitted provided that the following conditions
98a272653SPeter Holm# are met:
108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
118a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
138a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
148a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
158a272653SPeter Holm#
168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268a272653SPeter Holm# SUCH DAMAGE.
278a272653SPeter Holm#
288a272653SPeter Holm
298a272653SPeter Holm# Deadlock seen for file systems which suspend writes on unmount, such as
308a272653SPeter Holm# UFS and tmpfs.
318a272653SPeter Holm# http://people.freebsd.org/~pho/stress/log/link.txt
328a272653SPeter Holm# Fixed by r272130
338a272653SPeter Holm
348a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
358a272653SPeter Holm
368a272653SPeter Holm. ../default.cfg
378a272653SPeter Holm
388a272653SPeter Holmhere=`pwd`
398a272653SPeter Holmcd /tmp
408a272653SPeter Holmsed '1,/^EOF/d' < $here/$0 > link.c
418a272653SPeter Holmmycc -o link -Wall -Wextra -O2 -g link.c || exit 1
428a272653SPeter Holmrm -f link.c
438a272653SPeter Holm
448a272653SPeter Holmmount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
458a272653SPeter Holm[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
468a272653SPeter Holm
478a272653SPeter Holmmdconfig -a -t swap -s 1g -u $mdstart || exit 1
48*608c97bfSPeter Holmnewfs $newfs_flags md$mdstart > /dev/null
49*608c97bfSPeter Holmmount /dev/md$mdstart $mntpoint
508a272653SPeter Holm
518a272653SPeter Holmdaemon sh -c "(cd $here/../testcases/swap; ./swap -t 5m -i 20 -h -l 100)" \
528a272653SPeter Holm    > /dev/null 2>&1
538a272653SPeter Holm/tmp/link $mntpoint > /dev/null 2>&1 &
548a272653SPeter Holm
558a272653SPeter Holmfor i in `jot 100`; do
568a272653SPeter Holm	umount -f $mntpoint &&
57*608c97bfSPeter Holm	    mount /dev/md$mdstart $mntpoint
588a272653SPeter Holm	sleep .1
598a272653SPeter Holmdone
608a272653SPeter Holmpkill -9 link
618a272653SPeter Holmwait
628a272653SPeter Holm
638a272653SPeter Holmwhile mount | grep $mntpoint | grep -q /dev/md; do
648a272653SPeter Holm	umount $mntpoint || sleep 1
658a272653SPeter Holmdone
668a272653SPeter Holmmdconfig -d -u $mdstart
678a272653SPeter Holm
688a272653SPeter Holm# tmpfs
698a272653SPeter Holmmount -t tmpfs tmpfs $mntpoint
708a272653SPeter Holm
718a272653SPeter Holm/tmp/link $mntpoint > /dev/null 2>&1 &
728a272653SPeter Holm
738a272653SPeter Holmfor i in `jot 100`; do
748a272653SPeter Holm	umount -f $mntpoint &&
758a272653SPeter Holm	    mount -t tmpfs tmpfs $mntpoint
768a272653SPeter Holm	sleep .1
778a272653SPeter Holmdone
788a272653SPeter Holmpkill -9 link swap
798a272653SPeter Holmwait
808a272653SPeter Holm
818a272653SPeter Holmwhile pkill -9 swap; do
828a272653SPeter Holm	:
838a272653SPeter Holmdone > /dev/null 2>&1
848a272653SPeter Holmwhile mount | grep $mntpoint | grep -q tmpfs; do
858a272653SPeter Holm	umount $mntpoint || sleep 1
868a272653SPeter Holmdone
878a272653SPeter Holm[ -d "$mntpoint" ] && (cd $mntpoint && find . -delete)
888a272653SPeter Holmrm -f /tmp/link
898a272653SPeter Holmexit 0
908a272653SPeter HolmEOF
918a272653SPeter Holm#include <sys/param.h>
928a272653SPeter Holm#include <sys/wait.h>
938a272653SPeter Holm
948a272653SPeter Holm#include <err.h>
958a272653SPeter Holm#include <errno.h>
968a272653SPeter Holm#include <fcntl.h>
978a272653SPeter Holm#include <stdio.h>
988a272653SPeter Holm#include <time.h>
998a272653SPeter Holm#include <stdlib.h>
1008a272653SPeter Holm#include <unistd.h>
1018a272653SPeter Holm
1028a272653SPeter Holm#define PARALLEL 16
1038a272653SPeter Holm#define RUNTIME 300
1048a272653SPeter Holm
1058a272653SPeter Holmtime_t start;
1068a272653SPeter Holmchar *dir;
1078a272653SPeter Holm
1088a272653SPeter Holmvoid
1098a272653SPeter Holmtrename(void)
1108a272653SPeter Holm{
1118a272653SPeter Holm	int fd;
1128a272653SPeter Holm	char name1[MAXPATHLEN + 1];
1138a272653SPeter Holm	char name2[MAXPATHLEN + 1];
1148a272653SPeter Holm
1158a272653SPeter Holm	setproctitle(__func__);
1168a272653SPeter Holm	snprintf(name1, sizeof(name1), "%s/r1.%05d", dir, getpid());
1178a272653SPeter Holm	snprintf(name2, sizeof(name2), "%s/r2.%05d", dir, getpid());
1188a272653SPeter Holm	if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
1198a272653SPeter Holm		err(1, "open(%s)", name1);
1208a272653SPeter Holm	close(fd);
1218a272653SPeter Holm
1228a272653SPeter Holm	while (time(NULL) - start < RUNTIME) {
1238a272653SPeter Holm		if (rename(name1, name2) == -1) {
1248a272653SPeter Holm			if (errno == ENOENT) {
1258a272653SPeter Holm				if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
1268a272653SPeter Holm					err(1, "open(%s)", name1);
1278a272653SPeter Holm				close(fd);
1288a272653SPeter Holm				continue;
1298a272653SPeter Holm			} else
1308a272653SPeter Holm				warn("link(%s, %s)", name1, name2);
1318a272653SPeter Holm		}
1328a272653SPeter Holm		if (rename(name2, name1) == -1)
1338a272653SPeter Holm			warn("link(%s, %s)", name2, name1);
1348a272653SPeter Holm	}
1358a272653SPeter Holm	unlink(name1);
1368a272653SPeter Holm	unlink(name2);
1378a272653SPeter Holm	_exit(0);
1388a272653SPeter Holm}
1398a272653SPeter Holm
1408a272653SPeter Holmvoid
1418a272653SPeter Holmtlink(void)
1428a272653SPeter Holm{
1438a272653SPeter Holm	int fd;
1448a272653SPeter Holm	char name1[MAXPATHLEN + 1];
1458a272653SPeter Holm	char name2[MAXPATHLEN + 1];
1468a272653SPeter Holm
1478a272653SPeter Holm	setproctitle(__func__);
1488a272653SPeter Holm	snprintf(name1, sizeof(name1), "%s/f1.%05d", dir, getpid());
1498a272653SPeter Holm	snprintf(name2, sizeof(name2), "%s/f2.%05d", dir, getpid());
1508a272653SPeter Holm	if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
1518a272653SPeter Holm		err(1, "open(%s)", name1);
1528a272653SPeter Holm	close(fd);
1538a272653SPeter Holm
1548a272653SPeter Holm	while (time(NULL) - start < RUNTIME) {
1558a272653SPeter Holm		unlink(name2);
1568a272653SPeter Holm		if (link(name1, name2) == -1) {
1578a272653SPeter Holm			if (errno == ENOENT) {
1588a272653SPeter Holm				if ((fd = open(name1, O_RDWR | O_CREAT, 0644)) == -1)
1598a272653SPeter Holm					err(1, "open(%s)", name1);
1608a272653SPeter Holm				close(fd);
1618a272653SPeter Holm				continue;
1628a272653SPeter Holm			} else
1638a272653SPeter Holm				warn("link(%s, %s)", name1, name2);
1648a272653SPeter Holm		}
1658a272653SPeter Holm	}
1668a272653SPeter Holm	unlink(name1);
1678a272653SPeter Holm	unlink(name2);
1688a272653SPeter Holm	_exit(0);
1698a272653SPeter Holm}
1708a272653SPeter Holm
1718a272653SPeter Holmint
1728a272653SPeter Holmmain(int argc, char **argv)
1738a272653SPeter Holm{
1748a272653SPeter Holm	int i, type;
1758a272653SPeter Holm
1768a272653SPeter Holm	if (argc != 2)
1778a272653SPeter Holm		errx(1, "Usage: %s <full path to dir>", argv[0]);
1788a272653SPeter Holm	dir = argv[1];
1798a272653SPeter Holm	type = arc4random() % 2; /* test either link() or rename() */
1808a272653SPeter Holm
1818a272653SPeter Holm	start = time(NULL);
1828a272653SPeter Holm	for (i = 0; i < PARALLEL; i++) {
1838a272653SPeter Holm		if (type == 0 && fork() == 0)
1848a272653SPeter Holm			tlink();
1858a272653SPeter Holm		if (type == 1 && fork() == 0)
1868a272653SPeter Holm			trename();
1878a272653SPeter Holm	}
1888a272653SPeter Holm	for (i = 0; i < PARALLEL; i++)
1898a272653SPeter Holm		wait(NULL);
1908a272653SPeter Holm
1918a272653SPeter Holm	return(0);
1928a272653SPeter Holm}
193