xref: /freebsd/tools/test/stress2/misc/nlink3.sh (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2022 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# https://reviews.freebsd.org/D35514
31# link() version
32
33. ../default.cfg
34
35cat > /tmp/nlink3.c <<EOF
36#include <sys/stat.h>
37#include <ufs/ufs/dinode.h>
38#include <err.h>
39#include <errno.h>
40#include <fcntl.h>
41#include <stdio.h>
42#include <unistd.h>
43
44int
45main (void) {
46	int fd, i, mx;
47	char file[100];
48
49	snprintf(file, sizeof(file), "f");
50	if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC,
51	    DEFFILEMODE)) == -1)
52		err(1, "creat(%s)", file);
53	close(fd);
54
55	mx = UFS_LINK_MAX - 1; /* UFS_LINK_MAX = 32767 */
56	for (i = 0; i < mx; i++) {
57		snprintf(file, sizeof(file), "%d", i);
58		if (link("f", file) == -1)
59			err(1, "link(%s, %s)", "f", file);
60
61	}
62
63	/* The following link(2) must fail */
64	i = mx;
65	snprintf(file, sizeof(file), "%d", i);
66	if (link("f", file) != -1)
67		err(1, "link(%s, %s)", "f", file);
68	if (errno != EMLINK)
69		err(1, "Must fail: link(%s, %s)", "f", file);
70
71	i = 0;
72	snprintf(file, sizeof(file), "%d", i);
73	if (unlink(file) == -1)
74		err(1, "unlink(%s)", file);
75
76	/* Must succeed */
77	i = mx;
78	snprintf(file, sizeof(file), "%d", i);
79	if (link("f", file) == -1)
80		err(1, "link(%s, %s)", "f", file);
81
82	return (0);
83}
84EOF
85mycc -o /tmp/nlink3 -Wall -Wextra -O2 /tmp/nlink3.c || exit 1
86rm /tmp/nlink3.c
87
88set -e
89here=`pwd`
90mount | grep -q "on $mntpoint " && umount -f $mntpoint
91mdconfig -l | grep "md$mdstart " && mdconfig -d -u $mdstart
92mdconfig -a -t swap -s 1g -u $mdstart
93newfs -Un /dev/md$mdstart > /dev/null
94mount /dev/md$mdstart $mntpoint
95set +e
96
97cd $mntpoint
98/tmp/nlink3; s=$?
99n=`ls -a | wc -l`
100[ $s -ne 0 ] && echo "$n files"
101cd $here
102
103umount $mntpoint
104mdconfig -d -u $mdstart
105rm /tmp/nlink3
106exit $s
107