xref: /freebsd/tools/test/stress2/misc/nlink4.sh (revision bdd1243d)
1#!/bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2022 Peter Holm <pho@FreeBSD.org>
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# https://reviews.freebsd.org/D35514
30# rename(2) version
31
32. ../default.cfg
33
34cat > /tmp/nlink4.c <<EOF
35#include <sys/stat.h>
36#include <ufs/ufs/dinode.h>
37#include <err.h>
38#include <errno.h>
39#include <fcntl.h>
40#include <stdio.h>
41#include <unistd.h>
42
43int
44main (void) {
45	int fd, i, mx;
46	char file[100];
47
48	snprintf(file, sizeof(file), "f");
49	if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
50	    DEFFILEMODE)) == -1)
51		err(1, "creat(%s)", file);
52	close(fd);
53
54	mx = UFS_LINK_MAX - 1; /* UFS_LINK_MAX = 32767 */
55	for (i = 0; i < mx; i++) {
56		snprintf(file, sizeof(file), "%d", i);
57		if (link("f", file) == -1)
58			err(1, "link(%s, %s)", "f", file);
59
60	}
61
62	/* The following link(2) must fail */
63	i = mx;
64	snprintf(file, sizeof(file), "%d", i);
65	if (link("f", file) != -1)
66		err(1, "link(%s, %s)", "f", file);
67	if (errno != EMLINK)
68		err(1, "Must fail: link(%s, %s)", "f", file);
69
70	/* Must succeed */
71	i = 0;
72	snprintf(file, sizeof(file), "%d", i);
73	if (unlink(file) == -1)
74		err(1, "unlink(%s)", file);
75	if (rename("1", "a") == -1) {
76		if (errno == EMLINK)
77			fprintf(stderr, "Unexpected EMLINK\n");
78		err(1, "rename(%s, %s)", "1", "a");
79	}
80
81	return (0);
82}
83EOF
84mycc -o /tmp/nlink4 -Wall -Wextra -O2 /tmp/nlink4.c || exit 1
85rm /tmp/nlink4.c
86
87set -e
88here=`pwd`
89mount | grep -q "on $mntpoint " && umount -f $mntpoint
90mdconfig -l | grep "md$mdstart " && mdconfig -d -u $mdstart
91mdconfig -a -t swap -s 1g -u $mdstart
92newfs -Un /dev/md$mdstart > /dev/null
93mount /dev/md$mdstart $mntpoint
94set +e
95
96cd $mntpoint
97/tmp/nlink4; s=$?
98n=`ls -a | wc -l`
99[ $s -ne 0 ] && echo "$n files"
100cd $here
101
102umount $mntpoint
103mdconfig -d -u $mdstart
104rm /tmp/nlink4
105exit $s
106